Skip to content

Commit

Permalink
Merge pull request #298 from gkjohnson/reduce-redundant-textures
Browse files Browse the repository at this point in the history
index by source rather than texture
  • Loading branch information
gkjohnson authored Nov 24, 2022
2 parents d6f085e + 2067567 commit 1bb778b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
19 changes: 16 additions & 3 deletions src/uniforms/MaterialsTexture.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DataTexture, RGBAFormat, ClampToEdgeWrapping, FloatType, FrontSide, BackSide, DoubleSide } from 'three';
import { reduceTexturesToUniqueSources } from './utils.js';

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

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

return key in material ? textures.indexOf( material[ key ] ) : def;
if ( key in material && material[ key ] ) {

const source = material[ key ].source;
return uniqueTextures.findIndex( tex => tex.source === source );

} else {

return def;

}

}

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

// get the list of textures with unique sources
const uniqueTextures = reduceTexturesToUniqueSources( textures );

if ( image.width !== dimension ) {

this.dispose();
Expand Down Expand Up @@ -170,9 +183,9 @@ export class MaterialsTexture extends DataTexture {
// sample 1
// metalness & roughness
floatArray[ index ++ ] = getField( m, 'metalness', 0.0 );
floatArray[ index ++ ] = textures.indexOf( m.metalnessMap );
floatArray[ index ++ ] = uniqueTextures.indexOf( m.metalnessMap );
floatArray[ index ++ ] = getField( m, 'roughness', 0.0 );
floatArray[ index ++ ] = textures.indexOf( m.roughnessMap );
floatArray[ index ++ ] = uniqueTextures.indexOf( m.roughnessMap );

// sample 2
// transmission & emissiveIntensity
Expand Down
8 changes: 6 additions & 2 deletions src/uniforms/RenderTarget2DArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
NoToneMapping,
} from 'three';
import { FullScreenQuad } from 'three/examples/jsm/postprocessing/Pass.js';
import { reduceTexturesToUniqueSources } from './utils.js';

const prevColor = new Color();
export class RenderTarget2DArray extends WebGLArrayRenderTarget {
Expand Down Expand Up @@ -37,6 +38,9 @@ export class RenderTarget2DArray extends WebGLArrayRenderTarget {

setTextures( renderer, width, height, textures ) {

// get the list of textures with unique sources
const uniqueTextures = reduceTexturesToUniqueSources( textures );

// save previous renderer state
const prevRenderTarget = renderer.getRenderTarget();
const prevToneMapping = renderer.toneMapping;
Expand All @@ -45,7 +49,7 @@ export class RenderTarget2DArray extends WebGLArrayRenderTarget {

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

const texture = textures[ i ];
const texture = uniqueTextures[ i ];
if ( texture ) {

// revert to default texture transform before rendering
Expand Down
21 changes: 21 additions & 0 deletions src/uniforms/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// reduce the set of textures to just those with a unique source while retaining
// the order of the textures.
export function reduceTexturesToUniqueSources( textures ) {

const sourceSet = new Set();
const result = [];
for ( let i = 0, l = textures.length; i < l; i ++ ) {

const tex = textures[ i ];
if ( ! sourceSet.has( tex.source ) ) {

sourceSet.add( tex.source );
result.push( tex );

}

}

return result;

}

0 comments on commit 1bb778b

Please sign in to comment.