babylon.math.ts 139 KB

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