babylon.math.ts 140 KB

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