-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexperiment4.html
More file actions
131 lines (113 loc) · 3.46 KB
/
experiment4.html
File metadata and controls
131 lines (113 loc) · 3.46 KB
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
<html>
<head>
<title>My first three.js app</title>
<style>
body {
margin: 0;
}
canvas {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<script src="js/three.js"></script>
<script src="js/OrbitControls.js"></script>
<script>
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
0.1,
2000
);
var renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// adding controls
controls = new THREE.OrbitControls(camera, renderer.domElement);
// controls.minDistance = 250;
// controls.maxDistance = 250;
// adding a simple cube
var geometry = new THREE.BoxGeometry(100, 100, 100);
// adding a list of materials for cube faces
var cubeMaterials = [
new THREE.MeshBasicMaterial({
color: 0xffffff,
wireframe: false,
side: THREE.DoubleSide
}),
new THREE.MeshBasicMaterial({
color: 0x00ffff,
wireframe: false,
side: THREE.DoubleSide
}),
new THREE.MeshBasicMaterial({
color: 0xff00ff,
wireframe: false,
side: THREE.DoubleSide
}),
new THREE.MeshBasicMaterial({
color: 0xffff00,
wireframe: false,
side: THREE.DoubleSide
}),
new THREE.MeshBasicMaterial({
color: 0x00ff00,
wireframe: false,
side: THREE.DoubleSide
}),
new THREE.MeshBasicMaterial({
color: 0xff0000,
wireframe: false,
side: THREE.DoubleSide
})
];
var cube = new THREE.Mesh(geometry, cubeMaterials);
scene.add(cube);
// move back camera
camera.position.z = 49;
// added caster to detect intersection
var raycaster = new THREE.Raycaster();
var mouse = new THREE.Vector2(),
INTERSECTED;
var animate = function() {
raycaster.setFromCamera(mouse, camera);
var intersects = raycaster.intersectObjects(scene.children, true);
if (intersects.length > 0) {
if (INTERSECTED != intersects[0].object) {
if (INTERSECTED)
INTERSECTED.object.material[
INTERSECTED.face.materialIndex
].color.setHex(INTERSECTED.currentHex);
INTERSECTED = intersects[0];
INTERSECTED.currentHex = INTERSECTED.object.material[
INTERSECTED.face.materialIndex
].color.getHex();
INTERSECTED.object.material[
INTERSECTED.face.materialIndex
].color.setHex(0x000000);
}
} else {
if (INTERSECTED)
INTERSECTED.object.material[
INTERSECTED.face.materialIndex
].color.setHex(INTERSECTED.currentHex);
INTERSECTED = null;
}
controls.update();
requestAnimationFrame(animate);
renderer.render(scene, camera);
};
function onMouseMove(event) {
mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
}
mouse.x = undefined;
mouse.y = undefined;
window.addEventListener("mousemove", onMouseMove, false);
animate();
</script>
</body>
</html>