summaryrefslogtreecommitdiff
path: root/js/3D_Landscape.js
blob: 6871786329f65dd0dcb64000680766db37f68d5f (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
var container, stats;
var camera, controls, scene, renderer;
var mesh, texture, material;
var worldWidth = 256, worldDepth = 256, worldHalfWidth = worldWidth / 2, worldHalfDepth = worldDepth / 2;
var textureURL, heightmapURL;
var textureFile = 'img/test2.gif'; //texture doesn't need resizing
var heightmapFile = 'img/testnew.png';
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(function(){ animate() });
	//Start Scene Animation
}

function initGraphics(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;

//FIXME
  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);

  //	//Add FPS statistics
  //	stats = new Stats();
  //	stats.domElement.style.position = 'absolute';
  //	stats.domElement.style.top = '0px';
  //	container.appendChild(stats.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;
  //{{{init controls
    //Controls 
    

    //Toggle wire-frame
    shortcut.add("Space",function() {
      wf = !wf;
      material.wireframe = wf;
    });

    //Rotate Up
    shortcut.add("Shift+Up",function() {
      mesh.rotation.x += 0.1;
    });
    
    //Rotate Down
    shortcut.add("Shift+Down",function() {
      mesh.rotation.x -= 0.1;
    });
    
    //Rotate Left
    shortcut.add("Shift+Left",function() {
      mesh.rotation.z += 0.1;
    });
    
    //Rotate Right
    shortcut.add("Shift+Right",function() {
      mesh.rotation.z -= 0.1;
    });

    //Zoom In
    shortcut.add("Page_up",function() {
      mesh.position.z += 150;
    });

    //Zoom Out
    shortcut.add("Page_down",function() {
      mesh.position.z -= 150;
    });

    //Up
    shortcut.add("Up",function() {
      mesh.position.y += 150;
    });

    //Down
    shortcut.add("Down",function() {
      mesh.position.y -= 150;
    });
    
    //Left
    shortcut.add("Left",function() {
      mesh.position.x -= 150;
    });

    //Right
    shortcut.add("Right",function() {
      mesh.position.x += 150;
    });
    //Reset
    shortcut.add("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;
      
    });
  //}}}
  cb();
  };
}


function animate() {
	render();
	requestAnimationFrame(animate);
}

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

window.onload= function(e){
	initGraphics(function(){ animate() });
}