babylon.math.ts 139 KB

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