babylon.boneLookController.ts 21 KB

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