EZCreateMesh Template
package {
import alternativ5.engine3d.controllers.*;
import alternativ5.engine3d.core.*;
import alternativ5.engine3d.display.*;
import alternativ5.types.*;
import alternativ5.utils.*;
import flash.display.*;
import flash.events.*;
[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 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();
scene.root.addChild(EZCreatMesh.create(
<mesh>
<surface materialType="FillMaterial">
<face>
<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>
));
camera = new Camera3D();
camera.x = camera.y = camera.z = 500;
scene.root.addChild(camera);
view = new View();
addChild(view);
view.camera = camera;
cameraController = new CameraController(stage);
cameraController.camera = camera;
cameraController.setDefaultBindings();
cameraController.lookAt(new Point3D);
cameraController.checkCollisions = false;
cameraController.speed = 200;
cameraController.controlsEnabled = true;
FPS.init(stage);
stage.addEventListener(Event.RESIZE, onResize);
stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
onResize(null);
}
private function onResize(e:Event):void {
view.width = stage.stageWidth;
view.height = stage.stageHeight;
}
private function onEnterFrame(e:Event):void {
cameraController.processInput();
scene.calculate();
}
}
}
import alternativ5.engine3d.core.*;
import alternativ5.engine3d.materials.*;
import alternativ5.types.*;
import alternativ5.utils.*;
import flash.geom.*;
import flash.display.*;
class EZCreatMesh {
/**
* static const
*/
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";
/**
* 簡単に より早くメッシュを作成する為に
*
* Alternativa3Dを利用したマップ(Mesh)作成時に ASでは 頂点やフェイス、サーフェス全てに id(名前)をつける必要があり、これらの作業は非常に面倒です
* XMLに情報を記述することによって 値 変更時にコンパイルが不要になり、誰でも簡単にマップ作成が可能です
*
* @author br
*/
/**
* メッシュ作成
* @param xml メッシュの情報が記載されたXML
* @return Mesh
*/
public static function create(xml:XML, bmd:BitmapData = null):Mesh {
var _mesh:Mesh = new Mesh();
var surfaces:Array = new Array();
for (var a:uint = 0; a < xml.surface.length(); a++)
{
var surfaceName:String = String("s" + a);
var faces:Array = new Array();
for (var b:uint = 0; b < xml.surface[a].face.length(); b++)
{
var faceName:String = String("f" + a + b);
var vertices:Array = new Array();
for (var c:uint = 0; c < xml.surface[a].face[b].vertex.length(); c++)
{
var vertexName:String = String("v" + 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);
/**
* setUVsToFace
*/
if ("UVs" in xml.surface[a].face[b]) {
_mesh.setUVsToFace(
new Point(
Number(xml.surface[a].face[b].UVs.aUV.@x),
Number(xml.surface[a].face[b].UVs.aUV.@y)
),
new Point(
Number(xml.surface[a].face[b].UVs.bUV.@x),
Number(xml.surface[a].face[b].UVs.bUV.@y)
),
new Point(
Number(xml.surface[a].face[b].UVs.cUV.@x),
Number(xml.surface[a].face[b].UVs.cUV.@y)
),
faceName
);
}
/**
* push Faces
*/
faces.push(faceName);
}
/**
* createSurface
*/
_mesh.createSurface(faces, surfaceName);
surfaces.push(surfaceName);
_mesh.setMaterialToSurface(createMaterial(xml.surface[a]), surfaceName);
}
return _mesh;
/**
* マテリアルを作成
* @param material マテリアル情報が記載されたXML
* @return SurfaceMaterial
* @exampleText XMLの例を示します
* <listing version="3.0">
* <surface materialType="FillMaterial" color="0x99ab4e" wirethickness="1" wirecolor="0x7b8d42" />
* <surface materialType="TextureMaterial" repeat="true" smooth="true" src="image" precision="BEST" />
* </listing>
*/
function createMaterial(material:XML):SurfaceMaterial {
switch (true) {
/**
* DEV
*/
case material.@materialType == DEV :
return new DevMaterial(
uint(material.@parametertype) ? material.@parametertype : 0,
uint(material.@color) ? material.@color : 0xFFFFFF,
Number(material.@maxparametervalue) ? material.@maxparametervalue : 20,
Boolean(material.@shownormals) ? material.@shownormals : false,
uint(material.@normalscolor) ? material.@normalscolor : 0x00FFFF,
int(material.@minmobility) ? material.@minmobility : 0,
int(material.@maxmobility) ? material.@maxmobility : 255,
Number(material.@alpha) ? material.@alpha : 1,
String(material.@blendmode) ? material.@blendmode : "normal",
Number(material.@wirethickness) ? material.@wirethickness : -1,
Number(material.@wirecolor) ? material.@wirecolor : 0
);
break;
/**
* FILL
*/
case material.@materialType == FILL :
return new FillMaterial(
uint(material.@color)? material.@color: 0xffffff * Math.random(),
Number(material.@alpha)? material.@alpha : 1,
String(material.@blendmode)? material.@blendmode : "normal",
Number(material.@wirethickness)? material.@wirethickness : -1,
Number(material.@wirecolor)? material.@wirecolor : 0
);
break;
/**
* TEXTURE
*/
case material.@materialType == TEXTURE :
return new TextureMaterial(
new Texture(bmd),
uint(material.@alpha)? material.@alpha : 1,
Boolean(material.@repeat == "true")? true : true,
Boolean(material.@smooth == "true")? true : false,
String(material.@blendmode.toString())? material.@blendmode : BlendMode.NORMAL,
Number(material.@wirethickness)? material.@wirethickness : -1,
uint(material.@wirecolor)? material.@wirecolor : 0,
String(material.@precision.toString())? createTextureMaterialPrecision(material.@precision.toString()) : 10
);
break;
/**
* WIRE
*/
case material.@materialType == WIRE :
return new WireMaterial(
Number(material.@thickness) ? material.@thickness : 0,
uint(material.@color) ? material.@color : 0xffffff * Math.random(),
Number(material.@alpha) ? material.@alpha : 1,
String(material.@blendmode) ? material.@blendmode : "normal"
);
break;
default :
return null;
break;
}
}
/**
* createTextureMaterialPrecision
* @param precision
* @return
*/
function createTextureMaterialPrecision(precision:String):Number {
switch(true) {
case precision == "BEST" : return TextureMaterialPrecision.BEST; break;
case precision == "HIGH" : return TextureMaterialPrecision.HIGH; break;
case precision == "LOW" : return TextureMaterialPrecision.LOW; break;
case precision == "MEDIUM" : return TextureMaterialPrecision.MEDIUM; break;
case precision == "NONE" : return TextureMaterialPrecision.NONE; break;
case precision == "VERY_HIGH" : return TextureMaterialPrecision.VERY_HIGH; break;
case precision == "VERY_LOW" : return TextureMaterialPrecision.VERY_LOW; break;
default : return TextureMaterialPrecision.MEDIUM;
}
}
/**
* XMLを元にプロパティを変更したメッシュを返します
* @param mesh メッシュ
* @param properties プロパティが記載されたXML
* @return mesh プロパティが変更されたメッシュを返します
* @exampleText XMLの例を示します
* <listing version="3.0">
* <mesh z="-100" scaleX="5" scaleY="5" />
* </listing>
*/
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;
}
}
}