babylon.boneLookController.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. module BABYLON {
  2. export class BoneLookController {
  3. private static _tmpVecs: Vector3[] = [Vector3.Zero(), Vector3.Zero(), Vector3.Zero(),Vector3.Zero(), Vector3.Zero(), Vector3.Zero(), Vector3.Zero(), Vector3.Zero(), Vector3.Zero(), Vector3.Zero()];
  4. private static _tmpQuat = Quaternion.Identity();
  5. private static _tmpMats: Matrix[] = [Matrix.Identity(), Matrix.Identity(), Matrix.Identity(), Matrix.Identity()];
  6. /**
  7. * The target Vector3 that the bone will look at.
  8. */
  9. public target: Vector3;
  10. /**
  11. * The mesh that the bone is attached to.
  12. */
  13. public mesh: AbstractMesh;
  14. /**
  15. * The bone that will be looking to the target.
  16. */
  17. public bone: Bone;
  18. /**
  19. * The up axis of the coordinate system that is used when the bone is rotated.
  20. */
  21. public upAxis: Vector3 = Vector3.Up();
  22. /**
  23. * The space that the up axis is in - BABYLON.Space.BONE, BABYLON.Space.LOCAL (default), or BABYLON.Space.WORLD.
  24. */
  25. public upAxisSpace: Space = Space.LOCAL;
  26. /**
  27. * Used to make an adjustment to the yaw of the bone.
  28. */
  29. public adjustYaw = 0;
  30. /**
  31. * Used to make an adjustment to the pitch of the bone.
  32. */
  33. public adjustPitch = 0;
  34. /**
  35. * Used to make an adjustment to the roll of the bone.
  36. */
  37. public adjustRoll = 0;
  38. /**
  39. * The amount to slerp (spherical linear interpolation) to the target. Set this to a value between 0 and 1 (a value of 1 disables slerp).
  40. */
  41. public slerpAmount = 1;
  42. private _minYaw:number;
  43. private _maxYaw:number;
  44. private _minPitch:number;
  45. private _maxPitch:number;
  46. private _minYawSin:number;
  47. private _minYawCos:number;
  48. private _maxYawSin:number;
  49. private _maxYawCos:number;
  50. private _midYawConstraint:number;
  51. private _minPitchTan:number;
  52. private _maxPitchTan:number;
  53. private _boneQuat:Quaternion = Quaternion.Identity();
  54. private _slerping = false;
  55. private _transformYawPitch:Matrix;
  56. private _transformYawPitchInv:Matrix;
  57. private _firstFrameSkipped = false;
  58. private _yawRange:number;
  59. private _fowardAxis: Vector3 = Vector3.Forward();
  60. /**
  61. * Get/set the minimum yaw angle that the bone can look to.
  62. */
  63. get minYaw():number{
  64. return this._minYaw;
  65. }
  66. set minYaw(value:number){
  67. this._minYaw = value;
  68. this._minYawSin = Math.sin(value);
  69. this._minYawCos = Math.cos(value);
  70. if(this._maxYaw != null){
  71. this._midYawConstraint = this._getAngleDiff(this._minYaw, this._maxYaw)*.5 + this._minYaw;
  72. this._yawRange = this._maxYaw - this._minYaw;
  73. }
  74. }
  75. /**
  76. * Get/set the maximum yaw angle that the bone can look to.
  77. */
  78. get maxYaw():number{
  79. return this._maxYaw;
  80. }
  81. set maxYaw(value:number){
  82. this._maxYaw = value;
  83. this._maxYawSin = Math.sin(value);
  84. this._maxYawCos = Math.cos(value);
  85. if(this._minYaw != null){
  86. this._midYawConstraint = this._getAngleDiff(this._minYaw, this._maxYaw)*.5 + this._minYaw;
  87. this._yawRange = this._maxYaw - this._minYaw;
  88. }
  89. }
  90. /**
  91. * Get/set the minimum pitch angle that the bone can look to.
  92. */
  93. get minPitch():number{
  94. return this._minPitch;
  95. }
  96. set minPitch(value:number){
  97. this._minPitch = value;
  98. this._minPitchTan = Math.tan(value);
  99. }
  100. /**
  101. * Get/set the maximum pitch angle that the bone can look to.
  102. */
  103. get maxPitch():number{
  104. return this._maxPitch;
  105. }
  106. set maxPitch(value:number){
  107. this._maxPitch = value;
  108. this._maxPitchTan = Math.tan(value);
  109. }
  110. /**
  111. * Create a BoneLookController
  112. * @param mesh the mesh that the bone belongs to
  113. * @param bone the bone that will be looking to the target
  114. * @param target the target Vector3 to look at
  115. * @param settings optional settings:
  116. * - maxYaw: the maximum angle the bone will yaw to
  117. * - minYaw: the minimum angle the bone will yaw to
  118. * - maxPitch: the maximum angle the bone will pitch to
  119. * - minPitch: the minimum angle the bone will yaw to
  120. * - slerpAmount: set the between 0 and 1 to make the bone slerp to the target.
  121. * - upAxis: the up axis of the coordinate system
  122. * - upAxisSpace: the space that the up axis is in - BABYLON.Space.BONE, BABYLON.Space.LOCAL (default), or BABYLON.Space.WORLD.
  123. * - yawAxis: set yawAxis if the bone does not yaw on the y axis
  124. * - pitchAxis: set pitchAxis if the bone does not pitch on the x axis
  125. * - adjustYaw: used to make an adjustment to the yaw of the bone
  126. * - adjustPitch: used to make an adjustment to the pitch of the bone
  127. * - adjustRoll: used to make an adjustment to the roll of the bone
  128. **/
  129. constructor(mesh: AbstractMesh,
  130. bone: Bone,
  131. target: Vector3,
  132. options?: {
  133. adjustYaw?: number,
  134. adjustPitch?: number,
  135. adjustRoll?: number,
  136. slerpAmount?: number,
  137. maxYaw?:number,
  138. minYaw?:number,
  139. maxPitch?:number,
  140. minPitch?:number,
  141. upAxis?:Vector3,
  142. upAxisSpace?:Space,
  143. yawAxis?:Vector3,
  144. pitchAxis?:Vector3,
  145. showSpaceAxes?:boolean
  146. }){
  147. this.mesh = mesh;
  148. this.bone = bone;
  149. this.target = target;
  150. if(options){
  151. if(options.adjustYaw){
  152. this.adjustYaw = options.adjustYaw;
  153. }
  154. if(options.adjustPitch){
  155. this.adjustPitch = options.adjustPitch;
  156. }
  157. if(options.adjustRoll){
  158. this.adjustRoll = options.adjustRoll;
  159. }
  160. if(options.maxYaw != null){
  161. this.maxYaw = options.maxYaw;
  162. }
  163. if(options.minYaw != null){
  164. this.minYaw = options.minYaw;
  165. }
  166. if(options.maxPitch != null){
  167. this.maxPitch = options.maxPitch;
  168. }
  169. if(options.minPitch != null){
  170. this.minPitch = options.minPitch;
  171. }
  172. if(options.slerpAmount != null){
  173. this.slerpAmount = options.slerpAmount;
  174. }
  175. if(options.upAxis != null){
  176. this.upAxis = options.upAxis;
  177. }
  178. if(options.upAxisSpace != null){
  179. this.upAxisSpace = options.upAxisSpace;
  180. }
  181. if(options.yawAxis != null || options.pitchAxis != null){
  182. var newYawAxis = Axis.Y;
  183. var newPitchAxis = Axis.X;
  184. if(options.yawAxis != null){
  185. newYawAxis = options.yawAxis.clone();
  186. newYawAxis.normalize();
  187. }
  188. if(options.pitchAxis != null){
  189. newPitchAxis = options.pitchAxis.clone();
  190. newPitchAxis.normalize();
  191. }
  192. var newRollAxis = Vector3.Cross(newPitchAxis, newYawAxis);
  193. this._transformYawPitch = Matrix.Identity();
  194. Matrix.FromXYZAxesToRef(newPitchAxis, newYawAxis, newRollAxis, this._transformYawPitch);
  195. this._transformYawPitchInv = this._transformYawPitch.clone();
  196. this._transformYawPitch.invert();
  197. }
  198. }
  199. if(!bone.getParent() && this.upAxisSpace == Space.BONE){
  200. this.upAxisSpace = Space.LOCAL;
  201. }
  202. }
  203. /**
  204. * Update the bone to look at the target. This should be called before the scene is rendered (use scene.registerBeforeRender()).
  205. */
  206. public update (): void {
  207. //skip the first frame when slerping so that the mesh rotation is correct
  208. if(this.slerpAmount < 1 && !this._firstFrameSkipped){
  209. this._firstFrameSkipped = true;
  210. return;
  211. }
  212. var bone = this.bone;
  213. var bonePos = BoneLookController._tmpVecs[0];
  214. bone.getAbsolutePositionToRef(this.mesh, bonePos);
  215. var target = this.target;
  216. var _tmpMat1 = BoneLookController._tmpMats[0];
  217. var _tmpMat2 = BoneLookController._tmpMats[1];
  218. var mesh = this.mesh;
  219. var parentBone = bone.getParent();
  220. var upAxis = BoneLookController._tmpVecs[1];
  221. upAxis.copyFrom(this.upAxis);
  222. if(this.upAxisSpace == Space.BONE){
  223. if (this._transformYawPitch){
  224. Vector3.TransformCoordinatesToRef(upAxis, this._transformYawPitchInv, upAxis);
  225. }
  226. parentBone.getDirectionToRef(upAxis, this.mesh, upAxis);
  227. }else if(this.upAxisSpace == Space.LOCAL){
  228. mesh.getDirectionToRef(upAxis, upAxis);
  229. if(mesh.scaling.x != 1 || mesh.scaling.y != 1 || mesh.scaling.z != 1){
  230. upAxis.normalize();
  231. }
  232. }
  233. if(this._maxPitch != null || this._minPitch != null || this._maxYaw != null || this._minYaw != null){
  234. var _tmpMat3 = BoneLookController._tmpMats[2];
  235. var _tmpMat3Inv = BoneLookController._tmpMats[3];
  236. if(this.upAxisSpace == Space.BONE && upAxis.y == 1){
  237. parentBone.getRotationMatrixToRef(Space.WORLD, this.mesh, _tmpMat3);
  238. }else if(this.upAxisSpace == Space.LOCAL && upAxis.y == 1 && !parentBone){
  239. _tmpMat3.copyFrom(mesh.getWorldMatrix());
  240. }else{
  241. var forwardAxis = BoneLookController._tmpVecs[2];
  242. forwardAxis.copyFrom(this._fowardAxis);
  243. if (this._transformYawPitch) {
  244. Vector3.TransformCoordinatesToRef(forwardAxis, this._transformYawPitchInv, forwardAxis);
  245. }
  246. if(parentBone){
  247. parentBone.getDirectionToRef(forwardAxis, this.mesh, forwardAxis);
  248. }else{
  249. mesh.getDirectionToRef(forwardAxis, forwardAxis);
  250. }
  251. var rightAxis = Vector3.Cross(upAxis, forwardAxis);
  252. rightAxis.normalize();
  253. var forwardAxis = Vector3.Cross(rightAxis, upAxis);
  254. Matrix.FromXYZAxesToRef(rightAxis, upAxis, forwardAxis, _tmpMat3);
  255. }
  256. _tmpMat3.invertToRef(_tmpMat3Inv);
  257. var xzlen:number;
  258. if(this._maxPitch != null || this._minPitch != null){
  259. var localTarget = BoneLookController._tmpVecs[3];
  260. Vector3.TransformCoordinatesToRef(target.subtract(bonePos), _tmpMat3Inv, localTarget);
  261. var xzlen = Math.sqrt(localTarget.x * localTarget.x + localTarget.z * localTarget.z);
  262. var pitch = Math.atan2(localTarget.y, xzlen);
  263. var newPitch = pitch;
  264. if(pitch > this._maxPitch){
  265. localTarget.y = this._maxPitchTan*xzlen;
  266. newPitch = this._maxPitch;
  267. }else if(pitch < this._minPitch){
  268. localTarget.y = this._minPitchTan*xzlen;
  269. newPitch = this._minPitch;
  270. }
  271. if(pitch != newPitch){
  272. Vector3.TransformCoordinatesToRef(localTarget, _tmpMat3, localTarget);
  273. localTarget.addInPlace(bonePos);
  274. target = localTarget;
  275. }
  276. }
  277. if(this._maxYaw != null || this._minYaw != null){
  278. var localTarget = BoneLookController._tmpVecs[4];
  279. Vector3.TransformCoordinatesToRef(target.subtract(bonePos), _tmpMat3Inv, localTarget);
  280. var yaw = Math.atan2(localTarget.x, localTarget.z);
  281. var newYaw = yaw;
  282. if(yaw > this._maxYaw || yaw < this._minYaw){
  283. if(xzlen == null){
  284. xzlen = Math.sqrt(localTarget.x * localTarget.x + localTarget.z * localTarget.z);
  285. }
  286. if(yaw > this._maxYaw){
  287. localTarget.z = this._maxYawCos*xzlen;
  288. localTarget.x = this._maxYawSin*xzlen;
  289. newYaw = this._maxYaw;
  290. }else if(yaw < this._minYaw){
  291. localTarget.z = this._minYawCos*xzlen;
  292. localTarget.x = this._minYawSin*xzlen;
  293. newYaw = this._minYaw;
  294. }
  295. }
  296. if(this._slerping && this._yawRange > Math.PI){
  297. //are we going to be crossing into the min/max region
  298. var _tmpVec8 = BoneLookController._tmpVecs[8];
  299. _tmpVec8.copyFrom(BABYLON.Axis.Z);
  300. if (this._transformYawPitch){
  301. Vector3.TransformCoordinatesToRef(upAxis, this._transformYawPitchInv, upAxis);
  302. }
  303. var _tmpVec9 = BoneLookController._tmpVecs[9];
  304. Vector3.TransformCoordinatesToRef(_tmpVec8, bone.getLocalMatrix(), _tmpVec9);
  305. if (this._transformYawPitch){
  306. Vector3.TransformCoordinatesToRef(upAxis, this._transformYawPitch, upAxis);
  307. }
  308. var boneYaw = Math.atan2(_tmpVec9.x, _tmpVec9.z);
  309. var ang1 = this._getAngleBetween(boneYaw, yaw);
  310. var ang2 = this._getAngleBetween(boneYaw, this._midYawConstraint);
  311. if(ang1 > ang2){
  312. var ang3 = this._getAngleBetween(boneYaw, this._maxYaw);
  313. var ang4 = this._getAngleBetween(boneYaw, this._minYaw);
  314. if(ang4 < ang3){
  315. newYaw = boneYaw+Math.PI*.95;
  316. localTarget.z = Math.cos(newYaw) * xzlen;
  317. localTarget.x = Math.sin(newYaw) * xzlen;
  318. }else{
  319. newYaw = boneYaw-Math.PI*.95;
  320. localTarget.z = Math.cos(newYaw) * xzlen;
  321. localTarget.x = Math.sin(newYaw) * xzlen;
  322. }
  323. }
  324. }
  325. if(yaw != newYaw){
  326. Vector3.TransformCoordinatesToRef(localTarget, _tmpMat3, localTarget);
  327. localTarget.addInPlace(bonePos);
  328. target = localTarget;
  329. }
  330. }
  331. }
  332. var zaxis = BoneLookController._tmpVecs[5];
  333. var xaxis = BoneLookController._tmpVecs[6];
  334. var yaxis = BoneLookController._tmpVecs[7];
  335. var _tmpQuat = BoneLookController._tmpQuat;
  336. target.subtractToRef(bonePos, zaxis);
  337. zaxis.normalize();
  338. Vector3.CrossToRef(upAxis, zaxis, xaxis);
  339. xaxis.normalize();
  340. Vector3.CrossToRef(zaxis, xaxis, yaxis);
  341. yaxis.normalize();
  342. Matrix.FromXYZAxesToRef(xaxis, yaxis, zaxis, _tmpMat1);
  343. if(xaxis.x === 0 && xaxis.y === 0 && xaxis.z === 0){
  344. return;
  345. }
  346. if(yaxis.x === 0 && yaxis.y === 0 && yaxis.z === 0){
  347. return;
  348. }
  349. if(zaxis.x === 0 && zaxis.y === 0 && zaxis.z === 0){
  350. return;
  351. }
  352. if (this.adjustYaw || this.adjustPitch || this.adjustRoll) {
  353. Matrix.RotationYawPitchRollToRef(this.adjustYaw, this.adjustPitch, this.adjustRoll, _tmpMat2);
  354. _tmpMat2.multiplyToRef(_tmpMat1, _tmpMat1);
  355. }
  356. if (this.slerpAmount < 1) {
  357. if (!this._slerping) {
  358. this.bone.getRotationQuaternionToRef(Space.WORLD, this.mesh, this._boneQuat);
  359. }
  360. if(this._transformYawPitch){
  361. this._transformYawPitch.multiplyToRef(_tmpMat1, _tmpMat1);
  362. }
  363. Quaternion.FromRotationMatrixToRef(_tmpMat1, _tmpQuat);
  364. Quaternion.SlerpToRef(this._boneQuat, _tmpQuat, this.slerpAmount, this._boneQuat);
  365. this.bone.setRotationQuaternion(this._boneQuat, Space.WORLD, this.mesh);
  366. this._slerping = true;
  367. } else {
  368. if(this._transformYawPitch){
  369. this._transformYawPitch.multiplyToRef(_tmpMat1, _tmpMat1);
  370. }
  371. this.bone.setRotationMatrix(_tmpMat1, Space.WORLD, this.mesh);
  372. this._slerping = false;
  373. }
  374. }
  375. private _getAngleDiff(ang1, ang2):number {
  376. var angDiff = ang2 - ang1;
  377. angDiff %= Math.PI*2;
  378. if(angDiff > Math.PI){
  379. angDiff -= Math.PI*2;
  380. }else if (angDiff < -Math.PI){
  381. angDiff += Math.PI*2;
  382. }
  383. return angDiff;
  384. }
  385. private _getAngleBetween(ang1, ang2):number {
  386. ang1 %= (2 * Math.PI);
  387. ang1 = (ang1 < 0) ? ang1 + (2 * Math.PI) : ang1;
  388. ang2 %= (2 * Math.PI);
  389. ang2 = (ang2 < 0) ? ang2 + (2 * Math.PI) : ang2;
  390. var ab = 0;
  391. if(ang1 < ang2){
  392. ab = ang2 - ang1;
  393. }else{
  394. ab = ang1 - ang2;
  395. }
  396. if(ab > Math.PI){
  397. ab = Math.PI*2 - ab;
  398. }
  399. return ab;
  400. }
  401. }
  402. }