babylon.math.js 127 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var Color3 = (function () {
  4. function Color3(r, g, b) {
  5. if (r === void 0) { r = 0; }
  6. if (g === void 0) { g = 0; }
  7. if (b === void 0) { 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. return this;
  24. };
  25. Color3.prototype.toColor4 = function (alpha) {
  26. if (alpha === void 0) { alpha = 1; }
  27. return new Color4(this.r, this.g, this.b, alpha);
  28. };
  29. Color3.prototype.asArray = function () {
  30. var result = [];
  31. this.toArray(result, 0);
  32. return result;
  33. };
  34. Color3.prototype.toLuminance = function () {
  35. return this.r * 0.3 + this.g * 0.59 + this.b * 0.11;
  36. };
  37. Color3.prototype.multiply = function (otherColor) {
  38. return new Color3(this.r * otherColor.r, this.g * otherColor.g, this.b * otherColor.b);
  39. };
  40. Color3.prototype.multiplyToRef = function (otherColor, result) {
  41. result.r = this.r * otherColor.r;
  42. result.g = this.g * otherColor.g;
  43. result.b = this.b * otherColor.b;
  44. return this;
  45. };
  46. Color3.prototype.equals = function (otherColor) {
  47. return otherColor && this.r === otherColor.r && this.g === otherColor.g && this.b === otherColor.b;
  48. };
  49. Color3.prototype.scale = function (scale) {
  50. return new Color3(this.r * scale, this.g * scale, this.b * scale);
  51. };
  52. Color3.prototype.scaleToRef = function (scale, result) {
  53. result.r = this.r * scale;
  54. result.g = this.g * scale;
  55. result.b = this.b * scale;
  56. return this;
  57. };
  58. Color3.prototype.add = function (otherColor) {
  59. return new Color3(this.r + otherColor.r, this.g + otherColor.g, this.b + otherColor.b);
  60. };
  61. Color3.prototype.addToRef = function (otherColor, result) {
  62. result.r = this.r + otherColor.r;
  63. result.g = this.g + otherColor.g;
  64. result.b = this.b + otherColor.b;
  65. return this;
  66. };
  67. Color3.prototype.subtract = function (otherColor) {
  68. return new Color3(this.r - otherColor.r, this.g - otherColor.g, this.b - otherColor.b);
  69. };
  70. Color3.prototype.subtractToRef = function (otherColor, result) {
  71. result.r = this.r - otherColor.r;
  72. result.g = this.g - otherColor.g;
  73. result.b = this.b - otherColor.b;
  74. return this;
  75. };
  76. Color3.prototype.clone = function () {
  77. return new Color3(this.r, this.g, this.b);
  78. };
  79. Color3.prototype.copyFrom = function (source) {
  80. this.r = source.r;
  81. this.g = source.g;
  82. this.b = source.b;
  83. return this;
  84. };
  85. Color3.prototype.copyFromFloats = function (r, g, b) {
  86. this.r = r;
  87. this.g = g;
  88. this.b = b;
  89. return this;
  90. };
  91. // Statics
  92. Color3.FromArray = function (array, offset) {
  93. if (offset === void 0) { offset = 0; }
  94. return new Color3(array[offset], array[offset + 1], array[offset + 2]);
  95. };
  96. Color3.FromInts = function (r, g, b) {
  97. return new Color3(r / 255.0, g / 255.0, b / 255.0);
  98. };
  99. Color3.Lerp = function (start, end, amount) {
  100. var r = start.r + ((end.r - start.r) * amount);
  101. var g = start.g + ((end.g - start.g) * amount);
  102. var b = start.b + ((end.b - start.b) * amount);
  103. return new Color3(r, g, b);
  104. };
  105. Color3.Red = function () {
  106. return new Color3(1, 0, 0);
  107. };
  108. Color3.Green = function () {
  109. return new Color3(0, 1, 0);
  110. };
  111. Color3.Blue = function () {
  112. return new Color3(0, 0, 1);
  113. };
  114. Color3.Black = function () {
  115. return new Color3(0, 0, 0);
  116. };
  117. Color3.White = function () {
  118. return new Color3(1, 1, 1);
  119. };
  120. Color3.Purple = function () {
  121. return new Color3(0.5, 0, 0.5);
  122. };
  123. Color3.Magenta = function () {
  124. return new Color3(1, 0, 1);
  125. };
  126. Color3.Yellow = function () {
  127. return new Color3(1, 1, 0);
  128. };
  129. Color3.Gray = function () {
  130. return new Color3(0.5, 0.5, 0.5);
  131. };
  132. return Color3;
  133. })();
  134. BABYLON.Color3 = Color3;
  135. var Color4 = (function () {
  136. function Color4(r, g, b, a) {
  137. this.r = r;
  138. this.g = g;
  139. this.b = b;
  140. this.a = a;
  141. }
  142. // Operators
  143. Color4.prototype.addInPlace = function (right) {
  144. this.r += right.r;
  145. this.g += right.g;
  146. this.b += right.b;
  147. this.a += right.a;
  148. return this;
  149. };
  150. Color4.prototype.asArray = function () {
  151. var result = [];
  152. this.toArray(result, 0);
  153. return result;
  154. };
  155. Color4.prototype.toArray = function (array, index) {
  156. if (index === undefined) {
  157. index = 0;
  158. }
  159. array[index] = this.r;
  160. array[index + 1] = this.g;
  161. array[index + 2] = this.b;
  162. array[index + 3] = this.a;
  163. return this;
  164. };
  165. Color4.prototype.add = function (right) {
  166. return new Color4(this.r + right.r, this.g + right.g, this.b + right.b, this.a + right.a);
  167. };
  168. Color4.prototype.subtract = function (right) {
  169. return new Color4(this.r - right.r, this.g - right.g, this.b - right.b, this.a - right.a);
  170. };
  171. Color4.prototype.subtractToRef = function (right, result) {
  172. result.r = this.r - right.r;
  173. result.g = this.g - right.g;
  174. result.b = this.b - right.b;
  175. result.a = this.a - right.a;
  176. return this;
  177. };
  178. Color4.prototype.scale = function (scale) {
  179. return new Color4(this.r * scale, this.g * scale, this.b * scale, this.a * scale);
  180. };
  181. Color4.prototype.scaleToRef = function (scale, result) {
  182. result.r = this.r * scale;
  183. result.g = this.g * scale;
  184. result.b = this.b * scale;
  185. result.a = this.a * scale;
  186. return this;
  187. };
  188. Color4.prototype.toString = function () {
  189. return "{R: " + this.r + " G:" + this.g + " B:" + this.b + " A:" + this.a + "}";
  190. };
  191. Color4.prototype.clone = function () {
  192. return new Color4(this.r, this.g, this.b, this.a);
  193. };
  194. Color4.prototype.copyFrom = function (source) {
  195. this.r = source.r;
  196. this.g = source.g;
  197. this.b = source.b;
  198. this.a = source.a;
  199. return this;
  200. };
  201. // Statics
  202. Color4.Lerp = function (left, right, amount) {
  203. var result = new Color4(0, 0, 0, 0);
  204. Color4.LerpToRef(left, right, amount, result);
  205. return result;
  206. };
  207. Color4.LerpToRef = function (left, right, amount, result) {
  208. result.r = left.r + (right.r - left.r) * amount;
  209. result.g = left.g + (right.g - left.g) * amount;
  210. result.b = left.b + (right.b - left.b) * amount;
  211. result.a = left.a + (right.a - left.a) * amount;
  212. };
  213. Color4.FromArray = function (array, offset) {
  214. if (offset === void 0) { offset = 0; }
  215. return new Color4(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  216. };
  217. Color4.FromInts = function (r, g, b, a) {
  218. return new Color4(r / 255.0, g / 255.0, b / 255.0, a / 255.0);
  219. };
  220. return Color4;
  221. })();
  222. BABYLON.Color4 = Color4;
  223. var Vector2 = (function () {
  224. function Vector2(x, y) {
  225. this.x = x;
  226. this.y = y;
  227. }
  228. Vector2.prototype.toString = function () {
  229. return "{X: " + this.x + " Y:" + this.y + "}";
  230. };
  231. // Operators
  232. Vector2.prototype.toArray = function (array, index) {
  233. if (index === void 0) { index = 0; }
  234. array[index] = this.x;
  235. array[index + 1] = this.y;
  236. return this;
  237. };
  238. Vector2.prototype.asArray = function () {
  239. var result = [];
  240. this.toArray(result, 0);
  241. return result;
  242. };
  243. Vector2.prototype.copyFrom = function (source) {
  244. this.x = source.x;
  245. this.y = source.y;
  246. return this;
  247. };
  248. Vector2.prototype.copyFromFloats = function (x, y) {
  249. this.x = x;
  250. this.y = y;
  251. return this;
  252. };
  253. Vector2.prototype.add = function (otherVector) {
  254. return new Vector2(this.x + otherVector.x, this.y + otherVector.y);
  255. };
  256. Vector2.prototype.addVector3 = function (otherVector) {
  257. return new Vector2(this.x + otherVector.x, this.y + otherVector.y);
  258. };
  259. Vector2.prototype.subtract = function (otherVector) {
  260. return new Vector2(this.x - otherVector.x, this.y - otherVector.y);
  261. };
  262. Vector2.prototype.subtractInPlace = function (otherVector) {
  263. this.x -= otherVector.x;
  264. this.y -= otherVector.y;
  265. return this;
  266. };
  267. Vector2.prototype.multiplyInPlace = function (otherVector) {
  268. this.x *= otherVector.x;
  269. this.y *= otherVector.y;
  270. return this;
  271. };
  272. Vector2.prototype.multiply = function (otherVector) {
  273. return new Vector2(this.x * otherVector.x, this.y * otherVector.y);
  274. };
  275. Vector2.prototype.multiplyToRef = function (otherVector, result) {
  276. result.x = this.x * otherVector.x;
  277. result.y = this.y * otherVector.y;
  278. return this;
  279. };
  280. Vector2.prototype.multiplyByFloats = function (x, y) {
  281. return new Vector2(this.x * x, this.y * y);
  282. };
  283. Vector2.prototype.divide = function (otherVector) {
  284. return new Vector2(this.x / otherVector.x, this.y / otherVector.y);
  285. };
  286. Vector2.prototype.divideToRef = function (otherVector, result) {
  287. result.x = this.x / otherVector.x;
  288. result.y = this.y / otherVector.y;
  289. return this;
  290. };
  291. Vector2.prototype.negate = function () {
  292. return new Vector2(-this.x, -this.y);
  293. };
  294. Vector2.prototype.scaleInPlace = function (scale) {
  295. this.x *= scale;
  296. this.y *= scale;
  297. return this;
  298. };
  299. Vector2.prototype.scale = function (scale) {
  300. return new Vector2(this.x * scale, this.y * scale);
  301. };
  302. Vector2.prototype.equals = function (otherVector) {
  303. return otherVector && this.x === otherVector.x && this.y === otherVector.y;
  304. };
  305. // Properties
  306. Vector2.prototype.length = function () {
  307. return Math.sqrt(this.x * this.x + this.y * this.y);
  308. };
  309. Vector2.prototype.lengthSquared = function () {
  310. return (this.x * this.x + this.y * this.y);
  311. };
  312. // Methods
  313. Vector2.prototype.normalize = function () {
  314. var len = this.length();
  315. if (len === 0)
  316. return this;
  317. var num = 1.0 / len;
  318. this.x *= num;
  319. this.y *= num;
  320. return this;
  321. };
  322. Vector2.prototype.clone = function () {
  323. return new Vector2(this.x, this.y);
  324. };
  325. // Statics
  326. Vector2.Zero = function () {
  327. return new Vector2(0, 0);
  328. };
  329. Vector2.FromArray = function (array, offset) {
  330. if (offset === void 0) { offset = 0; }
  331. return new Vector2(array[offset], array[offset + 1]);
  332. };
  333. Vector2.FromArrayToRef = function (array, offset, result) {
  334. result.x = array[offset];
  335. result.y = array[offset + 1];
  336. };
  337. Vector2.CatmullRom = function (value1, value2, value3, value4, amount) {
  338. var squared = amount * amount;
  339. var cubed = amount * squared;
  340. 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));
  341. 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));
  342. return new Vector2(x, y);
  343. };
  344. Vector2.Clamp = function (value, min, max) {
  345. var x = value.x;
  346. x = (x > max.x) ? max.x : x;
  347. x = (x < min.x) ? min.x : x;
  348. var y = value.y;
  349. y = (y > max.y) ? max.y : y;
  350. y = (y < min.y) ? min.y : y;
  351. return new Vector2(x, y);
  352. };
  353. Vector2.Hermite = function (value1, tangent1, value2, tangent2, amount) {
  354. var squared = amount * amount;
  355. var cubed = amount * squared;
  356. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  357. var part2 = (-2.0 * cubed) + (3.0 * squared);
  358. var part3 = (cubed - (2.0 * squared)) + amount;
  359. var part4 = cubed - squared;
  360. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  361. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  362. return new Vector2(x, y);
  363. };
  364. Vector2.Lerp = function (start, end, amount) {
  365. var x = start.x + ((end.x - start.x) * amount);
  366. var y = start.y + ((end.y - start.y) * amount);
  367. return new Vector2(x, y);
  368. };
  369. Vector2.Dot = function (left, right) {
  370. return left.x * right.x + left.y * right.y;
  371. };
  372. Vector2.Normalize = function (vector) {
  373. var newVector = vector.clone();
  374. newVector.normalize();
  375. return newVector;
  376. };
  377. Vector2.Minimize = function (left, right) {
  378. var x = (left.x < right.x) ? left.x : right.x;
  379. var y = (left.y < right.y) ? left.y : right.y;
  380. return new Vector2(x, y);
  381. };
  382. Vector2.Maximize = function (left, right) {
  383. var x = (left.x > right.x) ? left.x : right.x;
  384. var y = (left.y > right.y) ? left.y : right.y;
  385. return new Vector2(x, y);
  386. };
  387. Vector2.Transform = function (vector, transformation) {
  388. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]);
  389. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]);
  390. return new Vector2(x, y);
  391. };
  392. Vector2.Distance = function (value1, value2) {
  393. return Math.sqrt(Vector2.DistanceSquared(value1, value2));
  394. };
  395. Vector2.DistanceSquared = function (value1, value2) {
  396. var x = value1.x - value2.x;
  397. var y = value1.y - value2.y;
  398. return (x * x) + (y * y);
  399. };
  400. return Vector2;
  401. })();
  402. BABYLON.Vector2 = Vector2;
  403. var Vector3 = (function () {
  404. function Vector3(x, y, z) {
  405. this.x = x;
  406. this.y = y;
  407. this.z = z;
  408. }
  409. Vector3.prototype.toString = function () {
  410. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + "}";
  411. };
  412. // Operators
  413. Vector3.prototype.asArray = function () {
  414. var result = [];
  415. this.toArray(result, 0);
  416. return result;
  417. };
  418. Vector3.prototype.toArray = function (array, index) {
  419. if (index === void 0) { index = 0; }
  420. array[index] = this.x;
  421. array[index + 1] = this.y;
  422. array[index + 2] = this.z;
  423. return this;
  424. };
  425. Vector3.prototype.toQuaternion = function () {
  426. var result = new Quaternion(0, 0, 0, 1);
  427. var cosxPlusz = Math.cos((this.x + this.z) * 0.5);
  428. var sinxPlusz = Math.sin((this.x + this.z) * 0.5);
  429. var coszMinusx = Math.cos((this.z - this.x) * 0.5);
  430. var sinzMinusx = Math.sin((this.z - this.x) * 0.5);
  431. var cosy = Math.cos(this.y * 0.5);
  432. var siny = Math.sin(this.y * 0.5);
  433. result.x = coszMinusx * siny;
  434. result.y = -sinzMinusx * siny;
  435. result.z = sinxPlusz * cosy;
  436. result.w = cosxPlusz * cosy;
  437. return result;
  438. };
  439. Vector3.prototype.addInPlace = function (otherVector) {
  440. this.x += otherVector.x;
  441. this.y += otherVector.y;
  442. this.z += otherVector.z;
  443. return this;
  444. };
  445. Vector3.prototype.add = function (otherVector) {
  446. return new Vector3(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z);
  447. };
  448. Vector3.prototype.addToRef = function (otherVector, result) {
  449. result.x = this.x + otherVector.x;
  450. result.y = this.y + otherVector.y;
  451. result.z = this.z + otherVector.z;
  452. return this;
  453. };
  454. Vector3.prototype.subtractInPlace = function (otherVector) {
  455. this.x -= otherVector.x;
  456. this.y -= otherVector.y;
  457. this.z -= otherVector.z;
  458. return this;
  459. };
  460. Vector3.prototype.subtract = function (otherVector) {
  461. return new Vector3(this.x - otherVector.x, this.y - otherVector.y, this.z - otherVector.z);
  462. };
  463. Vector3.prototype.subtractToRef = function (otherVector, result) {
  464. result.x = this.x - otherVector.x;
  465. result.y = this.y - otherVector.y;
  466. result.z = this.z - otherVector.z;
  467. return this;
  468. };
  469. Vector3.prototype.subtractFromFloats = function (x, y, z) {
  470. return new Vector3(this.x - x, this.y - y, this.z - z);
  471. };
  472. Vector3.prototype.subtractFromFloatsToRef = function (x, y, z, result) {
  473. result.x = this.x - x;
  474. result.y = this.y - y;
  475. result.z = this.z - z;
  476. return this;
  477. };
  478. Vector3.prototype.negate = function () {
  479. return new Vector3(-this.x, -this.y, -this.z);
  480. };
  481. Vector3.prototype.scaleInPlace = function (scale) {
  482. this.x *= scale;
  483. this.y *= scale;
  484. this.z *= scale;
  485. return this;
  486. };
  487. Vector3.prototype.scale = function (scale) {
  488. return new Vector3(this.x * scale, this.y * scale, this.z * scale);
  489. };
  490. Vector3.prototype.scaleToRef = function (scale, result) {
  491. result.x = this.x * scale;
  492. result.y = this.y * scale;
  493. result.z = this.z * scale;
  494. };
  495. Vector3.prototype.equals = function (otherVector) {
  496. return otherVector && this.x === otherVector.x && this.y === otherVector.y && this.z === otherVector.z;
  497. };
  498. Vector3.prototype.equalsWithEpsilon = function (otherVector) {
  499. 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;
  500. };
  501. Vector3.prototype.equalsToFloats = function (x, y, z) {
  502. return this.x === x && this.y === y && this.z === z;
  503. };
  504. Vector3.prototype.multiplyInPlace = function (otherVector) {
  505. this.x *= otherVector.x;
  506. this.y *= otherVector.y;
  507. this.z *= otherVector.z;
  508. return this;
  509. };
  510. Vector3.prototype.multiply = function (otherVector) {
  511. return new Vector3(this.x * otherVector.x, this.y * otherVector.y, this.z * otherVector.z);
  512. };
  513. Vector3.prototype.multiplyToRef = function (otherVector, result) {
  514. result.x = this.x * otherVector.x;
  515. result.y = this.y * otherVector.y;
  516. result.z = this.z * otherVector.z;
  517. return this;
  518. };
  519. Vector3.prototype.multiplyByFloats = function (x, y, z) {
  520. return new Vector3(this.x * x, this.y * y, this.z * z);
  521. };
  522. Vector3.prototype.divide = function (otherVector) {
  523. return new Vector3(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z);
  524. };
  525. Vector3.prototype.divideToRef = function (otherVector, result) {
  526. result.x = this.x / otherVector.x;
  527. result.y = this.y / otherVector.y;
  528. result.z = this.z / otherVector.z;
  529. return this;
  530. };
  531. Vector3.prototype.MinimizeInPlace = function (other) {
  532. if (other.x < this.x)
  533. this.x = other.x;
  534. if (other.y < this.y)
  535. this.y = other.y;
  536. if (other.z < this.z)
  537. this.z = other.z;
  538. return this;
  539. };
  540. Vector3.prototype.MaximizeInPlace = function (other) {
  541. if (other.x > this.x)
  542. this.x = other.x;
  543. if (other.y > this.y)
  544. this.y = other.y;
  545. if (other.z > this.z)
  546. this.z = other.z;
  547. return this;
  548. };
  549. // Properties
  550. Vector3.prototype.length = function () {
  551. return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
  552. };
  553. Vector3.prototype.lengthSquared = function () {
  554. return (this.x * this.x + this.y * this.y + this.z * this.z);
  555. };
  556. // Methods
  557. Vector3.prototype.normalize = function () {
  558. var len = this.length();
  559. if (len === 0)
  560. return this;
  561. var num = 1.0 / len;
  562. this.x *= num;
  563. this.y *= num;
  564. this.z *= num;
  565. return this;
  566. };
  567. Vector3.prototype.clone = function () {
  568. return new Vector3(this.x, this.y, this.z);
  569. };
  570. Vector3.prototype.copyFrom = function (source) {
  571. this.x = source.x;
  572. this.y = source.y;
  573. this.z = source.z;
  574. return this;
  575. };
  576. Vector3.prototype.copyFromFloats = function (x, y, z) {
  577. this.x = x;
  578. this.y = y;
  579. this.z = z;
  580. return this;
  581. };
  582. // Statics
  583. Vector3.GetClipFactor = function (vector0, vector1, axis, size) {
  584. var d0 = Vector3.Dot(vector0, axis) - size;
  585. var d1 = Vector3.Dot(vector1, axis) - size;
  586. var s = d0 / (d0 - d1);
  587. return s;
  588. };
  589. Vector3.FromArray = function (array, offset) {
  590. if (!offset) {
  591. offset = 0;
  592. }
  593. return new Vector3(array[offset], array[offset + 1], array[offset + 2]);
  594. };
  595. Vector3.FromArrayToRef = function (array, offset, result) {
  596. result.x = array[offset];
  597. result.y = array[offset + 1];
  598. result.z = array[offset + 2];
  599. };
  600. Vector3.FromFloatArrayToRef = function (array, offset, result) {
  601. result.x = array[offset];
  602. result.y = array[offset + 1];
  603. result.z = array[offset + 2];
  604. };
  605. Vector3.FromFloatsToRef = function (x, y, z, result) {
  606. result.x = x;
  607. result.y = y;
  608. result.z = z;
  609. };
  610. Vector3.Zero = function () {
  611. return new Vector3(0, 0, 0);
  612. };
  613. Vector3.Up = function () {
  614. return new Vector3(0, 1.0, 0);
  615. };
  616. Vector3.TransformCoordinates = function (vector, transformation) {
  617. var result = Vector3.Zero();
  618. Vector3.TransformCoordinatesToRef(vector, transformation, result);
  619. return result;
  620. };
  621. Vector3.TransformCoordinatesToRef = function (vector, transformation, result) {
  622. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + (vector.z * transformation.m[8]) + transformation.m[12];
  623. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + (vector.z * transformation.m[9]) + transformation.m[13];
  624. var z = (vector.x * transformation.m[2]) + (vector.y * transformation.m[6]) + (vector.z * transformation.m[10]) + transformation.m[14];
  625. var w = (vector.x * transformation.m[3]) + (vector.y * transformation.m[7]) + (vector.z * transformation.m[11]) + transformation.m[15];
  626. result.x = x / w;
  627. result.y = y / w;
  628. result.z = z / w;
  629. };
  630. Vector3.TransformCoordinatesFromFloatsToRef = function (x, y, z, transformation, result) {
  631. var rx = (x * transformation.m[0]) + (y * transformation.m[4]) + (z * transformation.m[8]) + transformation.m[12];
  632. var ry = (x * transformation.m[1]) + (y * transformation.m[5]) + (z * transformation.m[9]) + transformation.m[13];
  633. var rz = (x * transformation.m[2]) + (y * transformation.m[6]) + (z * transformation.m[10]) + transformation.m[14];
  634. var rw = (x * transformation.m[3]) + (y * transformation.m[7]) + (z * transformation.m[11]) + transformation.m[15];
  635. result.x = rx / rw;
  636. result.y = ry / rw;
  637. result.z = rz / rw;
  638. };
  639. Vector3.TransformCoordinatesToRefSIMD = function (vector, transformation, result) {
  640. var v = SIMD.float32x4.loadXYZ(vector._data, 0);
  641. var m0 = SIMD.float32x4.load(transformation.m, 0);
  642. var m1 = SIMD.float32x4.load(transformation.m, 4);
  643. var m2 = SIMD.float32x4.load(transformation.m, 8);
  644. var m3 = SIMD.float32x4.load(transformation.m, 12);
  645. var r = SIMD.float32x4.add(SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(v, 0, 0, 0, 0), m0), SIMD.float32x4.mul(SIMD.float32x4.swizzle(v, 1, 1, 1, 1), m1)), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(v, 2, 2, 2, 2), m2), m3));
  646. r = SIMD.float32x4.div(r, SIMD.float32x4.swizzle(r, 3, 3, 3, 3));
  647. SIMD.float32x4.storeXYZ(result._data, 0, r);
  648. };
  649. Vector3.TransformCoordinatesFromFloatsToRefSIMD = function (x, y, z, transformation, result) {
  650. var v0 = SIMD.float32x4.splat(x);
  651. var v1 = SIMD.float32x4.splat(y);
  652. var v2 = SIMD.float32x4.splat(z);
  653. var m0 = SIMD.float32x4.load(transformation.m, 0);
  654. var m1 = SIMD.float32x4.load(transformation.m, 4);
  655. var m2 = SIMD.float32x4.load(transformation.m, 8);
  656. var m3 = SIMD.float32x4.load(transformation.m, 12);
  657. var r = SIMD.float32x4.add(SIMD.float32x4.add(SIMD.float32x4.mul(v0, m0), SIMD.float32x4.mul(v1, m1)), SIMD.float32x4.add(SIMD.float32x4.mul(v2, m2), m3));
  658. r = SIMD.float32x4.div(r, SIMD.float32x4.swizzle(r, 3, 3, 3, 3));
  659. SIMD.float32x4.storeXYZ(result._data, 0, r);
  660. };
  661. Vector3.TransformNormal = function (vector, transformation) {
  662. var result = Vector3.Zero();
  663. Vector3.TransformNormalToRef(vector, transformation, result);
  664. return result;
  665. };
  666. Vector3.TransformNormalToRef = function (vector, transformation, result) {
  667. result.x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + (vector.z * transformation.m[8]);
  668. result.y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + (vector.z * transformation.m[9]);
  669. result.z = (vector.x * transformation.m[2]) + (vector.y * transformation.m[6]) + (vector.z * transformation.m[10]);
  670. };
  671. Vector3.TransformNormalFromFloatsToRef = function (x, y, z, transformation, result) {
  672. result.x = (x * transformation.m[0]) + (y * transformation.m[4]) + (z * transformation.m[8]);
  673. result.y = (x * transformation.m[1]) + (y * transformation.m[5]) + (z * transformation.m[9]);
  674. result.z = (x * transformation.m[2]) + (y * transformation.m[6]) + (z * transformation.m[10]);
  675. };
  676. Vector3.CatmullRom = function (value1, value2, value3, value4, amount) {
  677. var squared = amount * amount;
  678. var cubed = amount * squared;
  679. 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));
  680. 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));
  681. 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));
  682. return new Vector3(x, y, z);
  683. };
  684. Vector3.Clamp = function (value, min, max) {
  685. var x = value.x;
  686. x = (x > max.x) ? max.x : x;
  687. x = (x < min.x) ? min.x : x;
  688. var y = value.y;
  689. y = (y > max.y) ? max.y : y;
  690. y = (y < min.y) ? min.y : y;
  691. var z = value.z;
  692. z = (z > max.z) ? max.z : z;
  693. z = (z < min.z) ? min.z : z;
  694. return new Vector3(x, y, z);
  695. };
  696. Vector3.Hermite = function (value1, tangent1, value2, tangent2, amount) {
  697. var squared = amount * amount;
  698. var cubed = amount * squared;
  699. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  700. var part2 = (-2.0 * cubed) + (3.0 * squared);
  701. var part3 = (cubed - (2.0 * squared)) + amount;
  702. var part4 = cubed - squared;
  703. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  704. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  705. var z = (((value1.z * part1) + (value2.z * part2)) + (tangent1.z * part3)) + (tangent2.z * part4);
  706. return new Vector3(x, y, z);
  707. };
  708. Vector3.Lerp = function (start, end, amount) {
  709. var x = start.x + ((end.x - start.x) * amount);
  710. var y = start.y + ((end.y - start.y) * amount);
  711. var z = start.z + ((end.z - start.z) * amount);
  712. return new Vector3(x, y, z);
  713. };
  714. Vector3.Dot = function (left, right) {
  715. return (left.x * right.x + left.y * right.y + left.z * right.z);
  716. };
  717. Vector3.Cross = function (left, right) {
  718. var result = Vector3.Zero();
  719. Vector3.CrossToRef(left, right, result);
  720. return result;
  721. };
  722. Vector3.CrossToRef = function (left, right, result) {
  723. result.x = left.y * right.z - left.z * right.y;
  724. result.y = left.z * right.x - left.x * right.z;
  725. result.z = left.x * right.y - left.y * right.x;
  726. };
  727. Vector3.Normalize = function (vector) {
  728. var result = Vector3.Zero();
  729. Vector3.NormalizeToRef(vector, result);
  730. return result;
  731. };
  732. Vector3.NormalizeToRef = function (vector, result) {
  733. result.copyFrom(vector);
  734. result.normalize();
  735. };
  736. Vector3.Project = function (vector, world, transform, viewport) {
  737. var cw = viewport.width;
  738. var ch = viewport.height;
  739. var cx = viewport.x;
  740. var cy = viewport.y;
  741. var viewportMatrix = 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);
  742. var finalMatrix = world.multiply(transform).multiply(viewportMatrix);
  743. return Vector3.TransformCoordinates(vector, finalMatrix);
  744. };
  745. Vector3.UnprojectFromTransform = function (source, viewportWidth, viewportHeight, world, transform) {
  746. var matrix = world.multiply(transform);
  747. matrix.invert();
  748. source.x = source.x / viewportWidth * 2 - 1;
  749. source.y = -(source.y / viewportHeight * 2 - 1);
  750. var vector = Vector3.TransformCoordinates(source, matrix);
  751. var num = source.x * matrix.m[3] + source.y * matrix.m[7] + source.z * matrix.m[11] + matrix.m[15];
  752. if (BABYLON.Tools.WithinEpsilon(num, 1.0)) {
  753. vector = vector.scale(1.0 / num);
  754. }
  755. return vector;
  756. };
  757. Vector3.Unproject = function (source, viewportWidth, viewportHeight, world, view, projection) {
  758. var matrix = world.multiply(view).multiply(projection);
  759. matrix.invert();
  760. source.x = source.x / viewportWidth * 2 - 1;
  761. source.y = -(source.y / viewportHeight * 2 - 1);
  762. var vector = Vector3.TransformCoordinates(source, matrix);
  763. var num = source.x * matrix.m[3] + source.y * matrix.m[7] + source.z * matrix.m[11] + matrix.m[15];
  764. if (BABYLON.Tools.WithinEpsilon(num, 1.0)) {
  765. vector = vector.scale(1.0 / num);
  766. }
  767. return vector;
  768. };
  769. Vector3.Minimize = function (left, right) {
  770. var min = left.clone();
  771. min.MinimizeInPlace(right);
  772. return min;
  773. };
  774. Vector3.Maximize = function (left, right) {
  775. var max = left.clone();
  776. max.MaximizeInPlace(right);
  777. return max;
  778. };
  779. Vector3.Distance = function (value1, value2) {
  780. return Math.sqrt(Vector3.DistanceSquared(value1, value2));
  781. };
  782. Vector3.DistanceSquared = function (value1, value2) {
  783. var x = value1.x - value2.x;
  784. var y = value1.y - value2.y;
  785. var z = value1.z - value2.z;
  786. return (x * x) + (y * y) + (z * z);
  787. };
  788. Vector3.Center = function (value1, value2) {
  789. var center = value1.add(value2);
  790. center.scaleInPlace(0.5);
  791. return center;
  792. };
  793. return Vector3;
  794. })();
  795. BABYLON.Vector3 = Vector3;
  796. //Vector4 class created for EulerAngle class conversion to Quaternion
  797. var Vector4 = (function () {
  798. function Vector4(x, y, z, w) {
  799. this.x = x;
  800. this.y = y;
  801. this.z = z;
  802. this.w = w;
  803. }
  804. Vector4.prototype.toString = function () {
  805. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + "W:" + this.w + "}";
  806. };
  807. // Operators
  808. Vector4.prototype.asArray = function () {
  809. var result = [];
  810. this.toArray(result, 0);
  811. return result;
  812. };
  813. Vector4.prototype.toArray = function (array, index) {
  814. if (index === undefined) {
  815. index = 0;
  816. }
  817. array[index] = this.x;
  818. array[index + 1] = this.y;
  819. array[index + 2] = this.z;
  820. array[index + 3] = this.w;
  821. return this;
  822. };
  823. Vector4.prototype.addInPlace = function (otherVector) {
  824. this.x += otherVector.x;
  825. this.y += otherVector.y;
  826. this.z += otherVector.z;
  827. this.w += otherVector.w;
  828. return this;
  829. };
  830. Vector4.prototype.add = function (otherVector) {
  831. return new Vector4(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z, this.w + otherVector.w);
  832. };
  833. Vector4.prototype.addToRef = function (otherVector, result) {
  834. result.x = this.x + otherVector.x;
  835. result.y = this.y + otherVector.y;
  836. result.z = this.z + otherVector.z;
  837. result.w = this.w + otherVector.w;
  838. return this;
  839. };
  840. Vector4.prototype.subtractInPlace = function (otherVector) {
  841. this.x -= otherVector.x;
  842. this.y -= otherVector.y;
  843. this.z -= otherVector.z;
  844. this.w -= otherVector.w;
  845. return this;
  846. };
  847. Vector4.prototype.subtract = function (otherVector) {
  848. return new Vector4(this.x - otherVector.x, this.y - otherVector.y, this.z - otherVector.z, this.w - otherVector.w);
  849. };
  850. Vector4.prototype.subtractToRef = function (otherVector, result) {
  851. result.x = this.x - otherVector.x;
  852. result.y = this.y - otherVector.y;
  853. result.z = this.z - otherVector.z;
  854. result.w = this.w - otherVector.w;
  855. return this;
  856. };
  857. Vector4.prototype.subtractFromFloats = function (x, y, z, w) {
  858. return new Vector4(this.x - x, this.y - y, this.z - z, this.w - w);
  859. };
  860. Vector4.prototype.subtractFromFloatsToRef = function (x, y, z, w, result) {
  861. result.x = this.x - x;
  862. result.y = this.y - y;
  863. result.z = this.z - z;
  864. result.w = this.w - w;
  865. return this;
  866. };
  867. Vector4.prototype.negate = function () {
  868. return new Vector4(-this.x, -this.y, -this.z, -this.w);
  869. };
  870. Vector4.prototype.scaleInPlace = function (scale) {
  871. this.x *= scale;
  872. this.y *= scale;
  873. this.z *= scale;
  874. this.w *= scale;
  875. return this;
  876. };
  877. Vector4.prototype.scale = function (scale) {
  878. return new Vector4(this.x * scale, this.y * scale, this.z * scale, this.w * scale);
  879. };
  880. Vector4.prototype.scaleToRef = function (scale, result) {
  881. result.x = this.x * scale;
  882. result.y = this.y * scale;
  883. result.z = this.z * scale;
  884. result.w = this.w * scale;
  885. };
  886. Vector4.prototype.equals = function (otherVector) {
  887. return otherVector && this.x === otherVector.x && this.y === otherVector.y && this.z === otherVector.z && this.w === otherVector.w;
  888. };
  889. Vector4.prototype.equalsWithEpsilon = function (otherVector) {
  890. 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;
  891. };
  892. Vector4.prototype.equalsToFloats = function (x, y, z, w) {
  893. return this.x === x && this.y === y && this.z === z && this.w === w;
  894. };
  895. Vector4.prototype.multiplyInPlace = function (otherVector) {
  896. this.x *= otherVector.x;
  897. this.y *= otherVector.y;
  898. this.z *= otherVector.z;
  899. this.w *= otherVector.w;
  900. return this;
  901. };
  902. Vector4.prototype.multiply = function (otherVector) {
  903. return new Vector4(this.x * otherVector.x, this.y * otherVector.y, this.z * otherVector.z, this.w * otherVector.w);
  904. };
  905. Vector4.prototype.multiplyToRef = function (otherVector, result) {
  906. result.x = this.x * otherVector.x;
  907. result.y = this.y * otherVector.y;
  908. result.z = this.z * otherVector.z;
  909. result.w = this.w * otherVector.w;
  910. return this;
  911. };
  912. Vector4.prototype.multiplyByFloats = function (x, y, z, w) {
  913. return new Vector4(this.x * x, this.y * y, this.z * z, this.w * w);
  914. };
  915. Vector4.prototype.divide = function (otherVector) {
  916. return new Vector4(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z, this.w / otherVector.w);
  917. };
  918. Vector4.prototype.divideToRef = function (otherVector, result) {
  919. result.x = this.x / otherVector.x;
  920. result.y = this.y / otherVector.y;
  921. result.z = this.z / otherVector.z;
  922. result.w = this.w / otherVector.w;
  923. return this;
  924. };
  925. Vector4.prototype.MinimizeInPlace = function (other) {
  926. if (other.x < this.x)
  927. this.x = other.x;
  928. if (other.y < this.y)
  929. this.y = other.y;
  930. if (other.z < this.z)
  931. this.z = other.z;
  932. if (other.w < this.w)
  933. this.w = other.w;
  934. return this;
  935. };
  936. Vector4.prototype.MaximizeInPlace = function (other) {
  937. if (other.x > this.x)
  938. this.x = other.x;
  939. if (other.y > this.y)
  940. this.y = other.y;
  941. if (other.z > this.z)
  942. this.z = other.z;
  943. if (other.w > this.w)
  944. this.w = other.w;
  945. return this;
  946. };
  947. // Properties
  948. Vector4.prototype.length = function () {
  949. return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
  950. };
  951. Vector4.prototype.lengthSquared = function () {
  952. return (this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
  953. };
  954. // Methods
  955. Vector4.prototype.normalize = function () {
  956. var len = this.length();
  957. if (len === 0)
  958. return this;
  959. var num = 1.0 / len;
  960. this.x *= num;
  961. this.y *= num;
  962. this.z *= num;
  963. this.w *= num;
  964. return this;
  965. };
  966. Vector4.prototype.clone = function () {
  967. return new Vector4(this.x, this.y, this.z, this.w);
  968. };
  969. Vector4.prototype.copyFrom = function (source) {
  970. this.x = source.x;
  971. this.y = source.y;
  972. this.z = source.z;
  973. this.w = source.w;
  974. return this;
  975. };
  976. Vector4.prototype.copyFromFloats = function (x, y, z, w) {
  977. this.x = x;
  978. this.y = y;
  979. this.z = z;
  980. this.w = w;
  981. return this;
  982. };
  983. // Statics
  984. Vector4.FromArray = function (array, offset) {
  985. if (!offset) {
  986. offset = 0;
  987. }
  988. return new Vector4(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  989. };
  990. Vector4.FromArrayToRef = function (array, offset, result) {
  991. result.x = array[offset];
  992. result.y = array[offset + 1];
  993. result.z = array[offset + 2];
  994. result.w = array[offset + 3];
  995. };
  996. Vector4.FromFloatArrayToRef = function (array, offset, result) {
  997. result.x = array[offset];
  998. result.y = array[offset + 1];
  999. result.z = array[offset + 2];
  1000. result.w = array[offset + 3];
  1001. };
  1002. Vector4.FromFloatsToRef = function (x, y, z, w, result) {
  1003. result.x = x;
  1004. result.y = y;
  1005. result.z = z;
  1006. result.w = w;
  1007. };
  1008. Vector4.Zero = function () {
  1009. return new Vector4(0, 0, 0, 0);
  1010. };
  1011. Vector4.Normalize = function (vector) {
  1012. var result = Vector4.Zero();
  1013. Vector4.NormalizeToRef(vector, result);
  1014. return result;
  1015. };
  1016. Vector4.NormalizeToRef = function (vector, result) {
  1017. result.copyFrom(vector);
  1018. result.normalize();
  1019. };
  1020. Vector4.Minimize = function (left, right) {
  1021. var min = left.clone();
  1022. min.MinimizeInPlace(right);
  1023. return min;
  1024. };
  1025. Vector4.Maximize = function (left, right) {
  1026. var max = left.clone();
  1027. max.MaximizeInPlace(right);
  1028. return max;
  1029. };
  1030. Vector4.Distance = function (value1, value2) {
  1031. return Math.sqrt(Vector4.DistanceSquared(value1, value2));
  1032. };
  1033. Vector4.DistanceSquared = function (value1, value2) {
  1034. var x = value1.x - value2.x;
  1035. var y = value1.y - value2.y;
  1036. var z = value1.z - value2.z;
  1037. var w = value1.w - value2.w;
  1038. return (x * x) + (y * y) + (z * z) + (w * w);
  1039. };
  1040. Vector4.Center = function (value1, value2) {
  1041. var center = value1.add(value2);
  1042. center.scaleInPlace(0.5);
  1043. return center;
  1044. };
  1045. return Vector4;
  1046. })();
  1047. BABYLON.Vector4 = Vector4;
  1048. var Quaternion = (function () {
  1049. function Quaternion(x, y, z, w) {
  1050. if (x === void 0) { x = 0; }
  1051. if (y === void 0) { y = 0; }
  1052. if (z === void 0) { z = 0; }
  1053. if (w === void 0) { w = 1; }
  1054. this.x = x;
  1055. this.y = y;
  1056. this.z = z;
  1057. this.w = w;
  1058. }
  1059. Quaternion.prototype.toString = function () {
  1060. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + " W:" + this.w + "}";
  1061. };
  1062. Quaternion.prototype.asArray = function () {
  1063. return [this.x, this.y, this.z, this.w];
  1064. };
  1065. Quaternion.prototype.equals = function (otherQuaternion) {
  1066. return otherQuaternion && this.x === otherQuaternion.x && this.y === otherQuaternion.y && this.z === otherQuaternion.z && this.w === otherQuaternion.w;
  1067. };
  1068. Quaternion.prototype.clone = function () {
  1069. return new Quaternion(this.x, this.y, this.z, this.w);
  1070. };
  1071. Quaternion.prototype.copyFrom = function (other) {
  1072. this.x = other.x;
  1073. this.y = other.y;
  1074. this.z = other.z;
  1075. this.w = other.w;
  1076. return this;
  1077. };
  1078. Quaternion.prototype.copyFromFloats = function (x, y, z, w) {
  1079. this.x = x;
  1080. this.y = y;
  1081. this.z = z;
  1082. this.w = w;
  1083. return this;
  1084. };
  1085. Quaternion.prototype.add = function (other) {
  1086. return new Quaternion(this.x + other.x, this.y + other.y, this.z + other.z, this.w + other.w);
  1087. };
  1088. Quaternion.prototype.subtract = function (other) {
  1089. return new Quaternion(this.x - other.x, this.y - other.y, this.z - other.z, this.w - other.w);
  1090. };
  1091. Quaternion.prototype.scale = function (value) {
  1092. return new Quaternion(this.x * value, this.y * value, this.z * value, this.w * value);
  1093. };
  1094. Quaternion.prototype.multiply = function (q1) {
  1095. var result = new Quaternion(0, 0, 0, 1.0);
  1096. this.multiplyToRef(q1, result);
  1097. return result;
  1098. };
  1099. Quaternion.prototype.multiplyToRef = function (q1, result) {
  1100. result.x = this.x * q1.w + this.y * q1.z - this.z * q1.y + this.w * q1.x;
  1101. result.y = -this.x * q1.z + this.y * q1.w + this.z * q1.x + this.w * q1.y;
  1102. result.z = this.x * q1.y - this.y * q1.x + this.z * q1.w + this.w * q1.z;
  1103. result.w = -this.x * q1.x - this.y * q1.y - this.z * q1.z + this.w * q1.w;
  1104. return this;
  1105. };
  1106. Quaternion.prototype.length = function () {
  1107. return Math.sqrt((this.x * this.x) + (this.y * this.y) + (this.z * this.z) + (this.w * this.w));
  1108. };
  1109. Quaternion.prototype.normalize = function () {
  1110. var length = 1.0 / this.length();
  1111. this.x *= length;
  1112. this.y *= length;
  1113. this.z *= length;
  1114. this.w *= length;
  1115. return this;
  1116. };
  1117. Quaternion.prototype.toEulerAngles = function () {
  1118. var result = Vector3.Zero();
  1119. this.toEulerAnglesToRef(result);
  1120. return result;
  1121. };
  1122. Quaternion.prototype.toEulerAnglesToRef = function (result) {
  1123. //result is an EulerAngles in the in the z-x-z convention
  1124. var qx = this.x;
  1125. var qy = this.y;
  1126. var qz = this.z;
  1127. var qw = this.w;
  1128. var qxy = qx * qy;
  1129. var qxz = qx * qz;
  1130. var qwy = qw * qy;
  1131. var qwz = qw * qz;
  1132. var qwx = qw * qx;
  1133. var qyz = qy * qz;
  1134. var sqx = qx * qx;
  1135. var sqy = qy * qy;
  1136. var determinant = sqx + sqy;
  1137. if (determinant !== 0.000 && determinant !== 1.000) {
  1138. result.x = Math.atan2(qxz + qwy, qwx - qyz);
  1139. result.y = Math.acos(1 - 2 * determinant);
  1140. result.z = Math.atan2(qxz - qwy, qwx + qyz);
  1141. }
  1142. else {
  1143. if (determinant === 0.0) {
  1144. result.x = 0.0;
  1145. result.y = 0.0;
  1146. 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)
  1147. }
  1148. else {
  1149. 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)
  1150. result.y = Math.PI;
  1151. result.z = 0.0;
  1152. }
  1153. }
  1154. return this;
  1155. };
  1156. Quaternion.prototype.toRotationMatrix = function (result) {
  1157. var xx = this.x * this.x;
  1158. var yy = this.y * this.y;
  1159. var zz = this.z * this.z;
  1160. var xy = this.x * this.y;
  1161. var zw = this.z * this.w;
  1162. var zx = this.z * this.x;
  1163. var yw = this.y * this.w;
  1164. var yz = this.y * this.z;
  1165. var xw = this.x * this.w;
  1166. result.m[0] = 1.0 - (2.0 * (yy + zz));
  1167. result.m[1] = 2.0 * (xy + zw);
  1168. result.m[2] = 2.0 * (zx - yw);
  1169. result.m[3] = 0;
  1170. result.m[4] = 2.0 * (xy - zw);
  1171. result.m[5] = 1.0 - (2.0 * (zz + xx));
  1172. result.m[6] = 2.0 * (yz + xw);
  1173. result.m[7] = 0;
  1174. result.m[8] = 2.0 * (zx + yw);
  1175. result.m[9] = 2.0 * (yz - xw);
  1176. result.m[10] = 1.0 - (2.0 * (yy + xx));
  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. return this;
  1183. };
  1184. Quaternion.prototype.fromRotationMatrix = function (matrix) {
  1185. Quaternion.FromRotationMatrixToRef(matrix, this);
  1186. return this;
  1187. };
  1188. // Statics
  1189. Quaternion.FromRotationMatrix = function (matrix) {
  1190. var result = new Quaternion();
  1191. Quaternion.FromRotationMatrixToRef(matrix, result);
  1192. return result;
  1193. };
  1194. Quaternion.FromRotationMatrixToRef = function (matrix, result) {
  1195. var data = matrix.m;
  1196. var m11 = data[0], m12 = data[4], m13 = data[8];
  1197. var m21 = data[1], m22 = data[5], m23 = data[9];
  1198. var m31 = data[2], m32 = data[6], m33 = data[10];
  1199. var trace = m11 + m22 + m33;
  1200. var s;
  1201. if (trace > 0) {
  1202. s = 0.5 / Math.sqrt(trace + 1.0);
  1203. result.w = 0.25 / s;
  1204. result.x = (m32 - m23) * s;
  1205. result.y = (m13 - m31) * s;
  1206. result.z = (m21 - m12) * s;
  1207. }
  1208. else if (m11 > m22 && m11 > m33) {
  1209. s = 2.0 * Math.sqrt(1.0 + m11 - m22 - m33);
  1210. result.w = (m32 - m23) / s;
  1211. result.x = 0.25 * s;
  1212. result.y = (m12 + m21) / s;
  1213. result.z = (m13 + m31) / s;
  1214. }
  1215. else if (m22 > m33) {
  1216. s = 2.0 * Math.sqrt(1.0 + m22 - m11 - m33);
  1217. result.w = (m13 - m31) / s;
  1218. result.x = (m12 + m21) / s;
  1219. result.y = 0.25 * s;
  1220. result.z = (m23 + m32) / s;
  1221. }
  1222. else {
  1223. s = 2.0 * Math.sqrt(1.0 + m33 - m11 - m22);
  1224. result.w = (m21 - m12) / s;
  1225. result.x = (m13 + m31) / s;
  1226. result.y = (m23 + m32) / s;
  1227. result.z = 0.25 * s;
  1228. }
  1229. };
  1230. Quaternion.Inverse = function (q) {
  1231. return new Quaternion(-q.x, -q.y, -q.z, q.w);
  1232. };
  1233. Quaternion.Identity = function () {
  1234. return new Quaternion(0, 0, 0, 1);
  1235. };
  1236. Quaternion.RotationAxis = function (axis, angle) {
  1237. var result = new Quaternion();
  1238. var sin = Math.sin(angle / 2);
  1239. result.w = Math.cos(angle / 2);
  1240. result.x = axis.x * sin;
  1241. result.y = axis.y * sin;
  1242. result.z = axis.z * sin;
  1243. return result;
  1244. };
  1245. Quaternion.FromArray = function (array, offset) {
  1246. if (!offset) {
  1247. offset = 0;
  1248. }
  1249. return new Quaternion(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  1250. };
  1251. Quaternion.RotationYawPitchRoll = function (yaw, pitch, roll) {
  1252. var result = new Quaternion();
  1253. Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, result);
  1254. return result;
  1255. };
  1256. Quaternion.RotationYawPitchRollToRef = function (yaw, pitch, roll, result) {
  1257. // Produces a quaternion from Euler angles in the z-y-x orientation (Tait-Bryan angles)
  1258. var halfRoll = roll * 0.5;
  1259. var halfPitch = pitch * 0.5;
  1260. var halfYaw = yaw * 0.5;
  1261. var sinRoll = Math.sin(halfRoll);
  1262. var cosRoll = Math.cos(halfRoll);
  1263. var sinPitch = Math.sin(halfPitch);
  1264. var cosPitch = Math.cos(halfPitch);
  1265. var sinYaw = Math.sin(halfYaw);
  1266. var cosYaw = Math.cos(halfYaw);
  1267. result.x = (cosYaw * sinPitch * cosRoll) + (sinYaw * cosPitch * sinRoll);
  1268. result.y = (sinYaw * cosPitch * cosRoll) - (cosYaw * sinPitch * sinRoll);
  1269. result.z = (cosYaw * cosPitch * sinRoll) - (sinYaw * sinPitch * cosRoll);
  1270. result.w = (cosYaw * cosPitch * cosRoll) + (sinYaw * sinPitch * sinRoll);
  1271. };
  1272. Quaternion.RotationAlphaBetaGamma = function (alpha, beta, gamma) {
  1273. var result = new Quaternion();
  1274. Quaternion.RotationAlphaBetaGammaToRef(alpha, beta, gamma, result);
  1275. return result;
  1276. };
  1277. Quaternion.RotationAlphaBetaGammaToRef = function (alpha, beta, gamma, result) {
  1278. // Produces a quaternion from Euler angles in the z-x-z orientation
  1279. var halfGammaPlusAlpha = (gamma + alpha) * 0.5;
  1280. var halfGammaMinusAlpha = (gamma - alpha) * 0.5;
  1281. var halfBeta = beta * 0.5;
  1282. result.x = Math.cos(halfGammaMinusAlpha) * Math.sin(halfBeta);
  1283. result.y = Math.sin(halfGammaMinusAlpha) * Math.sin(halfBeta);
  1284. result.z = Math.sin(halfGammaPlusAlpha) * Math.cos(halfBeta);
  1285. result.w = Math.cos(halfGammaPlusAlpha) * Math.cos(halfBeta);
  1286. };
  1287. Quaternion.Slerp = function (left, right, amount) {
  1288. var num2;
  1289. var num3;
  1290. var num = amount;
  1291. var num4 = (((left.x * right.x) + (left.y * right.y)) + (left.z * right.z)) + (left.w * right.w);
  1292. var flag = false;
  1293. if (num4 < 0) {
  1294. flag = true;
  1295. num4 = -num4;
  1296. }
  1297. if (num4 > 0.999999) {
  1298. num3 = 1 - num;
  1299. num2 = flag ? -num : num;
  1300. }
  1301. else {
  1302. var num5 = Math.acos(num4);
  1303. var num6 = (1.0 / Math.sin(num5));
  1304. num3 = (Math.sin((1.0 - num) * num5)) * num6;
  1305. num2 = flag ? ((-Math.sin(num * num5)) * num6) : ((Math.sin(num * num5)) * num6);
  1306. }
  1307. 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));
  1308. };
  1309. return Quaternion;
  1310. })();
  1311. BABYLON.Quaternion = Quaternion;
  1312. var Matrix = (function () {
  1313. function Matrix() {
  1314. this.m = new Float32Array(16);
  1315. }
  1316. // Properties
  1317. Matrix.prototype.isIdentity = function () {
  1318. if (this.m[0] !== 1.0 || this.m[5] !== 1.0 || this.m[10] !== 1.0 || this.m[15] !== 1.0)
  1319. return false;
  1320. 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)
  1321. return false;
  1322. return true;
  1323. };
  1324. Matrix.prototype.determinant = function () {
  1325. var temp1 = (this.m[10] * this.m[15]) - (this.m[11] * this.m[14]);
  1326. var temp2 = (this.m[9] * this.m[15]) - (this.m[11] * this.m[13]);
  1327. var temp3 = (this.m[9] * this.m[14]) - (this.m[10] * this.m[13]);
  1328. var temp4 = (this.m[8] * this.m[15]) - (this.m[11] * this.m[12]);
  1329. var temp5 = (this.m[8] * this.m[14]) - (this.m[10] * this.m[12]);
  1330. var temp6 = (this.m[8] * this.m[13]) - (this.m[9] * this.m[12]);
  1331. 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))));
  1332. };
  1333. // Methods
  1334. Matrix.prototype.toArray = function () {
  1335. return this.m;
  1336. };
  1337. Matrix.prototype.asArray = function () {
  1338. return this.toArray();
  1339. };
  1340. Matrix.prototype.invert = function () {
  1341. this.invertToRef(this);
  1342. return this;
  1343. };
  1344. Matrix.prototype.invertToRef = function (other) {
  1345. var l1 = this.m[0];
  1346. var l2 = this.m[1];
  1347. var l3 = this.m[2];
  1348. var l4 = this.m[3];
  1349. var l5 = this.m[4];
  1350. var l6 = this.m[5];
  1351. var l7 = this.m[6];
  1352. var l8 = this.m[7];
  1353. var l9 = this.m[8];
  1354. var l10 = this.m[9];
  1355. var l11 = this.m[10];
  1356. var l12 = this.m[11];
  1357. var l13 = this.m[12];
  1358. var l14 = this.m[13];
  1359. var l15 = this.m[14];
  1360. var l16 = this.m[15];
  1361. var l17 = (l11 * l16) - (l12 * l15);
  1362. var l18 = (l10 * l16) - (l12 * l14);
  1363. var l19 = (l10 * l15) - (l11 * l14);
  1364. var l20 = (l9 * l16) - (l12 * l13);
  1365. var l21 = (l9 * l15) - (l11 * l13);
  1366. var l22 = (l9 * l14) - (l10 * l13);
  1367. var l23 = ((l6 * l17) - (l7 * l18)) + (l8 * l19);
  1368. var l24 = -(((l5 * l17) - (l7 * l20)) + (l8 * l21));
  1369. var l25 = ((l5 * l18) - (l6 * l20)) + (l8 * l22);
  1370. var l26 = -(((l5 * l19) - (l6 * l21)) + (l7 * l22));
  1371. var l27 = 1.0 / ((((l1 * l23) + (l2 * l24)) + (l3 * l25)) + (l4 * l26));
  1372. var l28 = (l7 * l16) - (l8 * l15);
  1373. var l29 = (l6 * l16) - (l8 * l14);
  1374. var l30 = (l6 * l15) - (l7 * l14);
  1375. var l31 = (l5 * l16) - (l8 * l13);
  1376. var l32 = (l5 * l15) - (l7 * l13);
  1377. var l33 = (l5 * l14) - (l6 * l13);
  1378. var l34 = (l7 * l12) - (l8 * l11);
  1379. var l35 = (l6 * l12) - (l8 * l10);
  1380. var l36 = (l6 * l11) - (l7 * l10);
  1381. var l37 = (l5 * l12) - (l8 * l9);
  1382. var l38 = (l5 * l11) - (l7 * l9);
  1383. var l39 = (l5 * l10) - (l6 * l9);
  1384. other.m[0] = l23 * l27;
  1385. other.m[4] = l24 * l27;
  1386. other.m[8] = l25 * l27;
  1387. other.m[12] = l26 * l27;
  1388. other.m[1] = -(((l2 * l17) - (l3 * l18)) + (l4 * l19)) * l27;
  1389. other.m[5] = (((l1 * l17) - (l3 * l20)) + (l4 * l21)) * l27;
  1390. other.m[9] = -(((l1 * l18) - (l2 * l20)) + (l4 * l22)) * l27;
  1391. other.m[13] = (((l1 * l19) - (l2 * l21)) + (l3 * l22)) * l27;
  1392. other.m[2] = (((l2 * l28) - (l3 * l29)) + (l4 * l30)) * l27;
  1393. other.m[6] = -(((l1 * l28) - (l3 * l31)) + (l4 * l32)) * l27;
  1394. other.m[10] = (((l1 * l29) - (l2 * l31)) + (l4 * l33)) * l27;
  1395. other.m[14] = -(((l1 * l30) - (l2 * l32)) + (l3 * l33)) * l27;
  1396. other.m[3] = -(((l2 * l34) - (l3 * l35)) + (l4 * l36)) * l27;
  1397. other.m[7] = (((l1 * l34) - (l3 * l37)) + (l4 * l38)) * l27;
  1398. other.m[11] = -(((l1 * l35) - (l2 * l37)) + (l4 * l39)) * l27;
  1399. other.m[15] = (((l1 * l36) - (l2 * l38)) + (l3 * l39)) * l27;
  1400. return this;
  1401. };
  1402. Matrix.prototype.invertToRefSIMD = function (other) {
  1403. var src = this.m;
  1404. var dest = other.m;
  1405. var row0, row1, row2, row3;
  1406. var tmp1;
  1407. var minor0, minor1, minor2, minor3;
  1408. var det;
  1409. // Load the 4 rows
  1410. var src0 = SIMD.float32x4.load(src, 0);
  1411. var src1 = SIMD.float32x4.load(src, 4);
  1412. var src2 = SIMD.float32x4.load(src, 8);
  1413. var src3 = SIMD.float32x4.load(src, 12);
  1414. // Transpose the source matrix. Sort of. Not a true transpose operation
  1415. tmp1 = SIMD.float32x4.shuffle(src0, src1, 0, 1, 4, 5);
  1416. row1 = SIMD.float32x4.shuffle(src2, src3, 0, 1, 4, 5);
  1417. row0 = SIMD.float32x4.shuffle(tmp1, row1, 0, 2, 4, 6);
  1418. row1 = SIMD.float32x4.shuffle(row1, tmp1, 1, 3, 5, 7);
  1419. tmp1 = SIMD.float32x4.shuffle(src0, src1, 2, 3, 6, 7);
  1420. row3 = SIMD.float32x4.shuffle(src2, src3, 2, 3, 6, 7);
  1421. row2 = SIMD.float32x4.shuffle(tmp1, row3, 0, 2, 4, 6);
  1422. row3 = SIMD.float32x4.shuffle(row3, tmp1, 1, 3, 5, 7);
  1423. // This is a true transposition, but it will lead to an incorrect result
  1424. //tmp1 = SIMD.float32x4.shuffle(src0, src1, 0, 1, 4, 5);
  1425. //tmp2 = SIMD.float32x4.shuffle(src2, src3, 0, 1, 4, 5);
  1426. //row0 = SIMD.float32x4.shuffle(tmp1, tmp2, 0, 2, 4, 6);
  1427. //row1 = SIMD.float32x4.shuffle(tmp1, tmp2, 1, 3, 5, 7);
  1428. //tmp1 = SIMD.float32x4.shuffle(src0, src1, 2, 3, 6, 7);
  1429. //tmp2 = SIMD.float32x4.shuffle(src2, src3, 2, 3, 6, 7);
  1430. //row2 = SIMD.float32x4.shuffle(tmp1, tmp2, 0, 2, 4, 6);
  1431. //row3 = SIMD.float32x4.shuffle(tmp1, tmp2, 1, 3, 5, 7);
  1432. // ----
  1433. tmp1 = SIMD.float32x4.mul(row2, row3);
  1434. tmp1 = SIMD.float32x4.swizzle(tmp1, 1, 0, 3, 2); // 0xB1 = 10110001
  1435. minor0 = SIMD.float32x4.mul(row1, tmp1);
  1436. minor1 = SIMD.float32x4.mul(row0, tmp1);
  1437. tmp1 = SIMD.float32x4.swizzle(tmp1, 2, 3, 0, 1); // 0x4E = 01001110
  1438. minor0 = SIMD.float32x4.sub(SIMD.float32x4.mul(row1, tmp1), minor0);
  1439. minor1 = SIMD.float32x4.sub(SIMD.float32x4.mul(row0, tmp1), minor1);
  1440. minor1 = SIMD.float32x4.swizzle(minor1, 2, 3, 0, 1); // 0x4E = 01001110
  1441. // ----
  1442. tmp1 = SIMD.float32x4.mul(row1, row2);
  1443. tmp1 = SIMD.float32x4.swizzle(tmp1, 1, 0, 3, 2); // 0xB1 = 10110001
  1444. minor0 = SIMD.float32x4.add(SIMD.float32x4.mul(row3, tmp1), minor0);
  1445. minor3 = SIMD.float32x4.mul(row0, tmp1);
  1446. tmp1 = SIMD.float32x4.swizzle(tmp1, 2, 3, 0, 1); // 0x4E = 01001110
  1447. minor0 = SIMD.float32x4.sub(minor0, SIMD.float32x4.mul(row3, tmp1));
  1448. minor3 = SIMD.float32x4.sub(SIMD.float32x4.mul(row0, tmp1), minor3);
  1449. minor3 = SIMD.float32x4.swizzle(minor3, 2, 3, 0, 1); // 0x4E = 01001110
  1450. // ----
  1451. tmp1 = SIMD.float32x4.mul(SIMD.float32x4.swizzle(row1, 2, 3, 0, 1), row3); // 0x4E = 01001110
  1452. tmp1 = SIMD.float32x4.swizzle(tmp1, 1, 0, 3, 2); // 0xB1 = 10110001
  1453. row2 = SIMD.float32x4.swizzle(row2, 2, 3, 0, 1); // 0x4E = 01001110
  1454. minor0 = SIMD.float32x4.add(SIMD.float32x4.mul(row2, tmp1), minor0);
  1455. minor2 = SIMD.float32x4.mul(row0, tmp1);
  1456. tmp1 = SIMD.float32x4.swizzle(tmp1, 2, 3, 0, 1); // 0x4E = 01001110
  1457. minor0 = SIMD.float32x4.sub(minor0, SIMD.float32x4.mul(row2, tmp1));
  1458. minor2 = SIMD.float32x4.sub(SIMD.float32x4.mul(row0, tmp1), minor2);
  1459. minor2 = SIMD.float32x4.swizzle(minor2, 2, 3, 0, 1); // 0x4E = 01001110
  1460. // ----
  1461. tmp1 = SIMD.float32x4.mul(row0, row1);
  1462. tmp1 = SIMD.float32x4.swizzle(tmp1, 1, 0, 3, 2); // 0xB1 = 10110001
  1463. minor2 = SIMD.float32x4.add(SIMD.float32x4.mul(row3, tmp1), minor2);
  1464. minor3 = SIMD.float32x4.sub(SIMD.float32x4.mul(row2, tmp1), minor3);
  1465. tmp1 = SIMD.float32x4.swizzle(tmp1, 2, 3, 0, 1); // 0x4E = 01001110
  1466. minor2 = SIMD.float32x4.sub(SIMD.float32x4.mul(row3, tmp1), minor2);
  1467. minor3 = SIMD.float32x4.sub(minor3, SIMD.float32x4.mul(row2, tmp1));
  1468. // ----
  1469. tmp1 = SIMD.float32x4.mul(row0, row3);
  1470. tmp1 = SIMD.float32x4.swizzle(tmp1, 1, 0, 3, 2); // 0xB1 = 10110001
  1471. minor1 = SIMD.float32x4.sub(minor1, SIMD.float32x4.mul(row2, tmp1));
  1472. minor2 = SIMD.float32x4.add(SIMD.float32x4.mul(row1, tmp1), minor2);
  1473. tmp1 = SIMD.float32x4.swizzle(tmp1, 2, 3, 0, 1); // 0x4E = 01001110
  1474. minor1 = SIMD.float32x4.add(SIMD.float32x4.mul(row2, tmp1), minor1);
  1475. minor2 = SIMD.float32x4.sub(minor2, SIMD.float32x4.mul(row1, tmp1));
  1476. // ----
  1477. tmp1 = SIMD.float32x4.mul(row0, row2);
  1478. tmp1 = SIMD.float32x4.swizzle(tmp1, 1, 0, 3, 2); // 0xB1 = 10110001
  1479. minor1 = SIMD.float32x4.add(SIMD.float32x4.mul(row3, tmp1), minor1);
  1480. minor3 = SIMD.float32x4.sub(minor3, SIMD.float32x4.mul(row1, tmp1));
  1481. tmp1 = SIMD.float32x4.swizzle(tmp1, 2, 3, 0, 1); // 0x4E = 01001110
  1482. minor1 = SIMD.float32x4.sub(minor1, SIMD.float32x4.mul(row3, tmp1));
  1483. minor3 = SIMD.float32x4.add(SIMD.float32x4.mul(row1, tmp1), minor3);
  1484. // Compute determinant
  1485. det = SIMD.float32x4.mul(row0, minor0);
  1486. det = SIMD.float32x4.add(SIMD.float32x4.swizzle(det, 2, 3, 0, 1), det); // 0x4E = 01001110
  1487. det = SIMD.float32x4.add(SIMD.float32x4.swizzle(det, 1, 0, 3, 2), det); // 0xB1 = 10110001
  1488. tmp1 = SIMD.float32x4.reciprocalApproximation(det);
  1489. det = SIMD.float32x4.sub(SIMD.float32x4.add(tmp1, tmp1), SIMD.float32x4.mul(det, SIMD.float32x4.mul(tmp1, tmp1)));
  1490. det = SIMD.float32x4.swizzle(det, 0, 0, 0, 0);
  1491. // These shuffles aren't necessary if the faulty transposition is done
  1492. // up at the top of this function.
  1493. //minor0 = SIMD.float32x4.swizzle(minor0, 2, 1, 0, 3);
  1494. //minor1 = SIMD.float32x4.swizzle(minor1, 2, 1, 0, 3);
  1495. //minor2 = SIMD.float32x4.swizzle(minor2, 2, 1, 0, 3);
  1496. //minor3 = SIMD.float32x4.swizzle(minor3, 2, 1, 0, 3);
  1497. // Compute final values by multiplying with 1/det
  1498. minor0 = SIMD.float32x4.mul(det, minor0);
  1499. minor1 = SIMD.float32x4.mul(det, minor1);
  1500. minor2 = SIMD.float32x4.mul(det, minor2);
  1501. minor3 = SIMD.float32x4.mul(det, minor3);
  1502. SIMD.float32x4.store(dest, 0, minor0);
  1503. SIMD.float32x4.store(dest, 4, minor1);
  1504. SIMD.float32x4.store(dest, 8, minor2);
  1505. SIMD.float32x4.store(dest, 12, minor3);
  1506. return this;
  1507. };
  1508. Matrix.prototype.setTranslation = function (vector3) {
  1509. this.m[12] = vector3.x;
  1510. this.m[13] = vector3.y;
  1511. this.m[14] = vector3.z;
  1512. return this;
  1513. };
  1514. Matrix.prototype.multiply = function (other) {
  1515. var result = new Matrix();
  1516. this.multiplyToRef(other, result);
  1517. return result;
  1518. };
  1519. Matrix.prototype.copyFrom = function (other) {
  1520. for (var index = 0; index < 16; index++) {
  1521. this.m[index] = other.m[index];
  1522. }
  1523. return this;
  1524. };
  1525. Matrix.prototype.copyToArray = function (array, offset) {
  1526. if (offset === void 0) { offset = 0; }
  1527. for (var index = 0; index < 16; index++) {
  1528. array[offset + index] = this.m[index];
  1529. }
  1530. return this;
  1531. };
  1532. Matrix.prototype.multiplyToRef = function (other, result) {
  1533. this.multiplyToArray(other, result.m, 0);
  1534. return this;
  1535. };
  1536. Matrix.prototype.multiplyToArray = function (other, result, offset) {
  1537. var tm0 = this.m[0];
  1538. var tm1 = this.m[1];
  1539. var tm2 = this.m[2];
  1540. var tm3 = this.m[3];
  1541. var tm4 = this.m[4];
  1542. var tm5 = this.m[5];
  1543. var tm6 = this.m[6];
  1544. var tm7 = this.m[7];
  1545. var tm8 = this.m[8];
  1546. var tm9 = this.m[9];
  1547. var tm10 = this.m[10];
  1548. var tm11 = this.m[11];
  1549. var tm12 = this.m[12];
  1550. var tm13 = this.m[13];
  1551. var tm14 = this.m[14];
  1552. var tm15 = this.m[15];
  1553. var om0 = other.m[0];
  1554. var om1 = other.m[1];
  1555. var om2 = other.m[2];
  1556. var om3 = other.m[3];
  1557. var om4 = other.m[4];
  1558. var om5 = other.m[5];
  1559. var om6 = other.m[6];
  1560. var om7 = other.m[7];
  1561. var om8 = other.m[8];
  1562. var om9 = other.m[9];
  1563. var om10 = other.m[10];
  1564. var om11 = other.m[11];
  1565. var om12 = other.m[12];
  1566. var om13 = other.m[13];
  1567. var om14 = other.m[14];
  1568. var om15 = other.m[15];
  1569. result[offset] = tm0 * om0 + tm1 * om4 + tm2 * om8 + tm3 * om12;
  1570. result[offset + 1] = tm0 * om1 + tm1 * om5 + tm2 * om9 + tm3 * om13;
  1571. result[offset + 2] = tm0 * om2 + tm1 * om6 + tm2 * om10 + tm3 * om14;
  1572. result[offset + 3] = tm0 * om3 + tm1 * om7 + tm2 * om11 + tm3 * om15;
  1573. result[offset + 4] = tm4 * om0 + tm5 * om4 + tm6 * om8 + tm7 * om12;
  1574. result[offset + 5] = tm4 * om1 + tm5 * om5 + tm6 * om9 + tm7 * om13;
  1575. result[offset + 6] = tm4 * om2 + tm5 * om6 + tm6 * om10 + tm7 * om14;
  1576. result[offset + 7] = tm4 * om3 + tm5 * om7 + tm6 * om11 + tm7 * om15;
  1577. result[offset + 8] = tm8 * om0 + tm9 * om4 + tm10 * om8 + tm11 * om12;
  1578. result[offset + 9] = tm8 * om1 + tm9 * om5 + tm10 * om9 + tm11 * om13;
  1579. result[offset + 10] = tm8 * om2 + tm9 * om6 + tm10 * om10 + tm11 * om14;
  1580. result[offset + 11] = tm8 * om3 + tm9 * om7 + tm10 * om11 + tm11 * om15;
  1581. result[offset + 12] = tm12 * om0 + tm13 * om4 + tm14 * om8 + tm15 * om12;
  1582. result[offset + 13] = tm12 * om1 + tm13 * om5 + tm14 * om9 + tm15 * om13;
  1583. result[offset + 14] = tm12 * om2 + tm13 * om6 + tm14 * om10 + tm15 * om14;
  1584. result[offset + 15] = tm12 * om3 + tm13 * om7 + tm14 * om11 + tm15 * om15;
  1585. return this;
  1586. };
  1587. Matrix.prototype.multiplyToArraySIMD = function (other, result, offset) {
  1588. if (offset === void 0) { offset = 0; }
  1589. var tm = this.m;
  1590. var om = other.m;
  1591. var om0 = SIMD.float32x4.load(om, 0);
  1592. var om1 = SIMD.float32x4.load(om, 4);
  1593. var om2 = SIMD.float32x4.load(om, 8);
  1594. var om3 = SIMD.float32x4.load(om, 12);
  1595. var tm0 = SIMD.float32x4.load(tm, 0);
  1596. SIMD.float32x4.store(result, offset + 0, SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm0, 0, 0, 0, 0), om0), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm0, 1, 1, 1, 1), om1), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm0, 2, 2, 2, 2), om2), SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm0, 3, 3, 3, 3), om3)))));
  1597. var tm1 = SIMD.float32x4.load(tm, 4);
  1598. SIMD.float32x4.store(result, offset + 4, SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm1, 0, 0, 0, 0), om0), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm1, 1, 1, 1, 1), om1), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm1, 2, 2, 2, 2), om2), SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm1, 3, 3, 3, 3), om3)))));
  1599. var tm2 = SIMD.float32x4.load(tm, 8);
  1600. SIMD.float32x4.store(result, offset + 8, SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm2, 0, 0, 0, 0), om0), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm2, 1, 1, 1, 1), om1), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm2, 2, 2, 2, 2), om2), SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm2, 3, 3, 3, 3), om3)))));
  1601. var tm3 = SIMD.float32x4.load(tm, 12);
  1602. SIMD.float32x4.store(result, offset + 12, SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm3, 0, 0, 0, 0), om0), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm3, 1, 1, 1, 1), om1), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm3, 2, 2, 2, 2), om2), SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm3, 3, 3, 3, 3), om3)))));
  1603. };
  1604. Matrix.prototype.equals = function (value) {
  1605. 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]);
  1606. };
  1607. Matrix.prototype.clone = function () {
  1608. 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]);
  1609. };
  1610. Matrix.prototype.decompose = function (scale, rotation, translation) {
  1611. translation.x = this.m[12];
  1612. translation.y = this.m[13];
  1613. translation.z = this.m[14];
  1614. var xs = BABYLON.Tools.Sign(this.m[0] * this.m[1] * this.m[2] * this.m[3]) < 0 ? -1 : 1;
  1615. var ys = BABYLON.Tools.Sign(this.m[4] * this.m[5] * this.m[6] * this.m[7]) < 0 ? -1 : 1;
  1616. var zs = BABYLON.Tools.Sign(this.m[8] * this.m[9] * this.m[10] * this.m[11]) < 0 ? -1 : 1;
  1617. scale.x = xs * Math.sqrt(this.m[0] * this.m[0] + this.m[1] * this.m[1] + this.m[2] * this.m[2]);
  1618. scale.y = ys * Math.sqrt(this.m[4] * this.m[4] + this.m[5] * this.m[5] + this.m[6] * this.m[6]);
  1619. scale.z = zs * Math.sqrt(this.m[8] * this.m[8] + this.m[9] * this.m[9] + this.m[10] * this.m[10]);
  1620. if (scale.x === 0 || scale.y === 0 || scale.z === 0) {
  1621. rotation.x = 0;
  1622. rotation.y = 0;
  1623. rotation.z = 0;
  1624. rotation.w = 1;
  1625. return false;
  1626. }
  1627. var rotationMatrix = Matrix.FromValues(this.m[0] / scale.x, this.m[1] / scale.x, this.m[2] / scale.x, 0, this.m[4] / scale.y, this.m[5] / scale.y, this.m[6] / scale.y, 0, this.m[8] / scale.z, this.m[9] / scale.z, this.m[10] / scale.z, 0, 0, 0, 0, 1);
  1628. Quaternion.FromRotationMatrixToRef(rotationMatrix, rotation);
  1629. return true;
  1630. };
  1631. // Statics
  1632. Matrix.FromArray = function (array, offset) {
  1633. var result = new Matrix();
  1634. if (!offset) {
  1635. offset = 0;
  1636. }
  1637. Matrix.FromArrayToRef(array, offset, result);
  1638. return result;
  1639. };
  1640. Matrix.FromArrayToRef = function (array, offset, result) {
  1641. for (var index = 0; index < 16; index++) {
  1642. result.m[index] = array[index + offset];
  1643. }
  1644. };
  1645. Matrix.FromValuesToRef = function (initialM11, initialM12, initialM13, initialM14, initialM21, initialM22, initialM23, initialM24, initialM31, initialM32, initialM33, initialM34, initialM41, initialM42, initialM43, initialM44, result) {
  1646. result.m[0] = initialM11;
  1647. result.m[1] = initialM12;
  1648. result.m[2] = initialM13;
  1649. result.m[3] = initialM14;
  1650. result.m[4] = initialM21;
  1651. result.m[5] = initialM22;
  1652. result.m[6] = initialM23;
  1653. result.m[7] = initialM24;
  1654. result.m[8] = initialM31;
  1655. result.m[9] = initialM32;
  1656. result.m[10] = initialM33;
  1657. result.m[11] = initialM34;
  1658. result.m[12] = initialM41;
  1659. result.m[13] = initialM42;
  1660. result.m[14] = initialM43;
  1661. result.m[15] = initialM44;
  1662. };
  1663. Matrix.FromValues = function (initialM11, initialM12, initialM13, initialM14, initialM21, initialM22, initialM23, initialM24, initialM31, initialM32, initialM33, initialM34, initialM41, initialM42, initialM43, initialM44) {
  1664. var result = new Matrix();
  1665. result.m[0] = initialM11;
  1666. result.m[1] = initialM12;
  1667. result.m[2] = initialM13;
  1668. result.m[3] = initialM14;
  1669. result.m[4] = initialM21;
  1670. result.m[5] = initialM22;
  1671. result.m[6] = initialM23;
  1672. result.m[7] = initialM24;
  1673. result.m[8] = initialM31;
  1674. result.m[9] = initialM32;
  1675. result.m[10] = initialM33;
  1676. result.m[11] = initialM34;
  1677. result.m[12] = initialM41;
  1678. result.m[13] = initialM42;
  1679. result.m[14] = initialM43;
  1680. result.m[15] = initialM44;
  1681. return result;
  1682. };
  1683. Matrix.Compose = function (scale, rotation, translation) {
  1684. var result = Matrix.FromValues(scale.x, 0, 0, 0, 0, scale.y, 0, 0, 0, 0, scale.z, 0, 0, 0, 0, 1);
  1685. var rotationMatrix = Matrix.Identity();
  1686. rotation.toRotationMatrix(rotationMatrix);
  1687. result = result.multiply(rotationMatrix);
  1688. result.setTranslation(translation);
  1689. return result;
  1690. };
  1691. Matrix.Identity = function () {
  1692. return Matrix.FromValues(1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0);
  1693. };
  1694. Matrix.IdentityToRef = function (result) {
  1695. Matrix.FromValuesToRef(1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0, result);
  1696. };
  1697. Matrix.Zero = function () {
  1698. return Matrix.FromValues(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
  1699. };
  1700. Matrix.RotationX = function (angle) {
  1701. var result = new Matrix();
  1702. Matrix.RotationXToRef(angle, result);
  1703. return result;
  1704. };
  1705. Matrix.Invert = function (source) {
  1706. var result = new Matrix();
  1707. source.invertToRef(result);
  1708. return result;
  1709. };
  1710. Matrix.RotationXToRef = function (angle, result) {
  1711. var s = Math.sin(angle);
  1712. var c = Math.cos(angle);
  1713. result.m[0] = 1.0;
  1714. result.m[15] = 1.0;
  1715. result.m[5] = c;
  1716. result.m[10] = c;
  1717. result.m[9] = -s;
  1718. result.m[6] = s;
  1719. result.m[1] = 0;
  1720. result.m[2] = 0;
  1721. result.m[3] = 0;
  1722. result.m[4] = 0;
  1723. result.m[7] = 0;
  1724. result.m[8] = 0;
  1725. result.m[11] = 0;
  1726. result.m[12] = 0;
  1727. result.m[13] = 0;
  1728. result.m[14] = 0;
  1729. };
  1730. Matrix.RotationY = function (angle) {
  1731. var result = new Matrix();
  1732. Matrix.RotationYToRef(angle, result);
  1733. return result;
  1734. };
  1735. Matrix.RotationYToRef = function (angle, result) {
  1736. var s = Math.sin(angle);
  1737. var c = Math.cos(angle);
  1738. result.m[5] = 1.0;
  1739. result.m[15] = 1.0;
  1740. result.m[0] = c;
  1741. result.m[2] = -s;
  1742. result.m[8] = s;
  1743. result.m[10] = c;
  1744. result.m[1] = 0;
  1745. result.m[3] = 0;
  1746. result.m[4] = 0;
  1747. result.m[6] = 0;
  1748. result.m[7] = 0;
  1749. result.m[9] = 0;
  1750. result.m[11] = 0;
  1751. result.m[12] = 0;
  1752. result.m[13] = 0;
  1753. result.m[14] = 0;
  1754. };
  1755. Matrix.RotationZ = function (angle) {
  1756. var result = new Matrix();
  1757. Matrix.RotationZToRef(angle, result);
  1758. return result;
  1759. };
  1760. Matrix.RotationZToRef = function (angle, result) {
  1761. var s = Math.sin(angle);
  1762. var c = Math.cos(angle);
  1763. result.m[10] = 1.0;
  1764. result.m[15] = 1.0;
  1765. result.m[0] = c;
  1766. result.m[1] = s;
  1767. result.m[4] = -s;
  1768. result.m[5] = c;
  1769. result.m[2] = 0;
  1770. result.m[3] = 0;
  1771. result.m[6] = 0;
  1772. result.m[7] = 0;
  1773. result.m[8] = 0;
  1774. result.m[9] = 0;
  1775. result.m[11] = 0;
  1776. result.m[12] = 0;
  1777. result.m[13] = 0;
  1778. result.m[14] = 0;
  1779. };
  1780. Matrix.RotationAxis = function (axis, angle) {
  1781. var s = Math.sin(-angle);
  1782. var c = Math.cos(-angle);
  1783. var c1 = 1 - c;
  1784. axis.normalize();
  1785. var result = Matrix.Zero();
  1786. result.m[0] = (axis.x * axis.x) * c1 + c;
  1787. result.m[1] = (axis.x * axis.y) * c1 - (axis.z * s);
  1788. result.m[2] = (axis.x * axis.z) * c1 + (axis.y * s);
  1789. result.m[3] = 0.0;
  1790. result.m[4] = (axis.y * axis.x) * c1 + (axis.z * s);
  1791. result.m[5] = (axis.y * axis.y) * c1 + c;
  1792. result.m[6] = (axis.y * axis.z) * c1 - (axis.x * s);
  1793. result.m[7] = 0.0;
  1794. result.m[8] = (axis.z * axis.x) * c1 - (axis.y * s);
  1795. result.m[9] = (axis.z * axis.y) * c1 + (axis.x * s);
  1796. result.m[10] = (axis.z * axis.z) * c1 + c;
  1797. result.m[11] = 0.0;
  1798. result.m[15] = 1.0;
  1799. return result;
  1800. };
  1801. Matrix.RotationYawPitchRoll = function (yaw, pitch, roll) {
  1802. var result = new Matrix();
  1803. Matrix.RotationYawPitchRollToRef(yaw, pitch, roll, result);
  1804. return result;
  1805. };
  1806. Matrix.RotationYawPitchRollToRef = function (yaw, pitch, roll, result) {
  1807. Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, this._tempQuaternion);
  1808. this._tempQuaternion.toRotationMatrix(result);
  1809. };
  1810. Matrix.Scaling = function (x, y, z) {
  1811. var result = Matrix.Zero();
  1812. Matrix.ScalingToRef(x, y, z, result);
  1813. return result;
  1814. };
  1815. Matrix.ScalingToRef = function (x, y, z, result) {
  1816. result.m[0] = x;
  1817. result.m[1] = 0;
  1818. result.m[2] = 0;
  1819. result.m[3] = 0;
  1820. result.m[4] = 0;
  1821. result.m[5] = y;
  1822. result.m[6] = 0;
  1823. result.m[7] = 0;
  1824. result.m[8] = 0;
  1825. result.m[9] = 0;
  1826. result.m[10] = z;
  1827. result.m[11] = 0;
  1828. result.m[12] = 0;
  1829. result.m[13] = 0;
  1830. result.m[14] = 0;
  1831. result.m[15] = 1.0;
  1832. };
  1833. Matrix.Translation = function (x, y, z) {
  1834. var result = Matrix.Identity();
  1835. Matrix.TranslationToRef(x, y, z, result);
  1836. return result;
  1837. };
  1838. Matrix.TranslationToRef = function (x, y, z, result) {
  1839. Matrix.FromValuesToRef(1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0, 0, x, y, z, 1.0, result);
  1840. };
  1841. Matrix.LookAtLH = function (eye, target, up) {
  1842. var result = Matrix.Zero();
  1843. Matrix.LookAtLHToRef(eye, target, up, result);
  1844. return result;
  1845. };
  1846. Matrix.LookAtLHToRef = function (eye, target, up, result) {
  1847. // Z axis
  1848. target.subtractToRef(eye, this._zAxis);
  1849. this._zAxis.normalize();
  1850. // X axis
  1851. Vector3.CrossToRef(up, this._zAxis, this._xAxis);
  1852. this._xAxis.normalize();
  1853. // Y axis
  1854. Vector3.CrossToRef(this._zAxis, this._xAxis, this._yAxis);
  1855. this._yAxis.normalize();
  1856. // Eye angles
  1857. var ex = -Vector3.Dot(this._xAxis, eye);
  1858. var ey = -Vector3.Dot(this._yAxis, eye);
  1859. var ez = -Vector3.Dot(this._zAxis, eye);
  1860. 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);
  1861. };
  1862. Matrix.LookAtLHToRefSIMD = function (eyeRef, targetRef, upRef, result) {
  1863. var out = result.m;
  1864. var center = SIMD.float32x4(targetRef.x, targetRef.y, targetRef.z, 0);
  1865. var eye = SIMD.float32x4(eyeRef.x, eyeRef.y, eyeRef.z, 0);
  1866. var up = SIMD.float32x4(upRef.x, upRef.y, upRef.z, 0);
  1867. // cc.kmVec3Subtract(f, pCenter, pEye);
  1868. var f = SIMD.float32x4.sub(center, eye);
  1869. // cc.kmVec3Normalize(f, f);
  1870. var tmp = SIMD.float32x4.mul(f, f);
  1871. tmp = SIMD.float32x4.add(tmp, SIMD.float32x4.add(SIMD.float32x4.swizzle(tmp, 1, 2, 0, 3), SIMD.float32x4.swizzle(tmp, 2, 0, 1, 3)));
  1872. f = SIMD.float32x4.mul(f, SIMD.float32x4.reciprocalSqrtApproximation(tmp));
  1873. // cc.kmVec3Assign(up, pUp);
  1874. // cc.kmVec3Normalize(up, up);
  1875. tmp = SIMD.float32x4.mul(up, up);
  1876. tmp = SIMD.float32x4.add(tmp, SIMD.float32x4.add(SIMD.float32x4.swizzle(tmp, 1, 2, 0, 3), SIMD.float32x4.swizzle(tmp, 2, 0, 1, 3)));
  1877. up = SIMD.float32x4.mul(up, SIMD.float32x4.reciprocalSqrtApproximation(tmp));
  1878. // cc.kmVec3Cross(s, f, up);
  1879. var s = SIMD.float32x4.sub(SIMD.float32x4.mul(SIMD.float32x4.swizzle(f, 1, 2, 0, 3), SIMD.float32x4.swizzle(up, 2, 0, 1, 3)), SIMD.float32x4.mul(SIMD.float32x4.swizzle(f, 2, 0, 1, 3), SIMD.float32x4.swizzle(up, 1, 2, 0, 3)));
  1880. // cc.kmVec3Normalize(s, s);
  1881. tmp = SIMD.float32x4.mul(s, s);
  1882. tmp = SIMD.float32x4.add(tmp, SIMD.float32x4.add(SIMD.float32x4.swizzle(tmp, 1, 2, 0, 3), SIMD.float32x4.swizzle(tmp, 2, 0, 1, 3)));
  1883. s = SIMD.float32x4.mul(s, SIMD.float32x4.reciprocalSqrtApproximation(tmp));
  1884. // cc.kmVec3Cross(u, s, f);
  1885. var u = SIMD.float32x4.sub(SIMD.float32x4.mul(SIMD.float32x4.swizzle(s, 1, 2, 0, 3), SIMD.float32x4.swizzle(f, 2, 0, 1, 3)), SIMD.float32x4.mul(SIMD.float32x4.swizzle(s, 2, 0, 1, 3), SIMD.float32x4.swizzle(f, 1, 2, 0, 3)));
  1886. // cc.kmVec3Normalize(s, s);
  1887. tmp = SIMD.float32x4.mul(s, s);
  1888. tmp = SIMD.float32x4.add(tmp, SIMD.float32x4.add(SIMD.float32x4.swizzle(tmp, 1, 2, 0, 3), SIMD.float32x4.swizzle(tmp, 2, 0, 1, 3)));
  1889. s = SIMD.float32x4.mul(s, SIMD.float32x4.reciprocalSqrtApproximation(tmp));
  1890. var zero = SIMD.float32x4.splat(0.0);
  1891. s = SIMD.float32x4.neg(s);
  1892. var tmp01 = SIMD.float32x4.shuffle(s, u, 0, 1, 4, 5);
  1893. var tmp23 = SIMD.float32x4.shuffle(f, zero, 0, 1, 4, 5);
  1894. var a0 = SIMD.float32x4.shuffle(tmp01, tmp23, 0, 2, 4, 6);
  1895. var a1 = SIMD.float32x4.shuffle(tmp01, tmp23, 1, 3, 5, 7);
  1896. tmp01 = SIMD.float32x4.shuffle(s, u, 2, 3, 6, 7);
  1897. tmp23 = SIMD.float32x4.shuffle(f, zero, 2, 3, 6, 7);
  1898. var a2 = SIMD.float32x4.shuffle(tmp01, tmp23, 0, 2, 4, 6);
  1899. var a3 = SIMD.float32x4(0.0, 0.0, 0.0, 1.0);
  1900. var b0 = SIMD.float32x4(1.0, 0.0, 0.0, 0.0);
  1901. var b1 = SIMD.float32x4(0.0, 1.0, 0.0, 0.0);
  1902. var b2 = SIMD.float32x4(0.0, 0.0, 1.0, 0.0);
  1903. var b3 = SIMD.float32x4.neg(eye);
  1904. b3 = SIMD.float32x4.withW(b3, 1.0);
  1905. SIMD.float32x4.store(out, 0, SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b0, 0, 0, 0, 0), a0), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b0, 1, 1, 1, 1), a1), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b0, 2, 2, 2, 2), a2), SIMD.float32x4.mul(SIMD.float32x4.swizzle(b0, 3, 3, 3, 3), a3)))));
  1906. SIMD.float32x4.store(out, 4, SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b1, 0, 0, 0, 0), a0), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b1, 1, 1, 1, 1), a1), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b1, 2, 2, 2, 2), a2), SIMD.float32x4.mul(SIMD.float32x4.swizzle(b1, 3, 3, 3, 3), a3)))));
  1907. SIMD.float32x4.store(out, 8, SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b2, 0, 0, 0, 0), a0), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b2, 1, 1, 1, 1), a1), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b2, 2, 2, 2, 2), a2), SIMD.float32x4.mul(SIMD.float32x4.swizzle(b2, 3, 3, 3, 3), a3)))));
  1908. SIMD.float32x4.store(out, 12, SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b3, 0, 0, 0, 0), a0), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b3, 1, 1, 1, 1), a1), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b3, 2, 2, 2, 2), a2), SIMD.float32x4.mul(SIMD.float32x4.swizzle(b3, 3, 3, 3, 3), a3)))));
  1909. };
  1910. Matrix.OrthoLH = function (width, height, znear, zfar) {
  1911. var matrix = Matrix.Zero();
  1912. Matrix.OrthoLHToRef(width, height, znear, zfar, matrix);
  1913. return matrix;
  1914. };
  1915. Matrix.OrthoLHToRef = function (width, height, znear, zfar, result) {
  1916. var hw = 2.0 / width;
  1917. var hh = 2.0 / height;
  1918. var id = 1.0 / (zfar - znear);
  1919. var nid = znear / (znear - zfar);
  1920. Matrix.FromValuesToRef(hw, 0, 0, 0, 0, hh, 0, 0, 0, 0, id, 0, 0, 0, nid, 1, result);
  1921. };
  1922. Matrix.OrthoOffCenterLH = function (left, right, bottom, top, znear, zfar) {
  1923. var matrix = Matrix.Zero();
  1924. Matrix.OrthoOffCenterLHToRef(left, right, bottom, top, znear, zfar, matrix);
  1925. return matrix;
  1926. };
  1927. Matrix.OrthoOffCenterLHToRef = function (left, right, bottom, top, znear, zfar, result) {
  1928. result.m[0] = 2.0 / (right - left);
  1929. result.m[1] = result.m[2] = result.m[3] = 0;
  1930. result.m[5] = 2.0 / (top - bottom);
  1931. result.m[4] = result.m[6] = result.m[7] = 0;
  1932. result.m[10] = -1.0 / (znear - zfar);
  1933. result.m[8] = result.m[9] = result.m[11] = 0;
  1934. result.m[12] = (left + right) / (left - right);
  1935. result.m[13] = (top + bottom) / (bottom - top);
  1936. result.m[14] = znear / (znear - zfar);
  1937. result.m[15] = 1.0;
  1938. };
  1939. Matrix.PerspectiveLH = function (width, height, znear, zfar) {
  1940. var matrix = Matrix.Zero();
  1941. matrix.m[0] = (2.0 * znear) / width;
  1942. matrix.m[1] = matrix.m[2] = matrix.m[3] = 0.0;
  1943. matrix.m[5] = (2.0 * znear) / height;
  1944. matrix.m[4] = matrix.m[6] = matrix.m[7] = 0.0;
  1945. matrix.m[10] = -zfar / (znear - zfar);
  1946. matrix.m[8] = matrix.m[9] = 0.0;
  1947. matrix.m[11] = 1.0;
  1948. matrix.m[12] = matrix.m[13] = matrix.m[15] = 0.0;
  1949. matrix.m[14] = (znear * zfar) / (znear - zfar);
  1950. return matrix;
  1951. };
  1952. Matrix.PerspectiveFovLH = function (fov, aspect, znear, zfar) {
  1953. var matrix = Matrix.Zero();
  1954. Matrix.PerspectiveFovLHToRef(fov, aspect, znear, zfar, matrix);
  1955. return matrix;
  1956. };
  1957. Matrix.PerspectiveFovLHToRef = function (fov, aspect, znear, zfar, result, fovMode) {
  1958. if (fovMode === void 0) { fovMode = BABYLON.Camera.FOVMODE_VERTICAL_FIXED; }
  1959. var tan = 1.0 / (Math.tan(fov * 0.5));
  1960. var v_fixed = (fovMode === BABYLON.Camera.FOVMODE_VERTICAL_FIXED);
  1961. if (v_fixed) {
  1962. result.m[0] = tan / aspect;
  1963. }
  1964. else {
  1965. result.m[0] = tan;
  1966. }
  1967. result.m[1] = result.m[2] = result.m[3] = 0.0;
  1968. if (v_fixed) {
  1969. result.m[5] = tan;
  1970. }
  1971. else {
  1972. result.m[5] = tan * aspect;
  1973. }
  1974. result.m[4] = result.m[6] = result.m[7] = 0.0;
  1975. result.m[8] = result.m[9] = 0.0;
  1976. result.m[10] = -zfar / (znear - zfar);
  1977. result.m[11] = 1.0;
  1978. result.m[12] = result.m[13] = result.m[15] = 0.0;
  1979. result.m[14] = (znear * zfar) / (znear - zfar);
  1980. };
  1981. Matrix.GetFinalMatrix = function (viewport, world, view, projection, zmin, zmax) {
  1982. var cw = viewport.width;
  1983. var ch = viewport.height;
  1984. var cx = viewport.x;
  1985. var cy = viewport.y;
  1986. 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);
  1987. return world.multiply(view).multiply(projection).multiply(viewportMatrix);
  1988. };
  1989. Matrix.Transpose = function (matrix) {
  1990. var result = new Matrix();
  1991. result.m[0] = matrix.m[0];
  1992. result.m[1] = matrix.m[4];
  1993. result.m[2] = matrix.m[8];
  1994. result.m[3] = matrix.m[12];
  1995. result.m[4] = matrix.m[1];
  1996. result.m[5] = matrix.m[5];
  1997. result.m[6] = matrix.m[9];
  1998. result.m[7] = matrix.m[13];
  1999. result.m[8] = matrix.m[2];
  2000. result.m[9] = matrix.m[6];
  2001. result.m[10] = matrix.m[10];
  2002. result.m[11] = matrix.m[14];
  2003. result.m[12] = matrix.m[3];
  2004. result.m[13] = matrix.m[7];
  2005. result.m[14] = matrix.m[11];
  2006. result.m[15] = matrix.m[15];
  2007. return result;
  2008. };
  2009. Matrix.Reflection = function (plane) {
  2010. var matrix = new Matrix();
  2011. Matrix.ReflectionToRef(plane, matrix);
  2012. return matrix;
  2013. };
  2014. Matrix.ReflectionToRef = function (plane, result) {
  2015. plane.normalize();
  2016. var x = plane.normal.x;
  2017. var y = plane.normal.y;
  2018. var z = plane.normal.z;
  2019. var temp = -2 * x;
  2020. var temp2 = -2 * y;
  2021. var temp3 = -2 * z;
  2022. result.m[0] = (temp * x) + 1;
  2023. result.m[1] = temp2 * x;
  2024. result.m[2] = temp3 * x;
  2025. result.m[3] = 0.0;
  2026. result.m[4] = temp * y;
  2027. result.m[5] = (temp2 * y) + 1;
  2028. result.m[6] = temp3 * y;
  2029. result.m[7] = 0.0;
  2030. result.m[8] = temp * z;
  2031. result.m[9] = temp2 * z;
  2032. result.m[10] = (temp3 * z) + 1;
  2033. result.m[11] = 0.0;
  2034. result.m[12] = temp * plane.d;
  2035. result.m[13] = temp2 * plane.d;
  2036. result.m[14] = temp3 * plane.d;
  2037. result.m[15] = 1.0;
  2038. };
  2039. Matrix._tempQuaternion = new Quaternion();
  2040. Matrix._xAxis = Vector3.Zero();
  2041. Matrix._yAxis = Vector3.Zero();
  2042. Matrix._zAxis = Vector3.Zero();
  2043. return Matrix;
  2044. })();
  2045. BABYLON.Matrix = Matrix;
  2046. var Plane = (function () {
  2047. function Plane(a, b, c, d) {
  2048. this.normal = new Vector3(a, b, c);
  2049. this.d = d;
  2050. }
  2051. Plane.prototype.asArray = function () {
  2052. return [this.normal.x, this.normal.y, this.normal.z, this.d];
  2053. };
  2054. // Methods
  2055. Plane.prototype.clone = function () {
  2056. return new Plane(this.normal.x, this.normal.y, this.normal.z, this.d);
  2057. };
  2058. Plane.prototype.normalize = function () {
  2059. var norm = (Math.sqrt((this.normal.x * this.normal.x) + (this.normal.y * this.normal.y) + (this.normal.z * this.normal.z)));
  2060. var magnitude = 0;
  2061. if (norm !== 0) {
  2062. magnitude = 1.0 / norm;
  2063. }
  2064. this.normal.x *= magnitude;
  2065. this.normal.y *= magnitude;
  2066. this.normal.z *= magnitude;
  2067. this.d *= magnitude;
  2068. return this;
  2069. };
  2070. Plane.prototype.transform = function (transformation) {
  2071. var transposedMatrix = Matrix.Transpose(transformation);
  2072. var x = this.normal.x;
  2073. var y = this.normal.y;
  2074. var z = this.normal.z;
  2075. var d = this.d;
  2076. var normalX = (((x * transposedMatrix.m[0]) + (y * transposedMatrix.m[1])) + (z * transposedMatrix.m[2])) + (d * transposedMatrix.m[3]);
  2077. var normalY = (((x * transposedMatrix.m[4]) + (y * transposedMatrix.m[5])) + (z * transposedMatrix.m[6])) + (d * transposedMatrix.m[7]);
  2078. var normalZ = (((x * transposedMatrix.m[8]) + (y * transposedMatrix.m[9])) + (z * transposedMatrix.m[10])) + (d * transposedMatrix.m[11]);
  2079. var finalD = (((x * transposedMatrix.m[12]) + (y * transposedMatrix.m[13])) + (z * transposedMatrix.m[14])) + (d * transposedMatrix.m[15]);
  2080. return new Plane(normalX, normalY, normalZ, finalD);
  2081. };
  2082. Plane.prototype.dotCoordinate = function (point) {
  2083. return ((((this.normal.x * point.x) + (this.normal.y * point.y)) + (this.normal.z * point.z)) + this.d);
  2084. };
  2085. Plane.prototype.copyFromPoints = function (point1, point2, point3) {
  2086. var x1 = point2.x - point1.x;
  2087. var y1 = point2.y - point1.y;
  2088. var z1 = point2.z - point1.z;
  2089. var x2 = point3.x - point1.x;
  2090. var y2 = point3.y - point1.y;
  2091. var z2 = point3.z - point1.z;
  2092. var yz = (y1 * z2) - (z1 * y2);
  2093. var xz = (z1 * x2) - (x1 * z2);
  2094. var xy = (x1 * y2) - (y1 * x2);
  2095. var pyth = (Math.sqrt((yz * yz) + (xz * xz) + (xy * xy)));
  2096. var invPyth;
  2097. if (pyth !== 0) {
  2098. invPyth = 1.0 / pyth;
  2099. }
  2100. else {
  2101. invPyth = 0;
  2102. }
  2103. this.normal.x = yz * invPyth;
  2104. this.normal.y = xz * invPyth;
  2105. this.normal.z = xy * invPyth;
  2106. this.d = -((this.normal.x * point1.x) + (this.normal.y * point1.y) + (this.normal.z * point1.z));
  2107. return this;
  2108. };
  2109. Plane.prototype.isFrontFacingTo = function (direction, epsilon) {
  2110. var dot = Vector3.Dot(this.normal, direction);
  2111. return (dot <= epsilon);
  2112. };
  2113. Plane.prototype.signedDistanceTo = function (point) {
  2114. return Vector3.Dot(point, this.normal) + this.d;
  2115. };
  2116. // Statics
  2117. Plane.FromArray = function (array) {
  2118. return new Plane(array[0], array[1], array[2], array[3]);
  2119. };
  2120. Plane.FromPoints = function (point1, point2, point3) {
  2121. var result = new Plane(0, 0, 0, 0);
  2122. result.copyFromPoints(point1, point2, point3);
  2123. return result;
  2124. };
  2125. Plane.FromPositionAndNormal = function (origin, normal) {
  2126. var result = new Plane(0, 0, 0, 0);
  2127. normal.normalize();
  2128. result.normal = normal;
  2129. result.d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  2130. return result;
  2131. };
  2132. Plane.SignedDistanceToPlaneFromPositionAndNormal = function (origin, normal, point) {
  2133. var d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  2134. return Vector3.Dot(point, normal) + d;
  2135. };
  2136. return Plane;
  2137. })();
  2138. BABYLON.Plane = Plane;
  2139. var Viewport = (function () {
  2140. function Viewport(x, y, width, height) {
  2141. this.x = x;
  2142. this.y = y;
  2143. this.width = width;
  2144. this.height = height;
  2145. }
  2146. Viewport.prototype.toGlobal = function (engine) {
  2147. var width = engine.getRenderWidth();
  2148. var height = engine.getRenderHeight();
  2149. return new Viewport(this.x * width, this.y * height, this.width * width, this.height * height);
  2150. };
  2151. return Viewport;
  2152. })();
  2153. BABYLON.Viewport = Viewport;
  2154. var Frustum = (function () {
  2155. function Frustum() {
  2156. }
  2157. Frustum.GetPlanes = function (transform) {
  2158. var frustumPlanes = [];
  2159. for (var index = 0; index < 6; index++) {
  2160. frustumPlanes.push(new Plane(0, 0, 0, 0));
  2161. }
  2162. Frustum.GetPlanesToRef(transform, frustumPlanes);
  2163. return frustumPlanes;
  2164. };
  2165. Frustum.GetPlanesToRef = function (transform, frustumPlanes) {
  2166. // Near
  2167. frustumPlanes[0].normal.x = transform.m[3] + transform.m[2];
  2168. frustumPlanes[0].normal.y = transform.m[7] + transform.m[6];
  2169. frustumPlanes[0].normal.z = transform.m[10] + transform.m[10];
  2170. frustumPlanes[0].d = transform.m[15] + transform.m[14];
  2171. frustumPlanes[0].normalize();
  2172. // Far
  2173. frustumPlanes[1].normal.x = transform.m[3] - transform.m[2];
  2174. frustumPlanes[1].normal.y = transform.m[7] - transform.m[6];
  2175. frustumPlanes[1].normal.z = transform.m[11] - transform.m[10];
  2176. frustumPlanes[1].d = transform.m[15] - transform.m[14];
  2177. frustumPlanes[1].normalize();
  2178. // Left
  2179. frustumPlanes[2].normal.x = transform.m[3] + transform.m[0];
  2180. frustumPlanes[2].normal.y = transform.m[7] + transform.m[4];
  2181. frustumPlanes[2].normal.z = transform.m[11] + transform.m[8];
  2182. frustumPlanes[2].d = transform.m[15] + transform.m[12];
  2183. frustumPlanes[2].normalize();
  2184. // Right
  2185. frustumPlanes[3].normal.x = transform.m[3] - transform.m[0];
  2186. frustumPlanes[3].normal.y = transform.m[7] - transform.m[4];
  2187. frustumPlanes[3].normal.z = transform.m[11] - transform.m[8];
  2188. frustumPlanes[3].d = transform.m[15] - transform.m[12];
  2189. frustumPlanes[3].normalize();
  2190. // Top
  2191. frustumPlanes[4].normal.x = transform.m[3] - transform.m[1];
  2192. frustumPlanes[4].normal.y = transform.m[7] - transform.m[5];
  2193. frustumPlanes[4].normal.z = transform.m[11] - transform.m[9];
  2194. frustumPlanes[4].d = transform.m[15] - transform.m[13];
  2195. frustumPlanes[4].normalize();
  2196. // Bottom
  2197. frustumPlanes[5].normal.x = transform.m[3] + transform.m[1];
  2198. frustumPlanes[5].normal.y = transform.m[7] + transform.m[5];
  2199. frustumPlanes[5].normal.z = transform.m[11] + transform.m[9];
  2200. frustumPlanes[5].d = transform.m[15] + transform.m[13];
  2201. frustumPlanes[5].normalize();
  2202. };
  2203. return Frustum;
  2204. })();
  2205. BABYLON.Frustum = Frustum;
  2206. var Ray = (function () {
  2207. function Ray(origin, direction, length) {
  2208. if (length === void 0) { length = Number.MAX_VALUE; }
  2209. this.origin = origin;
  2210. this.direction = direction;
  2211. this.length = length;
  2212. }
  2213. // Methods
  2214. Ray.prototype.intersectsBoxMinMax = function (minimum, maximum) {
  2215. var d = 0.0;
  2216. var maxValue = Number.MAX_VALUE;
  2217. if (Math.abs(this.direction.x) < 0.0000001) {
  2218. if (this.origin.x < minimum.x || this.origin.x > maximum.x) {
  2219. return false;
  2220. }
  2221. }
  2222. else {
  2223. var inv = 1.0 / this.direction.x;
  2224. var min = (minimum.x - this.origin.x) * inv;
  2225. var max = (maximum.x - this.origin.x) * inv;
  2226. if (max === -Infinity) {
  2227. max = Infinity;
  2228. }
  2229. if (min > max) {
  2230. var temp = min;
  2231. min = max;
  2232. max = temp;
  2233. }
  2234. d = Math.max(min, d);
  2235. maxValue = Math.min(max, maxValue);
  2236. if (d > maxValue) {
  2237. return false;
  2238. }
  2239. }
  2240. if (Math.abs(this.direction.y) < 0.0000001) {
  2241. if (this.origin.y < minimum.y || this.origin.y > maximum.y) {
  2242. return false;
  2243. }
  2244. }
  2245. else {
  2246. inv = 1.0 / this.direction.y;
  2247. min = (minimum.y - this.origin.y) * inv;
  2248. max = (maximum.y - this.origin.y) * inv;
  2249. if (max === -Infinity) {
  2250. max = Infinity;
  2251. }
  2252. if (min > max) {
  2253. temp = min;
  2254. min = max;
  2255. max = temp;
  2256. }
  2257. d = Math.max(min, d);
  2258. maxValue = Math.min(max, maxValue);
  2259. if (d > maxValue) {
  2260. return false;
  2261. }
  2262. }
  2263. if (Math.abs(this.direction.z) < 0.0000001) {
  2264. if (this.origin.z < minimum.z || this.origin.z > maximum.z) {
  2265. return false;
  2266. }
  2267. }
  2268. else {
  2269. inv = 1.0 / this.direction.z;
  2270. min = (minimum.z - this.origin.z) * inv;
  2271. max = (maximum.z - this.origin.z) * inv;
  2272. if (max === -Infinity) {
  2273. max = Infinity;
  2274. }
  2275. if (min > max) {
  2276. temp = min;
  2277. min = max;
  2278. max = temp;
  2279. }
  2280. d = Math.max(min, d);
  2281. maxValue = Math.min(max, maxValue);
  2282. if (d > maxValue) {
  2283. return false;
  2284. }
  2285. }
  2286. return true;
  2287. };
  2288. Ray.prototype.intersectsBox = function (box) {
  2289. return this.intersectsBoxMinMax(box.minimum, box.maximum);
  2290. };
  2291. Ray.prototype.intersectsSphere = function (sphere) {
  2292. var x = sphere.center.x - this.origin.x;
  2293. var y = sphere.center.y - this.origin.y;
  2294. var z = sphere.center.z - this.origin.z;
  2295. var pyth = (x * x) + (y * y) + (z * z);
  2296. var rr = sphere.radius * sphere.radius;
  2297. if (pyth <= rr) {
  2298. return true;
  2299. }
  2300. var dot = (x * this.direction.x) + (y * this.direction.y) + (z * this.direction.z);
  2301. if (dot < 0.0) {
  2302. return false;
  2303. }
  2304. var temp = pyth - (dot * dot);
  2305. return temp <= rr;
  2306. };
  2307. Ray.prototype.intersectsTriangle = function (vertex0, vertex1, vertex2) {
  2308. if (!this._edge1) {
  2309. this._edge1 = Vector3.Zero();
  2310. this._edge2 = Vector3.Zero();
  2311. this._pvec = Vector3.Zero();
  2312. this._tvec = Vector3.Zero();
  2313. this._qvec = Vector3.Zero();
  2314. }
  2315. vertex1.subtractToRef(vertex0, this._edge1);
  2316. vertex2.subtractToRef(vertex0, this._edge2);
  2317. Vector3.CrossToRef(this.direction, this._edge2, this._pvec);
  2318. var det = Vector3.Dot(this._edge1, this._pvec);
  2319. if (det === 0) {
  2320. return null;
  2321. }
  2322. var invdet = 1 / det;
  2323. this.origin.subtractToRef(vertex0, this._tvec);
  2324. var bu = Vector3.Dot(this._tvec, this._pvec) * invdet;
  2325. if (bu < 0 || bu > 1.0) {
  2326. return null;
  2327. }
  2328. Vector3.CrossToRef(this._tvec, this._edge1, this._qvec);
  2329. var bv = Vector3.Dot(this.direction, this._qvec) * invdet;
  2330. if (bv < 0 || bu + bv > 1.0) {
  2331. return null;
  2332. }
  2333. //check if the distance is longer than the predefined length.
  2334. var distance = Vector3.Dot(this._edge2, this._qvec) * invdet;
  2335. if (distance > this.length) {
  2336. return null;
  2337. }
  2338. return new BABYLON.IntersectionInfo(bu, bv, distance);
  2339. };
  2340. // Statics
  2341. Ray.CreateNew = function (x, y, viewportWidth, viewportHeight, world, view, projection) {
  2342. var start = Vector3.Unproject(new Vector3(x, y, 0), viewportWidth, viewportHeight, world, view, projection);
  2343. var end = Vector3.Unproject(new Vector3(x, y, 1), viewportWidth, viewportHeight, world, view, projection);
  2344. var direction = end.subtract(start);
  2345. direction.normalize();
  2346. return new Ray(start, direction);
  2347. };
  2348. /**
  2349. * 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
  2350. * transformed to the given world matrix.
  2351. * @param origin The origin point
  2352. * @param end The end point
  2353. * @param world a matrix to transform the ray to. Default is the identity matrix.
  2354. */
  2355. Ray.CreateNewFromTo = function (origin, end, world) {
  2356. if (world === void 0) { world = Matrix.Identity(); }
  2357. var direction = end.subtract(origin);
  2358. var length = Math.sqrt((direction.x * direction.x) + (direction.y * direction.y) + (direction.z * direction.z));
  2359. direction.normalize();
  2360. return Ray.Transform(new Ray(origin, direction, length), world);
  2361. };
  2362. Ray.Transform = function (ray, matrix) {
  2363. var newOrigin = Vector3.TransformCoordinates(ray.origin, matrix);
  2364. var newDirection = Vector3.TransformNormal(ray.direction, matrix);
  2365. return new Ray(newOrigin, newDirection, ray.length);
  2366. };
  2367. return Ray;
  2368. })();
  2369. BABYLON.Ray = Ray;
  2370. (function (Space) {
  2371. Space[Space["LOCAL"] = 0] = "LOCAL";
  2372. Space[Space["WORLD"] = 1] = "WORLD";
  2373. })(BABYLON.Space || (BABYLON.Space = {}));
  2374. var Space = BABYLON.Space;
  2375. var Axis = (function () {
  2376. function Axis() {
  2377. }
  2378. Axis.X = new Vector3(1, 0, 0);
  2379. Axis.Y = new Vector3(0, 1, 0);
  2380. Axis.Z = new Vector3(0, 0, 1);
  2381. return Axis;
  2382. })();
  2383. BABYLON.Axis = Axis;
  2384. ;
  2385. var BezierCurve = (function () {
  2386. function BezierCurve() {
  2387. }
  2388. BezierCurve.interpolate = function (t, x1, y1, x2, y2) {
  2389. // Extract X (which is equal to time here)
  2390. var f0 = 1 - 3 * x2 + 3 * x1;
  2391. var f1 = 3 * x2 - 6 * x1;
  2392. var f2 = 3 * x1;
  2393. var refinedT = t;
  2394. for (var i = 0; i < 5; i++) {
  2395. var refinedT2 = refinedT * refinedT;
  2396. var refinedT3 = refinedT2 * refinedT;
  2397. var x = f0 * refinedT3 + f1 * refinedT2 + f2 * refinedT;
  2398. var slope = 1.0 / (3.0 * f0 * refinedT2 + 2.0 * f1 * refinedT + f2);
  2399. refinedT -= (x - t) * slope;
  2400. refinedT = Math.min(1, Math.max(0, refinedT));
  2401. }
  2402. // Resolve cubic bezier for the given x
  2403. return 3 * Math.pow(1 - refinedT, 2) * refinedT * y1 + 3 * (1 - refinedT) * Math.pow(refinedT, 2) * y2 + Math.pow(refinedT, 3);
  2404. };
  2405. return BezierCurve;
  2406. })();
  2407. BABYLON.BezierCurve = BezierCurve;
  2408. (function (Orientation) {
  2409. Orientation[Orientation["CW"] = 0] = "CW";
  2410. Orientation[Orientation["CCW"] = 1] = "CCW";
  2411. })(BABYLON.Orientation || (BABYLON.Orientation = {}));
  2412. var Orientation = BABYLON.Orientation;
  2413. var Angle = (function () {
  2414. function Angle(radians) {
  2415. var _this = this;
  2416. this.degrees = function () { return _this._radians * 180 / Math.PI; };
  2417. this.radians = function () { return _this._radians; };
  2418. this._radians = radians;
  2419. if (this._radians < 0)
  2420. this._radians += (2 * Math.PI);
  2421. }
  2422. Angle.BetweenTwoPoints = function (a, b) {
  2423. var delta = b.subtract(a);
  2424. var theta = Math.atan2(delta.y, delta.x);
  2425. return new Angle(theta);
  2426. };
  2427. Angle.FromRadians = function (radians) {
  2428. return new Angle(radians);
  2429. };
  2430. Angle.FromDegrees = function (degrees) {
  2431. return new Angle(degrees * Math.PI / 180);
  2432. };
  2433. return Angle;
  2434. })();
  2435. BABYLON.Angle = Angle;
  2436. var Arc2 = (function () {
  2437. function Arc2(startPoint, midPoint, endPoint) {
  2438. this.startPoint = startPoint;
  2439. this.midPoint = midPoint;
  2440. this.endPoint = endPoint;
  2441. var temp = Math.pow(midPoint.x, 2) + Math.pow(midPoint.y, 2);
  2442. var startToMid = (Math.pow(startPoint.x, 2) + Math.pow(startPoint.y, 2) - temp) / 2.;
  2443. var midToEnd = (temp - Math.pow(endPoint.x, 2) - Math.pow(endPoint.y, 2)) / 2.;
  2444. var det = (startPoint.x - midPoint.x) * (midPoint.y - endPoint.y) - (midPoint.x - endPoint.x) * (startPoint.y - midPoint.y);
  2445. this.centerPoint = new Vector2((startToMid * (midPoint.y - endPoint.y) - midToEnd * (startPoint.y - midPoint.y)) / det, ((startPoint.x - midPoint.x) * midToEnd - (midPoint.x - endPoint.x) * startToMid) / det);
  2446. this.radius = this.centerPoint.subtract(this.startPoint).length();
  2447. this.startAngle = Angle.BetweenTwoPoints(this.centerPoint, this.startPoint);
  2448. var a1 = this.startAngle.degrees();
  2449. var a2 = Angle.BetweenTwoPoints(this.centerPoint, this.midPoint).degrees();
  2450. var a3 = Angle.BetweenTwoPoints(this.centerPoint, this.endPoint).degrees();
  2451. // angles correction
  2452. if (a2 - a1 > +180.0)
  2453. a2 -= 360.0;
  2454. if (a2 - a1 < -180.0)
  2455. a2 += 360.0;
  2456. if (a3 - a2 > +180.0)
  2457. a3 -= 360.0;
  2458. if (a3 - a2 < -180.0)
  2459. a3 += 360.0;
  2460. this.orientation = (a2 - a1) < 0 ? 0 /* CW */ : 1 /* CCW */;
  2461. this.angle = Angle.FromDegrees(this.orientation === 0 /* CW */ ? a1 - a3 : a3 - a1);
  2462. }
  2463. return Arc2;
  2464. })();
  2465. BABYLON.Arc2 = Arc2;
  2466. var PathCursor = (function () {
  2467. function PathCursor(path) {
  2468. this.path = path;
  2469. this._onchange = new Array();
  2470. this.value = 0;
  2471. this.animations = new Array();
  2472. }
  2473. PathCursor.prototype.getPoint = function () {
  2474. var point = this.path.getPointAtLengthPosition(this.value);
  2475. return new Vector3(point.x, 0, point.y);
  2476. };
  2477. PathCursor.prototype.moveAhead = function (step) {
  2478. if (step === void 0) { step = 0.002; }
  2479. this.move(step);
  2480. return this;
  2481. };
  2482. PathCursor.prototype.moveBack = function (step) {
  2483. if (step === void 0) { step = 0.002; }
  2484. this.move(-step);
  2485. return this;
  2486. };
  2487. PathCursor.prototype.move = function (step) {
  2488. if (Math.abs(step) > 1) {
  2489. throw "step size should be less than 1.";
  2490. }
  2491. this.value += step;
  2492. this.ensureLimits();
  2493. this.raiseOnChange();
  2494. return this;
  2495. };
  2496. PathCursor.prototype.ensureLimits = function () {
  2497. while (this.value > 1) {
  2498. this.value -= 1;
  2499. }
  2500. while (this.value < 0) {
  2501. this.value += 1;
  2502. }
  2503. return this;
  2504. };
  2505. // used by animation engine
  2506. PathCursor.prototype.markAsDirty = function (propertyName) {
  2507. this.ensureLimits();
  2508. this.raiseOnChange();
  2509. return this;
  2510. };
  2511. PathCursor.prototype.raiseOnChange = function () {
  2512. var _this = this;
  2513. this._onchange.forEach(function (f) { return f(_this); });
  2514. return this;
  2515. };
  2516. PathCursor.prototype.onchange = function (f) {
  2517. this._onchange.push(f);
  2518. return this;
  2519. };
  2520. return PathCursor;
  2521. })();
  2522. BABYLON.PathCursor = PathCursor;
  2523. var Path2 = (function () {
  2524. function Path2(x, y) {
  2525. this._points = new Array();
  2526. this._length = 0;
  2527. this.closed = false;
  2528. this._points.push(new Vector2(x, y));
  2529. }
  2530. Path2.prototype.addLineTo = function (x, y) {
  2531. if (closed) {
  2532. BABYLON.Tools.Error("cannot add lines to closed paths");
  2533. return this;
  2534. }
  2535. var newPoint = new Vector2(x, y);
  2536. var previousPoint = this._points[this._points.length - 1];
  2537. this._points.push(newPoint);
  2538. this._length += newPoint.subtract(previousPoint).length();
  2539. return this;
  2540. };
  2541. Path2.prototype.addArcTo = function (midX, midY, endX, endY, numberOfSegments) {
  2542. if (numberOfSegments === void 0) { numberOfSegments = 36; }
  2543. if (closed) {
  2544. BABYLON.Tools.Error("cannot add arcs to closed paths");
  2545. return this;
  2546. }
  2547. var startPoint = this._points[this._points.length - 1];
  2548. var midPoint = new Vector2(midX, midY);
  2549. var endPoint = new Vector2(endX, endY);
  2550. var arc = new Arc2(startPoint, midPoint, endPoint);
  2551. var increment = arc.angle.radians() / numberOfSegments;
  2552. if (arc.orientation === 0 /* CW */)
  2553. increment *= -1;
  2554. var currentAngle = arc.startAngle.radians() + increment;
  2555. for (var i = 0; i < numberOfSegments; i++) {
  2556. var x = Math.cos(currentAngle) * arc.radius + arc.centerPoint.x;
  2557. var y = Math.sin(currentAngle) * arc.radius + arc.centerPoint.y;
  2558. this.addLineTo(x, y);
  2559. currentAngle += increment;
  2560. }
  2561. return this;
  2562. };
  2563. Path2.prototype.close = function () {
  2564. this.closed = true;
  2565. return this;
  2566. };
  2567. Path2.prototype.length = function () {
  2568. var result = this._length;
  2569. if (!this.closed) {
  2570. var lastPoint = this._points[this._points.length - 1];
  2571. var firstPoint = this._points[0];
  2572. result += (firstPoint.subtract(lastPoint).length());
  2573. }
  2574. return result;
  2575. };
  2576. Path2.prototype.getPoints = function () {
  2577. return this._points;
  2578. };
  2579. Path2.prototype.getPointAtLengthPosition = function (normalizedLengthPosition) {
  2580. if (normalizedLengthPosition < 0 || normalizedLengthPosition > 1) {
  2581. BABYLON.Tools.Error("normalized length position should be between 0 and 1.");
  2582. return Vector2.Zero();
  2583. }
  2584. var lengthPosition = normalizedLengthPosition * this.length();
  2585. var previousOffset = 0;
  2586. for (var i = 0; i < this._points.length; i++) {
  2587. var j = (i + 1) % this._points.length;
  2588. var a = this._points[i];
  2589. var b = this._points[j];
  2590. var bToA = b.subtract(a);
  2591. var nextOffset = (bToA.length() + previousOffset);
  2592. if (lengthPosition >= previousOffset && lengthPosition <= nextOffset) {
  2593. var dir = bToA.normalize();
  2594. var localOffset = lengthPosition - previousOffset;
  2595. return new Vector2(a.x + (dir.x * localOffset), a.y + (dir.y * localOffset));
  2596. }
  2597. previousOffset = nextOffset;
  2598. }
  2599. BABYLON.Tools.Error("internal error");
  2600. return Vector2.Zero();
  2601. };
  2602. Path2.StartingAt = function (x, y) {
  2603. return new Path2(x, y);
  2604. };
  2605. return Path2;
  2606. })();
  2607. BABYLON.Path2 = Path2;
  2608. var Path3D = (function () {
  2609. function Path3D(path) {
  2610. this.path = path;
  2611. this._curve = new Array();
  2612. this._distances = new Array();
  2613. this._tangents = new Array();
  2614. this._normals = new Array();
  2615. this._binormals = new Array();
  2616. this._curve = path.slice(); // copy array
  2617. this._compute();
  2618. }
  2619. Path3D.prototype.getCurve = function () {
  2620. return this._curve;
  2621. };
  2622. Path3D.prototype.getTangents = function () {
  2623. return this._tangents;
  2624. };
  2625. Path3D.prototype.getNormals = function () {
  2626. return this._normals;
  2627. };
  2628. Path3D.prototype.getBinormals = function () {
  2629. return this._binormals;
  2630. };
  2631. Path3D.prototype.getDistances = function () {
  2632. return this._distances;
  2633. };
  2634. Path3D.prototype.update = function (path) {
  2635. for (var i = 0; i < path.length; i++) {
  2636. this._curve[i] = path[i];
  2637. }
  2638. this._compute();
  2639. return this;
  2640. };
  2641. // private function compute() : computes tangents, normals and binormals
  2642. Path3D.prototype._compute = function () {
  2643. var l = this._curve.length;
  2644. // first and last tangents
  2645. this._tangents[0] = this._curve[1].subtract(this._curve[0]);
  2646. this._tangents[0].normalize();
  2647. this._tangents[l - 1] = this._curve[l - 1].subtract(this._curve[l - 2]);
  2648. this._tangents[l - 1].normalize();
  2649. // normals and binormals at first point : arbitrary vector with _normalVector()
  2650. var tg0 = this._tangents[0];
  2651. var pp0 = this._normalVector(this._curve[0], tg0);
  2652. this._normals[0] = pp0;
  2653. this._normals[0].normalize();
  2654. this._binormals[0] = Vector3.Cross(tg0, this._normals[0]);
  2655. this._normals[0].normalize();
  2656. this._distances[0] = 0;
  2657. // normals and binormals : next points
  2658. var prev; // previous vector (segment)
  2659. var cur; // current vector (segment)
  2660. var curTang; // current tangent
  2661. var prevNorm; // previous normal
  2662. var prevBinor; // previous binormal
  2663. for (var i = 1; i < l; i++) {
  2664. // tangents
  2665. prev = this._curve[i].subtract(this._curve[i - 1]);
  2666. if (i < l - 1) {
  2667. cur = this._curve[i + 1].subtract(this._curve[i]);
  2668. this._tangents[i] = prev.add(cur);
  2669. this._tangents[i].normalize();
  2670. }
  2671. this._distances[i] = this._distances[i - 1] + prev.length();
  2672. // normals and binormals
  2673. // http://www.cs.cmu.edu/afs/andrew/scs/cs/15-462/web/old/asst2camera.html
  2674. curTang = this._tangents[i];
  2675. prevNorm = this._normals[i - 1];
  2676. prevBinor = this._binormals[i - 1];
  2677. this._normals[i] = Vector3.Cross(prevBinor, curTang);
  2678. this._normals[i].normalize();
  2679. this._binormals[i] = Vector3.Cross(curTang, this._normals[i]);
  2680. this._binormals[i].normalize();
  2681. }
  2682. };
  2683. // private function normalVector(v0, vt) :
  2684. // returns an arbitrary point in the plane defined by the point v0 and the vector vt orthogonal to this plane
  2685. Path3D.prototype._normalVector = function (v0, vt) {
  2686. var point;
  2687. if (vt.x !== 1) {
  2688. point = new Vector3(1, 0, 0);
  2689. }
  2690. else if (vt.y !== 1) {
  2691. point = new Vector3(0, 1, 0);
  2692. }
  2693. else if (vt.z !== 1) {
  2694. point = new Vector3(0, 0, 1);
  2695. }
  2696. var normal0 = Vector3.Cross(vt, point);
  2697. normal0.normalize();
  2698. return normal0;
  2699. };
  2700. return Path3D;
  2701. })();
  2702. BABYLON.Path3D = Path3D;
  2703. var Curve3 = (function () {
  2704. function Curve3(points) {
  2705. this._points = points;
  2706. }
  2707. // QuadraticBezier(origin_V3, control_V3, destination_V3 )
  2708. Curve3.CreateQuadraticBezier = function (v0, v1, v2, nbPoints) {
  2709. nbPoints = nbPoints > 2 ? nbPoints : 3;
  2710. var bez = new Array();
  2711. var equation = function (t, val0, val1, val2) {
  2712. var res = (1 - t) * (1 - t) * val0 + 2 * t * (1 - t) * val1 + t * t * val2;
  2713. return res;
  2714. };
  2715. for (var i = 0; i <= nbPoints; i++) {
  2716. bez.push(new Vector3(equation(i / nbPoints, v0.x, v1.x, v2.x), equation(i / nbPoints, v0.y, v1.y, v2.y), equation(i / nbPoints, v0.z, v1.z, v2.z)));
  2717. }
  2718. return new Curve3(bez);
  2719. };
  2720. // CubicBezier(origin_V3, control1_V3, control2_V3, destination_V3)
  2721. Curve3.CreateCubicBezier = function (v0, v1, v2, v3, nbPoints) {
  2722. nbPoints = nbPoints > 3 ? nbPoints : 4;
  2723. var bez = new Array();
  2724. var equation = function (t, val0, val1, val2, val3) {
  2725. var res = (1 - t) * (1 - t) * (1 - t) * val0 + 3 * t * (1 - t) * (1 - t) * val1 + 3 * t * t * (1 - t) * val2 + t * t * t * val3;
  2726. return res;
  2727. };
  2728. for (var i = 0; i <= nbPoints; i++) {
  2729. bez.push(new Vector3(equation(i / nbPoints, v0.x, v1.x, v2.x, v3.x), equation(i / nbPoints, v0.y, v1.y, v2.y, v3.y), equation(i / nbPoints, v0.z, v1.z, v2.z, v3.z)));
  2730. }
  2731. return new Curve3(bez);
  2732. };
  2733. Curve3.prototype.getPoints = function () {
  2734. return this._points;
  2735. };
  2736. Curve3.prototype.continue = function (curve) {
  2737. var lastPoint = this._points[this._points.length - 1];
  2738. var continuedPoints = this._points.slice();
  2739. var curvePoints = curve.getPoints();
  2740. for (var i = 1; i < curvePoints.length; i++) {
  2741. continuedPoints.push(curvePoints[i].subtract(curvePoints[0]).add(lastPoint));
  2742. }
  2743. return new Curve3(continuedPoints);
  2744. };
  2745. return Curve3;
  2746. })();
  2747. BABYLON.Curve3 = Curve3;
  2748. // Vertex formats
  2749. var PositionNormalVertex = (function () {
  2750. function PositionNormalVertex(position, normal) {
  2751. if (position === void 0) { position = Vector3.Zero(); }
  2752. if (normal === void 0) { normal = Vector3.Up(); }
  2753. this.position = position;
  2754. this.normal = normal;
  2755. }
  2756. PositionNormalVertex.prototype.clone = function () {
  2757. return new PositionNormalVertex(this.position.clone(), this.normal.clone());
  2758. };
  2759. return PositionNormalVertex;
  2760. })();
  2761. BABYLON.PositionNormalVertex = PositionNormalVertex;
  2762. var PositionNormalTextureVertex = (function () {
  2763. function PositionNormalTextureVertex(position, normal, uv) {
  2764. if (position === void 0) { position = Vector3.Zero(); }
  2765. if (normal === void 0) { normal = Vector3.Up(); }
  2766. if (uv === void 0) { uv = Vector2.Zero(); }
  2767. this.position = position;
  2768. this.normal = normal;
  2769. this.uv = uv;
  2770. }
  2771. PositionNormalTextureVertex.prototype.clone = function () {
  2772. return new PositionNormalTextureVertex(this.position.clone(), this.normal.clone(), this.uv.clone());
  2773. };
  2774. return PositionNormalTextureVertex;
  2775. })();
  2776. BABYLON.PositionNormalTextureVertex = PositionNormalTextureVertex;
  2777. // SIMD
  2778. var previousMultiplyToArray = Matrix.prototype.multiplyToArray;
  2779. var previousInvertToRef = Matrix.prototype.invertToRef;
  2780. var previousLookAtLHToRef = Matrix.LookAtLHToRef;
  2781. var previousTransformCoordinatesToRef = Vector3.TransformCoordinatesToRef;
  2782. var previousTransformCoordinatesFromFloatsToRef = Vector3.TransformCoordinatesFromFloatsToRef;
  2783. var SIMDHelper = (function () {
  2784. function SIMDHelper() {
  2785. }
  2786. Object.defineProperty(SIMDHelper, "IsEnabled", {
  2787. get: function () {
  2788. return SIMDHelper._isEnabled;
  2789. },
  2790. enumerable: true,
  2791. configurable: true
  2792. });
  2793. SIMDHelper.DisableSIMD = function () {
  2794. // Replace functions
  2795. Matrix.prototype.multiplyToArray = previousMultiplyToArray;
  2796. Matrix.prototype.invertToRef = previousInvertToRef;
  2797. Matrix.LookAtLHToRef = previousLookAtLHToRef;
  2798. Vector3.TransformCoordinatesToRef = previousTransformCoordinatesToRef;
  2799. Vector3.TransformCoordinatesFromFloatsToRef = previousTransformCoordinatesFromFloatsToRef;
  2800. SIMDHelper._isEnabled = false;
  2801. };
  2802. SIMDHelper.EnableSIMD = function () {
  2803. if (window.SIMD === undefined) {
  2804. return;
  2805. }
  2806. // Replace functions
  2807. Matrix.prototype.multiplyToArray = Matrix.prototype.multiplyToArraySIMD;
  2808. Matrix.prototype.invertToRef = Matrix.prototype.invertToRefSIMD;
  2809. Matrix.LookAtLHToRef = Matrix.LookAtLHToRefSIMD;
  2810. Vector3.TransformCoordinatesToRef = Vector3.TransformCoordinatesToRefSIMD;
  2811. Vector3.TransformCoordinatesFromFloatsToRef = Vector3.TransformCoordinatesFromFloatsToRefSIMD;
  2812. Object.defineProperty(BABYLON.Vector3.prototype, "x", {
  2813. get: function () {
  2814. return this._data[0];
  2815. },
  2816. set: function (value) {
  2817. if (!this._data) {
  2818. this._data = new Float32Array(3);
  2819. }
  2820. this._data[0] = value;
  2821. }
  2822. });
  2823. Object.defineProperty(BABYLON.Vector3.prototype, "y", {
  2824. get: function () {
  2825. return this._data[1];
  2826. },
  2827. set: function (value) {
  2828. this._data[1] = value;
  2829. }
  2830. });
  2831. Object.defineProperty(BABYLON.Vector3.prototype, "z", {
  2832. get: function () {
  2833. return this._data[2];
  2834. },
  2835. set: function (value) {
  2836. this._data[2] = value;
  2837. }
  2838. });
  2839. SIMDHelper._isEnabled = true;
  2840. };
  2841. SIMDHelper._isEnabled = false;
  2842. return SIMDHelper;
  2843. })();
  2844. BABYLON.SIMDHelper = SIMDHelper;
  2845. if (window.SIMD !== undefined) {
  2846. SIMDHelper.EnableSIMD();
  2847. }
  2848. })(BABYLON || (BABYLON = {}));
  2849. //# sourceMappingURL=babylon.math.js.map