@@ -20,22 +20,23 @@ export const MeshRefractionMaterial = /* @__PURE__ */ shaderMaterial(
20
20
fresnel : 0 ,
21
21
bvh : /* @__PURE__ */ new MeshBVHUniformStruct ( ) ,
22
22
color : /* @__PURE__ */ new THREE . Color ( 'white' ) ,
23
+ opacity : 1 ,
23
24
resolution : /* @__PURE__ */ new THREE . Vector2 ( ) ,
24
25
viewMatrixInverse : /* @__PURE__ */ new THREE . Matrix4 ( ) ,
25
26
projectionMatrixInverse : /* @__PURE__ */ new THREE . Matrix4 ( ) ,
26
27
} ,
27
28
/*glsl*/ `
28
29
uniform mat4 viewMatrixInverse;
29
30
30
- varying vec3 vWorldPosition;
31
+ varying vec3 vWorldPosition;
31
32
varying vec3 vNormal;
32
33
varying mat4 vModelMatrixInverse;
33
34
34
- #ifdef USE_INSTANCING_COLOR
35
- varying vec3 vInstanceColor;
36
- #endif
35
+ #include <color_pars_vertex>
36
+
37
+ void main() {
38
+ #include <color_vertex>
37
39
38
- void main() {
39
40
vec4 transformedNormal = vec4(normal, 0.0);
40
41
vec4 transformedPosition = vec4(position, 1.0);
41
42
#ifdef USE_INSTANCING
@@ -49,10 +50,6 @@ export const MeshRefractionMaterial = /* @__PURE__ */ shaderMaterial(
49
50
vModelMatrixInverse = inverse(modelMatrix);
50
51
#endif
51
52
52
- #ifdef USE_INSTANCING_COLOR
53
- vInstanceColor = instanceColor.rgb;
54
- #endif
55
-
56
53
vWorldPosition = (modelMatrix * transformedPosition).xyz;
57
54
vNormal = normalize((viewMatrixInverse * vec4(normalMatrix * transformedNormal.xyz, 0.0)).xyz);
58
55
gl_Position = projectionMatrix * viewMatrix * modelMatrix * transformedPosition;
@@ -65,16 +62,14 @@ export const MeshRefractionMaterial = /* @__PURE__ */ shaderMaterial(
65
62
varying vec3 vNormal;
66
63
varying mat4 vModelMatrixInverse;
67
64
68
- #ifdef USE_INSTANCING_COLOR
69
- varying vec3 vInstanceColor;
70
- #endif
71
-
65
+ #include <color_pars_fragment>
66
+
72
67
#ifdef ENVMAP_TYPE_CUBEM
73
68
uniform samplerCube envMap;
74
69
#else
75
70
uniform sampler2D envMap;
76
71
#endif
77
-
72
+
78
73
uniform float bounces;
79
74
${ shaderStructs }
80
75
${ shaderIntersectFunction }
@@ -88,11 +83,12 @@ export const MeshRefractionMaterial = /* @__PURE__ */ shaderMaterial(
88
83
uniform mat4 viewMatrixInverse;
89
84
uniform float aberrationStrength;
90
85
uniform vec3 color;
91
-
86
+ uniform float opacity;
87
+
92
88
float fresnelFunc(vec3 viewDirection, vec3 worldNormal) {
93
89
return pow( 1.0 + dot( viewDirection, worldNormal), 10.0 );
94
90
}
95
-
91
+
96
92
vec3 totalInternalReflection(vec3 ro, vec3 rd, vec3 normal, float ior, mat4 modelMatrixInverse) {
97
93
vec3 rayOrigin = ro;
98
94
vec3 rayDirection = rd;
@@ -107,7 +103,7 @@ export const MeshRefractionMaterial = /* @__PURE__ */ shaderMaterial(
107
103
float side = 1.0;
108
104
float dist = 0.0;
109
105
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);
111
107
vec3 tempDir = refract(rayDirection, faceNormal, ior);
112
108
if (length(tempDir) != 0.0) {
113
109
rayDirection = tempDir;
@@ -119,10 +115,10 @@ export const MeshRefractionMaterial = /* @__PURE__ */ shaderMaterial(
119
115
rayDirection = normalize((modelMatrix * vec4(rayDirection, 0.0)).xyz);
120
116
return rayDirection;
121
117
}
122
-
118
+
123
119
#include <common>
124
120
#include <cube_uv_reflection_fragment>
125
-
121
+
126
122
#ifdef ENVMAP_TYPE_CUBEM
127
123
vec4 textureGradient(samplerCube envMap, vec3 rayDirection, vec3 directionCamPerfect) {
128
124
return textureGrad(envMap, rayDirection, dFdx(correctMips ? directionCamPerfect: rayDirection), dFdy(correctMips ? directionCamPerfect: rayDirection));
@@ -134,7 +130,7 @@ export const MeshRefractionMaterial = /* @__PURE__ */ shaderMaterial(
134
130
return textureGrad(envMap, uvv, dFdx(correctMips ? smoothUv : uvv), dFdy(correctMips ? smoothUv : uvv));
135
131
}
136
132
#endif
137
-
133
+
138
134
void main() {
139
135
vec2 uv = gl_FragCoord.xy / resolution;
140
136
vec3 directionCamPerfect = (projectionMatrixInverse * vec4(uv * 2.0 - 1.0, 0.0, 1.0)).xyz;
@@ -143,10 +139,13 @@ export const MeshRefractionMaterial = /* @__PURE__ */ shaderMaterial(
143
139
vec3 normal = vNormal;
144
140
vec3 rayOrigin = cameraPosition;
145
141
vec3 rayDirection = normalize(vWorldPosition - cameraPosition);
146
- vec3 finalColor;
142
+
143
+ vec4 diffuseColor = vec4(color, opacity);
144
+ #include <color_fragment>
145
+
147
146
#ifdef CHROMATIC_ABERRATIONS
148
147
vec3 rayDirectionG = totalInternalReflection(rayOrigin, rayDirection, normal, max(ior, 1.0), vModelMatrixInverse);
149
- #ifdef FAST_CHROMA
148
+ #ifdef FAST_CHROMA
150
149
vec3 rayDirectionR = normalize(rayDirectionG + 1.0 * vec3(aberrationStrength / 2.0));
151
150
vec3 rayDirectionB = normalize(rayDirectionG - 1.0 * vec3(aberrationStrength / 2.0));
152
151
#else
@@ -156,20 +155,16 @@ export const MeshRefractionMaterial = /* @__PURE__ */ shaderMaterial(
156
155
float finalColorR = textureGradient(envMap, rayDirectionR, directionCamPerfect).r;
157
156
float finalColorG = textureGradient(envMap, rayDirectionG, directionCamPerfect).g;
158
157
float finalColorB = textureGradient(envMap, rayDirectionB, directionCamPerfect).b;
159
- finalColor = vec3(finalColorR, finalColorG, finalColorB);
158
+ diffuseColor.rgb * = vec3(finalColorR, finalColorG, finalColorB);
160
159
#else
161
160
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;
168
162
#endif
169
163
170
164
vec3 viewDirection = normalize(vWorldPosition - cameraPosition);
171
165
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
+
173
168
#include <tonemapping_fragment>
174
169
#include <${ version >= 154 ? 'colorspace_fragment' : 'encodings_fragment' } >
175
170
}`
0 commit comments