Recently diving into the world of WebGl, I decided to experiment with a simple example. Here is how I set up my script:
Here's my setup script
import testVertexShader from './shaders/test/vertex.glsl'
import testFragmentShader from './shaders/test/fragment.glsl'
const gui = new dat.GUI()
const canvas = document.querySelector('canvas.webgl')
const scene = new THREE.Scene()
// Textures
const textureLoader = new THREE.TextureLoader()
const dialTexture = textureLoader.load('/textures/textureD.png')
// Test mesh
const geometry = new THREE.TorusGeometry(3, 1, 100, 100)
const material = new THREE.ShaderMaterial({
vertexShader: testVertexShader,
fragmentShader: testFragmentShader,
uniforms:
{
uTime: { value: 0 },
uTexture: { value: dialTexture }
}
})
const mesh = new THREE.Mesh(geometry, material)
scene.add(mesh)
// Sizes
const sizes = {
width: window.innerWidth,
height: window.innerHeight
}
window.addEventListener('resize', () =>
{
sizes.width = window.innerWidth
sizes.height = window.innerHeight
camera.aspect = sizes.width / sizes.height
camera.updateProjectionMatrix()
renderer.setSize(sizes.width, sizes.height)
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
})
// Camera
const camera = new THREE.PerspectiveCamera(75, sizes.width / sizes.height, 0.1, 100)
scene.add(camera)
// Controls
const controls = new OrbitControls(camera, canvas)
controls.enableDamping = true
// Renderer
const renderer = new THREE.WebGLRenderer({
canvas: canvas
})
renderer.setSize(sizes.width, sizes.height)
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
// Animate
const clock = new THREE.Clock()
const tick = () =>
{
const elapsedTime = clock.getElapsedTime()
material.uniforms.uTime.value = elapsedTime
controls.update()
renderer.render(scene, camera)
window.requestAnimationFrame(tick)
}
tick();
I defined my shaders like so:
VERTEX Shader
varying vec2 vUv;
uniform float uTime;
void main()
{
vUv = uv;
gl_Position = projectionMatrix * modelPosition * vec4(position, 1.0);;
}
FRAGMENT Shader
uniform float uTime;
uniform sampler2D uTexture;
varying vec2 vUv;
void main()
{
vec4 color = texture2D(uTexture, vUv);
gl_FragColor = color;
}
Even after carefully setting everything up, an error keeps popping up in the console, and I'm puzzled as to why :(
Three.module.js:19006 THREE.WebGLProgram: Shader Error 0 - VALIDATE_STATUS false
Program Info Log: Vertex shader is not compiled.
VERTEX
ERROR: 0:64: 'modelPosition' : undeclared identifier
Can you provide me with some guidance?
I checked the console log for the vertex shader, and it seems fine, but still no luck compiling.