-
Notifications
You must be signed in to change notification settings - Fork 2
/
terrain.js
77 lines (70 loc) · 2.2 KB
/
terrain.js
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
/*
Taming Macaws
Adapted from http://threejs.org/examples/#canvas_geometry_birds
Available on https://github.com/ongmingyang/macaws
Ong Ming Yang <[email protected]>
2014
*/
function addTerrain ( scene ) {
// Fog
scene.fog = new THREE.Fog( 0xffffff, 100 );
//gridHelper = new THREE.GridHelper( 100, 10 );
//scene.add( gridHelper );
// Light
light = new THREE.DirectionalLight( 0xffddcc, 1.5 );
light.position.set( -150, 150, 0 );
light.castShadow = true;
//light.shadowCameraVisible = true;
d = 200;
light.shadowCameraLeft = -d;
light.shadowCameraRight = d;
light.shadowCameraTop = d;
light.shadowCameraBottom = -d;
light.shadowCameraFar = 500;
scene.add( light );
scene.add( new THREE.AmbientLight( 0x404040 ) );
// Ground texture
plane = new THREE.PlaneGeometry( 2000, 2000 );
groundTexture = THREE.ImageUtils.loadTexture( 'textures/grass.jpg' );
groundTexture.wrapS = groundTexture.wrapT = THREE.RepeatWrapping;
groundTexture.repeat.set( 25, 25 );
groundTexture.anisotropy = 16;
groundMaterial = new THREE.MeshPhongMaterial({
color: 0x335533,
specular: 0x111111,
map: groundTexture
});
planeMesh = new THREE.Mesh( plane, groundMaterial );
planeMesh.rotation.x = - Math.PI / 2;
planeMesh.receiveShadow = true;
scene.add( planeMesh );
// Add obstacles
dv = function () {
return Math.random() * 100 - 50;
}
positions = [
{ x: 80 + dv(), z: 80 + dv() },
{ x: -80 + dv(), z: 80 + dv() },
{ x: 80 + dv(), z: -80 + dv() },
{ x: -80 + dv(), z: -80 + dv() },
{ x: dv(), z: dv() }
];
for ( var i = 0; i < positions.length; i++ ) {
p = positions[i];
_height = 100;
_radius = 4;
geometry = new THREE.CylinderGeometry( _radius/2, _radius, _height );
treeTexture = THREE.ImageUtils.loadTexture( 'textures/tree.jpg' );
material = new THREE.MeshPhongMaterial({
color: 0xffeecc,
map: treeTexture
});
obstacle = new THREE.Mesh( geometry, material );
obstacle.castShadow = true;
obstacle.receiveShadow = true;
obstacle.position.set( p.x, _height/2, p.z );
obstacle.avoidRadius = _radius;
obstacles.push( obstacle );
scene.add( obstacle );
}
}