In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

CLICK, MOUSE_OVER, ... - EZCreateMesh

EZCreateMesh - MouseEvent3D DEMO
addEventListenerも簡単に
Get Adobe Flash player
by TmskSt 19 Oct 2010
package {
	import alternativ5.engine3d.controllers.*;
	import alternativ5.engine3d.core.*;
	import alternativ5.engine3d.display.*;
	import alternativ5.engine3d.events.MouseEvent3D;
	import alternativ5.types.*;
	import alternativ5.utils.*;

	import flash.display.*;
	import flash.events.*;
	
	/**
	 * EZCreateMesh - MouseEvent3D DEMO
	 * addEventListenerも簡単に
	 */
	
	[SWF(backgroundColor="#000000", frameRate="120")]
	public class Main extends Sprite {
	    
		private var scene:Scene3D;
		private var view:View;
		private var camera:Camera3D;
		private var cameraController:CameraController;
		
		public var textures:/***/Array;
		private var meshes:/*Mesh*/Array;
		private var map:Map;
		
		public function Main() {
			this.addEventListener(Event.ADDED, init);
		}
		
		public function init(e:Event):void {
			this.removeEventListener(Event.ADDED, init);
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			scene = new Scene3D();
			scene.root = new Object3D();
			
			camera = new Camera3D();
			camera.x = camera.y = 800;
			camera.z = 1000;
			scene.root.addChild(camera);
			
			view = new View();
			view.interactive = true;
			view.camera = camera;
			addChild(view);
			
			
			cameraController = new CameraController(stage);
			cameraController.camera = camera;
			cameraController.checkCollisions = false;
			cameraController.speed = 500;
			cameraController.controlsEnabled = true;
			cameraController.setDefaultBindings();
			cameraController.lookAt(new Point3D());
			
			FPS.init(stage);
			
			stage.addEventListener(Event.RESIZE, onResize);
			stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
			onResize(null);
			
			
			//create
			create();
		}
		
		/**
		 * 
		 */
		private function create():void {
			
			/**
			 * メッシュ情報
			 */
			var xml:XML = 
			<meshes>
				<mesh name="Geo">
					< surface
						materialType={ EZCreateMesh.FILL }
						color = "0xFF0000"
						{MouseEvent3D.CLICK } = "click"
						{MouseEvent3D.MOUSE_OVER } = "mouseOver"
						{MouseEvent3D.MOUSE_OUT } = "mouseOut"
					>
						<face>
							<UVs>
								<aUV x="0" y="0" />
								<bUV x="0" y="1" />
								<cUV x="1" y="1" />
							</UVs>
							<vertex x="-200" y="-200" z="0" />
							<vertex x="200" y="-200" z="0" />
							<vertex x="200" y="200" z="0" />
							<vertex x="-200" y="200" z="0" />
						</face>
					</surface>
				</mesh>
			</meshes>;
			
			
			// createMeshes
			meshes = EZCreateMesh.createMeshes(xml, textures, this);
			
			// addMehses
			addMehses(meshes);
		}
		
		public function click(e:MouseEvent3D):void {
			meshes["Geo"].setMaterialToSurface(EZCreateMesh.createMaterial(<surface materialType={EZCreateMesh.FILL} color="0xFFFFFF" />), e.surface.id);
		}
		
		public function mouseOver(e:MouseEvent3D):void {
			meshes["Geo"].setMaterialToSurface(EZCreateMesh.createMaterial(<surface materialType={EZCreateMesh.FILL} color="0xFFFF00" />), e.surface.id);
		}
		
		public function mouseOut(e:MouseEvent3D):void {
			meshes["Geo"].setMaterialToSurface(EZCreateMesh.createMaterial(<surface materialType={EZCreateMesh.FILL} color="0xFF0000" />), e.surface.id);
		}
		
		/**
		 * addMeshes
		 * @param	meshes
		 */
		private function addMehses(meshes:Array):void {
			for (var o:* in meshes) {
				scene.root.addChild(meshes[o]);
			}
		}
		
		/**
		 * 
		 * @param	targetMap
		 * @param	targetSet
		 */
		private function addSetObjects(targetMap:Map, targetSet:String):void {
			addObjects(targetMap[EZCreateMesh.KEY_MAP + EZCreateMesh.ID_CONCAT + targetSet]);
		}
		
		/**
		 * addObjects
		 * @param	objects
		 */
		private function addObjects(objects:Set):void {
			for (var o:* in objects) {
				scene.root.addChild(o);
			}
		}
		
		/**
		 * onResize
		 * @param	e
		 */
		private function onResize(e:Event):void {
			view.width = stage.stageWidth;
			view.height = stage.stageHeight;
		}
		
		/**
		 * 
		 * @param	e
		 */
		private function onEnterFrame(e:Event):void {
			cameraController.processInput();
			scene.calculate();
		}
	}
}


/**
 * EZCreateMesh
 * 
 * 簡単に より早くメッシュを作成する為に
 * 
 * Alternativa3Dを利用したマップ(Mesh)作成時に ASでは 頂点やフェイス、
 * サーフェス全てに id(名前)をつける必要があり、これらの作業は非常に面倒です
 * 
 * XMLに情報を記述することによって 
 * 値 変更時にコンパイルが不要になり、誰でも簡単にマップ作成が可能です
 * 
 * @author br
 */
import alternativ5.engine3d.core.*;
import alternativ5.engine3d.materials.*;
import alternativ5.types.*;
import alternativ5.utils.*;
import org.libspark.betweenas3.BetweenAS3;
import org.libspark.betweenas3.tweens.ITween;

import flash.geom.*;
import flash.display.*;

import alternativ5.engine3d.core.*;
import alternativ5.engine3d.materials.*;
import alternativ5.types.*;
import alternativ5.utils.*;

import flash.geom.*;
import flash.display.*;

import alternativ5.engine3d.events.*;

class EZCreateMesh {
	
	/**
	 * static const
	 */
	
	// MATERIAL
	public static const  DEV:String = "DevMaterial";
	public static const  FILL:String = "FillMaterial";
	public static const  TEXTURE:String = "TextureMaterial";
	public static const WIRE:String = "WireMaterial";
	public static const MOVIECLIP:String = "MovieClipMaterial";
	
	
	// SPEITE 3D
	public static const SPRITE_TEXTURE:String = "SpriteTextureMaterial";
	public static const SPRITE:String = "SpriteMaterial";
	
	public static const ID_CONCAT:String = ":";
	public static const ID_SURFACE:String = "s";
	public static const ID_FACE:String = "f";
	public static const ID_VERTEX:String = "v";
	
	public static const KEY_MAP:String = "m";
	
	
	
	/**
	 * 渡されたXMLに記載されているMeshを全て作成し配列で返します
	 * @param	db
	 */
	public static function createMeshes(db:XML, textures:Array = null, main:Main = null):/*Mesh*/Array {
		
		var _meshes:/*Mesh*/Array = [];
		
		for (var a:int = 0; a < db.mesh.length(); a++) {
			_meshes[db.mesh[a].@name] = new Mesh();
			
			var textureLists:XMLList = db.mesh[a].surface.(@materialType == TEXTURE).@name;
			
			_meshes[db.mesh[a].@name] = createMesh(db.mesh[a], textures, main) as Mesh;
		}
		
		return _meshes;
	}
	
	/**
	 * マップを作成
	 * @param	map
	 * @param	mapName
	 */
	public static function createMap(db:XML, _meshes:/*Mesh*/Array):Map {
		var map:Map = new Map();
		
		for (var a:int = 0; a < db.set.length(); a++) {
			
			var _set:Set;
			var elements:Array = [];
			
			for (var b:int = 0; b < db.set[a].mesh.length(); b++) {
				var mesh:Mesh = meshPropertiesSetup(
					_meshes[db.set[a].mesh[b].@name].clone(),
					db.set[a].mesh[b]
				);
				elements.push(mesh);
			}
			
			_set = Set.createFromArray(elements);
			map.add(KEY_MAP + ID_CONCAT + db.set[a].@name, _set);
		}
		
		return map;
	}
	
	/**
	 * XMLを元にプロパティを変更したメッシュを返します
	 * @param	mesh メッシュ
	 * @param	properties プロパティが記載されたXML
	 * @return mesh プロパティが変更されたメッシュを返します
	 * @exampleText XMLの例を示します
	 * <listing version="3.0">
	 * <mesh z="-100" scaleX="5" scaleY="5" />
	 * </listing>
	 */
	public static function meshPropertiesSetup(mesh:Mesh, properties:XML):Mesh {
		if (properties.hasOwnProperty("@x")) mesh.x = Number(properties.@x);
		if (properties.hasOwnProperty("@y")) mesh.y = Number(properties.@y);
		if (properties.hasOwnProperty("@z")) mesh.z = Number(properties.@z);
		if (properties.hasOwnProperty("@scaleX")) mesh.scaleX = Number(properties.@scaleX);
		if (properties.hasOwnProperty("@scaleY")) mesh.scaleY = Number(properties.@scaleY);
		if (properties.hasOwnProperty("@scaleZ")) mesh.scaleZ = Number(properties.@scaleZ);
		if (properties.hasOwnProperty("@rotationX")) mesh.rotationX = MathUtils.toRadian(Number(properties.@rotationX));
		if (properties.hasOwnProperty("@rotationY")) mesh.rotationY = MathUtils.toRadian(Number(properties.@rotationY));
		if (properties.hasOwnProperty("@rotationZ")) mesh.rotationZ = MathUtils.toRadian(Number(properties.@rotationZ));
		return mesh;
	}
	
	
	
	/**
	 * メッシュ作成
	 * @param	xml メッシュの情報が記載されたXML
	 * @return Mesh
	 */
	public static function createMesh(xml:XML, textures:/*BitmapData*/Array = null, main:Main = null):Mesh {
		
		var _mesh:Mesh = new Mesh();
		var surfaces:Array = [];
		
		for (var a:int = 0; a < xml.surface.length(); a++)
		{
			
			// eg - s:s (s:0)
			var surfaceName:String = xml.surface[a].hasOwnProperty("@id") ? xml.surface[a].@id : String(ID_SURFACE + ID_CONCAT + a);
			var materialType:String = xml.surface[a].@materialType;
			
			var faces:Array = [];
			
			for (var b:int = 0; b < xml.surface[a].face.length(); b++)
			{
				
				// eg - f:sf (f:00)
				var faceName:String = xml.surface[a].face[b].hasOwnProperty("@id") ? xml.surface[a].face[b].@id : String(ID_FACE + ID_CONCAT + a + b);
				var vertices:Array = [];
				
				for (var c:int = 0; c < xml.surface[a].face[b]..vertex.length(); c++)
				{
					
					// eg - v:sfv (v:000)
					var vertexName:String = xml.surface[a].face[b]..vertex[c].hasOwnProperty("@id") ? xml.surface[a].face[b]..vertex[c].@id : String(ID_VERTEX + ID_CONCAT + a + b + c);
					
					/**
					 * createVertex
					 */
					_mesh.createVertex(
						xml.surface[a].face[b]..vertex[c].@x,
						xml.surface[a].face[b]..vertex[c].@y,
						xml.surface[a].face[b]..vertex[c].@z,
						vertexName
					);
					vertices.push(vertexName);
				}
				
				/**
				 * createFace
				 */
				_mesh.createFace(vertices, faceName);
				
				with ( _mesh.getFaceById( faceName ) )
				{
					if ( xml.surface[a].face[b].hasOwnProperty("@click") ) 
						addEventListener(MouseEvent3D.CLICK , main[xml.surface[a].face[b].@click]);
					
					if ( xml.surface[a].face[b].hasOwnProperty("@mouseDown") )
						addEventListener(MouseEvent3D.MOUSE_DOWN, main[xml.surface[a].face[b].@mouseDown]);
					
					if ( xml.surface[a].face[b].hasOwnProperty("@mouseMove") )
						addEventListener(MouseEvent3D.MOUSE_MOVE , main[xml.surface[a].face[b].@mouseMove]);
					
					if ( xml.surface[a].face[b].hasOwnProperty("@mouseOut") )
						addEventListener(MouseEvent3D.MOUSE_OUT, main[xml.surface[a].face[b].@mouseOut]);
					
					if ( xml.surface[a].face[b].hasOwnProperty("@mouseOver") )
						addEventListener(MouseEvent3D.MOUSE_OVER, main[xml.surface[a].face[b].@mouseOver]);
					
					if ( xml.surface[a].face[b].hasOwnProperty("@mouseUp") )
						addEventListener(MouseEvent3D.MOUSE_UP , main[xml.surface[a].face[b].@mouseUp]);
					
					if ( xml.surface[a].face[b].hasOwnProperty("@mouseWheel") )
						addEventListener(MouseEvent3D.MOUSE_WHEEL, main[xml.surface[a].face[b].@mouseWheel]);
				}
				
				
				/**
				 * setUVsToFace
				 */
				if ("UVs" in xml.surface[a].face[b]) {
					with ( xml.surface[a].face[b].UVs ) {
						_mesh.setUVsToFace(
							new Point(aUV.@x, aUV.@y),
							new Point(bUV.@x, bUV.@y),
							new Point(cUV.@x, cUV.@y),
							faceName
						);
					}
				}
				
				/**
				 * push Faces
				 */
				faces.push(faceName);
			}
			
			/**
			 * createSurface
			 */
			_mesh.createSurface(faces, surfaceName);
			
			with ( _mesh.getSurfaceById(surfaceName) )
			{
				if ( xml.surface[a].hasOwnProperty("@click") )
					addEventListener(MouseEvent3D.CLICK , main[xml.surface[a].@click]);
				
				if ( xml.surface[a].hasOwnProperty("@mouseDown") )
					addEventListener(MouseEvent3D.MOUSE_DOWN, main[xml.surface[a].@mouseDown]);
				
				if ( xml.surface[a].hasOwnProperty("@mouseMove") )
					addEventListener(MouseEvent3D.MOUSE_MOVE , main[xml.surface[a].@mouseMove]);
				
				if ( xml.surface[a].hasOwnProperty("@mouseOut") )
					addEventListener(MouseEvent3D.MOUSE_OUT, main[xml.surface[a].@mouseOut]);
				
				if ( xml.surface[a].hasOwnProperty("@mouseOver") )
					addEventListener(MouseEvent3D.MOUSE_OVER, main[xml.surface[a].@mouseOver]);
				
				if ( xml.surface[a].hasOwnProperty("@mouseUp") )
					addEventListener(MouseEvent3D.MOUSE_UP , main[xml.surface[a].@mouseUp]);
				
				if ( xml.surface[a].hasOwnProperty("@mouseWheel") )
					addEventListener(MouseEvent3D.MOUSE_WHEEL, main[xml.surface[a].@mouseWheel]);
			}
			
			
			/**
			 * push
			 */
			surfaces.push(surfaceName);
			
			/**
			 * setMaterialToSurface
			 */
			_mesh.setMaterialToSurface(
				createMaterial(
					xml.surface[a],
					( materialType == TEXTURE || materialType == MOVIECLIP ) ? textures[xml.surface[a].@name] : null),
				surfaceName
			);
		}
		
		return _mesh;
	}
	
	/**
	 * マテリアルを作成
	 * @param	material マテリアル情報が記載されたXML
	 * @return SurfaceMaterial
	 * @exampleText XMLの例を示します
	 * <listing version="3.0">
	 * <surface materialType={EZCreateMesh.FILL} color="0x99ab4e" wirethickness="1" wirecolor="0x7b8d42" />
	 * <surface materialType="TextureMaterial" repeat="true" smooth="true" src="image" precision="BEST" />
	 * </listing>
	 */
	public static function createMaterial(material:XML, texture:* = null):SurfaceMaterial {
		with ( material ) {
			switch ( @materialType.toString() ) {
				
				case DEV : 
					return new DevMaterial(
						hasOwnProperty("@parametertype") ? @parametertype : 0,
						hasOwnProperty("@color") ? @color: 0xffffff * Math.random(),
						hasOwnProperty("@maxparametervalue") ? @maxparametervalue : 20,
						hasOwnProperty("@shownormals") ? @shownormals : false,
						hasOwnProperty("@normalscolor") ? @normalscolor : 0x00FFFF,
						hasOwnProperty("@minmobility") ? @minmobility : 0,
						hasOwnProperty("@maxmobility") ? @maxmobility : 255,
						hasOwnProperty("@alpha") ? @alpha : 1,
						hasOwnProperty("@blendmode") ? @blendmode : BlendMode.NORMAL,
						hasOwnProperty("@wirethickness") ? @wirethickness : -1,
						hasOwnProperty("@wirecolor") ? @wirecolor : 0
					);
				break;
				
				case FILL : 
					return new FillMaterial(
						hasOwnProperty("@color") ? @color: 0xffffff * Math.random(),
						hasOwnProperty("@alpha") ? @alpha : 1,
						hasOwnProperty("@blendmode") ? @blendmode : BlendMode.NORMAL,
						hasOwnProperty("@wirethickness") ? @wirethickness : -1,
						hasOwnProperty("@wirecolor") ? @wirecolor : 0
					);
				break;
				
				case TEXTURE : 
					return new TextureMaterial(
					new Texture(texture),
					hasOwnProperty("@alpha") ? @alpha : 1,
					hasOwnProperty("@repeat") ? (@repeat == "true" ? true : false) : true,
					hasOwnProperty("@smooth") ? (@smooth == "true" ? true : false) : false,
					hasOwnProperty("@blendmode") ? @blendmode : BlendMode.NORMAL,
					hasOwnProperty("@wirethickness") ? @wirethickness : -1,
					hasOwnProperty("@wirecolor") ? @wirecolor : 0,
					hasOwnProperty("@precision") ? @precision : 10
					);
				break;
				
				case WIRE : 
					return new WireMaterial(
						hasOwnProperty("@thickness") ? @thickness : 0,
						hasOwnProperty("@color") ? @color : 0xffffff * Math.random(),
						hasOwnProperty("@alpha") ? @alpha : 1,
						hasOwnProperty("@blendmode") ? @blendmode : BlendMode.NORMAL
					);
				break;
				
				case MOVIECLIP : 
					return new MovieClipMaterial(
						MovieClip( texture ),
						@textureWidth,
						@textureHeight,
						new Rectangle(
							hasOwnProperty("@clipX") ? @clipX : 0,
							hasOwnProperty("@clipY") ? @clipY : 0,
							hasOwnProperty("@clipWidth") ? @clipWidth : 0,
							hasOwnProperty("@clipHeight") ? @clipHeight : 0
						),
						new Matrix(
							hasOwnProperty("@a") ? @a : 1,
							hasOwnProperty("@b") ? @b : 0,
							hasOwnProperty("@c") ? @c : 0,
							hasOwnProperty("@d") ? @d : 1,
							hasOwnProperty("@tx") ? @tx : 0,
							hasOwnProperty("@ty") ? @ty : 0
						),
						hasOwnProperty("@smooth") ? (@smooth == "true" ? true : false) : false,
						hasOwnProperty("@precision") ? @precision : 10,
						hasOwnProperty("@fillColor") ? @fillColor : 0xffffff * Math.random(),
						hasOwnProperty("@refreshRate") ? @refreshRate : 1
					);
				break;
				
				default : 
					return null;
				break;
			}
		}
	}
}