babylon.math.js 73 KB

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