forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebgl_collisions_trigger.html
168 lines (133 loc) · 5.08 KB
/
webgl_collisions_trigger.html
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<html>
<head>
<title>three.js webgl - collision detection</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<style type="text/css">
body {
font-family: Monospace;
background-color: #f0f0f0;
margin: 0px;
overflow: hidden;
}
#oldie {
background-color: #ddd !important
}
#info {
position: absolute;
top: 30px;
left: 10px;
width: 800px;
color: #000000;
padding: 5px;
font-family: Monospace;
font-size: 13px;
text-align: left;
z-index: 100;
}
#options {
position: absolute;
top: 10px;
left: 10px;
width: 800px;
color: #000000;
padding: 5px;
font-family: Monospace;
font-size: 13px;
text-align: left;
z-index: 100;
}
</style>
<script type="text/javascript" src="../build/Three.js">
</script>
<script type="text/javascript" src="js/RequestAnimationFrame.js">
</script>
<script type="text/javascript">
var scene, camera, renderer, info, mouse2d, sun, loader, sphere;
var range = 400;
var speed = 1;
var sphereSize = 4;
var cubes = [];
function init(){
container = document.createElement('div');
document.body.appendChild(container);
info = document.getElementById("info");
camera = new THREE.Camera(40, window.innerWidth / window.innerHeight, 1, 10000);
camera.position.y = 120;
camera.position.x = 300;
camera.position.z = 0;
mouse2d = new THREE.Vector3(0, 0, 1);
loader = new THREE.Loader(true);
scene = new THREE.Scene();
sphere = new THREE.Mesh(new THREE.Sphere(sphereSize, 10, 10), new THREE.MeshLambertMaterial({
color: 0xff0000
}));
scene.addObject(sphere);
renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
container.appendChild(renderer.domElement);
var ambientLight = new THREE.AmbientLight(0xdddddd);
scene.addLight(ambientLight);
sun = new THREE.DirectionalLight(0xffffff);
sun.position = new THREE.Vector3(1, -1, 1).normalize();
scene.addLight(sun);
createObstacles();
container.onmousemove = onDocumentMouseMove;
animate();
}
function createObstacles(){
createCube(100, 50, 10, new THREE.Vector3(0, 0, 100));
camera.target = createCube(100, 50, 10, new THREE.Vector3(0, 0, 200));
createCube(100, 50, 10, new THREE.Vector3(0, 0, 300));
}
function createCube(sx, sy, sz, p){
var cube = new THREE.Mesh(new THREE.Cube(sx, sy, sz, 1, 1, 1), new THREE.MeshLambertMaterial({
color: 0x003300//, wireframe: true
}));
cube.position = p;
scene.addObject(cube);
THREE.Collisions.colliders.push( THREE.CollisionUtils.MeshOBB(cube) );
cubes.push(cube);
return cube;
}
function onDocumentMouseMove(event){
event.preventDefault();
mouse2d.x = (event.clientX / window.innerWidth) * 2 - 1;
mouse2d.y = -(event.clientY / window.innerHeight) * 2 + 1;
mouse2d.z = 1;
}
function animate(){
requestAnimationFrame(animate);
sphere.position.z += speed;
if(sphere.position.z > range) sphere.position.z = 0;
for (var i = 0; i < cubes.length; i++) {
cubes[i].materials[0].color = new THREE.Color(0x003300);
}
var ray = new THREE.Ray( sphere.position, new THREE.Vector3(0,0,1) );
var c = THREE.Collisions.rayCastNearest(ray);
if (c && c.distance == -1) {
info.innerHTML = "Colliding!";
c.mesh.materials[0].color = new THREE.Color(0xff0000);
} else if(c && c.distance >= 0) {
info.innerHTML = "Approaching @ " + c.distance;
} else {
info.innerHTML = "No collider in sight.";
}
camera.position.x = Math.cos(mouse2d.x * Math.PI) * 300;
camera.position.z = 200 + Math.sin(mouse2d.x * Math.PI) * 300;
renderer.render(scene, camera);
}
function vts(v){
if (!v)
return "undefined<br>";
else
return v.x + " , " + v.y + " , " + v.z + "<br>";
}
</script>
</head>
<body onload="init();">
<div id="info">
</div>
<div id="options">
</div>
</body>
</html>