summaryrefslogtreecommitdiff
path: root/js/3D_Landscape.js
blob: 777323f6951e524a0213d1e2524be992bf257495 (plain)
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
var container, stats;
var camera, controls, scene, renderer;
var mesh, texture, material;
//FIXME experiment with these fixed params
var worldWidth = 256, worldDepth = 256, worldHalfWidth = worldWidth / 2, worldHalfDepth = worldDepth / 2;
var textureFile = 'img/Texture.jpg'; 
var heightmapFile = 'img/Heightmap.jpg';
var pos_x, pos_y, pos_z, rot_x, rot_y, rot_z;
var wf = false;


function runWebGLSimulation(){
	//Detect WebGL
	if (!Detector.webgl) {
		Detector.addGetWebGLMessage();
		document.getElementById('container').innerHTML = "";
	}

	//Start Graphics
	initGraphics(textureFile, heightmapFile, function(){ animate() });
	//Start Scene Animation
}

function initGraphics(textureFile, heightmapFile, cb) {

	container = document.getElementById('container');
	//Set camera
	camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 20000);
	//Set scene
	scene = new THREE.Scene();
	
	//Get heightmap data
	//Generates the heightmap data from the heightmap image
	var size = worldWidth * worldDepth, data = new Float32Array(size);

	var canvas = document.createElement('canvas');
	canvas.width = worldWidth;
	canvas.height = worldDepth;
	context = canvas.getContext('2d');
	context.fillStyle = '#000';
	context.fillRect(0, 0, worldWidth, worldDepth);

	var img = new Image();
	img.src = heightmapFile;
  img.onerror = function() { 
    alert("The following url did not work: \n"+heightmapFile.slice(15)); 
    is_generating = false;
    toggle_background();
  };

  img.onload = function(){
    context.drawImage(img, 0, 0);
    image = context.getImageData(0, 0, worldWidth, worldDepth);
    var imageData = image.data;
    var pixels = size;
    for (var i=0; i<pixels; i++){
      // Get RGB
      red = imageData[4 * i + 0];
      green = imageData[4 * i + 1];
      blue = imageData[4 * i + 2];
      // Get grayscale
      gray = (red + green + blue) / 3;
      // Put heightmap value
      data[i] = gray;
    }

    //Set camera position
    camera.position.y = data[worldHalfWidth + worldHalfDepth * worldWidth] + 500;
    scene.add(camera);
    
    //Create geometry data used for mesh
    var geometry = new THREE.PlaneGeometry(7500, 7500, worldWidth - 1,	worldDepth - 1);
    for ( var i = 0, l = geometry.vertices.length; i < l; i++) {
      geometry.vertices[i].position.z = data[i] * 3;
    }
    
    //Load texture
    texture = THREE.ImageUtils.loadTexture(textureFile, {}, function() {
      renderer.render(scene, camera);
    });
    texture.needsUpdate = true;
    
    //Create mesh from heightmap and texture
    material = new THREE.MeshBasicMaterial({
      map : texture,
      wireframe: wf
    });
    mesh = new THREE.Mesh(geometry, material);
    mesh.rotation.x = -90 * Math.PI / 180;
    scene.add(mesh);

    //Create renderer
    renderer = new THREE.WebGLRenderer({
      preserveDrawingBuffer : true // required to support .toDataURL()
    }); 
    renderer.setSize(window.innerWidth, window.innerHeight - 150); //FIXME, add height of the controls
    container.innerHTML = "";
    container.appendChild(renderer.domElement);

    //Get default values
    pos_x = mesh.position.x;
    pos_y = mesh.position.y;
    pos_z = mesh.position.z;
    rot_x = mesh.rotation.x;
    rot_y = mesh.rotation.y;
    rot_z = mesh.rotation.z;
  is_generating = false;
  toggle_background();
  cb();
  };
}

var requestId;
function loop() {
    render();
    requestId = window.requestAnimationFrame(loop);
}
function animate() {
    if (!requestId) {
       loop();
    }
}
function stop_animating() {
    if (requestId) {
       window.cancelAnimationFrame(requestId);
       requestId = undefined;
    }
}


function render() {
	renderer.render(scene, camera);
}

function init_controls(){
  var listener = new window.keypress.Listener();
  var my_scope = this;
  var my_combos = listener.register_many([
      {
          "keys"          : "up",
          "is_solitary"  : true,
          "on_keydown"    : function() {
              mesh.position.y += 150;
          },
          "this"          : my_scope
      },
      {
          "keys"          : "down",
          "is_solitary"  : true,
          "on_keydown"    : function() {
              mesh.position.y -= 150;
          },
          "this"          : my_scope
      },
      {
          "keys"          : "right",
          "is_solitary"  : true,
          "on_keydown"    : function() {
              mesh.position.x += 150;
          },
          "this"          : my_scope
      },
      {
          "keys"          : "left",
          "is_solitary"  : true,
          "on_keydown"    : function() {
              mesh.position.x -= 150;
          },
          "this"          : my_scope
      },
      {
          "keys"          : "shift up",
          "is_exclusive"  : true,
          "on_keydown"    : function() {
              mesh.rotation.x += 0.1;
          },
          "this"          : my_scope
      },
      {
          "keys"          : "shift down",
          "is_exclusive"  : true,
          "on_keydown"    : function() {
              mesh.rotation.x -= 0.1;
          },
          "this"          : my_scope
      },
      {
          "keys"          : "shift right",
          "is_exclusive"  : true,
          "on_keydown"    : function() {
              mesh.rotation.z -= 0.1;
          },
          "this"          : my_scope
      },
      {
          "keys"          : "shift left",
          "is_exclusive"  : true,
          "on_keydown"    : function() {
              mesh.rotation.z += 0.1;
          },
          "this"          : my_scope
      },
  ]);
  //Reset
  listener.simple_combo("delete",function() {
    mesh.position.x = pos_x;
    mesh.position.y = pos_y;
    mesh.position.z = pos_z;
    mesh.rotation.x = rot_x;
    mesh.rotation.y = rot_y;
    mesh.rotation.z = rot_z;
    
  });
  //Zoom In
  listener.simple_combo("pageup",function() {
    mesh.position.z += 150;
  });

  //Zoom Out
  listener.simple_combo("pagedown",function() {
    mesh.position.z -= 150;
  });

}


window.onload= function(e){
  runWebGLSimulation();
  init_controls();
}