three-gpu-pathtracer 0.0.5 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +815 -728
- package/build/index.module.js +5474 -3706
- package/build/index.module.js.map +1 -1
- package/build/index.umd.cjs +5479 -3704
- package/build/index.umd.cjs.map +1 -1
- package/package.json +68 -60
- package/src/core/DynamicPathTracingSceneGenerator.js +119 -111
- package/src/core/MaterialReducer.js +256 -256
- package/src/core/PathTracingRenderer.js +270 -255
- package/src/core/PathTracingSceneGenerator.js +4 -3
- package/src/index.js +35 -26
- package/src/materials/AlphaDisplayMaterial.js +48 -48
- package/src/materials/AmbientOcclusionMaterial.js +197 -197
- package/src/materials/BlendMaterial.js +67 -67
- package/src/materials/DenoiseMaterial.js +142 -0
- package/src/materials/GraphMaterial.js +243 -0
- package/src/materials/LambertPathTracingMaterial.js +285 -285
- package/src/materials/MaterialBase.js +56 -56
- package/src/materials/PhysicalPathTracingMaterial.js +970 -848
- package/src/{core → objects}/EquirectCamera.js +13 -13
- package/src/{core → objects}/PhysicalCamera.js +28 -28
- package/src/objects/PhysicalSpotLight.js +14 -0
- package/src/objects/ShapedAreaLight.js +12 -0
- package/src/shader/shaderEnvMapSampling.js +59 -59
- package/src/shader/shaderGGXFunctions.js +100 -108
- package/src/shader/shaderIridescenceFunctions.js +130 -0
- package/src/shader/shaderLightSampling.js +231 -87
- package/src/shader/shaderMaterialSampling.js +542 -501
- package/src/shader/shaderSheenFunctions.js +98 -0
- package/src/shader/shaderStructs.js +321 -191
- package/src/shader/shaderUtils.js +364 -287
- package/src/uniforms/EquirectHdrInfoUniform.js +259 -263
- package/src/uniforms/IESProfilesTexture.js +100 -0
- package/src/uniforms/LightsInfoUniformStruct.js +162 -0
- package/src/uniforms/MaterialsTexture.js +426 -319
- package/src/uniforms/PhysicalCameraUniform.js +36 -36
- package/src/uniforms/RenderTarget2DArray.js +93 -93
- package/src/utils/BlurredEnvMapGenerator.js +113 -113
- package/src/utils/GeometryPreparationUtils.js +21 -1
- package/src/utils/IESLoader.js +325 -0
- package/src/utils/UVUnwrapper.js +101 -101
- package/src/workers/PathTracingSceneWorker.js +42 -41
- package/src/uniforms/LightsTexture.js +0 -83
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DataTexture,
|
|
3
|
+
FileLoader,
|
|
4
|
+
FloatType,
|
|
5
|
+
LinearFilter,
|
|
6
|
+
RedFormat,
|
|
7
|
+
MathUtils,
|
|
8
|
+
Loader,
|
|
9
|
+
} from 'three';
|
|
10
|
+
|
|
11
|
+
function IESLamp( text ) {
|
|
12
|
+
|
|
13
|
+
const _self = this;
|
|
14
|
+
|
|
15
|
+
const textArray = text.split( '\n' );
|
|
16
|
+
|
|
17
|
+
let lineNumber = 0;
|
|
18
|
+
let line;
|
|
19
|
+
|
|
20
|
+
_self.verAngles = [ ];
|
|
21
|
+
_self.horAngles = [ ];
|
|
22
|
+
|
|
23
|
+
_self.candelaValues = [ ];
|
|
24
|
+
|
|
25
|
+
_self.tiltData = { };
|
|
26
|
+
_self.tiltData.angles = [ ];
|
|
27
|
+
_self.tiltData.mulFactors = [ ];
|
|
28
|
+
|
|
29
|
+
function textToArray( text ) {
|
|
30
|
+
|
|
31
|
+
text = text.replace( /^\s+|\s+$/g, '' ); // remove leading or trailing spaces
|
|
32
|
+
text = text.replace( /,/g, ' ' ); // replace commas with spaces
|
|
33
|
+
text = text.replace( /\s\s+/g, ' ' ); // replace white space/tabs etc by single whitespace
|
|
34
|
+
|
|
35
|
+
const array = text.split( ' ' );
|
|
36
|
+
|
|
37
|
+
return array;
|
|
38
|
+
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function readArray( count, array ) {
|
|
42
|
+
|
|
43
|
+
while ( true ) {
|
|
44
|
+
|
|
45
|
+
const line = textArray[ lineNumber ++ ];
|
|
46
|
+
const lineData = textToArray( line );
|
|
47
|
+
|
|
48
|
+
for ( let i = 0; i < lineData.length; ++ i ) {
|
|
49
|
+
|
|
50
|
+
array.push( Number( lineData[ i ] ) );
|
|
51
|
+
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if ( array.length === count )
|
|
55
|
+
break;
|
|
56
|
+
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function readTilt() {
|
|
62
|
+
|
|
63
|
+
let line = textArray[ lineNumber ++ ];
|
|
64
|
+
let lineData = textToArray( line );
|
|
65
|
+
|
|
66
|
+
_self.tiltData.lampToLumGeometry = Number( lineData[ 0 ] );
|
|
67
|
+
|
|
68
|
+
line = textArray[ lineNumber ++ ];
|
|
69
|
+
lineData = textToArray( line );
|
|
70
|
+
|
|
71
|
+
_self.tiltData.numAngles = Number( lineData[ 0 ] );
|
|
72
|
+
|
|
73
|
+
readArray( _self.tiltData.numAngles, _self.tiltData.angles );
|
|
74
|
+
readArray( _self.tiltData.numAngles, _self.tiltData.mulFactors );
|
|
75
|
+
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function readLampValues() {
|
|
79
|
+
|
|
80
|
+
const values = [ ];
|
|
81
|
+
readArray( 10, values );
|
|
82
|
+
|
|
83
|
+
_self.count = Number( values[ 0 ] );
|
|
84
|
+
_self.lumens = Number( values[ 1 ] );
|
|
85
|
+
_self.multiplier = Number( values[ 2 ] );
|
|
86
|
+
_self.numVerAngles = Number( values[ 3 ] );
|
|
87
|
+
_self.numHorAngles = Number( values[ 4 ] );
|
|
88
|
+
_self.gonioType = Number( values[ 5 ] );
|
|
89
|
+
_self.units = Number( values[ 6 ] );
|
|
90
|
+
_self.width = Number( values[ 7 ] );
|
|
91
|
+
_self.length = Number( values[ 8 ] );
|
|
92
|
+
_self.height = Number( values[ 9 ] );
|
|
93
|
+
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function readLampFactors() {
|
|
97
|
+
|
|
98
|
+
const values = [ ];
|
|
99
|
+
readArray( 3, values );
|
|
100
|
+
|
|
101
|
+
_self.ballFactor = Number( values[ 0 ] );
|
|
102
|
+
_self.blpFactor = Number( values[ 1 ] );
|
|
103
|
+
_self.inputWatts = Number( values[ 2 ] );
|
|
104
|
+
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
while ( true ) {
|
|
108
|
+
|
|
109
|
+
line = textArray[ lineNumber ++ ];
|
|
110
|
+
|
|
111
|
+
if ( line.includes( 'TILT' ) ) {
|
|
112
|
+
|
|
113
|
+
break;
|
|
114
|
+
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if ( ! line.includes( 'NONE' ) ) {
|
|
120
|
+
|
|
121
|
+
if ( line.includes( 'INCLUDE' ) ) {
|
|
122
|
+
|
|
123
|
+
readTilt();
|
|
124
|
+
|
|
125
|
+
} else {
|
|
126
|
+
|
|
127
|
+
// TODO:: Read tilt data from a file
|
|
128
|
+
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
readLampValues();
|
|
134
|
+
|
|
135
|
+
readLampFactors();
|
|
136
|
+
|
|
137
|
+
// Initialize candela value array
|
|
138
|
+
for ( let i = 0; i < _self.numHorAngles; ++ i ) {
|
|
139
|
+
|
|
140
|
+
_self.candelaValues.push( [ ] );
|
|
141
|
+
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Parse Angles
|
|
145
|
+
readArray( _self.numVerAngles, _self.verAngles );
|
|
146
|
+
readArray( _self.numHorAngles, _self.horAngles );
|
|
147
|
+
|
|
148
|
+
// Parse Candela values
|
|
149
|
+
for ( let i = 0; i < _self.numHorAngles; ++ i ) {
|
|
150
|
+
|
|
151
|
+
readArray( _self.numVerAngles, _self.candelaValues[ i ] );
|
|
152
|
+
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Calculate actual candela values, and normalize.
|
|
156
|
+
for ( let i = 0; i < _self.numHorAngles; ++ i ) {
|
|
157
|
+
|
|
158
|
+
for ( let j = 0; j < _self.numVerAngles; ++ j ) {
|
|
159
|
+
|
|
160
|
+
_self.candelaValues[ i ][ j ] *= _self.candelaValues[ i ][ j ] * _self.multiplier
|
|
161
|
+
* _self.ballFactor * _self.blpFactor;
|
|
162
|
+
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
let maxVal = - 1;
|
|
168
|
+
for ( let i = 0; i < _self.numHorAngles; ++ i ) {
|
|
169
|
+
|
|
170
|
+
for ( let j = 0; j < _self.numVerAngles; ++ j ) {
|
|
171
|
+
|
|
172
|
+
const value = _self.candelaValues[ i ][ j ];
|
|
173
|
+
maxVal = maxVal < value ? value : maxVal;
|
|
174
|
+
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const bNormalize = true;
|
|
180
|
+
if ( bNormalize && maxVal > 0 ) {
|
|
181
|
+
|
|
182
|
+
for ( let i = 0; i < _self.numHorAngles; ++ i ) {
|
|
183
|
+
|
|
184
|
+
for ( let j = 0; j < _self.numVerAngles; ++ j ) {
|
|
185
|
+
|
|
186
|
+
_self.candelaValues[ i ][ j ] /= maxVal;
|
|
187
|
+
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export class IESLoader extends Loader {
|
|
197
|
+
|
|
198
|
+
_getIESValues( iesLamp ) {
|
|
199
|
+
|
|
200
|
+
const width = 360;
|
|
201
|
+
const height = 180;
|
|
202
|
+
const size = width * height;
|
|
203
|
+
|
|
204
|
+
const data = new Float32Array( size );
|
|
205
|
+
|
|
206
|
+
function interpolateCandelaValues( phi, theta ) {
|
|
207
|
+
|
|
208
|
+
let phiIndex = 0, thetaIndex = 0;
|
|
209
|
+
let startTheta = 0, endTheta = 0, startPhi = 0, endPhi = 0;
|
|
210
|
+
|
|
211
|
+
for ( let i = 0; i < iesLamp.numHorAngles - 1; ++ i ) { // numHorAngles = horAngles.length-1 because of extra padding, so this wont cause an out of bounds error
|
|
212
|
+
|
|
213
|
+
if ( theta < iesLamp.horAngles[ i + 1 ] || i == iesLamp.numHorAngles - 2 ) {
|
|
214
|
+
|
|
215
|
+
thetaIndex = i;
|
|
216
|
+
startTheta = iesLamp.horAngles[ i ];
|
|
217
|
+
endTheta = iesLamp.horAngles[ i + 1 ];
|
|
218
|
+
|
|
219
|
+
break;
|
|
220
|
+
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
for ( let i = 0; i < iesLamp.numVerAngles - 1; ++ i ) {
|
|
226
|
+
|
|
227
|
+
if ( phi < iesLamp.verAngles[ i + 1 ] || i == iesLamp.numVerAngles - 2 ) {
|
|
228
|
+
|
|
229
|
+
phiIndex = i;
|
|
230
|
+
startPhi = iesLamp.verAngles[ i ];
|
|
231
|
+
endPhi = iesLamp.verAngles[ i + 1 ];
|
|
232
|
+
|
|
233
|
+
break;
|
|
234
|
+
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
const deltaTheta = endTheta - startTheta;
|
|
240
|
+
const deltaPhi = endPhi - startPhi;
|
|
241
|
+
|
|
242
|
+
if ( deltaPhi === 0 ) // Outside range
|
|
243
|
+
return 0;
|
|
244
|
+
|
|
245
|
+
const t1 = deltaTheta === 0 ? 0 : ( theta - startTheta ) / deltaTheta;
|
|
246
|
+
const t2 = ( phi - startPhi ) / deltaPhi;
|
|
247
|
+
|
|
248
|
+
const nextThetaIndex = deltaTheta === 0 ? thetaIndex : thetaIndex + 1;
|
|
249
|
+
|
|
250
|
+
const v1 = MathUtils.lerp( iesLamp.candelaValues[ thetaIndex ][ phiIndex ], iesLamp.candelaValues[ nextThetaIndex ][ phiIndex ], t1 );
|
|
251
|
+
const v2 = MathUtils.lerp( iesLamp.candelaValues[ thetaIndex ][ phiIndex + 1 ], iesLamp.candelaValues[ nextThetaIndex ][ phiIndex + 1 ], t1 );
|
|
252
|
+
const v = MathUtils.lerp( v1, v2, t2 );
|
|
253
|
+
|
|
254
|
+
return v;
|
|
255
|
+
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
const startTheta = iesLamp.horAngles[ 0 ], endTheta = iesLamp.horAngles[ iesLamp.numHorAngles - 1 ];
|
|
259
|
+
for ( let i = 0; i < size; ++ i ) {
|
|
260
|
+
|
|
261
|
+
let theta = i % width;
|
|
262
|
+
const phi = Math.floor( i / width );
|
|
263
|
+
|
|
264
|
+
if ( endTheta - startTheta !== 0 && ( theta < startTheta || theta >= endTheta ) ) { // Handle symmetry for hor angles
|
|
265
|
+
|
|
266
|
+
theta %= endTheta * 2;
|
|
267
|
+
if ( theta > endTheta )
|
|
268
|
+
theta = endTheta * 2 - theta;
|
|
269
|
+
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
data[ i ] = interpolateCandelaValues( phi, theta );
|
|
273
|
+
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
return data;
|
|
277
|
+
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
load( url, onLoad, onProgress, onError ) {
|
|
281
|
+
|
|
282
|
+
const loader = new FileLoader( this.manager );
|
|
283
|
+
loader.setResponseType( 'text' );
|
|
284
|
+
loader.setCrossOrigin( this.crossOrigin );
|
|
285
|
+
loader.setWithCredentials( this.withCredentials );
|
|
286
|
+
loader.setPath( this.path );
|
|
287
|
+
loader.setRequestHeader( this.requestHeader );
|
|
288
|
+
|
|
289
|
+
const texture = new DataTexture( null, 360, 180, RedFormat, FloatType );
|
|
290
|
+
texture.minFilter = LinearFilter;
|
|
291
|
+
texture.magFilter = LinearFilter;
|
|
292
|
+
|
|
293
|
+
loader.load( url, text => {
|
|
294
|
+
|
|
295
|
+
const iesLamp = new IESLamp( text );
|
|
296
|
+
|
|
297
|
+
texture.image.data = this._getIESValues( iesLamp );
|
|
298
|
+
texture.needsUpdate = true;
|
|
299
|
+
|
|
300
|
+
if ( onLoad !== undefined ) {
|
|
301
|
+
|
|
302
|
+
onLoad( texture );
|
|
303
|
+
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
}, onProgress, onError );
|
|
307
|
+
|
|
308
|
+
return texture;
|
|
309
|
+
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
parse( text ) {
|
|
313
|
+
|
|
314
|
+
const iesLamp = new IESLamp( text );
|
|
315
|
+
const texture = new DataTexture( null, 360, 180, RedFormat, FloatType );
|
|
316
|
+
texture.minFilter = LinearFilter;
|
|
317
|
+
texture.magFilter = LinearFilter;
|
|
318
|
+
texture.image.data = this._getIESValues( iesLamp );
|
|
319
|
+
texture.needsUpdate = true;
|
|
320
|
+
|
|
321
|
+
return texture;
|
|
322
|
+
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
}
|
package/src/utils/UVUnwrapper.js
CHANGED
|
@@ -1,101 +1,101 @@
|
|
|
1
|
-
// https://github.com/mozilla/Spoke/commit/9701d647020e09d584885bd457eb225e9995c12f
|
|
2
|
-
import XAtlas from 'xatlas-web';
|
|
3
|
-
import { BufferGeometry, Float32BufferAttribute, Uint32BufferAttribute } from 'three';
|
|
4
|
-
|
|
5
|
-
export class UVUnwrapper {
|
|
6
|
-
|
|
7
|
-
constructor() {
|
|
8
|
-
|
|
9
|
-
this._module = null;
|
|
10
|
-
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
async load() {
|
|
14
|
-
|
|
15
|
-
const wasmurl = new URL( '../../node_modules/xatlas-web/dist/xatlas-web.wasm', import.meta.url );
|
|
16
|
-
this._module = XAtlas( {
|
|
17
|
-
|
|
18
|
-
locateFile( path ) {
|
|
19
|
-
|
|
20
|
-
if ( path.endsWith( '.wasm' ) ) {
|
|
21
|
-
|
|
22
|
-
return wasmurl.toString();
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
return path;
|
|
27
|
-
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
} );
|
|
31
|
-
return this._module.ready;
|
|
32
|
-
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
generate( geometry ) {
|
|
36
|
-
|
|
37
|
-
const xatlas = this._module;
|
|
38
|
-
const originalVertexCount = geometry.attributes.position.count;
|
|
39
|
-
const originalIndexCount = geometry.index.count;
|
|
40
|
-
|
|
41
|
-
xatlas.createAtlas();
|
|
42
|
-
|
|
43
|
-
const meshInfo = xatlas.createMesh( originalVertexCount, originalIndexCount, true, true );
|
|
44
|
-
xatlas.HEAPU16.set( geometry.index.array, meshInfo.indexOffset / Uint16Array.BYTES_PER_ELEMENT );
|
|
45
|
-
xatlas.HEAPF32.set( geometry.attributes.position.array, meshInfo.positionOffset / Float32Array.BYTES_PER_ELEMENT );
|
|
46
|
-
xatlas.HEAPF32.set( geometry.attributes.normal.array, meshInfo.normalOffset / Float32Array.BYTES_PER_ELEMENT );
|
|
47
|
-
xatlas.HEAPF32.set( geometry.attributes.uv.array, meshInfo.uvOffset / Float32Array.BYTES_PER_ELEMENT );
|
|
48
|
-
|
|
49
|
-
const statusCode = xatlas.addMesh();
|
|
50
|
-
if ( statusCode !== AddMeshStatus.Success ) {
|
|
51
|
-
|
|
52
|
-
throw new Error( `UVUnwrapper: Error adding mesh. Status code ${ statusCode }` );
|
|
53
|
-
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
xatlas.generateAtlas();
|
|
57
|
-
|
|
58
|
-
const meshData = xatlas.getMeshData( meshInfo.meshId );
|
|
59
|
-
const oldPositionArray = geometry.attributes.position.array;
|
|
60
|
-
const oldNormalArray = geometry.attributes.normal.array;
|
|
61
|
-
const oldUvArray = geometry.attributes.uv.array;
|
|
62
|
-
|
|
63
|
-
const newPositionArray = new Float32Array( meshData.newVertexCount * 3 );
|
|
64
|
-
const newNormalArray = new Float32Array( meshData.newVertexCount * 3 );
|
|
65
|
-
const newUvArray = new Float32Array( meshData.newVertexCount * 2 );
|
|
66
|
-
const newUv2Array = new Float32Array( xatlas.HEAPF32.buffer, meshData.uvOffset, meshData.newVertexCount * 2 );
|
|
67
|
-
const newIndexArray = new Uint32Array( xatlas.HEAPU32.buffer, meshData.indexOffset, meshData.newIndexCount );
|
|
68
|
-
const originalIndexArray = new Uint32Array(
|
|
69
|
-
xatlas.HEAPU32.buffer,
|
|
70
|
-
meshData.originalIndexOffset,
|
|
71
|
-
meshData.newVertexCount
|
|
72
|
-
);
|
|
73
|
-
|
|
74
|
-
for ( let i = 0; i < meshData.newVertexCount; i ++ ) {
|
|
75
|
-
|
|
76
|
-
const originalIndex = originalIndexArray[ i ];
|
|
77
|
-
newPositionArray[ i * 3 ] = oldPositionArray[ originalIndex * 3 ];
|
|
78
|
-
newPositionArray[ i * 3 + 1 ] = oldPositionArray[ originalIndex * 3 + 1 ];
|
|
79
|
-
newPositionArray[ i * 3 + 2 ] = oldPositionArray[ originalIndex * 3 + 2 ];
|
|
80
|
-
newNormalArray[ i * 3 ] = oldNormalArray[ originalIndex * 3 ];
|
|
81
|
-
newNormalArray[ i * 3 + 1 ] = oldNormalArray[ originalIndex * 3 + 1 ];
|
|
82
|
-
newNormalArray[ i * 3 + 2 ] = oldNormalArray[ originalIndex * 3 + 2 ];
|
|
83
|
-
newUvArray[ i * 2 ] = oldUvArray[ originalIndex * 2 ];
|
|
84
|
-
newUvArray[ i * 2 + 1 ] = oldUvArray[ originalIndex * 2 + 1 ];
|
|
85
|
-
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
const newGeometry = new BufferGeometry();
|
|
89
|
-
newGeometry.addAttribute( 'position', new Float32BufferAttribute( newPositionArray, 3 ) );
|
|
90
|
-
newGeometry.addAttribute( 'normal', new Float32BufferAttribute( newNormalArray, 3 ) );
|
|
91
|
-
newGeometry.addAttribute( 'uv', new Float32BufferAttribute( newUvArray, 2 ) );
|
|
92
|
-
newGeometry.addAttribute( 'uv2', new Float32BufferAttribute( newUv2Array, 2 ) );
|
|
93
|
-
newGeometry.setIndex( new Uint32BufferAttribute( newIndexArray, 1 ) );
|
|
94
|
-
|
|
95
|
-
mesh.geometry = newGeometry;
|
|
96
|
-
|
|
97
|
-
xatlas.destroyAtlas();
|
|
98
|
-
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
}
|
|
1
|
+
// https://github.com/mozilla/Spoke/commit/9701d647020e09d584885bd457eb225e9995c12f
|
|
2
|
+
import XAtlas from 'xatlas-web';
|
|
3
|
+
import { BufferGeometry, Float32BufferAttribute, Uint32BufferAttribute } from 'three';
|
|
4
|
+
|
|
5
|
+
export class UVUnwrapper {
|
|
6
|
+
|
|
7
|
+
constructor() {
|
|
8
|
+
|
|
9
|
+
this._module = null;
|
|
10
|
+
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
async load() {
|
|
14
|
+
|
|
15
|
+
const wasmurl = new URL( '../../node_modules/xatlas-web/dist/xatlas-web.wasm', import.meta.url );
|
|
16
|
+
this._module = XAtlas( {
|
|
17
|
+
|
|
18
|
+
locateFile( path ) {
|
|
19
|
+
|
|
20
|
+
if ( path.endsWith( '.wasm' ) ) {
|
|
21
|
+
|
|
22
|
+
return wasmurl.toString();
|
|
23
|
+
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return path;
|
|
27
|
+
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
} );
|
|
31
|
+
return this._module.ready;
|
|
32
|
+
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
generate( geometry ) {
|
|
36
|
+
|
|
37
|
+
const xatlas = this._module;
|
|
38
|
+
const originalVertexCount = geometry.attributes.position.count;
|
|
39
|
+
const originalIndexCount = geometry.index.count;
|
|
40
|
+
|
|
41
|
+
xatlas.createAtlas();
|
|
42
|
+
|
|
43
|
+
const meshInfo = xatlas.createMesh( originalVertexCount, originalIndexCount, true, true );
|
|
44
|
+
xatlas.HEAPU16.set( geometry.index.array, meshInfo.indexOffset / Uint16Array.BYTES_PER_ELEMENT );
|
|
45
|
+
xatlas.HEAPF32.set( geometry.attributes.position.array, meshInfo.positionOffset / Float32Array.BYTES_PER_ELEMENT );
|
|
46
|
+
xatlas.HEAPF32.set( geometry.attributes.normal.array, meshInfo.normalOffset / Float32Array.BYTES_PER_ELEMENT );
|
|
47
|
+
xatlas.HEAPF32.set( geometry.attributes.uv.array, meshInfo.uvOffset / Float32Array.BYTES_PER_ELEMENT );
|
|
48
|
+
|
|
49
|
+
const statusCode = xatlas.addMesh();
|
|
50
|
+
if ( statusCode !== AddMeshStatus.Success ) {
|
|
51
|
+
|
|
52
|
+
throw new Error( `UVUnwrapper: Error adding mesh. Status code ${ statusCode }` );
|
|
53
|
+
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
xatlas.generateAtlas();
|
|
57
|
+
|
|
58
|
+
const meshData = xatlas.getMeshData( meshInfo.meshId );
|
|
59
|
+
const oldPositionArray = geometry.attributes.position.array;
|
|
60
|
+
const oldNormalArray = geometry.attributes.normal.array;
|
|
61
|
+
const oldUvArray = geometry.attributes.uv.array;
|
|
62
|
+
|
|
63
|
+
const newPositionArray = new Float32Array( meshData.newVertexCount * 3 );
|
|
64
|
+
const newNormalArray = new Float32Array( meshData.newVertexCount * 3 );
|
|
65
|
+
const newUvArray = new Float32Array( meshData.newVertexCount * 2 );
|
|
66
|
+
const newUv2Array = new Float32Array( xatlas.HEAPF32.buffer, meshData.uvOffset, meshData.newVertexCount * 2 );
|
|
67
|
+
const newIndexArray = new Uint32Array( xatlas.HEAPU32.buffer, meshData.indexOffset, meshData.newIndexCount );
|
|
68
|
+
const originalIndexArray = new Uint32Array(
|
|
69
|
+
xatlas.HEAPU32.buffer,
|
|
70
|
+
meshData.originalIndexOffset,
|
|
71
|
+
meshData.newVertexCount
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
for ( let i = 0; i < meshData.newVertexCount; i ++ ) {
|
|
75
|
+
|
|
76
|
+
const originalIndex = originalIndexArray[ i ];
|
|
77
|
+
newPositionArray[ i * 3 ] = oldPositionArray[ originalIndex * 3 ];
|
|
78
|
+
newPositionArray[ i * 3 + 1 ] = oldPositionArray[ originalIndex * 3 + 1 ];
|
|
79
|
+
newPositionArray[ i * 3 + 2 ] = oldPositionArray[ originalIndex * 3 + 2 ];
|
|
80
|
+
newNormalArray[ i * 3 ] = oldNormalArray[ originalIndex * 3 ];
|
|
81
|
+
newNormalArray[ i * 3 + 1 ] = oldNormalArray[ originalIndex * 3 + 1 ];
|
|
82
|
+
newNormalArray[ i * 3 + 2 ] = oldNormalArray[ originalIndex * 3 + 2 ];
|
|
83
|
+
newUvArray[ i * 2 ] = oldUvArray[ originalIndex * 2 ];
|
|
84
|
+
newUvArray[ i * 2 + 1 ] = oldUvArray[ originalIndex * 2 + 1 ];
|
|
85
|
+
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const newGeometry = new BufferGeometry();
|
|
89
|
+
newGeometry.addAttribute( 'position', new Float32BufferAttribute( newPositionArray, 3 ) );
|
|
90
|
+
newGeometry.addAttribute( 'normal', new Float32BufferAttribute( newNormalArray, 3 ) );
|
|
91
|
+
newGeometry.addAttribute( 'uv', new Float32BufferAttribute( newUvArray, 2 ) );
|
|
92
|
+
newGeometry.addAttribute( 'uv2', new Float32BufferAttribute( newUv2Array, 2 ) );
|
|
93
|
+
newGeometry.setIndex( new Uint32BufferAttribute( newIndexArray, 1 ) );
|
|
94
|
+
|
|
95
|
+
mesh.geometry = newGeometry;
|
|
96
|
+
|
|
97
|
+
xatlas.destroyAtlas();
|
|
98
|
+
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
}
|
|
@@ -1,41 +1,42 @@
|
|
|
1
|
-
import { PathTracingSceneGenerator } from '../core/PathTracingSceneGenerator.js';
|
|
2
|
-
import { SAH } from 'three-mesh-bvh';
|
|
3
|
-
import { GenerateMeshBVHWorker } from 'three-mesh-bvh/src/workers/GenerateMeshBVHWorker.js';
|
|
4
|
-
|
|
5
|
-
export class PathTracingSceneWorker extends PathTracingSceneGenerator {
|
|
6
|
-
|
|
7
|
-
constructor() {
|
|
8
|
-
|
|
9
|
-
super();
|
|
10
|
-
this.bvhGenerator = new GenerateMeshBVHWorker();
|
|
11
|
-
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
generate( scene, options = {} ) {
|
|
15
|
-
|
|
16
|
-
const { bvhGenerator } = this;
|
|
17
|
-
const { geometry, materials, textures, lights } = this.prepScene( scene );
|
|
18
|
-
|
|
19
|
-
const bvhOptions = { strategy: SAH, ...options, maxLeafTris: 1 };
|
|
20
|
-
const bvhPromise = bvhGenerator.generate( geometry, bvhOptions );
|
|
21
|
-
return bvhPromise.then( bvh => {
|
|
22
|
-
|
|
23
|
-
return {
|
|
24
|
-
scene,
|
|
25
|
-
materials,
|
|
26
|
-
textures,
|
|
27
|
-
lights,
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
1
|
+
import { PathTracingSceneGenerator } from '../core/PathTracingSceneGenerator.js';
|
|
2
|
+
import { SAH } from 'three-mesh-bvh';
|
|
3
|
+
import { GenerateMeshBVHWorker } from 'three-mesh-bvh/src/workers/GenerateMeshBVHWorker.js';
|
|
4
|
+
|
|
5
|
+
export class PathTracingSceneWorker extends PathTracingSceneGenerator {
|
|
6
|
+
|
|
7
|
+
constructor() {
|
|
8
|
+
|
|
9
|
+
super();
|
|
10
|
+
this.bvhGenerator = new GenerateMeshBVHWorker();
|
|
11
|
+
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
generate( scene, options = {} ) {
|
|
15
|
+
|
|
16
|
+
const { bvhGenerator } = this;
|
|
17
|
+
const { geometry, materials, textures, lights, spotLights } = this.prepScene( scene );
|
|
18
|
+
|
|
19
|
+
const bvhOptions = { strategy: SAH, ...options, maxLeafTris: 1 };
|
|
20
|
+
const bvhPromise = bvhGenerator.generate( geometry, bvhOptions );
|
|
21
|
+
return bvhPromise.then( bvh => {
|
|
22
|
+
|
|
23
|
+
return {
|
|
24
|
+
scene,
|
|
25
|
+
materials,
|
|
26
|
+
textures,
|
|
27
|
+
lights,
|
|
28
|
+
spotLights,
|
|
29
|
+
bvh,
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
} );
|
|
33
|
+
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
dispose() {
|
|
37
|
+
|
|
38
|
+
this.bvhGenerator.dispose();
|
|
39
|
+
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
}
|
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
import { DataTexture, RGBAFormat, ClampToEdgeWrapping, FloatType, Vector3, Quaternion } from 'three';
|
|
2
|
-
|
|
3
|
-
const LIGHT_PIXELS = 4;
|
|
4
|
-
|
|
5
|
-
export class LightsTexture extends DataTexture {
|
|
6
|
-
|
|
7
|
-
constructor() {
|
|
8
|
-
|
|
9
|
-
super( new Float32Array( 4 ), 1, 1 );
|
|
10
|
-
|
|
11
|
-
this.format = RGBAFormat;
|
|
12
|
-
this.type = FloatType;
|
|
13
|
-
this.wrapS = ClampToEdgeWrapping;
|
|
14
|
-
this.wrapT = ClampToEdgeWrapping;
|
|
15
|
-
this.generateMipmaps = false;
|
|
16
|
-
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
updateFrom( lights ) {
|
|
20
|
-
|
|
21
|
-
let index = 0;
|
|
22
|
-
const pixelCount = lights.length * LIGHT_PIXELS;
|
|
23
|
-
const dimension = Math.ceil( Math.sqrt( pixelCount ) );
|
|
24
|
-
|
|
25
|
-
if ( this.image.width !== dimension ) {
|
|
26
|
-
|
|
27
|
-
this.dispose();
|
|
28
|
-
|
|
29
|
-
this.image.data = new Float32Array( dimension * dimension * 4 );
|
|
30
|
-
this.image.width = dimension;
|
|
31
|
-
this.image.height = dimension;
|
|
32
|
-
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const floatArray = this.image.data;
|
|
36
|
-
|
|
37
|
-
const u = new Vector3();
|
|
38
|
-
const v = new Vector3();
|
|
39
|
-
const worldQuaternion = new Quaternion();
|
|
40
|
-
|
|
41
|
-
for ( let i = 0, l = lights.length; i < l; i ++ ) {
|
|
42
|
-
|
|
43
|
-
const l = lights[ i ];
|
|
44
|
-
|
|
45
|
-
// position
|
|
46
|
-
l.getWorldPosition( v );
|
|
47
|
-
floatArray[ index ++ ] = v.x;
|
|
48
|
-
floatArray[ index ++ ] = v.y;
|
|
49
|
-
floatArray[ index ++ ] = v.z;
|
|
50
|
-
index ++;
|
|
51
|
-
|
|
52
|
-
// color
|
|
53
|
-
floatArray[ index ++ ] = l.color.r;
|
|
54
|
-
floatArray[ index ++ ] = l.color.g;
|
|
55
|
-
floatArray[ index ++ ] = l.color.b;
|
|
56
|
-
|
|
57
|
-
// intensity
|
|
58
|
-
floatArray[ index ++ ] = l.intensity;
|
|
59
|
-
|
|
60
|
-
// u vector
|
|
61
|
-
l.getWorldQuaternion( worldQuaternion );
|
|
62
|
-
u.set( l.width, 0, 0 ).applyQuaternion( worldQuaternion );
|
|
63
|
-
floatArray[ index ++ ] = u.x;
|
|
64
|
-
floatArray[ index ++ ] = u.y;
|
|
65
|
-
floatArray[ index ++ ] = u.z;
|
|
66
|
-
index ++;
|
|
67
|
-
|
|
68
|
-
// v vector
|
|
69
|
-
v.set( 0, l.height, 0 ).applyQuaternion( worldQuaternion );
|
|
70
|
-
floatArray[ index ++ ] = v.x;
|
|
71
|
-
floatArray[ index ++ ] = v.y;
|
|
72
|
-
floatArray[ index ++ ] = v.z;
|
|
73
|
-
|
|
74
|
-
// area
|
|
75
|
-
floatArray[ index ++ ] = u.cross( v ).length();
|
|
76
|
-
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
this.needsUpdate = true;
|
|
80
|
-
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
}
|