babylon.math.js 134 KB

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