Skip to content

Commit 88f83c0

Browse files
committed
Shaders: Remove struct GeometricContext to improve Adreno GPU support.
1 parent 1e673db commit 88f83c0

19 files changed

+103
-116
lines changed

examples/jsm/csm/CSMShader.js

+21-21
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@ import { ShaderChunk } from 'three';
22

33
const CSMShader = {
44
lights_fragment_begin: /* glsl */`
5-
GeometricContext geometry;
5+
vec3 geometryPosition = - vViewPosition;
6+
vec3 geometryNormal = normal;
7+
vec3 geometryViewDir = ( isOrthographic ) ? vec3( 0, 0, 1 ) : normalize( vViewPosition );
68
7-
geometry.position = - vViewPosition;
8-
geometry.normal = normal;
9-
geometry.viewDir = ( isOrthographic ) ? vec3( 0, 0, 1 ) : normalize( vViewPosition );
9+
vec3 geometryClearcoatNormal;
1010
1111
#ifdef USE_CLEARCOAT
1212
13-
geometry.clearcoatNormal = clearcoatNormal;
13+
geometryClearcoatNormal = clearcoatNormal;
1414
1515
#endif
1616
1717
#ifdef USE_IRIDESCENCE
18-
float dotNVi = saturate( dot( normal, geometry.viewDir ) );
18+
float dotNVi = saturate( dot( normal, geometryViewDir ) );
1919
if ( material.iridescenceThickness == 0.0 ) {
2020
material.iridescence = 0.0;
2121
} else {
@@ -42,14 +42,14 @@ IncidentLight directLight;
4242
4343
pointLight = pointLights[ i ];
4444
45-
getPointLightInfo( pointLight, geometry, directLight );
45+
getPointLightInfo( pointLight, geometryPosition, directLight );
4646
4747
#if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_POINT_LIGHT_SHADOWS )
4848
pointLightShadow = pointLightShadows[ i ];
4949
directLight.color *= ( directLight.visible && receiveShadow ) ? getPointShadow( pointShadowMap[ i ], pointLightShadow.shadowMapSize, pointLightShadow.shadowBias, pointLightShadow.shadowRadius, vPointShadowCoord[ i ], pointLightShadow.shadowCameraNear, pointLightShadow.shadowCameraFar ) : 1.0;
5050
#endif
5151
52-
RE_Direct( directLight, geometry, material, reflectedLight );
52+
RE_Direct( directLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight );
5353
5454
}
5555
#pragma unroll_loop_end
@@ -72,7 +72,7 @@ IncidentLight directLight;
7272
7373
spotLight = spotLights[ i ];
7474
75-
getSpotLightInfo( spotLight, geometry, directLight );
75+
getSpotLightInfo( spotLight, geometryPosition, directLight );
7676
7777
// spot lights are ordered [shadows with maps, shadows without maps, maps without shadows, none]
7878
#if ( UNROLLED_LOOP_INDEX < NUM_SPOT_LIGHT_SHADOWS_WITH_MAPS )
@@ -96,7 +96,7 @@ IncidentLight directLight;
9696
9797
#endif
9898
99-
RE_Direct( directLight, geometry, material, reflectedLight );
99+
RE_Direct( directLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight );
100100
101101
}
102102
#pragma unroll_loop_end
@@ -123,7 +123,7 @@ IncidentLight directLight;
123123
for ( int i = 0; i < NUM_DIR_LIGHTS; i ++ ) {
124124
125125
directionalLight = directionalLights[ i ];
126-
getDirectionalLightInfo( directionalLight, geometry, directLight );
126+
getDirectionalLightInfo( directionalLight, directLight );
127127
128128
#if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_DIR_LIGHT_SHADOWS )
129129
// NOTE: Depth gets larger away from the camera.
@@ -147,7 +147,7 @@ IncidentLight directLight;
147147
directLight.color = mix( prevColor, directLight.color, shouldFadeLastCascade ? ratio : 1.0 );
148148
149149
ReflectedLight prevLight = reflectedLight;
150-
RE_Direct( directLight, geometry, material, reflectedLight );
150+
RE_Direct( directLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight );
151151
152152
bool shouldBlend = UNROLLED_LOOP_INDEX != CSM_CASCADES - 1 || UNROLLED_LOOP_INDEX == CSM_CASCADES - 1 && linearDepth < cascadeCenter;
153153
float blendRatio = shouldBlend ? ratio : 1.0;
@@ -168,14 +168,14 @@ IncidentLight directLight;
168168
for ( int i = 0; i < NUM_DIR_LIGHTS; i ++ ) {
169169
170170
directionalLight = directionalLights[ i ];
171-
getDirectionalLightInfo( directionalLight, geometry, directLight );
171+
getDirectionalLightInfo( directionalLight, directLight );
172172
173173
#if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_DIR_LIGHT_SHADOWS )
174174
175175
directionalLightShadow = directionalLightShadows[ i ];
176176
if(linearDepth >= CSM_cascades[UNROLLED_LOOP_INDEX].x && linearDepth < CSM_cascades[UNROLLED_LOOP_INDEX].y) directLight.color *= ( directLight.visible && receiveShadow ) ? getShadow( directionalShadowMap[ i ], directionalLightShadow.shadowMapSize, directionalLightShadow.shadowBias, directionalLightShadow.shadowRadius, vDirectionalShadowCoord[ i ] ) : 1.0;
177177
178-
if(linearDepth >= CSM_cascades[UNROLLED_LOOP_INDEX].x && (linearDepth < CSM_cascades[UNROLLED_LOOP_INDEX].y || UNROLLED_LOOP_INDEX == CSM_CASCADES - 1)) RE_Direct( directLight, geometry, material, reflectedLight );
178+
if(linearDepth >= CSM_cascades[UNROLLED_LOOP_INDEX].x && (linearDepth < CSM_cascades[UNROLLED_LOOP_INDEX].y || UNROLLED_LOOP_INDEX == CSM_CASCADES - 1)) RE_Direct( directLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight );
179179
180180
#endif
181181
@@ -192,9 +192,9 @@ IncidentLight directLight;
192192
193193
directionalLight = directionalLights[ i ];
194194
195-
getDirectionalLightInfo( directionalLight, geometry, directLight );
195+
getDirectionalLightInfo( directionalLight, directLight );
196196
197-
RE_Direct( directLight, geometry, material, reflectedLight );
197+
RE_Direct( directLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight );
198198
199199
}
200200
#pragma unroll_loop_end
@@ -216,14 +216,14 @@ IncidentLight directLight;
216216
217217
directionalLight = directionalLights[ i ];
218218
219-
getDirectionalLightInfo( directionalLight, geometry, directLight );
219+
getDirectionalLightInfo( directionalLight, directLight );
220220
221221
#if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_DIR_LIGHT_SHADOWS )
222222
directionalLightShadow = directionalLightShadows[ i ];
223223
directLight.color *= ( directLight.visible && receiveShadow ) ? getShadow( directionalShadowMap[ i ], directionalLightShadow.shadowMapSize, directionalLightShadow.shadowBias, directionalLightShadow.shadowRadius, vDirectionalShadowCoord[ i ] ) : 1.0;
224224
#endif
225225
226-
RE_Direct( directLight, geometry, material, reflectedLight );
226+
RE_Direct( directLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight );
227227
228228
}
229229
#pragma unroll_loop_end
@@ -238,7 +238,7 @@ IncidentLight directLight;
238238
for ( int i = 0; i < NUM_RECT_AREA_LIGHTS; i ++ ) {
239239
240240
rectAreaLight = rectAreaLights[ i ];
241-
RE_Direct_RectArea( rectAreaLight, geometry, material, reflectedLight );
241+
RE_Direct_RectArea( rectAreaLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight );
242242
243243
}
244244
#pragma unroll_loop_end
@@ -253,7 +253,7 @@ IncidentLight directLight;
253253
254254
#if defined( USE_LIGHT_PROBES )
255255
256-
irradiance += getLightProbeIrradiance( lightProbe, geometry.normal );
256+
irradiance += getLightProbeIrradiance( lightProbe, geometryNormal );
257257
258258
#endif
259259
@@ -262,7 +262,7 @@ IncidentLight directLight;
262262
#pragma unroll_loop_start
263263
for ( int i = 0; i < NUM_HEMI_LIGHTS; i ++ ) {
264264
265-
irradiance += getHemisphereLightIrradiance( hemisphereLights[ i ], geometry.normal );
265+
irradiance += getHemisphereLightIrradiance( hemisphereLights[ i ], geometryNormal );
266266
267267
}
268268
#pragma unroll_loop_end

examples/jsm/materials/MeshGouraudMaterial.js

+14-18
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,11 @@ const GouraudShader = {
7474
7575
vec3 diffuse = vec3( 1.0 );
7676
77-
GeometricContext geometry;
78-
geometry.position = mvPosition.xyz;
79-
geometry.normal = normalize( transformedNormal );
80-
geometry.viewDir = ( isOrthographic ) ? vec3( 0, 0, 1 ) : normalize( -mvPosition.xyz );
77+
vec3 geometryPosition = mvPosition.xyz;
78+
vec3 geometryNormal = normalize( transformedNormal );
79+
vec3 geometryViewDir = ( isOrthographic ) ? vec3( 0, 0, 1 ) : normalize( -mvPosition.xyz );
8180
82-
GeometricContext backGeometry;
83-
backGeometry.position = geometry.position;
84-
backGeometry.normal = -geometry.normal;
85-
backGeometry.viewDir = geometry.viewDir;
81+
vec3 backGeometryNormal = - geometryNormal;
8682
8783
vLightFront = vec3( 0.0 );
8884
vIndirectFront = vec3( 0.0 );
@@ -99,7 +95,7 @@ const GouraudShader = {
9995
10096
#if defined( USE_LIGHT_PROBES )
10197
102-
vIndirectFront += getLightProbeIrradiance( lightProbe, geometry.normal );
98+
vIndirectFront += getLightProbeIrradiance( lightProbe, geometryNormal );
10399
104100
#endif
105101
@@ -109,7 +105,7 @@ const GouraudShader = {
109105
110106
#if defined( USE_LIGHT_PROBES )
111107
112-
vIndirectBack += getLightProbeIrradiance( lightProbe, backGeometry.normal );
108+
vIndirectBack += getLightProbeIrradiance( lightProbe, backGeometryNormal );
113109
114110
#endif
115111
@@ -120,9 +116,9 @@ const GouraudShader = {
120116
#pragma unroll_loop_start
121117
for ( int i = 0; i < NUM_POINT_LIGHTS; i ++ ) {
122118
123-
getPointLightInfo( pointLights[ i ], geometry, directLight );
119+
getPointLightInfo( pointLights[ i ], geometryPosition, directLight );
124120
125-
dotNL = dot( geometry.normal, directLight.direction );
121+
dotNL = dot( geometryNormal, directLight.direction );
126122
directLightColor_Diffuse = directLight.color;
127123
128124
vLightFront += saturate( dotNL ) * directLightColor_Diffuse;
@@ -143,9 +139,9 @@ const GouraudShader = {
143139
#pragma unroll_loop_start
144140
for ( int i = 0; i < NUM_SPOT_LIGHTS; i ++ ) {
145141
146-
getSpotLightInfo( spotLights[ i ], geometry, directLight );
142+
getSpotLightInfo( spotLights[ i ], geometryPosition, directLight );
147143
148-
dotNL = dot( geometry.normal, directLight.direction );
144+
dotNL = dot( geometryNormal, directLight.direction );
149145
directLightColor_Diffuse = directLight.color;
150146
151147
vLightFront += saturate( dotNL ) * directLightColor_Diffuse;
@@ -165,9 +161,9 @@ const GouraudShader = {
165161
#pragma unroll_loop_start
166162
for ( int i = 0; i < NUM_DIR_LIGHTS; i ++ ) {
167163
168-
getDirectionalLightInfo( directionalLights[ i ], geometry, directLight );
164+
getDirectionalLightInfo( directionalLights[ i ], directLight );
169165
170-
dotNL = dot( geometry.normal, directLight.direction );
166+
dotNL = dot( geometryNormal, directLight.direction );
171167
directLightColor_Diffuse = directLight.color;
172168
173169
vLightFront += saturate( dotNL ) * directLightColor_Diffuse;
@@ -188,11 +184,11 @@ const GouraudShader = {
188184
#pragma unroll_loop_start
189185
for ( int i = 0; i < NUM_HEMI_LIGHTS; i ++ ) {
190186
191-
vIndirectFront += getHemisphereLightIrradiance( hemisphereLights[ i ], geometry.normal );
187+
vIndirectFront += getHemisphereLightIrradiance( hemisphereLights[ i ], geometryNormal );
192188
193189
#ifdef DOUBLE_SIDED
194190
195-
vIndirectBack += getHemisphereLightIrradiance( hemisphereLights[ i ], backGeometry.normal );
191+
vIndirectBack += getHemisphereLightIrradiance( hemisphereLights[ i ], backGeometryNormal );
196192
197193
#endif
198194

examples/jsm/renderers/webgl-legacy/nodes/WebGLNodeBuilder.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ class WebGLNodeBuilder extends NodeBuilder {
220220
this.addSlot( 'fragment', new SlotNode( {
221221
node: material.clearcoatNormalNode,
222222
nodeType: 'vec3',
223-
source: 'vec3 clearcoatNormal = geometryNormal;',
223+
source: 'vec3 clearcoatNormal = nonPerturbedNormal;',
224224
target: 'vec3 clearcoatNormal = %RESULT%;'
225225
} ) );
226226

examples/jsm/shaders/MMDToonShader.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@ struct BlinnPhongMaterial {
2727
2828
};
2929
30-
void RE_Direct_BlinnPhong( const in IncidentLight directLight, const in GeometricContext geometry, const in BlinnPhongMaterial material, inout ReflectedLight reflectedLight ) {
30+
void RE_Direct_BlinnPhong( const in IncidentLight directLight, const in vec3 geometryPosition, const in vec3 geometryNormal, const in vec3 geometryViewDir, const in vec3 geometryClearcoatNormal, const in BlinnPhongMaterial material, inout ReflectedLight reflectedLight ) {
3131
32-
vec3 irradiance = getGradientIrradiance( geometry.normal, directLight.direction ) * directLight.color;
32+
vec3 irradiance = getGradientIrradiance( geometryNormal, directLight.direction ) * directLight.color;
3333
3434
reflectedLight.directDiffuse += irradiance * BRDF_Lambert( material.diffuseColor );
3535
36-
reflectedLight.directSpecular += irradiance * BRDF_BlinnPhong( directLight.direction, geometry.viewDir, geometry.normal, material.specularColor, material.specularShininess ) * material.specularStrength;
36+
reflectedLight.directSpecular += irradiance * BRDF_BlinnPhong( directLight.direction, geometryViewDir, geometryNormal, material.specularColor, material.specularShininess ) * material.specularStrength;
3737
3838
}
3939
40-
void RE_IndirectDiffuse_BlinnPhong( const in vec3 irradiance, const in GeometricContext geometry, const in BlinnPhongMaterial material, inout ReflectedLight reflectedLight ) {
40+
void RE_IndirectDiffuse_BlinnPhong( const in vec3 irradiance, const in vec3 geometryPosition, const in vec3 geometryNormal, const in vec3 geometryViewDir, const in vec3 geometryClearcoatNormal, const in BlinnPhongMaterial material, inout ReflectedLight reflectedLight ) {
4141
4242
reflectedLight.indirectDiffuse += irradiance * BRDF_Lambert( material.diffuseColor );
4343

examples/jsm/shaders/SubsurfaceScatteringShader.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ const SubsurfaceScatteringShader = {
5757
'uniform float thicknessAttenuation;',
5858
'uniform vec3 thicknessColor;',
5959

60-
'void RE_Direct_Scattering(const in IncidentLight directLight, const in vec2 uv, const in GeometricContext geometry, inout ReflectedLight reflectedLight) {',
60+
'void RE_Direct_Scattering(const in IncidentLight directLight, const in vec2 uv, const in vec3 geometryPosition, const in vec3 geometryNormal, const in vec3 geometryViewDir, const in vec3 geometryClearcoatNormal, inout ReflectedLight reflectedLight) {',
6161
' vec3 thickness = thicknessColor * texture2D(thicknessMap, uv).r;',
62-
' vec3 scatteringHalf = normalize(directLight.direction + (geometry.normal * thicknessDistortion));',
63-
' float scatteringDot = pow(saturate(dot(geometry.viewDir, -scatteringHalf)), thicknessPower) * thicknessScale;',
62+
' vec3 scatteringHalf = normalize(directLight.direction + (geometryNormal * thicknessDistortion));',
63+
' float scatteringDot = pow(saturate(dot(geometryViewDir, -scatteringHalf)), thicknessPower) * thicknessScale;',
6464
' vec3 scatteringIllu = (scatteringDot + thicknessAmbient) * thickness;',
6565
' reflectedLight.directDiffuse += scatteringIllu * thicknessAttenuation * directLight.color;',
6666
'}',
@@ -69,12 +69,12 @@ const SubsurfaceScatteringShader = {
6969

7070
replaceAll(
7171
ShaderChunk[ 'lights_fragment_begin' ],
72-
'RE_Direct( directLight, geometry, material, reflectedLight );',
72+
'RE_Direct( directLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight );',
7373
[
74-
'RE_Direct( directLight, geometry, material, reflectedLight );',
74+
'RE_Direct( directLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, reflectedLight );',
7575

7676
'#if defined( SUBSURFACE ) && defined( USE_UV )',
77-
' RE_Direct_Scattering(directLight, vUv, geometry, reflectedLight);',
77+
' RE_Direct_Scattering(directLight, vUv, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, reflectedLight);',
7878
'#endif',
7979
].join( '\n' )
8080
),

src/renderers/shaders/ShaderChunk/aomap_fragment.glsl.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default /* glsl */`
88
99
#if defined( USE_ENVMAP ) && defined( STANDARD )
1010
11-
float dotNV = saturate( dot( geometry.normal, geometry.viewDir ) );
11+
float dotNV = saturate( dot( geometryNormal, geometryViewDir ) );
1212
1313
reflectedLight.indirectSpecular *= computeSpecularOcclusion( dotNV, ambientOcclusion, material.roughness );
1414
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export default /* glsl */`
22
#ifdef USE_CLEARCOAT
33
4-
vec3 clearcoatNormal = geometryNormal;
4+
vec3 clearcoatNormal = nonPerturbedNormal;
55
66
#endif
77
`;

src/renderers/shaders/ShaderChunk/common.glsl.js

-9
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,6 @@ struct ReflectedLight {
5252
vec3 indirectSpecular;
5353
};
5454
55-
struct GeometricContext {
56-
vec3 position;
57-
vec3 normal;
58-
vec3 viewDir;
59-
#ifdef USE_CLEARCOAT
60-
vec3 clearcoatNormal;
61-
#endif
62-
};
63-
6455
#ifdef USE_ALPHAHASH
6556
6657
varying vec3 vPosition;

0 commit comments

Comments
 (0)