babylon.math.js 73 KB

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