|
1 | 1 | import { DataTexture, RGBAFormat, ClampToEdgeWrapping, FloatType, FrontSide, BackSide, DoubleSide } from 'three'; |
| 2 | +import { reduceTexturesToUniqueSources } from './utils.js'; |
2 | 3 |
|
3 | 4 | const MATERIAL_PIXELS = 45; |
4 | 5 | const MATERIAL_STRIDE = MATERIAL_PIXELS * 4; |
@@ -58,7 +59,16 @@ export class MaterialsTexture extends DataTexture { |
58 | 59 |
|
59 | 60 | function getTexture( material, key, def = - 1 ) { |
60 | 61 |
|
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 | + } |
62 | 72 |
|
63 | 73 | } |
64 | 74 |
|
@@ -140,6 +150,9 @@ export class MaterialsTexture extends DataTexture { |
140 | 150 | const dimension = Math.ceil( Math.sqrt( pixelCount ) ); |
141 | 151 | const { threeCompatibilityTransforms, image } = this; |
142 | 152 |
|
| 153 | + // get the list of textures with unique sources |
| 154 | + const uniqueTextures = reduceTexturesToUniqueSources( textures ); |
| 155 | + |
143 | 156 | if ( image.width !== dimension ) { |
144 | 157 |
|
145 | 158 | this.dispose(); |
@@ -170,9 +183,9 @@ export class MaterialsTexture extends DataTexture { |
170 | 183 | // sample 1 |
171 | 184 | // metalness & roughness |
172 | 185 | floatArray[ index ++ ] = getField( m, 'metalness', 0.0 ); |
173 | | - floatArray[ index ++ ] = textures.indexOf( m.metalnessMap ); |
| 186 | + floatArray[ index ++ ] = uniqueTextures.indexOf( m.metalnessMap ); |
174 | 187 | floatArray[ index ++ ] = getField( m, 'roughness', 0.0 ); |
175 | | - floatArray[ index ++ ] = textures.indexOf( m.roughnessMap ); |
| 188 | + floatArray[ index ++ ] = uniqueTextures.indexOf( m.roughnessMap ); |
176 | 189 |
|
177 | 190 | // sample 2 |
178 | 191 | // transmission & emissiveIntensity |
|
0 commit comments