Skip to content

Commit ab4f082

Browse files
authored
Merge pull request #296 from gkjohnson/implicit-side
MaterialTexture: Automatically use side from Material
2 parents f33fbde + b75cbab commit ab4f082

File tree

4 files changed

+30
-57
lines changed

4 files changed

+30
-57
lines changed

README.md

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -722,14 +722,6 @@ Three.js materials support only a single set of UV transforms in a certain fallb
722722

723723
See fallback order documentation [here](https://threejs.org/docs/#api/en/textures/Texture.offset).
724724

725-
### .setSide
726-
727-
```js
728-
setSide( index : Number, side : FrontSide | BackSide | DoubleSide ) : void
729-
```
730-
731-
Sets the side to render for the given material.
732-
733725
### .setMatte
734726

735727
```js

example/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
</div>
6161
</div>
6262
<script type="module">
63-
import { MeshPhysicalMaterial, Color } from 'three';
63+
import { MeshPhysicalMaterial, Color, DoubleSide } from 'three';
6464

6565
window.MODEL_LIST = {
6666
'M2020 Rover': {
@@ -300,6 +300,7 @@
300300
roughness: 0.25,
301301
transmission: 1,
302302
ior: 1.5,
303+
side: DoubleSide,
303304
} ) ;
304305

305306
model.traverse( c => {

example/index.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
Box3,
55
LoadingManager,
66
Sphere,
7-
Color,
87
DoubleSide,
98
Mesh,
109
MeshStandardMaterial,
@@ -166,7 +165,8 @@ async function init() {
166165
transparent: true,
167166
color: 0x080808,
168167
roughness: 0.1,
169-
metalness: 0.0
168+
metalness: 0.0,
169+
side: DoubleSide,
170170
} )
171171
);
172172
floorPlane.scale.setScalar( 3 );
@@ -176,8 +176,7 @@ async function init() {
176176
document.body.appendChild( stats.dom );
177177
renderer.physicallyCorrectLights = true;
178178
renderer.toneMapping = ACESFilmicToneMapping;
179-
ptRenderer.material.setDefine( 'FEATURE_GRADIENT_BG', 1 );
180-
scene.background = new Color( 0x060606 );
179+
scene.background = backgroundMap;
181180
ptRenderer.tiles.set( params.tilesX, params.tilesY );
182181

183182
updateCamera( params.cameraProjection );
@@ -374,7 +373,7 @@ function buildGui() {
374373

375374
if ( v === 'Gradient' ) {
376375

377-
scene.background = new Color( 0x060606 );
376+
scene.background = backgroundMap;
378377
ptRenderer.material.backgroundMap = backgroundMap;
379378

380379
} else {

src/uniforms/MaterialsTexture.js

Lines changed: 24 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { DataTexture, RGBAFormat, ClampToEdgeWrapping, FloatType, FrontSide, Bac
33
const MATERIAL_PIXELS = 45;
44
const MATERIAL_STRIDE = MATERIAL_PIXELS * 4;
55

6-
const SIDE_OFFSET = 13 * 4 + 3; // s12.a
76
const MATTE_OFFSET = 14 * 4 + 0; // s14.r
87
const SHADOW_OFFSET = 14 * 4 + 1; // s14.g
98

@@ -39,45 +38,6 @@ export class MaterialsTexture extends DataTexture {
3938

4039
}
4140

42-
setSide( materialIndex, side ) {
43-
44-
const array = this.image.data;
45-
const index = materialIndex * MATERIAL_STRIDE + SIDE_OFFSET;
46-
switch ( side ) {
47-
48-
case FrontSide:
49-
array[ index ] = 1;
50-
break;
51-
case BackSide:
52-
array[ index ] = - 1;
53-
break;
54-
case DoubleSide:
55-
array[ index ] = 0;
56-
break;
57-
58-
}
59-
60-
}
61-
62-
getSide( materialIndex ) {
63-
64-
const array = this.image.data;
65-
const index = materialIndex * MATERIAL_STRIDE + SIDE_OFFSET;
66-
switch ( array[ index ] ) {
67-
68-
case 0:
69-
return DoubleSide;
70-
case 1:
71-
return FrontSide;
72-
case - 1:
73-
return BackSide;
74-
75-
}
76-
77-
return 0;
78-
79-
}
80-
8141
setMatte( materialIndex, matte ) {
8242

8343
const array = this.image.data;
@@ -336,8 +296,9 @@ export class MaterialsTexture extends DataTexture {
336296
floatArray[ index ++ ] = getField( m, 'specularIntensity', 1.0 );
337297
floatArray[ index ++ ] = getTexture( m, 'specularIntensityMap' );
338298

339-
// thickness
340-
floatArray[ index ++ ] = getField( m, 'thickness', 0.0 ) === 0.0 && getField( m, 'attenuationDistance', Infinity ) === Infinity;
299+
// isThinFilm
300+
const isThinFilm = getField( m, 'thickness', 0.0 ) === 0.0 && getField( m, 'attenuationDistance', Infinity ) === Infinity;
301+
floatArray[ index ++ ] = Number( isThinFilm );
341302
index ++;
342303

343304
// sample 12
@@ -364,7 +325,27 @@ export class MaterialsTexture extends DataTexture {
364325
// side & matte
365326
floatArray[ index ++ ] = m.opacity;
366327
floatArray[ index ++ ] = m.alphaTest;
367-
index ++; // side
328+
if ( ! isThinFilm && m.transmission > 0.0 ) {
329+
330+
floatArray[ index ++ ] = 0;
331+
332+
} else {
333+
334+
switch ( m.side ) {
335+
336+
case FrontSide:
337+
floatArray[ index ++ ] = 1;
338+
break;
339+
case BackSide:
340+
floatArray[ index ++ ] = - 1;
341+
break;
342+
case DoubleSide:
343+
floatArray[ index ++ ] = 0;
344+
break;
345+
346+
}
347+
348+
}
368349

369350
// sample 14
370351
index ++; // matte

0 commit comments

Comments
 (0)