three-gpu-pathtracer 0.0.1 → 0.0.4
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 +678 -386
- package/build/index.module.js +3166 -1690
- package/build/index.module.js.map +1 -1
- package/build/index.umd.cjs +3176 -1692
- package/build/index.umd.cjs.map +1 -1
- package/package.json +60 -57
- package/src/core/DynamicPathTracingSceneGenerator.js +106 -0
- package/src/core/MaterialReducer.js +256 -256
- package/src/core/PathTracingRenderer.js +125 -28
- package/src/core/PathTracingSceneGenerator.js +52 -46
- package/src/core/PhysicalCamera.js +28 -0
- package/src/index.js +25 -21
- package/src/materials/AlphaDisplayMaterial.js +48 -0
- package/src/materials/AmbientOcclusionMaterial.js +197 -197
- package/src/materials/BlendMaterial.js +67 -0
- package/src/materials/LambertPathTracingMaterial.js +285 -285
- package/src/materials/MaterialBase.js +56 -56
- package/src/materials/PhysicalPathTracingMaterial.js +684 -370
- package/src/shader/shaderEnvMapSampling.js +67 -0
- package/src/shader/shaderGGXFunctions.js +108 -107
- package/src/shader/shaderMaterialSampling.js +345 -333
- package/src/shader/shaderStructs.js +131 -30
- package/src/shader/shaderUtils.js +246 -140
- package/src/uniforms/EquirectHdrInfoUniform.js +263 -0
- package/src/uniforms/MaterialsTexture.js +251 -0
- package/src/uniforms/PhysicalCameraUniform.js +36 -0
- package/src/uniforms/RenderTarget2DArray.js +93 -80
- package/src/utils/BlurredEnvMapGenerator.js +113 -0
- package/src/utils/GeometryPreparationUtils.js +194 -172
- package/src/utils/UVUnwrapper.js +101 -101
- package/src/workers/PathTracingSceneWorker.js +40 -0
- package/src/uniforms/EquirectPdfUniform.js +0 -132
- package/src/uniforms/MaterialStructArrayUniform.js +0 -18
- package/src/uniforms/MaterialStructUniform.js +0 -94
- package/src/viewers/PathTracingViewer.js +0 -259
|
@@ -1,259 +0,0 @@
|
|
|
1
|
-
import { Scene, WebGLRenderer, MeshBasicMaterial, Vector2, Mesh, PerspectiveCamera, sRGBEncoding } from 'three';
|
|
2
|
-
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
|
|
3
|
-
import { FullScreenQuad } from 'three/examples/jsm/postprocessing/Pass.js';
|
|
4
|
-
import { SAH } from 'three-mesh-bvh';
|
|
5
|
-
import { GenerateMeshBVHWorker } from 'three-mesh-bvh/src/workers/GenerateMeshBVHWorker.js';
|
|
6
|
-
import { PathTracingRenderer } from '../core/PathTracingRenderer.js';
|
|
7
|
-
import { mergeMeshes } from '../utils/GeometryPreparationUtils.js';
|
|
8
|
-
import { PhysicalPathTracingMaterial } from '../materials/PhysicalPathTracingMaterial.js';
|
|
9
|
-
import { MaterialReducer } from '../core/MaterialReducer.js';
|
|
10
|
-
|
|
11
|
-
export class PathTracingViewer {
|
|
12
|
-
|
|
13
|
-
get domElement() {
|
|
14
|
-
|
|
15
|
-
return this._container;
|
|
16
|
-
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
constructor() {
|
|
20
|
-
|
|
21
|
-
this.scene = new Scene();
|
|
22
|
-
this.camera = new PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.025, 500 );
|
|
23
|
-
this.camera.position.set( - 1, 0.25, 1 );
|
|
24
|
-
|
|
25
|
-
this.renderer = new WebGLRenderer( { antialias: true } );
|
|
26
|
-
this.fsQuad = new FullScreenQuad( new MeshBasicMaterial( { transparent: true } ) );
|
|
27
|
-
this.ptRenderer = new PathTracingRenderer( this.renderer );
|
|
28
|
-
this.ptModel = null;
|
|
29
|
-
this.ptMaterials = null;
|
|
30
|
-
this.ptTextures = null;
|
|
31
|
-
this.model = null;
|
|
32
|
-
this.bvhGenerator = new GenerateMeshBVHWorker();
|
|
33
|
-
this.onRender = null;
|
|
34
|
-
this.enablePathTracing = true;
|
|
35
|
-
this.pausePathTracing = false;
|
|
36
|
-
this.samplesPerFrame = 1;
|
|
37
|
-
this._scale = 1;
|
|
38
|
-
this._nextObject = null;
|
|
39
|
-
this._needsSizeUpdate = false;
|
|
40
|
-
this._newSize = new Vector2();
|
|
41
|
-
this._resizeObserver = new ResizeObserver( entries => {
|
|
42
|
-
|
|
43
|
-
const { contentRect } = entries[ 0 ];
|
|
44
|
-
this._newSize.set( contentRect.width, contentRect.height );
|
|
45
|
-
this._needsSizeUpdate = true;
|
|
46
|
-
|
|
47
|
-
} );
|
|
48
|
-
|
|
49
|
-
const container = document.createElement( 'div' );
|
|
50
|
-
container.style.overflow = 'hidden';
|
|
51
|
-
container.appendChild( this.renderer.domElement );
|
|
52
|
-
this._container = container;
|
|
53
|
-
|
|
54
|
-
this.ptRenderer.camera = this.camera;
|
|
55
|
-
this.ptRenderer.material = new PhysicalPathTracingMaterial();
|
|
56
|
-
this.renderer.outputEncoding = sRGBEncoding;
|
|
57
|
-
this._resizeObserver.observe( container );
|
|
58
|
-
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
_updateSize() {
|
|
62
|
-
|
|
63
|
-
const dpr = window.devicePixelRatio;
|
|
64
|
-
const scale = this._scale;
|
|
65
|
-
const size = this._newSize;
|
|
66
|
-
this.renderer.setPixelRatio( dpr );
|
|
67
|
-
|
|
68
|
-
this.renderer.domElement.style.aspectRatio = `${ size.width } / ${ size.height }`;
|
|
69
|
-
this.renderer.domElement.style.width = `100%`;
|
|
70
|
-
this.renderer.setSize( scale * size.width, scale * size.height, false );
|
|
71
|
-
this.ptRenderer.target.setSize( size.width * scale * dpr, size.height * scale * dpr );
|
|
72
|
-
this.camera.aspect = size.width / size.height;
|
|
73
|
-
this.camera.updateProjectionMatrix();
|
|
74
|
-
this._needsSizeUpdate = false;
|
|
75
|
-
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
setScale( scale ) {
|
|
79
|
-
|
|
80
|
-
this._scale = scale;
|
|
81
|
-
this._needsSizeUpdate = true;
|
|
82
|
-
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
setModel( object, bvhOptions = {} ) {
|
|
86
|
-
|
|
87
|
-
if ( this.bvhGenerator.running ) {
|
|
88
|
-
|
|
89
|
-
this._nextObject = object;
|
|
90
|
-
return;
|
|
91
|
-
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
object.updateMatrixWorld( true );
|
|
95
|
-
|
|
96
|
-
const materialReducer = new MaterialReducer();
|
|
97
|
-
materialReducer.process( object );
|
|
98
|
-
|
|
99
|
-
const meshes = [];
|
|
100
|
-
object.traverse( c => {
|
|
101
|
-
|
|
102
|
-
if ( c.isMesh ) {
|
|
103
|
-
|
|
104
|
-
meshes.push( c );
|
|
105
|
-
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
} );
|
|
109
|
-
|
|
110
|
-
const { geometry, materials, textures } = mergeMeshes( meshes, { attributes: [ 'position', 'normal', 'tangent', 'uv' ] } );
|
|
111
|
-
return this
|
|
112
|
-
.bvhGenerator
|
|
113
|
-
.generate( geometry, { strategy: SAH, maxLeafTris: 1, ...bvhOptions } )
|
|
114
|
-
.then( bvh => {
|
|
115
|
-
|
|
116
|
-
if ( this._nextObject ) {
|
|
117
|
-
|
|
118
|
-
this.setModel( this._nextObject );
|
|
119
|
-
this._nextObject = null;
|
|
120
|
-
return;
|
|
121
|
-
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
if ( this.model ) {
|
|
125
|
-
|
|
126
|
-
this.scene.remove( this.model );
|
|
127
|
-
this.ptTextures.forEach( tex => tex.dispose() );
|
|
128
|
-
this.ptMaterials.forEach( mat => mat.dispose() );
|
|
129
|
-
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
const mesh = new Mesh( geometry );
|
|
133
|
-
this.scene.add( object );
|
|
134
|
-
this.ptModel = mesh;
|
|
135
|
-
this.ptMaterials = materials;
|
|
136
|
-
this.ptTextures = textures;
|
|
137
|
-
this.model = object;
|
|
138
|
-
|
|
139
|
-
const { ptRenderer } = this;
|
|
140
|
-
const { material } = ptRenderer;
|
|
141
|
-
|
|
142
|
-
// dispose of textures because they cannot be updated in later version of three.js
|
|
143
|
-
material.bvh.dispose();
|
|
144
|
-
material.normalAttribute.dispose();
|
|
145
|
-
material.tangentAttribute.dispose();
|
|
146
|
-
material.uvAttribute.dispose();
|
|
147
|
-
material.materialIndexAttribute.dispose();
|
|
148
|
-
material.textures.dispose();
|
|
149
|
-
|
|
150
|
-
material.bvh.updateFrom( bvh );
|
|
151
|
-
material.normalAttribute.updateFrom( geometry.attributes.normal );
|
|
152
|
-
material.tangentAttribute.updateFrom( geometry.attributes.tangent );
|
|
153
|
-
material.uvAttribute.updateFrom( geometry.attributes.uv );
|
|
154
|
-
material.materialIndexAttribute.updateFrom( geometry.attributes.materialIndex );
|
|
155
|
-
material.textures.setTextures( this.renderer, 2048, 2048, textures );
|
|
156
|
-
material.materials.updateFrom( materials, textures );
|
|
157
|
-
material.setDefine( 'MATERIAL_LENGTH', materials.length );
|
|
158
|
-
ptRenderer.reset();
|
|
159
|
-
|
|
160
|
-
} );
|
|
161
|
-
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
setEnvironment( envMap ) {
|
|
165
|
-
|
|
166
|
-
this.scene.environment = envMap;
|
|
167
|
-
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
init() {
|
|
171
|
-
|
|
172
|
-
const { ptRenderer, renderer, fsQuad, camera } = this;
|
|
173
|
-
|
|
174
|
-
const controls = new OrbitControls( camera, renderer.domElement );
|
|
175
|
-
let delaySamples = 0;
|
|
176
|
-
controls.addEventListener( 'change', () => {
|
|
177
|
-
|
|
178
|
-
const tiles = this.ptRenderer.tiles;
|
|
179
|
-
if ( tiles.x * tiles.y !== 1 ) {
|
|
180
|
-
|
|
181
|
-
delaySamples = 1;
|
|
182
|
-
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
ptRenderer.reset();
|
|
186
|
-
|
|
187
|
-
} );
|
|
188
|
-
|
|
189
|
-
renderer.setAnimationLoop( () => {
|
|
190
|
-
|
|
191
|
-
if ( this.pausePathTracing ) {
|
|
192
|
-
|
|
193
|
-
return;
|
|
194
|
-
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
if ( this._needsSizeUpdate ) {
|
|
198
|
-
|
|
199
|
-
this._updateSize();
|
|
200
|
-
this.ptRenderer.reset();
|
|
201
|
-
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
if ( this.model ) {
|
|
205
|
-
|
|
206
|
-
// ensure materials are updated
|
|
207
|
-
const { ptRenderer, ptMaterials, ptTextures } = this;
|
|
208
|
-
ptRenderer.material.materials.updateFrom( ptMaterials, ptTextures );
|
|
209
|
-
|
|
210
|
-
if ( this.enablePathTracing && delaySamples === 0 ) {
|
|
211
|
-
|
|
212
|
-
camera.updateMatrixWorld();
|
|
213
|
-
|
|
214
|
-
for ( let i = 0, l = this.samplesPerFrame; i < l; i ++ ) {
|
|
215
|
-
|
|
216
|
-
ptRenderer.update();
|
|
217
|
-
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
if ( ptRenderer.samples < 1 ) {
|
|
221
|
-
|
|
222
|
-
renderer.render( this.scene, this.camera );
|
|
223
|
-
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
renderer.autoClear = false;
|
|
227
|
-
fsQuad.material.map = ptRenderer.target.texture;
|
|
228
|
-
fsQuad.render( renderer );
|
|
229
|
-
renderer.autoClear = true;
|
|
230
|
-
|
|
231
|
-
} else {
|
|
232
|
-
|
|
233
|
-
if ( delaySamples > 0 ) {
|
|
234
|
-
|
|
235
|
-
delaySamples --;
|
|
236
|
-
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
renderer.render( this.scene, this.camera );
|
|
240
|
-
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
} else {
|
|
244
|
-
|
|
245
|
-
renderer.clear();
|
|
246
|
-
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
if ( this.onRender ) {
|
|
250
|
-
|
|
251
|
-
this.onRender();
|
|
252
|
-
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
} );
|
|
256
|
-
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
}
|