-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSGILoader.js
134 lines (102 loc) · 2.81 KB
/
SGILoader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { SGILoaderBase } from './SGILoaderBase.js';
import {
DataTexture,
RGBAFormat,
RGFormat,
RedFormat,
UnsignedByteType,
UnsignedShortType,
LinearFilter,
LinearMipmapLinearFilter,
DefaultLoadingManager,
sRGBEncoding,
LinearEncoding,
} from 'three';
/**
* A three.js implementation of SGILoader that returns a data texture rather than raw results.
*/
export class SGILoader extends SGILoaderBase {
/**
* @param {LoadingManager} manager
*/
constructor( manager = DefaultLoadingManager ) {
super();
/**
* @member {LoadingManager}
* @default DefaultLoadingManager
*/
this.manager = manager;
}
/**
* Loads and parses the SGI file and returns a DataTexture. If a DataTexture is passed into
* the function the data is applied to it.
* @param {String} url
* @param {DataTexture} texture
* @returns {DataTexture}
*/
load( url, texture = new DataTexture() ) {
const manager = this.manager;
manager.itemStart( url );
return super.load( url ).then( result => {
texture.copy( result );
texture.needsUpdate = true;
return texture;
} ).catch( err => {
manager.itemError( url, err );
throw err;
} ).finally( () => {
manager.itemEnd( url );
} );
}
/**
* Parses the contents of the given SGI contents and returns a texture.
* @param {ArrayBuffer|Uint8Array} buffer
* @param {DataTexture} texture
* @returns {DataTexture}
*/
parse( buffer, texture = new DataTexture() ) {
let result = buffer;
if ( buffer instanceof ArrayBuffer ) {
result = super.parse( buffer );
}
texture.image.width = result.width;
texture.image.height = result.height;
texture.image.data = result.data;
texture.generateMipmaps = true;
texture.minFilter = LinearMipmapLinearFilter;
texture.magFilter = LinearFilter;
texture.type = result.data.BYTES_PER_ELEMENT === 1 ? UnsignedByteType : UnsignedShortType;
texture.encoding = result.data.BYTES_PER_ELEMENT === 1 ? sRGBEncoding : LinearEncoding;
switch ( result.channels ) {
case 1:
texture.format = RedFormat;
break;
case 2:
texture.format = RGFormat;
break;
case 3: {
// three.js does not support RGBFormat so convert the data to
// 4 channel data.
const { width, height, data } = result;
const newData = new data.constructor( width * height * 4 );
const maxValue = Math.pow( 2, newData.BYTES_PER_ELEMENT * 8 );
for ( let i = 0, l = width * height; i < l; i ++ ) {
const i3 = 3 * i;
const i4 = 4 * i;
newData[ i4 + 0 ] = data[ i3 + 0 ];
newData[ i4 + 1 ] = data[ i3 + 1 ];
newData[ i4 + 2 ] = data[ i3 + 2 ];
newData[ i4 + 3 ] = maxValue;
}
texture.format = RGBAFormat;
texture.image.data = newData;
break;
}
case 4:
texture.format = RGBAFormat;
break;
}
texture.needsUpdate = true;
return texture;
}
}