- 最后登录
- 2022-10-8
- 注册时间
- 2010-12-6
- 阅读权限
- 100
- 积分
- 14150
![Rank: 7](static/image/common//star_level3.gif) ![Rank: 7](static/image/common//star_level2.gif) ![Rank: 7](static/image/common//star_level1.gif)
- 纳金币
- 76544
- 精华
- 23
|
本文分享的是Away3D中MD5动画加载和互动实例,全文摘录如下:全屏演示请点击3D动画效果截图进入: /* MD5 animation loading and interaction example in Away3d Demonstrates: How to load MD5 mesh and anim files with bones animation from embedded resources.How to map animation data after loading in order to playback an animation sequence.How to control the movement of a game character using keys. Code by Rob Bateman & David Lenaertsrob@infiniteturtles.co.ukhttp://www.infiniteturtles.co.ukdavid.lenaerts@gmail.comhttp://www.derschmale.com This code is distributed under the MIT License Copyright (c) Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the “Software”), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so,subject to the following conditions: The above copyright notice and this permission notice shall be included inall copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS INTHE SOFTWARE. */
package
{import away3d.animators.*;
import away3d.animators.data.*;
import away3d.animators.skeleton.*;
import away3d.cameras.*;
import away3d.containers.*;
import away3d.controllers.*;
import away3d.core.base.*;
import away3d.debug.*;
import away3d.entities.Mesh;
import away3d.entities.Sprite3D;
import away3d.events.*;
import away3d.library.*;
import away3d.library.assets.*;
import away3d.lights.*;
import away3d.lights.shadowmaps.CubeMapShadowMapper;
import away3d.loaders.*;
import away3d.loaders.parsers.*;
import away3d.materials.*;
import away3d.materials.lightpickers.StaticLightPicker;
import away3d.materials.methods.*;
import away3d.primitives.*;
import away3d.textures.*;
import flash.display.*;
import flash.events.*;
import flash.filters.*;
import flash.geom.*;
import flash.net.*;
import flash.text.*;
import flash.ui.*;
[SWF(backgroundColor="#000000", frameRate="30", quality="LOW")]
publicclass Intermediate_MD5Animation extends Sprite
{//signature swf[Embed(source="/../embeds/signature.swf", symbol="Signature")]publicvar SignatureSwf:Class;
//floor diffuse map[Embed(source="/../embeds/rockbase_diffuse.jpg")]privatevar FloorDiffuse:Class;
//floor normal map[Embed(source="/../embeds/rockbase_normals.png")]privatevar FloorNormals:Class;
//floor specular map[Embed(source="/../embeds/rockbase_specular.png")]privatevar FloorSpecular:Class;
//body diffuse map[Embed(source="/../embeds/hellknight/hellknight_diffuse.jpg")]privatevar BodyDiffuse:Class;
//body normal map[Embed(source="/../embeds/hellknight/hellknight_normals.png")]privatevar BodyNormals:Class;
//bidy specular map[Embed(source="/../embeds/hellknight/hellknight_specular.png")]privatevar BodySpecular:Class;
//skybox[Embed(source="/../embeds/skybox/grimnight_posX.png")]privatevar EnvPosX:Class;
[Embed(source="/../embeds/skybox/grimnight_posY.png")]privatevar EnvPosY:Class;
[Embed(source="/../embeds/skybox/grimnight_posZ.png")]privatevar EnvPosZ:Class;
[Embed(source="/../embeds/skybox/grimnight_negX.png")]privatevar EnvNegX:Class;
[Embed(source="/../embeds/skybox/grimnight_negY.png")]privatevar EnvNegY:Class;
[Embed(source="/../embeds/skybox/grimnight_negZ.png")]privatevar EnvNegZ:Class;
//billboard texture for red light[Embed(source="/../embeds/redlight.png")]privatevar RedLight:Class;
//billboard texture for blue light[Embed(source="/../embeds/bluelight.png")]privatevar BlueLight:Class;
//hellknight mesh[Embed(source="/../embeds/hellknight/hellknight.md5mesh", mimeType="application/octet-stream")]privatevar HellKnight_Mesh:Class;
//hellknight animations[Embed(source="/../embeds/hellknight/idle2.md5anim", mimeType="application/octet-stream")]privatevar HellKnight_Idle2:Class;
[Embed(source="/../embeds/hellknight/walk7.md5anim", mimeType="application/octet-stream")]privatevar HellKnight_Walk7:Class;
[Embed(source="/../embeds/hellknight/attack3.md5anim", mimeType="application/octet-stream")]privatevar HellKnight_Attack3:Class;
[Embed(source="/../embeds/hellknight/turret_attack.md5anim", mimeType="application/octet-stream")]privatevar HellKnight_TurretAttack:Class;
[Embed(source="/../embeds/hellknight/attack2.md5anim", mimeType="application/octet-stream")]privatevar HellKnight_Attack2:Class;
[Embed(source="/../embeds/hellknight/chest.md5anim", mimeType="application/octet-stream")]privatevar HellKnight_Chest:Class;
[Embed(source="/../embeds/hellknight/roar1.md5anim", mimeType="application/octet-stream")]privatevar HellKnight_Roar1:Class;
[Embed(source="/../embeds/hellknight/leftslash.md5anim", mimeType="application/octet-stream")]privatevar HellKnight_LeftSlash:Class;
[Embed(source="/../embeds/hellknight/headpain.md5anim", mimeType="application/octet-stream")]privatevar HellKnight_HeadPain:Class;
[Embed(source="/../embeds/hellknight/pain1.md5anim", mimeType="application/octet-stream")]privatevar HellKnight_Pain1:Class;
[Embed(source="/../embeds/hellknight/pain_luparm.md5anim", mimeType="application/octet-stream")]privatevar HellKnight_PainLUPArm:Class;
[Embed(source="/../embeds/hellknight/range_attack2.md5anim", mimeType="application/octet-stream")]privatevar HellKnight_RangeAttack2:Class;
//engine variablesprivatevar scene:Scene3D;
privatevarcamera:Camera3D;
privatevar view:View3D;
privatevar cameraController ookAtController;
privatevar awayStats:AwayStats;
//animation variablesprivatevar animation:SkeletonAnimation;
privatevar animator:SmoothSkeletonAnimator;
privatevar breatheSequence:SkeletonAnimationSequence;
privatevar walkSequence:SkeletonAnimationSequence;
privatevar***nSequence:SkeletonAnimationSequence;
privatevar isRunning:Boolean;
privatevar isMoving:Boolean;
privatevar movementDirection:Number;
privatevar onceAnim:String;
privatevar currentAnim:String;
privatevar currentRotationInc:Number = 0;
privatevar action:uint;
//animation constantsprivate const MESH_NAME:String = "hellknight";
private const IDLE_NAME:String = "idle2";
private const WALK_NAME:String = "walk7";
private const ANIM_NAMES:Array = [IDLE_NAME, WALK_NAME, "attack3", "turret_attack", "attack2", "chest", "roar1", "leftslash", "headpain", "pain1", "pain_luparm", "range_attack2"];
private const ANIM_CLASSES:Array = [HellKnight_Idle2, HellKnight_Walk7, HellKnight_Attack3, HellKnight_TurretAttack, HellKnight_Attack2, HellKnight_Chest, HellKnight_Roar1, HellKnight_LeftSlash, HellKnight_HeadPain, HellKnight_Pain1, HellKnight_PainLUPArm, HellKnight_RangeAttack2];
private const XFADE_TIME:Number = 0.5;
private const ROTATION_SPEED:Number = 3;
private const RUN_SPEED:Number = 2;
private const WALK_SPEED:Number = 1;
private const IDLE_SPEED:Number = 1;
private const ACTION_SPEED:Number = 1;
//signature variablesprivatevar Signature:Sprite;
privatevar SignatureBitmap:Bitmap;
//light objectsprivatevar redLight ointLight;
privatevar blueLight ointLight;
privatevar whiteLight irectionalLight;
privatevar lightPicker:StaticLightPicker;
privatevar filteredShadowMapMethod:TripleFilteredShadowMapMethod;
privatevar fogMethod:FogMethod;
privatevar count:Number = 0;
//material objectsprivatevar redLightMaterial:TextureMaterial;
privatevar blueLightMaterial:TextureMaterial;
privatevar groundMaterial:TextureMaterial;
privatevar bodyMaterial:TextureMaterial;
privatevar cubeTexture:BitmapCubeTexture;
//scene objectsprivatevartext:TextField;
privatevar placeHolder:Mesh;
privatevar mesh:Mesh;
privatevar ground:Mesh;
privatevar skyBox:SkyBox;
/** * Cons***ctor */publicfunction Intermediate_MD5Animation(){
init();
}
/** * Global initialise function */privatefunction init():void{
initEngine();
initText();
initLights();
initMaterials();
initObjects();
initListeners();
}
/** * Initialise the engine */privatefunction initEngine():void{stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
view = new View3D();
scene = view.scene;
camera = view.camera;
camera.lens.far = 5000;
camera.z = -200;
camera.y = 160;
//setup controller to be used on the camera
placeHolder = new Mesh();
placeHolder.y = 50;
cameraController = new LookAtController(camera, placeHolder);
view.addSourceURL("srcview/index.html");
addChild(view);
//add signature
Signature = Sprite(new SignatureSwf());
SignatureBitmap = new Bitmap(new BitmapData(Signature.width, Signature.height, ***e, 0));
stage.quality = StageQuality.HIGH;
SignatureBitmap.bitmapData.draw(Signature);
stage.quality = StageQuality.LOW;
addChild(SignatureBitmap);
awayStats = new AwayStats(view)
addChild(awayStats);
}
/** * Create an ins***ctions overlay */privatefunction initText():void{text = newTextField();
text.defaultTextFormat = newTextFormat("Verdana", 11, 0xFFFFFF);
text.width = 240;
text.height = 100;
text.selectable = false;
text.mouseEnabled = false;
text.text = "Cursor keys / WSAD - move
";
text.appendText("SHIFT - hold down to ***n
");
text.appendText("Numbers 1-9 - Attack
");
text.filters = [new DropShadowFilter(1, 45, 0x0, 1, 0, 0)];
addChild(text);
}
/** * Initialise the lights */privatefunction initLights():void{//create a light for shadows that mimics the sun's position in the skybox
redLight = new PointLight();
redLight.x = -1000;
redLight.y = 200;
redLight.z = -1400;
redLight.color = 0xff1111;
scene.addChild(redLight);
blueLight = new PointLight();
blueLight.x = 1000;
blueLight.y = 200;
blueLight.z = 1400;
blueLight.color = 0x1111ff;
scene.addChild(blueLight);
whiteLight = new DirectionalLight(-50, -20, 10);
whiteLight.color = 0xffffee;
whiteLight.castsShadows = ***e;
scene.addChild(whiteLight);
lightPicker = new StaticLightPicker([redLight, blueLight, whiteLight]);
//create a global shadow method
filteredShadowMapMethod = new TripleFilteredShadowMapMethod(whiteLight);
//create a global fog method
fogMethod = new FogMethod(0, camera.lens.far*0.5, 0x000000);
}
/** * Initialise the materials */privatefunction initMaterials():void{//red light material
redLightMaterial = new TextureMaterial(new BitmapTexture(new RedLight().bitmapData));
redLightMaterial.alphaBlending = ***e;
redLightMaterial.addMethod(fogMethod);
//blue light material
blueLightMaterial = new TextureMaterial(new BitmapTexture(new BlueLight().bitmapData));
blueLightMaterial.alphaBlending = ***e;
blueLightMaterial.addMethod(fogMethod);
//ground material
groundMaterial = new TextureMaterial(new BitmapTexture(new FloorDiffuse().bitmapData));
groundMaterial.smooth = ***e;
groundMaterial.repeat = ***e;
groundMaterial.mipmap = ***e;
groundMaterial.lightPicker = lightPicker;
groundMaterial.ambientColor = 0x202030;
groundMaterial.ambient = 1;
groundMaterial.normalMap = new BitmapTexture(new FloorNormals().bitmapData);
groundMaterial.specularMap = new BitmapTexture(new FloorSpecular().bitmapData);
groundMaterial.shadowMethod = filteredShadowMapMethod;
groundMaterial.addMethod(fogMethod);
//body material
bodyMaterial = new TextureMaterial(new BitmapTexture(new BodyDiffuse().bitmapData));
bodyMaterial.gloss = 20;
bodyMaterial.specular = 1.5;
bodyMaterial.ambientColor = 0x505060;
bodyMaterial.ambient = 1;
bodyMaterial.specularMap = new BitmapTexture(new BodySpecular().bitmapData);
bodyMaterial.normalMap = new BitmapTexture(new BodyNormals().bitmapData);
bodyMaterial.addMethod(fogMethod);
bodyMaterial.lightPicker = lightPicker;
bodyMaterial.shadowMethod = filteredShadowMapMethod;
}
/** * Initialise the scene objects */privatefunction initObjects():void{//create light billboards
redLight.addChild(new Sprite3D(redLightMaterial, 200, 200));
blueLight.addChild(new Sprite3D(blueLightMaterial, 200, 200))
//AssetLibrary.enableParser(MD5MeshParser);//AssetLibrary.enableParser(MD5AnimParser);
initMesh();
//create a snowy ground plane
ground = new Mesh(new PlaneGeometry(50000, 50000, 1, 1), groundMaterial);
ground.geometry.scaleUV(200, 200);
ground.castsShadows = false;
scene.addChild(ground);
//create a skybox
cubeTexture = new BitmapCubeTexture(new EnvPosX().bitmapData, new EnvNegX().bitmapData, new EnvPosY().bitmapData, new EnvNegY().bitmapData, new EnvPosZ().bitmapData, new EnvNegZ().bitmapData);
skyBox = new SkyBox(cubeTexture);
scene.addChild(skyBox);
}
/** * Initialise the hellknight mesh */privatefunction initMesh():void{//parse hellknight mesh
AssetLibrary.addEventListener(AssetEvent.ASSET_COMPLETE, onAssetComplete);
AssetLibrary.loadData(new HellKnight_Mesh(), null, null, new MD5MeshParser());
}
/** * Initialise the hellknight animations */privatefunction initAnimations():void{
animator = new SmoothSkeletonAnimator(mesh.animationState as SkeletonAnimationState);
for(var i:uint = 0; i < ANIM_NAMES.length; ++i)
AssetLibrary.loadData(new ANIM_CLASSES(), null, ANIM_NAMES, new MD5AnimParser());
}
/** * Initialise the listeners */privatefunction initListeners():void{
addEventListener(Event.ENTER_FRAME, onEnterFrame);
stage.addEventListener(Event.RESIZE, onResize);
onResize();
}
/** * Navigation and render loop */privatefunctiononEnterFrame(event:Event):void{//update character animationif(mesh)
mesh.rotationY += currentRotationInc;
count += 0.01;
redLight.x = Math.sin(count)*1500;
redLight.y = 250 + Math.sin(count*0.54)*200;
redLight.z = Math.cos(count*0.7)*1500;
blueLight.x = -Math.sin(count*0.8)*1500;
blueLight.y = 250 - Math.sin(count*.65)*200;
blueLight.z = -Math.cos(count*0.9)*1500;
view.render();
}
/** * Listener function for asset complete event on loader */privatefunction onAssetComplete(event:AssetEvent):void{if(event.asset.assetType == AssetType.ANIMATION){
var seq:SkeletonAnimationSequence = event.asset as SkeletonAnimationSequence;
seq.name = event.asset.assetNamespace;
animator.addSequence(seq);
if(seq.name == IDLE_NAME || seq.name == WALK_NAME){
seq.looping = ***e;
}else{
seq.looping = false;
seq.addEventListener(AnimatorEvent.SEQUENCE_DONE, onSequenceDone);
}
if(seq.name == IDLE_NAME)stop();
}elseif(event.asset.assetType == AssetType.MESH){//grab mesh object and assign our material object
mesh = event.asset as Mesh;
mesh.material = bodyMaterial;
mesh.castsShadows = ***e;
scene.addChild(mesh);
//add our lookat object to the mesh
mesh.addChild(placeHolder);
//initialise animation data
initAnimations();
//add key listenersstage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
}}
privatefunction onSequenceDone(event:AnimatorEvent):void{
onceAnim = null;
animator.play(currentAnim, XFADE_TIME);
animator.timeScale = isMoving? movementDirection*(isRunning? RUN_SPEED : WALK_SPEED) : IDLE_SPEED;
}
privatefunction playAction(val:uint):void{
onceAnim = ANIM_NAMES[val + 2];
animator.timeScale = ACTION_SPEED;
animator.play(onceAnim, XFADE_TIME);
}
/** * Key down listener for animation */privatefunctiononKeyDown(event:KeyboardEvent):void{switch(event.keyCode){case Keyboard.SHIFT:
isRunning = ***e;
if(isMoving)
updateMovement(movementDirection);
break;
case Keyboard.UP:
case Keyboard.W:
updateMovement(movementDirection = 1);
break;
case Keyboard.DOWN:
case Keyboard.S:
updateMovement(movementDirection = -1);
break;
case Keyboard.LEFT:
case Keyboard.A:
currentRotationInc = -ROTATION_SPEED;
break;
case Keyboard.RIGHT:
case Keyboard.D:
currentRotationInc = ROTATION_SPEED;
break;
case Keyboard.NUMBER_1:
playAction(1);
break;
case Keyboard.NUMBER_2:
playAction(2);
break;
case Keyboard.NUMBER_3:
playAction(3);
break;
case Keyboard.NUMBER_4:
playAction(4);
break;
case Keyboard.NUMBER_5:
playAction(5);
break;
case Keyboard.NUMBER_6:
playAction(6);
break;
case Keyboard.NUMBER_7:
playAction(7);
break;
case Keyboard.NUMBER_8:
playAction(8);
break;
case Keyboard.NUMBER_9:
playAction(9);
break;
}}
privatefunctiononKeyUp(event:KeyboardEvent):void{switch(event.keyCode){case Keyboard.SHIFT:
isRunning = false;
if(isMoving)
updateMovement(movementDirection);
break;
case Keyboard.UP:
case Keyboard.W:
case Keyboard.DOWN:
case Keyboard.S:
stop();
break;
case Keyboard.LEFT:
case Keyboard.A:
case Keyboard.RIGHT:
case Keyboard.D:
currentRotationInc = 0;
break;
}}
privatefunction updateMovement(dir:Number):void{
isMoving = ***e;
animator.timeScale = dir*(isRunning? RUN_SPEED : WALK_SPEED);
if(currentAnim == WALK_NAME)return;
currentAnim = WALK_NAME;
if(onceAnim)return;
//update animator
animator.play(currentAnim, XFADE_TIME);
}
privatefunctionstop():void{
isMoving = false;
if(currentAnim == IDLE_NAME)return;
currentAnim = IDLE_NAME;
if(onceAnim)return;
//update animator
animator.timeScale = IDLE_SPEED;
animator.play(currentAnim, XFADE_TIME);
}
/** * stage listener for resize events */privatefunctiononResize(event:Event = null):void{
view.width = stage.stageWidth;
view.height = stage.stageHeight;
SignatureBitmap.y = stage.stageHeight - Signature.height;
awayStats.x = stage.stageWidth - awayStats.width;
}}} |
|