babylon.math.js 69 KB

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