forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebgl_collisions_box.html
164 lines (110 loc) · 3.29 KB
/
webgl_collisions_box.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
<html>
<head>
<title>three.js webgl - intersection: ray with box</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: 150px; 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" src="js/Stats.js"></script>
<script type="text/javascript">
var camera, scene, projector, renderer,
info, mouse = { x: 0, y: 0 }, sun, cube;
var bounce = 0;
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.z = -500;
scene = new THREE.Scene();
projector = new THREE.Projector();
renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild(renderer.domElement);
var ambientLight = new THREE.AmbientLight( 0x606060 );
scene.addLight( ambientLight );
sun = new THREE.DirectionalLight( 0xffffff );
sun.position = camera.position.clone();
scene.addLight( sun );
createCube( 200, new THREE.Vector3( 0,0,0 ) );
stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.top = '0px';
container.appendChild( stats.domElement );
container.onmousemove = onDocumentMouseMove;
animate();
}
function createCube( s, p ) {
cube = new THREE.Mesh (
new THREE.CubeGeometry( s, s, s ),
new THREE.MeshLambertMaterial( { color: 0x003300 } )
);
cube.position = p;
scene.addObject( cube );
THREE.Collisions.colliders.push( THREE.CollisionUtils.MeshOBB( cube ) );
};
function onDocumentMouseMove( event ) {
event.preventDefault();
mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
};
function animate() {
requestAnimationFrame( animate );
info.innerHTML = "";
var vector = new THREE.Vector3( mouse.x, mouse.y, 0.5 );
projector.unprojectVector( vector, camera );
var ray = new THREE.Ray( camera.position, vector.subSelf( camera.position ).normalize() );
var c = THREE.Collisions.rayCastNearest( ray );
if ( c ) {
info.innerHTML += "Found @ distance " + c.distance.toFixed(2);
c.mesh.materials[ 0 ].color.setHex( 0xaa0000 );
} else {
info.innerHTML += "No intersection";
cube.materials[0].color.setHex( 0x003300 );
}
cube.rotation.x += 0.01;
cube.rotation.y += 0.02;
cube.position.x = Math.sin(bounce) * 100;
bounce += 0.01;
renderer.render( scene, camera );
stats.update();
};
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>