babylon.math.ts 129 KB

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