Terrain

基于高度图的地形支持
See Demo
Heightmap:

Code:

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
package {
import flash.display.Bitmap;
import flash.ui.Keyboard;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.geom.Vector3D;

import org.papervision3d.cameras.CameraType;
import org.papervision3d.lights.PointLight3D;
import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
import org.papervision3d.materials.utils.MaterialsList;
import org.papervision3d.objects.DisplayObject3D;
import org.papervision3d.objects.primitives.*;
import org.papervision3d.view.BasicView;
import org.papervision3d.view.stats.StatsView;
import org.papervision3d.view.layer.ViewportLayer;
import org.papervision3d.view.layer.util.ViewportLayerSortMode;

import jiglib.cof.JConfig;
import jiglib.geometry.*;
import jiglib.math.*;
import jiglib.physics.RigidBody;
import jiglib.plugin.papervision3d.*;

[SWF(width="800", height="600", backgroundColor="#ffffff", frameRate="60")]
public class TestTerrain extends BasicView
{
[Embed(source="res/hightmap1.jpg")]
public var TERRIAN_MAP:Class;

private var terrain:JTerrain;
private var ballBody:Vector.;
private var boxBody:Vector.;
private var capsuleBody:Vector.;
private var physics:Papervision3DPhysics;

private var keyRight :Boolean = false;
private var keyLeft :Boolean = false;
private var keyForward :Boolean = false;
private var keyReverse :Boolean = false;
private var keyUp:Boolean = false;

public function TestTerrain()
{
super(800, 600, true, true, CameraType.TARGET);

stage.addEventListener( KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener( KeyboardEvent.KEY_UP, keyUpHandler);

init();
var stats:StatsView = new StatsView(renderer);
addChild(stats);
}

private function init():void
{
viewport.containerSprite.sortMode = ViewportLayerSortMode.INDEX_SORT;

physics = new Papervision3DPhysics(scene, 8);

var mylight:PointLight3D = new PointLight3D(true, true);
mylight.y = 600;
mylight.z = -400;
camera.y = mylight.y;
camera.z = mylight.z;

var terrainBMD:Bitmap = new TERRIAN_MAP;
var shadeMateria:FlatShadeMaterial = new FlatShadeMaterial(mylight, 0x77ee77);

//create terrain
terrain = physics.createTerrain(terrainBMD.bitmapData, shadeMateria, 800, 800, 350, 10, 10);
terrain.friction = 0.2;
viewport.getChildLayer(DisplayObject3D(terrain.terrainMesh)).layerIndex = 1;

var vplObjects:ViewportLayer = new ViewportLayer(viewport,null);
vplObjects.layerIndex = 2;
vplObjects.sortMode = ViewportLayerSortMode.Z_SORT;
viewport.containerSprite.addLayer(vplObjects);

ballBody = new Vector.();
var color:uint;
for (var i:int = 0; i < 6; i++)
{
color = (i == 0)?0xff8888:0xeeee00;
shadeMateria = new FlatShadeMaterial(mylight, color);
ballBody[i] = physics.createSphere(shadeMateria, 25);
ballBody[i].moveTo(new Vector3D( 0, 100 + (60 * i + 60), 200));
vplObjects.addDisplayObject3D(physics.getMesh(ballBody[i]));
}
ballBody[0].mass = 10;

shadeMateria = new FlatShadeMaterial(mylight,0xeeee00);
var materiaList:MaterialsList = new MaterialsList();
materiaList.addMaterial(shadeMateria,"all");
boxBody=new Vector.();
for (i = 0; i < 2; i++)
{
boxBody[i] = physics.createCube(materiaList, 50, 40, 30);
boxBody[i].moveTo(new Vector3D(-200, 100 + (50 * i + 50), 0));
vplObjects.addDisplayObject3D(physics.getMesh(boxBody[i]));
}

var capsuleSkin:Cylinder;
capsuleBody = new Vector.();
for (i = 0; i < 2; i++)
{
capsuleSkin = new Cylinder(shadeMateria, 20, 50);
scene.addChild(capsuleSkin);
vplObjects.addDisplayObject3D(capsuleSkin);

capsuleBody[i] = new JCapsule(new Pv3dMesh(capsuleSkin), 20, 30);
capsuleBody[i].moveTo(new Vector3D(200, 100 + (80 * i + 80), 0));
physics.addBody(capsuleBody[i]);
}

startRendering();
}

private function keyDownHandler(event:KeyboardEvent):void
{
switch(event.keyCode)
{
case Keyboard.UP:
keyForward = true;
keyReverse = false;
break;

case Keyboard.DOWN:
keyReverse = true;
keyForward = false;
break;

case Keyboard.LEFT:
keyLeft = true;
keyRight = false;
break;

case Keyboard.RIGHT:
keyRight = true;
keyLeft = false;
break;
case Keyboard.SPACE:
keyUp = true;
break;
}
}

private function keyUpHandler(event:KeyboardEvent):void
{
switch(event.keyCode)
{
case Keyboard.UP:
keyForward = false;
break;

case Keyboard.DOWN:
keyReverse = false;
break;

case Keyboard.LEFT:
keyLeft = false;
break;

case Keyboard.RIGHT:
keyRight = false;
break;
case Keyboard.SPACE:
keyUp=false;
}
}

override protected function onRenderTick(event:Event=null):void
{
if(keyLeft)
{
ballBody[0].addWorldForce(new Vector3D(-100,0,0),ballBody[0].currentState.position);
}
if(keyRight)
{
ballBody[0].addWorldForce(new Vector3D(100,0,0),ballBody[0].currentState.position);
}
if(keyForward)
{
ballBody[0].addWorldForce(new Vector3D(0,0,100),ballBody[0].currentState.position);
}
if(keyReverse)
{
ballBody[0].addWorldForce(new Vector3D(0,0,-100),ballBody[0].currentState.position);
}
if(keyUp)
{
ballBody[0].addWorldForce(new Vector3D(0, 100, 0), ballBody[0].currentState.position);
}

physics.engine.integrate(0.2);
renderer.renderScene(scene, camera, viewport);
}
}
}

2 thoughts on “Terrain

  1. You wont believe but 2 days ago i got back (after 1/2 year) to car demo with added spring camera and some other minor modifications. I was just starting to build track for car by adding planes to scene and now I see terrain support!!!!!!! I read somewhere that it was in plans but thats a surprise I would never expect . Great work

  2. Pingback: AW Travel Blog » Blog Archive » First day of skiing at Marmot Basin | Skiing Leisure Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *