three-gpu-pathtracer 0.0.5 → 0.0.6
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 +781 -728
- package/build/index.module.js +5223 -3956
- package/build/index.module.js.map +1 -1
- package/build/index.umd.cjs +5226 -3954
- 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 +255 -255
- package/src/core/PathTracingSceneGenerator.js +68 -68
- package/src/index.js +33 -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/LambertPathTracingMaterial.js +285 -285
- package/src/materials/MaterialBase.js +56 -56
- package/src/materials/PhysicalPathTracingMaterial.js +922 -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 +108 -108
- package/src/shader/shaderIridescenceFunctions.js +130 -0
- package/src/shader/shaderLightSampling.js +231 -87
- package/src/shader/shaderMaterialSampling.js +546 -501
- package/src/shader/shaderSheenFunctions.js +98 -0
- package/src/shader/shaderStructs.js +307 -191
- package/src/shader/shaderUtils.js +350 -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 +406 -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 +194 -194
- 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
|
@@ -1,194 +1,194 @@
|
|
|
1
|
-
import { BufferAttribute } from 'three';
|
|
2
|
-
import { mergeBufferGeometries, mergeVertices } from 'three/examples/jsm/utils/BufferGeometryUtils.js';
|
|
3
|
-
export function getGroupMaterialIndicesAttribute( geometry, materials, allMaterials ) {
|
|
4
|
-
|
|
5
|
-
const indexAttr = geometry.index;
|
|
6
|
-
const posAttr = geometry.attributes.position;
|
|
7
|
-
const vertCount = posAttr.count;
|
|
8
|
-
const materialArray = new Uint8Array( vertCount );
|
|
9
|
-
const totalCount = indexAttr ? indexAttr.count : vertCount;
|
|
10
|
-
let groups = geometry.groups;
|
|
11
|
-
if ( groups.length === 0 ) {
|
|
12
|
-
|
|
13
|
-
groups = [ { count: totalCount, start: 0, materialIndex: 0 } ];
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
for ( let i = 0; i < groups.length; i ++ ) {
|
|
18
|
-
|
|
19
|
-
const group = groups[ i ];
|
|
20
|
-
const start = group.start;
|
|
21
|
-
const count = group.count;
|
|
22
|
-
const endCount = Math.min( count, totalCount - start );
|
|
23
|
-
|
|
24
|
-
const mat = Array.isArray( materials ) ? materials[ group.materialIndex ] : materials;
|
|
25
|
-
const materialIndex = allMaterials.indexOf( mat );
|
|
26
|
-
|
|
27
|
-
for ( let j = 0; j < endCount; j ++ ) {
|
|
28
|
-
|
|
29
|
-
let index = start + j;
|
|
30
|
-
if ( indexAttr ) {
|
|
31
|
-
|
|
32
|
-
index = indexAttr.getX( index );
|
|
33
|
-
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
materialArray[ index ] = materialIndex;
|
|
37
|
-
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
return new BufferAttribute( materialArray, 1, false );
|
|
43
|
-
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export function trimToAttributes( geometry, attributes ) {
|
|
47
|
-
|
|
48
|
-
// trim any unneeded attributes
|
|
49
|
-
if ( attributes ) {
|
|
50
|
-
|
|
51
|
-
for ( const key in geometry.attributes ) {
|
|
52
|
-
|
|
53
|
-
if ( ! attributes.includes( key ) ) {
|
|
54
|
-
|
|
55
|
-
geometry.deleteAttribute( key );
|
|
56
|
-
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export function setCommonAttributes( geometry, options ) {
|
|
66
|
-
|
|
67
|
-
const { attributes = [], normalMapRequired = false } = options;
|
|
68
|
-
|
|
69
|
-
if ( ! geometry.attributes.normal && ( attributes && attributes.includes( 'normal' ) ) ) {
|
|
70
|
-
|
|
71
|
-
geometry.computeVertexNormals();
|
|
72
|
-
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
if ( ! geometry.attributes.uv && ( attributes && attributes.includes( 'uv' ) ) ) {
|
|
76
|
-
|
|
77
|
-
const vertCount = geometry.attributes.position.count;
|
|
78
|
-
geometry.setAttribute( 'uv', new BufferAttribute( new Float32Array( vertCount * 2 ), 2, false ) );
|
|
79
|
-
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
if ( ! geometry.attributes.tangent && ( attributes && attributes.includes( 'tangent' ) ) ) {
|
|
83
|
-
|
|
84
|
-
if ( normalMapRequired ) {
|
|
85
|
-
|
|
86
|
-
// computeTangents requires an index buffer
|
|
87
|
-
if ( geometry.index === null ) {
|
|
88
|
-
|
|
89
|
-
geometry = mergeVertices( geometry );
|
|
90
|
-
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
geometry.computeTangents();
|
|
94
|
-
|
|
95
|
-
} else {
|
|
96
|
-
|
|
97
|
-
const vertCount = geometry.attributes.position.count;
|
|
98
|
-
geometry.setAttribute( 'tangent', new BufferAttribute( new Float32Array( vertCount * 4 ), 4, false ) );
|
|
99
|
-
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
if ( ! geometry.index ) {
|
|
105
|
-
|
|
106
|
-
// TODO: compute a typed array
|
|
107
|
-
const indexCount = geometry.attributes.position.count;
|
|
108
|
-
const array = new Array( indexCount );
|
|
109
|
-
for ( let i = 0; i < indexCount; i ++ ) {
|
|
110
|
-
|
|
111
|
-
array[ i ] = i;
|
|
112
|
-
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
geometry.setIndex( array );
|
|
116
|
-
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
export function mergeMeshes( meshes, options = {} ) {
|
|
122
|
-
|
|
123
|
-
options = { attributes: null, cloneGeometry: true, ...options };
|
|
124
|
-
|
|
125
|
-
const transformedGeometry = [];
|
|
126
|
-
const materialSet = new Set();
|
|
127
|
-
for ( let i = 0, l = meshes.length; i < l; i ++ ) {
|
|
128
|
-
|
|
129
|
-
// save any materials
|
|
130
|
-
const mesh = meshes[ i ];
|
|
131
|
-
if ( mesh.visible === false ) continue;
|
|
132
|
-
|
|
133
|
-
if ( Array.isArray( mesh.material ) ) {
|
|
134
|
-
|
|
135
|
-
mesh.material.forEach( m => materialSet.add( m ) );
|
|
136
|
-
|
|
137
|
-
} else {
|
|
138
|
-
|
|
139
|
-
materialSet.add( mesh.material );
|
|
140
|
-
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
const materials = Array.from( materialSet );
|
|
146
|
-
for ( let i = 0, l = meshes.length; i < l; i ++ ) {
|
|
147
|
-
|
|
148
|
-
// ensure the matrix world is up to date
|
|
149
|
-
const mesh = meshes[ i ];
|
|
150
|
-
if ( mesh.visible === false ) continue;
|
|
151
|
-
|
|
152
|
-
mesh.updateMatrixWorld();
|
|
153
|
-
|
|
154
|
-
// apply the matrix world to the geometry
|
|
155
|
-
const originalGeometry = meshes[ i ].geometry;
|
|
156
|
-
const geometry = options.cloneGeometry ? originalGeometry.clone() : originalGeometry;
|
|
157
|
-
geometry.applyMatrix4( mesh.matrixWorld );
|
|
158
|
-
|
|
159
|
-
// ensure our geometry has common attributes
|
|
160
|
-
setCommonAttributes( geometry, {
|
|
161
|
-
attributes: options.attributes,
|
|
162
|
-
normalMapRequired: ! ! mesh.material.normalMap,
|
|
163
|
-
} );
|
|
164
|
-
trimToAttributes( geometry, options.attributes );
|
|
165
|
-
|
|
166
|
-
// create the material index attribute
|
|
167
|
-
const materialIndexAttribute = getGroupMaterialIndicesAttribute( geometry, mesh.material, materials );
|
|
168
|
-
geometry.setAttribute( 'materialIndex', materialIndexAttribute );
|
|
169
|
-
|
|
170
|
-
transformedGeometry.push( geometry );
|
|
171
|
-
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
const textureSet = new Set();
|
|
175
|
-
materials.forEach( material => {
|
|
176
|
-
|
|
177
|
-
for ( const key in material ) {
|
|
178
|
-
|
|
179
|
-
const value = material[ key ];
|
|
180
|
-
if ( value && value.isTexture ) {
|
|
181
|
-
|
|
182
|
-
textureSet.add( value );
|
|
183
|
-
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
} );
|
|
189
|
-
|
|
190
|
-
const geometry = mergeBufferGeometries( transformedGeometry, false );
|
|
191
|
-
const textures = Array.from( textureSet );
|
|
192
|
-
return { geometry, materials, textures };
|
|
193
|
-
|
|
194
|
-
}
|
|
1
|
+
import { BufferAttribute } from 'three';
|
|
2
|
+
import { mergeBufferGeometries, mergeVertices } from 'three/examples/jsm/utils/BufferGeometryUtils.js';
|
|
3
|
+
export function getGroupMaterialIndicesAttribute( geometry, materials, allMaterials ) {
|
|
4
|
+
|
|
5
|
+
const indexAttr = geometry.index;
|
|
6
|
+
const posAttr = geometry.attributes.position;
|
|
7
|
+
const vertCount = posAttr.count;
|
|
8
|
+
const materialArray = new Uint8Array( vertCount );
|
|
9
|
+
const totalCount = indexAttr ? indexAttr.count : vertCount;
|
|
10
|
+
let groups = geometry.groups;
|
|
11
|
+
if ( groups.length === 0 ) {
|
|
12
|
+
|
|
13
|
+
groups = [ { count: totalCount, start: 0, materialIndex: 0 } ];
|
|
14
|
+
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
for ( let i = 0; i < groups.length; i ++ ) {
|
|
18
|
+
|
|
19
|
+
const group = groups[ i ];
|
|
20
|
+
const start = group.start;
|
|
21
|
+
const count = group.count;
|
|
22
|
+
const endCount = Math.min( count, totalCount - start );
|
|
23
|
+
|
|
24
|
+
const mat = Array.isArray( materials ) ? materials[ group.materialIndex ] : materials;
|
|
25
|
+
const materialIndex = allMaterials.indexOf( mat );
|
|
26
|
+
|
|
27
|
+
for ( let j = 0; j < endCount; j ++ ) {
|
|
28
|
+
|
|
29
|
+
let index = start + j;
|
|
30
|
+
if ( indexAttr ) {
|
|
31
|
+
|
|
32
|
+
index = indexAttr.getX( index );
|
|
33
|
+
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
materialArray[ index ] = materialIndex;
|
|
37
|
+
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return new BufferAttribute( materialArray, 1, false );
|
|
43
|
+
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function trimToAttributes( geometry, attributes ) {
|
|
47
|
+
|
|
48
|
+
// trim any unneeded attributes
|
|
49
|
+
if ( attributes ) {
|
|
50
|
+
|
|
51
|
+
for ( const key in geometry.attributes ) {
|
|
52
|
+
|
|
53
|
+
if ( ! attributes.includes( key ) ) {
|
|
54
|
+
|
|
55
|
+
geometry.deleteAttribute( key );
|
|
56
|
+
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function setCommonAttributes( geometry, options ) {
|
|
66
|
+
|
|
67
|
+
const { attributes = [], normalMapRequired = false } = options;
|
|
68
|
+
|
|
69
|
+
if ( ! geometry.attributes.normal && ( attributes && attributes.includes( 'normal' ) ) ) {
|
|
70
|
+
|
|
71
|
+
geometry.computeVertexNormals();
|
|
72
|
+
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if ( ! geometry.attributes.uv && ( attributes && attributes.includes( 'uv' ) ) ) {
|
|
76
|
+
|
|
77
|
+
const vertCount = geometry.attributes.position.count;
|
|
78
|
+
geometry.setAttribute( 'uv', new BufferAttribute( new Float32Array( vertCount * 2 ), 2, false ) );
|
|
79
|
+
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if ( ! geometry.attributes.tangent && ( attributes && attributes.includes( 'tangent' ) ) ) {
|
|
83
|
+
|
|
84
|
+
if ( normalMapRequired ) {
|
|
85
|
+
|
|
86
|
+
// computeTangents requires an index buffer
|
|
87
|
+
if ( geometry.index === null ) {
|
|
88
|
+
|
|
89
|
+
geometry = mergeVertices( geometry );
|
|
90
|
+
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
geometry.computeTangents();
|
|
94
|
+
|
|
95
|
+
} else {
|
|
96
|
+
|
|
97
|
+
const vertCount = geometry.attributes.position.count;
|
|
98
|
+
geometry.setAttribute( 'tangent', new BufferAttribute( new Float32Array( vertCount * 4 ), 4, false ) );
|
|
99
|
+
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if ( ! geometry.index ) {
|
|
105
|
+
|
|
106
|
+
// TODO: compute a typed array
|
|
107
|
+
const indexCount = geometry.attributes.position.count;
|
|
108
|
+
const array = new Array( indexCount );
|
|
109
|
+
for ( let i = 0; i < indexCount; i ++ ) {
|
|
110
|
+
|
|
111
|
+
array[ i ] = i;
|
|
112
|
+
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
geometry.setIndex( array );
|
|
116
|
+
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export function mergeMeshes( meshes, options = {} ) {
|
|
122
|
+
|
|
123
|
+
options = { attributes: null, cloneGeometry: true, ...options };
|
|
124
|
+
|
|
125
|
+
const transformedGeometry = [];
|
|
126
|
+
const materialSet = new Set();
|
|
127
|
+
for ( let i = 0, l = meshes.length; i < l; i ++ ) {
|
|
128
|
+
|
|
129
|
+
// save any materials
|
|
130
|
+
const mesh = meshes[ i ];
|
|
131
|
+
if ( mesh.visible === false ) continue;
|
|
132
|
+
|
|
133
|
+
if ( Array.isArray( mesh.material ) ) {
|
|
134
|
+
|
|
135
|
+
mesh.material.forEach( m => materialSet.add( m ) );
|
|
136
|
+
|
|
137
|
+
} else {
|
|
138
|
+
|
|
139
|
+
materialSet.add( mesh.material );
|
|
140
|
+
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const materials = Array.from( materialSet );
|
|
146
|
+
for ( let i = 0, l = meshes.length; i < l; i ++ ) {
|
|
147
|
+
|
|
148
|
+
// ensure the matrix world is up to date
|
|
149
|
+
const mesh = meshes[ i ];
|
|
150
|
+
if ( mesh.visible === false ) continue;
|
|
151
|
+
|
|
152
|
+
mesh.updateMatrixWorld();
|
|
153
|
+
|
|
154
|
+
// apply the matrix world to the geometry
|
|
155
|
+
const originalGeometry = meshes[ i ].geometry;
|
|
156
|
+
const geometry = options.cloneGeometry ? originalGeometry.clone() : originalGeometry;
|
|
157
|
+
geometry.applyMatrix4( mesh.matrixWorld );
|
|
158
|
+
|
|
159
|
+
// ensure our geometry has common attributes
|
|
160
|
+
setCommonAttributes( geometry, {
|
|
161
|
+
attributes: options.attributes,
|
|
162
|
+
normalMapRequired: ! ! mesh.material.normalMap,
|
|
163
|
+
} );
|
|
164
|
+
trimToAttributes( geometry, options.attributes );
|
|
165
|
+
|
|
166
|
+
// create the material index attribute
|
|
167
|
+
const materialIndexAttribute = getGroupMaterialIndicesAttribute( geometry, mesh.material, materials );
|
|
168
|
+
geometry.setAttribute( 'materialIndex', materialIndexAttribute );
|
|
169
|
+
|
|
170
|
+
transformedGeometry.push( geometry );
|
|
171
|
+
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const textureSet = new Set();
|
|
175
|
+
materials.forEach( material => {
|
|
176
|
+
|
|
177
|
+
for ( const key in material ) {
|
|
178
|
+
|
|
179
|
+
const value = material[ key ];
|
|
180
|
+
if ( value && value.isTexture ) {
|
|
181
|
+
|
|
182
|
+
textureSet.add( value );
|
|
183
|
+
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
} );
|
|
189
|
+
|
|
190
|
+
const geometry = mergeBufferGeometries( transformedGeometry, false );
|
|
191
|
+
const textures = Array.from( textureSet );
|
|
192
|
+
return { geometry, materials, textures };
|
|
193
|
+
|
|
194
|
+
}
|
|
@@ -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
|
+
}
|