Im kind of new to WebGl, but I wanted to try an example. I created my script like this:
import testVertexShader from './shaders/test/vertex.glsl'
import testFragmentShader from './shaders/test/fragment.glsl'
// Debug
const gui = new dat.GUI()
// Canvas
const canvas = document.querySelector('canvas.webgl')
// Scene
const scene = new THREE.Scene()
/**
* Textures
*/
const textureLoader = new THREE.TextureLoader()
const dialTexture = textureLoader.load('/textures/textureD.png')
/**
* Test mesh
*/
// Geometry
const geometry = new THREE.TorusGeometry(3, 1, 100, 100)
// Material
const material = new THREE.ShaderMaterial({
vertexShader: testVertexShader,
fragmentShader: testFragmentShader,
uniforms:
{
uTime: { value: 0 },
uTexture: { value: dialTexture }
}
})
// Mesh
const mesh = new THREE.Mesh(geometry, material)
scene.add(mesh)
/**
* Sizes
*/
const sizes = {
width: window.innerWidth,
height: window.innerHeight
}
window.addEventListener('resize', () =>
{
// Update sizes
sizes.width = window.innerWidth
sizes.height = window.innerHeight
// Update camera
camera.aspect = sizes.width / sizes.height
camera.updateProjectionMatrix()
// Update renderer
renderer.setSize(sizes.width, sizes.height)
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
})
/**
* Camera
*/
// Base 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()
// Update material
material.uniforms.uTime.value = elapsedTime
// Update controls
controls.update()
// Render
renderer.render(scene, camera)
// Call tick again on the next frame
window.requestAnimationFrame(tick)
}
tick();
And my shaders like this:
varying vec2 vUv;
uniform float uTime;
void main()
{
vUv = uv;
gl_Position = projectionMatrix * modelPosition * vec4(position, 1.0);;
}
uniform float uTime;
uniform sampler2D uTexture;
varying vec2 vUv;
void main()
{
vec4 color = texture2D(uTexture, vUv);
gl_FragColor = color;
}
But I keep getting this error in the console and I don't know 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
59:
60: void main()
61: {
62: vUv = uv;
63:
> 64: gl_Position = projectionMatrix * modelPosition * vec4(position, 1.0);;
Can you give me a hint?
I consoled log the vertex shader and it appears, but I cannot compile it
See WebGLProgram. The uniform variable modelPosition
does not exist, but modelMatrix
does, which is the model transformation matrix and contains the position, orientation and scaling of the 3D mesh.
gl_Position = projectionMatrix * modelMatrix * vec4(position, 1.0);