babylon.math.ts 137 KB

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