-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patha6.js
152 lines (126 loc) · 5.51 KB
/
a6.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/////////////////////////////////////////////////////////////////////////////////////////
// UBC CPSC 314, Vjan2018
// Assignment 6 Template; compatible with three.js r90
/////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////
// SETUP RENDERER, SCENE & CAMERA //
////////////////////////////////////////////////
var canvas = document.getElementById('canvas');
var scene = new THREE.Scene();
var renderer = new THREE.WebGLRenderer();
renderer.setClearColor(0xd0f0d0); // set background colour
canvas.appendChild(renderer.domElement);
// SETUP CAMERA
var camera = new THREE.PerspectiveCamera(30,1,0.1,10000); // view angle, aspect ratio, near, far
camera.position.set(0,45,45);
camera.lookAt(0,0,0);
scene.add(camera);
// SETUP ORBIT CONTROLS OF THE CAMERA
var controls = new THREE.OrbitControls(camera);
controls.damping = 0.2;
controls.autoRotate = false;
// ADAPT TO WINDOW RESIZE
function resize() {
renderer.setSize(window.innerWidth,window.innerHeight);
camera.aspect = window.innerWidth/window.innerHeight;
camera.updateProjectionMatrix();
}
// EVENT LISTENER RESIZE
window.addEventListener('resize',resize);
resize();
//SCROLLBAR FUNCTION DISABLE
window.onscroll = function () {
window.scrollTo(0,0);
}
////////////////////////////////////////////////
// ADD LIGHTS //
////////////////////////////////////////////////
light = new THREE.PointLight(0xffffff);
light.position.set(-5,-3,4); // WCS coords for light
var vcsLight = new THREE.Vector3(0.0,0.0,0.0); // VCS coords for light
scene.add(light);
ambientLight = new THREE.AmbientLight(0x606060);
scene.add(ambientLight);
var diffuseMaterial = new THREE.MeshLambertMaterial( {color: 0xff0000} );
////////////////////////////////////////////////
// FLOOR with texture //
////////////////////////////////////////////////
var textureLoader = new THREE.TextureLoader();
floorTexture = textureLoader.load( "images/floor.jpg" );
floorTexture.magFilter = THREE.NearestFilter;
floorTexture.minFilter = THREE.NearestFilter
floorMaterial = new THREE.MeshBasicMaterial( {map: floorTexture, side:THREE.DoubleSide });
floorGeometry = new THREE.PlaneBufferGeometry(25.0, 25.0);
floor = new THREE.Mesh(floorGeometry, floorMaterial);
floor.position.y = 0.0;
floor.rotation.x = Math.PI / 2;
////////////////////////////////////////////////
// SHADER & UNIFORMS //
////////////////////////////////////////////////
var raytracerMaterial = new THREE.ShaderMaterial( {
uniforms: {
lightPosition: {value: light.position},
resolution: {value: new THREE.Vector2(window.innerWidth, window.innerHeight)},
ambient_light_color: {value: new THREE.Vector3(0.376, 0.376, 0.376)},
diffuse_light_color: {value: new THREE.Vector3(1.0, 1.0, 1.0)},
myFloat1: {value: 0.5},
myFloat2: {value: 0.5}
},
vertexShader: document.getElementById( 'raytracerVertShader' ).textContent,
fragmentShader: document.getElementById( 'raytracerFragShader' ).textContent
} );
////////////////////////////////////////////////
// SCENE OBJECTS //
////////////////////////////////////////////////
sphere_small = new THREE.Mesh(new THREE.SphereGeometry(2.0, 32, 32), diffuseMaterial);
sphere_small.position.set(-1.0,16.0,0.0);
sphere_med = new THREE.Mesh(new THREE.SphereGeometry(3.0, 32, 32), diffuseMaterial);
sphere_med.position.set(6.0,13.0,0.0);
sphere_large = new THREE.Mesh(new THREE.SphereGeometry(6.0, 32, 32), diffuseMaterial);
sphere_large.position.set(0.0,6.0,-1.0);
////////////////////////////////////////////////
// RAYTRACER OUTPUT "SCREEN" //
////////////////////////////////////////////////
var resolution_scale = 10.0;
screenGeometry = new THREE.PlaneBufferGeometry(window.innerHeight, window.innerHeight);
raytracerScreen = new THREE.Mesh(screenGeometry, raytracerMaterial);
raytracerScreen.position.set(0.0,0.0,0.0);
raytracerScreen.rotation.x = -Math.PI / 6;
raytracerScreen.material.side = THREE.DoubleSide;
scene.add(raytracerScreen);
////////////////////////////////////////////////
// SCENE to SHADER MAPPING, FUNC //
////////////////////////////////////////////////
////////////////////////////////////////////////
// LISTEN TO KEYBOARD //
////////////////////////////////////////////////
var keyboard = new THREEx.KeyboardState();
function checkKeyboard() {
if (keyboard.pressed("W")) {
console.log('W pressed');
light.position.y += 0.1;
} else if (keyboard.pressed("S"))
light.position.y -= 0.1;
if (keyboard.pressed("A"))
light.position.x -= 0.1;
else if (keyboard.pressed("D"))
light.position.x += 0.1;
}
////////////////////////////////////////////////
// UPDATE CALLBACK //
////////////////////////////////////////////////
function update() {
checkKeyboard();
requestAnimationFrame(update);
raytracerScreen.lookAt(camera.position);
raytracerMaterial.uniforms.lightPosition.value = light.position;
raytracerMaterial.uniforms.lightPosition.value.needsUpdate = true;
var i = 0.5+0.5*Math.sin(Date.now()*0.001*5.0);
raytracerMaterial.uniforms.myFloat1.value = i;
raytracerMaterial.uniforms.myFloat1.needsUpdate = true;
var i2 = 0.5+0.5*Math.cos(Date.now()*0.001*5.0);
raytracerMaterial.uniforms.myFloat2.value = i2;
raytracerMaterial.uniforms.myFloat2.needsUpdate = true;
renderer.render(scene, camera);
}
update();