Skip to content

Commit 1bb778b

Browse files
authored
Merge pull request #298 from gkjohnson/reduce-redundant-textures
index by source rather than texture
2 parents d6f085e + 2067567 commit 1bb778b

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

src/uniforms/MaterialsTexture.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { DataTexture, RGBAFormat, ClampToEdgeWrapping, FloatType, FrontSide, BackSide, DoubleSide } from 'three';
2+
import { reduceTexturesToUniqueSources } from './utils.js';
23

34
const MATERIAL_PIXELS = 45;
45
const MATERIAL_STRIDE = MATERIAL_PIXELS * 4;
@@ -58,7 +59,16 @@ export class MaterialsTexture extends DataTexture {
5859

5960
function getTexture( material, key, def = - 1 ) {
6061

61-
return key in material ? textures.indexOf( material[ key ] ) : def;
62+
if ( key in material && material[ key ] ) {
63+
64+
const source = material[ key ].source;
65+
return uniqueTextures.findIndex( tex => tex.source === source );
66+
67+
} else {
68+
69+
return def;
70+
71+
}
6272

6373
}
6474

@@ -140,6 +150,9 @@ export class MaterialsTexture extends DataTexture {
140150
const dimension = Math.ceil( Math.sqrt( pixelCount ) );
141151
const { threeCompatibilityTransforms, image } = this;
142152

153+
// get the list of textures with unique sources
154+
const uniqueTextures = reduceTexturesToUniqueSources( textures );
155+
143156
if ( image.width !== dimension ) {
144157

145158
this.dispose();
@@ -170,9 +183,9 @@ export class MaterialsTexture extends DataTexture {
170183
// sample 1
171184
// metalness & roughness
172185
floatArray[ index ++ ] = getField( m, 'metalness', 0.0 );
173-
floatArray[ index ++ ] = textures.indexOf( m.metalnessMap );
186+
floatArray[ index ++ ] = uniqueTextures.indexOf( m.metalnessMap );
174187
floatArray[ index ++ ] = getField( m, 'roughness', 0.0 );
175-
floatArray[ index ++ ] = textures.indexOf( m.roughnessMap );
188+
floatArray[ index ++ ] = uniqueTextures.indexOf( m.roughnessMap );
176189

177190
// sample 2
178191
// transmission & emissiveIntensity

src/uniforms/RenderTarget2DArray.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
NoToneMapping,
1010
} from 'three';
1111
import { FullScreenQuad } from 'three/examples/jsm/postprocessing/Pass.js';
12+
import { reduceTexturesToUniqueSources } from './utils.js';
1213

1314
const prevColor = new Color();
1415
export class RenderTarget2DArray extends WebGLArrayRenderTarget {
@@ -37,6 +38,9 @@ export class RenderTarget2DArray extends WebGLArrayRenderTarget {
3738

3839
setTextures( renderer, width, height, textures ) {
3940

41+
// get the list of textures with unique sources
42+
const uniqueTextures = reduceTexturesToUniqueSources( textures );
43+
4044
// save previous renderer state
4145
const prevRenderTarget = renderer.getRenderTarget();
4246
const prevToneMapping = renderer.toneMapping;
@@ -45,7 +49,7 @@ export class RenderTarget2DArray extends WebGLArrayRenderTarget {
4549

4650
// resize the render target and ensure we don't have an empty texture
4751
// render target depth must be >= 1 to avoid unbound texture error on android devices
48-
const depth = textures.length || 1;
52+
const depth = uniqueTextures.length || 1;
4953
this.setSize( width, height, depth );
5054
renderer.setClearColor( 0, 0 );
5155
renderer.toneMapping = NoToneMapping;
@@ -54,7 +58,7 @@ export class RenderTarget2DArray extends WebGLArrayRenderTarget {
5458
const fsQuad = this.fsQuad;
5559
for ( let i = 0, l = depth; i < l; i ++ ) {
5660

57-
const texture = textures[ i ];
61+
const texture = uniqueTextures[ i ];
5862
if ( texture ) {
5963

6064
// revert to default texture transform before rendering

src/uniforms/utils.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// reduce the set of textures to just those with a unique source while retaining
2+
// the order of the textures.
3+
export function reduceTexturesToUniqueSources( textures ) {
4+
5+
const sourceSet = new Set();
6+
const result = [];
7+
for ( let i = 0, l = textures.length; i < l; i ++ ) {
8+
9+
const tex = textures[ i ];
10+
if ( ! sourceSet.has( tex.source ) ) {
11+
12+
sourceSet.add( tex.source );
13+
result.push( tex );
14+
15+
}
16+
17+
}
18+
19+
return result;
20+
21+
}

0 commit comments

Comments
 (0)