UNPKG

layaair-js

Version:

An open source HTML5 engine. Edition JavaScript.

1,768 lines (1,674 loc) 128 kB
(function(window,document,Laya){ var __un=Laya.un,__uns=Laya.uns,__static=Laya.static,__class=Laya.class,__getset=Laya.getset,__newvec=Laya.__newvec; var Browser=laya.utils.Browser,Byte=laya.utils.Byte,Event=laya.events.Event,EventDispatcher=laya.events.EventDispatcher; var Graphics=laya.display.Graphics,Handler=laya.utils.Handler,Loader=laya.net.Loader,MathUtil=laya.maths.MathUtil; var Matrix=laya.maths.Matrix,Render=laya.renders.Render,RunDriver=laya.utils.RunDriver,Sprite=laya.display.Sprite; var Stat=laya.utils.Stat,Texture=laya.resource.Texture,URL=laya.net.URL; /** *@private */ //class laya.ani.AnimationState var AnimationState=(function(){ function AnimationState(){} __class(AnimationState,'laya.ani.AnimationState'); AnimationState.stopped=0; AnimationState.paused=1; AnimationState.playing=2; return AnimationState; })() /** *@private */ //class laya.ani.bone.Bone var Bone=(function(){ function Bone(){ this.name=null; this.root=null; this.parentBone=null; this.length=10; this.transform=null; this.inheritScale=true; this.inheritRotation=true; this.rotation=NaN; this.resultRotation=NaN; this._tempMatrix=null; this._sprite=null; this.resultTransform=new Transform(); this.resultMatrix=new Matrix(); this._children=[]; } __class(Bone,'laya.ani.bone.Bone'); var __proto=Bone.prototype; __proto.setTempMatrix=function(matrix){ this._tempMatrix=matrix; var i=0,n=0; var tBone; for (i=0,n=this._children.length;i < n;i++){ tBone=this._children[i]; tBone.setTempMatrix(this._tempMatrix); } } __proto.update=function(pMatrix){ this.rotation=this.transform.skX; var tResultMatrix; if (pMatrix){ tResultMatrix=this.resultTransform.getMatrix(); Matrix.mul(tResultMatrix,pMatrix,this.resultMatrix); this.resultRotation=this.rotation; }else { this.resultRotation=this.rotation+this.parentBone.resultRotation; if (this.parentBone){ if (this.inheritRotation && this.inheritScale){ tResultMatrix=this.resultTransform.getMatrix(); Matrix.mul(tResultMatrix,this.parentBone.resultMatrix,this.resultMatrix); }else { var temp=0; var parent=this.parentBone; var tAngle=NaN; var cos=NaN; var sin=NaN; var tParentMatrix=this.parentBone.resultMatrix; var worldX=tParentMatrix.a *this.transform.x+tParentMatrix.c *this.transform.y+tParentMatrix.tx; var worldY=tParentMatrix.b *this.transform.x+tParentMatrix.d *this.transform.y+tParentMatrix.ty; var tTestMatrix=new Matrix(); if (this.inheritRotation){ tAngle=Math.atan2(parent.resultMatrix.b,parent.resultMatrix.a); cos=Math.cos(tAngle),sin=Math.sin(tAngle); tTestMatrix.setTo(cos,sin,-sin,cos,0,0); Matrix.mul(this._tempMatrix,tTestMatrix,Matrix.TEMP); Matrix.TEMP.copyTo(tTestMatrix); tResultMatrix=this.resultTransform.getMatrix(); Matrix.mul(tResultMatrix,tTestMatrix,this.resultMatrix); this.resultMatrix.tx=worldX; this.resultMatrix.ty=worldY; }else if (this.inheritScale){ tResultMatrix=this.resultTransform.getMatrix(); Matrix.TEMP.identity(); Matrix.TEMP.d=-1; Matrix.mul(tResultMatrix,Matrix.TEMP,this.resultMatrix); this.resultMatrix.tx=worldX; this.resultMatrix.ty=worldY; }else { tResultMatrix=this.resultTransform.getMatrix(); Matrix.TEMP.identity(); Matrix.TEMP.d=-1; Matrix.mul(tResultMatrix,Matrix.TEMP,this.resultMatrix); this.resultMatrix.tx=worldX; this.resultMatrix.ty=worldY; } } }else { tResultMatrix=this.resultTransform.getMatrix(); tResultMatrix.copyTo(this.resultMatrix); } }; var i=0,n=0; var tBone; for (i=0,n=this._children.length;i < n;i++){ tBone=this._children[i]; tBone.update(); } } __proto.updateChild=function(){ var i=0,n=0; var tBone; for (i=0,n=this._children.length;i < n;i++){ tBone=this._children[i]; tBone.update(); } } __proto.updateDraw=function(x,y){ if (this._sprite){ this._sprite.x=x+this.resultMatrix.tx; this._sprite.y=y+this.resultMatrix.ty; }else { this._sprite=new Sprite(); this._sprite.graphics.drawCircle(0,0,5,"#ff0000"); this._sprite.graphics.fillText(this.name,0,0,"20px Arial","#00ff00","center"); Laya.stage.addChild(this._sprite); this._sprite.x=x+this.resultMatrix.tx; this._sprite.y=y+this.resultMatrix.ty; }; var i=0,n=0; var tBone; for (i=0,n=this._children.length;i < n;i++){ tBone=this._children[i]; tBone.updateDraw(x,y); } } __proto.addChild=function(bone){ this._children.push(bone); bone.parentBone=this; } __proto.findBone=function(boneName){ if (this.name==boneName){ return this; }else { var i=0,n=0; var tBone; var tResult; for (i=0,n=this._children.length;i < n;i++){ tBone=this._children[i]; tResult=tBone.findBone(boneName); if (tResult){ return tResult; } } } return null; } __proto.localToWorld=function(local){ var localX=local[0]; var localY=local[1]; local[0]=localX *this.resultMatrix.a+localY *this.resultMatrix.c+this.resultMatrix.tx; local[1]=localX *this.resultMatrix.b+localY *this.resultMatrix.d+this.resultMatrix.ty; } return Bone; })() /** *@private */ //class laya.ani.bone.BoneSlot var BoneSlot=(function(){ function BoneSlot(){ this.name=null; this.parent=null; this.attachmentName=null; this.srcDisplayIndex=-1; this.type="src"; this.templet=null; this.currSlotData=null; this.currTexture=null; this.currDisplayData=null; this.displayIndex=-1; this._diyTexture=null; this._parentMatrix=null; this._resultMatrix=null; this._skinSprite=null; this.deformData=null; } __class(BoneSlot,'laya.ani.bone.BoneSlot'); var __proto=BoneSlot.prototype; /** *设置要显示的插槽数据 *@param slotData *@param disIndex */ __proto.showSlotData=function(slotData){ this.currSlotData=slotData; this.displayIndex=this.srcDisplayIndex; this.currDisplayData=null; this.currTexture=null; } /** *通过名字显示指定对象 *@param name */ __proto.showDisplayByName=function(name){ if (this.currSlotData){ this.showDisplayByIndex(this.currSlotData.getDisplayByName(name)); } } /** *指定显示对象 *@param index */ __proto.showDisplayByIndex=function(index){ if (this.currSlotData && index >-1 && index < this.currSlotData.displayArr.length){ this.displayIndex=index; this.currDisplayData=this.currSlotData.displayArr[index]; if (this.currDisplayData){ var tName=this.currDisplayData.name; this.currTexture=this.templet.getTexture(tName); if (this.currTexture && Render.isWebGL && this.currDisplayData.type==0 && this.currDisplayData.uvs){ this.currTexture=this.currDisplayData.createTexture(this.currTexture); } } }else { this.displayIndex=-1; this.currDisplayData=null; this.currTexture=null; } } /** *替换皮肤 *@param _texture */ __proto.replaceSkin=function(_texture){ this._diyTexture=_texture; } /** *保存父矩阵的索引 *@param parentMatrix */ __proto.setParentMatrix=function(parentMatrix){ this._parentMatrix=parentMatrix; } /** *把纹理画到Graphics上 *@param graphics *@param noUseSave */ __proto.draw=function(graphics,boneMatrixArray,noUseSave,alpha){ (noUseSave===void 0)&& (noUseSave=false); (alpha===void 0)&& (alpha=1); if ((this._diyTexture==null && this.currTexture==null)|| this.currDisplayData==null){ if (!(this.currDisplayData && this.currDisplayData.type==3)){ return; } }; var tTexture=this.currTexture; if (this._diyTexture)tTexture=this._diyTexture; var tSkinSprite; switch (this.currDisplayData.type){ case 0: if (graphics){ var tCurrentMatrix=this.getDisplayMatrix(); if (this._parentMatrix){ var tRotateKey=false; if (tCurrentMatrix){ Matrix.mul(tCurrentMatrix,this._parentMatrix,Matrix.TEMP); var tResultMatrix; if (noUseSave){ if (this._resultMatrix==null)this._resultMatrix=new Matrix(); tResultMatrix=this._resultMatrix; }else { tResultMatrix=new Matrix(); } if ((!Render.isWebGL && this.currDisplayData.uvs)|| (Render.isWebGL && this._diyTexture)){ var tTestMatrix=new Matrix(1,0,0,1); if (this.currDisplayData.uvs[1] > this.currDisplayData.uvs[5]){ tTestMatrix.d=-1; } if (this.currDisplayData.uvs[0] > this.currDisplayData.uvs[4] && this.currDisplayData.uvs[1] > this.currDisplayData.uvs[5]){ tRotateKey=true; tTestMatrix.rotate(-Math.PI/2); } Matrix.mul(tTestMatrix,Matrix.TEMP,tResultMatrix); }else { Matrix.TEMP.copyTo(tResultMatrix); } if (tRotateKey){ graphics.drawTexture(tTexture,-this.currDisplayData.height / 2,-this.currDisplayData.width / 2,this.currDisplayData.height,this.currDisplayData.width,tResultMatrix); }else { graphics.drawTexture(tTexture,-this.currDisplayData.width / 2,-this.currDisplayData.height / 2,this.currDisplayData.width,this.currDisplayData.height,tResultMatrix); } } } } break ; case 1: if (noUseSave){ if (this._skinSprite==null){ this._skinSprite=RunDriver.skinAniSprite(); } tSkinSprite=this._skinSprite; }else { tSkinSprite=RunDriver.skinAniSprite(); } if (tSkinSprite==null){ return; }; var tVBArray=[]; var tIBArray=[]; var tRed=1; var tGreed=1; var tBlue=1; var tAlpha=1; if (this.currDisplayData.bones==null){ var tVertices=this.currDisplayData.weights; if (this.deformData){ tVertices=this.deformData; } for (var i=0,ii=0;i < tVertices.length && ii< this.currDisplayData.uvs.length;){ var tX=tVertices[i++]; var tY=tVertices[i++]; tVBArray.push(tX,tY,this.currDisplayData.uvs[ii++],this.currDisplayData.uvs[ii++],tRed,tGreed,tBlue,tAlpha); }; var tTriangleNum=this.currDisplayData.triangles.length / 3; for (i=0;i < tTriangleNum;i++){ tIBArray.push(this.currDisplayData.triangles[i *3]); tIBArray.push(this.currDisplayData.triangles[i *3+1]); tIBArray.push(this.currDisplayData.triangles[i *3+2]); } tSkinSprite.init(this.currTexture,tVBArray,tIBArray); var tCurrentMatrix2=this.getDisplayMatrix(); if (this._parentMatrix){ if (tCurrentMatrix2){ Matrix.mul(tCurrentMatrix2,this._parentMatrix,Matrix.TEMP); var tResultMatrix2; if (noUseSave){ if (this._resultMatrix==null)this._resultMatrix=new Matrix(); tResultMatrix2=this._resultMatrix; }else { tResultMatrix2=new Matrix(); } Matrix.TEMP.copyTo(tResultMatrix2); tSkinSprite.transform=tResultMatrix2; } } }else { this.skinMesh(boneMatrixArray,tSkinSprite,alpha); } graphics.drawSkin(tSkinSprite); break ; case 2: if (noUseSave){ if (this._skinSprite==null){ this._skinSprite=RunDriver.skinAniSprite(); } tSkinSprite=this._skinSprite; }else { tSkinSprite=RunDriver.skinAniSprite(); } if (tSkinSprite==null){ return; } this.skinMesh(boneMatrixArray,tSkinSprite,alpha); graphics.drawSkin(tSkinSprite); break ; case 3: break ; } } /** *显示蒙皮动画 *@param boneMatrixArray 当前帧的骨骼矩阵 */ __proto.skinMesh=function(boneMatrixArray,skinSprite,alpha){ var tBones=this.currDisplayData.bones; var tUvs=this.currDisplayData.uvs; var tWeights=this.currDisplayData.weights; var tTriangles=this.currDisplayData.triangles; var tVBArray=[]; var tIBArray=[]; var tRx=0; var tRy=0; var nn=0; var tMatrix; var tX=NaN; var tY=NaN; var tB=0; var tWeight=0; var tVertices=[]; var i=0,j=0,n=0; var tRed=1; var tGreed=1; var tBlue=1; var tAlpha=alpha; if (this.deformData && this.deformData.length > 0){ var f=0; for (i=0,n=tBones.length;i < n;){ nn=tBones[i++]+i; tRx=0,tRy=0; for (;i < nn;i++){ tMatrix=boneMatrixArray[tBones[i]] tX=tWeights[tB]+this.deformData[f++]; tY=tWeights[tB+1]+this.deformData[f++]; tWeight=tWeights[tB+2]; tRx+=(tX *tMatrix.a+tY *tMatrix.c+tMatrix.tx)*tWeight; tRy+=(tX *tMatrix.b+tY *tMatrix.d+tMatrix.ty)*tWeight; tB+=3; } tVertices.push(tRx,tRy); } }else { for (i=0,n=tBones.length;i < n;){ nn=tBones[i++]+i; tRx=0,tRy=0; for (;i < nn;i++){ tMatrix=boneMatrixArray[tBones[i]] tX=tWeights[tB]; tY=tWeights[tB+1]; tWeight=tWeights[tB+2]; tRx+=(tX *tMatrix.a+tY *tMatrix.c+tMatrix.tx)*tWeight; tRy+=(tX *tMatrix.b+tY *tMatrix.d+tMatrix.ty)*tWeight; tB+=3; } tVertices.push(tRx,tRy); } } for (i=0,j=0;i < tVertices.length && j < tUvs.length;){ tRx=tVertices[i++]; tRy=tVertices[i++]; tVBArray.push(tRx,tRy,tUvs[j++],tUvs[j++],tRed,tGreed,tBlue,tAlpha); } for (i=0,n=tTriangles.length;i < n;i++){ tIBArray.push(tTriangles[i]); } skinSprite.init(this.currTexture,tVBArray,tIBArray); } /** *画骨骼的起始点,方便调试 *@param graphics */ __proto.drawBonePoint=function(graphics){ if (graphics && this._parentMatrix){ graphics.drawCircle(this._parentMatrix.tx,this._parentMatrix.ty,5,"#ff0000"); } } /** *得到显示对象的矩阵 *@return */ __proto.getDisplayMatrix=function(){ if (this.currDisplayData){ return this.currDisplayData.transform.getMatrix(); } return null; } /** *得到插糟的矩阵 *@return */ __proto.getMatrix=function(){ return this._resultMatrix; } /** *用原始数据拷贝出一个 *@return */ __proto.copy=function(){ var tBoneSlot=new BoneSlot(); tBoneSlot.type="copy"; tBoneSlot.name=this.name; tBoneSlot.attachmentName=this.attachmentName; tBoneSlot.srcDisplayIndex=this.srcDisplayIndex; tBoneSlot.parent=this.parent; tBoneSlot.displayIndex=this.displayIndex; tBoneSlot.templet=this.templet; tBoneSlot.currSlotData=this.currSlotData; tBoneSlot.currTexture=this.currTexture; tBoneSlot.currDisplayData=this.currDisplayData; return tBoneSlot; } return BoneSlot; })() /** *@private */ //class laya.ani.bone.DeformAniData var DeformAniData=(function(){ function DeformAniData(){ this.deformSlotDataList=[]; } __class(DeformAniData,'laya.ani.bone.DeformAniData'); return DeformAniData; })() /** *... *@author */ //class laya.ani.bone.DeformSlotData var DeformSlotData=(function(){ function DeformSlotData(){ this.deformSlotDisplayList=[]; } __class(DeformSlotData,'laya.ani.bone.DeformSlotData'); return DeformSlotData; })() /** *@private */ //class laya.ani.bone.DeformSlotDisplayData var DeformSlotDisplayData=(function(){ function DeformSlotDisplayData(){ this.boneSlot=null; this.slotIndex=-1; this.attachment=null; this.deformData=null; this.frameIndex=0; this.timeList=[]; this.vectices=[]; } __class(DeformSlotDisplayData,'laya.ani.bone.DeformSlotDisplayData'); var __proto=DeformSlotDisplayData.prototype; __proto.binarySearch1=function(values,target){ var low=0; var high=values.length-2; if (high==0) return 1; var current=high >>> 1; while (true){ if (values[Math.floor(current+1)] <=target) low=current+1; else high=current; if (low==high) return low+1; current=(low+high)>>> 1; } return 0; } // Can't happen. __proto.apply=function(time,boneSlot,alpha){ (alpha===void 0)&& (alpha=1); if (this.timeList.length <=0){ return; }; var i=0; var n=0; var tTime=this.timeList[0]; if (time < tTime){ return; }; var tVertexCount=this.vectices[0].length; var tVertices=[]; var tFrameIndex=this.binarySearch1(this.timeList,time); this.frameIndex=tFrameIndex; if (time >=this.timeList[this.timeList.length-1]){ var lastVertices=this.vectices[this.vectices.length-1]; if (alpha < 1){ for (i=0;i < tVertexCount;i++){ tVertices[i]+=(lastVertices[i]-tVertices[i])*alpha; } }else { for (i=0;i < tVertexCount;i++){ tVertices[i]=lastVertices[i]; } } this.deformData=tVertices; return; }; var tPrevVertices=this.vectices[this.frameIndex-1]; var tNextVertices=this.vectices[this.frameIndex]; var tFrameTime=this.timeList[this.frameIndex]; var tPercent=1-(time-tFrameTime)/ (this.timeList[this.frameIndex-1]-tFrameTime); var tPrev=NaN; if (alpha < 1){ for (i=0;i < tVertexCount;i++){ tPrev=tPrevVertices[i]; tVertices[i]+=(tPrev+(tNextVertices[i]-tPrev)*tPercent-tVertices[i])*alpha; } }else { for (i=0;i < tVertexCount;i++){ tPrev=tPrevVertices[i]; tVertices[i]=tPrev+(tNextVertices[i]-tPrev)*tPercent; } } this.deformData=tVertices; } return DeformSlotDisplayData; })() /** *... *@author */ //class laya.ani.bone.DrawOrderData var DrawOrderData=(function(){ function DrawOrderData(){ this.time=NaN; this.drawOrder=[]; } __class(DrawOrderData,'laya.ani.bone.DrawOrderData'); return DrawOrderData; })() //class laya.ani.bone.EventData var EventData=(function(){ function EventData(){ this.name=null; this.intValue=0; this.floatValue=NaN; this.stringValue=null; this.time=NaN; } __class(EventData,'laya.ani.bone.EventData'); return EventData; })() /** *@private */ //class laya.ani.bone.IkConstraint var IkConstraint=(function(){ function IkConstraint(data,bones){ this._targetBone=null; this._bones=null; this._data=null; this.name=null; this.mix=NaN; this.bendDirection=NaN; this._data=data; this._targetBone=bones[data.targetBoneIndex]; if (this._bones==null)this._bones=[]; this._bones.length=0; for (var i=0,n=data.boneIndexs.length;i < n;i++){ this._bones.push(bones[data.boneIndexs[i]]); } this.name=data.name; this.mix=data.mix; this.bendDirection=data.bendDirection; } __class(IkConstraint,'laya.ani.bone.IkConstraint'); var __proto=IkConstraint.prototype; __proto.apply=function(){ switch (this._bones.length){ case 1: this._applyIk1(this._bones[0],this._targetBone.resultMatrix.tx,this._targetBone.resultMatrix.ty,this.mix); break ; case 2: this._applyIk2(this._bones[0],this._bones[1],this._targetBone.resultMatrix.tx,this._targetBone.resultMatrix.ty,this.bendDirection,this.mix); break ; } } __proto._applyIk1=function(bone,targetX,targetY,alpha){ var pp=bone.parentBone; var id=1 / (pp.resultMatrix.a *pp.resultMatrix.d-pp.resultMatrix.b *pp.resultMatrix.c); var x=targetX-pp.resultMatrix.tx; var y=targetY-pp.resultMatrix.ty; var tx=(x *pp.resultMatrix.d-y *pp.resultMatrix.c)*id-bone.transform.x; var ty=(y *pp.resultMatrix.a-x *pp.resultMatrix.b)*id-bone.transform.y; var rotationIK=Math.atan2(ty,tx)*IkConstraint.radDeg-0-bone.transform.skX; if (bone.transform.scX < 0)rotationIK+=180; if (rotationIK > 180) rotationIK-=360; else if (rotationIK <-180)rotationIK+=360; bone.transform.skX=bone.transform.skY=bone.transform.skX+rotationIK *alpha; bone.update(); } __proto._applyIk2=function(parent,child,targetX,targetY,bendDir,alpha){ if (alpha==0){ return; }; var px=parent.resultTransform.x,py=parent.resultTransform.y; var psx=parent.transform.scX,psy=parent.transform.scY; var csx=child.transform.scX; var os1=0,os2=0,s2=0; if (psx < 0){ psx=-psx; os1=180; s2=-1; }else { os1=0; s2=1; } if (psy < 0){ psy=-psy; s2=-s2; } if (csx < 0){ csx=-csx; os2=180; }else { os2=0 }; var cx=child.resultTransform.x,cy=NaN,cwx=NaN,cwy=NaN; var a=parent.resultMatrix.a,b=parent.resultMatrix.c; var c=parent.resultMatrix.b,d=parent.resultMatrix.d; var u=Math.abs(psx-psy)<=0.0001; if (!u){ cy=0; cwx=a *cx+parent.resultMatrix.tx; cwy=c *cx+parent.resultMatrix.ty; }else { cy=child.resultTransform.y; cwx=a *cx+b *cy+parent.resultMatrix.tx; cwy=c *cx+d *cy+parent.resultMatrix.ty; }; var pp=parent.parentBone; a=pp.resultMatrix.a; b=pp.resultMatrix.c; c=pp.resultMatrix.b; d=pp.resultMatrix.d; var id=1 / (a *d-b *c); var x=targetX-pp.resultMatrix.tx,y=targetY-pp.resultMatrix.ty; var tx=(x *d-y *b)*id-px; var ty=(y *a-x *c)*id-py; x=cwx-pp.resultMatrix.tx; y=cwy-pp.resultMatrix.ty; var dx=(x *d-y *b)*id-px; var dy=(y *a-x *c)*id-py; var l1=Math.sqrt(dx *dx+dy *dy); var l2=child.length *csx; var a1=NaN,a2=NaN; if (u){ l2 *=psx; var cos=(tx *tx+ty *ty-l1 *l1-l2 *l2)/ (2 *l1 *l2); if (cos <-1) cos=-1; else if (cos > 1)cos=1; a2=Math.acos(cos)*bendDir; a=l1+l2 *cos; b=l2 *Math.sin(a2); a1=Math.atan2(ty *a-tx *b,tx *a+ty *b); }else { a=psx *l2; b=psy *l2; var aa=a *a,bb=b *b,dd=tx *tx+ty *ty,ta=Math.atan2(ty,tx); c=bb *l1 *l1+aa *dd-aa *bb; var c1=-2 *bb *l1,c2=bb-aa; d=c1 *c1-4 *c2 *c; if (d > 0){ var q=Math.sqrt(d); if (c1 < 0)q=-q; q=-(c1+q)/ 2; var r0=q / c2,r1=c / q; var r=Math.abs(r0)< Math.abs(r1)? r0 :r1; if (r *r <=dd){ y=Math.sqrt(dd-r *r)*bendDir; a1=ta-Math.atan2(y,r); a2=Math.atan2(y / psy,(r-l1)/ psx); } }; var minAngle=0,minDist=Number.MAX_VALUE,minX=0,minY=0; var maxAngle=0,maxDist=0,maxX=0,maxY=0; x=l1+a; d=x *x; if (d > maxDist){ maxAngle=0; maxDist=d; maxX=x; } x=l1-a; d=x *x; if (d < minDist){ minAngle=Math.PI; minDist=d; minX=x; }; var angle=Math.acos(-a *l1 / (aa-bb)); x=a *Math.cos(angle)+l1; y=b *Math.sin(angle); d=x *x+y *y; if (d < minDist){ minAngle=angle; minDist=d; minX=x; minY=y; } if (d > maxDist){ maxAngle=angle; maxDist=d; maxX=x; maxY=y; } if (dd <=(minDist+maxDist)/ 2){ a1=ta-Math.atan2(minY *bendDir,minX); a2=minAngle *bendDir; }else { a1=ta-Math.atan2(maxY *bendDir,maxX); a2=maxAngle *bendDir; } }; var os=Math.atan2(cy,cx)*s2; var rotation=parent.resultTransform.skX; a1=(a1-os)*IkConstraint.radDeg+os1-rotation; if (a1 > 180) a1-=360; else if (a1 <-180)a1+=360; parent.resultTransform.x=px; parent.resultTransform.y=py; parent.resultTransform.skX=parent.resultTransform.skY=rotation+a1 *alpha; rotation=child.resultTransform.skX; rotation=rotation % 360; a2=((a2+os)*IkConstraint.radDeg-0)*s2+os2-rotation; if (a2 > 180) a2-=360; else if (a2 <-180)a2+=360; child.resultTransform.x=cx; child.resultTransform.y=cy; child.resultTransform.skX=child.resultTransform.skY=child.resultTransform.skY+a2 *alpha; parent.update(); } __static(IkConstraint, ['radDeg',function(){return this.radDeg=180 / Math.PI;},'degRad',function(){return this.degRad=Math.PI / 180;} ]); return IkConstraint; })() /** *@private */ //class laya.ani.bone.IkConstraintData var IkConstraintData=(function(){ function IkConstraintData(){ this.name=null; this.targetBoneName=null; this.bendDirection=1; this.mix=1; this.targetBoneIndex=-1; this.boneNames=[]; this.boneIndexs=[]; } __class(IkConstraintData,'laya.ani.bone.IkConstraintData'); return IkConstraintData; })() /** *@private *路径作用器 *1,生成根据骨骼计算控制点 *2,根据控制点生成路径,并计算路径上的节点 *3,根据节点,重新调整骨骼位置 */ //class laya.ani.bone.PathConstraint var PathConstraint=(function(){ function PathConstraint(data,bones){ this.target=null; this.data=null; this.bones=null; this.position=NaN; this.spacing=NaN; this.rotateMix=NaN; this.translateMix=NaN; this._debugKey=false; this._spaces=null; this._segments=[]; this._curves=[]; this.data=data; this.position=data.position; this.spacing=data.spacing; this.rotateMix=data.rotateMix; this.translateMix=data.translateMix; this.bones=[]; var tBoneIds=this.data.bones; for (var i=0,n=tBoneIds.length;i < n;i++){ this.bones.push(bones[tBoneIds[i]]); } } __class(PathConstraint,'laya.ani.bone.PathConstraint'); var __proto=PathConstraint.prototype; /** *计算骨骼在路径上的节点 *@param boneSlot *@param boneMatrixArray *@param graphics */ __proto.apply=function(boneList,graphics){ var tTranslateMix=this.translateMix; var tRotateMix=this.translateMix; var tTranslate=tTranslateMix > 0; var tRotate=tRotateMix > 0; var tSpacingMode=this.data.spacingMode; var tLengthSpacing=tSpacingMode=="length"; var tRotateMode=this.data.rotateMode; var tTangents=tRotateMode=="tangent"; var tScale=tRotateMode=="chainScale"; var lengths=[]; var boneCount=this.bones.length; var spacesCount=tTangents ? boneCount :boneCount+1; var spaces=[]; this._spaces=spaces; spaces[0]=this.position; var spacing=this.spacing; if (tScale || tLengthSpacing){ for (var i=0,n=spacesCount-1;i < n;){ var bone=this.bones[i]; var length=bone.length; var x=length *bone.transform.getMatrix().a; var y=length *bone.transform.getMatrix().c; length=Math.sqrt(x *x+y *y); if (tScale)lengths[i]=length; spaces[++i]=tLengthSpacing ? Math.max(0,length+spacing):spacing; } }else { for (i=1;i < spacesCount;i++){ spaces[i]=spacing; } }; var positions=this.computeWorldPositions(this.target,boneList,graphics,spacesCount,tTangents,this.data.positionMode=="percent",tSpacingMode=="percent"); if (this._debugKey){ for (i=0;i < positions.length;i++){ graphics.drawCircle(positions[i++],positions[i++],5,"#00ff00"); }; var tLinePos=[]; for (i=0;i < positions.length;i++){ tLinePos.push(positions[i++],positions[i++]); } graphics.drawLines(0,0,tLinePos,"#ff0000"); }; var skeletonX=NaN; var skeletonY=NaN; var boneX=positions[0]; var boneY=positions[1]; var offsetRotation=this.data.offsetRotation; var tip=tRotateMode=="chain" && offsetRotation==0; var p=NaN; for (i=0,p=3;i < boneCount;i++,p+=3){ bone=this.bones[i]; bone.resultMatrix.tx+=(boneX-bone.resultMatrix.tx)*tTranslateMix; bone.resultMatrix.ty+=(boneY-bone.resultMatrix.ty)*tTranslateMix; x=positions[p]; y=positions[p+1]; var dx=x-boneX,dy=y-boneY; if (tScale){ length=lengths[i]; if (length !=0){ var s=(Math.sqrt(dx *dx+dy *dy)/ length-1)*tRotateMix+1; bone.resultMatrix.a *=s; bone.resultMatrix.c *=s; } } boneX=x; boneY=y; if (tRotate){ var a=bone.resultMatrix.a; var b=bone.resultMatrix.b; var c=bone.resultMatrix.c; var d=bone.resultMatrix.d; var r=NaN; var cos=NaN; var sin=NaN; if (tTangents){ r=positions[p-1]; }else if (spaces[i+1]==0){ r=positions[p+2]; }else { r=Math.atan2(dy,dx); } r-=Math.atan2(c,a)-offsetRotation / 180 *Math.PI; if (tip){ cos=Math.cos(r); sin=Math.sin(r); length=bone.length; boneX+=(length *(cos *a-sin *c)-dx)*tRotateMix; boneY+=(length *(sin *a+cos *c)-dy)*tRotateMix; } if (r > Math.PI){ r-=(Math.PI *2); }else if (r <-Math.PI){ r+=(Math.PI *2); } r *=tRotateMix; cos=Math.cos(r); sin=Math.sin(r); bone.resultMatrix.a=cos *a-sin *c; bone.resultMatrix.c=cos *b-sin *d; bone.resultMatrix.b=sin *a+cos *c; bone.resultMatrix.d=sin *b+cos *d; } } } /** *计算顶点的世界坐标 *@param boneSlot *@param boneList *@param start *@param count *@param worldVertices *@param offset */ __proto.computeWorldVertices2=function(boneSlot,boneList,start,count,worldVertices,offset){ var tBones=boneSlot.currDisplayData.bones; var tWeights=boneSlot.currDisplayData.weights; var tTriangles=boneSlot.currDisplayData.triangles; var tMatrix; var i=0; var v=0; var skip=0; var n=0; var w=0; var b=0; var wx=0; var wy=0; var vx=0; var vy=0; for (i=0;i < start;i+=2){ n=tBones[v]; v+=n+1; skip+=n; }; var skeletonBones=boneList; for (w=offset,b=skip *3;w < count;w+=2){ wx=0,wy=0; n=tBones[v++]; n+=v; for (;v < n;v++,b+=3){ tMatrix=skeletonBones[tBones[v]].resultMatrix; vx=tWeights[b]; vy=tWeights[b+1]; var weight=tWeights[b+2]; wx+=(vx *tMatrix.a+vy *tMatrix.c+tMatrix.tx)*weight; wy+=(vx *tMatrix.b+vy *tMatrix.d+tMatrix.ty)*weight; } worldVertices[w]=wx; worldVertices[w+1]=wy; } } /** *计算路径上的节点 *@param boneSlot *@param boneList *@param graphics *@param spacesCount *@param tangents *@param percentPosition *@param percentSpacing *@return */ __proto.computeWorldPositions=function(boneSlot,boneList,graphics,spacesCount,tangents,percentPosition,percentSpacing){ var tBones=boneSlot.currDisplayData.bones; var tWeights=boneSlot.currDisplayData.weights; var tTriangles=boneSlot.currDisplayData.triangles; var tRx=0; var tRy=0; var nn=0; var tMatrix; var tX=NaN; var tY=NaN; var tB=0; var tWeight=0; var tVertices=[]; var i=0,j=0,n=0; var verticesLength=boneSlot.currDisplayData.verLen; var target=boneSlot; var position=this.position; var spaces=this._spaces; var world=[]; var out=[]; var closed=false; var curveCount=verticesLength / 6; var prevCurve=-1; var pathLength=NaN; var o=0,curve=0; var p=NaN; var space=NaN; var prev=NaN; var length=NaN; if (!true){ var lengths=boneSlot.currDisplayData.lengths; curveCount-=closed ? 1 :2; pathLength=lengths[curveCount]; if (percentPosition)position *=pathLength; if (percentSpacing){ for (i=0;i < spacesCount;i++) spaces[i] *=pathLength; } world.length=8; for (i=0,o=0,curve=0;i < spacesCount;i++,o+=3){ space=spaces[i]; position+=space; p=position; if (closed){ p %=pathLength; if (p < 0)p+=pathLength; curve=0; }else if (p < 0){ if (prevCurve !=PathConstraint.BEFORE){ prevCurve=PathConstraint.BEFORE; this.computeWorldVertices2(target,boneList,2,4,world,0); } this.addBeforePosition(p,world,0,out,o); continue ; }else if (p > pathLength){ if (prevCurve !=PathConstraint.AFTER){ prevCurve=PathConstraint.AFTER; this.computeWorldVertices2(target,boneList,verticesLength-6,4,world,0); } this.addAfterPosition(p-pathLength,world,0,out,o); continue ; } for (;;curve++){ length=lengths[curve]; if (p > length)continue ; if (curve==0) p /=length; else { prev=lengths[curve-1]; p=(p-prev)/ (length-prev); } break ; } if (curve !=prevCurve){ prevCurve=curve; if (closed && curve==curveCount){ this.computeWorldVertices2(target,boneList,verticesLength-4,4,world,0); this.computeWorldVertices2(target,boneList,0,4,world,4); }else this.computeWorldVertices2(target,boneList,curve *6+2,8,world,0); } this.addCurvePosition(p,world[0],world[1],world[2],world[3],world[4],world[5],world[6],world[7],out,o,tangents || (i > 0 && space==0)); } return out; } if (closed){ verticesLength+=2; world[verticesLength-2]=world[0]; world[verticesLength-1]=world[1]; }else { curveCount--; verticesLength-=4; this.computeWorldVertices2(boneSlot,boneList,2,verticesLength,tVertices,0); if (this._debugKey){ for (i=0;i < tVertices.length;){ graphics.drawCircle(tVertices[i++],tVertices[i++],10,"#ff0000"); } } world=tVertices; } this._curves.length=curveCount; var curves=this._curves; pathLength=0; var x1=world[0],y1=world[1],cx1=0,cy1=0,cx2=0,cy2=0,x2=0,y2=0; var tmpx=NaN,tmpy=NaN,dddfx=NaN,dddfy=NaN,ddfx=NaN,ddfy=NaN,dfx=NaN,dfy=NaN; var w=0; for (i=0,w=2;i < curveCount;i++,w+=6){ cx1=world[w]; cy1=world[w+1]; cx2=world[w+2]; cy2=world[w+3]; x2=world[w+4]; y2=world[w+5]; tmpx=(x1-cx1 *2+cx2)*0.1875; tmpy=(y1-cy1 *2+cy2)*0.1875; dddfx=((cx1-cx2)*3-x1+x2)*0.09375; dddfy=((cy1-cy2)*3-y1+y2)*0.09375; ddfx=tmpx *2+dddfx; ddfy=tmpy *2+dddfy; dfx=(cx1-x1)*0.75+tmpx+dddfx *0.16666667; dfy=(cy1-y1)*0.75+tmpy+dddfy *0.16666667; pathLength+=Math.sqrt(dfx *dfx+dfy *dfy); dfx+=ddfx; dfy+=ddfy; ddfx+=dddfx; ddfy+=dddfy; pathLength+=Math.sqrt(dfx *dfx+dfy *dfy); dfx+=ddfx; dfy+=ddfy; pathLength+=Math.sqrt(dfx *dfx+dfy *dfy); dfx+=ddfx+dddfx; dfy+=ddfy+dddfy; pathLength+=Math.sqrt(dfx *dfx+dfy *dfy); curves[i]=pathLength; x1=x2; y1=y2; } if (percentPosition)position *=pathLength; if (percentSpacing){ for (i=0;i < spacesCount;i++) spaces[i] *=pathLength; }; var segments=this._segments; var curveLength=0; var segment=0; for (i=0,o=0,curve=0,segment=0;i < spacesCount;i++,o+=3){ space=spaces[i]; position+=space; p=position; if (closed){ p %=pathLength; if (p < 0)p+=pathLength; curve=0; }else if (p < 0){ this.addBeforePosition(p,world,0,out,o); continue ; }else if (p > pathLength){ this.addAfterPosition(p-pathLength,world,verticesLength-4,out,o); continue ; } for (;;curve++){ length=curves[curve]; if (p > length)continue ; if (curve==0) p /=length; else { prev=curves[curve-1]; p=(p-prev)/ (length-prev); } break ; } if (curve !=prevCurve){ prevCurve=curve; var ii=curve *6; x1=world[ii]; y1=world[ii+1]; cx1=world[ii+2]; cy1=world[ii+3]; cx2=world[ii+4]; cy2=world[ii+5]; x2=world[ii+6]; y2=world[ii+7]; tmpx=(x1-cx1 *2+cx2)*0.03; tmpy=(y1-cy1 *2+cy2)*0.03; dddfx=((cx1-cx2)*3-x1+x2)*0.006; dddfy=((cy1-cy2)*3-y1+y2)*0.006; ddfx=tmpx *2+dddfx; ddfy=tmpy *2+dddfy; dfx=(cx1-x1)*0.3+tmpx+dddfx *0.16666667; dfy=(cy1-y1)*0.3+tmpy+dddfy *0.16666667; curveLength=Math.sqrt(dfx *dfx+dfy *dfy); segments[0]=curveLength; for (ii=1;ii < 8;ii++){ dfx+=ddfx; dfy+=ddfy; ddfx+=dddfx; ddfy+=dddfy; curveLength+=Math.sqrt(dfx *dfx+dfy *dfy); segments[ii]=curveLength; } dfx+=ddfx; dfy+=ddfy; curveLength+=Math.sqrt(dfx *dfx+dfy *dfy); segments[8]=curveLength; dfx+=ddfx+dddfx; dfy+=ddfy+dddfy; curveLength+=Math.sqrt(dfx *dfx+dfy *dfy); segments[9]=curveLength; segment=0; } p *=curveLength; for (;;segment++){ length=segments[segment]; if (p > length)continue ; if (segment==0) p /=length; else { prev=segments[segment-1]; p=segment+(p-prev)/ (length-prev); } break ; } this.addCurvePosition(p *0.1,x1,y1,cx1,cy1,cx2,cy2,x2,y2,out,o,tangents || (i > 0 && space==0)); } return out; } __proto.addBeforePosition=function(p,temp,i,out,o){ var x1=temp[i],y1=temp[i+1],dx=temp[i+2]-x1,dy=temp[i+3]-y1,r=Math.atan2(dy,dx); out[o]=x1+p *Math.cos(r); out[o+1]=y1+p *Math.sin(r); out[o+2]=r; } __proto.addAfterPosition=function(p,temp,i,out,o){ var x1=temp[i+2],y1=temp[i+3],dx=x1-temp[i],dy=y1-temp[i+1],r=Math.atan2(dy,dx); out[o]=x1+p *Math.cos(r); out[o+1]=y1+p *Math.sin(r); out[o+2]=r; } __proto.addCurvePosition=function(p,x1,y1,cx1,cy1,cx2,cy2,x2,y2,out,o,tangents){ if (p==0)p=0.0001; var tt=p *p,ttt=tt *p,u=1-p,uu=u *u,uuu=uu *u; var ut=u *p,ut3=ut *3,uut3=u *ut3,utt3=ut3 *p; var x=x1 *uuu+cx1 *uut3+cx2 *utt3+x2 *ttt,y=y1 *uuu+cy1 *uut3+cy2 *utt3+y2 *ttt; out[o]=x; out[o+1]=y; if (tangents){ out[o+2]=Math.atan2(y-(y1 *uu+cy1 *ut *2+cy2 *tt),x-(x1 *uu+cx1 *ut *2+cx2 *tt)); }else { out[o+2]=0; } } PathConstraint.NONE=-1; PathConstraint.BEFORE=-2; PathConstraint.AFTER=-3; return PathConstraint; })() /** *@private */ //class laya.ani.bone.PathConstraintData var PathConstraintData=(function(){ function PathConstraintData(){ this.name=null; this.target=null; this.positionMode=null; this.spacingMode=null; this.rotateMode=null; this.offsetRotation=NaN; this.position=NaN; this.spacing=NaN; this.rotateMix=NaN; this.translateMix=NaN; this.bones=[]; } __class(PathConstraintData,'laya.ani.bone.PathConstraintData'); return PathConstraintData; })() /** *@private */ //class laya.ani.bone.SkinData var SkinData=(function(){ function SkinData(){ this.name=null; this.slotArr=[]; } __class(SkinData,'laya.ani.bone.SkinData'); return SkinData; })() /** *@private */ //class laya.ani.bone.SkinSlotDisplayData var SkinSlotDisplayData=(function(){ function SkinSlotDisplayData(){ this.name=null; this.attachmentName=null; this.type=0; this.transform=null; this.width=NaN; this.height=NaN; this.texture=null; this.bones=null; this.uvs=null; this.weights=null; this.triangles=null; this.vertices=null; this.lengths=null; this.verLen=0; } __class(SkinSlotDisplayData,'laya.ani.bone.SkinSlotDisplayData'); var __proto=SkinSlotDisplayData.prototype; __proto.createTexture=function(currTexture){ if (this.texture)return this.texture; this.texture=new Texture(currTexture.bitmap,this.uvs); if (this.uvs[0] > this.uvs[4] && this.uvs[1] > this.uvs[5]){ this.texture.width=currTexture.height; this.texture.height=currTexture.width; this.texture.offsetX=-currTexture.offsetX; this.texture.offsetY=-currTexture.offsetY; this.texture.sourceWidth=currTexture.sourceHeight; this.texture.sourceHeight=currTexture.sourceWidth; }else { this.texture.width=currTexture.width; this.texture.height=currTexture.height; this.texture.offsetX=-currTexture.offsetX; this.texture.offsetY=-currTexture.offsetY; this.texture.sourceWidth=currTexture.sourceWidth; this.texture.sourceHeight=currTexture.sourceHeight; } return this.texture; } __proto.destory=function(){ if (this.texture)this.texture.destroy(); } return SkinSlotDisplayData; })() /** *@private */ //class laya.ani.bone.SlotData var SlotData=(function(){ function SlotData(){ this.name=null; this.displayArr=[]; } __class(SlotData,'laya.ani.bone.SlotData'); var __proto=SlotData.prototype; __proto.getDisplayByName=function(name){ var tDisplay; for (var i=0,n=this.displayArr.length;i < n;i++){ tDisplay=this.displayArr[i]; if (tDisplay.attachmentName==name){ return i; } } return-1; } return SlotData; })() /** *@private */ //class laya.ani.bone.TfConstraint var TfConstraint=(function(){ function TfConstraint(data,bones){ this._data=null; this._bones=null; this.target=null; this.rotateMix=NaN; this.translateMix=NaN; this.scaleMix=NaN; this.shearMix=NaN; this._temp=__newvec(2,0); this._data=data; if (this._bones==null){ this._bones=[]; } this.target=bones[data.targetIndex]; var j=0,n=0; for (j=0,n=data.boneIndexs.length;j < n;j++){ this._bones.push(bones[data.boneIndexs[j]]); } this.rotateMix=data.rotateMix; this.translateMix=data.translateMix; this.scaleMix=data.scaleMix; this.shearMix=data.shearMix; } __class(TfConstraint,'laya.ani.bone.TfConstraint'); var __proto=TfConstraint.prototype; __proto.apply=function(){ var tTfBone; var ta=this.target.resultMatrix.a,tb=this.target.resultMatrix.b,tc=this.target.resultMatrix.c,td=this.target.resultMatrix.d; for (var j=0,n=this._bones.length;j < n;j++){ tTfBone=this._bones[j]; if (this.rotateMix > 0){ var a=tTfBone.resultMatrix.a,b=tTfBone.resultMatrix.b,c=tTfBone.resultMatrix.c,d=tTfBone.resultMatrix.d; var r=Math.atan2(tc,ta)-Math.atan2(c,a)+this._data.offsetRotation *Math.PI / 180; if (r > Math.PI) r-=Math.PI *2; else if (r <-Math.PI)r+=Math.PI *2; r *=this.rotateMix; var cos=Math.cos(r),sin=Math.sin(r); tTfBone.resultMatrix.a=cos *a-sin *c; tTfBone.resultMatrix.b=cos *b-sin *d; tTfBone.resultMatrix.c=sin *a+cos *c; tTfBone.resultMatrix.d=sin *b+cos *d; } if (this.translateMix){ this._temp[0]=this._data.offsetX; this._temp[1]=this._data.offsetY; this.target.localToWorld(this._temp); tTfBone.resultMatrix.tx+=(this._temp[0]-tTfBone.resultMatrix.tx)*this.translateMix; tTfBone.resultMatrix.ty+=(this._temp[1]-tTfBone.resultMatrix.ty)*this.translateMix; tTfBone.updateChild(); } if (this.scaleMix > 0){ var bs=Math.sqrt(tTfBone.resultMatrix.a *tTfBone.resultMatrix.a+tTfBone.resultMatrix.c *tTfBone.resultMatrix.c); var ts=Math.sqrt(ta *ta+tc *tc); var s=bs > 0.00001 ? (bs+(ts-bs+this._data.offsetScaleX)*this.scaleMix)/ bs :0; tTfBone.resultMatrix.a *=s; tTfBone.resultMatrix.c *=s; bs=Math.sqrt(tTfBone.resultMatrix.b *tTfBone.resultMatrix.b+tTfBone.resultMatrix.d *tTfBone.resultMatrix.d); ts=Math.sqrt(tb *tb+td *td); s=bs > 0.00001 ? (bs+(ts-bs+this._data.offsetScaleY)*this.scaleMix)/ bs :0; tTfBone.resultMatrix.b *=s; tTfBone.resultMatrix.d *=s; } if (this.shearMix > 0){ b=tTfBone.resultMatrix.b,d=tTfBone.resultMatrix.d; var by=Math.atan2(d,b); r=Math.atan2(td,tb)-Math.atan2(tc,ta)-(by-Math.atan2(tTfBone.resultMatrix.c,tTfBone.resultMatrix.a)); if (r > Math.PI) r-=Math.PI *2; else if (r <-Math.PI)r+=Math.PI *2; r=by+(r+this._data.offsetShearY *Math.PI / 180)*this.shearMix; s=Math.sqrt(b *b+d *d); tTfBone.resultMatrix.b=Math.cos(r)*s; tTfBone.resultMatrix.d=Math.sin(r)*s; } } } return TfConstraint; })() /** *@private */ //class laya.ani.bone.TfConstraintData var TfConstraintData=(function(){ function TfConstraintData(){ this.name=null; this.targetIndex=0; this.rotateMix=NaN; this.translateMix=NaN; this.scaleMix=NaN; this.shearMix=NaN; this.offsetRotation=NaN; this.offsetX=NaN; this.offsetY=NaN; this.offsetScaleX=NaN; this.offsetScaleY=NaN; this.offsetShearY=NaN; this.boneIndexs=[]; } __class(TfConstraintData,'laya.ani.bone.TfConstraintData'); return TfConstraintData; })() /** *@private */ //class laya.ani.bone.Transform var Transform=(function(){ function Transform(){ this.skX=0; this.skY=0; this.scX=1; this.scY=1; this.x=0; this.y=0; this.mMatrix=null; } __class(Transform,'laya.ani.bone.Transform'); var __proto=Transform.prototype; __proto.initData=function(data){ if (data.x !=undefined){ this.x=data.x; } if (data.y !=undefined){ this.y=data.y; } if (data.skX !=undefined){ this.skX=data.skX; } if (data.skY !=undefined){ this.skY=data.skY; } if (data.scX !=undefined){ this.scX=data.scX; } if (data.scY !=undefined){ this.scY=data.scY; } } __proto.getMatrix=function(){ var tMatrix; if (this.mMatrix){ tMatrix=this.mMatrix; }else { tMatrix=this.mMatrix=new Matrix(); } tMatrix.a=Math.cos(this.skY); if (this.skX !=0 || this.skY !=0){ var tAngle=this.skX *Math.PI / 180; var cos=Math.cos(tAngle),sin=Math.sin(tAngle); tMatrix.setTo(this.scX *cos,this.scX *sin,this.scY *-sin,this.scY *cos,this.x,this.y); }else { tMatrix.setTo(this.scX,this.skX,this.skY,this.scY,this.x,this.y); } return tMatrix; } return Transform; })() /** *<code>AnimationPlayer</code> 类用于动画播放器。 */ //class laya.ani.AnimationPlayer extends laya.events.EventDispatcher var AnimationPlayer=(function(_super){ function AnimationPlayer(){ this._templet=null; this._currentTime=NaN; this._currentFrameTime=NaN; this._playStart=NaN; this._playEnd=NaN; this._playDuration=NaN; this._overallDuration=NaN; this._stopWhenCircleFinish=false; this._elapsedPlaybackTime=NaN; this._startUpdateLoopCount=NaN; this._currentAnimationClipIndex=0; this._currentKeyframeIndex=0; this._paused=false; this._cacheFrameRate=0; this._cacheFrameRateInterval=NaN; this._cachePlayRate=NaN; this._fullFrames=null; this.isCache=true; this.playbackRate=1.0; this.returnToZeroStopped=true; AnimationPlayer.__super.call(this); this._currentAnimationClipIndex=-1; this._currentKeyframeIndex=-1; this._currentTime=0.0; this._overallDuration=Number.MAX_VALUE; this._stopWhenCircleFinish=false; this._elapsedPlaybackTime=0; this._startUpdateLoopCount=-1; this._cachePlayRate=1.0; this.cacheFrameRate=60; } __class(AnimationPlayer,'laya.ani.AnimationPlayer',_super); var __proto=AnimationPlayer.prototype; /** *@private */ __proto._onTempletLoadedComputeFullKeyframeIndices=function(cachePlayRate,cacheFrameRate,templet){ if (this._templet===templet && this._cachePlayRate===cachePlayRate && this._cacheFrameRate===cacheFrameRate) this._computeFullKeyframeIndices(); } /** *@private */ __proto._computeFullKeyframeIndices=function(){ var anifullFrames=this._fullFrames=[]; var templet=this._templet; var cacheFrameInterval=this._cacheFrameRateInterval*this._cachePlayRate; for (var i=0,iNum=templet.getAnimationCount();i < iNum;i++){ var aniFullFrame=[]; for (var j=0,jNum=templet.getAnimation(i).nodes.length;j < jNum;j++){ var node=templet.getAnimation(i).nodes[j]; var frameCount=Math.floor(node.playTime / cacheFrameInterval); var nodeFullFrames=new Uint16Array(frameCount+1); var lastFrameIndex=-1; for (var n=0,nNum=node.keyFrame.length;n < nNum;n++){ var keyFrame=node.keyFrame[n]; var tm=keyFrame.startTime; var endTm=tm+keyFrame.duration+cacheFrameInterval; do { var frameIndex=Math.floor(tm / cacheFrameInterval+0.5); for (var k=lastFrameIndex+1;k < frameIndex;k++) nodeFullFrames[k]=n; lastFrameIndex=frameIndex; nodeFullFrames[frameIndex]=n; tm+=cacheFrameInterval; }while (tm <=endTm); } aniFullFrame.push(nodeFullFrames); } anifullFrames.push(aniFullFrame); } this.event(/*laya.events.Event.CACHEFRAMEINDEX_CHANGED*/"cacheframeindexchanged",this); } /** *@private */ __proto._calculatePlayDuration=function(){ if (this.state!==/*laya.ani.AnimationState.stopped*/0){ var oriDuration=this._templet.getAniDuration(this._currentAnimationClipIndex); (this._playEnd===0)&& (this._playEnd=oriDuration); if (Math.floor(this._playEnd)> oriDuration) throw new Error("AnimationPlayer:playEnd must less than original Duration."); this._playDuration=this._playEnd-this._playStart; } } /** *播放动画。 *@param index 动画索引。 *@param playbackRate 播放速率。 *@param duration 播放时长(0为1次,Number.MAX_VALUE为循环播放)。 *@param playStart 播放的起始时间位置。 *@param playEnd 播放的结束时间位置。(0为动画一次循环的最长结束时间位置)。 */ __proto.play=function(index,playbackRate,overallDuration,playStart,playEnd){ (index===void 0)&& (index=0); (playbackRate===void 0)&& (playbackRate=1.0); (overallDuration===void 0)&& (overallDuration=2147483647); (playStart===void 0)&& (playStart=0); (playEnd===void 0)&& (playEnd=0); if (!this._templet) throw new Error("AnimationPlayer:templet must not be null,maybe you need to set url."); if (overallDuration < 0 || playStart < 0 || playEnd < 0) throw new Error("AnimationPlayer:overallDuration,playStart and playEnd must large than zero."); if ((playEnd!==0)&& (playStart > playEnd)) throw new Error("AnimationPlayer:start must less than end."); this._currentTime=0; this._currentFrameTime=0; this._elapsedPlaybackTime=0; this.playbackRate=playb