The issue arises from the discrepancies in the various versions of THREE
WebGL
GLSL
. Your current version is r82
, which utilizes GLSL 1.0
, requiring the use of texture2D
that has been deprecated in GLSL 3.0
, the version used by ShaderToy
.
For details on GLSL
versions corresponding to different THREE
releases, refer to
To determine the current GLSL
version for a specific THREE
release, check from r119
onwards where the GLSL
version is automatically added.
For a comprehensive guide on migration between versions r119
and r120
, visit https://github.com/mrdoob/three.js/wiki/Migration-Guide#r119--r120
const renderer = new THREE.WebGLRenderer();
const gl = renderer.getContext();
const glslVersion = gl.getParameter(gl.SHADING_LANGUAGE_VERSION); console.log('GLSL Version:', glslVersion);
Furthermore, ensure to rename the main function to main
as ShaderToy
recognizes mainImage
/ fragColor => gl_FragColor
conventions. ShaderToy
operates within its own specific environment and abstractions.
Image shaders execute the mainImage() function to create
procedural images by determining a color for each pixel.
Even after making all the necessary changes, if you still encounter a black screen, consider adjusting the position of your camera
as it might be too close to the scene.
Try setting:
camera.position.z = 10;
//...
return (lightIntensity * (k_d * dotLN + k_s * pow(dotRV, alpha)) * 0.5 + 0.5 * texture2D(iChannel0, ref.xy).xyz);
//...
void main() {
vec2 fragCoord = gl_FragCoord.xy;
vec3 dir = rayDirection(45.0, iResolution.xy, fragCoord);
vec3 eye = vec3(0.0, 0.0, 5.0);
vec2 sdf = shortestDistanceToSurface(eye, dir, MIN_DIST, MAX_DIST);
float dist = sdf.x;
if (dist > MAX_DIST - EPSILON) {
gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);
return;
}
vec3 p = eye + dist * dir;
vec3 N = estimateNormal(p);
float l1 = length(cross(vec3(0., 0.0, 1.), N));
l1 = smoothstep(0.65, 0.6, l1) - smoothstep(l1, 0.65, 0.6);
float l2 = length(cross(vec3(0., 0.01, 1.05), N));
l2 = smoothstep(0.65, 0.6, l2) - smoothstep(l2, 0.65, 0.6);
float l3 = length(cross(vec3(0.02, 0.0, 1.1), N));
l3 = smoothstep(0.65, 0.59, l3) - smoothstep(l3, 0.65, 0.59);
vec3 color = vec3(l1, l2, l3);
gl_FragColor = vec4(color, 1.0);
}
Implement these changes along with the updated scripts to resolve the issue and enhance the rendering of your scene.