If I use this code getting lots of error which are not identified.
My code is the bellow.
import * as THREE from 'three'
import { MeshBVHUniformStruct, shaderStructs, shaderIntersectFunction } from "three-mesh-bvh";
// import { fragmentShader } from './fragmentShader';
// import { vertexShader } from './vertexShader';
// import vertexShader from './vertex.glsl'
const getVersion = () => parseInt(THREE.REVISION.replace(/\D+/g, ''));
const version = /* @__PURE__ */getVersion();
export const shaderMaterial = ({ envMap, color, ior }) => {
const material = new THREE.ShaderMaterial({
uniforms: {
envMap: null,
bounces: 3,
ior: 2.4,
correctMips: true,
aberrationStrength: 0.01,
fresnel: 0,
bvh: /* @__PURE__ */ new MeshBVHUniformStruct(),
color: /* @__PURE__ */ new THREE.Color('white'),
resolution: /* @__PURE__ */ new THREE.Vector2(),
viewMatrixInverse: /* @__PURE__ */ new THREE.Matrix4(),
projectionMatrixInverse: /* @__PURE__ */ new THREE.Matrix4(),
},
vertexShader: `
uniform mat4 viewMatrixInverse;
varying vec3 vWorldPosition;
varying vec3 vNormal;
varying mat4 vModelMatrixInverse;
#ifdef USE_INSTANCING_COLOR
varying vec3 vInstanceColor;
#endif
void main() {
vec4 transformedNormal = vec4(normal, 0.0);
vec4 transformedPosition = vec4(position, 1.0);
#ifdef USE_INSTANCING
transformedNormal = instanceMatrix * transformedNormal;
transformedPosition = instanceMatrix * transformedPosition;
#endif
#ifdef USE_INSTANCING
vModelMatrixInverse = inverse(modelMatrix * instanceMatrix);
#else
vModelMatrixInverse = inverse(modelMatrix);
#endif
#ifdef USE_INSTANCING_COLOR
vInstanceColor = instanceColor.rgb;
#endif
vWorldPosition = (modelMatrix * transformedPosition).xyz;
vNormal = normalize((viewMatrixInverse * vec4(normalMatrix * transformedNormal.xyz, 0.0)).xyz);
gl_Position = projectionMatrix * viewMatrix * modelMatrix * transformedPosition;
}`,
fragmentShader: `
#define ENVMAP_TYPE_CUBE_UV
precision highp isampler2D;
precision highp usampler2D;
varying vec3 vWorldPosition;
varying vec3 vNormal;
varying mat4 vModelMatrixInverse;
#ifdef USE_INSTANCING_COLOR
varying vec3 vInstanceColor;
#endif
#ifdef ENVMAP_TYPE_CUBEM
uniform samplerCube envMap;
#else
uniform sampler2D envMap;
#endif
uniform float bounces;
${shaderStructs}
${shaderIntersectFunction}
uniform BVH bvh;
uniform float ior;
uniform bool correctMips;
uniform vec2 resolution;
uniform float fresnel;
uniform mat4 modelMatrix;
uniform mat4 projectionMatrixInverse;
uniform mat4 viewMatrixInverse;
uniform float aberrationStrength;
uniform vec3 color;
float fresnelFunc(vec3 viewDirection, vec3 worldNormal) {
return pow( 1.0 + dot( viewDirection, worldNormal), 10.0 );
}
vec3 totalInternalReflection(vec3 ro, vec3 rd, vec3 normal, float ior, mat4 modelMatrixInverse) {
vec3 rayOrigin = ro;
vec3 rayDirection = rd;
rayDirection = refract(rayDirection, normal, 1.0 / ior);
rayOrigin = vWorldPosition + rayDirection * 0.001;
rayOrigin = (modelMatrixInverse * vec4(rayOrigin, 1.0)).xyz;
rayDirection = normalize((modelMatrixInverse * vec4(rayDirection, 0.0)).xyz);
for(float i = 0.0; i < bounces; i++) {
uvec4 faceIndices = uvec4( 0u );
vec3 faceNormal = vec3( 0.0, 0.0, 1.0 );
vec3 barycoord = vec3( 0.0 );
float side = 1.0;
float dist = 0.0;
bvhIntersectFirstHit( bvh, rayOrigin, rayDirection, faceIndices, faceNormal, barycoord, side, dist );
vec3 hitPos = rayOrigin + rayDirection * max(dist - 0.001, 0.0);
vec3 tempDir = refract(rayDirection, faceNormal, ior);
if (length(tempDir) != 0.0) {
rayDirection = tempDir;
break;
}
rayDirection = reflect(rayDirection, faceNormal);
rayOrigin = hitPos + rayDirection * 0.01;
}
rayDirection = normalize((modelMatrix * vec4(rayDirection, 0.0)).xyz);
return rayDirection;
}
#include <common>
#include <cube_uv_reflection_fragment>
#ifdef ENVMAP_TYPE_CUBEM
vec4 textureGradient(samplerCube envMap, vec3 rayDirection, vec3 directionCamPerfect) {
return textureGrad(envMap, rayDirection, dFdx(correctMips ? directionCamPerfect: rayDirection), dFdy(correctMips ? directionCamPerfect: rayDirection));
}
#else
vec4 textureGradient(sampler2D envMap, vec3 rayDirection, vec3 directionCamPerfect) {
vec2 uvv = equirectUv( rayDirection );
vec2 smoothUv = equirectUv( directionCamPerfect );
return textureGrad(envMap, uvv, dFdx(correctMips ? smoothUv : uvv), dFdy(correctMips ? smoothUv : uvv));
}
#endif
void main() {
vec2 uv = gl_FragCoord.xy / resolution;
vec3 directionCamPerfect = (projectionMatrixInverse * vec4(uv * 2.0 - 1.0, 0.0, 1.0)).xyz;
directionCamPerfect = (viewMatrixInverse * vec4(directionCamPerfect, 0.0)).xyz;
directionCamPerfect = normalize(directionCamPerfect);
vec3 normal = vNormal;
vec3 rayOrigin = cameraPosition;
vec3 rayDirection = normalize(vWorldPosition - cameraPosition);
vec3 finalColor;
#ifdef CHROMATIC_ABERRATIONS
vec3 rayDirectionG = totalInternalReflection(rayOrigin, rayDirection, normal, max(ior, 1.0), vModelMatrixInverse);
#ifdef FAST_CHROMA
vec3 rayDirectionR = normalize(rayDirectionG + 1.0 * vec3(aberrationStrength / 2.0));
vec3 rayDirectionB = normalize(rayDirectionG - 1.0 * vec3(aberrationStrength / 2.0));
#else
vec3 rayDirectionR = totalInternalReflection(rayOrigin, rayDirection, normal, max(ior * (1.0 - aberrationStrength), 1.0), vModelMatrixInverse);
vec3 rayDirectionB = totalInternalReflection(rayOrigin, rayDirection, normal, max(ior * (1.0 + aberrationStrength), 1.0), vModelMatrixInverse);
#endif
float finalColorR = textureGradient(envMap, rayDirectionR, directionCamPerfect).r;
float finalColorG = textureGradient(envMap, rayDirectionG, directionCamPerfect).g;
float finalColorB = textureGradient(envMap, rayDirectionB, directionCamPerfect).b;
finalColor = vec3(finalColorR, finalColorG, finalColorB);
#else
rayDirection = totalInternalReflection(rayOrigin, rayDirection, normal, max(ior, 1.0), vModelMatrixInverse);
finalColor = textureGradient(envMap, rayDirection, directionCamPerfect).rgb;
#endif
finalColor *= color;
#ifdef USE_INSTANCING_COLOR
finalColor *= vInstanceColor;
#endif
vec3 viewDirection = normalize(vWorldPosition - cameraPosition);
float nFresnel = fresnelFunc(viewDirection, normal) * fresnel;
gl_FragColor = vec4(mix(finalColor, vec3(1.0), nFresnel), 1.0);
#include <tonemapping_fragment>
#include <${version >= 154 ? 'colorspace_fragment' : 'encodings_fragment'}>
}`,
// transparent: true,
});
return material;
}
Errors are the bellow.
THREE.WebGLProgram: Shader Error 0 - VALIDATE_STATUS false
Program Info Log: Fragment shader is not compiled.
FRAGMENT
ERROR: 0:554: 'CUBEUV_MAX_MIP' : undeclared identifier
ERROR: 0:555: 'CUBEUV_TEXEL_WIDTH' : undeclared identifier
ERROR: 0:556: 'CUBEUV_TEXEL_HEIGHT' : undeclared identifier
ERROR: 0:593: 'CUBEUV_MAX_MIP' : undeclared identifier
549: uv.y += faceSize;
550: face -= 3.0;
551: }
552: uv.x += face * faceSize;
553: uv.x += filterInt * 3.0 * cubeUV_minTileSize;
> 554: uv.y += 4.0 * ( exp2( CUBEUV_MAX_MIP ) - faceSize );
555: uv.x *= CUBEUV_TEXEL_WIDTH;
556: uv.y *= CUBEUV_TEXEL_HEIGHT;
557: #ifdef texture2DGradEXT
558: return texture2DGradEXT( envMap, uv, vec2( 0.0 ), vec2( 0.0 ) ).rgb;
559: #else
560: return texture2D( envMap, uv ).rgb;