-
Notifications
You must be signed in to change notification settings - Fork 2
/
StartingCode-heightmap.html
122 lines (90 loc) · 2.85 KB
/
StartingCode-heightmap.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
<html>
<head>
<title>Starting Code for 1st Project 2017</title>
<style>
body {
font-family: Monospace;
background-color: #f0f0f0;
margin: 0px;
overflow: hidden;
}
canvas {
width: 100%;
height: 100%;
}
</style>
<script src="lib/three.min.js"></script>
<script src="lib/stats.min.js"></script>
<script src="lib/Coordinates.js"></script>
<script src="lib/OrbitControls.js"></script>
</head>
<body>
<script>
var scene, camera, renderer, controls, stats;
//return array with height data from img, taken from: http://danni-three.blogspot.it/2013/09/threejs-heightmaps.html
function getHeightData(img,scale) {
if (scale == undefined) scale=1;
var canvas = document.createElement( 'canvas' );
canvas.width = img.width;
canvas.height = img.height;
var context = canvas.getContext( '2d' );
var size = img.width * img.height;
console.log(size);
var data = new Float32Array( size );
context.drawImage(img,0,0);
for ( var i = 0; i < size; i ++ ) {
data[i] = 0
}
var imgd = context.getImageData(0, 0, img.width, img.height);
var pix = imgd.data;
var j=0;
for (var i = 0; i<pix.length; i +=4) {
var all = pix[i]+pix[i+1]+pix[i+2]; // all is in range 0 - 255*3
data[j++] = scale*all/3;
}
return data;
}
function Start() {
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
renderer = new THREE.WebGLRenderer( {antialias: true} );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setClearColor( 0xf0f0f0 );
document.body.appendChild( renderer.domElement );
camera.position.set(3,4,6);
camera.lookAt( new THREE.Vector3(0,0,0));
var geometry = new THREE.BoxGeometry(1,1,1);
var material = new THREE.MeshBasicMaterial( { color: 0xaaaaaa } );
var cube = new THREE.Mesh( geometry, material );
scene.add( cube );
stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.top = '0px';
document.body.appendChild( stats.domElement );
// uncomment if you need to draw coordinate axes when building the scene
//Coordinates.drawAllAxes();
controls = new THREE.OrbitControls( camera );
controls.addEventListener( 'change', Render );
// terrain
var img = new Image();
img.onload = function () {
//get height data from img
var data = getHeightData(img,0.1);
// load img source
img.src = "textures/heightmap2.png";
}
}
function Update() {
requestAnimationFrame( Update );
controls.update();
stats.update();
Render();
}
function Render() {
renderer.render(scene, camera);
}
Start();
Update();
</script>
</body>
</html>