babylon.math.js 99 KB

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