animation.monkey2 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. Namespace mojo3d
  2. #rem monkeydoc @hidden
  3. #end
  4. Alias PositionKey:AnimationKey<Vec3f>
  5. #rem monkeydoc @hidden
  6. #end
  7. Alias RotationKey:AnimationKey<Quatf>
  8. #rem monkeydoc @hidden
  9. #end
  10. Alias ScaleKey:AnimationKey<Vec3f>
  11. Enum AnimationMode
  12. OneShot=1
  13. Looping
  14. PingPoong
  15. End
  16. #rem monkeydoc @hidden
  17. #end
  18. Class Animation
  19. Method New( name:String,channels:AnimationChannel[],duration:Float,hertz:Float,mode:AnimationMode )
  20. _name=name
  21. _channels=channels
  22. _duration=duration
  23. _hertz=hertz ?Else 24
  24. _mode=mode
  25. End
  26. #rem monkeydoc Animation name.
  27. #end
  28. Property Name:String()
  29. Return _name
  30. End
  31. #rem monkeydoc Animation channels.
  32. There is a channel for each bone in the animation's skeleton.
  33. #end
  34. Property Channels:AnimationChannel[]()
  35. Return _channels
  36. End
  37. #rem monkeydoc Duration.
  38. The duration of the animation in seconds
  39. #end
  40. Property Duration:Float()
  41. Return _duration
  42. End
  43. #rem monkeydoc Hertz.
  44. The frequency of the animation in seconds
  45. #end
  46. Property Hertz:Float()
  47. Return _hertz
  48. End
  49. #rem monkeydoc Animation mode.
  50. #end
  51. Property Mode:AnimationMode()
  52. Return _mode
  53. End
  54. Method Slice:Animation( name:String,begin:Float,term:Float,mode:AnimationMode )
  55. Local channels:=_channels.Slice( 0 )
  56. Local duration:=term-begin
  57. For Local i:=0 Until _channels.Length
  58. Local channel:=_channels[i]
  59. If Not channel Continue
  60. Local posKeys:=New Stack<PositionKey>
  61. posKeys.Add( New PositionKey( 0,channel.GetPosition( begin ) ) )
  62. For Local key:=Eachin channel.PositionKeys
  63. If key.Time>=term Exit
  64. If key.Time>begin posKeys.Add( New PositionKey( key.Time-begin,key.Value ) )
  65. Next
  66. posKeys.Add( New PositionKey( duration,channel.GetPosition( term ) ) )
  67. Local rotKeys:=New Stack<RotationKey>
  68. rotKeys.Add( New RotationKey( 0,channel.GetRotation( begin ) ) )
  69. For Local key:=Eachin channel.RotationKeys
  70. If key.Time>=term Exit
  71. If key.Time>begin rotKeys.Add( New RotationKey( key.Time-begin,key.Value ) )
  72. Next
  73. rotKeys.Add( New RotationKey( duration,channel.GetRotation( term ) ) )
  74. Local sclKeys:=New Stack<ScaleKey>
  75. sclKeys.Add( New ScaleKey( 0,channel.GetScale( begin ) ) )
  76. For Local key:=Eachin channel.ScaleKeys
  77. If key.Time>=term Exit
  78. If key.Time>begin sclKeys.Add( New ScaleKey( key.Time-begin,key.Value ) )
  79. Next
  80. sclKeys.Add( New ScaleKey( duration,channel.GetScale( term ) ) )
  81. channels[i]=New AnimationChannel( posKeys.ToArray(),rotKeys.ToArray(),sclKeys.ToArray() )
  82. Next
  83. Local animation:=New Animation( name,channels,duration,_hertz,mode )
  84. Return animation
  85. End
  86. Function Load:Animation( path:String )
  87. For Local loader:=Eachin Mojo3dLoader.Instances
  88. Local animation:=loader.LoadAnimation( path )
  89. If animation Return animation
  90. Next
  91. Return Null
  92. End
  93. Private
  94. Field _name:String
  95. Field _channels:AnimationChannel[]
  96. Field _duration:Float
  97. Field _hertz:Float
  98. Field _mode:AnimationMode
  99. End
  100. #rem monkeydoc @hidden
  101. #end
  102. Class AnimationChannel
  103. Method New( posKeys:PositionKey[],rotKeys:RotationKey[],sclKeys:ScaleKey[] )
  104. _posKeys=posKeys
  105. _rotKeys=rotKeys
  106. _sclKeys=sclKeys
  107. End
  108. Property PositionKeys:PositionKey[]()
  109. Return _posKeys
  110. End
  111. Property RotationKeys:RotationKey[]()
  112. Return _rotKeys
  113. End
  114. Property ScaleKeys:ScaleKey[]()
  115. Return _sclKeys
  116. End
  117. Method GetPosition:Vec3f( time:Float )
  118. If Not _posKeys Return New Vec3f( 0 )
  119. Return GetKey( _posKeys,time )
  120. End
  121. Method GetRotation:Quatf( time:Float )
  122. If Not _rotKeys Return New Quatf( 0,0,0,1 )
  123. Return GetKey( _rotKeys,time )
  124. End
  125. Method GetScale:Vec3f( time:Float )
  126. If Not _sclKeys Return New Vec3f( 1 )
  127. Return GetKey( _sclKeys,time )
  128. End
  129. Method GetMatrix:AffineMat4f( time:Float )
  130. Local pos:=GetPosition( time )
  131. Local rot:=GetRotation( time )
  132. Local scl:=GetScale( time )
  133. Return New AffineMat4f( Mat3f.Rotation( rot ).Scale( scl ),pos )
  134. End
  135. Private
  136. Field _posKeys:PositionKey[]
  137. Field _rotKeys:RotationKey[]
  138. Field _sclKeys:ScaleKey[]
  139. Method Blend:Vec3f( a:Vec3f,b:Vec3f,alpha:Float )
  140. Return a.Blend( b,alpha )
  141. End
  142. Method Blend:Quatf( a:Quatf,b:Quatf,alpha:Float )
  143. Return a.Slerp( b,alpha )
  144. End
  145. Method GetKey<T>:T( keys:AnimationKey<T>[],time:Float )
  146. DebugAssert( keys )
  147. Local pkey:AnimationKey<T>
  148. For Local key:=Eachin keys
  149. If time<=key.Time
  150. If pkey Return Blend( pkey.Value,key.Value,(time-pkey.Time)/(key.Time-pkey.Time) )
  151. Return key.Value
  152. Endif
  153. pkey=key
  154. End
  155. Return pkey.Value
  156. End
  157. End
  158. #rem monkeydoc @hidden
  159. #end
  160. Class AnimationKey<T>
  161. Method New( time:Float,value:T )
  162. _time=time
  163. _value=value
  164. End
  165. Property Time:Float()
  166. Return _time
  167. Setter( time:Float )
  168. _time=time
  169. End
  170. Property Value:T()
  171. Return _value
  172. Setter( value:T )
  173. _value=value
  174. End
  175. Private
  176. Field _time:float
  177. Field _value:T
  178. End