babylon.math.js 71 KB

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