babylon.math.ts 136 KB

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