It is about shader in relation to Three.
To illustrate my problem, I have integrated the simplest example of “Books of shader” within Three.
Generally speaking, it seems that it works if a plane has the same dimensions as the screen.
But, I wonder that I cannot control the horizontal orientation and the dimensions of the plane (which is a shader-only square without texture in the example).
So, I also set the screen height for the plane’s width and center it horizontally which does not work.
Neither the plane is centered nor it is square.
Has anyone an idea what is happening here?
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<style type="text/css">
body {
overflow: hidden;
}
canvas {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/110/three.min.js"></script>
<script>
const vshader = `
void main() {
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
}
`
const fshader = `
//origin: https://thebookofshaders.com/02/
#ifdef GL_ES
precision mediump float;
#endif
void main() {
gl_FragColor = vec4(1.0,0.0,1.0,1.0);
}
`
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xffffff);
const camera = new THREE.PerspectiveCamera(
50,
window.innerWidth / window.innerHeight,
0.1,
2000
);
camera.position.z = 5;
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
const geometry = new THREE.PlaneGeometry(window.innerHeight, window.innerHeight);
const uniforms = {
}
const material = new THREE.ShaderMaterial({
uniforms: uniforms,
vertexShader: vshader,
fragmentShader: fshader
});
const plane = new THREE.Mesh(geometry, material);
scene.add(plane);
animate();
function animate() {
requestAnimationFrame(animate);
render();
}
function render() {
renderer.render(scene, camera);
}
</script>
</body>
</html>