-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
315 lines (243 loc) · 12 KB
/
index.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<!-- Gary Ang | Ming | playgrd -->
<!-- playgrd.com -->
<!-- three.js port of Daniel Shiffman's Nature of Code Chapter 2-->
<!-- Path following -->
<!-- This code does not work smoothly. Please do correct if you see what is wrong. -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Nature of Code in three.js</title>
<style>
</style>
<script type="text/javascript" src="./libs/three.js"></script>
<script type="text/javascript" src="./libs/OrbitControls.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.8/d3.min.js" type="text/JavaScript"></script>
<script language="javascript" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/p5.min.js"></script>
<script type="text/javascript" src="https://cdn.rawgit.com/josephg/noisejs/master/perlin.js"></script>
</head>
<body>
<div id='svg'></div>
<script>
var p5 = new p5();
var scene = new THREE.Scene();
// var camera = new THREE.PerspectiveCamera( 1000, window.innerWidth / window.innerHeight, 0.1, 1000 );
var camera = new THREE.OrthographicCamera( -10,60,-10,60, 0.1, 1000 );
camera.position.z = 30;
var renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setClearColor( 0xF9B3D1, 1 );
document.body.appendChild( renderer.domElement );
var orbit = new THREE.OrbitControls( camera, renderer.domElement );
orbit.enableZoom = false;
var ambientLight = new THREE.AmbientLight( '#D63AF9', 100),
hemiLight = new THREE.HemisphereLight('#D63AF9', '#2F1847', 1),
light = new THREE.PointLight( '#D63AF9', 10, 100 );
ambientLight.position.set( 0, 0, 0 );
hemiLight.position.set( 10, 10, 10 );
light.position.set( 10, 10, 10 );
scene.add( ambientLight );
scene.add( hemiLight );
scene.add( light );
var group = new THREE.Group();
mover = new Mover();
mover.initialise();
scene.add( group );
var prevFog = true;
var pathradius = 30;
var pathpoints = [];
var geometry = new THREE.Geometry();
geometry.vertices.push(new THREE.Vector3( 0, 25, 0));
pathpoints.push(new THREE.Vector3( 0, 25, 0));
geometry.vertices.push(new THREE.Vector3( 5, 40, 0));
pathpoints.push(new THREE.Vector3( 5, 40, 0));
geometry.vertices.push(new THREE.Vector3( 50, 25, 0));
pathpoints.push(new THREE.Vector3( 50, 25, 0));
// geometry.vertices.push(new THREE.Vector3( 0, 25, 0));
// pathpoints.push(new THREE.Vector3( 0, 25, 0));
console.log(pathpoints);
console.log(geometry.vertices);
var material = new THREE.LineBasicMaterial( { color : 0xffffff } );
var lineObj = new THREE.Line( geometry, material );
group.add(lineObj);
// var wind = new THREE.Vector2(0.1,0);
// var gravity = new THREE.Vector2(0,0.000001);
var render = function () {
requestAnimationFrame( render );
// orbit.update();
mover.follow(pathpoints);
mover.update();
// mover.checkEdges();
mover.display();
// group.rotation.x += 0.002;
// group.rotation.y += 0.002;
// group.rotation.z += 0.002;
renderer.render( scene, camera );
};
window.addEventListener( 'resize', function () {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}, false );
render();
function Mover() {
this.position = new THREE.Vector3();
this.velocity = new THREE.Vector3();
this.acceleration = new THREE.Vector3();
this.max = new THREE.Vector3(50,50,50);
this.min = new THREE.Vector3(-50,-50,-50);
var geometry = new THREE.SphereGeometry(1,10,10);
var material = new THREE.MeshToonMaterial({ color: 0xffffff, opacity:0.5, transparent:true, wireframe:true, emissive: 0xffffff,emissiveIntensity:0.1} );
var sphere = new THREE.Mesh(geometry, material);
group.add(sphere);
this.initialise = function() {
this.position.x = 0;
this.position.y = 25;
this.position.z = 0;
this.velocity.x = 0.01;
this.velocity.y = 0.01;
this.velocity.z = 0.0;
this.acceleration.x = 0.0;
this.acceleration.y = 0.0;
this.acceleration.z = 0.0;
this.mass = 10000;
// console.log('Initialise', this.position);
}
this.applyForce = function(force){
var f = force.divideScalar(this.mass);
this.acceleration.add(f);
}
this.update = function() {
this.velocity.add(this.acceleration);
this.velocity.clamp(this.min, this.max);
this.position.add(this.velocity);
this.acceleration.multiplyScalar(0);
// console.log('Position', this.position);
}
this.display = function(){
sphere.position.x = this.position.x;
sphere.position.y = this.position.y;
sphere.position.z = this.position.z;
// console.log(sphere.position);
}
this.checkEdges = function() {
if (this.position.x > 50){
this.position.x = 50;
this.velocity.x *= -1;
// console.log(this.velocity);
}
else if (this.position.x < 0){
this.position.x = 0;
this.velocity.x *= -1;
// console.log('HIT');
}
if (this.position.y > 50){
this.position.y = 50;
this.velocity.y *= -1;
}
else if (this.position.y < 0){
this.position.y = 0;
this.velocity.y *= -1;
}
if (this.position.z > 50){
this.position.z = 50;
this.velocity.z *= -1;
}
else if (this.position.z < 0){
this.position.z = 0;
this.velocity.z *= -1;
}
}
this.follow = function(p) {
// Predict location 50 (arbitrary choice) frames ahead
// This could be based on speed
var predict = this.velocity.clone();
predict.normalize();
predict.multiplyScalar(50);
var pos = this.position.clone()
var predictLoc = pos.add(predict);
// Now we must find the normal to the path from the predicted location
// We look at the normal for each line segment and pick out the closest one
var normal = null;
var target = null;
var worldRecord = 1000000; // Start with a very high record distance that can easily be beaten
// Loop through all points of the path
for (var i = 0; i < p.length-1; i++) {
// Look at a line segment
var a = p[i];
var b = p[i+1];
//println(b);
// Get the normal point to that line
var normalPoint = getNormalPoint(predictLoc, a, b);
console.log('NormalPt', normalPoint);
console.log('PredictLoc', predictLoc);
// This only works because we know our path goes from left to right
// We could have a more sophisticated test to tell if the point is in the line segment or not
if (normalPoint.x < a.x || normalPoint.x > b.x) {
// This is something of a hacky solution, but if it's not within the line segment
// consider the normal to just be the end of the line segment (point b)
normalPoint = b.clone();
}
// How far away are we from the path?
var distance = predictLoc.distanceTo(normalPoint);
console.log('Distance', distance);
// Did we beat the record and find the closest line segment?
if (distance < worldRecord) {
worldRecord = distance;
// console.log('Record', worldRecord)
// If so the target we want to steer towards is the normal
normal = normalPoint;
// Look at the direction of the line segment so we can seek a little bit ahead of the normal
var b_ = b.clone()
var dir = b_.sub(a);
dir.normalize();
// This is an oversimplification
// Should be based on distance to path & velocity
dir.multiplyScalar(10);
target = normalPoint.clone();
target.add(dir);
}
}
// console.log(worldRecord);
// Only if the distance is greater than the path's radius do we bother to steer
if (worldRecord > pathradius && target !== null) {
this.seek(target);
}
};
// A method that calculates and applies a steering force towards a target
// STEER = DESIRED MINUS VELOCITY
this.seek = function(target) {
var tgt = target.clone()
var desired = tgt.sub(this.position); // A vector pointing from the position to the target
// If the magnitude of desired equals 0, skip out of here
// (We could optimize this to check if x and y are 0 to avoid mag() square root
if (desired.length() === 0) return;
// Normalize desired and scale to maximum speed
desired.normalize();
desired.multiplyScalar(10);
// Steering = Desired minus Velocity
var desired_ = desired.clone();
var steer = desired_.sub(this.velocity);
// steer.limit(this.maxforce); // Limit to maximum steering force
console.log('Steer',steer);
this.applyForce(steer);
};
var getNormalPoint = function(p, a, b) {
// Vector from a to p
var p_ = p.clone();
var a_ = a.clone();
var b_ = b.clone();
var ap = p_.sub(a_);
// Vector from a to b
var ab = b_.sub(a_);
ab.normalize(); // Normalize the line
// Project vector "diff" onto line by using the dot product
ab.multiplyScalar(ap.dot(ab));
var normalPoint = a_.add(ab);
return normalPoint;
};
}
</script>
</body>
</html>