Skip to content

Commit 7ceab1a

Browse files
fix(MeshRefractionMaterial): use color chunks, support opacity prop (#2020)
1 parent 88cb779 commit 7ceab1a

File tree

1 file changed

+25
-30
lines changed

1 file changed

+25
-30
lines changed

src/materials/MeshRefractionMaterial.tsx

+25-30
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,23 @@ export const MeshRefractionMaterial = /* @__PURE__ */ shaderMaterial(
2020
fresnel: 0,
2121
bvh: /* @__PURE__ */ new MeshBVHUniformStruct(),
2222
color: /* @__PURE__ */ new THREE.Color('white'),
23+
opacity: 1,
2324
resolution: /* @__PURE__ */ new THREE.Vector2(),
2425
viewMatrixInverse: /* @__PURE__ */ new THREE.Matrix4(),
2526
projectionMatrixInverse: /* @__PURE__ */ new THREE.Matrix4(),
2627
},
2728
/*glsl*/ `
2829
uniform mat4 viewMatrixInverse;
2930
30-
varying vec3 vWorldPosition;
31+
varying vec3 vWorldPosition;
3132
varying vec3 vNormal;
3233
varying mat4 vModelMatrixInverse;
3334
34-
#ifdef USE_INSTANCING_COLOR
35-
varying vec3 vInstanceColor;
36-
#endif
35+
#include <color_pars_vertex>
36+
37+
void main() {
38+
#include <color_vertex>
3739
38-
void main() {
3940
vec4 transformedNormal = vec4(normal, 0.0);
4041
vec4 transformedPosition = vec4(position, 1.0);
4142
#ifdef USE_INSTANCING
@@ -49,10 +50,6 @@ export const MeshRefractionMaterial = /* @__PURE__ */ shaderMaterial(
4950
vModelMatrixInverse = inverse(modelMatrix);
5051
#endif
5152
52-
#ifdef USE_INSTANCING_COLOR
53-
vInstanceColor = instanceColor.rgb;
54-
#endif
55-
5653
vWorldPosition = (modelMatrix * transformedPosition).xyz;
5754
vNormal = normalize((viewMatrixInverse * vec4(normalMatrix * transformedNormal.xyz, 0.0)).xyz);
5855
gl_Position = projectionMatrix * viewMatrix * modelMatrix * transformedPosition;
@@ -65,16 +62,14 @@ export const MeshRefractionMaterial = /* @__PURE__ */ shaderMaterial(
6562
varying vec3 vNormal;
6663
varying mat4 vModelMatrixInverse;
6764
68-
#ifdef USE_INSTANCING_COLOR
69-
varying vec3 vInstanceColor;
70-
#endif
71-
65+
#include <color_pars_fragment>
66+
7267
#ifdef ENVMAP_TYPE_CUBEM
7368
uniform samplerCube envMap;
7469
#else
7570
uniform sampler2D envMap;
7671
#endif
77-
72+
7873
uniform float bounces;
7974
${shaderStructs}
8075
${shaderIntersectFunction}
@@ -88,11 +83,12 @@ export const MeshRefractionMaterial = /* @__PURE__ */ shaderMaterial(
8883
uniform mat4 viewMatrixInverse;
8984
uniform float aberrationStrength;
9085
uniform vec3 color;
91-
86+
uniform float opacity;
87+
9288
float fresnelFunc(vec3 viewDirection, vec3 worldNormal) {
9389
return pow( 1.0 + dot( viewDirection, worldNormal), 10.0 );
9490
}
95-
91+
9692
vec3 totalInternalReflection(vec3 ro, vec3 rd, vec3 normal, float ior, mat4 modelMatrixInverse) {
9793
vec3 rayOrigin = ro;
9894
vec3 rayDirection = rd;
@@ -107,7 +103,7 @@ export const MeshRefractionMaterial = /* @__PURE__ */ shaderMaterial(
107103
float side = 1.0;
108104
float dist = 0.0;
109105
bvhIntersectFirstHit( bvh, rayOrigin, rayDirection, faceIndices, faceNormal, barycoord, side, dist );
110-
vec3 hitPos = rayOrigin + rayDirection * max(dist - 0.001, 0.0);
106+
vec3 hitPos = rayOrigin + rayDirection * max(dist - 0.001, 0.0);
111107
vec3 tempDir = refract(rayDirection, faceNormal, ior);
112108
if (length(tempDir) != 0.0) {
113109
rayDirection = tempDir;
@@ -119,10 +115,10 @@ export const MeshRefractionMaterial = /* @__PURE__ */ shaderMaterial(
119115
rayDirection = normalize((modelMatrix * vec4(rayDirection, 0.0)).xyz);
120116
return rayDirection;
121117
}
122-
118+
123119
#include <common>
124120
#include <cube_uv_reflection_fragment>
125-
121+
126122
#ifdef ENVMAP_TYPE_CUBEM
127123
vec4 textureGradient(samplerCube envMap, vec3 rayDirection, vec3 directionCamPerfect) {
128124
return textureGrad(envMap, rayDirection, dFdx(correctMips ? directionCamPerfect: rayDirection), dFdy(correctMips ? directionCamPerfect: rayDirection));
@@ -134,7 +130,7 @@ export const MeshRefractionMaterial = /* @__PURE__ */ shaderMaterial(
134130
return textureGrad(envMap, uvv, dFdx(correctMips ? smoothUv : uvv), dFdy(correctMips ? smoothUv : uvv));
135131
}
136132
#endif
137-
133+
138134
void main() {
139135
vec2 uv = gl_FragCoord.xy / resolution;
140136
vec3 directionCamPerfect = (projectionMatrixInverse * vec4(uv * 2.0 - 1.0, 0.0, 1.0)).xyz;
@@ -143,10 +139,13 @@ export const MeshRefractionMaterial = /* @__PURE__ */ shaderMaterial(
143139
vec3 normal = vNormal;
144140
vec3 rayOrigin = cameraPosition;
145141
vec3 rayDirection = normalize(vWorldPosition - cameraPosition);
146-
vec3 finalColor;
142+
143+
vec4 diffuseColor = vec4(color, opacity);
144+
#include <color_fragment>
145+
147146
#ifdef CHROMATIC_ABERRATIONS
148147
vec3 rayDirectionG = totalInternalReflection(rayOrigin, rayDirection, normal, max(ior, 1.0), vModelMatrixInverse);
149-
#ifdef FAST_CHROMA
148+
#ifdef FAST_CHROMA
150149
vec3 rayDirectionR = normalize(rayDirectionG + 1.0 * vec3(aberrationStrength / 2.0));
151150
vec3 rayDirectionB = normalize(rayDirectionG - 1.0 * vec3(aberrationStrength / 2.0));
152151
#else
@@ -156,20 +155,16 @@ export const MeshRefractionMaterial = /* @__PURE__ */ shaderMaterial(
156155
float finalColorR = textureGradient(envMap, rayDirectionR, directionCamPerfect).r;
157156
float finalColorG = textureGradient(envMap, rayDirectionG, directionCamPerfect).g;
158157
float finalColorB = textureGradient(envMap, rayDirectionB, directionCamPerfect).b;
159-
finalColor = vec3(finalColorR, finalColorG, finalColorB);
158+
diffuseColor.rgb *= vec3(finalColorR, finalColorG, finalColorB);
160159
#else
161160
rayDirection = totalInternalReflection(rayOrigin, rayDirection, normal, max(ior, 1.0), vModelMatrixInverse);
162-
finalColor = textureGradient(envMap, rayDirection, directionCamPerfect).rgb;
163-
#endif
164-
165-
finalColor *= color;
166-
#ifdef USE_INSTANCING_COLOR
167-
finalColor *= vInstanceColor;
161+
diffuseColor.rgb *= textureGradient(envMap, rayDirection, directionCamPerfect).rgb;
168162
#endif
169163
170164
vec3 viewDirection = normalize(vWorldPosition - cameraPosition);
171165
float nFresnel = fresnelFunc(viewDirection, normal) * fresnel;
172-
gl_FragColor = vec4(mix(finalColor, vec3(1.0), nFresnel), 1.0);
166+
gl_FragColor = vec4(mix(diffuseColor.rgb, vec3(1.0), nFresnel), diffuseColor.a);
167+
173168
#include <tonemapping_fragment>
174169
#include <${version >= 154 ? 'colorspace_fragment' : 'encodings_fragment'}>
175170
}`

0 commit comments

Comments
 (0)