babylon.math.js 89 KB

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