Skip to content

Commit f427ba7

Browse files
authored
Merge pull request #295 from gkjohnson/proc-equirect
Add Gradient Textures
2 parents 5537dd8 + 78cf501 commit f427ba7

18 files changed

+525
-89
lines changed

README.md

Lines changed: 83 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,12 @@ const { bvh, textures, materials, lights } = generator.generate( scene );
110110

111111
// update bvh and geometry attribute textures
112112
ptMaterial.bvh.updateFrom( bvh );
113-
ptMaterial.normalAttribute.updateFrom( geometry.attributes.normal );
114-
ptMaterial.tangentAttribute.updateFrom( geometry.attributes.tangent );
115-
ptMaterial.uvAttribute.updateFrom( geometry.attributes.uv );
113+
ptMaterial.attributesArray.updateFrom(
114+
geometry.attributes.normal,
115+
geometry.attributes.tangent,
116+
geometry.attributes.uv,
117+
geometry.attributes.color,
118+
);
116119

117120
// update materials and texture arrays
118121
ptMaterial.materialIndexAttribute.updateFrom( geometry.attributes.materialIndex );
@@ -501,6 +504,38 @@ dispose() : void
501504

502505
Disposes of the temporary files and textures for generation.
503506

507+
## GradientEquirectMap
508+
509+
### .exponent
510+
511+
```js
512+
exponent = 2 : Number
513+
```
514+
515+
### .topColor
516+
517+
```js
518+
topColor = 0xffffff : Color
519+
```
520+
521+
### .bottomColor
522+
523+
```js
524+
bottomColor = 0x000000 : Color
525+
```
526+
527+
### constructor
528+
529+
```js
530+
constructor( resolution = 512 : Number )
531+
```
532+
533+
### .update
534+
535+
```js
536+
update() : void
537+
```
538+
504539
## MaterialBase
505540

506541
_extends THREE.ShaderMaterial_
@@ -531,9 +566,7 @@ _extends MaterialBase_
531566

532567
// Geometry and BVH information
533568
bvh: MeshBVHUniformStruct,
534-
normalAttribute: FloatVertexAttributeTexture,
535-
tangentAttribute: FloatVertexAttributeTexture,
536-
uvAttribute: FloatVertexAttributeTexture,
569+
attributesArray: AttributesTextureArray,
537570
materialIndexAttribute: UIntVertexAttributeTexture,
538571
materials: MaterialsTexture,
539572
textures: RenderTarget2DArray,
@@ -555,9 +588,8 @@ _extends MaterialBase_
555588
// specular caustics.
556589
filterGlossyFactor = 0: Number,
557590

558-
// The colors to use for the gradient background when enabled.
559-
bgGradientTop: Color,
560-
bgGradientBottom: Color,
591+
// The equirectangular map to render as the background.
592+
backgroundMap = null: Texture,
561593

562594
// The transparency to render the background with. Note that the "alpha" option
563595
// must be set to true on PathTracingRenderer for this field to work properly.
@@ -573,9 +605,6 @@ _extends MaterialBase_
573605
// Whether to use multiple importance sampling to help the image converge more quickly
574606
FEATURE_MIS = 1 : Number,
575607

576-
// Whether to use the "bg" gradient fields to sample for the background
577-
FEATURE_GRADIENT_BG = 0 : Number
578-
579608
// The number of transparent pixels to allow on top of existing bounces for object transparency.
580609
TRANSPARENT_TRAVERSALS = 5 : Number,
581610

@@ -622,7 +651,7 @@ setTextures(
622651
) : void
623652
```
624653

625-
Takes the rendering context to updateh the target for, the target dimensions of the texture array, and the array of textures to render into the 2D texture array. Every texture is stretched to the dimensions of the texture array at the same index they are provided in.
654+
Takes the rendering context to update the target for, the target dimensions of the texture array, and the array of textures to render into the 2D texture array. Every texture is stretched to the dimensions of the texture array at the same index they are provided in.
626655

627656
## PhysicalCameraUniform
628657

@@ -636,6 +665,47 @@ updateFrom( camera : PerspectiveCamera | PhysicalCamera ) : void
636665

637666
Copies all fields from the passed PhysicalCamera if available otherwise the defaults are used.
638667

668+
## AttributesTextureArray
669+
670+
A combined texture array used to store normal, tangent, uv, and color attributes in the same texture sampler array rather than separate samplers. Necessary to save texture slots.
671+
672+
Normals, tangents, uvs, and color attribute data are stored in the 1st, 2nd, 3rd, and 4th layers of the array respectively.
673+
674+
### .updateNormalAttribute
675+
676+
```js
677+
updateNormalAttribute( attr : BufferAttribute ) : void
678+
```
679+
680+
### .updateTangentAttribute
681+
682+
```js
683+
updateTangentAttribute( attr : BufferAttribute ) : void
684+
```
685+
686+
### .updateUvAttribute
687+
688+
```js
689+
updateUvAttribute( attr : BufferAttribute ) : void
690+
```
691+
692+
### .updateColorAttribute
693+
694+
```js
695+
updateColorAttribute( attr : BufferAttribute ) : void
696+
```
697+
698+
### .updateFrom
699+
700+
```js
701+
updateFrom(
702+
normal : BufferAttribute,
703+
tangent : BufferAttribute,
704+
uv : BufferAttribute,
705+
color : BufferAttribute
706+
) : void
707+
```
708+
639709
## MaterialsTexture
640710

641711
_extends DataTexture_

example/areaLight.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,12 @@ async function init() {
204204
const material = ptRenderer.material;
205205

206206
material.bvh.updateFrom( bvh );
207-
material.normalAttribute.updateFrom( geometry.attributes.normal );
208-
material.tangentAttribute.updateFrom( geometry.attributes.tangent );
209-
material.uvAttribute.updateFrom( geometry.attributes.uv );
207+
material.attributesArray.updateFrom(
208+
geometry.attributes.normal,
209+
geometry.attributes.tangent,
210+
geometry.attributes.uv,
211+
geometry.attributes.color,
212+
);
210213
material.materialIndexAttribute.updateFrom( geometry.attributes.materialIndex );
211214
material.textures.setTextures( renderer, 2048, 2048, textures );
212215
material.materials.updateFrom( materials, textures );

example/basic.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,12 @@ async function init() {
9090
const material = pathTracer.material;
9191

9292
material.bvh.updateFrom( bvh );
93-
material.normalAttribute.updateFrom( geometry.attributes.normal );
94-
material.tangentAttribute.updateFrom( geometry.attributes.tangent );
95-
material.uvAttribute.updateFrom( geometry.attributes.uv );
93+
material.attributesArray.updateFrom(
94+
geometry.attributes.normal,
95+
geometry.attributes.tangent,
96+
geometry.attributes.uv,
97+
geometry.attributes.color,
98+
);
9699
material.materialIndexAttribute.updateFrom( geometry.attributes.materialIndex );
97100
material.textures.setTextures( renderer, 2048, 2048, textures );
98101
material.materials.updateFrom( materials, textures );

example/depthOfField.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as THREE from 'three';
22
import { FullScreenQuad } from 'three/examples/jsm/postprocessing/Pass.js';
33
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js';
44
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
5-
import { PathTracingRenderer, PhysicalPathTracingMaterial, PhysicalCamera, BlurredEnvMapGenerator } from '../src/index.js';
5+
import { PathTracingRenderer, PhysicalPathTracingMaterial, PhysicalCamera, BlurredEnvMapGenerator, GradientEquirectTexture } from '../src/index.js';
66
import { PathTracingSceneWorker } from '../src/workers/PathTracingSceneWorker.js';
77
import { RGBELoader } from 'three/examples/jsm/loaders/RGBELoader.js';
88
import { MeshoptDecoder } from 'three/examples/jsm/libs/meshopt_decoder.module.js';
@@ -53,14 +53,17 @@ async function init() {
5353

5454
scene = new THREE.Scene();
5555

56+
const gradientMap = new GradientEquirectTexture();
57+
gradientMap.topColor.set( 0x390f20 ).convertSRGBToLinear();
58+
gradientMap.bottomColor.set( 0x151b1f ).convertSRGBToLinear();
59+
gradientMap.update();
60+
5661
ptRenderer = new PathTracingRenderer( renderer );
5762
ptRenderer.camera = camera;
5863
ptRenderer.material = new PhysicalPathTracingMaterial();
5964
ptRenderer.tiles.set( params.tiles, params.tiles );
60-
ptRenderer.material.setDefine( 'FEATURE_GRADIENT_BG', 1 );
6165
ptRenderer.material.setDefine( 'FEATURE_MIS', 0 );
62-
ptRenderer.material.bgGradientTop.set( 0x390f20 ).convertSRGBToLinear();
63-
ptRenderer.material.bgGradientBottom.set( 0x151b1f ).convertSRGBToLinear();
66+
ptRenderer.material.backgroundMap = gradientMap;
6467

6568
fsQuad = new FullScreenQuad( new THREE.MeshBasicMaterial( {
6669
map: ptRenderer.target.texture,
@@ -147,9 +150,12 @@ async function init() {
147150
const material = ptRenderer.material;
148151

149152
material.bvh.updateFrom( bvh );
150-
material.normalAttribute.updateFrom( geometry.attributes.normal );
151-
material.tangentAttribute.updateFrom( geometry.attributes.tangent );
152-
material.uvAttribute.updateFrom( geometry.attributes.uv );
153+
material.attributesArray.updateFrom(
154+
geometry.attributes.normal,
155+
geometry.attributes.tangent,
156+
geometry.attributes.uv,
157+
geometry.attributes.color,
158+
);
153159
material.materialIndexAttribute.updateFrom( geometry.attributes.materialIndex );
154160
material.textures.setTextures( renderer, 2048, 2048, textures );
155161
material.materials.updateFrom( materials, textures );

example/index.js

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import { GUI } from 'three/examples/jsm/libs/lil-gui.module.min.js';
2929
import Stats from 'three/examples/jsm/libs/stats.module.js';
3030
import { generateRadialFloorTexture } from './utils/generateRadialFloorTexture.js';
3131
import { PathTracingSceneWorker } from '../src/workers/PathTracingSceneWorker.js';
32-
import { PhysicalPathTracingMaterial, PathTracingRenderer, MaterialReducer, BlurredEnvMapGenerator } from '../src/index.js';
32+
import { PhysicalPathTracingMaterial, PathTracingRenderer, MaterialReducer, BlurredEnvMapGenerator, GradientEquirectTexture } from '../src/index.js';
3333
import { FullScreenQuad } from 'three/examples/jsm/postprocessing/Pass.js';
3434
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
3535

@@ -106,7 +106,7 @@ let creditEl, loadingEl, samplesEl;
106106
let floorPlane, gui, stats, sceneInfo;
107107
let renderer, orthoCamera, perspectiveCamera, activeCamera;
108108
let ptRenderer, fsQuad, controls, scene;
109-
let envMap, envMapGenerator;
109+
let envMap, envMapGenerator, backgroundMap;
110110
let loadingModel = false;
111111
let delaySamples = 0;
112112

@@ -135,14 +135,17 @@ async function init() {
135135
orthoCamera = new OrthographicCamera( orthoWidth / - 2, orthoWidth / 2, orthoHeight / 2, orthoHeight / - 2, 0, 100 );
136136
orthoCamera.position.set( - 1, 0.25, 1 );
137137

138+
backgroundMap = new GradientEquirectTexture();
139+
backgroundMap.topColor.set( params.bgGradientTop );
140+
backgroundMap.bottomColor.set( params.bgGradientBottom );
141+
backgroundMap.update();
142+
138143
ptRenderer = new PathTracingRenderer( renderer );
139144
ptRenderer.alpha = true;
140145
ptRenderer.material = new PhysicalPathTracingMaterial();
141146
ptRenderer.tiles.set( params.tiles, params.tiles );
142-
ptRenderer.material.setDefine( 'FEATURE_GRADIENT_BG', 1 );
143147
ptRenderer.material.setDefine( 'FEATURE_MIS', Number( params.multipleImportanceSampling ) );
144-
ptRenderer.material.bgGradientTop.set( params.bgGradientTop );
145-
ptRenderer.material.bgGradientBottom.set( params.bgGradientBottom );
148+
ptRenderer.material.backgroundMap = backgroundMap;
146149

147150
fsQuad = new FullScreenQuad( new MeshBasicMaterial( {
148151
map: ptRenderer.target.texture,
@@ -204,8 +207,6 @@ function animate() {
204207
floorPlane.material.roughness = params.floorRoughness;
205208
floorPlane.material.metalness = params.floorMetalness;
206209
floorPlane.material.opacity = params.floorOpacity;
207-
ptRenderer.material.bgGradientTop.set( params.bgGradientTop );
208-
ptRenderer.material.bgGradientBottom.set( params.bgGradientBottom );
209210

210211
if ( ptRenderer.samples < 1.0 || ! params.enable ) {
211212

@@ -371,26 +372,33 @@ function buildGui() {
371372
const backgroundFolder = gui.addFolder( 'background' );
372373
backgroundFolder.add( params, 'backgroundType', [ 'Environment', 'Gradient' ] ).onChange( v => {
373374

374-
ptRenderer.material.setDefine( 'FEATURE_GRADIENT_BG', Number( v === 'Gradient' ) );
375375
if ( v === 'Gradient' ) {
376376

377377
scene.background = new Color( 0x060606 );
378+
ptRenderer.material.backgroundMap = backgroundMap;
378379

379380
} else {
380381

381382
scene.background = scene.environment;
383+
ptRenderer.material.backgroundMap = null;
382384

383385
}
384386

385387
ptRenderer.reset();
386388

387389
} );
388-
backgroundFolder.addColor( params, 'bgGradientTop' ).onChange( () => {
390+
backgroundFolder.addColor( params, 'bgGradientTop' ).onChange( v => {
391+
392+
backgroundMap.topColor.set( v );
393+
backgroundMap.update();
389394

390395
ptRenderer.reset();
391396

392397
} );
393-
backgroundFolder.addColor( params, 'bgGradientBottom' ).onChange( () => {
398+
backgroundFolder.addColor( params, 'bgGradientBottom' ).onChange( v => {
399+
400+
backgroundMap.bottomColor.set( v );
401+
backgroundMap.update();
394402

395403
ptRenderer.reset();
396404

@@ -685,11 +693,13 @@ async function updateModel() {
685693
const material = ptRenderer.material;
686694

687695
material.bvh.updateFrom( bvh );
688-
material.normalAttribute.updateFrom( geometry.attributes.normal );
689-
material.tangentAttribute.updateFrom( geometry.attributes.tangent );
690-
material.uvAttribute.updateFrom( geometry.attributes.uv );
696+
material.attributesArray.updateFrom(
697+
geometry.attributes.normal,
698+
geometry.attributes.tangent,
699+
geometry.attributes.uv,
700+
geometry.attributes.color,
701+
);
691702
material.materialIndexAttribute.updateFrom( geometry.attributes.materialIndex );
692-
material.colorAttribute.updateFrom( geometry.attributes.color );
693703
material.textures.setTextures( renderer, 2048, 2048, textures );
694704
material.materials.updateFrom( materials, textures );
695705

@@ -706,6 +716,10 @@ async function updateModel() {
706716
params.bgGradientTop = modelInfo.gradientTop || '#111111';
707717
params.bgGradientBottom = modelInfo.gradientBot || '#000000';
708718

719+
backgroundMap.topColor.set( params.bgGradientTop );
720+
backgroundMap.bottomColor.set( params.bgGradientBottom );
721+
backgroundMap.update();
722+
709723
buildGui();
710724

711725
loadingModel = false;

example/interior.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,12 @@ async function init() {
149149
const material = ptRenderer.material;
150150

151151
material.bvh.updateFrom( bvh );
152-
material.normalAttribute.updateFrom( geometry.attributes.normal );
153-
material.tangentAttribute.updateFrom( geometry.attributes.tangent );
154-
material.uvAttribute.updateFrom( geometry.attributes.uv );
152+
material.attributesArray.updateFrom(
153+
geometry.attributes.normal,
154+
geometry.attributes.tangent,
155+
geometry.attributes.uv,
156+
geometry.attributes.color,
157+
);
155158
material.materialIndexAttribute.updateFrom( geometry.attributes.materialIndex );
156159
material.textures.setTextures( renderer, 2048, 2048, textures );
157160
material.materials.updateFrom( materials, textures );

example/materialBall.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,9 +279,12 @@ async function init() {
279279
const material = ptRenderer.material;
280280

281281
material.bvh.updateFrom( bvh );
282-
material.normalAttribute.updateFrom( geometry.attributes.normal );
283-
material.tangentAttribute.updateFrom( geometry.attributes.tangent );
284-
material.uvAttribute.updateFrom( geometry.attributes.uv );
282+
material.attributesArray.updateFrom(
283+
geometry.attributes.normal,
284+
geometry.attributes.tangent,
285+
geometry.attributes.uv,
286+
geometry.attributes.color,
287+
);
285288
material.materialIndexAttribute.updateFrom( geometry.attributes.materialIndex );
286289
material.textures.setTextures( renderer, 2048, 2048, textures );
287290
material.materials.updateFrom( materials, textures );

example/materialDatabase.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,12 @@ async function init() {
164164
const material = ptRenderer.material;
165165

166166
material.bvh.updateFrom( bvh );
167-
material.normalAttribute.updateFrom( geometry.attributes.normal );
168-
material.tangentAttribute.updateFrom( geometry.attributes.tangent );
169-
material.uvAttribute.updateFrom( geometry.attributes.uv );
167+
material.attributesArray.updateFrom(
168+
geometry.attributes.normal,
169+
geometry.attributes.tangent,
170+
geometry.attributes.uv,
171+
geometry.attributes.color,
172+
);
170173
material.materialIndexAttribute.updateFrom( geometry.attributes.materialIndex );
171174
material.textures.setTextures( renderer, 2048, 2048, textures );
172175
material.materials.updateFrom( materials, textures );

0 commit comments

Comments
 (0)