Detailed usage of away3d 4x bone controller

Posted by mandred on Fri, 04 Mar 2022 21:08:27 +0100

Finally understand away3d's SkeletonAnimator. Here, write down the specific steps of using SkeletonAnimator;
1: Skeleton animation: use 3dmax to make a short animation; And export to files through format plug-ins / scripts; The md5 export script of max is used here; After the animation is finished, select the menu bar - MAXscript - > Run script; Select the md5exporter to download MS will open a dialog box similar to the command panel; In the Frame option tab of the mesh & Animation rollout, set start frame and end frame as the starting frames of the animation; Then click the add button to select the model with skin modifier added in the scene; Under the export option tab, select the Export both radio box; Then click the Export button. First, a save dialog box will pop up, prompting you to save a document with the extension md5mesh; After saving, the save dialog box will pop up further, prompting to save another file in md5anim format; Md5mesh format file contains the vertex and uv data of the exported object, while md5anim saves the bone and frame data of the animation; It should be noted that if the exported model does not have any map, the script will report an error that the exported map is empty. At this time, you need to open the material editor and randomly assign the material of a shader to the model;
2: Loading files required for Animation: next, you need to import the generated files into the project as resources; Embed two files in the code with embed tag;

[Embed(source="box.md5mesh", mimeType="application/octet-stream")]
var box:Class;
[Embed(source="box2.md5anim", mimeType="application/octet-stream")]
var boxanime:Class;

Because it is an embedded document, you only need the new keyword to get the data; Of course, this data is only data. This data must be parsed before it can be used. Therefore, the loadData method of AssetLibrary is used;

AssetLibrary.loadData(new box(), null, null, new MD5MeshParser()); 

Next, the event handler for loading resources must be set;

AssetLibrary.addEventListener(AssetEvent.ASSET_COMPLETE, onAssetComplete);

3: Data required to generate SkeletonAnimator:
SkeletonAnimator requires SkeletonAnimationSet and Skeleton (bone definition), while SkeletonClipNode (bone fragment node) is required to generate SkeletonAnimationSet as the content of planning animation set; The following processing method allocates the resources required above;

function onAssetComplete(event:AssetEvent):void
if(event.asset.assetType==AssetType.ANIMATION_NODE){//The SkeletonClipNode object obtained from the md5anim document is loaded here
var skn:SkeletonClipNode=event.asset as SkeletonClipNode;"anime1";//You must set a name (for example, run, jump, etc.) before saving
skn.looping=true;//If the node is set to true, it is considered to play circularly. The animation played circularly does not schedule animationstateevent PLAYBACK_ Of complete events
skn.looping=false;//Set false to control the animation segment
animatorSet.addAnimation(skn);//Finally, add the node to the animation set (so that you can access specific actions according to the name you just set)
                //In order to use the function of animation end control, you also need to add an AnimationStateEvent event callback to the node; Of course, by calling the getAnimation method of the SkeletonAnimationSet object, you can also get the node, so you can set it whenever you want to join the callback; 
}else if (event.asset.assetType == AssetType.ANIMATION_SET) {
                //Here is the SkeletonAnimationSet object obtained from the md5anim file; 
animatorSet=event.asset as SkeletonAnimationSet;
        } else if (event.asset.assetType == AssetType.MESH) {
        //This is used to load model data 
              mesh = event.asset as Mesh;
AssetLibrary.loadData(new boxanime(), null,null, new MD5AnimParser());
        }else if(event.asset.assetType==AssetType.SKELETON){
//Here you get the Skeleton definition
                 skl=event.asset as Skeleton;

4: Bind SkeletonAnimator and control
After all data objects are parsed, you can start to create and use SkeletonAnimator;

function all_com(e:LoaderEvent){
if(animatorSet!=null && animatorSet.hasAnimation("anime1")){
//The reason for judging here is that the same parsing method is used in the program to process resources; 
animator=new SkeletonAnimator(animatorSet,skl);//Create a SkeletonAnimator object;
mesh.animator=animator;//Bind objects to models
stage.addEventListener(MouseEvent.CLICK,_c);//This callback method enables users to control the playback and stop of animation by clicking on the screen;
var played:Boolean=false;//Playback status
import away3d.animators.transitions.CrossfadeTransition;
var cft:CrossfadeTransition=new CrossfadeTransition(0.5); //Difference calculation unit
function _c(e:MouseEvent){
if(!played){"anime1",cft,0);//The first parameter of play is the name of SkeletonClipNode, and the second parameter is a CrossfadeTransition object, which is understood here as a functional agent containing bone difference calculation; The third parameter is the number of frames of the animation playback (here, it starts from 0. If you don't write, it will start from the current frame. However, if the looping attribute of the node is false, when the animation has been played to the last frame, the execution of play will always display the last frame)

Topics: Game Development 3d