Ejemplo: Proscenium – Modificar los vertices Año: 2012 V.: AS3
[e]
Proscenium – Flash – AS3 – Modify the vertices of a triangle (vertex elevation – part 1)
Example:
This movie requires Flash Player 9
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 |
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; import com.xavirobot.media.LoadMedia; //Conf UI [ SWF( width="600", height="400", backgroundColor="0x000000", frameRate="125" ) ] public class mainVertexTransformPart1 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; private var PLANE: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; //CONTRUCTOR public function mainVertexTransformPart1(){ getContext = new LoadContext(onContext3DInit); addChild(getContext); } //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(12,25,35); CAMARA.prependRotation(-20,Vector3D.X_AXIS); //LIGHT LUZ = new SceneLight(); LUZ.color.set( .5, .5, .5 ); LUZ.appendTranslation( -20, 20, 20 ); LUZ.kind = "distant"; LUZ.shadowMapEnabled = true; LUZ.setShadowMapSize( 256, 256 ); //LUZ.transform.prependRotation( -90, Vector3D.Y_AXIS ); LUZ.transform.prependRotation( -55, Vector3D.X_AXIS ); instance.scene.addChild( LUZ ); //MAP MAP = new SceneMesh(); instance.scene.addChild( MAP ); PLANE = MeshUtils.createPlane(200,200, 1, 1,null, "plane" ); instance.scene.addChild( PLANE ); PLANE.move(0,-0.1,0); LUZ.addToShadowMap(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); addElement_btn.addEventListener(MouseEvent.CLICK, onCreateElement); delElement_btn.addEventListener(MouseEvent.CLICK, onDeleteElement); addElement_btn.enabled = false; //LOAD TEXTURE loaderR.Load("source/stone.jpg",onLoadTexture); } /* * ON TEXTURE LOADED */ private function onLoadTexture():void{ onCreateElement(); } /* * CREATE ELEMENT */ private function onCreateElement(e:Event = null):void { addElement_btn.enabled = false; delElement_btn.enabled = true; //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,false,false,0,"Texture1Name",false,false,1); //Indicamos los vertices y la posicion de cada uno que formaran el rectangulo mediante dos triangulos //Elevamos el ultimo vertice a 30 respecto al eje Y var b:int = 25; var vertices:Vector.<Number > = new Vector.<Number >; var y1:int = int(vy1.text); var y2:int = int(vy2.text); var y3:int = int(vy3.text); var y4:int = int(vy4.text); vertices = Vector.<Number>([ 0, y1, 0, 0, 0, 0, 0, 0, //(Vertice 1, Indice 0) x, y, z, nx, ny, nz, u, v b, y2, 0, 0, 0, 0, 1, 0, //(Vertice 2, Indice 1) 0, y3, -b, 0, 0, 0, 0, 1, //(Vertice 3, Indice 2) b, y4, -b, 0, 0, 0, 1, 1 //(Vertice 4, Indice 3) ]); //Conectamos los vertices de los triangulos mediante los indices var indices:Vector.<uint > = new Vector.<uint > ; indices = Vector.<uint > ([0,1,2,1,2,3]); //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); //Creamos el elemento var meshE:MeshElement = new MeshElement("MeshElementName","MaterialName",mat); //Especificamos el formato en el que se divide el vector de los vertices var vertiFmt:VertexFormat = new VertexFormat(new <VertexFormatElement>[ new VertexFormatElement( VertexFormatElement.SEMANTIC_POSITION, 0, Context3DVertexBufferFormat.FLOAT_3, 0, "position" ), new VertexFormatElement( VertexFormatElement.SEMANTIC_NORMAL, 3, Context3DVertexBufferFormat.FLOAT_3, 0, "normal" ), new VertexFormatElement( VertexFormatElement.SEMANTIC_TEXCOORD, 6, Context3DVertexBufferFormat.FLOAT_2, 0, "texcoord" ) ]); //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 ); } /* * DELETE ELEMENT */ private function onDeleteElement(e:Event):void{ addElement_btn.enabled = true; delElement_btn.enabled = false; //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); } /* * RESIZE */ public function Resize(e:Event=null):void{ if ( !instance.scene )return; instance.configureBackBuffer( stage.stageWidth, stage.stageHeight, 6, 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); } }//End Class }//End package } |
Si te descargas el ejemplo recuerda actualizar la ruta de las librerias proscenium.swc y pellet.swc para que te funcione.
Download Source:
[download id=»40″ format=»2″]