-
Notifications
You must be signed in to change notification settings - Fork 3
/
octree_example.html
197 lines (151 loc) · 5.5 KB
/
octree_example.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<!DOCTYPE html>
<html lang="en">
<head>
<title>Octree test</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
font-family: Monospace;
background-color: #f0f0f0;
margin: 0px;
overflow: hidden;
}
</style>
</head>
<body>
<script src="libs/three.min.js"></script>
<script src="libs/stats.min.js"></script>
<script src="octree.js"></script>
<script>
function createSphere(x,y,z) {
var geo = new THREE.SphereGeometry( 10, 20, 20 );
var material = new THREE.MeshPhongMaterial( { ambient: 0x0, color: 0x2C590A, specular: 0x990000, shininess: 30 } );//new THREE.MeshBasicMaterial( { color:0x2C590A, wireframe: false, opacity: 0.5 } );
var sphere = new THREE.Mesh( geo, material );
sphere.position.set(x,y,z);
return sphere;
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
var mouse2D = new THREE.Vector2();
var magnitude = 1000;
var mousedown = false;
function onDocumentMouseDown(event) {
event.preventDefault();
mousedown = true;
mouse2D = new THREE.Vector2( ( event.clientX / window.innerWidth ) * 2 - 1, - ( event.clientY / window.innerHeight ) * 2 + 1 );
}
function onDocumentMouseUp(event) {
event.preventDefault();
mousedown = false;
}
function onDocumentMouseMove(event) {
event.preventDefault();
if(mousedown)
{
var currentMouse2D = new THREE.Vector2( ( event.clientX / window.innerWidth ) * 2 - 1, - ( event.clientY / window.innerHeight ) * 2 + 1 );
var diff = ((new THREE.Vector2()).subVectors(mouse2D,currentMouse2D)).x;
var q = new THREE.Quaternion();
q.setFromAxisAngle(new THREE.Vector3( 0, 1, 0 ), (magnitude*diff/window.innerWidth)*2*Math.PI);
camera.position.applyQuaternion(q);
camera.lookAt(target);
mouse2D = currentMouse2D;
}
}
function init() {
container = document.createElement( 'div' );
document.body.appendChild( container );
//
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.y = 800;
camera.position.x = 1400 * Math.sin( theta * Math.PI / 360 );
camera.position.z = 1400 * Math.cos( theta * Math.PI / 360 );
camera.useQuaternion = true;
camera.lookAt( target );
renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild(renderer.domElement);
stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.top = '0px';
container.appendChild( stats.domElement );
//
octree = new Octree(null, new THREE.Vector3(0,0,0), dim.x, dim.y, dim.z);
scene.add(octree.mesh);
for(var i = 0; i < 100; i++)
{
var sphere = createSphere((Math.random()-0.5)*dim.x*2, (Math.random()-0.5)*dim.y*2, (Math.random()-0.5)*dim.z*2);
sphere.dir = new THREE.Vector3(); // for animating
sphere.changeFreq = Math.random() * 10000; // for animating
sphere.timeAtLastChange = (new Date()).getTime(); // for animating
spheres.push(sphere);
octree.add(sphere);
scene.add(sphere);
}
//
// Lights
var ambientLight = new THREE.AmbientLight( 0x606060 );
scene.add( ambientLight );
var directionalLight = new THREE.DirectionalLight( 0xffffff );
directionalLight.position.x = Math.random() - 0.5;
directionalLight.position.y = Math.random() - 0.5;
directionalLight.position.z = Math.random() - 0.5;
directionalLight.position.normalize();
scene.add( directionalLight );
var directionalLight = new THREE.DirectionalLight( 0x808080 );
directionalLight.position.x = Math.random() - 0.5;
directionalLight.position.y = Math.random() - 0.5;
directionalLight.position.z = Math.random() - 0.5;
directionalLight.position.normalize();
scene.add( directionalLight );
//
window.addEventListener( 'resize', onWindowResize, false );
document.addEventListener( 'mousemove', onDocumentMouseMove, false );
document.addEventListener( 'mousedown', onDocumentMouseDown, false );
document.addEventListener( 'mouseup', onDocumentMouseUp, false );
}
function animate() {
var now = (new Date()).getTime();
requestAnimationFrame( animate );
spheres.forEach(function(sphere){
var timeSinceLastChange = now - sphere.timeAtLastChange
if(timeSinceLastChange > sphere.changeFreq)
{
sphere.dir.set( Math.random() - 0.5, Math.random( ) - 0.5, Math.random() - 0.5 );
sphere.dir.normalize();
sphere.timeAtLastChange = (new Date()).getTime();
}
var pos = (new THREE.Vector3()).addVectors(sphere.position, sphere.dir);
if(octree.contains(pos))
{
sphere.position = pos;
octree.onEntityPoseChanged(sphere);
}
else
{
sphere.dir.set( Math.random() - 0.5, Math.random( ) - 0.5, Math.random() - 0.5 );
}
});
octree.update();
render();
stats.update();
}
function render() {
renderer.render( scene, camera );
}
var container, stats;
var camera, scene, renderer;
var octree;
var theta = 45;
target = new THREE.Vector3( 0, 0, 0 );
var dim = {x: 500, y:500, z:500};
var spheres = new Array();
init();
animate();
</script>
</body>
</html>