I’m trying to match my UV coordinates image in shader but it won’t do but it has already loaded the texture so everything is fine… I forgot to match the UV coordinates can anyone help me?
Example source code here…
https://codesandbox.io/p/sandbox/vigorous-joji-nzgfq4?file=%2Fsrc%2Findex.js%3A10%2C13
This is my shaders…
const fragmentShader = `
varying vec2 vUv;
vec3 colorA = vec3(0.008,0.895,0.940);
vec3 colorB = vec3(0.129,0.299,1.000);
uniform sampler2D u_texture;
uniform vec2 u_resolution;
void main() {
vec2 st = gl_FragCoord.xy / u_resolution.xy;
vec4 img = texture2D(u_texture,st);
vec2 normalizedPixel = gl_FragCoord.xy/500.0;
vec3 color = mix(colorA, colorB, normalizedPixel.x);
gl_FragColor = vec4(img.xyz ,1.0);
}
`
const vertexShader = `
uniform float u_time;
varying vec2 vUv;
void main() {
vec4 modelPosition = modelMatrix * vec4(position, 1.0);
modelPosition.y += sin(modelPosition.x * 4.0 + u_time * 2.0) * 0.2;
modelPosition.x += sin(modelPosition.y * 4.0 + u_time * 2.0) * 0.2;
modelPosition.z += sin((modelPosition.y * 4.0 + modelPosition.x * 4.0) + u_time * 2.0) * 0.2;
// Uncomment the code and hit the refresh button below for a more complex effect 🪄
// modelPosition.y += sin(modelPosition.z * 6.0 + u_time * 2.0) * 0.1;
vec4 viewPosition = viewMatrix * modelPosition;
vec4 projectedPosition = projectionMatrix * viewPosition;
gl_Position = projectedPosition;
}
`
And this is the react-three codes
export const Scene = () => {
const shadersRef = useRef(null)
const texture = useLoader(THREE.TextureLoader, './img/ase3.jpg');
const data = useMemo(() => ({
u_texture: { value: texture },
u_time: { value: 0.0 },
u_resolution: { value: new THREE.Vector2(0.0,0.0) },
transform: { value: new THREE.Vector3(0,0,0) }
}),[texture])
useFrame(({ clock }) => {
if (shadersRef.current) {
shadersRef.current.uniforms.u_time.value = clock.getElapsedTime();
}
});
return (
<>
<ambientLight />
<mesh position={[0,0,0]} rotation={[-Math.PI / 2 , 0, 0]}>
<planeGeometry args={[2,2,64,64]} />
<shaderMaterial
ref={shadersRef}
uniforms={data}
fragmentShader={fragmentShader}
vertexShader={vertexShader}
side={THREE.DoubleSide}
/>
</mesh>
</>
)
}