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.
Files changed (42) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +781 -728
  3. package/build/index.module.js +5223 -3956
  4. package/build/index.module.js.map +1 -1
  5. package/build/index.umd.cjs +5226 -3954
  6. package/build/index.umd.cjs.map +1 -1
  7. package/package.json +68 -60
  8. package/src/core/DynamicPathTracingSceneGenerator.js +119 -111
  9. package/src/core/MaterialReducer.js +256 -256
  10. package/src/core/PathTracingRenderer.js +255 -255
  11. package/src/core/PathTracingSceneGenerator.js +68 -68
  12. package/src/index.js +33 -26
  13. package/src/materials/AlphaDisplayMaterial.js +48 -48
  14. package/src/materials/AmbientOcclusionMaterial.js +197 -197
  15. package/src/materials/BlendMaterial.js +67 -67
  16. package/src/materials/LambertPathTracingMaterial.js +285 -285
  17. package/src/materials/MaterialBase.js +56 -56
  18. package/src/materials/PhysicalPathTracingMaterial.js +922 -848
  19. package/src/{core → objects}/EquirectCamera.js +13 -13
  20. package/src/{core → objects}/PhysicalCamera.js +28 -28
  21. package/src/objects/PhysicalSpotLight.js +14 -0
  22. package/src/objects/ShapedAreaLight.js +12 -0
  23. package/src/shader/shaderEnvMapSampling.js +59 -59
  24. package/src/shader/shaderGGXFunctions.js +108 -108
  25. package/src/shader/shaderIridescenceFunctions.js +130 -0
  26. package/src/shader/shaderLightSampling.js +231 -87
  27. package/src/shader/shaderMaterialSampling.js +546 -501
  28. package/src/shader/shaderSheenFunctions.js +98 -0
  29. package/src/shader/shaderStructs.js +307 -191
  30. package/src/shader/shaderUtils.js +350 -287
  31. package/src/uniforms/EquirectHdrInfoUniform.js +259 -263
  32. package/src/uniforms/IESProfilesTexture.js +100 -0
  33. package/src/uniforms/LightsInfoUniformStruct.js +162 -0
  34. package/src/uniforms/MaterialsTexture.js +406 -319
  35. package/src/uniforms/PhysicalCameraUniform.js +36 -36
  36. package/src/uniforms/RenderTarget2DArray.js +93 -93
  37. package/src/utils/BlurredEnvMapGenerator.js +113 -113
  38. package/src/utils/GeometryPreparationUtils.js +194 -194
  39. package/src/utils/IESLoader.js +325 -0
  40. package/src/utils/UVUnwrapper.js +101 -101
  41. package/src/workers/PathTracingSceneWorker.js +42 -41
  42. package/src/uniforms/LightsTexture.js +0 -83
@@ -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
- bvh,
29
- };
30
-
31
- } );
32
-
33
- }
34
-
35
- dispose() {
36
-
37
- this.bvhGenerator.dispose();
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
- }