babylon.math.ts 146 KB

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