babylon.math.js 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961
  1. "use strict";
  2. var BABYLON = BABYLON || {};
  3. (function () {
  4. ////////////////////////////////// Ray //////////////////////////////////
  5. BABYLON.Ray = function (origin, direction) {
  6. this.origin = origin;
  7. this.direction = direction;
  8. };
  9. // Methods
  10. BABYLON.Ray.prototype.intersectsBox = function (box) {
  11. var d = 0.0;
  12. var maxValue = Number.MAX_VALUE;
  13. if (Math.abs(this.direction.x) < 0.0000001) {
  14. if (this.origin.x < box.minimum.x || this.origin.x > box.maximum.x) {
  15. return false;
  16. }
  17. }
  18. else {
  19. var inv = 1.0 / this.direction.x;
  20. var min = (box.minimum.x - this.origin.x) * inv;
  21. var max = (box.maximum.x - this.origin.x) * inv;
  22. if (min > max) {
  23. var temp = min;
  24. min = max;
  25. max = temp;
  26. }
  27. d = Math.max(min, d);
  28. maxValue = Math.min(max, maxValue);
  29. if (d > maxValue) {
  30. return false;
  31. }
  32. }
  33. if (Math.abs(this.direction.y) < 0.0000001) {
  34. if (this.origin.y < box.minimum.y || this.origin.y > box.maximum.y) {
  35. return false;
  36. }
  37. }
  38. else {
  39. var inv = 1.0 / this.direction.y;
  40. var min = (box.minimum.y - this.origin.y) * inv;
  41. var max = (box.maximum.y - this.origin.y) * inv;
  42. if (min > max) {
  43. var temp = min;
  44. min = max;
  45. max = temp;
  46. }
  47. d = Math.max(min, d);
  48. maxValue = Math.min(max, maxValue);
  49. if (d > maxValue) {
  50. return false;
  51. }
  52. }
  53. if (Math.abs(this.direction.z) < 0.0000001) {
  54. if (this.origin.z < box.minimum.z || this.origin.z > box.maximum.z) {
  55. return false;
  56. }
  57. }
  58. else {
  59. var inv = 1.0 / this.direction.z;
  60. var min = (box.minimum.z - this.origin.z) * inv;
  61. var max = (box.maximum.z - this.origin.z) * inv;
  62. if (min > max) {
  63. var temp = min;
  64. min = max;
  65. max = temp;
  66. }
  67. d = Math.max(min, d);
  68. maxValue = Math.min(max, maxValue);
  69. if (d > maxValue) {
  70. return false;
  71. }
  72. }
  73. return true;
  74. };
  75. BABYLON.Ray.prototype.intersectsSphere = function (sphere) {
  76. var x = sphere.center.x - this.origin.x;
  77. var y = sphere.center.y - this.origin.y;
  78. var z = sphere.center.z - this.origin.z;
  79. var pyth = (x * x) + (y * y) + (z * z);
  80. var rr = sphere.radius * sphere.radius;
  81. if (pyth <= rr) {
  82. return true;
  83. }
  84. var dot = (x * this.direction.x) + (y * this.direction.y) + (z * this.direction.z);
  85. if (dot < 0.0) {
  86. return false;
  87. }
  88. var temp = pyth - (dot * dot);
  89. return temp <= rr;
  90. };
  91. BABYLON.Ray.prototype.intersectsTriangle = function (vertex0, vertex1, vertex2) {
  92. if (!this._edge1) {
  93. this._edge1 = BABYLON.Vector3.Zero();
  94. this._edge2 = BABYLON.Vector3.Zero();
  95. this._pvec = BABYLON.Vector3.Zero();
  96. this._tvec = BABYLON.Vector3.Zero();
  97. this._qvec = BABYLON.Vector3.Zero();
  98. }
  99. vertex1.subtractToRef(vertex0, this._edge1);
  100. vertex2.subtractToRef(vertex0, this._edge2);
  101. BABYLON.Vector3.CrossToRef(this.direction, this._edge2, this._pvec);
  102. var det = BABYLON.Vector3.Dot(this._edge1, this._pvec);
  103. if (det === 0) {
  104. return null;
  105. }
  106. var invdet = 1 / det;
  107. this.origin.subtractToRef(vertex0, this._tvec);
  108. var bu = BABYLON.Vector3.Dot(this._tvec, this._pvec) * invdet;
  109. if (bu < 0 || bu > 1.0) {
  110. return null;
  111. }
  112. BABYLON.Vector3.CrossToRef(this._tvec, this._edge1, this._qvec);
  113. var bv = BABYLON.Vector3.Dot(this.direction, this._qvec) * invdet;
  114. if (bv < 0 || bu + bv > 1.0) {
  115. return null;
  116. }
  117. return {
  118. bu: bu,
  119. bv: bv,
  120. distance: BABYLON.Vector3.Dot(this._edge2, this._qvec) * invdet
  121. };
  122. };
  123. // Statics
  124. BABYLON.Ray.CreateNew = function (x, y, viewportWidth, viewportHeight, world, view, projection) {
  125. var start = BABYLON.Vector3.Unproject(new BABYLON.Vector3(x, y, 0), viewportWidth, viewportHeight, world, view, projection);
  126. var end = BABYLON.Vector3.Unproject(new BABYLON.Vector3(x, y, 1), viewportWidth, viewportHeight, world, view, projection);
  127. var direction = end.subtract(start);
  128. direction.normalize();
  129. return new BABYLON.Ray(start, direction);
  130. };
  131. BABYLON.Ray.Transform = function (ray, matrix) {
  132. var newOrigin = BABYLON.Vector3.TransformCoordinates(ray.origin, matrix);
  133. var newDirection = BABYLON.Vector3.TransformNormal(ray.direction, matrix);
  134. return new BABYLON.Ray(newOrigin, newDirection);
  135. };
  136. ////////////////////////////////// Color3 //////////////////////////////////
  137. BABYLON.Color3 = function (initialR, initialG, initialB) {
  138. this.r = initialR;
  139. this.g = initialG;
  140. this.b = initialB;
  141. };
  142. BABYLON.Color3.prototype.toString = function () {
  143. return "{R: " + this.r + " G:" + this.g + " B:" + this.b + "}";
  144. };
  145. // Operators
  146. BABYLON.Color3.prototype.asArray = function () {
  147. var result = [];
  148. this.toArray(result, 0);
  149. return result;
  150. };
  151. BABYLON.Color3.prototype.toArray = function (array, index) {
  152. if (index === undefined) {
  153. index = 0;
  154. }
  155. array[index] = this.r;
  156. array[index + 1] = this.g;
  157. array[index + 2] = this.b;
  158. };
  159. BABYLON.Color3.prototype.multiply = function (otherColor) {
  160. return new BABYLON.Color3(this.r * otherColor.r, this.g * otherColor.g, this.b * otherColor.b);
  161. };
  162. BABYLON.Color3.prototype.multiplyToRef = function (otherColor, result) {
  163. result.r = this.r * otherColor.r;
  164. result.g = this.g * otherColor.g;
  165. result.b = this.b * otherColor.b;
  166. };
  167. BABYLON.Color3.prototype.equals = function (otherColor) {
  168. return otherColor && this.r === otherColor.r && this.g === otherColor.g && this.b === otherColor.b;
  169. };
  170. BABYLON.Color3.prototype.scale = function (scale) {
  171. return new BABYLON.Color3(this.r * scale, this.g * scale, this.b * scale);
  172. };
  173. BABYLON.Color3.prototype.scaleToRef = function (scale, result) {
  174. result.r = this.r * scale;
  175. result.g = this.g * scale;
  176. result.b = this.b * scale;
  177. };
  178. BABYLON.Color3.prototype.clone = function () {
  179. return new BABYLON.Color3(this.r, this.g, this.b);
  180. };
  181. BABYLON.Color3.prototype.copyFrom = function (source) {
  182. this.r = source.r;
  183. this.g = source.g;
  184. this.b = source.b;
  185. };
  186. BABYLON.Color3.prototype.copyFromFloats = function (r, g, b) {
  187. this.r = r;
  188. this.g = g;
  189. this.b = b;
  190. };
  191. // Statics
  192. BABYLON.Color3.FromArray = function (array) {
  193. return new BABYLON.Color3(array[0], array[1], array[2]);
  194. };
  195. ////////////////////////////////// Color4 //////////////////////////////////
  196. BABYLON.Color4 = function (initialR, initialG, initialB, initialA) {
  197. this.r = initialR;
  198. this.g = initialG;
  199. this.b = initialB;
  200. this.a = initialA;
  201. };
  202. // Operators
  203. BABYLON.Color4.prototype.addInPlace = function (right) {
  204. this.r += right.r;
  205. this.g += right.g;
  206. this.b += right.b;
  207. this.a += right.a;
  208. };
  209. BABYLON.Color4.prototype.asArray = function () {
  210. var result = [];
  211. this.toArray(result, 0);
  212. return result;
  213. };
  214. BABYLON.Color4.prototype.toArray = function (array, index) {
  215. if (index === undefined) {
  216. index = 0;
  217. }
  218. array[index] = this.r;
  219. array[index + 1] = this.g;
  220. array[index + 2] = this.b;
  221. array[index + 3] = this.a;
  222. };
  223. BABYLON.Color4.prototype.add = function (right) {
  224. return new BABYLON.Color4(this.r + right.r, this.g + right.g, this.b + right.b, this.a + right.a);
  225. };
  226. BABYLON.Color4.prototype.subtract = function (right) {
  227. return new BABYLON.Color4(this.r - right.r, this.g - right.g, this.b - right.b, this.a - right.a);
  228. };
  229. BABYLON.Color4.prototype.subtractToRef = function (right, result) {
  230. result.r = this.r - right.r;
  231. result.g = this.g - right.g;
  232. result.b = this.b - right.b;
  233. result.a = this.a - right.a;
  234. };
  235. BABYLON.Color4.prototype.scale = function (scale) {
  236. return new BABYLON.Color4(this.r * scale, this.g * scale, this.b * scale, this.a * scale);
  237. };
  238. BABYLON.Color4.prototype.scaleToRef = function (scale, result) {
  239. result.r = this.r * scale;
  240. result.g = this.g * scale;
  241. result.b = this.b * scale;
  242. result.a = this.a * scale;
  243. };
  244. BABYLON.Color4.prototype.toString = function () {
  245. return "{R: " + this.r + " G:" + this.g + " B:" + this.b + " A:" + this.a + "}";
  246. };
  247. BABYLON.Color4.prototype.clone = function () {
  248. return new BABYLON.Color4(this.r, this.g, this.b, this.a);
  249. };
  250. // Statics
  251. BABYLON.Color4.Lerp = function (left, right, amount) {
  252. var result = new BABYLON.Color4(0, 0, 0, 0);
  253. BABYLON.Color4.LerpToRef(left, right, amount, result);
  254. return result;
  255. };
  256. BABYLON.Color4.LerpToRef = function (left, right, amount, result) {
  257. result.r = left.r + (right.r - left.r) * amount;
  258. result.g = left.g + (right.g - left.g) * amount;
  259. result.b = left.b + (right.b - left.b) * amount;
  260. result.a = left.a + (right.a - left.a) * amount;
  261. };
  262. BABYLON.Color4.FromArray = function (array, offset) {
  263. if (!offset) {
  264. offset = 0;
  265. }
  266. return new BABYLON.Color4(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  267. };
  268. ////////////////////////////////// Vector2 //////////////////////////////////
  269. BABYLON.Vector2 = function (initialX, initialY) {
  270. this.x = initialX;
  271. this.y = initialY;
  272. };
  273. BABYLON.Vector2.prototype.toString = function () {
  274. return "{X: " + this.x + " Y:" + this.y + "}";
  275. };
  276. // Operators
  277. BABYLON.Vector2.prototype.asArray = function () {
  278. var result = [];
  279. this.toArray(result, 0);
  280. return result;
  281. };
  282. BABYLON.Vector2.prototype.toArray = function (array, index) {
  283. if (index === undefined) {
  284. index = 0;
  285. }
  286. array[index] = this.x;
  287. array[index + 1] = this.y;
  288. };
  289. BABYLON.Vector2.prototype.add = function (otherVector) {
  290. return new BABYLON.Vector2(this.x + otherVector.x, this.y + otherVector.y);
  291. };
  292. BABYLON.Vector2.prototype.subtract = function (otherVector) {
  293. return new BABYLON.Vector2(this.x - otherVector.x, this.y - otherVector.y);
  294. };
  295. BABYLON.Vector2.prototype.negate = function () {
  296. return new BABYLON.Vector2(-this.x, -this.y);
  297. };
  298. BABYLON.Vector2.prototype.scaleInPlace = function (scale) {
  299. this.x *= scale;
  300. this.y *= scale;
  301. };
  302. BABYLON.Vector2.prototype.scale = function (scale) {
  303. return new BABYLON.Vector2(this.x * scale, this.y * scale);
  304. };
  305. BABYLON.Vector2.prototype.equals = function (otherVector) {
  306. return otherVector && this.x === otherVector.x && this.y === otherVector.y;
  307. };
  308. // Properties
  309. BABYLON.Vector2.prototype.length = function () {
  310. return Math.sqrt(this.x * this.x + this.y * this.y);
  311. };
  312. BABYLON.Vector2.prototype.lengthSquared = function () {
  313. return (this.x * this.x + this.y * this.y);
  314. };
  315. // Methods
  316. BABYLON.Vector2.prototype.normalize = function () {
  317. var len = this.length();
  318. if (len === 0)
  319. return;
  320. var num = 1.0 / len;
  321. this.x *= num;
  322. this.y *= num;
  323. };
  324. BABYLON.Vector2.prototype.clone = function () {
  325. return new BABYLON.Vector2(this.x, this.y);
  326. };
  327. // Statics
  328. BABYLON.Vector2.Zero = function () {
  329. return new BABYLON.Vector2(0, 0);
  330. };
  331. BABYLON.Vector2.CatmullRom = function (value1, value2, value3, value4, amount) {
  332. var squared = amount * amount;
  333. var cubed = amount * squared;
  334. var x = 0.5 * ((((2.0 * value2.x) + ((-value1.x + value3.x) * amount)) +
  335. (((((2.0 * value1.x) - (5.0 * value2.x)) + (4.0 * value3.x)) - value4.x) * squared)) +
  336. ((((-value1.x + (3.0 * value2.x)) - (3.0 * value3.x)) + value4.x) * cubed));
  337. var y = 0.5 * ((((2.0 * value2.y) + ((-value1.y + value3.y) * amount)) +
  338. (((((2.0 * value1.y) - (5.0 * value2.y)) + (4.0 * value3.y)) - value4.y) * squared)) +
  339. ((((-value1.y + (3.0 * value2.y)) - (3.0 * value3.y)) + value4.y) * cubed));
  340. return new BABYLON.Vector2(x, y);
  341. };
  342. BABYLON.Vector2.Clamp = function (value, min, max) {
  343. var x = value.x;
  344. x = (x > max.x) ? max.x : x;
  345. x = (x < min.x) ? min.x : x;
  346. var y = value.y;
  347. y = (y > max.y) ? max.y : y;
  348. y = (y < min.y) ? min.y : y;
  349. return new BABYLON.Vector2(x, y);
  350. };
  351. BABYLON.Vector2.Hermite = function (value1, tangent1, value2, tangent2, amount) {
  352. var squared = amount * amount;
  353. var cubed = amount * squared;
  354. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  355. var part2 = (-2.0 * cubed) + (3.0 * squared);
  356. var part3 = (cubed - (2.0 * squared)) + amount;
  357. var part4 = cubed - squared;
  358. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  359. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  360. return new BABYLON.Vector2(x, y);
  361. };
  362. BABYLON.Vector2.Lerp = function (start, end, amount) {
  363. var x = start.x + ((end.x - start.x) * amount);
  364. var y = start.y + ((end.y - start.y) * amount);
  365. return new BABYLON.Vector2(x, y);
  366. };
  367. BABYLON.Vector2.Dot = function (left, right) {
  368. return left.x * right.x + left.y * right.y;
  369. };
  370. BABYLON.Vector2.Normalize = function (vector) {
  371. var newVector = vector.clone();
  372. newVector.normalize();
  373. return newVector;
  374. };
  375. BABYLON.Vector2.Minimize = function (left, right) {
  376. var x = (left.x < right.x) ? left.x : right.x;
  377. var y = (left.y < right.y) ? left.y : right.y;
  378. return new BABYLON.Vector2(x, y);
  379. };
  380. BABYLON.Vector2.Maximize = function (left, right) {
  381. var x = (left.x > right.x) ? left.x : right.x;
  382. var y = (left.y > right.y) ? left.y : right.y;
  383. return new BABYLON.Vector2(x, y);
  384. };
  385. BABYLON.Vector2.Transform = function (vector, transformation) {
  386. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]);
  387. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]);
  388. return new BABYLON.Vector2(x, y);
  389. };
  390. BABYLON.Vector2.Distance = function (value1, value2) {
  391. return Math.sqrt(BABYLON.Vector2.DistanceSquared(value1, value2));
  392. };
  393. BABYLON.Vector2.DistanceSquared = function (value1, value2) {
  394. var x = value1.x - value2.x;
  395. var y = value1.y - value2.y;
  396. return (x * x) + (y * y);
  397. };
  398. ////////////////////////////////// Vector3 //////////////////////////////////
  399. BABYLON.Vector3 = function (initialX, initialY, initialZ) {
  400. this.x = initialX;
  401. this.y = initialY;
  402. this.z = initialZ;
  403. };
  404. BABYLON.Vector3.prototype.toString = function () {
  405. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + "}";
  406. };
  407. // Operators
  408. BABYLON.Vector3.prototype.asArray = function () {
  409. var result = [];
  410. this.toArray(result, 0);
  411. return result;
  412. };
  413. BABYLON.Vector3.prototype.toArray = function (array, index) {
  414. if (index === undefined) {
  415. index = 0;
  416. }
  417. array[index] = this.x;
  418. array[index + 1] = this.y;
  419. array[index + 2] = this.z;
  420. };
  421. BABYLON.Vector3.prototype.addInPlace = function (otherVector) {
  422. this.x += otherVector.x;
  423. this.y += otherVector.y;
  424. this.z += otherVector.z;
  425. };
  426. BABYLON.Vector3.prototype.add = function (otherVector) {
  427. return new BABYLON.Vector3(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z);
  428. };
  429. BABYLON.Vector3.prototype.addToRef = function (otherVector, result) {
  430. result.x = this.x + otherVector.x;
  431. result.y = this.y + otherVector.y;
  432. result.z = this.z + otherVector.z;
  433. };
  434. BABYLON.Vector3.prototype.subtractInPlace = function (otherVector) {
  435. this.x -= otherVector.x;
  436. this.y -= otherVector.y;
  437. this.z -= otherVector.z;
  438. };
  439. BABYLON.Vector3.prototype.subtract = function (otherVector) {
  440. return new BABYLON.Vector3(this.x - otherVector.x, this.y - otherVector.y, this.z - otherVector.z);
  441. };
  442. BABYLON.Vector3.prototype.subtractToRef = function (otherVector, result) {
  443. result.x = this.x - otherVector.x;
  444. result.y = this.y - otherVector.y;
  445. result.z = this.z - otherVector.z;
  446. };
  447. BABYLON.Vector3.prototype.subtractFromFloats = function (x, y, z) {
  448. return new BABYLON.Vector3(this.x - x, this.y - y, this.z - z);
  449. };
  450. BABYLON.Vector3.prototype.subtractFromFloatsToRef = function (x, y, z, result) {
  451. result.x = this.x - x;
  452. result.y = this.y - y;
  453. result.z = this.z - z;
  454. };
  455. BABYLON.Vector3.prototype.negate = function () {
  456. return new BABYLON.Vector3(-this.x, -this.y, -this.z);
  457. };
  458. BABYLON.Vector3.prototype.scaleInPlace = function (scale) {
  459. this.x *= scale;
  460. this.y *= scale;
  461. this.z *= scale;
  462. };
  463. BABYLON.Vector3.prototype.scale = function (scale) {
  464. return new BABYLON.Vector3(this.x * scale, this.y * scale, this.z * scale);
  465. };
  466. BABYLON.Vector3.prototype.scaleToRef = function (scale, result) {
  467. result.x = this.x * scale;
  468. result.y = this.y * scale;
  469. result.z = this.z * scale;
  470. };
  471. BABYLON.Vector3.prototype.equals = function (otherVector) {
  472. return otherVector && this.x === otherVector.x && this.y === otherVector.y && this.z === otherVector.z;
  473. };
  474. BABYLON.Vector3.prototype.equalsToFloats = function (x, y, z) {
  475. return this.x === x && this.y === y && this.z === z;
  476. };
  477. BABYLON.Vector3.prototype.multiplyInPlace = function (otherVector) {
  478. this.x *= otherVector.x;
  479. this.y *= otherVector.y;
  480. this.z *= otherVector.z;
  481. };
  482. BABYLON.Vector3.prototype.multiply = function (otherVector) {
  483. return new BABYLON.Vector3(this.x * otherVector.x, this.y * otherVector.y, this.z * otherVector.z);
  484. };
  485. BABYLON.Vector3.prototype.multiplyToRef = function (otherVector, result) {
  486. result.x = this.x * otherVector.x;
  487. result.y = this.y * otherVector.y;
  488. result.z = this.z * otherVector.z;
  489. };
  490. BABYLON.Vector3.prototype.multiplyByFloats = function (x, y, z) {
  491. return new BABYLON.Vector3(this.x * x, this.y * y, this.z * z);
  492. };
  493. BABYLON.Vector3.prototype.divide = function (otherVector) {
  494. return new BABYLON.Vector3(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z);
  495. };
  496. BABYLON.Vector3.prototype.divideToRef = function (otherVector, result) {
  497. result.x = this.x / otherVector.x;
  498. result.y = this.y / otherVector.y;
  499. result.z = this.z / otherVector.z;
  500. };
  501. // Properties
  502. BABYLON.Vector3.prototype.length = function () {
  503. return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
  504. };
  505. BABYLON.Vector3.prototype.lengthSquared = function () {
  506. return (this.x * this.x + this.y * this.y + this.z * this.z);
  507. };
  508. // Methods
  509. BABYLON.Vector3.prototype.normalize = function () {
  510. var len = this.length();
  511. if (len === 0)
  512. return;
  513. var num = 1.0 / len;
  514. this.x *= num;
  515. this.y *= num;
  516. this.z *= num;
  517. };
  518. BABYLON.Vector3.prototype.clone = function () {
  519. return new BABYLON.Vector3(this.x, this.y, this.z);
  520. };
  521. BABYLON.Vector3.prototype.copyFrom = function (source) {
  522. this.x = source.x;
  523. this.y = source.y;
  524. this.z = source.z;
  525. };
  526. BABYLON.Vector3.prototype.copyFromFloats = function (x, y, z) {
  527. this.x = x;
  528. this.y = y;
  529. this.z = z;
  530. };
  531. // Statics
  532. BABYLON.Vector3.FromArray = function (array, offset) {
  533. if (!offset) {
  534. offset = 0;
  535. }
  536. return new BABYLON.Vector3(array[offset], array[offset + 1], array[offset + 2]);
  537. };
  538. BABYLON.Vector3.FromArrayToRef = function (array, offset, result) {
  539. if (!offset) {
  540. offset = 0;
  541. }
  542. result.x = array[offset];
  543. result.y = array[offset + 1];
  544. result.z = array[offset + 2];
  545. };
  546. BABYLON.Vector3.FromFloatsToRef = function (x, y, z, result) {
  547. result.x = x;
  548. result.y = y;
  549. result.z = z;
  550. };
  551. BABYLON.Vector3.Zero = function () {
  552. return new BABYLON.Vector3(0, 0, 0);
  553. };
  554. BABYLON.Vector3.Up = function () {
  555. return new BABYLON.Vector3(0, 1.0, 0);
  556. };
  557. BABYLON.Vector3.TransformCoordinates = function (vector, transformation) {
  558. var result = BABYLON.Vector3.Zero();
  559. BABYLON.Vector3.TransformCoordinatesToRef(vector, transformation, result);
  560. return result;
  561. };
  562. BABYLON.Vector3.TransformCoordinatesToRef = function (vector, transformation, result) {
  563. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + (vector.z * transformation.m[8]) + transformation.m[12];
  564. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + (vector.z * transformation.m[9]) + transformation.m[13];
  565. var z = (vector.x * transformation.m[2]) + (vector.y * transformation.m[6]) + (vector.z * transformation.m[10]) + transformation.m[14];
  566. var w = (vector.x * transformation.m[3]) + (vector.y * transformation.m[7]) + (vector.z * transformation.m[11]) + transformation.m[15];
  567. result.x = x / w;
  568. result.y = y / w;
  569. result.z = z / w;
  570. };
  571. BABYLON.Vector3.TransformCoordinatesFromFloatsToRef = function (x, y, z, transformation, result) {
  572. var rx = (x * transformation.m[0]) + (y * transformation.m[4]) + (z * transformation.m[8]) + transformation.m[12];
  573. var ry = (x * transformation.m[1]) + (y * transformation.m[5]) + (z * transformation.m[9]) + transformation.m[13];
  574. var rz = (x * transformation.m[2]) + (y * transformation.m[6]) + (z * transformation.m[10]) + transformation.m[14];
  575. var rw = (x * transformation.m[3]) + (y * transformation.m[7]) + (z * transformation.m[11]) + transformation.m[15];
  576. result.x = rx / rw;
  577. result.y = ry / rw;
  578. result.z = rz / rw;
  579. };
  580. BABYLON.Vector3.TransformNormal = function (vector, transformation) {
  581. var result = BABYLON.Vector3.Zero();
  582. BABYLON.Vector3.TransformNormalToRef(vector, transformation, result);
  583. return result;
  584. };
  585. BABYLON.Vector3.TransformNormalToRef = function (vector, transformation, result) {
  586. result.x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + (vector.z * transformation.m[8]);
  587. result.y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + (vector.z * transformation.m[9]);
  588. result.z = (vector.x * transformation.m[2]) + (vector.y * transformation.m[6]) + (vector.z * transformation.m[10]);
  589. };
  590. BABYLON.Vector3.TransformNormalFromFloatsToRef = function (x, y, z, transformation, result) {
  591. result.x = (x * transformation.m[0]) + (y * transformation.m[4]) + (z * transformation.m[8]);
  592. result.y = (x * transformation.m[1]) + (y * transformation.m[5]) + (z * transformation.m[9]);
  593. result.z = (x * transformation.m[2]) + (y * transformation.m[6]) + (z * transformation.m[10]);
  594. };
  595. BABYLON.Vector3.CatmullRom = function (value1, value2, value3, value4, amount) {
  596. var squared = amount * amount;
  597. var cubed = amount * squared;
  598. var x = 0.5 * ((((2.0 * value2.x) + ((-value1.x + value3.x) * amount)) +
  599. (((((2.0 * value1.x) - (5.0 * value2.x)) + (4.0 * value3.x)) - value4.x) * squared)) +
  600. ((((-value1.x + (3.0 * value2.x)) - (3.0 * value3.x)) + value4.x) * cubed));
  601. var y = 0.5 * ((((2.0 * value2.y) + ((-value1.y + value3.y) * amount)) +
  602. (((((2.0 * value1.y) - (5.0 * value2.y)) + (4.0 * value3.y)) - value4.y) * squared)) +
  603. ((((-value1.y + (3.0 * value2.y)) - (3.0 * value3.y)) + value4.y) * cubed));
  604. var z = 0.5 * ((((2.0 * value2.z) + ((-value1.z + value3.z) * amount)) +
  605. (((((2.0 * value1.z) - (5.0 * value2.z)) + (4.0 * value3.z)) - value4.z) * squared)) +
  606. ((((-value1.z + (3.0 * value2.z)) - (3.0 * value3.z)) + value4.z) * cubed));
  607. return new BABYLON.Vector3(x, y, z);
  608. };
  609. BABYLON.Vector3.Clamp = function (value, min, max) {
  610. var x = value.x;
  611. x = (x > max.x) ? max.x : x;
  612. x = (x < min.x) ? min.x : x;
  613. var y = value.y;
  614. y = (y > max.y) ? max.y : y;
  615. y = (y < min.y) ? min.y : y;
  616. var z = value.z;
  617. z = (z > max.z) ? max.z : z;
  618. z = (z < min.z) ? min.z : z;
  619. return new BABYLON.Vector3(x, y, z);
  620. };
  621. BABYLON.Vector3.Hermite = function (value1, tangent1, value2, tangent2, amount) {
  622. var squared = amount * amount;
  623. var cubed = amount * squared;
  624. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  625. var part2 = (-2.0 * cubed) + (3.0 * squared);
  626. var part3 = (cubed - (2.0 * squared)) + amount;
  627. var part4 = cubed - squared;
  628. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  629. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  630. var z = (((value1.z * part1) + (value2.z * part2)) + (tangent1.z * part3)) + (tangent2.z * part4);
  631. return new BABYLON.Vector3(x, y, z);
  632. };
  633. BABYLON.Vector3.Lerp = function (start, end, amount) {
  634. var x = start.x + ((end.x - start.x) * amount);
  635. var y = start.y + ((end.y - start.y) * amount);
  636. var z = start.z + ((end.z - start.z) * amount);
  637. return new BABYLON.Vector3(x, y, z);
  638. };
  639. BABYLON.Vector3.Dot = function (left, right) {
  640. return (left.x * right.x + left.y * right.y + left.z * right.z);
  641. };
  642. BABYLON.Vector3.Cross = function (left, right) {
  643. var result = BABYLON.Vector3.Zero();
  644. BABYLON.Vector3.CrossToRef(left, right, result);
  645. return result;
  646. };
  647. BABYLON.Vector3.CrossToRef = function (left, right, result) {
  648. result.x = left.y * right.z - left.z * right.y;
  649. result.y = left.z * right.x - left.x * right.z;
  650. result.z = left.x * right.y - left.y * right.x;
  651. };
  652. BABYLON.Vector3.Normalize = function (vector) {
  653. var result = BABYLON.Vector3.Zero();
  654. BABYLON.Vector3.NormalizeToRef(vector, result);
  655. return result;
  656. };
  657. BABYLON.Vector3.NormalizeToRef = function (vector, result) {
  658. result.copyFrom(vector);
  659. result.normalize();
  660. };
  661. BABYLON.Vector3.Project = function (vector, world, transform, viewport) {
  662. var cw = viewport.width;
  663. var ch = viewport.height;
  664. var cx = viewport.x;
  665. var cy = viewport.y;
  666. var viewportMatrix = BABYLON.Matrix.FromValues(
  667. cw / 2.0, 0, 0, 0,
  668. 0, -ch / 2.0, 0, 0,
  669. 0, 0, 1, 0,
  670. cx + cw / 2.0, ch / 2.0 + cy, 0, 1);
  671. var finalMatrix = world.multiply(transform).multiply(viewportMatrix);
  672. return BABYLON.Vector3.TransformCoordinates(vector, finalMatrix);
  673. };
  674. BABYLON.Vector3.Unproject = function (source, viewportWidth, viewportHeight, world, view, projection) {
  675. var matrix = world.multiply(view).multiply(projection);
  676. matrix.invert();
  677. source.x = source.x / viewportWidth * 2 - 1;
  678. source.y = -(source.y / viewportHeight * 2 - 1);
  679. var vector = BABYLON.Vector3.TransformCoordinates(source, matrix);
  680. var num = source.x * matrix.m[3] + source.y * matrix.m[7] + source.z * matrix.m[11] + matrix.m[15];
  681. if (BABYLON.Tools.WithinEpsilon(num, 1.0)) {
  682. vector = vector.scale(1.0 / num);
  683. }
  684. return vector;
  685. };
  686. BABYLON.Vector3.Minimize = function (left, right) {
  687. var x = (left.x < right.x) ? left.x : right.x;
  688. var y = (left.y < right.y) ? left.y : right.y;
  689. var z = (left.z < right.z) ? left.z : right.z;
  690. return new BABYLON.Vector3(x, y, z);
  691. };
  692. BABYLON.Vector3.Maximize = function (left, right) {
  693. var x = (left.x > right.x) ? left.x : right.x;
  694. var y = (left.y > right.y) ? left.y : right.y;
  695. var z = (left.z > right.z) ? left.z : right.z;
  696. return new BABYLON.Vector3(x, y, z);
  697. };
  698. BABYLON.Vector3.Distance = function (value1, value2) {
  699. return Math.sqrt(BABYLON.Vector3.DistanceSquared(value1, value2));
  700. };
  701. BABYLON.Vector3.DistanceSquared = function (value1, value2) {
  702. var x = value1.x - value2.x;
  703. var y = value1.y - value2.y;
  704. var z = value1.z - value2.z;
  705. return (x * x) + (y * y) + (z * z);
  706. };
  707. ////////////////////////////////// Quaternion //////////////////////////////////
  708. BABYLON.Quaternion = function (initialX, initialY, initialZ, initialW) {
  709. this.x = initialX;
  710. this.y = initialY;
  711. this.z = initialZ;
  712. this.w = initialW;
  713. };
  714. BABYLON.Quaternion.prototype.toString = function () {
  715. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + " W:" + this.w + "}";
  716. };
  717. BABYLON.Quaternion.prototype.asArray = function () {
  718. return [this.x, this.y, this.z, this.w];
  719. };
  720. BABYLON.Quaternion.prototype.equals = function (otherQuaternion) {
  721. return otherQuaternion && this.x === otherQuaternion.x && this.y === otherQuaternion.y && this.z === otherQuaternion.z && this.w === otherQuaternion.w;
  722. };
  723. BABYLON.Quaternion.prototype.clone = function () {
  724. return new BABYLON.Quaternion(this.x, this.y, this.z, this.w);
  725. };
  726. BABYLON.Quaternion.prototype.copyFrom = function (other) {
  727. this.x = other.x;
  728. this.y = other.y;
  729. this.z = other.z;
  730. this.w = other.w;
  731. };
  732. BABYLON.Quaternion.prototype.add = function (other) {
  733. return new BABYLON.Quaternion(this.x + other.x, this.y + other.y, this.z + other.z, this.w + other.w);
  734. };
  735. BABYLON.Quaternion.prototype.scale = function (value) {
  736. return new BABYLON.Quaternion(this.x * value, this.y * value, this.z * value, this.w * value);
  737. };
  738. BABYLON.Quaternion.prototype.multiply = function (q1) {
  739. var result = new BABYLON.Quaternion(0, 0, 0, 1.0);
  740. this.multiplyToRef(q1, result);
  741. return result;
  742. };
  743. BABYLON.Quaternion.prototype.multiplyToRef = function (q1, result) {
  744. result.x = this.x * q1.w + this.y * q1.z - this.z * q1.y + this.w * q1.x;
  745. result.y = -this.x * q1.z + this.y * q1.w + this.z * q1.x + this.w * q1.y;
  746. result.z = this.x * q1.y - this.y * q1.x + this.z * q1.w + this.w * q1.z;
  747. result.w = -this.x * q1.x - this.y * q1.y - this.z * q1.z + this.w * q1.w;
  748. };
  749. BABYLON.Quaternion.prototype.length = function () {
  750. return Math.sqrt((this.x * this.x) + (this.y * this.y) + (this.z * this.z) + (this.w * this.w));
  751. };
  752. BABYLON.Quaternion.prototype.normalize = function () {
  753. var length = 1.0 / this.length();
  754. this.x *= length;
  755. this.y *= length;
  756. this.z *= length;
  757. this.w *= length;
  758. };
  759. BABYLON.Quaternion.prototype.toEulerAngles = function () {
  760. var qx = this.x;
  761. var qy = this.y;
  762. var qz = this.z;
  763. var qw = this.w;
  764. var sqx = qx * qx;
  765. var sqy = qy * qy;
  766. var sqz = qz * qz;
  767. var yaw = Math.atan2(2.0 * (qy * qw - qx * qz), 1.0 - 2.0 * (sqy + sqz));
  768. var pitch = Math.asin(2.0 * (qx * qy + qz * qw));
  769. var roll = Math.atan2(2.0 * (qx * qw - qy * qz), 1.0 - 2.0 * (sqx + sqz));
  770. var gimbaLockTest = qx * qy + qz * qw;
  771. if (gimbaLockTest > 0.499) {
  772. yaw = 2.0 * Math.atan2(qx, qw);
  773. roll = 0;
  774. } else if (gimbaLockTest < -0.499) {
  775. yaw = -2.0 * Math.atan2(qx, qw);
  776. roll = 0;
  777. }
  778. return new BABYLON.Vector3(pitch, yaw, roll);
  779. };
  780. BABYLON.Quaternion.prototype.toRotationMatrix = function (result) {
  781. var xx = this.x * this.x;
  782. var yy = this.y * this.y;
  783. var zz = this.z * this.z;
  784. var xy = this.x * this.y;
  785. var zw = this.z * this.w;
  786. var zx = this.z * this.x;
  787. var yw = this.y * this.w;
  788. var yz = this.y * this.z;
  789. var xw = this.x * this.w;
  790. result.m[0] = 1.0 - (2.0 * (yy + zz));
  791. result.m[1] = 2.0 * (xy + zw);
  792. result.m[2] = 2.0 * (zx - yw);
  793. result.m[3] = 0;
  794. result.m[4] = 2.0 * (xy - zw);
  795. result.m[5] = 1.0 - (2.0 * (zz + xx));
  796. result.m[6] = 2.0 * (yz + xw);
  797. result.m[7] = 0;
  798. result.m[8] = 2.0 * (zx + yw);
  799. result.m[9] = 2.0 * (yz - xw);
  800. result.m[10] = 1.0 - (2.0 * (yy + xx));
  801. result.m[11] = 0;
  802. result.m[12] = 0;
  803. result.m[13] = 0;
  804. result.m[14] = 0;
  805. result.m[15] = 1.0;
  806. };
  807. // Statics
  808. BABYLON.Quaternion.RotationAxis = function (axis, angle) {
  809. var result = new BABYLON.Quaternion();
  810. result.w = Math.cos(angle / 2);
  811. result.x = axis.x * Math.sin(angle / 2);
  812. result.y = axis.y * Math.sin(angle / 2);
  813. result.z = axis.z * Math.sin(angle / 2);
  814. return result;
  815. };
  816. BABYLON.Quaternion.FromArray = function (array, offset) {
  817. if (!offset) {
  818. offset = 0;
  819. }
  820. return new BABYLON.Quaternion(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  821. };
  822. BABYLON.Quaternion.RotationYawPitchRoll = function (yaw, pitch, roll) {
  823. var result = new BABYLON.Quaternion();
  824. BABYLON.Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, result);
  825. return result;
  826. };
  827. BABYLON.Quaternion.RotationYawPitchRollToRef = function (yaw, pitch, roll, result) {
  828. var halfRoll = roll * 0.5;
  829. var halfPitch = pitch * 0.5;
  830. var halfYaw = yaw * 0.5;
  831. var sinRoll = Math.sin(halfRoll);
  832. var cosRoll = Math.cos(halfRoll);
  833. var sinPitch = Math.sin(halfPitch);
  834. var cosPitch = Math.cos(halfPitch);
  835. var sinYaw = Math.sin(halfYaw);
  836. var cosYaw = Math.cos(halfYaw);
  837. result.x = (cosYaw * sinPitch * cosRoll) + (sinYaw * cosPitch * sinRoll);
  838. result.y = (sinYaw * cosPitch * cosRoll) - (cosYaw * sinPitch * sinRoll);
  839. result.z = (cosYaw * cosPitch * sinRoll) - (sinYaw * sinPitch * cosRoll);
  840. result.w = (cosYaw * cosPitch * cosRoll) + (sinYaw * sinPitch * sinRoll);
  841. };
  842. BABYLON.Quaternion.Slerp = function (left, right, amount) {
  843. var num2;
  844. var num3;
  845. var num = amount;
  846. var num4 = (((left.x * right.x) + (left.y * right.y)) + (left.z * right.z)) + (left.w * right.w);
  847. var flag = false;
  848. if (num4 < 0) {
  849. flag = true;
  850. num4 = -num4;
  851. }
  852. if (num4 > 0.999999) {
  853. num3 = 1 - num;
  854. num2 = flag ? -num : num;
  855. }
  856. else {
  857. var num5 = Math.acos(num4);
  858. var num6 = (1.0 / Math.sin(num5));
  859. num3 = (Math.sin((1.0 - num) * num5)) * num6;
  860. num2 = flag ? ((-Math.sin(num * num5)) * num6) : ((Math.sin(num * num5)) * num6);
  861. }
  862. return new BABYLON.Quaternion((num3 * left.x) + (num2 * right.x), (num3 * left.y) + (num2 * right.y), (num3 * left.z) + (num2 * right.z), (num3 * left.w) + (num2 * right.w));
  863. };
  864. ////////////////////////////////// Matrix //////////////////////////////////
  865. if (!BABYLON.MatrixType) {
  866. BABYLON.MatrixType = (typeof Float32Array !== 'undefined') ? Float32Array : Array;
  867. }
  868. BABYLON.Matrix = function () {
  869. this.m = new BABYLON.MatrixType(16);
  870. };
  871. // Properties
  872. BABYLON.Matrix.prototype.isIdentity = function () {
  873. if (this.m[0] != 1.0 || this.m[5] != 1.0 || this.m[10] != 1.0 || this.m[15] != 1.0)
  874. return false;
  875. if (this.m[1] != 0.0 || this.m[2] != 0.0 || this.m[3] != 0.0 ||
  876. this.m[4] != 0.0 || this.m[6] != 0.0 || this.m[7] != 0.0 ||
  877. this.m[8] != 0.0 || this.m[9] != 0.0 || this.m[11] != 0.0 ||
  878. this.m[12] != 0.0 || this.m[13] != 0.0 || this.m[14] != 0.0)
  879. return false;
  880. return true;
  881. };
  882. BABYLON.Matrix.prototype.determinant = function () {
  883. var temp1 = (this.m[10] * this.m[15]) - (this.m[11] * this.m[14]);
  884. var temp2 = (this.m[9] * this.m[15]) - (this.m[11] * this.m[13]);
  885. var temp3 = (this.m[9] * this.m[14]) - (this.m[10] * this.m[13]);
  886. var temp4 = (this.m[8] * this.m[15]) - (this.m[11] * this.m[12]);
  887. var temp5 = (this.m[8] * this.m[14]) - (this.m[10] * this.m[12]);
  888. var temp6 = (this.m[8] * this.m[13]) - (this.m[9] * this.m[12]);
  889. return ((((this.m[0] * (((this.m[5] * temp1) - (this.m[6] * temp2)) + (this.m[7] * temp3))) - (this.m[1] * (((this.m[4] * temp1) -
  890. (this.m[6] * temp4)) + (this.m[7] * temp5)))) + (this.m[2] * (((this.m[4] * temp2) - (this.m[5] * temp4)) + (this.m[7] * temp6)))) -
  891. (this.m[3] * (((this.m[4] * temp3) - (this.m[5] * temp5)) + (this.m[6] * temp6))));
  892. };
  893. // Methods
  894. BABYLON.Matrix.prototype.toArray = function () {
  895. return this.m;
  896. };
  897. BABYLON.Matrix.prototype.asArray = function () {
  898. return this.toArray();
  899. };
  900. BABYLON.Matrix.prototype.invert = function () {
  901. this.invertToRef(this);
  902. };
  903. BABYLON.Matrix.prototype.invertToRef = function (other) {
  904. var l1 = this.m[0];
  905. var l2 = this.m[1];
  906. var l3 = this.m[2];
  907. var l4 = this.m[3];
  908. var l5 = this.m[4];
  909. var l6 = this.m[5];
  910. var l7 = this.m[6];
  911. var l8 = this.m[7];
  912. var l9 = this.m[8];
  913. var l10 = this.m[9];
  914. var l11 = this.m[10];
  915. var l12 = this.m[11];
  916. var l13 = this.m[12];
  917. var l14 = this.m[13];
  918. var l15 = this.m[14];
  919. var l16 = this.m[15];
  920. var l17 = (l11 * l16) - (l12 * l15);
  921. var l18 = (l10 * l16) - (l12 * l14);
  922. var l19 = (l10 * l15) - (l11 * l14);
  923. var l20 = (l9 * l16) - (l12 * l13);
  924. var l21 = (l9 * l15) - (l11 * l13);
  925. var l22 = (l9 * l14) - (l10 * l13);
  926. var l23 = ((l6 * l17) - (l7 * l18)) + (l8 * l19);
  927. var l24 = -(((l5 * l17) - (l7 * l20)) + (l8 * l21));
  928. var l25 = ((l5 * l18) - (l6 * l20)) + (l8 * l22);
  929. var l26 = -(((l5 * l19) - (l6 * l21)) + (l7 * l22));
  930. var l27 = 1.0 / ((((l1 * l23) + (l2 * l24)) + (l3 * l25)) + (l4 * l26));
  931. var l28 = (l7 * l16) - (l8 * l15);
  932. var l29 = (l6 * l16) - (l8 * l14);
  933. var l30 = (l6 * l15) - (l7 * l14);
  934. var l31 = (l5 * l16) - (l8 * l13);
  935. var l32 = (l5 * l15) - (l7 * l13);
  936. var l33 = (l5 * l14) - (l6 * l13);
  937. var l34 = (l7 * l12) - (l8 * l11);
  938. var l35 = (l6 * l12) - (l8 * l10);
  939. var l36 = (l6 * l11) - (l7 * l10);
  940. var l37 = (l5 * l12) - (l8 * l9);
  941. var l38 = (l5 * l11) - (l7 * l9);
  942. var l39 = (l5 * l10) - (l6 * l9);
  943. other.m[0] = l23 * l27;
  944. other.m[4] = l24 * l27;
  945. other.m[8] = l25 * l27;
  946. other.m[12] = l26 * l27;
  947. other.m[1] = -(((l2 * l17) - (l3 * l18)) + (l4 * l19)) * l27;
  948. other.m[5] = (((l1 * l17) - (l3 * l20)) + (l4 * l21)) * l27;
  949. other.m[9] = -(((l1 * l18) - (l2 * l20)) + (l4 * l22)) * l27;
  950. other.m[13] = (((l1 * l19) - (l2 * l21)) + (l3 * l22)) * l27;
  951. other.m[2] = (((l2 * l28) - (l3 * l29)) + (l4 * l30)) * l27;
  952. other.m[6] = -(((l1 * l28) - (l3 * l31)) + (l4 * l32)) * l27;
  953. other.m[10] = (((l1 * l29) - (l2 * l31)) + (l4 * l33)) * l27;
  954. other.m[14] = -(((l1 * l30) - (l2 * l32)) + (l3 * l33)) * l27;
  955. other.m[3] = -(((l2 * l34) - (l3 * l35)) + (l4 * l36)) * l27;
  956. other.m[7] = (((l1 * l34) - (l3 * l37)) + (l4 * l38)) * l27;
  957. other.m[11] = -(((l1 * l35) - (l2 * l37)) + (l4 * l39)) * l27;
  958. other.m[15] = (((l1 * l36) - (l2 * l38)) + (l3 * l39)) * l27;
  959. };
  960. BABYLON.Matrix.prototype.setTranslation = function (vector3) {
  961. this.m[12] = vector3.x;
  962. this.m[13] = vector3.y;
  963. this.m[14] = vector3.z;
  964. };
  965. BABYLON.Matrix.prototype.multiply = function (other) {
  966. var result = new BABYLON.Matrix();
  967. this.multiplyToRef(other, result);
  968. return result;
  969. };
  970. BABYLON.Matrix.prototype.copyFrom = function (other) {
  971. for (var index = 0; index < 16; index++) {
  972. this.m[index] = other.m[index];
  973. }
  974. };
  975. BABYLON.Matrix.prototype.multiplyToRef = function (other, result) {
  976. this.multiplyToArray(other, result.m, 0);
  977. };
  978. BABYLON.Matrix.prototype.multiplyToArray = function (other, result, offset) {
  979. var tm0 = this.m[0];
  980. var tm1 = this.m[1];
  981. var tm2 = this.m[2];
  982. var tm3 = this.m[3];
  983. var tm4 = this.m[4];
  984. var tm5 = this.m[5];
  985. var tm6 = this.m[6];
  986. var tm7 = this.m[7];
  987. var tm8 = this.m[8];
  988. var tm9 = this.m[9];
  989. var tm10 = this.m[10];
  990. var tm11 = this.m[11];
  991. var tm12 = this.m[12];
  992. var tm13 = this.m[13];
  993. var tm14 = this.m[14];
  994. var tm15 = this.m[15];
  995. var om0 = other.m[0];
  996. var om1 = other.m[1];
  997. var om2 = other.m[2];
  998. var om3 = other.m[3];
  999. var om4 = other.m[4];
  1000. var om5 = other.m[5];
  1001. var om6 = other.m[6];
  1002. var om7 = other.m[7];
  1003. var om8 = other.m[8];
  1004. var om9 = other.m[9];
  1005. var om10 = other.m[10];
  1006. var om11 = other.m[11];
  1007. var om12 = other.m[12];
  1008. var om13 = other.m[13];
  1009. var om14 = other.m[14];
  1010. var om15 = other.m[15];
  1011. result[offset] = tm0 * om0 + tm1 * om4 + tm2 * om8 + tm3 * om12;
  1012. result[offset + 1] = tm0 * om1 + tm1 * om5 + tm2 * om9 + tm3 * om13;
  1013. result[offset + 2] = tm0 * om2 + tm1 * om6 + tm2 * om10 + tm3 * om14;
  1014. result[offset + 3] = tm0 * om3 + tm1 * om7 + tm2 * om11 + tm3 * om15;
  1015. result[offset + 4] = tm4 * om0 + tm5 * om4 + tm6 * om8 + tm7 * om12;
  1016. result[offset + 5] = tm4 * om1 + tm5 * om5 + tm6 * om9 + tm7 * om13;
  1017. result[offset + 6] = tm4 * om2 + tm5 * om6 + tm6 * om10 + tm7 * om14;
  1018. result[offset + 7] = tm4 * om3 + tm5 * om7 + tm6 * om11 + tm7 * om15;
  1019. result[offset + 8] = tm8 * om0 + tm9 * om4 + tm10 * om8 + tm11 * om12;
  1020. result[offset + 9] = tm8 * om1 + tm9 * om5 + tm10 * om9 + tm11 * om13;
  1021. result[offset + 10] = tm8 * om2 + tm9 * om6 + tm10 * om10 + tm11 * om14;
  1022. result[offset + 11] = tm8 * om3 + tm9 * om7 + tm10 * om11 + tm11 * om15;
  1023. result[offset + 12] = tm12 * om0 + tm13 * om4 + tm14 * om8 + tm15 * om12;
  1024. result[offset + 13] = tm12 * om1 + tm13 * om5 + tm14 * om9 + tm15 * om13;
  1025. result[offset + 14] = tm12 * om2 + tm13 * om6 + tm14 * om10 + tm15 * om14;
  1026. result[offset + 15] = tm12 * om3 + tm13 * om7 + tm14 * om11 + tm15 * om15;
  1027. };
  1028. BABYLON.Matrix.prototype.equals = function (value) {
  1029. return value &&
  1030. (this.m[0] === value.m[0] && this.m[1] === value.m[1] && this.m[2] === value.m[2] && this.m[3] === value.m[3] &&
  1031. this.m[4] === value.m[4] && this.m[5] === value.m[5] && this.m[6] === value.m[6] && this.m[7] === value.m[7] &&
  1032. this.m[8] === value.m[8] && this.m[9] === value.m[9] && this.m[10] === value.m[10] && this.m[11] === value.m[11] &&
  1033. this.m[12] === value.m[12] && this.m[13] === value.m[13] && this.m[14] === value.m[14] && this.m[15] === value.m[15]);
  1034. };
  1035. BABYLON.Matrix.prototype.clone = function () {
  1036. return BABYLON.Matrix.FromValues(this.m[0], this.m[1], this.m[2], this.m[3],
  1037. this.m[4], this.m[5], this.m[6], this.m[7],
  1038. this.m[8], this.m[9], this.m[10], this.m[11],
  1039. this.m[12], this.m[13], this.m[14], this.m[15]);
  1040. };
  1041. // Statics
  1042. BABYLON.Matrix.FromArray = function (array, offset) {
  1043. var result = new BABYLON.Matrix();
  1044. BABYLON.Matrix.FromArrayToRef(array, offset, result);
  1045. return result;
  1046. };
  1047. BABYLON.Matrix.FromArrayToRef = function (array, offset, result) {
  1048. if (!offset) {
  1049. offset = 0;
  1050. }
  1051. for (var index = 0; index < 16; index++) {
  1052. result.m[index] = array[index + offset];
  1053. }
  1054. };
  1055. BABYLON.Matrix.FromValuesToRef = function (initialM11, initialM12, initialM13, initialM14,
  1056. initialM21, initialM22, initialM23, initialM24,
  1057. initialM31, initialM32, initialM33, initialM34,
  1058. initialM41, initialM42, initialM43, initialM44, result) {
  1059. result.m[0] = initialM11;
  1060. result.m[1] = initialM12;
  1061. result.m[2] = initialM13;
  1062. result.m[3] = initialM14;
  1063. result.m[4] = initialM21;
  1064. result.m[5] = initialM22;
  1065. result.m[6] = initialM23;
  1066. result.m[7] = initialM24;
  1067. result.m[8] = initialM31;
  1068. result.m[9] = initialM32;
  1069. result.m[10] = initialM33;
  1070. result.m[11] = initialM34;
  1071. result.m[12] = initialM41;
  1072. result.m[13] = initialM42;
  1073. result.m[14] = initialM43;
  1074. result.m[15] = initialM44;
  1075. };
  1076. BABYLON.Matrix.FromValues = function (initialM11, initialM12, initialM13, initialM14,
  1077. initialM21, initialM22, initialM23, initialM24,
  1078. initialM31, initialM32, initialM33, initialM34,
  1079. initialM41, initialM42, initialM43, initialM44) {
  1080. var result = new BABYLON.Matrix();
  1081. result.m[0] = initialM11;
  1082. result.m[1] = initialM12;
  1083. result.m[2] = initialM13;
  1084. result.m[3] = initialM14;
  1085. result.m[4] = initialM21;
  1086. result.m[5] = initialM22;
  1087. result.m[6] = initialM23;
  1088. result.m[7] = initialM24;
  1089. result.m[8] = initialM31;
  1090. result.m[9] = initialM32;
  1091. result.m[10] = initialM33;
  1092. result.m[11] = initialM34;
  1093. result.m[12] = initialM41;
  1094. result.m[13] = initialM42;
  1095. result.m[14] = initialM43;
  1096. result.m[15] = initialM44;
  1097. return result;
  1098. };
  1099. BABYLON.Matrix.Identity = function () {
  1100. return BABYLON.Matrix.FromValues(1.0, 0, 0, 0,
  1101. 0, 1.0, 0, 0,
  1102. 0, 0, 1.0, 0,
  1103. 0, 0, 0, 1.0);
  1104. };
  1105. BABYLON.Matrix.IdentityToRef = function (result) {
  1106. BABYLON.Matrix.FromValuesToRef(1.0, 0, 0, 0,
  1107. 0, 1.0, 0, 0,
  1108. 0, 0, 1.0, 0,
  1109. 0, 0, 0, 1.0, result);
  1110. };
  1111. BABYLON.Matrix.Zero = function () {
  1112. return BABYLON.Matrix.FromValues(0, 0, 0, 0,
  1113. 0, 0, 0, 0,
  1114. 0, 0, 0, 0,
  1115. 0, 0, 0, 0);
  1116. };
  1117. BABYLON.Matrix.RotationX = function (angle) {
  1118. var result = new BABYLON.Matrix();
  1119. BABYLON.Matrix.RotationXToRef(angle, result);
  1120. return result;
  1121. };
  1122. BABYLON.Matrix.RotationXToRef = function (angle, result) {
  1123. var s = Math.sin(angle);
  1124. var c = Math.cos(angle);
  1125. result.m[0] = 1.0;
  1126. result.m[15] = 1.0;
  1127. result.m[5] = c;
  1128. result.m[10] = c;
  1129. result.m[9] = -s;
  1130. result.m[6] = s;
  1131. result.m[1] = 0;
  1132. result.m[2] = 0;
  1133. result.m[3] = 0;
  1134. result.m[4] = 0;
  1135. result.m[7] = 0;
  1136. result.m[8] = 0;
  1137. result.m[11] = 0;
  1138. result.m[12] = 0;
  1139. result.m[13] = 0;
  1140. result.m[14] = 0;
  1141. };
  1142. BABYLON.Matrix.RotationY = function (angle) {
  1143. var result = new BABYLON.Matrix();
  1144. BABYLON.Matrix.RotationYToRef(angle, result);
  1145. return result;
  1146. };
  1147. BABYLON.Matrix.RotationYToRef = function (angle, result) {
  1148. var s = Math.sin(angle);
  1149. var c = Math.cos(angle);
  1150. result.m[5] = 1.0;
  1151. result.m[15] = 1.0;
  1152. result.m[0] = c;
  1153. result.m[2] = -s;
  1154. result.m[8] = s;
  1155. result.m[10] = c;
  1156. result.m[1] = 0;
  1157. result.m[3] = 0;
  1158. result.m[4] = 0;
  1159. result.m[6] = 0;
  1160. result.m[7] = 0;
  1161. result.m[9] = 0;
  1162. result.m[11] = 0;
  1163. result.m[12] = 0;
  1164. result.m[13] = 0;
  1165. result.m[14] = 0;
  1166. };
  1167. BABYLON.Matrix.RotationZ = function (angle) {
  1168. var result = new BABYLON.Matrix();
  1169. BABYLON.Matrix.RotationZToRef(angle, result);
  1170. return result;
  1171. };
  1172. BABYLON.Matrix.RotationZToRef = function (angle, result) {
  1173. var s = Math.sin(angle);
  1174. var c = Math.cos(angle);
  1175. result.m[10] = 1.0;
  1176. result.m[15] = 1.0;
  1177. result.m[0] = c;
  1178. result.m[1] = s;
  1179. result.m[4] = -s;
  1180. result.m[5] = c;
  1181. result.m[2] = 0;
  1182. result.m[3] = 0;
  1183. result.m[6] = 0;
  1184. result.m[7] = 0;
  1185. result.m[8] = 0;
  1186. result.m[9] = 0;
  1187. result.m[11] = 0;
  1188. result.m[12] = 0;
  1189. result.m[13] = 0;
  1190. result.m[14] = 0;
  1191. };
  1192. BABYLON.Matrix.RotationAxis = function (axis, angle) {
  1193. var s = Math.sin(-angle);
  1194. var c = Math.cos(-angle);
  1195. var c1 = 1 - c;
  1196. axis.normalize();
  1197. var result = BABYLON.Matrix.Zero();
  1198. result.m[0] = (axis.x * axis.x) * c1 + c;
  1199. result.m[1] = (axis.x * axis.y) * c1 - (axis.z * s);
  1200. result.m[2] = (axis.x * axis.z) * c1 + (axis.y * s);
  1201. result.m[3] = 0.0;
  1202. result.m[4] = (axis.y * axis.x) * c1 + (axis.z * s);
  1203. result.m[5] = (axis.y * axis.y) * c1 + c;
  1204. result.m[6] = (axis.y * axis.z) * c1 - (axis.x * s);
  1205. result.m[7] = 0.0;
  1206. result.m[8] = (axis.z * axis.x) * c1 - (axis.y * s);
  1207. result.m[9] = (axis.z * axis.y) * c1 + (axis.x * s);
  1208. result.m[10] = (axis.z * axis.z) * c1 + c;
  1209. result.m[11] = 0.0;
  1210. result.m[15] = 1.0;
  1211. return result;
  1212. };
  1213. BABYLON.Matrix.RotationYawPitchRoll = function (yaw, pitch, roll) {
  1214. var result = new BABYLON.Matrix();
  1215. BABYLON.Matrix.RotationYawPitchRollToRef(yaw, pitch, roll, result);
  1216. return result;
  1217. };
  1218. var tempQuaternion = new BABYLON.Quaternion(); // For RotationYawPitchRoll
  1219. BABYLON.Matrix.RotationYawPitchRollToRef = function (yaw, pitch, roll, result) {
  1220. BABYLON.Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, tempQuaternion);
  1221. tempQuaternion.toRotationMatrix(result);
  1222. };
  1223. BABYLON.Matrix.Scaling = function (x, y, z) {
  1224. var result = BABYLON.Matrix.Zero();
  1225. BABYLON.Matrix.ScalingToRef(x, y, z, result);
  1226. return result;
  1227. };
  1228. BABYLON.Matrix.ScalingToRef = function (x, y, z, result) {
  1229. result.m[0] = x;
  1230. result.m[1] = 0;
  1231. result.m[2] = 0;
  1232. result.m[3] = 0;
  1233. result.m[4] = 0;
  1234. result.m[5] = y;
  1235. result.m[6] = 0;
  1236. result.m[7] = 0;
  1237. result.m[8] = 0;
  1238. result.m[9] = 0;
  1239. result.m[10] = z;
  1240. result.m[11] = 0;
  1241. result.m[12] = 0;
  1242. result.m[13] = 0;
  1243. result.m[14] = 0;
  1244. result.m[15] = 1.0;
  1245. };
  1246. BABYLON.Matrix.Translation = function (x, y, z) {
  1247. var result = BABYLON.Matrix.Identity();
  1248. BABYLON.Matrix.TranslationToRef(x, y, z, result);
  1249. return result;
  1250. };
  1251. BABYLON.Matrix.TranslationToRef = function (x, y, z, result) {
  1252. BABYLON.Matrix.FromValuesToRef(1.0, 0, 0, 0,
  1253. 0, 1.0, 0, 0,
  1254. 0, 0, 1.0, 0,
  1255. x, y, z, 1.0, result);
  1256. };
  1257. BABYLON.Matrix.LookAtLH = function (eye, target, up) {
  1258. var result = BABYLON.Matrix.Zero();
  1259. BABYLON.Matrix.LookAtLHToRef(eye, target, up, result);
  1260. return result;
  1261. };
  1262. var xAxis = BABYLON.Vector3.Zero();
  1263. var yAxis = BABYLON.Vector3.Zero();
  1264. var zAxis = BABYLON.Vector3.Zero();
  1265. BABYLON.Matrix.LookAtLHToRef = function (eye, target, up, result) {
  1266. // Z axis
  1267. target.subtractToRef(eye, zAxis);
  1268. zAxis.normalize();
  1269. // X axis
  1270. BABYLON.Vector3.CrossToRef(up, zAxis, xAxis);
  1271. xAxis.normalize();
  1272. // Y axis
  1273. BABYLON.Vector3.CrossToRef(zAxis, xAxis, yAxis);
  1274. yAxis.normalize();
  1275. // Eye angles
  1276. var ex = -BABYLON.Vector3.Dot(xAxis, eye);
  1277. var ey = -BABYLON.Vector3.Dot(yAxis, eye);
  1278. var ez = -BABYLON.Vector3.Dot(zAxis, eye);
  1279. return BABYLON.Matrix.FromValuesToRef(xAxis.x, yAxis.x, zAxis.x, 0,
  1280. xAxis.y, yAxis.y, zAxis.y, 0,
  1281. xAxis.z, yAxis.z, zAxis.z, 0,
  1282. ex, ey, ez, 1, result);
  1283. };
  1284. BABYLON.Matrix.OrthoLH = function (width, height, znear, zfar) {
  1285. var hw = 2.0 / width;
  1286. var hh = 2.0 / height;
  1287. var id = 1.0 / (zfar - znear);
  1288. var nid = znear / (znear - zfar);
  1289. return BABYLON.Matrix.FromValues(hw, 0, 0, 0,
  1290. 0, hh, 0, 0,
  1291. 0, 0, id, 0,
  1292. 0, 0, nid, 1);
  1293. };
  1294. BABYLON.Matrix.OrthoOffCenterLH = function (left, right, bottom, top, znear, zfar) {
  1295. var matrix = BABYLON.Matrix.Zero();
  1296. BABYLON.Matrix.OrthoOffCenterLHToRef(left, right, bottom, top, znear, zfar, matrix);
  1297. return matrix;
  1298. };
  1299. BABYLON.Matrix.OrthoOffCenterLHToRef = function (left, right, bottom, top, znear, zfar, result) {
  1300. result.m[0] = 2.0 / (right - left);
  1301. result.m[1] = result.m[2] = result.m[3] = 0;
  1302. result.m[5] = 2.0 / (top - bottom);
  1303. result.m[4] = result.m[6] = result.m[7] = 0;
  1304. result.m[10] = -1.0 / (znear - zfar);
  1305. result.m[8] = result.m[9] = result.m[11] = 0;
  1306. result.m[12] = (left + right) / (left - right);
  1307. result.m[13] = (top + bottom) / (bottom - top);
  1308. result.m[14] = znear / (znear - zfar);
  1309. result.m[15] = 1.0;
  1310. };
  1311. BABYLON.Matrix.PerspectiveLH = function (width, height, znear, zfar) {
  1312. var matrix = BABYLON.Matrix.Zero();
  1313. matrix.m[0] = (2.0 * znear) / width;
  1314. matrix.m[1] = matrix.m[2] = matrix.m[3] = 0.0;
  1315. matrix.m[5] = (2.0 * znear) / height;
  1316. matrix.m[4] = matrix.m[6] = matrix.m[7] = 0.0;
  1317. matrix.m[10] = -zfar / (znear - zfar);
  1318. matrix.m[8] = matrix.m[9] = 0.0;
  1319. matrix.m[11] = 1.0;
  1320. matrix.m[12] = matrix.m[13] = matrix.m[15] = 0.0;
  1321. matrix.m[14] = (znear * zfar) / (znear - zfar);
  1322. return matrix;
  1323. };
  1324. BABYLON.Matrix.PerspectiveFovLH = function (fov, aspect, znear, zfar) {
  1325. var matrix = BABYLON.Matrix.Zero();
  1326. BABYLON.Matrix.PerspectiveFovLHToRef(fov, aspect, znear, zfar, matrix);
  1327. return matrix;
  1328. };
  1329. BABYLON.Matrix.PerspectiveFovLHToRef = function (fov, aspect, znear, zfar, result) {
  1330. var tan = 1.0 / (Math.tan(fov * 0.5));
  1331. result.m[0] = tan / aspect;
  1332. result.m[1] = result.m[2] = result.m[3] = 0.0;
  1333. result.m[5] = tan;
  1334. result.m[4] = result.m[6] = result.m[7] = 0.0;
  1335. result.m[8] = result.m[9] = 0.0;
  1336. result.m[10] = -zfar / (znear - zfar);
  1337. result.m[11] = 1.0;
  1338. result.m[12] = result.m[13] = result.m[15] = 0.0;
  1339. result.m[14] = (znear * zfar) / (znear - zfar);
  1340. };
  1341. BABYLON.Matrix.AffineTransformation = function (scaling, rotationCenter, rotation, translation) {
  1342. return BABYLON.Matrix.Scaling(scaling, scaling, scaling) * BABYLON.Matrix.Translation(-rotationCenter) *
  1343. BABYLON.Matrix.RotationQuaternion(rotation) * BABYLON.Matrix.Translation(rotationCenter) * BABYLON.Matrix.Translation(translation);
  1344. };
  1345. BABYLON.Matrix.GetFinalMatrix = function (viewport, world, view, projection, zmin, zmax) {
  1346. var cw = viewport.width;
  1347. var ch = viewport.height;
  1348. var cx = viewport.x;
  1349. var cy = viewport.y;
  1350. var viewportMatrix = new BABYLON.Matrix(cw / 2.0, 0, 0, 0,
  1351. 0, -ch / 2.0, 0, 0,
  1352. 0, 0, zmax - zmin, 0,
  1353. cx + cw / 2.0, ch / 2.0 + cy, zmin, 1);
  1354. return world.multiply(view).multiply(projection).multiply(viewportMatrix);
  1355. };
  1356. BABYLON.Matrix.Transpose = function (matrix) {
  1357. var result = new BABYLON.Matrix();
  1358. result.m[0] = matrix.m[0];
  1359. result.m[1] = matrix.m[4];
  1360. result.m[2] = matrix.m[8];
  1361. result.m[3] = matrix.m[12];
  1362. result.m[4] = matrix.m[1];
  1363. result.m[5] = matrix.m[5];
  1364. result.m[6] = matrix.m[9];
  1365. result.m[7] = matrix.m[13];
  1366. result.m[8] = matrix.m[2];
  1367. result.m[9] = matrix.m[6];
  1368. result.m[10] = matrix.m[10];
  1369. result.m[11] = matrix.m[14];
  1370. result.m[12] = matrix.m[3];
  1371. result.m[13] = matrix.m[7];
  1372. result.m[14] = matrix.m[11];
  1373. result.m[15] = matrix.m[15];
  1374. return result;
  1375. };
  1376. BABYLON.Matrix.Reflection = function (plane) {
  1377. var matrix = new BABYLON.Matrix();
  1378. BABYLON.Matrix.ReflectionToRef(plane, matrix);
  1379. return matrix;
  1380. };
  1381. BABYLON.Matrix.ReflectionToRef = function (plane, result) {
  1382. plane.normalize();
  1383. var x = plane.normal.x;
  1384. var y = plane.normal.y;
  1385. var z = plane.normal.z;
  1386. var temp = -2 * x;
  1387. var temp2 = -2 * y;
  1388. var temp3 = -2 * z;
  1389. result.m[0] = (temp * x) + 1;
  1390. result.m[1] = temp2 * x;
  1391. result.m[2] = temp3 * x;
  1392. result.m[3] = 0.0;
  1393. result.m[4] = temp * y;
  1394. result.m[5] = (temp2 * y) + 1;
  1395. result.m[6] = temp3 * y;
  1396. result.m[7] = 0.0;
  1397. result.m[8] = temp * z;
  1398. result.m[9] = temp2 * z;
  1399. result.m[10] = (temp3 * z) + 1;
  1400. result.m[11] = 0.0;
  1401. result.m[12] = temp * plane.d;
  1402. result.m[13] = temp2 * plane.d;
  1403. result.m[14] = temp3 * plane.d;
  1404. result.m[15] = 1.0;
  1405. };
  1406. ////////////////////////////////// Plane //////////////////////////////////
  1407. BABYLON.Plane = function (a, b, c, d) {
  1408. this.normal = new BABYLON.Vector3(a, b, c);
  1409. this.d = d;
  1410. };
  1411. BABYLON.Plane.prototype.asArray = function () {
  1412. return [this.normal.x, this.normal.y, this.normal.z, this.d];
  1413. };
  1414. // Methods
  1415. BABYLON.Plane.prototype.normalize = function () {
  1416. var norm = (Math.sqrt((this.normal.x * this.normal.x) + (this.normal.y * this.normal.y) + (this.normal.z * this.normal.z)));
  1417. var magnitude = 0;
  1418. if (norm != 0) {
  1419. magnitude = 1.0 / norm;
  1420. }
  1421. this.normal.x *= magnitude;
  1422. this.normal.y *= magnitude;
  1423. this.normal.z *= magnitude;
  1424. this.d *= magnitude;
  1425. };
  1426. BABYLON.Plane.prototype.transform = function (transformation) {
  1427. var transposedMatrix = BABYLON.Matrix.Transpose(transformation);
  1428. var x = this.normal.x;
  1429. var y = this.normal.y;
  1430. var z = this.normal.z;
  1431. var d = this.d;
  1432. var normalX = (((x * transposedMatrix.m[0]) + (y * transposedMatrix.m[1])) + (z * transposedMatrix.m[2])) + (d * transposedMatrix.m[3]);
  1433. var normalY = (((x * transposedMatrix.m[4]) + (y * transposedMatrix.m[5])) + (z * transposedMatrix.m[6])) + (d * transposedMatrix.m[7]);
  1434. var normalZ = (((x * transposedMatrix.m[8]) + (y * transposedMatrix.m[9])) + (z * transposedMatrix.m[10])) + (d * transposedMatrix.m[11]);
  1435. var finalD = (((x * transposedMatrix.m[12]) + (y * transposedMatrix.m[13])) + (z * transposedMatrix.m[14])) + (d * transposedMatrix.m[15]);
  1436. return new BABYLON.Plane(normalX, normalY, normalZ, finalD);
  1437. };
  1438. BABYLON.Plane.prototype.dotCoordinate = function (point) {
  1439. return ((((this.normal.x * point.x) + (this.normal.y * point.y)) + (this.normal.z * point.z)) + this.d);
  1440. };
  1441. BABYLON.Plane.prototype.copyFromPoints = function (point1, point2, point3) {
  1442. var x1 = point2.x - point1.x;
  1443. var y1 = point2.y - point1.y;
  1444. var z1 = point2.z - point1.z;
  1445. var x2 = point3.x - point1.x;
  1446. var y2 = point3.y - point1.y;
  1447. var z2 = point3.z - point1.z;
  1448. var yz = (y1 * z2) - (z1 * y2);
  1449. var xz = (z1 * x2) - (x1 * z2);
  1450. var xy = (x1 * y2) - (y1 * x2);
  1451. var pyth = (Math.sqrt((yz * yz) + (xz * xz) + (xy * xy)));
  1452. var invPyth;
  1453. if (pyth != 0) {
  1454. invPyth = 1.0 / pyth;
  1455. }
  1456. else {
  1457. invPyth = 0;
  1458. }
  1459. this.normal.x = yz * invPyth;
  1460. this.normal.y = xz * invPyth;
  1461. this.normal.z = xy * invPyth;
  1462. this.d = -((this.normal.x * point1.x) + (this.normal.y * point1.y) + (this.normal.z * point1.z));
  1463. };
  1464. BABYLON.Plane.prototype.isFrontFacingTo = function (direction, epsilon) {
  1465. var dot = BABYLON.Vector3.Dot(this.normal, direction);
  1466. return (dot <= epsilon);
  1467. };
  1468. BABYLON.Plane.prototype.signedDistanceTo = function (point) {
  1469. return BABYLON.Vector3.Dot(point, this.normal) + this.d;
  1470. };
  1471. // Statics
  1472. BABYLON.Plane.FromArray = function (array) {
  1473. return new BABYLON.Plane(array[0], array[1], array[2], array[3]);
  1474. };
  1475. BABYLON.Plane.FromPoints = function (point1, point2, point3) {
  1476. var result = new BABYLON.Plane(0, 0, 0, 0);
  1477. result.copyFromPoints(point1, point2, point3);
  1478. return result;
  1479. };
  1480. BABYLON.Plane.FromPositionAndNormal = function (origin, normal) {
  1481. var result = new BABYLON.Plane(0, 0, 0, 0);
  1482. normal.normalize();
  1483. result.normal = normal;
  1484. result.d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  1485. return result;
  1486. };
  1487. BABYLON.Plane.SignedDistanceToPlaneFromPositionAndNormal = function (origin, normal, point) {
  1488. var d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  1489. return BABYLON.Vector3.Dot(point, normal) + d;
  1490. };
  1491. ////////////////////////////////// Frustum //////////////////////////////////
  1492. BABYLON.Frustum = {};
  1493. // Statics
  1494. BABYLON.Frustum.GetPlanes = function (transform) {
  1495. var frustumPlanes = [];
  1496. for (var index = 0; index < 6; index++) {
  1497. frustumPlanes.push(new BABYLON.Plane(0, 0, 0, 0));
  1498. }
  1499. BABYLON.Frustum.GetPlanesToRef(transform, frustumPlanes);
  1500. return frustumPlanes;
  1501. };
  1502. BABYLON.Frustum.GetPlanesToRef = function (transform, frustumPlanes) {
  1503. // Near
  1504. frustumPlanes[0].normal.x = transform.m[3] + transform.m[2];
  1505. frustumPlanes[0].normal.y = transform.m[7] + transform.m[6];
  1506. frustumPlanes[0].normal.z = transform.m[10] + transform.m[10];
  1507. frustumPlanes[0].d = transform.m[15] + transform.m[14];
  1508. frustumPlanes[0].normalize();
  1509. // Far
  1510. frustumPlanes[1].normal.x = transform.m[3] - transform.m[2];
  1511. frustumPlanes[1].normal.y = transform.m[7] - transform.m[6];
  1512. frustumPlanes[1].normal.z = transform.m[11] - transform.m[10];
  1513. frustumPlanes[1].d = transform.m[15] - transform.m[14];
  1514. frustumPlanes[1].normalize();
  1515. // Left
  1516. frustumPlanes[2].normal.x = transform.m[3] + transform.m[0];
  1517. frustumPlanes[2].normal.y = transform.m[7] + transform.m[4];
  1518. frustumPlanes[2].normal.z = transform.m[11] + transform.m[8];
  1519. frustumPlanes[2].d = transform.m[15] + transform.m[12];
  1520. frustumPlanes[2].normalize();
  1521. // Right
  1522. frustumPlanes[3].normal.x = transform.m[3] - transform.m[0];
  1523. frustumPlanes[3].normal.y = transform.m[7] - transform.m[4];
  1524. frustumPlanes[3].normal.z = transform.m[11] - transform.m[8];
  1525. frustumPlanes[3].d = transform.m[15] - transform.m[12];
  1526. frustumPlanes[3].normalize();
  1527. // Top
  1528. frustumPlanes[4].normal.x = transform.m[3] - transform.m[1];
  1529. frustumPlanes[4].normal.y = transform.m[7] - transform.m[5];
  1530. frustumPlanes[4].normal.z = transform.m[11] - transform.m[9];
  1531. frustumPlanes[4].d = transform.m[15] - transform.m[13];
  1532. frustumPlanes[4].normalize();
  1533. // Bottom
  1534. frustumPlanes[5].normal.x = transform.m[3] + transform.m[1];
  1535. frustumPlanes[5].normal.y = transform.m[7] + transform.m[5];
  1536. frustumPlanes[5].normal.z = transform.m[11] + transform.m[9];
  1537. frustumPlanes[5].d = transform.m[15] + transform.m[13];
  1538. frustumPlanes[5].normalize();
  1539. };
  1540. ////////////////////////////////// Viewport //////////////////////////////////
  1541. BABYLON.Viewport = {};
  1542. BABYLON.Viewport = function (x, y, width, height) {
  1543. this.width = width;
  1544. this.height = height;
  1545. this.x = x;
  1546. this.y = y;
  1547. };
  1548. BABYLON.Viewport.prototype.toGlobal = function (engine) {
  1549. var width = engine.getRenderWidth() * engine.getHardwareScalingLevel();
  1550. var height = engine.getRenderHeight() * engine.getHardwareScalingLevel();
  1551. return new BABYLON.Viewport(this.x * width, this.y * height, this.width * width, this.height * height);
  1552. };
  1553. })();