boneLookController.ts 21 KB

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