-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathray_caster_shader.js
82 lines (58 loc) · 1.72 KB
/
ray_caster_shader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import * as THREE from "three"
import {GUI as InputPanel} from "lil-gui";
import Stats from "stats.js"
const log=console.log.bind(console);
const stats=new Stats();
document.body.appendChild(stats.dom);
const inputPanel=new InputPanel();
inputPanel.close()
const scene = new THREE.Scene();
const camera = new THREE.OrthographicCamera();
const resolution=new THREE.Vector2();
scene.add(camera);
const renderer = new THREE.WebGLRenderer();
function resize(){
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setPixelRatio(devicePixelRatio);
renderer.getDrawingBufferSize(resolution);
camera.aspect=window.innerWidth/ window.innerHeight;
camera.updateProjectionMatrix()
}
resize();
addEventListener("resize",resize );
document.body.appendChild( renderer.domElement );
const planeGeo=new THREE.PlaneGeometry(2,2);
const shaderMat=new THREE.RawShaderMaterial({
glslVersion:THREE.GLSL3,
vertexShader:document.getElementById("vxShader").textContent,
fragmentShader:document.getElementById("fgShader").textContent,
uniforms:{
time:{value:0},
resolution:{value:resolution},
touch:{value:new THREE.Vector2()},
}
});
const {uniforms}= shaderMat;
scene.add(new THREE.Mesh(planeGeo, shaderMat));
function animate(time)
{
if(renderer.info.render.frame===1)
log(`rendered at ${Math.floor(performance.now())}ms`);
uniforms.time.value=time/1000.0;
renderer.render( scene, camera);
stats.update();
}
function onTouchMove(index, x, y){
uniforms.touch.value.set(x,y)
uniforms.touch.value.multiplyScalar(devicePixelRatio)
}
renderer.domElement.addEventListener("touchmove",
e=>{
let i=0;
for(let t of e.touches){
onTouchMove(i,t.clientX,t.clientY)
i++;
}
}
);
renderer.setAnimationLoop(animate);