Ejemplo: Proscenium – crear terreno Año: 2011 V.: AS3
[e]
Proscenium – Flash – AS3 – Modify vertex of triangle (vertex elevation – part 2 – creating a terrain with elevations)
The next example (Part 3) will create a dynamic map loading textures go and boost level as it progresses
El próximo ejemplo (Parte 3) será crear un mapa dinámico que vaya cargando las texturas y el nivel de elevación a medida que se avanza
Example:
If you set the width and height of the tiles between 1×1 or 5×5 and you get closer to the ground you will appreciate the speed increased, the smaller the tile faster, bigger slower the camera will move.
Try this setting one that will really slow Although only one box is displayed
Cols = 1
Rows = 1
Width = 200px
Height = 200px
RandomY = 1
Try these settings and see that going very fast although rectangles display 100×100 = 10,000 10,000 * 2 = 20,000 Triangles (With this configuration the map is very small and you must stick to the floor with Downy button)
Cols = 100
Rows = 100
Width = 1px
Height = 1px
RandomY = 1
Note:If the map is not drawn correctly the first time, then you press the button again drawn
The 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 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 |
/* * blog.xavirobot.com (2012) Javier Vicente Medina */ package{ /* * FLASH LIBRARI IMPORTS */ import flash.display.Sprite; import flash.display.BitmapData; import flash.display.Bitmap; import flash.geom.Vector3D; import flash.display3D.Context3D; import flash.display3D.Context3DVertexBufferFormat; import flash.events.Event; import flash.events.KeyboardEvent; import flash.events.MouseEvent; /* * PROSCENIUM LIBRARI IMPORTS */ import com.adobe.scenegraph.Instance3D; import com.adobe.scenegraph.SceneCamera; import com.adobe.scenegraph.SceneMesh; import com.adobe.scenegraph.SceneLight; import com.adobe.scenegraph.MaterialStandard; import com.adobe.scenegraph.MeshElement; import com.adobe.scenegraph.VertexFormat; import com.adobe.scenegraph.VertexFormatElement; import com.adobe.scenegraph.TextureMap; //import com.adobe.scenegraph.MeshUtils; /* * PROSCENIUM LIBRARI IMPORTS */ import com.xavirobot.media.LoadMedia; //Conf UI [ SWF( width="600", height="400", backgroundColor="0x000000", frameRate="125" ) ] public class mainVertexTransformPart2 extends Sprite{ private var loaderR:LoadMedia = new LoadMedia(); private var getContext:LoadContext; private var instance:Instance3D; private var context3D:Context3D; //OBJECTS private var CAMARA:SceneCamera; private var GLOBCAM:SceneMesh; private var LUZ:SceneLight; private var MAP:SceneMesh; //CONTROL CAMERA private var sR:Number = 2;//speedRotationKeyBoard private var xyVel:Number = 1.5; private var consPi:Number = Math.PI / 180; private var rad:Number =-90*consPi; private var cos:Number = xyVel * Math.cos(rad); private var sen:Number = xyVel * Math.sin(rad); private var ka:Array = new Array(); private var ko:Array = new Array(0,0,0,0,0,0,0,0,0); private var key:int; private var pres:int = 3; private var WORLDRotation:Number = 0; //TRIANGLE VARS private var vertices:Vector.<Number > = new Vector.<Number>; private var indices:Vector.<uint > = new Vector.<uint >; private var vertiFmt:VertexFormat; //FLAG private var startedMesh:Boolean = false; // started? //CONTRUCTOR public function mainVertexTransformPart2(){ getContext = new LoadContext(onContext3DInit); addChild(getContext); //Especificamos el formato en el que se divide el vector de los vertices vertiFmt = new VertexFormat(new <VertexFormatElement> [new VertexFormatElement("POSITION", 0, "float3", 0, "position") ,new VertexFormatElement("NORMAL" , 3, "float3", 0, "normal" ) ,new VertexFormatElement("TEXCOORD", 6, "float2", 0, "texcoord")]); } //INITIALIZE protected function onContext3DInit(_instance:Instance3D):void{ //CONTEXT3D instance = _instance; instance.scene.ambientColor.setFromUInt(0xFFFFFF); Resize(); //INIT CAMERA //Insertamos la camara dentro de otro contenedor //Esta tecnica permite modificar la rotacion X,Y,Z de la camara //Sin afectar los ejes de movimiento del contenedor GLOBCAM = new SceneMesh();//CONTAINER CAMERA FOR MOVE IN Z,X,Y instance.scene.addChild(GLOBCAM); CAMARA = instance.scene.activeCamera; //The camera can rotate in x, y, z without affecting the translation axes of the container CAMARA.identity(); GLOBCAM.addChild(CAMARA);//ADD CAMERA TO CONTAINER GLOBCAM.position = new Vector3D(55,25,100); CAMARA.prependRotation(-20,Vector3D.X_AXIS); //LIGHT LUZ = new SceneLight(); LUZ.color.set( .5, .5, .5 ); LUZ.kind = "distant"; LUZ.shadowMapEnabled = true; LUZ.setShadowMapSize( 256, 256 ); instance.scene.addChild( LUZ ); //MAP MAP = new SceneMesh(); instance.scene.addChild( MAP ); //LISTENERS stage.addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel); stage.addEventListener(KeyboardEvent.KEY_DOWN,onKey); stage.addEventListener(KeyboardEvent.KEY_UP,onKey); stage.addEventListener( Event.ENTER_FRAME, RENDER_FRAME); stage.addEventListener( Event.RESIZE, Resize); redrawMesh_btn.addEventListener(MouseEvent.CLICK, onCreateElement); //LOAD TEXTURE loaderR.Load("source/stone1.png",onLoadTexture); } /* * ON TEXTURE LOADED */ private function onLoadTexture():void{ onCreateElement(); } /* * CREATE ELEMENT */ private function onCreateElement(e:Event = null):void { //Creamos el material var mat:MaterialStandard = new MaterialStandard("material1"); //Recuperamos el bitamp de la textura cargada y la clonamos var bitData:BitmapData = Bitmap(loaderR._Mvc.content).bitmapData.clone(); //Añadimos la textura al material mat.diffuseMap = new TextureMap(bitData,false,ch_mp.selected,ch_lf.selected,0,"Texture1Name",false,false,1); drawMalla(int(txt_cols.text),int(txt_rows.text),int(txt_width.text),int(txt_height.text)); //Añadimos los vertices a otro vector var vertiSets:Vector.<Vector.<Number>> = new Vector.<Vector.<Number>>; vertiSets.push(vertices); //Añadimos los indices a otro vector var indiSets:Vector.<Vector.<uint>> = new Vector.<Vector.<uint>>; indiSets.push(indices); //Si la malla ya fue creada entonces la eliminamos para poder crear otra if(startedMesh){ onDeleteElement(); instance.configureBackBuffer( stage.stageWidth, stage.stageHeight, int(txt_aa.text), true ); } //Creamos el elemento var meshE:MeshElement = new MeshElement("MeshElementName","MaterialName",mat); //Inicializamos el elemento indicando los vertices los indices y el formato meshE.initialize(vertiSets,indiSets,vertiFmt); //Añadimos el elemento al Mapa MAP.addElement( meshE ); startedMesh = true; } /* * DELETE ELEMENT */ private function onDeleteElement(e:Event=null):void{ //Recuperamos el elemento por su nombre var meshElement:MeshElement = MAP.getElementByName("MeshElementName"); //Recuperamos el material del elemento var mat:MaterialStandard = meshElement.material as MaterialStandard; //Recuperamos la textura del material var dif:TextureMap = mat.diffuseMap as TextureMap; //Borramos el bitmap clonado de la textura dif.data.dispose(); //Finalmente eliminamos el elemento MAP.deleteElement(meshElement); //Y reiniciamos los array vectores vertices = new Vector.<Number>; indices = new Vector.<uint>; } /* * RESIZE */ public function Resize(e:Event=null):void{ if ( !instance.scene )return; instance.configureBackBuffer( stage.stageWidth, stage.stageHeight, int(txt_aa.text), true ); instance.scene.activeCamera.aspect = stage.stageWidth / stage.stageHeight; instance.render( 0, false ); instance.present(); } /* * RENDER */ protected function RENDER_FRAME( event:Event ):void { if (int(ka[0])) { rad =((WORLDRotation +=sR)-90)*consPi; GLOBCAM.prependRotation(-sR,Vector3D.Y_AXIS);//Rotate Left; cos = xyVel * Math.cos(rad); sen = xyVel * Math.sin(rad); } else if (int(ka[1])) { rad =((WORLDRotation -=sR)-90)*consPi; GLOBCAM.prependRotation(sR,Vector3D.Y_AXIS);//Rotate Right; cos = xyVel * Math.cos(rad); sen = xyVel * Math.sin(rad); } if (int(ka[2])) {//move Left GLOBCAM.move(+sen,0,-cos); } else if (int(ka[3])) {//move Right GLOBCAM.move(-sen,0,+cos); } if (int(ka[4])) {//move Up GLOBCAM.move(+cos,0,+sen); } else if (int(ka[5])) {//move Down GLOBCAM.move(-cos,0,-sen); } if (int(ka[7])) {//Key UP Rotate Cam X CAMARA.prependRotation(sR,Vector3D.X_AXIS); } else if (int(ka[8])) {//Key DOWN Rotate Cam X CAMARA.prependRotation(-sR,Vector3D.X_AXIS); } //PRESENT instance.render( 0, false ); instance.present(); } /* * KEYS PRESSED */ protected function onKey( event:KeyboardEvent ):void{ var k:int = event.keyCode; event.type == "keyUp" ? [pres = 0,key == k ? key = 0:[]]:[key != k ? [pres = 1,key = k]:[]]; if (pres == 0 || pres == 1) { if (k == 32 || k == 65 || k == 68 || k == 37 || k == 39 || k == 87 || k == 40 || k == 38 || k == 83) { if (k == 39) { ko[0] = pres;//rotate } if (k == 37) { ko[1] = pres;//rotate } if (k == 65) {//move Left; ko[2] = pres; } if (k == 68) {//move Right; ko[3] = pres; } if (k == 87 ) {//move Up ko[4] = pres; } if (k == 83) {//Key[S] move Down ko[5] = pres; } if (k == 32) {//ESPACIO ko[6] = pres; } if (k == 38) {//Key UP Rotate Cam X ko[7] = pres; } if (k == 40) {//Key DOWN Rotate Cam X ko[8] = pres; } sendKeys(ko.join()); }//End if check number Keys if (k == 32 || k == 65 || k == 68 etc.. pres = 3; }//End if (pres == 0 || pres == 1) { }//End onKey /* * KEYS PRESSED SENDER */ public function sendKeys(keystrokes:String):void { ka = new Array(); ka = keystrokes.split(","); } /* * CAMERA ELEVATION Y */ private function onMouseWheel(event:MouseEvent) : void{ GLOBCAM.move(0,event.delta*2,0); } //Author: Javier Vicente Medina private function drawMalla(cols:int,rows:int,rectH:int,rectW):void { if(cols <=0){cols = 1;} if(rows <=0){rows = 1;} /* Creamos los vertices */ var uvCount:int = 0; var ux:int = 0; var vy:int = 0; var parinpar:Boolean = true; for (var r:int=0; r<=rows; r++) { parinpar = !parinpar; uvCount = 0; trace(parinpar); for (var n:int=0; n<=cols; n++) { vertices.push(rectW*n); //X Pos vertices.push(Math.random()*int(txt_vyRandom.text)); //Y Elevation Random vertices.push(rectH*r); //Z Pos vertices.push(0); //nx vertices.push(0); //ny vertices.push(0); //nz if(!parinpar){ if(uvCount == 0){ ux = vy = 0; }else if(uvCount == 1){ ux = 1; vy = 0; uvCount = -1; } }else{ if(uvCount == 0){ ux = 0;vy = 1; }else if(uvCount == 1){ ux = 1; vy = 1; uvCount = -1; } } vertices.push(ux); //u data Random vertices.push(vy); //v data Random uvCount++; trace(uvCount); } } /* Conectamos los índices con los vértices */ var p:int = 0; for (var s:int=0; s<rows*cols; s++) { if (s % cols == 0 && s != 0) { p++; } indices.push(p); indices.push(p+1); indices.push((p+1)+cols); indices.push(p+1); indices.push(((p+1)+cols)+1); indices.push((p+1)+cols); p++; } }//End DrawMalla }//End Class }//End package |
Download Source:
[download id=»41″ format=»2″]