Mar.13

Terrain creation using height maps and splat maps in Away3d

Terrain creation using height maps and splat maps in Away3d

Code by Rob Bateman & David Lenaerts, demonstrates:

  • How to create a 3D terrain out of a height map
  • How to enhance the detail of a material close-up by applying splat maps.
  • How to create a realistic lake effect.
  • How to create first-person camera motion using the FirstPersonController.

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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*
 
Terrain creation using height maps and splat maps
 
Demonstrates:
 
How to create a 3D terrain out of a hieght map
How to enhance the detail of a material close-up by applying splat maps.
How to create a realistic lake effect.
How to create first-person camera motion using the FirstPersonController.
 
Code by Rob Bateman & David Lenaerts
rob@infiniteturtles.co.uk
http://www.infiniteturtles.co.uk
david.lenaerts@gmail.com
http://www.derschmale.com
 
This code is distributed under the MIT License
 
Copyright (c)  
 
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
 
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
 
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
 
*/
 
package
{
	import away3d.cameras.*;
	import away3d.containers.*;
	import away3d.controllers.*;
	import away3d.debug.*;
	import away3d.entities.Mesh;
	import away3d.extrusions.*;
	import away3d.filters.*;
	import away3d.lights.*;
	import away3d.materials.*;
	import away3d.materials.lightpickers.*;
	import away3d.materials.methods.*;
	import away3d.primitives.*;
	import away3d.textures.*;
 
	import flash.display.*;
	import flash.events.*;
	import flash.filters.*;
	import flash.text.*;
	import flash.ui.*;
 
	[SWF(backgroundColor="#000000", frameRate="30", quality="LOW")]
 
	public class Advanced_TerrainDemo extends Sprite
	{
		//signature swf
		[Embed(source="/../embeds/signature.swf", symbol="Signature")]
		public var SignatureSwf:Class;
 
		// Environment map.
		[Embed(source="/../embeds/skybox/snow_positive_x.jpg")]
		private var EnvPosX:Class;
		[Embed(source="/../embeds/skybox/snow_positive_y.jpg")]
		private var EnvPosY:Class;
		[Embed(source="/../embeds/skybox/snow_positive_z.jpg")]
		private var EnvPosZ:Class;
		[Embed(source="/../embeds/skybox/snow_negative_x.jpg")]
		private var EnvNegX:Class;
		[Embed(source="/../embeds/skybox/snow_negative_y.jpg")]
		private var EnvNegY:Class;
		[Embed(source="/../embeds/skybox/snow_negative_z.jpg")]
		private var EnvNegZ:Class;
 
		//water normal map
		[Embed(source="/../embeds/water_normals.jpg")]
		private var WaterNormals:Class;
 
		// terrain height map
		[Embed(source="/../embeds/terrain/terrain_heights.jpg")]
		private var HeightMap:Class;
 
		// terrain texture map
		[Embed(source="/../embeds/terrain/terrain_diffuse.jpg")]
		private var Albedo:Class;
 
		// terrain normal map
		[Embed(source="/../embeds/terrain/terrain_normals.jpg")]
		private var Normals:Class;
 
		//splat texture maps
		[Embed(source="/../embeds/terrain/grass.jpg")]
		private var Grass:Class;
		[Embed(source="/../embeds/terrain/rock.jpg")]
		private var Rock:Class;
		[Embed(source="/../embeds/terrain/beach.jpg")]
		private var Beach:Class;
 
		//splat blend map
		[Embed(source="/../embeds/terrain/terrain_splats.png")]
		private var Blend:Class;
 
		//engine variables
		private var scene:Scene3D;
		private var camera:Camera3D;
		private var view:View3D;
		private var cameraController:FirstPersonController;
		private var awayStats:AwayStats;
 
		//signature variables
		private var Signature:Sprite;
		private var SignatureBitmap:Bitmap;
 
		//light objects
		private var sunLight:DirectionalLight;
		private var lightPicker:StaticLightPicker;
		private var fogMethod:FogMethod;
 
		//material objects
		private var terrainMethod:TerrainDiffuseMethod;
		private var waterMethod:SimpleWaterNormalMethod;
		private var fresnelMethod:FresnelSpecularMethod;
		private var terrainMaterial:TextureMaterial;
		private var waterMaterial:TextureMaterial;
		private var cubeTexture:BitmapCubeTexture;
 
		//scene objects
		private var text:TextField;
		private var terrain:Elevation;
		private var plane:Mesh;
 
		//rotation variables
		private var move:Boolean = false;
		private var lastPanAngle:Number;
		private var lastTiltAngle:Number;
		private var lastMouseX:Number;
		private var lastMouseY:Number;
 
		//movement variables
		private var drag:Number = 0.5;
		private var walkIncrement:Number = 2;
		private var strafeIncrement:Number = 2;
		private var walkSpeed:Number = 0;
		private var strafeSpeed:Number = 0;
		private var walkAcceleration:Number = 0;
		private var strafeAcceleration:Number = 0;
 
		/**
		 * Constructor
		 */
		public function Advanced_TerrainDemo()
		{
			init();
		}
 
		/**
		 * Global initialise function
		 */
		private function init():void
		{
			initEngine();
			initText();
			initLights();
			initMaterials();
			initObjects();
			initListeners();
		}
 
		/**
		 * Initialise the engine
		 */
		private function initEngine():void
		{
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
 
			view = new View3D();
			scene = view.scene;
			camera = view.camera;
 
			camera.lens.far = 14000;
			camera.lens.near = .05;
			camera.y = 300;
 
			//setup controller to be used on the camera
			cameraController = new FirstPersonController(camera, 180, 0, -80, 80);
 
			view.addSourceURL("srcview/index.html");
			addChild(view);
 
			//add signature
			Signature = Sprite(new SignatureSwf());
			SignatureBitmap = new Bitmap(new BitmapData(Signature.width, Signature.height, true, 0));
			stage.quality = StageQuality.HIGH;
			SignatureBitmap.bitmapData.draw(Signature);
			stage.quality = StageQuality.LOW;
			addChild(SignatureBitmap);
 
			awayStats = new AwayStats(view)
			addChild(awayStats);
		}
 
		/**
		 * Create an instructions overlay
		 */
		private function initText():void
		{
			text = new TextField();
			text.defaultTextFormat = new TextFormat("Verdana", 11, 0xFFFFFF);
			text.width = 240;
			text.height = 100;
			text.selectable = false;
			text.mouseEnabled = false;
			text.text = "Mouse click and drag - rotate\n" + 
				"Cursor keys / WSAD - move\n";
 
			text.filters = [new DropShadowFilter(1, 45, 0x0, 1, 0, 0)];
 
			addChild(text);
		}
 
		/**
		 * Initialise the lights
		 */
		private function initLights():void
		{
			sunLight = new DirectionalLight(-300, -300, -5000);
			sunLight.color = 0xfffdc5;
			sunLight.ambient = 1;
			scene.addChild(sunLight);
 
			lightPicker = new StaticLightPicker([sunLight]);
 
			//create a global fog method
			fogMethod = new FogMethod(0, 8000, 0xcfd9de);
		}
 
		/**
		 * Initialise the material
		 */
		private function initMaterials():void
		{
			cubeTexture = new BitmapCubeTexture(new EnvPosX().bitmapData, new EnvNegX().bitmapData, new EnvPosY().bitmapData, new EnvNegY().bitmapData, new EnvPosZ().bitmapData, new EnvNegZ().bitmapData);
 
			terrainMethod = new TerrainDiffuseMethod([new BitmapTexture(new Beach().bitmapData), new BitmapTexture(new Grass().bitmapData), new BitmapTexture(new Rock().bitmapData)], new BitmapTexture(new Blend().bitmapData) , [1, 50, 150, 100]);
 
			terrainMaterial = new TextureMaterial(new BitmapTexture(new Albedo().bitmapData));
			terrainMaterial.diffuseMethod = terrainMethod;
			terrainMaterial.normalMap = new BitmapTexture(new Normals().bitmapData);
			terrainMaterial.lightPicker = lightPicker;
			terrainMaterial.ambientColor = 0x303040;
			terrainMaterial.ambient = 1;
			terrainMaterial.specular = .2;
			terrainMaterial.addMethod(fogMethod);
 
			waterMethod = new SimpleWaterNormalMethod(new BitmapTexture(new WaterNormals().bitmapData), new BitmapTexture(new WaterNormals().bitmapData));
			fresnelMethod = new FresnelSpecularMethod();
			fresnelMethod.normalReflectance = .3;
 
			waterMaterial = new TextureMaterial(new BitmapTexture(new BitmapData(512, 512, true, 0xaa404070)));
			waterMaterial.alphaBlending = true;
			waterMaterial.lightPicker = lightPicker;
			waterMaterial.repeat = true;
			waterMaterial.normalMethod = waterMethod
			waterMaterial.addMethod(new EnvMapMethod(cubeTexture));
			waterMaterial.specularMethod = fresnelMethod;
			waterMaterial.gloss = 100;
			waterMaterial.specular = 1;
		}
 
 
		/**
		 * Initialise the scene objects
		 */
		private function initObjects():void
		{
			//create skybox.
			scene.addChild(new SkyBox(cubeTexture));
 
			//create mountain like terrain
			terrain = new Elevation(terrainMaterial, new HeightMap().bitmapData, 5000, 1300, 5000, 250, 250);
			scene.addChild(terrain);
 
			//create water
			plane = new Mesh(new PlaneGeometry(5000, 5000), waterMaterial);
			plane.geometry.scaleUV(50, 50);
			plane.y = 285;
			scene.addChild(plane);
		}
 
		/**
		 * Initialise the listeners
		 */
		private function initListeners():void
		{
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
			stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			stage.addEventListener(Event.RESIZE, onResize);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
			onResize();
		}
 
		/**
		 * Navigation and render loop
		 */
		private function onEnterFrame(event:Event):void
		{
			//set the camera height based on the terrain (with smoothing)
			camera.y += 0.2*(terrain.getHeightAt(camera.x, camera.z) + 20 - camera.y);
 
			if (move) {
				cameraController.panAngle = 0.3*(stage.mouseX - lastMouseX) + lastPanAngle;
				cameraController.tiltAngle = 0.3*(stage.mouseY - lastMouseY) + lastTiltAngle;
 
			}
 
			if (walkSpeed || walkAcceleration) {
				walkSpeed = (walkSpeed + walkAcceleration)*drag;
				if (Math.abs(walkSpeed) < 0.01)
					walkSpeed = 0;
				cameraController.incrementWalk(walkSpeed);
			}
 
			if (strafeSpeed || strafeAcceleration) {
				strafeSpeed = (strafeSpeed + strafeAcceleration)*drag;
				if (Math.abs(strafeSpeed) < 0.01)
					strafeSpeed = 0;
				cameraController.incrementStrafe(strafeSpeed);
			}
 
			//animate our lake material
			waterMethod.water1OffsetX += .001;
			waterMethod.water1OffsetY += .001;
			waterMethod.water2OffsetX += .0007;
			waterMethod.water2OffsetY += .0006;
 
			view.render();
		}
 
		/**
		 * Key down listener for camera control
		 */
		private function onKeyDown(event:KeyboardEvent):void
		{
			switch (event.keyCode) {
				case Keyboard.UP:
				case Keyboard.W:
					walkAcceleration = walkIncrement;
					break;
				case Keyboard.DOWN:
				case Keyboard.S:
					walkAcceleration = -walkIncrement;
					break;
				case Keyboard.LEFT:
				case Keyboard.A:
					strafeAcceleration = -strafeIncrement;
					break;
				case Keyboard.RIGHT:
				case Keyboard.D:
					strafeAcceleration = strafeIncrement;
					break;
			}
		}
 
		/**
		 * Key up listener for camera control
		 */
		private function onKeyUp(event:KeyboardEvent):void
		{
			switch (event.keyCode) {
				case Keyboard.UP:
				case Keyboard.W:
				case Keyboard.DOWN:
				case Keyboard.S:
					walkAcceleration = 0;
					break;
				case Keyboard.LEFT:
				case Keyboard.A:
				case Keyboard.RIGHT:
				case Keyboard.D:
					strafeAcceleration = 0;
					break;
			}
		}
 
		/**
		 * Mouse down listener for navigation
		 */
		private function onMouseDown(event:MouseEvent):void
		{
			move = true;
			lastPanAngle = cameraController.panAngle;
			lastTiltAngle = cameraController.tiltAngle;
			lastMouseX = stage.mouseX;
			lastMouseY = stage.mouseY;
			stage.addEventListener(Event.MOUSE_LEAVE, onStageMouseLeave);
		}
 
		/**
		 * Mouse up listener for navigation
		 */
		private function onMouseUp(event:MouseEvent):void
		{
			move = false;
			stage.removeEventListener(Event.MOUSE_LEAVE, onStageMouseLeave);
		}
 
		/**
		 * Mouse stage leave listener for navigation
		 */
		private function onStageMouseLeave(event:Event):void
		{
			move = false;
			stage.removeEventListener(Event.MOUSE_LEAVE, onStageMouseLeave);
		}
 
		/**
		 * stage listener for resize events
		 */
		private function onResize(event:Event = null):void
		{
			view.width = stage.stageWidth;
			view.height = stage.stageHeight;
			SignatureBitmap.y = stage.stageHeight - Signature.height;
			awayStats.x = stage.stageWidth - awayStats.width;
		}
	}
}
Technology
Share this Story:
  • facebook
  • twitter
  • gplus

About Jason Sturges

Avant-garde experimental artist; Objective-C; dhtml; ActionScript; motion and interactive design; photographer; Chapman Stick performer; human consciousness.

Comments(1)

  1. alex
    600 days ago

    awesome!!!!!!!!

Leave a comment

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax

Comment