-
Notifications
You must be signed in to change notification settings - Fork 0
/
basics.html
251 lines (212 loc) · 8.54 KB
/
basics.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<!DOCTYPE html>
<html>
<head>
<title>TriPAS</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="https://callum.com">
<style type="text/css">
body {
background-color: #000;
font-family: courier;
margin: 0;
overflow: hidden;
}
#title {
position: absolute;
left: 0px;
width: 100%;
top: 8px;
text-align: center;
z-index: 10;
font-size: 1.5em;
color: #0f0;
pointer-events:none;
}
#debug_output {
position: absolute;
left: 8px;
bottom: 8px;
z-index: 10;
color: orange;
}
</style>
<script type="text/javascript" src="lib/three.min.js"></script>
<script type="text/javascript" src="lib/Detector.js"></script>
<script type="text/javascript" src="lib/TrackballControls.js"></script>
<script type="text/javascript" src="lib/tween.min.js"></script>
<script type="text/javascript">
var camera, scene, renderer;
var mesh;
var smaterial;
var smaterial2;
var mesh_scale = 0.999;
var controls;
var helper_mesh;
var projector;
var intersect_point = undefined;
var last_mouse_x = Infinity, last_mouse_y = Infinity;
var spin_speed = 1.0;
document.addEventListener("DOMContentLoaded", start_app, false);
function start_app() {
init();
animate();
}
function init() {
if (!Detector.webgl) {
Detector.addGetWebGLMessage();
}
renderer = new THREE.WebGLRenderer({
antialias: true
});
renderer.setClearColor(0x000000, 1.0);
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 10, 1000);
camera.position.z = 80;
// var ambient_light = new THREE.AmbientLight(0x333333);
// scene.add(ambient_light);
// var directionalLight = new THREE.DirectionalLight(0xffffff, 0.5);
// directionalLight.position.set(0, -10, 0);
// scene.add(directionalLight);
var point_light = new THREE.PointLight(0xffffff);
point_light.position.x = 20;
point_light.position.y = 50;
point_light.position.z = 100;
scene.add(point_light);
var point_light = new THREE.PointLight(0x444444);
point_light.position.x = -100;
point_light.position.y = -100;
point_light.position.z = -100;
scene.add(point_light);
var point_light = new THREE.PointLight(0x444444);
point_light.position.x = -100;
point_light.position.y = 100;
point_light.position.z = -100;
scene.add(point_light);
var point_light = new THREE.PointLight(0x444444);
point_light.position.x = 100;
point_light.position.y = 100;
point_light.position.z = -100;
scene.add(point_light);
var point_light = new THREE.PointLight(0x444444);
point_light.position.x = 100;
point_light.position.y = -100;
point_light.position.z = -100;
scene.add(point_light);
var shading_type = THREE.FlatShading;
var material = new THREE.MeshPhongMaterial({
color: 0x0000ff,
shading: shading_type,
shininess: 50,
transparent: true,
opacity: 0.3
});
var geometry = new THREE.BoxGeometry(20, 20, 20, 2, 2, 2);
mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
smaterial = new THREE.MeshPhongMaterial({
color: 0xff0000,
shading: shading_type,
shininess: 50,
transparent: false
});
smaterial2 = new THREE.MeshPhongMaterial({
color: 0xffff00,
shading: shading_type,
shininess: 50,
transparent: false
});
var sphere = new THREE.SphereGeometry(3);
mesh = new THREE.Mesh(sphere, smaterial);
scene.add(mesh);
controls = new THREE.TrackballControls(camera, renderer.domElement);
controls.rotateSpeed = 0.4;
controls.noZoom = false;
controls.noPan = false;
controls.staticMoving = false;
controls.minDistance = 50;
controls.maxDistance = 400;
controls.dynamicDampingFactor = 0.2;
// var tween = new TWEEN.Tween({
// scale: 100
// })
// .to({
// scale: 50
// }, 1000)
// .easing(TWEEN.Easing.Elastic.InOut)
// .onUpdate(function () {
// var real_scale = this.scale / 100.0;
// mesh.scale.set(real_scale, real_scale, real_scale);
// });
// tween.repeat(Infinity);
// tween.yoyo(true);
// tween.start();
projector = new THREE.Projector();
var helper_geometry = new THREE.SphereGeometry( 0.5, 32, 32 );
helper_mesh = new THREE.Mesh(helper_geometry, new THREE.MeshNormalMaterial());
scene.add(helper_mesh);
document.addEventListener('mousemove', onDocumentMouseMove, false);
window.addEventListener('resize', onWindowResize, false);
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function onDocumentMouseMove(event) {
last_mouse_x = (event.clientX / window.innerWidth) * 2 - 1;
last_mouse_y = -(event.clientY / window.innerHeight) * 2 + 1;
event.preventDefault();
checkIntersect();
}
function checkIntersect() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
var vector = new THREE.Vector3(last_mouse_x, last_mouse_y, -1);
projector.unprojectVector(vector, camera);
var raycaster = new THREE.Raycaster(camera.position, vector.sub(camera.position).normalize());
var intersects = raycaster.intersectObject(mesh, true);
intersect_point = undefined;
mesh.material = smaterial;
if (intersects.length > 0) {
if (intersects[0].point !== null ) {
helper_mesh.position.copy(intersects[0].point);
intersect_point = intersects[0].point;
update_output();
mesh.material = smaterial2;
} else {
update_output();
}
} else {
helper_mesh.position.set(Infinity, 0, 0);
update_output();
}
}
function update_output() {
var div = document.getElementById("debug_output");
if ( div ) {
if ( typeof intersect_point !== "undefined" ) {
div.innerHTML = "Intersects at " + intersect_point.x.toFixed(2) + ", " + intersect_point.y.toFixed(2) + ", " + intersect_point.z.toFixed(2);
} else {
div.innerHTML = "Does not intersect";
}
}
}
function animate() {
requestAnimationFrame(animate);
controls.update();
TWEEN.update();
renderer.render(scene, camera);
rS('intersect').start();
checkIntersect();
rS('intersect').end();
}
</script>
</head>
<body>
<div id="title">TriPAS</div>
<div id="debug_output"></div>
</body>
</html>