-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
151 lines (114 loc) · 4.02 KB
/
index.html
File metadata and controls
151 lines (114 loc) · 4.02 KB
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
<html>
<head>
<meta charset="utf8" />
<title>path finding with 3js</title>
<style>* { margin: 0; overflow: hidden; background-color: black; }</style>
</head>
<body>
<script src="https://rawcdn.githack.com/mrdoob/three.js/e0a31ea77c0242ae29ef6e951fa588216e1d247f/build/three.min.js"></script>
<script src="https://rawcdn.githack.com/mrdoob/three.js/e0a31ea77c0242ae29ef6e951fa588216e1d247f/examples/js/controls/OrbitControls.js"></script>
<script src="pathfinding.js"></script>
<script>
var Pathfinder = function( geometry ) {
// assume non-interleaved positions for speed
var positions = geometry.getAttribute( "position" ).array;
var indices = geometry.getIndex().array;
var astar = new de.polygonal.ai.pathfinding.AStar();
var graph = new de.polygonal.ds.Graph();
var nodes = {};
for( var i = 0, j = 0; i < positions.length; i += 3, j++ ) {
var wp = new de.polygonal.ai.pathfinding.AStarWaypoint();
wp.x = positions[ i ];
wp.y = positions[ i + 1 ];
wp.z = positions[ i + 2 ];
wp.node = graph.createNode( wp ); graph.addNode( wp.node );
nodes[ j ] = wp;
}
var edges = {};
for( var i = 0; i < indices.length; i += 3 ) {
for( var j = 0; j < 3; j++ ) {
var k = ( j + 1 ) % 3;
var a = indices[ i + j ];
var b = indices[ i + k ];
var c = Math.min( a, b ) + "-" + Math.max( a, b );
if( edges[ c ] === undefined ) {
edges[ c ] = true;
// assume equal cost arcs for speed
graph.addMutualArc( nodes[ a ].node, nodes[ b ].node );
}
}
}
edges = undefined;
this.findPath = function( a, b ) {
var path = [];
if( astar.find( graph, nodes[ a ], nodes[ b ], path ) ) {
return path;
}
};
};
var loader = new THREE.FileLoader();
loader.load( "terrain.obj", function( text ) {
console.time( "parsing obj file" );
var positions = [], indices = [];
for( var line of text.split( "\n" ) ) {
var words = line.split( " " );
switch( line.charAt( 0 ) ) {
case "v":
positions.push( parseFloat( words[1] ), parseFloat( words[2] ), parseFloat( words[3] ) );
break;
case "f":
indices.push( parseInt( words[1] ) -1, parseInt( words[2] ) -1, parseInt( words[3] ) -1 );
break;
}
}
var geometry = new THREE.BufferGeometry();
geometry.setAttribute( "position", new THREE.Float32BufferAttribute( positions, 3 ) );
geometry.setIndex( new THREE.Int16BufferAttribute( indices, 1 ) );
console.timeEnd( "parsing obj file" );
console.time( "setting the path finder up" );
var pathfinder = new Pathfinder( geometry );
console.timeEnd( "setting the path finder up" );
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera( 42, 1, 2, 200 );
camera.position.set( 30, 40, 70 );
camera.lookAt( scene.position );
var renderer = new THREE.WebGLRenderer( { antialias: true } );
document.body.appendChild( renderer.domElement );
var render = function() {
renderer.render( scene, camera );
};
var resize = function() {
var w = innerWidth, h = innerHeight;
camera.aspect = w / h; camera.updateProjectionMatrix();
renderer.setSize( w, h );
render();
};
addEventListener( "resize", resize );
scene.add( new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { color: 0x333333, wireframe: true } ) ) );
resize();
var controls = new THREE.OrbitControls( camera, renderer.domElement );
controls.minDistance = 50;
controls.maxDistance = 150;
controls.addEventListener( "change", render );
// demo
var line = new THREE.Line( new THREE.BufferGeometry(), new THREE.LineBasicMaterial( {
color: 0xff0000, depthTest: false, transparent: true
} ) );
scene.add( line );
setInterval( function() {
var n = positions.length / 3;
var a = Math.floor( n * Math.random() ) % n;
var b = ( a + Math.floor( n / 2 ) ) % n;
console.time( "finding the path from " + a + " to " + b );
var path = pathfinder.findPath( a, b );
console.timeEnd( "finding the path from " + a + " to " + b );
if( path ) {
line.geometry.dispose();
line.geometry = new THREE.BufferGeometry().setFromPoints( path );
requestAnimationFrame( render );
}
}, 1234 );
});
</script>
</body>
</html>