babylon.math.ts 203 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140
  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. /**
  8. * Boolean : true if the absolute difference between a and b is lower than epsilon (default = 1.401298E-45)
  9. */
  10. public static WithinEpsilon(a: number, b: number, epsilon: number = 1.401298E-45): boolean {
  11. var num = a - b;
  12. return -epsilon <= num && num <= epsilon;
  13. }
  14. /**
  15. * Returns a string : the upper case translation of the number i to hexadecimal.
  16. */
  17. public static ToHex(i: number): string {
  18. var str = i.toString(16);
  19. if (i <= 15) {
  20. return ("0" + str).toUpperCase();
  21. }
  22. return str.toUpperCase();
  23. }
  24. /**
  25. * Returns -1 if value is negative and +1 is value is positive.
  26. * Returns the value itself if it's equal to zero.
  27. */
  28. public static Sign(value: number): number {
  29. value = +value; // convert to a number
  30. if (value === 0 || isNaN(value))
  31. return value;
  32. return value > 0 ? 1 : -1;
  33. }
  34. /**
  35. * Returns the value itself if it's between min and max.
  36. * Returns min if the value is lower than min.
  37. * Returns max if the value is greater than max.
  38. */
  39. public static Clamp(value: number, min = 0, max = 1): number {
  40. return Math.min(max, Math.max(min, value));
  41. }
  42. }
  43. export class Color3 {
  44. /**
  45. * Creates a new Color3 object from red, green, blue values, all between 0 and 1.
  46. */
  47. constructor(public r: number = 0, public g: number = 0, public b: number = 0) {
  48. }
  49. /**
  50. * Returns a string with the Color3 current values.
  51. */
  52. public toString(): string {
  53. return "{R: " + this.r + " G:" + this.g + " B:" + this.b + "}";
  54. }
  55. /**
  56. * Returns the string "Color3".
  57. */
  58. public getClassName(): string {
  59. return "Color3";
  60. }
  61. /**
  62. * Returns the Color3 hash code.
  63. */
  64. public getHashCode(): number {
  65. let hash = this.r || 0;
  66. hash = (hash * 397) ^ (this.g || 0);
  67. hash = (hash * 397) ^ (this.b || 0);
  68. return hash;
  69. }
  70. // Operators
  71. /**
  72. * Stores in the passed array from the passed starting index the red, green, blue values as successive elements.
  73. * Returns the Color3.
  74. */
  75. public toArray(array: number[] | Float32Array, index?: number): Color3 {
  76. if (index === undefined) {
  77. index = 0;
  78. }
  79. array[index] = this.r;
  80. array[index + 1] = this.g;
  81. array[index + 2] = this.b;
  82. return this;
  83. }
  84. /**
  85. * Returns a new Color4 object from the current Color3 and the passed alpha.
  86. */
  87. public toColor4(alpha = 1): Color4 {
  88. return new Color4(this.r, this.g, this.b, alpha);
  89. }
  90. /**
  91. * Returns a new array populated with 3 numeric elements : red, green and blue values.
  92. */
  93. public asArray(): number[] {
  94. var result = [];
  95. this.toArray(result, 0);
  96. return result;
  97. }
  98. /**
  99. * Returns the luminance value (float).
  100. */
  101. public toLuminance(): number {
  102. return this.r * 0.3 + this.g * 0.59 + this.b * 0.11;
  103. }
  104. /**
  105. * Multiply each Color3 rgb values by the passed Color3 rgb values in a new Color3 object.
  106. * Returns this new object.
  107. */
  108. public multiply(otherColor: Color3): Color3 {
  109. return new Color3(this.r * otherColor.r, this.g * otherColor.g, this.b * otherColor.b);
  110. }
  111. /**
  112. * Multiply the rgb values of the Color3 and the passed Color3 and stores the result in the object "result".
  113. * Returns the current Color3.
  114. */
  115. public multiplyToRef(otherColor: Color3, result: Color3): Color3 {
  116. result.r = this.r * otherColor.r;
  117. result.g = this.g * otherColor.g;
  118. result.b = this.b * otherColor.b;
  119. return this;
  120. }
  121. /**
  122. * Boolean : True if the rgb values are equal to the passed ones.
  123. */
  124. public equals(otherColor: Color3): boolean {
  125. return otherColor && this.r === otherColor.r && this.g === otherColor.g && this.b === otherColor.b;
  126. }
  127. /**
  128. * Boolean : True if the rgb values are equal to the passed ones.
  129. */
  130. public equalsFloats(r: number, g: number, b: number): boolean {
  131. return this.r === r && this.g === g && this.b === b;
  132. }
  133. /**
  134. * Multiplies in place each rgb value by scale.
  135. * Returns the updated Color3.
  136. */
  137. public scale(scale: number): Color3 {
  138. return new Color3(this.r * scale, this.g * scale, this.b * scale);
  139. }
  140. /**
  141. * Multiplies the rgb values by scale and stores the result into "result".
  142. * Returns the unmodified current Color3.
  143. */
  144. public scaleToRef(scale: number, result: Color3): Color3 {
  145. result.r = this.r * scale;
  146. result.g = this.g * scale;
  147. result.b = this.b * scale;
  148. return this;
  149. }
  150. /**
  151. * Returns a new Color3 set with the added values of the current Color3 and of the passed one.
  152. */
  153. public add(otherColor: Color3): Color3 {
  154. return new Color3(this.r + otherColor.r, this.g + otherColor.g, this.b + otherColor.b);
  155. }
  156. /**
  157. * Stores the result of the addition of the current Color3 and passed one rgb values into "result".
  158. * Returns the unmodified current Color3.
  159. */
  160. public addToRef(otherColor: Color3, result: Color3): Color3 {
  161. result.r = this.r + otherColor.r;
  162. result.g = this.g + otherColor.g;
  163. result.b = this.b + otherColor.b;
  164. return this;
  165. }
  166. /**
  167. * Returns a new Color3 set with the subtracted values of the passed one from the current Color3 .
  168. */
  169. public subtract(otherColor: Color3): Color3 {
  170. return new Color3(this.r - otherColor.r, this.g - otherColor.g, this.b - otherColor.b);
  171. }
  172. /**
  173. * Stores the result of the subtraction of passed one from the current Color3 rgb values into "result".
  174. * Returns the unmodified current Color3.
  175. */
  176. public subtractToRef(otherColor: Color3, result: Color3): Color3 {
  177. result.r = this.r - otherColor.r;
  178. result.g = this.g - otherColor.g;
  179. result.b = this.b - otherColor.b;
  180. return this;
  181. }
  182. /**
  183. * Returns a new Color3 copied the current one.
  184. */
  185. public clone(): Color3 {
  186. return new Color3(this.r, this.g, this.b);
  187. }
  188. /**
  189. * Copies the rgb values from the source in the current Color3.
  190. * Returns the updated Color3.
  191. */
  192. public copyFrom(source: Color3): Color3 {
  193. this.r = source.r;
  194. this.g = source.g;
  195. this.b = source.b;
  196. return this;
  197. }
  198. /**
  199. * Updates the Color3 rgb values from the passed floats.
  200. * Returns the Color3.
  201. */
  202. public copyFromFloats(r: number, g: number, b: number): Color3 {
  203. this.r = r;
  204. this.g = g;
  205. this.b = b;
  206. return this;
  207. }
  208. /**
  209. * Updates the Color3 rgb values from the passed floats.
  210. * Returns the Color3.
  211. */
  212. public set(r: number, g: number, b: number): Color3 {
  213. return this.copyFromFloats(r, g, b);
  214. }
  215. /**
  216. * Returns the Color3 hexadecimal code as a string.
  217. */
  218. public toHexString(): string {
  219. var intR = (this.r * 255) | 0;
  220. var intG = (this.g * 255) | 0;
  221. var intB = (this.b * 255) | 0;
  222. return "#" + MathTools.ToHex(intR) + MathTools.ToHex(intG) + MathTools.ToHex(intB);
  223. }
  224. /**
  225. * Returns a new Color3 converted to linear space.
  226. */
  227. public toLinearSpace(): Color3 {
  228. var convertedColor = new Color3();
  229. this.toLinearSpaceToRef(convertedColor);
  230. return convertedColor;
  231. }
  232. /**
  233. * Converts the Color3 values to linear space and stores the result in "convertedColor".
  234. * Returns the unmodified Color3.
  235. */
  236. public toLinearSpaceToRef(convertedColor: Color3): Color3 {
  237. convertedColor.r = Math.pow(this.r, ToLinearSpace);
  238. convertedColor.g = Math.pow(this.g, ToLinearSpace);
  239. convertedColor.b = Math.pow(this.b, ToLinearSpace);
  240. return this;
  241. }
  242. /**
  243. * Returns a new Color3 converted to gamma space.
  244. */
  245. public toGammaSpace(): Color3 {
  246. var convertedColor = new Color3();
  247. this.toGammaSpaceToRef(convertedColor);
  248. return convertedColor;
  249. }
  250. /**
  251. * Converts the Color3 values to gamma space and stores the result in "convertedColor".
  252. * Returns the unmodified Color3.
  253. */
  254. public toGammaSpaceToRef(convertedColor: Color3): Color3 {
  255. convertedColor.r = Math.pow(this.r, ToGammaSpace);
  256. convertedColor.g = Math.pow(this.g, ToGammaSpace);
  257. convertedColor.b = Math.pow(this.b, ToGammaSpace);
  258. return this;
  259. }
  260. // Statics
  261. /**
  262. * Creates a new Color3 from the string containing valid hexadecimal values.
  263. */
  264. public static FromHexString(hex: string): Color3 {
  265. if (hex.substring(0, 1) !== "#" || hex.length !== 7) {
  266. //Tools.Warn("Color3.FromHexString must be called with a string like #FFFFFF");
  267. return new Color3(0, 0, 0);
  268. }
  269. var r = parseInt(hex.substring(1, 3), 16);
  270. var g = parseInt(hex.substring(3, 5), 16);
  271. var b = parseInt(hex.substring(5, 7), 16);
  272. return Color3.FromInts(r, g, b);
  273. }
  274. /**
  275. * Creates a new Vector3 from the startind index of the passed array.
  276. */
  277. public static FromArray(array: number[], offset: number = 0): Color3 {
  278. return new Color3(array[offset], array[offset + 1], array[offset + 2]);
  279. }
  280. /**
  281. * Creates a new Color3 from integer values ( < 256).
  282. */
  283. public static FromInts(r: number, g: number, b: number): Color3 {
  284. return new Color3(r / 255.0, g / 255.0, b / 255.0);
  285. }
  286. /**
  287. * Creates a new Color3 with values linearly interpolated of "amount" between the start Color3 and the end Color3.
  288. */
  289. public static Lerp(start: Color3, end: Color3, amount: number): Color3 {
  290. var r = start.r + ((end.r - start.r) * amount);
  291. var g = start.g + ((end.g - start.g) * amount);
  292. var b = start.b + ((end.b - start.b) * amount);
  293. return new Color3(r, g, b);
  294. }
  295. public static Red(): Color3 { return new Color3(1, 0, 0); }
  296. public static Green(): Color3 { return new Color3(0, 1, 0); }
  297. public static Blue(): Color3 { return new Color3(0, 0, 1); }
  298. public static Black(): Color3 { return new Color3(0, 0, 0); }
  299. public static White(): Color3 { return new Color3(1, 1, 1); }
  300. public static Purple(): Color3 { return new Color3(0.5, 0, 0.5); }
  301. public static Magenta(): Color3 { return new Color3(1, 0, 1); }
  302. public static Yellow(): Color3 { return new Color3(1, 1, 0); }
  303. public static Gray(): Color3 { return new Color3(0.5, 0.5, 0.5); }
  304. public static Random(): Color3 { return new Color3(Math.random(), Math.random(), Math.random()); }
  305. }
  306. export class Color4 {
  307. /**
  308. * Creates a new Color4 object from the passed float values ( < 1) : red, green, blue, alpha.
  309. */
  310. constructor(public r: number, public g: number, public b: number, public a: number) {
  311. }
  312. // Operators
  313. /**
  314. * Adds in place the passed Color4 values to the current Color4.
  315. * Returns the updated Color4.
  316. */
  317. public addInPlace(right): Color4 {
  318. this.r += right.r;
  319. this.g += right.g;
  320. this.b += right.b;
  321. this.a += right.a;
  322. return this;
  323. }
  324. /**
  325. * Returns a new array populated with 4 numeric elements : red, green, blue, alpha values.
  326. */
  327. public asArray(): number[] {
  328. var result = [];
  329. this.toArray(result, 0);
  330. return result;
  331. }
  332. /**
  333. * Stores from the starting index in the passed array the Color4 successive values.
  334. * Returns the Color4.
  335. */
  336. public toArray(array: number[], index?: number): Color4 {
  337. if (index === undefined) {
  338. index = 0;
  339. }
  340. array[index] = this.r;
  341. array[index + 1] = this.g;
  342. array[index + 2] = this.b;
  343. array[index + 3] = this.a;
  344. return this;
  345. }
  346. /**
  347. * Returns a new Color4 set with the added values of the current Color4 and of the passed one.
  348. */
  349. public add(right: Color4): Color4 {
  350. return new Color4(this.r + right.r, this.g + right.g, this.b + right.b, this.a + right.a);
  351. }
  352. /**
  353. * Returns a new Color4 set with the subtracted values of the passed one from the current Color4.
  354. */
  355. public subtract(right: Color4): Color4 {
  356. return new Color4(this.r - right.r, this.g - right.g, this.b - right.b, this.a - right.a);
  357. }
  358. /**
  359. * Subtracts the passed ones from the current Color4 values and stores the results in "result".
  360. * Returns the Color4.
  361. */
  362. public subtractToRef(right: Color4, result: Color4): Color4 {
  363. result.r = this.r - right.r;
  364. result.g = this.g - right.g;
  365. result.b = this.b - right.b;
  366. result.a = this.a - right.a;
  367. return this;
  368. }
  369. /**
  370. * Creates a new Color4 with the current Color4 values multiplied by scale.
  371. */
  372. public scale(scale: number): Color4 {
  373. return new Color4(this.r * scale, this.g * scale, this.b * scale, this.a * scale);
  374. }
  375. /**
  376. * Multiplies the current Color4 values by scale and stores the result in "result".
  377. * Returns the Color4.
  378. */
  379. public scaleToRef(scale: number, result: Color4): Color4 {
  380. result.r = this.r * scale;
  381. result.g = this.g * scale;
  382. result.b = this.b * scale;
  383. result.a = this.a * scale;
  384. return this;
  385. }
  386. /**
  387. * Multipy an RGBA Color4 value by another and return a new Color4 object
  388. * @param color The Color4 (RGBA) value to multiply by
  389. * @returns A new Color4.
  390. */
  391. public multiply(color: Color4): Color4 {
  392. return new Color4(this.r * color.r, this.g * color.g, this.b * color.b, this.a * color.a);
  393. }
  394. /**
  395. * Multipy an RGBA Color4 value by another and push the result in a reference value
  396. * @param color The Color4 (RGBA) value to multiply by
  397. * @param result The Color4 (RGBA) to fill the result in
  398. * @returns the result Color4.
  399. */
  400. public multiplyToRef(color: Color4, result: Color4): Color4 {
  401. result.r = this.r * color.r;
  402. result.g = this.g * color.g;
  403. result.b = this.b * color.b;
  404. result.a = this.a * color.a;
  405. return result;
  406. }
  407. /**
  408. * Returns a string with the Color4 values.
  409. */
  410. public toString(): string {
  411. return "{R: " + this.r + " G:" + this.g + " B:" + this.b + " A:" + this.a + "}";
  412. }
  413. /**
  414. * Returns the string "Color4"
  415. */
  416. public getClassName(): string {
  417. return "Color4";
  418. }
  419. /**
  420. * Return the Color4 hash code as a number.
  421. */
  422. public getHashCode(): number {
  423. let hash = this.r || 0;
  424. hash = (hash * 397) ^ (this.g || 0);
  425. hash = (hash * 397) ^ (this.b || 0);
  426. hash = (hash * 397) ^ (this.a || 0);
  427. return hash;
  428. }
  429. /**
  430. * Creates a new Color4 copied from the current one.
  431. */
  432. public clone(): Color4 {
  433. return new Color4(this.r, this.g, this.b, this.a);
  434. }
  435. /**
  436. * Copies the passed Color4 values into the current one.
  437. * Returns the updated Color4.
  438. */
  439. public copyFrom(source: Color4): Color4 {
  440. this.r = source.r;
  441. this.g = source.g;
  442. this.b = source.b;
  443. this.a = source.a;
  444. return this;
  445. }
  446. /**
  447. * Copies the passed float values into the current one.
  448. * Returns the updated Color4.
  449. */
  450. public copyFromFloats(r: number, g: number, b: number, a: number): Color4 {
  451. this.r = r;
  452. this.g = g;
  453. this.b = b;
  454. this.a = a;
  455. return this;
  456. }
  457. /**
  458. * Copies the passed float values into the current one.
  459. * Returns the updated Color4.
  460. */
  461. public set(r: number, g: number, b: number, a: number): Color4 {
  462. return this.copyFromFloats(r, g, b, a);
  463. }
  464. /**
  465. * Returns a string containing the hexadecimal Color4 code.
  466. */
  467. public toHexString(): string {
  468. var intR = (this.r * 255) | 0;
  469. var intG = (this.g * 255) | 0;
  470. var intB = (this.b * 255) | 0;
  471. var intA = (this.a * 255) | 0;
  472. return "#" + MathTools.ToHex(intR) + MathTools.ToHex(intG) + MathTools.ToHex(intB) + MathTools.ToHex(intA);
  473. }
  474. // Statics
  475. /**
  476. * Creates a new Color4 from the valid hexadecimal value contained in the passed string.
  477. */
  478. public static FromHexString(hex: string): Color4 {
  479. if (hex.substring(0, 1) !== "#" || hex.length !== 9) {
  480. //Tools.Warn("Color4.FromHexString must be called with a string like #FFFFFFFF");
  481. return new Color4(0.0, 0.0, 0.0, 0.0);
  482. }
  483. var r = parseInt(hex.substring(1, 3), 16);
  484. var g = parseInt(hex.substring(3, 5), 16);
  485. var b = parseInt(hex.substring(5, 7), 16);
  486. var a = parseInt(hex.substring(7, 9), 16);
  487. return Color4.FromInts(r, g, b, a);
  488. }
  489. /**
  490. * Creates a new Color4 object set with the linearly interpolated values of "amount" between the left Color4 and the right Color4.
  491. */
  492. public static Lerp(left: Color4, right: Color4, amount: number): Color4 {
  493. var result = new Color4(0.0, 0.0, 0.0, 0.0);
  494. Color4.LerpToRef(left, right, amount, result);
  495. return result;
  496. }
  497. /**
  498. * Set the passed "result" with the linearly interpolated values of "amount" between the left Color4 and the right Color4.
  499. */
  500. public static LerpToRef(left: Color4, right: Color4, amount: number, result: Color4): void {
  501. result.r = left.r + (right.r - left.r) * amount;
  502. result.g = left.g + (right.g - left.g) * amount;
  503. result.b = left.b + (right.b - left.b) * amount;
  504. result.a = left.a + (right.a - left.a) * amount;
  505. }
  506. /**
  507. * Creates a new Color4 from the starting index element of the passed array.
  508. */
  509. public static FromArray(array: number[], offset: number = 0): Color4 {
  510. return new Color4(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  511. }
  512. /**
  513. * Creates a new Color4 from the passed integers ( < 256 ).
  514. */
  515. public static FromInts(r: number, g: number, b: number, a: number): Color4 {
  516. return new Color4(r / 255.0, g / 255.0, b / 255.0, a / 255.0);
  517. }
  518. public static CheckColors4(colors: number[], count: number): number[] {
  519. // Check if color3 was used
  520. if (colors.length === count * 3) {
  521. var colors4 = [];
  522. for (var index = 0; index < colors.length; index += 3) {
  523. var newIndex = (index / 3) * 4;
  524. colors4[newIndex] = colors[index];
  525. colors4[newIndex + 1] = colors[index + 1];
  526. colors4[newIndex + 2] = colors[index + 2];
  527. colors4[newIndex + 3] = 1.0;
  528. }
  529. return colors4;
  530. }
  531. return colors;
  532. }
  533. }
  534. export class Vector2 {
  535. /**
  536. * Creates a new Vector2 from the passed x and y coordinates.
  537. */
  538. constructor(public x: number, public y: number) {
  539. }
  540. /**
  541. * Returns a string with the Vector2 coordinates.
  542. */
  543. public toString(): string {
  544. return "{X: " + this.x + " Y:" + this.y + "}";
  545. }
  546. /**
  547. * Returns the string "Vector2"
  548. */
  549. public getClassName(): string {
  550. return "Vector2";
  551. }
  552. /**
  553. * Returns the Vector2 hash code as a number.
  554. */
  555. public getHashCode(): number {
  556. let hash = this.x || 0;
  557. hash = (hash * 397) ^ (this.y || 0);
  558. return hash;
  559. }
  560. // Operators
  561. /**
  562. * Sets the Vector2 coordinates in the passed array or Float32Array from the passed index.
  563. * Returns the Vector2.
  564. */
  565. public toArray(array: number[] | Float32Array, index: number = 0): Vector2 {
  566. array[index] = this.x;
  567. array[index + 1] = this.y;
  568. return this;
  569. }
  570. /**
  571. * Returns a new array with 2 elements : the Vector2 coordinates.
  572. */
  573. public asArray(): number[] {
  574. var result = [];
  575. this.toArray(result, 0);
  576. return result;
  577. }
  578. /**
  579. * Sets the Vector2 coordinates with the passed Vector2 coordinates.
  580. * Returns the updated Vector2.
  581. */
  582. public copyFrom(source: Vector2): Vector2 {
  583. this.x = source.x;
  584. this.y = source.y;
  585. return this;
  586. }
  587. /**
  588. * Sets the Vector2 coordinates with the passed floats.
  589. * Returns the updated Vector2.
  590. */
  591. public copyFromFloats(x: number, y: number): Vector2 {
  592. this.x = x;
  593. this.y = y;
  594. return this;
  595. }
  596. /**
  597. * Sets the Vector2 coordinates with the passed floats.
  598. * Returns the updated Vector2.
  599. */
  600. public set(x: number, y: number): Vector2 {
  601. return this.copyFromFloats(x, y);
  602. }
  603. /**
  604. * Returns a new Vector2 set with the addition of the current Vector2 and the passed one coordinates.
  605. */
  606. public add(otherVector: Vector2): Vector2 {
  607. return new Vector2(this.x + otherVector.x, this.y + otherVector.y);
  608. }
  609. /**
  610. * Sets the "result" coordinates with the addition of the current Vector2 and the passed one coordinates.
  611. * Returns the Vector2.
  612. */
  613. public addToRef(otherVector: Vector2, result: Vector2): Vector2 {
  614. result.x = this.x + otherVector.x;
  615. result.y = this.y + otherVector.y;
  616. return this;
  617. }
  618. /**
  619. * Set the Vector2 coordinates by adding the passed Vector2 coordinates.
  620. * Returns the updated Vector2.
  621. */
  622. public addInPlace(otherVector: Vector2): Vector2 {
  623. this.x += otherVector.x;
  624. this.y += otherVector.y;
  625. return this;
  626. }
  627. /**
  628. * Returns a new Vector2 by adding the current Vector2 coordinates to the passed Vector3 x, y coordinates.
  629. */
  630. public addVector3(otherVector: Vector3): Vector2 {
  631. return new Vector2(this.x + otherVector.x, this.y + otherVector.y);
  632. }
  633. /**
  634. * Returns a new Vector2 set with the subtracted coordinates of the passed one from the current Vector2.
  635. */
  636. public subtract(otherVector: Vector2): Vector2 {
  637. return new Vector2(this.x - otherVector.x, this.y - otherVector.y);
  638. }
  639. /**
  640. * Sets the "result" coordinates with the subtraction of the passed one from the current Vector2 coordinates.
  641. * Returns the Vector2.
  642. */
  643. public subtractToRef(otherVector: Vector2, result: Vector2): Vector2 {
  644. result.x = this.x - otherVector.x;
  645. result.y = this.y - otherVector.y;
  646. return this;
  647. }
  648. /**
  649. * Sets the current Vector2 coordinates by subtracting from it the passed one coordinates.
  650. * Returns the updated Vector2.
  651. */
  652. public subtractInPlace(otherVector: Vector2): Vector2 {
  653. this.x -= otherVector.x;
  654. this.y -= otherVector.y;
  655. return this;
  656. }
  657. /**
  658. * Multiplies in place the current Vector2 coordinates by the passed ones.
  659. * Returns the updated Vector2.
  660. */
  661. public multiplyInPlace(otherVector: Vector2): Vector2 {
  662. this.x *= otherVector.x;
  663. this.y *= otherVector.y;
  664. return this;
  665. }
  666. /**
  667. * Returns a new Vector2 set with the multiplication of the current Vector2 and the passed one coordinates.
  668. */
  669. public multiply(otherVector: Vector2): Vector2 {
  670. return new Vector2(this.x * otherVector.x, this.y * otherVector.y);
  671. }
  672. /**
  673. * Sets "result" coordinates with the multiplication of the current Vector2 and the passed one coordinates.
  674. * Returns the Vector2.
  675. */
  676. public multiplyToRef(otherVector: Vector2, result: Vector2): Vector2 {
  677. result.x = this.x * otherVector.x;
  678. result.y = this.y * otherVector.y;
  679. return this;
  680. }
  681. /**
  682. * Returns a new Vector2 set with the Vector2 coordinates multiplied by the passed floats.
  683. */
  684. public multiplyByFloats(x: number, y: number): Vector2 {
  685. return new Vector2(this.x * x, this.y * y);
  686. }
  687. /**
  688. * Returns a new Vector2 set with the Vector2 coordinates divided by the passed one coordinates.
  689. */
  690. public divide(otherVector: Vector2): Vector2 {
  691. return new Vector2(this.x / otherVector.x, this.y / otherVector.y);
  692. }
  693. /**
  694. * Sets the "result" coordinates with the Vector2 divided by the passed one coordinates.
  695. * Returns the Vector2.
  696. */
  697. public divideToRef(otherVector: Vector2, result: Vector2): Vector2 {
  698. result.x = this.x / otherVector.x;
  699. result.y = this.y / otherVector.y;
  700. return this;
  701. }
  702. /**
  703. * Returns a new Vector2 with current Vector2 negated coordinates.
  704. */
  705. public negate(): Vector2 {
  706. return new Vector2(-this.x, -this.y);
  707. }
  708. /**
  709. * Multiply the Vector2 coordinates by scale.
  710. * Returns the updated Vector2.
  711. */
  712. public scaleInPlace(scale: number): Vector2 {
  713. this.x *= scale;
  714. this.y *= scale;
  715. return this;
  716. }
  717. /**
  718. * Returns a new Vector2 scaled by "scale" from the current Vector2.
  719. */
  720. public scale(scale: number): Vector2 {
  721. return new Vector2(this.x * scale, this.y * scale);
  722. }
  723. /**
  724. * Boolean : True if the passed vector coordinates strictly equal the current Vector2 ones.
  725. */
  726. public equals(otherVector: Vector2): boolean {
  727. return otherVector && this.x === otherVector.x && this.y === otherVector.y;
  728. }
  729. /**
  730. * Boolean : True if the passed vector coordinates are close to the current ones by a distance of epsilon.
  731. */
  732. public equalsWithEpsilon(otherVector: Vector2, epsilon: number = Epsilon): boolean {
  733. return otherVector && MathTools.WithinEpsilon(this.x, otherVector.x, epsilon) && MathTools.WithinEpsilon(this.y, otherVector.y, epsilon);
  734. }
  735. // Properties
  736. /**
  737. * Returns the vector length (float).
  738. */
  739. public length(): number {
  740. return Math.sqrt(this.x * this.x + this.y * this.y);
  741. }
  742. /**
  743. * Returns the vector squared length (float);
  744. */
  745. public lengthSquared(): number {
  746. return (this.x * this.x + this.y * this.y);
  747. }
  748. // Methods
  749. /**
  750. * Normalize the vector.
  751. * Returns the updated Vector2.
  752. */
  753. public normalize(): Vector2 {
  754. var len = this.length();
  755. if (len === 0)
  756. return this;
  757. var num = 1.0 / len;
  758. this.x *= num;
  759. this.y *= num;
  760. return this;
  761. }
  762. /**
  763. * Returns a new Vector2 copied from the Vector2.
  764. */
  765. public clone(): Vector2 {
  766. return new Vector2(this.x, this.y);
  767. }
  768. // Statics
  769. /**
  770. * Returns a new Vector2(0, 0)
  771. */
  772. public static Zero(): Vector2 {
  773. return new Vector2(0, 0);
  774. }
  775. /**
  776. * Returns a new Vector2 set from the passed index element of the passed array or Float32Array.
  777. */
  778. public static FromArray(array: number[] | Float32Array, offset: number = 0): Vector2 {
  779. return new Vector2(array[offset], array[offset + 1]);
  780. }
  781. /**
  782. * Sets "result" from the passed index element of the passed array or Float32Array.
  783. * Returns the Vector2.
  784. */
  785. public static FromArrayToRef(array: number[] | Float32Array, offset: number, result: Vector2): void {
  786. result.x = array[offset];
  787. result.y = array[offset + 1];
  788. }
  789. /**
  790. * Retuns a new Vector2 located for "amount" (float) on the CatmullRom spline defined by the passed four Vector2.
  791. */
  792. public static CatmullRom(value1: Vector2, value2: Vector2, value3: Vector2, value4: Vector2, amount: number): Vector2 {
  793. var squared = amount * amount;
  794. var cubed = amount * squared;
  795. var x = 0.5 * ((((2.0 * value2.x) + ((-value1.x + value3.x) * amount)) +
  796. (((((2.0 * value1.x) - (5.0 * value2.x)) + (4.0 * value3.x)) - value4.x) * squared)) +
  797. ((((-value1.x + (3.0 * value2.x)) - (3.0 * value3.x)) + value4.x) * cubed));
  798. var y = 0.5 * ((((2.0 * value2.y) + ((-value1.y + value3.y) * amount)) +
  799. (((((2.0 * value1.y) - (5.0 * value2.y)) + (4.0 * value3.y)) - value4.y) * squared)) +
  800. ((((-value1.y + (3.0 * value2.y)) - (3.0 * value3.y)) + value4.y) * cubed));
  801. return new Vector2(x, y);
  802. }
  803. /**
  804. * Returns a new Vector2 set with same the coordinates than "value" ones if the vector "value" is in the square defined by "min" and "max".
  805. * If a coordinate of "value" is lower than "min" coordinates, the returned Vector2 is given this "min" coordinate.
  806. * If a coordinate of "value" is greater than "max" coordinates, the returned Vector2 is given this "max" coordinate.
  807. */
  808. public static Clamp(value: Vector2, min: Vector2, max: Vector2): Vector2 {
  809. var x = value.x;
  810. x = (x > max.x) ? max.x : x;
  811. x = (x < min.x) ? min.x : x;
  812. var y = value.y;
  813. y = (y > max.y) ? max.y : y;
  814. y = (y < min.y) ? min.y : y;
  815. return new Vector2(x, y);
  816. }
  817. /**
  818. * Returns a new Vecto2 located for "amount" (float) on the Hermite spline defined by the vectors "value1", "value3", "tangent1", "tangent2".
  819. */
  820. public static Hermite(value1: Vector2, tangent1: Vector2, value2: Vector2, tangent2: Vector2, amount: number): Vector2 {
  821. var squared = amount * amount;
  822. var cubed = amount * squared;
  823. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  824. var part2 = (-2.0 * cubed) + (3.0 * squared);
  825. var part3 = (cubed - (2.0 * squared)) + amount;
  826. var part4 = cubed - squared;
  827. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  828. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  829. return new Vector2(x, y);
  830. }
  831. /**
  832. * Returns a new Vector2 located for "amount" (float) on the linear interpolation between the vector "start" adn the vector "end".
  833. */
  834. public static Lerp(start: Vector2, end: Vector2, amount: number): Vector2 {
  835. var x = start.x + ((end.x - start.x) * amount);
  836. var y = start.y + ((end.y - start.y) * amount);
  837. return new Vector2(x, y);
  838. }
  839. /**
  840. * Returns the dot product (float) of the vector "left" and the vector "right".
  841. */
  842. public static Dot(left: Vector2, right: Vector2): number {
  843. return left.x * right.x + left.y * right.y;
  844. }
  845. /**
  846. * Returns a new Vector2 equal to the normalized passed vector.
  847. */
  848. public static Normalize(vector: Vector2): Vector2 {
  849. var newVector = vector.clone();
  850. newVector.normalize();
  851. return newVector;
  852. }
  853. /**
  854. * Returns a new Vecto2 set with the minimal coordinate values from the "left" and "right" vectors.
  855. */
  856. public static Minimize(left: Vector2, right: Vector2): Vector2 {
  857. var x = (left.x < right.x) ? left.x : right.x;
  858. var y = (left.y < right.y) ? left.y : right.y;
  859. return new Vector2(x, y);
  860. }
  861. /**
  862. * Returns a new Vecto2 set with the maximal coordinate values from the "left" and "right" vectors.
  863. */
  864. public static Maximize(left: Vector2, right: Vector2): Vector2 {
  865. var x = (left.x > right.x) ? left.x : right.x;
  866. var y = (left.y > right.y) ? left.y : right.y;
  867. return new Vector2(x, y);
  868. }
  869. /**
  870. * Returns a new Vecto2 set with the transformed coordinates of the passed vector by the passed transformation matrix.
  871. */
  872. public static Transform(vector: Vector2, transformation: Matrix): Vector2 {
  873. let r = Vector2.Zero();
  874. Vector2.TransformToRef(vector, transformation, r);
  875. return r;
  876. }
  877. /**
  878. * Transforms the passed vector coordinates by the passed transformation matrix and stores the result in the vector "result" coordinates.
  879. */
  880. public static TransformToRef(vector: Vector2, transformation: Matrix, result: Vector2) {
  881. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + transformation.m[12];
  882. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + transformation.m[13];
  883. result.x = x;
  884. result.y = y;
  885. }
  886. /**
  887. * Boolean : True if the point "p" is in the triangle defined by the vertors "p0", "p1", "p2"
  888. */
  889. public static PointInTriangle(p: Vector2, p0: Vector2, p1: Vector2, p2: Vector2) {
  890. let a = 1 / 2 * (-p1.y * p2.x + p0.y * (-p1.x + p2.x) + p0.x * (p1.y - p2.y) + p1.x * p2.y);
  891. let sign = a < 0 ? -1 : 1;
  892. let s = (p0.y * p2.x - p0.x * p2.y + (p2.y - p0.y) * p.x + (p0.x - p2.x) * p.y) * sign;
  893. let t = (p0.x * p1.y - p0.y * p1.x + (p0.y - p1.y) * p.x + (p1.x - p0.x) * p.y) * sign;
  894. return s > 0 && t > 0 && (s + t) < 2 * a * sign;
  895. }
  896. /**
  897. * Returns the distance (float) between the vectors "value1" and "value2".
  898. */
  899. public static Distance(value1: Vector2, value2: Vector2): number {
  900. return Math.sqrt(Vector2.DistanceSquared(value1, value2));
  901. }
  902. /**
  903. * Returns the squared distance (float) between the vectors "value1" and "value2".
  904. */
  905. public static DistanceSquared(value1: Vector2, value2: Vector2): number {
  906. var x = value1.x - value2.x;
  907. var y = value1.y - value2.y;
  908. return (x * x) + (y * y);
  909. }
  910. /**
  911. * Returns a new Vecto2 located at the center of the vectors "value1" and "value2".
  912. */
  913. public static Center(value1: Vector2, value2: Vector2): Vector2 {
  914. var center = value1.add(value2);
  915. center.scaleInPlace(0.5);
  916. return center;
  917. }
  918. /**
  919. * Returns the shortest distance (float) between the point "p" and the segment defined by the two points "segA" and "segB".
  920. */
  921. public static DistanceOfPointFromSegment(p: Vector2, segA: Vector2, segB: Vector2): number {
  922. let l2 = Vector2.DistanceSquared(segA, segB);
  923. if (l2 === 0.0) {
  924. return Vector2.Distance(p, segA);
  925. }
  926. let v = segB.subtract(segA);
  927. let t = Math.max(0, Math.min(1, Vector2.Dot(p.subtract(segA), v) / l2));
  928. let proj = segA.add(v.multiplyByFloats(t, t));
  929. return Vector2.Distance(p, proj);
  930. }
  931. }
  932. export class Vector3 {
  933. /**
  934. * Creates a new Vector3 object from the passed x, y, z (floats) coordinates.
  935. * A Vector3 is the main object used in 3D geometry.
  936. * It can represent etiher the coordinates of a point the space, either a direction.
  937. */
  938. constructor(public x: number, public y: number, public z: number) {
  939. }
  940. /**
  941. * Returns a string with the Vector3 coordinates.
  942. */
  943. public toString(): string {
  944. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + "}";
  945. }
  946. /**
  947. * Returns the string "Vector3"
  948. */
  949. public getClassName(): string {
  950. return "Vector3";
  951. }
  952. /**
  953. * Returns the Vector hash code.
  954. */
  955. public getHashCode(): number {
  956. let hash = this.x || 0;
  957. hash = (hash * 397) ^ (this.y || 0);
  958. hash = (hash * 397) ^ (this.z || 0);
  959. return hash;
  960. }
  961. // Operators
  962. /**
  963. * Returns a new array with three elements : the coordinates the Vector3.
  964. */
  965. public asArray(): number[] {
  966. var result: number[] = [];
  967. this.toArray(result, 0);
  968. return result;
  969. }
  970. /**
  971. * Populates the passed array or Float32Array from the passed index with the successive coordinates of the Vector3.
  972. * Returns the Vector3.
  973. */
  974. public toArray(array: number[] | Float32Array, index: number = 0): Vector3 {
  975. array[index] = this.x;
  976. array[index + 1] = this.y;
  977. array[index + 2] = this.z;
  978. return this;
  979. }
  980. /**
  981. * Returns a new Quaternion object, computed from the Vector3 coordinates.
  982. */
  983. public toQuaternion(): Quaternion {
  984. var result = new Quaternion(0.0, 0.0, 0.0, 1.0);
  985. var cosxPlusz = Math.cos((this.x + this.z) * 0.5);
  986. var sinxPlusz = Math.sin((this.x + this.z) * 0.5);
  987. var coszMinusx = Math.cos((this.z - this.x) * 0.5);
  988. var sinzMinusx = Math.sin((this.z - this.x) * 0.5);
  989. var cosy = Math.cos(this.y * 0.5);
  990. var siny = Math.sin(this.y * 0.5);
  991. result.x = coszMinusx * siny;
  992. result.y = -sinzMinusx * siny;
  993. result.z = sinxPlusz * cosy;
  994. result.w = cosxPlusz * cosy;
  995. return result;
  996. }
  997. /**
  998. * Adds the passed vector to the current Vector3.
  999. * Returns the updated Vector3.
  1000. */
  1001. public addInPlace(otherVector: Vector3): Vector3 {
  1002. this.x += otherVector.x;
  1003. this.y += otherVector.y;
  1004. this.z += otherVector.z;
  1005. return this;
  1006. }
  1007. /**
  1008. * Returns a new Vector3, result of the addition the current Vector3 and the passed vector.
  1009. */
  1010. public add(otherVector: Vector3): Vector3 {
  1011. return new Vector3(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z);
  1012. }
  1013. /**
  1014. * Adds the current Vector3 to the passed one and stores the result in the vector "result".
  1015. * Returns the current Vector3.
  1016. */
  1017. public addToRef(otherVector: Vector3, result: Vector3): Vector3 {
  1018. result.x = this.x + otherVector.x;
  1019. result.y = this.y + otherVector.y;
  1020. result.z = this.z + otherVector.z;
  1021. return this;
  1022. }
  1023. /**
  1024. * Subtract the passed vector from the current Vector3.
  1025. * Returns the updated Vector3.
  1026. */
  1027. public subtractInPlace(otherVector: Vector3): Vector3 {
  1028. this.x -= otherVector.x;
  1029. this.y -= otherVector.y;
  1030. this.z -= otherVector.z;
  1031. return this;
  1032. }
  1033. /**
  1034. * Returns a new Vector3, result of the subtraction of the passed vector from the current Vector3.
  1035. */
  1036. public subtract(otherVector: Vector3): Vector3 {
  1037. return new Vector3(this.x - otherVector.x, this.y - otherVector.y, this.z - otherVector.z);
  1038. }
  1039. /**
  1040. * Subtracts the passed vector from the current Vector3 and stores the result in the vector "result".
  1041. * Returns the current Vector3.
  1042. */
  1043. public subtractToRef(otherVector: Vector3, result: Vector3): Vector3 {
  1044. result.x = this.x - otherVector.x;
  1045. result.y = this.y - otherVector.y;
  1046. result.z = this.z - otherVector.z;
  1047. return this;
  1048. }
  1049. /**
  1050. * Returns a new Vector3 set with the subtraction of the passed floats from the current Vector3 coordinates.
  1051. */
  1052. public subtractFromFloats(x: number, y: number, z: number): Vector3 {
  1053. return new Vector3(this.x - x, this.y - y, this.z - z);
  1054. }
  1055. /**
  1056. * Subtracts the passed floats from the current Vector3 coordinates and set the passed vector "result" with this result.
  1057. * Returns the current Vector3.
  1058. */
  1059. public subtractFromFloatsToRef(x: number, y: number, z: number, result: Vector3): Vector3 {
  1060. result.x = this.x - x;
  1061. result.y = this.y - y;
  1062. result.z = this.z - z;
  1063. return this;
  1064. }
  1065. /**
  1066. * Returns a new Vector3 set with the current Vector3 negated coordinates.
  1067. */
  1068. public negate(): Vector3 {
  1069. return new Vector3(-this.x, -this.y, -this.z);
  1070. }
  1071. /**
  1072. * Multiplies the Vector3 coordinates by the float "scale".
  1073. * Returns the updated Vector3.
  1074. */
  1075. public scaleInPlace(scale: number): Vector3 {
  1076. this.x *= scale;
  1077. this.y *= scale;
  1078. this.z *= scale;
  1079. return this;
  1080. }
  1081. /**
  1082. * Returns a new Vector3 set with the current Vector3 coordinates multiplied by the float "scale".
  1083. */
  1084. public scale(scale: number): Vector3 {
  1085. return new Vector3(this.x * scale, this.y * scale, this.z * scale);
  1086. }
  1087. /**
  1088. * Multiplies the current Vector3 coordinates by the float "scale" and stores the result in the passed vector "result" coordinates.
  1089. * Returns the current Vector3.
  1090. */
  1091. public scaleToRef(scale: number, result: Vector3): Vector3 {
  1092. result.x = this.x * scale;
  1093. result.y = this.y * scale;
  1094. result.z = this.z * scale;
  1095. return this;
  1096. }
  1097. /**
  1098. * Boolean : True if the current Vector3 and the passed vector coordinates are strictly equal.
  1099. */
  1100. public equals(otherVector: Vector3): boolean {
  1101. return otherVector && this.x === otherVector.x && this.y === otherVector.y && this.z === otherVector.z;
  1102. }
  1103. /**
  1104. * Boolean : True if the current Vector3 and the passed vector coordinates are distant less than epsilon.
  1105. */
  1106. public equalsWithEpsilon(otherVector: Vector3, epsilon: number = Epsilon): boolean {
  1107. return otherVector && MathTools.WithinEpsilon(this.x, otherVector.x, epsilon) && MathTools.WithinEpsilon(this.y, otherVector.y, epsilon) && MathTools.WithinEpsilon(this.z, otherVector.z, epsilon);
  1108. }
  1109. /**
  1110. * Boolean : True if the current Vector3 coordinate equal the passed floats.
  1111. */
  1112. public equalsToFloats(x: number, y: number, z: number): boolean {
  1113. return this.x === x && this.y === y && this.z === z;
  1114. }
  1115. /**
  1116. * Muliplies the current Vector3 coordinates by the passed ones.
  1117. * Returns the updated Vector3.
  1118. */
  1119. public multiplyInPlace(otherVector: Vector3): Vector3 {
  1120. this.x *= otherVector.x;
  1121. this.y *= otherVector.y;
  1122. this.z *= otherVector.z;
  1123. return this;
  1124. }
  1125. /**
  1126. * Returns a new Vector3, result of the multiplication of the current Vector3 by the passed vector.
  1127. */
  1128. public multiply(otherVector: Vector3): Vector3 {
  1129. return new Vector3(this.x * otherVector.x, this.y * otherVector.y, this.z * otherVector.z);
  1130. }
  1131. /**
  1132. * Multiplies the current Vector3 by the passed one and stores the result in the passed vector "result".
  1133. * Returns the current Vector3.
  1134. */
  1135. public multiplyToRef(otherVector: Vector3, result: Vector3): Vector3 {
  1136. result.x = this.x * otherVector.x;
  1137. result.y = this.y * otherVector.y;
  1138. result.z = this.z * otherVector.z;
  1139. return this;
  1140. }
  1141. /**
  1142. * Returns a new Vector3 set witth the result of the mulliplication of the current Vector3 coordinates by the passed floats.
  1143. */
  1144. public multiplyByFloats(x: number, y: number, z: number): Vector3 {
  1145. return new Vector3(this.x * x, this.y * y, this.z * z);
  1146. }
  1147. /**
  1148. * Returns a new Vector3 set witth the result of the division of the current Vector3 coordinates by the passed ones.
  1149. */
  1150. public divide(otherVector: Vector3): Vector3 {
  1151. return new Vector3(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z);
  1152. }
  1153. /**
  1154. * Divides the current Vector3 coordinates by the passed ones and stores the result in the passed vector "result".
  1155. * Returns the current Vector3.
  1156. */
  1157. public divideToRef(otherVector: Vector3, result: Vector3): Vector3 {
  1158. result.x = this.x / otherVector.x;
  1159. result.y = this.y / otherVector.y;
  1160. result.z = this.z / otherVector.z;
  1161. return this;
  1162. }
  1163. /**
  1164. * Updates the current Vector3 with the minimal coordinate values between its and the passed vector ones.
  1165. * Returns the updated Vector3.
  1166. */
  1167. public MinimizeInPlace(other: Vector3): Vector3 {
  1168. if (other.x < this.x) this.x = other.x;
  1169. if (other.y < this.y) this.y = other.y;
  1170. if (other.z < this.z) this.z = other.z;
  1171. return this;
  1172. }
  1173. /**
  1174. * Updates the current Vector3 with the maximal coordinate values between its and the passed vector ones.
  1175. * Returns the updated Vector3.
  1176. */
  1177. public MaximizeInPlace(other: Vector3): Vector3 {
  1178. if (other.x > this.x) this.x = other.x;
  1179. if (other.y > this.y) this.y = other.y;
  1180. if (other.z > this.z) this.z = other.z;
  1181. return this;
  1182. }
  1183. // Properties
  1184. /**
  1185. * Returns the length of the Vector3 (float).
  1186. */
  1187. public length(): number {
  1188. return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
  1189. }
  1190. /**
  1191. * Returns the squared length of the Vector3 (float).
  1192. */
  1193. public lengthSquared(): number {
  1194. return (this.x * this.x + this.y * this.y + this.z * this.z);
  1195. }
  1196. // Methods
  1197. /**
  1198. * Normalize the current Vector3.
  1199. * Returns the updated Vector3.
  1200. */
  1201. public normalize(): Vector3 {
  1202. var len = this.length();
  1203. if (len === 0 || len === 1.0)
  1204. return this;
  1205. var num = 1.0 / len;
  1206. this.x *= num;
  1207. this.y *= num;
  1208. this.z *= num;
  1209. return this;
  1210. }
  1211. /**
  1212. * Returns a new Vector3 copied from the current Vector3.
  1213. */
  1214. public clone(): Vector3 {
  1215. return new Vector3(this.x, this.y, this.z);
  1216. }
  1217. /**
  1218. * Copies the passed vector coordinates to the current Vector3 ones.
  1219. * Returns the updated Vector3.
  1220. */
  1221. public copyFrom(source: Vector3): Vector3 {
  1222. this.x = source.x;
  1223. this.y = source.y;
  1224. this.z = source.z;
  1225. return this;
  1226. }
  1227. /**
  1228. * Copies the passed floats to the current Vector3 coordinates.
  1229. * Returns the updated Vector3.
  1230. */
  1231. public copyFromFloats(x: number, y: number, z: number): Vector3 {
  1232. this.x = x;
  1233. this.y = y;
  1234. this.z = z;
  1235. return this;
  1236. }
  1237. /**
  1238. * Copies the passed floats to the current Vector3 coordinates.
  1239. * Returns the updated Vector3.
  1240. */
  1241. public set(x: number, y: number, z: number): Vector3 {
  1242. return this.copyFromFloats(x, y, z);
  1243. }
  1244. // Statics
  1245. /**
  1246. *
  1247. */
  1248. public static GetClipFactor(vector0: Vector3, vector1: Vector3, axis: Vector3, size) {
  1249. var d0 = Vector3.Dot(vector0, axis) - size;
  1250. var d1 = Vector3.Dot(vector1, axis) - size;
  1251. var s = d0 / (d0 - d1);
  1252. return s;
  1253. }
  1254. /**
  1255. * Returns a new Vector3 set from the index "offset" of the passed array or Float32Array.
  1256. */
  1257. public static FromArray(array: number[] | Float32Array, offset?: number): Vector3 {
  1258. if (!offset) {
  1259. offset = 0;
  1260. }
  1261. return new Vector3(array[offset], array[offset + 1], array[offset + 2]);
  1262. }
  1263. /**
  1264. * Returns a new Vector3 set from the index "offset" of the passed Float32Array.
  1265. */
  1266. public static FromFloatArray(array: Float32Array, offset?: number): Vector3 {
  1267. if (!offset) {
  1268. offset = 0;
  1269. }
  1270. return new Vector3(array[offset], array[offset + 1], array[offset + 2]);
  1271. }
  1272. /**
  1273. * Sets the passed vector "result" with the element values from the index "offset" of the passed array or Float32Array.
  1274. */
  1275. public static FromArrayToRef(array: number[] | Float32Array, offset: number, result: Vector3): void {
  1276. result.x = array[offset];
  1277. result.y = array[offset + 1];
  1278. result.z = array[offset + 2];
  1279. }
  1280. /**
  1281. * Sets the passed vector "result" with the element values from the index "offset" of the passed Float32Array.
  1282. */
  1283. public static FromFloatArrayToRef(array: Float32Array, offset: number, result: Vector3): void {
  1284. result.x = array[offset];
  1285. result.y = array[offset + 1];
  1286. result.z = array[offset + 2];
  1287. }
  1288. /**
  1289. * Sets the passed vector "result" with the passed floats.
  1290. */
  1291. public static FromFloatsToRef(x: number, y: number, z: number, result: Vector3): void {
  1292. result.x = x;
  1293. result.y = y;
  1294. result.z = z;
  1295. }
  1296. /**
  1297. * Returns a new Vector3 set to (0.0, 0.0, 0.0).
  1298. */
  1299. public static Zero(): Vector3 {
  1300. return new Vector3(0.0, 0.0, 0.0);
  1301. }
  1302. /**
  1303. * Returns a new Vector3 set to (0.0, 1.0, 0.0)
  1304. */
  1305. public static Up(): Vector3 {
  1306. return new Vector3(0.0, 1.0, 0.0);
  1307. }
  1308. /**
  1309. * Returns a new Vector3 set to (0.0, 0.0, 1.0)
  1310. */
  1311. public static Forward(): Vector3 {
  1312. return new Vector3(0.0, 0.0, 1.0);
  1313. }
  1314. /**
  1315. * Returns a new Vector3 set to (1.0, 0.0, 0.0)
  1316. */
  1317. public static Right(): Vector3 {
  1318. return new Vector3(1.0, 0.0, 0.0);
  1319. }
  1320. /**
  1321. * Returns a new Vector3 set to (-1.0, 0.0, 0.0)
  1322. */
  1323. public static Left(): Vector3 {
  1324. return new Vector3(-1.0, 0.0, 0.0);
  1325. }
  1326. /**
  1327. * Returns a new Vector3 set with the result of the transformation by the passed matrix of the passed vector.
  1328. * This method computes tranformed coordinates only, not transformed direction vectors.
  1329. */
  1330. public static TransformCoordinates(vector: Vector3, transformation: Matrix): Vector3 {
  1331. var result = Vector3.Zero();
  1332. Vector3.TransformCoordinatesToRef(vector, transformation, result);
  1333. return result;
  1334. }
  1335. /**
  1336. * Sets the passed vector "result" coordinates with the result of the transformation by the passed matrix of the passed vector.
  1337. * This method computes tranformed coordinates only, not transformed direction vectors.
  1338. */
  1339. public static TransformCoordinatesToRef(vector: Vector3, transformation: Matrix, result: Vector3): void {
  1340. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + (vector.z * transformation.m[8]) + transformation.m[12];
  1341. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + (vector.z * transformation.m[9]) + transformation.m[13];
  1342. var z = (vector.x * transformation.m[2]) + (vector.y * transformation.m[6]) + (vector.z * transformation.m[10]) + transformation.m[14];
  1343. var w = (vector.x * transformation.m[3]) + (vector.y * transformation.m[7]) + (vector.z * transformation.m[11]) + transformation.m[15];
  1344. result.x = x / w;
  1345. result.y = y / w;
  1346. result.z = z / w;
  1347. }
  1348. /**
  1349. * Sets the passed vector "result" coordinates with the result of the transformation by the passed matrix of the passed floats (x, y, z).
  1350. * This method computes tranformed coordinates only, not transformed direction vectors.
  1351. */
  1352. public static TransformCoordinatesFromFloatsToRef(x: number, y: number, z: number, transformation: Matrix, result: Vector3): void {
  1353. var rx = (x * transformation.m[0]) + (y * transformation.m[4]) + (z * transformation.m[8]) + transformation.m[12];
  1354. var ry = (x * transformation.m[1]) + (y * transformation.m[5]) + (z * transformation.m[9]) + transformation.m[13];
  1355. var rz = (x * transformation.m[2]) + (y * transformation.m[6]) + (z * transformation.m[10]) + transformation.m[14];
  1356. var rw = (x * transformation.m[3]) + (y * transformation.m[7]) + (z * transformation.m[11]) + transformation.m[15];
  1357. result.x = rx / rw;
  1358. result.y = ry / rw;
  1359. result.z = rz / rw;
  1360. }
  1361. /**
  1362. * Returns a new Vector3 set with the result of the normal transformation by the passed matrix of the passed vector.
  1363. * This methods computes transformed normalized direction vectors only.
  1364. */
  1365. public static TransformNormal(vector: Vector3, transformation: Matrix): Vector3 {
  1366. var result = Vector3.Zero();
  1367. Vector3.TransformNormalToRef(vector, transformation, result);
  1368. return result;
  1369. }
  1370. /**
  1371. * Sets the passed vector "result" with the result of the normal transformation by the passed matrix of the passed vector.
  1372. * This methods computes transformed normalized direction vectors only.
  1373. */
  1374. public static TransformNormalToRef(vector: Vector3, transformation: Matrix, result: Vector3): void {
  1375. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + (vector.z * transformation.m[8]);
  1376. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + (vector.z * transformation.m[9]);
  1377. var z = (vector.x * transformation.m[2]) + (vector.y * transformation.m[6]) + (vector.z * transformation.m[10]);
  1378. result.x = x;
  1379. result.y = y;
  1380. result.z = z;
  1381. }
  1382. /**
  1383. * Sets the passed vector "result" with the result of the normal transformation by the passed matrix of the passed floats (x, y, z).
  1384. * This methods computes transformed normalized direction vectors only.
  1385. */
  1386. public static TransformNormalFromFloatsToRef(x: number, y: number, z: number, transformation: Matrix, result: Vector3): void {
  1387. result.x = (x * transformation.m[0]) + (y * transformation.m[4]) + (z * transformation.m[8]);
  1388. result.y = (x * transformation.m[1]) + (y * transformation.m[5]) + (z * transformation.m[9]);
  1389. result.z = (x * transformation.m[2]) + (y * transformation.m[6]) + (z * transformation.m[10]);
  1390. }
  1391. /**
  1392. * Returns a new Vector3 located for "amount" on the CatmullRom interpolation spline defined by the vectors "value1", "value2", "value3", "value4".
  1393. */
  1394. public static CatmullRom(value1: Vector3, value2: Vector3, value3: Vector3, value4: Vector3, amount: number): Vector3 {
  1395. var squared = amount * amount;
  1396. var cubed = amount * squared;
  1397. var x = 0.5 * ((((2.0 * value2.x) + ((-value1.x + value3.x) * amount)) +
  1398. (((((2.0 * value1.x) - (5.0 * value2.x)) + (4.0 * value3.x)) - value4.x) * squared)) +
  1399. ((((-value1.x + (3.0 * value2.x)) - (3.0 * value3.x)) + value4.x) * cubed));
  1400. var y = 0.5 * ((((2.0 * value2.y) + ((-value1.y + value3.y) * amount)) +
  1401. (((((2.0 * value1.y) - (5.0 * value2.y)) + (4.0 * value3.y)) - value4.y) * squared)) +
  1402. ((((-value1.y + (3.0 * value2.y)) - (3.0 * value3.y)) + value4.y) * cubed));
  1403. var z = 0.5 * ((((2.0 * value2.z) + ((-value1.z + value3.z) * amount)) +
  1404. (((((2.0 * value1.z) - (5.0 * value2.z)) + (4.0 * value3.z)) - value4.z) * squared)) +
  1405. ((((-value1.z + (3.0 * value2.z)) - (3.0 * value3.z)) + value4.z) * cubed));
  1406. return new Vector3(x, y, z);
  1407. }
  1408. /**
  1409. * Returns a new Vector3 set with the coordinates of "value", if the vector "value" is in the cube defined by the vectors "min" and "max".
  1410. * If a coordinate value of "value" is lower than one of the "min" coordinate, then this "value" coordinate is set with the "min" one.
  1411. * If a coordinate value of "value" is greater than one of the "max" coordinate, then this "value" coordinate is set with the "max" one.
  1412. */
  1413. public static Clamp(value: Vector3, min: Vector3, max: Vector3): Vector3 {
  1414. var x = value.x;
  1415. x = (x > max.x) ? max.x : x;
  1416. x = (x < min.x) ? min.x : x;
  1417. var y = value.y;
  1418. y = (y > max.y) ? max.y : y;
  1419. y = (y < min.y) ? min.y : y;
  1420. var z = value.z;
  1421. z = (z > max.z) ? max.z : z;
  1422. z = (z < min.z) ? min.z : z;
  1423. return new Vector3(x, y, z);
  1424. }
  1425. /**
  1426. * Returns a new Vector3 located for "amount" (float) on the Hermite interpolation spline defined by the vectors "value1", "tangent1", "value2", "tangent2".
  1427. */
  1428. public static Hermite(value1: Vector3, tangent1: Vector3, value2: Vector3, tangent2: Vector3, amount: number): Vector3 {
  1429. var squared = amount * amount;
  1430. var cubed = amount * squared;
  1431. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  1432. var part2 = (-2.0 * cubed) + (3.0 * squared);
  1433. var part3 = (cubed - (2.0 * squared)) + amount;
  1434. var part4 = cubed - squared;
  1435. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  1436. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  1437. var z = (((value1.z * part1) + (value2.z * part2)) + (tangent1.z * part3)) + (tangent2.z * part4);
  1438. return new Vector3(x, y, z);
  1439. }
  1440. /**
  1441. * Returns a new Vector3 located for "amount" (float) on the linear interpolation between the vectors "start" and "end".
  1442. */
  1443. public static Lerp(start: Vector3, end: Vector3, amount: number): Vector3 {
  1444. var result = new Vector3(0, 0, 0);
  1445. Vector3.LerpToRef(start, end, amount, result);
  1446. return result;
  1447. }
  1448. /**
  1449. * Sets the passed vector "result" with the result of the linear interpolation from the vector "start" for "amount" to the vector "end".
  1450. */
  1451. public static LerpToRef(start: Vector3, end: Vector3, amount: number, result: Vector3): void {
  1452. result.x = start.x + ((end.x - start.x) * amount);
  1453. result.y = start.y + ((end.y - start.y) * amount);
  1454. result.z = start.z + ((end.z - start.z) * amount);
  1455. }
  1456. /**
  1457. * Returns the dot product (float) between the vectors "left" and "right".
  1458. */
  1459. public static Dot(left: Vector3, right: Vector3): number {
  1460. return (left.x * right.x + left.y * right.y + left.z * right.z);
  1461. }
  1462. /**
  1463. * Returns a new Vector3 as the cross product of the vectors "left" and "right".
  1464. * The cross product is then orthogonal to both "left" and "right".
  1465. */
  1466. public static Cross(left: Vector3, right: Vector3): Vector3 {
  1467. var result = Vector3.Zero();
  1468. Vector3.CrossToRef(left, right, result);
  1469. return result;
  1470. }
  1471. /**
  1472. * Sets the passed vector "result" with the cross product of "left" and "right".
  1473. * The cross product is then orthogonal to both "left" and "right".
  1474. */
  1475. public static CrossToRef(left: Vector3, right: Vector3, result: Vector3): void {
  1476. MathTmp.Vector3[0].x = left.y * right.z - left.z * right.y;
  1477. MathTmp.Vector3[0].y = left.z * right.x - left.x * right.z;
  1478. MathTmp.Vector3[0].z = left.x * right.y - left.y * right.x;
  1479. result.copyFrom(MathTmp.Vector3[0]);
  1480. }
  1481. /**
  1482. * Returns a new Vector3 as the normalization of the passed vector.
  1483. */
  1484. public static Normalize(vector: Vector3): Vector3 {
  1485. var result = Vector3.Zero();
  1486. Vector3.NormalizeToRef(vector, result);
  1487. return result;
  1488. }
  1489. /**
  1490. * Sets the passed vector "result" with the normalization of the passed first vector.
  1491. */
  1492. public static NormalizeToRef(vector: Vector3, result: Vector3): void {
  1493. result.copyFrom(vector);
  1494. result.normalize();
  1495. }
  1496. private static _viewportMatrixCache: Matrix;
  1497. private static _matrixCache: Matrix;
  1498. public static Project(vector: Vector3, world: Matrix, transform: Matrix, viewport: Viewport): Vector3 {
  1499. var cw = viewport.width;
  1500. var ch = viewport.height;
  1501. var cx = viewport.x;
  1502. var cy = viewport.y;
  1503. var viewportMatrix = Vector3._viewportMatrixCache ? Vector3._viewportMatrixCache : (Vector3._viewportMatrixCache = new Matrix());
  1504. Matrix.FromValuesToRef(
  1505. cw / 2.0, 0, 0, 0,
  1506. 0, -ch / 2.0, 0, 0,
  1507. 0, 0, 0.5, 0,
  1508. cx + cw / 2.0, ch / 2.0 + cy, 0.5, 1, viewportMatrix);
  1509. var matrix = Vector3._matrixCache ? Vector3._matrixCache : (Vector3._matrixCache = new Matrix());
  1510. world.multiplyToRef(transform, matrix);
  1511. matrix.multiplyToRef(viewportMatrix, matrix);
  1512. return Vector3.TransformCoordinates(vector, matrix);
  1513. }
  1514. public static UnprojectFromTransform(source: Vector3, viewportWidth: number, viewportHeight: number, world: Matrix, transform: Matrix): Vector3 {
  1515. var matrix = Vector3._matrixCache ? Vector3._matrixCache : (Vector3._matrixCache = new Matrix());
  1516. world.multiplyToRef(transform, matrix);
  1517. matrix.invert();
  1518. source.x = source.x / viewportWidth * 2 - 1;
  1519. source.y = -(source.y / viewportHeight * 2 - 1);
  1520. var vector = Vector3.TransformCoordinates(source, matrix);
  1521. var num = source.x * matrix.m[3] + source.y * matrix.m[7] + source.z * matrix.m[11] + matrix.m[15];
  1522. if (MathTools.WithinEpsilon(num, 1.0)) {
  1523. vector = vector.scale(1.0 / num);
  1524. }
  1525. return vector;
  1526. }
  1527. public static Unproject(source: Vector3, viewportWidth: number, viewportHeight: number, world: Matrix, view: Matrix, projection: Matrix): Vector3 {
  1528. var matrix = Vector3._matrixCache ? Vector3._matrixCache : (Vector3._matrixCache = new Matrix());
  1529. world.multiplyToRef(view, matrix)
  1530. matrix.multiplyToRef(projection, matrix);
  1531. matrix.invert();
  1532. var screenSource = new Vector3(source.x / viewportWidth * 2 - 1, -(source.y / viewportHeight * 2 - 1), 2 * source.z - 1.0);
  1533. var vector = Vector3.TransformCoordinates(screenSource, matrix);
  1534. var num = screenSource.x * matrix.m[3] + screenSource.y * matrix.m[7] + screenSource.z * matrix.m[11] + matrix.m[15];
  1535. if (MathTools.WithinEpsilon(num, 1.0)) {
  1536. vector = vector.scale(1.0 / num);
  1537. }
  1538. return vector;
  1539. }
  1540. public static Minimize(left: Vector3, right: Vector3): Vector3 {
  1541. var min = left.clone();
  1542. min.MinimizeInPlace(right);
  1543. return min;
  1544. }
  1545. public static Maximize(left: Vector3, right: Vector3): Vector3 {
  1546. var max = left.clone();
  1547. max.MaximizeInPlace(right);
  1548. return max;
  1549. }
  1550. /**
  1551. * Returns the distance (float) between the vectors "value1" and "value2".
  1552. */
  1553. public static Distance(value1: Vector3, value2: Vector3): number {
  1554. return Math.sqrt(Vector3.DistanceSquared(value1, value2));
  1555. }
  1556. /**
  1557. * Returns the squared distance (float) between the vectors "value1" and "value2".
  1558. */
  1559. public static DistanceSquared(value1: Vector3, value2: Vector3): number {
  1560. var x = value1.x - value2.x;
  1561. var y = value1.y - value2.y;
  1562. var z = value1.z - value2.z;
  1563. return (x * x) + (y * y) + (z * z);
  1564. }
  1565. /**
  1566. * Returns a new Vector3 located at the center between "value1" and "value2".
  1567. */
  1568. public static Center(value1: Vector3, value2: Vector3): Vector3 {
  1569. var center = value1.add(value2);
  1570. center.scaleInPlace(0.5);
  1571. return center;
  1572. }
  1573. /**
  1574. * Given three orthogonal normalized left-handed oriented Vector3 axis in space (target system),
  1575. * RotationFromAxis() returns the rotation Euler angles (ex : rotation.x, rotation.y, rotation.z) to apply
  1576. * to something in order to rotate it from its local system to the given target system.
  1577. * Note : axis1, axis2 and axis3 are normalized during this operation.
  1578. * Returns a new Vector3.
  1579. */
  1580. public static RotationFromAxis(axis1: Vector3, axis2: Vector3, axis3: Vector3): Vector3 {
  1581. var rotation = Vector3.Zero();
  1582. Vector3.RotationFromAxisToRef(axis1, axis2, axis3, rotation);
  1583. return rotation;
  1584. }
  1585. /**
  1586. * The same than RotationFromAxis but updates the passed ref Vector3 parameter instead of returning a new Vector3.
  1587. */
  1588. public static RotationFromAxisToRef(axis1: Vector3, axis2: Vector3, axis3: Vector3, ref: Vector3): void {
  1589. var quat = MathTmp.Quaternion[0];
  1590. Quaternion.RotationQuaternionFromAxisToRef(axis1, axis2, axis3, quat);
  1591. quat.toEulerAnglesToRef(ref);
  1592. }
  1593. }
  1594. //Vector4 class created for EulerAngle class conversion to Quaternion
  1595. export class Vector4 {
  1596. /**
  1597. * Creates a Vector4 object from the passed floats.
  1598. */
  1599. constructor(public x: number, public y: number, public z: number, public w: number) { }
  1600. /**
  1601. * Returns the string with the Vector4 coordinates.
  1602. */
  1603. public toString(): string {
  1604. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + " W:" + this.w + "}";
  1605. }
  1606. /**
  1607. * Returns the string "Vector4".
  1608. */
  1609. public getClassName(): string {
  1610. return "Vector4";
  1611. }
  1612. /**
  1613. * Returns the Vector4 hash code.
  1614. */
  1615. public getHashCode(): number {
  1616. let hash = this.x || 0;
  1617. hash = (hash * 397) ^ (this.y || 0);
  1618. hash = (hash * 397) ^ (this.z || 0);
  1619. hash = (hash * 397) ^ (this.w || 0);
  1620. return hash;
  1621. }
  1622. // Operators
  1623. /**
  1624. * Returns a new array populated with 4 elements : the Vector4 coordinates.
  1625. */
  1626. public asArray(): number[] {
  1627. var result = [];
  1628. this.toArray(result, 0);
  1629. return result;
  1630. }
  1631. /**
  1632. * Populates the passed array from the passed index with the Vector4 coordinates.
  1633. * Returns the Vector4.
  1634. */
  1635. public toArray(array: number[] | Float32Array, index?: number): Vector4 {
  1636. if (index === undefined) {
  1637. index = 0;
  1638. }
  1639. array[index] = this.x;
  1640. array[index + 1] = this.y;
  1641. array[index + 2] = this.z;
  1642. array[index + 3] = this.w;
  1643. return this;
  1644. }
  1645. /**
  1646. * Adds the passed vector to the current Vector4.
  1647. * Returns the updated Vector4.
  1648. */
  1649. public addInPlace(otherVector: Vector4): Vector4 {
  1650. this.x += otherVector.x;
  1651. this.y += otherVector.y;
  1652. this.z += otherVector.z;
  1653. this.w += otherVector.w;
  1654. return this;
  1655. }
  1656. /**
  1657. * Returns a new Vector4 as the result of the addition of the current Vector4 and the passed one.
  1658. */
  1659. public add(otherVector: Vector4): Vector4 {
  1660. return new Vector4(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z, this.w + otherVector.w);
  1661. }
  1662. /**
  1663. * Updates the passed vector "result" with the result of the addition of the current Vector4 and the passed one.
  1664. * Returns the current Vector4.
  1665. */
  1666. public addToRef(otherVector: Vector4, result: Vector4): Vector4 {
  1667. result.x = this.x + otherVector.x;
  1668. result.y = this.y + otherVector.y;
  1669. result.z = this.z + otherVector.z;
  1670. result.w = this.w + otherVector.w;
  1671. return this;
  1672. }
  1673. /**
  1674. * Subtract in place the passed vector from the current Vector4.
  1675. * Returns the updated Vector4.
  1676. */
  1677. public subtractInPlace(otherVector: Vector4): Vector4 {
  1678. this.x -= otherVector.x;
  1679. this.y -= otherVector.y;
  1680. this.z -= otherVector.z;
  1681. this.w -= otherVector.w;
  1682. return this;
  1683. }
  1684. /**
  1685. * Returns a new Vector4 with the result of the subtraction of the passed vector from the current Vector4.
  1686. */
  1687. public subtract(otherVector: Vector4): Vector4 {
  1688. return new Vector4(this.x - otherVector.x, this.y - otherVector.y, this.z - otherVector.z, this.w - otherVector.w);
  1689. }
  1690. /**
  1691. * Sets the passed vector "result" with the result of the subtraction of the passed vector from the current Vector4.
  1692. * Returns the current Vector4.
  1693. */
  1694. public subtractToRef(otherVector: Vector4, result: Vector4): Vector4 {
  1695. result.x = this.x - otherVector.x;
  1696. result.y = this.y - otherVector.y;
  1697. result.z = this.z - otherVector.z;
  1698. result.w = this.w - otherVector.w;
  1699. return this;
  1700. }
  1701. /**
  1702. * Returns a new Vector4 set with the result of the subtraction of the passed floats from the current Vector4 coordinates.
  1703. */
  1704. public subtractFromFloats(x: number, y: number, z: number, w: number): Vector4 {
  1705. return new Vector4(this.x - x, this.y - y, this.z - z, this.w - w);
  1706. }
  1707. /**
  1708. * Sets the passed vector "result" set with the result of the subtraction of the passed floats from the current Vector4 coordinates.
  1709. * Returns the current Vector4.
  1710. */
  1711. public subtractFromFloatsToRef(x: number, y: number, z: number, w: number, result: Vector4): Vector4 {
  1712. result.x = this.x - x;
  1713. result.y = this.y - y;
  1714. result.z = this.z - z;
  1715. result.w = this.w - w;
  1716. return this;
  1717. }
  1718. /**
  1719. * Returns a new Vector4 set with the current Vector4 negated coordinates.
  1720. */
  1721. public negate(): Vector4 {
  1722. return new Vector4(-this.x, -this.y, -this.z, -this.w);
  1723. }
  1724. /**
  1725. * Multiplies the current Vector4 coordinates by scale (float).
  1726. * Returns the updated Vector4.
  1727. */
  1728. public scaleInPlace(scale: number): Vector4 {
  1729. this.x *= scale;
  1730. this.y *= scale;
  1731. this.z *= scale;
  1732. this.w *= scale;
  1733. return this;
  1734. }
  1735. /**
  1736. * Returns a new Vector4 set with the current Vector4 coordinates multiplied by scale (float).
  1737. */
  1738. public scale(scale: number): Vector4 {
  1739. return new Vector4(this.x * scale, this.y * scale, this.z * scale, this.w * scale);
  1740. }
  1741. /**
  1742. * Sets the passed vector "result" with the current Vector4 coordinates multiplied by scale (float).
  1743. * Returns the current Vector4.
  1744. */
  1745. public scaleToRef(scale: number, result: Vector4): Vector4 {
  1746. result.x = this.x * scale;
  1747. result.y = this.y * scale;
  1748. result.z = this.z * scale;
  1749. result.w = this.w * scale;
  1750. return this;
  1751. }
  1752. /**
  1753. * Boolean : True if the current Vector4 coordinates are stricly equal to the passed ones.
  1754. */
  1755. public equals(otherVector: Vector4): boolean {
  1756. return otherVector && this.x === otherVector.x && this.y === otherVector.y && this.z === otherVector.z && this.w === otherVector.w;
  1757. }
  1758. /**
  1759. * Boolean : True if the current Vector4 coordinates are each beneath the distance "epsilon" from the passed vector ones.
  1760. */
  1761. public equalsWithEpsilon(otherVector: Vector4, epsilon: number = Epsilon): boolean {
  1762. return otherVector
  1763. && MathTools.WithinEpsilon(this.x, otherVector.x, epsilon)
  1764. && MathTools.WithinEpsilon(this.y, otherVector.y, epsilon)
  1765. && MathTools.WithinEpsilon(this.z, otherVector.z, epsilon)
  1766. && MathTools.WithinEpsilon(this.w, otherVector.w, epsilon);
  1767. }
  1768. /**
  1769. * Boolean : True if the passed floats are strictly equal to the current Vector4 coordinates.
  1770. */
  1771. public equalsToFloats(x: number, y: number, z: number, w: number): boolean {
  1772. return this.x === x && this.y === y && this.z === z && this.w === w;
  1773. }
  1774. /**
  1775. * Multiplies in place the current Vector4 by the passed one.
  1776. * Returns the updated Vector4.
  1777. */
  1778. public multiplyInPlace(otherVector: Vector4): Vector4 {
  1779. this.x *= otherVector.x;
  1780. this.y *= otherVector.y;
  1781. this.z *= otherVector.z;
  1782. this.w *= otherVector.w;
  1783. return this;
  1784. }
  1785. /**
  1786. * Returns a new Vector4 set with the multiplication result of the current Vector4 and the passed one.
  1787. */
  1788. public multiply(otherVector: Vector4): Vector4 {
  1789. return new Vector4(this.x * otherVector.x, this.y * otherVector.y, this.z * otherVector.z, this.w * otherVector.w);
  1790. }
  1791. /**
  1792. * Updates the passed vector "result" with the multiplication result of the current Vector4 and the passed one.
  1793. * Returns the current Vector4.
  1794. */
  1795. public multiplyToRef(otherVector: Vector4, result: Vector4): Vector4 {
  1796. result.x = this.x * otherVector.x;
  1797. result.y = this.y * otherVector.y;
  1798. result.z = this.z * otherVector.z;
  1799. result.w = this.w * otherVector.w;
  1800. return this;
  1801. }
  1802. /**
  1803. * Returns a new Vector4 set with the multiplication result of the passed floats and the current Vector4 coordinates.
  1804. */
  1805. public multiplyByFloats(x: number, y: number, z: number, w: number): Vector4 {
  1806. return new Vector4(this.x * x, this.y * y, this.z * z, this.w * w);
  1807. }
  1808. /**
  1809. * Returns a new Vector4 set with the division result of the current Vector4 by the passed one.
  1810. */
  1811. public divide(otherVector: Vector4): Vector4 {
  1812. return new Vector4(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z, this.w / otherVector.w);
  1813. }
  1814. /**
  1815. * Updates the passed vector "result" with the division result of the current Vector4 by the passed one.
  1816. * Returns the current Vector4.
  1817. */
  1818. public divideToRef(otherVector: Vector4, result: Vector4): Vector4 {
  1819. result.x = this.x / otherVector.x;
  1820. result.y = this.y / otherVector.y;
  1821. result.z = this.z / otherVector.z;
  1822. result.w = this.w / otherVector.w;
  1823. return this;
  1824. }
  1825. /**
  1826. * Updates the Vector4 coordinates with the minimum values between its own and the passed vector ones.
  1827. */
  1828. public MinimizeInPlace(other: Vector4): Vector4 {
  1829. if (other.x < this.x) this.x = other.x;
  1830. if (other.y < this.y) this.y = other.y;
  1831. if (other.z < this.z) this.z = other.z;
  1832. if (other.w < this.w) this.w = other.w;
  1833. return this;
  1834. }
  1835. /**
  1836. * Updates the Vector4 coordinates with the maximum values between its own and the passed vector ones.
  1837. */
  1838. public MaximizeInPlace(other: Vector4): Vector4 {
  1839. if (other.x > this.x) this.x = other.x;
  1840. if (other.y > this.y) this.y = other.y;
  1841. if (other.z > this.z) this.z = other.z;
  1842. if (other.w > this.w) this.w = other.w;
  1843. return this;
  1844. }
  1845. // Properties
  1846. /**
  1847. * Returns the Vector4 length (float).
  1848. */
  1849. public length(): number {
  1850. return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
  1851. }
  1852. /**
  1853. * Returns the Vector4 squared length (float).
  1854. */
  1855. public lengthSquared(): number {
  1856. return (this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
  1857. }
  1858. // Methods
  1859. /**
  1860. * Normalizes in place the Vector4.
  1861. * Returns the updated Vector4.
  1862. */
  1863. public normalize(): Vector4 {
  1864. var len = this.length();
  1865. if (len === 0)
  1866. return this;
  1867. var num = 1.0 / len;
  1868. this.x *= num;
  1869. this.y *= num;
  1870. this.z *= num;
  1871. this.w *= num;
  1872. return this;
  1873. }
  1874. /**
  1875. * Returns a new Vector3 from the Vector4 (x, y, z) coordinates.
  1876. */
  1877. public toVector3(): Vector3 {
  1878. return new Vector3(this.x, this.y, this.z);
  1879. }
  1880. /**
  1881. * Returns a new Vector4 copied from the current one.
  1882. */
  1883. public clone(): Vector4 {
  1884. return new Vector4(this.x, this.y, this.z, this.w);
  1885. }
  1886. /**
  1887. * Updates the current Vector4 with the passed one coordinates.
  1888. * Returns the updated Vector4.
  1889. */
  1890. public copyFrom(source: Vector4): Vector4 {
  1891. this.x = source.x;
  1892. this.y = source.y;
  1893. this.z = source.z;
  1894. this.w = source.w;
  1895. return this;
  1896. }
  1897. /**
  1898. * Updates the current Vector4 coordinates with the passed floats.
  1899. * Returns the updated Vector4.
  1900. */
  1901. public copyFromFloats(x: number, y: number, z: number, w: number): Vector4 {
  1902. this.x = x;
  1903. this.y = y;
  1904. this.z = z;
  1905. this.w = w;
  1906. return this;
  1907. }
  1908. /**
  1909. * Updates the current Vector4 coordinates with the passed floats.
  1910. * Returns the updated Vector4.
  1911. */
  1912. public set(x: number, y: number, z: number, w: number): Vector4 {
  1913. return this.copyFromFloats(x, y, z, w);
  1914. }
  1915. // Statics
  1916. /**
  1917. * Returns a new Vector4 set from the starting index of the passed array.
  1918. */
  1919. public static FromArray(array: number[] | Float32Array, offset?: number): Vector4 {
  1920. if (!offset) {
  1921. offset = 0;
  1922. }
  1923. return new Vector4(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  1924. }
  1925. /**
  1926. * Updates the passed vector "result" from the starting index of the passed array.
  1927. */
  1928. public static FromArrayToRef(array: number[] | Float32Array, offset: number, result: Vector4): void {
  1929. result.x = array[offset];
  1930. result.y = array[offset + 1];
  1931. result.z = array[offset + 2];
  1932. result.w = array[offset + 3];
  1933. }
  1934. /**
  1935. * Updates the passed vector "result" from the starting index of the passed Float32Array.
  1936. */
  1937. public static FromFloatArrayToRef(array: Float32Array, offset: number, result: Vector4): void {
  1938. result.x = array[offset];
  1939. result.y = array[offset + 1];
  1940. result.z = array[offset + 2];
  1941. result.w = array[offset + 3];
  1942. }
  1943. /**
  1944. * Updates the passed vector "result" coordinates from the passed floats.
  1945. */
  1946. public static FromFloatsToRef(x: number, y: number, z: number, w: number, result: Vector4): void {
  1947. result.x = x;
  1948. result.y = y;
  1949. result.z = z;
  1950. result.w = w;
  1951. }
  1952. /**
  1953. * Returns a new Vector4 set to (0.0, 0.0, 0.0, 0.0)
  1954. */
  1955. public static Zero(): Vector4 {
  1956. return new Vector4(0.0, 0.0, 0.0, 0.0);
  1957. }
  1958. /**
  1959. * Returns a new normalized Vector4 from the passed one.
  1960. */
  1961. public static Normalize(vector: Vector4): Vector4 {
  1962. var result = Vector4.Zero();
  1963. Vector4.NormalizeToRef(vector, result);
  1964. return result;
  1965. }
  1966. /**
  1967. * Updates the passed vector "result" from the normalization of the passed one.
  1968. */
  1969. public static NormalizeToRef(vector: Vector4, result: Vector4): void {
  1970. result.copyFrom(vector);
  1971. result.normalize();
  1972. }
  1973. public static Minimize(left: Vector4, right: Vector4): Vector4 {
  1974. var min = left.clone();
  1975. min.MinimizeInPlace(right);
  1976. return min;
  1977. }
  1978. public static Maximize(left: Vector4, right: Vector4): Vector4 {
  1979. var max = left.clone();
  1980. max.MaximizeInPlace(right);
  1981. return max;
  1982. }
  1983. /**
  1984. * Returns the distance (float) between the vectors "value1" and "value2".
  1985. */
  1986. public static Distance(value1: Vector4, value2: Vector4): number {
  1987. return Math.sqrt(Vector4.DistanceSquared(value1, value2));
  1988. }
  1989. /**
  1990. * Returns the squared distance (float) between the vectors "value1" and "value2".
  1991. */
  1992. public static DistanceSquared(value1: Vector4, value2: Vector4): number {
  1993. var x = value1.x - value2.x;
  1994. var y = value1.y - value2.y;
  1995. var z = value1.z - value2.z;
  1996. var w = value1.w - value2.w;
  1997. return (x * x) + (y * y) + (z * z) + (w * w);
  1998. }
  1999. /**
  2000. * Returns a new Vector4 located at the center between the vectors "value1" and "value2".
  2001. */
  2002. public static Center(value1: Vector4, value2: Vector4): Vector4 {
  2003. var center = value1.add(value2);
  2004. center.scaleInPlace(0.5);
  2005. return center;
  2006. }
  2007. /**
  2008. * Returns a new Vector4 set with the result of the normal transformation by the passed matrix of the passed vector.
  2009. * This methods computes transformed normalized direction vectors only.
  2010. */
  2011. public static TransformNormal(vector: Vector4, transformation: Matrix): Vector4 {
  2012. var result = Vector4.Zero();
  2013. Vector4.TransformNormalToRef(vector, transformation, result);
  2014. return result;
  2015. }
  2016. /**
  2017. * Sets the passed vector "result" with the result of the normal transformation by the passed matrix of the passed vector.
  2018. * This methods computes transformed normalized direction vectors only.
  2019. */
  2020. public static TransformNormalToRef(vector: Vector4, transformation: Matrix, result: Vector4): void {
  2021. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + (vector.z * transformation.m[8]);
  2022. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + (vector.z * transformation.m[9]);
  2023. var z = (vector.x * transformation.m[2]) + (vector.y * transformation.m[6]) + (vector.z * transformation.m[10]);
  2024. result.x = x;
  2025. result.y = y;
  2026. result.z = z;
  2027. result.w = vector.w;
  2028. }
  2029. /**
  2030. * Sets the passed vector "result" with the result of the normal transformation by the passed matrix of the passed floats (x, y, z, w).
  2031. * This methods computes transformed normalized direction vectors only.
  2032. */
  2033. public static TransformNormalFromFloatsToRef(x: number, y: number, z: number, w: number, transformation: Matrix, result: Vector4): void {
  2034. result.x = (x * transformation.m[0]) + (y * transformation.m[4]) + (z * transformation.m[8]);
  2035. result.y = (x * transformation.m[1]) + (y * transformation.m[5]) + (z * transformation.m[9]);
  2036. result.z = (x * transformation.m[2]) + (y * transformation.m[6]) + (z * transformation.m[10]);
  2037. result.w = w;
  2038. }
  2039. }
  2040. export interface ISize {
  2041. width: number;
  2042. height: number;
  2043. }
  2044. export class Size implements ISize {
  2045. width: number;
  2046. height: number;
  2047. /**
  2048. * Creates a Size object from the passed width and height (floats).
  2049. */
  2050. public constructor(width: number, height: number) {
  2051. this.width = width;
  2052. this.height = height;
  2053. }
  2054. // Returns a string with the Size width and height.
  2055. public toString(): string {
  2056. return `{W: ${this.width}, H: ${this.height}}`;
  2057. }
  2058. /**
  2059. * Returns the string "Size"
  2060. */
  2061. public getClassName(): string {
  2062. return "Size";
  2063. }
  2064. /**
  2065. * Returns the Size hash code.
  2066. */
  2067. public getHashCode(): number {
  2068. let hash = this.width || 0;
  2069. hash = (hash * 397) ^ (this.height || 0);
  2070. return hash;
  2071. }
  2072. /**
  2073. * Updates the current size from the passed one.
  2074. * Returns the updated Size.
  2075. */
  2076. public copyFrom(src: Size) {
  2077. this.width = src.width;
  2078. this.height = src.height;
  2079. }
  2080. /**
  2081. * Updates in place the current Size from the passed floats.
  2082. * Returns the updated Size.
  2083. */
  2084. public copyFromFloats(width: number, height: number): Size {
  2085. this.width = width;
  2086. this.height = height;
  2087. return this;
  2088. }
  2089. /**
  2090. * Updates in place the current Size from the passed floats.
  2091. * Returns the updated Size.
  2092. */
  2093. public set(width: number, height: number): Size {
  2094. return this.copyFromFloats(width, height);
  2095. }
  2096. /**
  2097. * Returns a new Size set with the multiplication result of the current Size and the passed floats.
  2098. */
  2099. public multiplyByFloats(w: number, h: number): Size {
  2100. return new Size(this.width * w, this.height * h);
  2101. }
  2102. /**
  2103. * Returns a new Size copied from the passed one.
  2104. */
  2105. public clone(): Size {
  2106. return new Size(this.width, this.height);
  2107. }
  2108. /**
  2109. * Boolean : True if the current Size and the passed one width and height are strictly equal.
  2110. */
  2111. public equals(other: Size): boolean {
  2112. if (!other) {
  2113. return false;
  2114. }
  2115. return (this.width === other.width) && (this.height === other.height);
  2116. }
  2117. /**
  2118. * Returns the surface of the Size : width * height (float).
  2119. */
  2120. public get surface(): number {
  2121. return this.width * this.height;
  2122. }
  2123. /**
  2124. * Returns a new Size set to (0.0, 0.0)
  2125. */
  2126. public static Zero(): Size {
  2127. return new Size(0.0, 0.0);
  2128. }
  2129. /**
  2130. * Returns a new Size set as the addition result of the current Size and the passed one.
  2131. */
  2132. public add(otherSize: Size): Size {
  2133. let r = new Size(this.width + otherSize.width, this.height + otherSize.height);
  2134. return r;
  2135. }
  2136. /**
  2137. * Returns a new Size set as the subtraction result of the passed one from the current Size.
  2138. */
  2139. public subtract(otherSize: Size): Size {
  2140. let r = new Size(this.width - otherSize.width, this.height - otherSize.height);
  2141. return r;
  2142. }
  2143. /**
  2144. * Returns a new Size set at the linear interpolation "amount" between "start" and "end".
  2145. */
  2146. public static Lerp(start: Size, end: Size, amount: number): Size {
  2147. var w = start.width + ((end.width - start.width) * amount);
  2148. var h = start.height + ((end.height - start.height) * amount);
  2149. return new Size(w, h);
  2150. }
  2151. }
  2152. export class Quaternion {
  2153. /**
  2154. * Creates a new Quaternion from the passed floats.
  2155. */
  2156. constructor(public x: number = 0.0, public y: number = 0.0, public z: number = 0.0, public w: number = 1.0) {
  2157. }
  2158. /**
  2159. * Returns a string with the Quaternion coordinates.
  2160. */
  2161. public toString(): string {
  2162. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + " W:" + this.w + "}";
  2163. }
  2164. /**
  2165. * Returns the string "Quaternion".
  2166. */
  2167. public getClassName(): string {
  2168. return "Quaternion";
  2169. }
  2170. /**
  2171. * Returns the Quaternion hash code.
  2172. */
  2173. public getHashCode(): number {
  2174. let hash = this.x || 0;
  2175. hash = (hash * 397) ^ (this.y || 0);
  2176. hash = (hash * 397) ^ (this.z || 0);
  2177. hash = (hash * 397) ^ (this.w || 0);
  2178. return hash;
  2179. }
  2180. /**
  2181. * Returns a new array populated with 4 elements : the Quaternion coordinates.
  2182. */
  2183. public asArray(): number[] {
  2184. return [this.x, this.y, this.z, this.w];
  2185. }
  2186. /**
  2187. * Boolean : True if the current Quaterion and the passed one coordinates are strictly equal.
  2188. */
  2189. public equals(otherQuaternion: Quaternion): boolean {
  2190. return otherQuaternion && this.x === otherQuaternion.x && this.y === otherQuaternion.y && this.z === otherQuaternion.z && this.w === otherQuaternion.w;
  2191. }
  2192. /**
  2193. * Returns a new Quaternion copied from the current one.
  2194. */
  2195. public clone(): Quaternion {
  2196. return new Quaternion(this.x, this.y, this.z, this.w);
  2197. }
  2198. /**
  2199. * Updates the current Quaternion from the passed one coordinates.
  2200. * Returns the updated Quaterion.
  2201. */
  2202. public copyFrom(other: Quaternion): Quaternion {
  2203. this.x = other.x;
  2204. this.y = other.y;
  2205. this.z = other.z;
  2206. this.w = other.w;
  2207. return this;
  2208. }
  2209. /**
  2210. * Updates the current Quaternion from the passed float coordinates.
  2211. * Returns the updated Quaterion.
  2212. */
  2213. public copyFromFloats(x: number, y: number, z: number, w: number): Quaternion {
  2214. this.x = x;
  2215. this.y = y;
  2216. this.z = z;
  2217. this.w = w;
  2218. return this;
  2219. }
  2220. /**
  2221. * Updates the current Quaternion from the passed float coordinates.
  2222. * Returns the updated Quaterion.
  2223. */
  2224. public set(x: number, y: number, z: number, w: number): Quaternion {
  2225. return this.copyFromFloats(x, y, z, w);
  2226. }
  2227. /**
  2228. * Returns a new Quaternion as the addition result of the passed one and the current Quaternion.
  2229. */
  2230. public add(other: Quaternion): Quaternion {
  2231. return new Quaternion(this.x + other.x, this.y + other.y, this.z + other.z, this.w + other.w);
  2232. }
  2233. /**
  2234. * Returns a new Quaternion as the subtraction result of the passed one from the current Quaternion.
  2235. */
  2236. public subtract(other: Quaternion): Quaternion {
  2237. return new Quaternion(this.x - other.x, this.y - other.y, this.z - other.z, this.w - other.w);
  2238. }
  2239. /**
  2240. * Returns a new Quaternion set by multiplying the current Quaterion coordinates by the float "scale".
  2241. */
  2242. public scale(value: number): Quaternion {
  2243. return new Quaternion(this.x * value, this.y * value, this.z * value, this.w * value);
  2244. }
  2245. /**
  2246. * Returns a new Quaternion set as the quaternion mulplication result of the current one with the passed one "q1".
  2247. */
  2248. public multiply(q1: Quaternion): Quaternion {
  2249. var result = new Quaternion(0, 0, 0, 1.0);
  2250. this.multiplyToRef(q1, result);
  2251. return result;
  2252. }
  2253. /**
  2254. * Sets the passed "result" as the quaternion mulplication result of the current one with the passed one "q1".
  2255. * Returns the current Quaternion.
  2256. */
  2257. public multiplyToRef(q1: Quaternion, result: Quaternion): Quaternion {
  2258. var x = this.x * q1.w + this.y * q1.z - this.z * q1.y + this.w * q1.x;
  2259. var y = -this.x * q1.z + this.y * q1.w + this.z * q1.x + this.w * q1.y;
  2260. var z = this.x * q1.y - this.y * q1.x + this.z * q1.w + this.w * q1.z;
  2261. var w = -this.x * q1.x - this.y * q1.y - this.z * q1.z + this.w * q1.w;
  2262. result.copyFromFloats(x, y, z, w);
  2263. return this;
  2264. }
  2265. /**
  2266. * Updates the current Quaternion with the quaternion mulplication result of itself with the passed one "q1".
  2267. * Returns the updated Quaternion.
  2268. */
  2269. public multiplyInPlace(q1: Quaternion): Quaternion {
  2270. this.multiplyToRef(q1, this);
  2271. return this;
  2272. }
  2273. /**
  2274. * Sets the passed "ref" with the conjugation of the current Quaternion.
  2275. * Returns the current Quaternion.
  2276. */
  2277. public conjugateToRef(ref: Quaternion): Quaternion {
  2278. ref.copyFromFloats(-this.x, -this.y, -this.z, this.w);
  2279. return this;
  2280. }
  2281. /**
  2282. * Conjugates in place the current Quaternion.
  2283. * Returns the updated Quaternion.
  2284. */
  2285. public conjugateInPlace(): Quaternion {
  2286. this.x *= -1;
  2287. this.y *= -1;
  2288. this.z *= -1;
  2289. return this;
  2290. }
  2291. /**
  2292. * Returns a new Quaternion as the conjugate of the current Quaternion.
  2293. */
  2294. public conjugate(): Quaternion {
  2295. var result = new Quaternion(-this.x, -this.y, -this.z, this.w);
  2296. return result;
  2297. }
  2298. /**
  2299. * Returns the Quaternion length (float).
  2300. */
  2301. public length(): number {
  2302. return Math.sqrt((this.x * this.x) + (this.y * this.y) + (this.z * this.z) + (this.w * this.w));
  2303. }
  2304. /**
  2305. * Normalize in place the current Quaternion.
  2306. * Returns the updated Quaternion.
  2307. */
  2308. public normalize(): Quaternion {
  2309. var length = 1.0 / this.length();
  2310. this.x *= length;
  2311. this.y *= length;
  2312. this.z *= length;
  2313. this.w *= length;
  2314. return this;
  2315. }
  2316. /**
  2317. * Returns a new Vector3 set with the Euler angles translated from the current Quaternion.
  2318. */
  2319. public toEulerAngles(order = "YZX"): Vector3 {
  2320. var result = Vector3.Zero();
  2321. this.toEulerAnglesToRef(result, order);
  2322. return result;
  2323. }
  2324. /**
  2325. * Sets the passed vector3 "result" with the Euler angles translated from the current Quaternion.
  2326. * Returns the current Quaternion.
  2327. */
  2328. public toEulerAnglesToRef(result: Vector3, order = "YZX"): Quaternion {
  2329. var qz = this.z;
  2330. var qx = this.x;
  2331. var qy = this.y;
  2332. var qw = this.w;
  2333. var sqw = qw * qw;
  2334. var sqz = qz * qz;
  2335. var sqx = qx * qx;
  2336. var sqy = qy * qy;
  2337. var zAxisY = qy * qz - qx * qw;
  2338. var limit = .4999999;
  2339. if (zAxisY < -limit) {
  2340. result.y = 2 * Math.atan2(qy, qw);
  2341. result.x = Math.PI / 2;
  2342. result.z = 0;
  2343. } else if (zAxisY > limit) {
  2344. result.y = 2 * Math.atan2(qy, qw);
  2345. result.x = -Math.PI / 2;
  2346. result.z = 0;
  2347. } else {
  2348. result.z = Math.atan2(2.0 * (qx * qy + qz * qw), (-sqz - sqx + sqy + sqw));
  2349. result.x = Math.asin(-2.0 * (qz * qy - qx * qw));
  2350. result.y = Math.atan2(2.0 * (qz * qx + qy * qw), (sqz - sqx - sqy + sqw));
  2351. }
  2352. return this;
  2353. }
  2354. /**
  2355. * Updates the passed rotation matrix with the current Quaternion values.
  2356. * Returns the current Quaternion.
  2357. */
  2358. public toRotationMatrix(result: Matrix): Quaternion {
  2359. var xx = this.x * this.x;
  2360. var yy = this.y * this.y;
  2361. var zz = this.z * this.z;
  2362. var xy = this.x * this.y;
  2363. var zw = this.z * this.w;
  2364. var zx = this.z * this.x;
  2365. var yw = this.y * this.w;
  2366. var yz = this.y * this.z;
  2367. var xw = this.x * this.w;
  2368. result.m[0] = 1.0 - (2.0 * (yy + zz));
  2369. result.m[1] = 2.0 * (xy + zw);
  2370. result.m[2] = 2.0 * (zx - yw);
  2371. result.m[3] = 0;
  2372. result.m[4] = 2.0 * (xy - zw);
  2373. result.m[5] = 1.0 - (2.0 * (zz + xx));
  2374. result.m[6] = 2.0 * (yz + xw);
  2375. result.m[7] = 0;
  2376. result.m[8] = 2.0 * (zx + yw);
  2377. result.m[9] = 2.0 * (yz - xw);
  2378. result.m[10] = 1.0 - (2.0 * (yy + xx));
  2379. result.m[11] = 0;
  2380. result.m[12] = 0;
  2381. result.m[13] = 0;
  2382. result.m[14] = 0;
  2383. result.m[15] = 1.0;
  2384. result._markAsUpdated();
  2385. return this;
  2386. }
  2387. /**
  2388. * Updates the current Quaternion from the passed rotation matrix values.
  2389. * Returns the updated Quaternion.
  2390. */
  2391. public fromRotationMatrix(matrix: Matrix): Quaternion {
  2392. Quaternion.FromRotationMatrixToRef(matrix, this);
  2393. return this;
  2394. }
  2395. // Statics
  2396. /**
  2397. * Returns a new Quaternion set from the passed rotation matrix values.
  2398. */
  2399. public static FromRotationMatrix(matrix: Matrix): Quaternion {
  2400. var result = new Quaternion();
  2401. Quaternion.FromRotationMatrixToRef(matrix, result);
  2402. return result;
  2403. }
  2404. /**
  2405. * Updates the passed quaternion "result" with the passed rotation matrix values.
  2406. */
  2407. public static FromRotationMatrixToRef(matrix: Matrix, result: Quaternion): void {
  2408. var data = matrix.m;
  2409. var m11 = data[0], m12 = data[4], m13 = data[8];
  2410. var m21 = data[1], m22 = data[5], m23 = data[9];
  2411. var m31 = data[2], m32 = data[6], m33 = data[10];
  2412. var trace = m11 + m22 + m33;
  2413. var s;
  2414. if (trace > 0) {
  2415. s = 0.5 / Math.sqrt(trace + 1.0);
  2416. result.w = 0.25 / s;
  2417. result.x = (m32 - m23) * s;
  2418. result.y = (m13 - m31) * s;
  2419. result.z = (m21 - m12) * s;
  2420. } else if (m11 > m22 && m11 > m33) {
  2421. s = 2.0 * Math.sqrt(1.0 + m11 - m22 - m33);
  2422. result.w = (m32 - m23) / s;
  2423. result.x = 0.25 * s;
  2424. result.y = (m12 + m21) / s;
  2425. result.z = (m13 + m31) / s;
  2426. } else if (m22 > m33) {
  2427. s = 2.0 * Math.sqrt(1.0 + m22 - m11 - m33);
  2428. result.w = (m13 - m31) / s;
  2429. result.x = (m12 + m21) / s;
  2430. result.y = 0.25 * s;
  2431. result.z = (m23 + m32) / s;
  2432. } else {
  2433. s = 2.0 * Math.sqrt(1.0 + m33 - m11 - m22);
  2434. result.w = (m21 - m12) / s;
  2435. result.x = (m13 + m31) / s;
  2436. result.y = (m23 + m32) / s;
  2437. result.z = 0.25 * s;
  2438. }
  2439. }
  2440. /**
  2441. * Returns a new Quaternion set to (0.0, 0.0, 0.0).
  2442. */
  2443. public static Zero(): Quaternion {
  2444. return new Quaternion(0.0, 0.0, 0.0, 0.0);
  2445. }
  2446. /**
  2447. * Returns a new Quaternion as the inverted current Quaternion.
  2448. */
  2449. public static Inverse(q: Quaternion): Quaternion {
  2450. return new Quaternion(-q.x, -q.y, -q.z, q.w);
  2451. }
  2452. /**
  2453. * Returns the identity Quaternion.
  2454. */
  2455. public static Identity(): Quaternion {
  2456. return new Quaternion(0.0, 0.0, 0.0, 1.0);
  2457. }
  2458. public static IsIdentity(quaternion: Quaternion) {
  2459. return quaternion && quaternion.x === 0 && quaternion.y === 0 && quaternion.z === 0 && quaternion.w === 1;
  2460. }
  2461. /**
  2462. * Returns a new Quaternion set from the passed axis (Vector3) and angle in radians (float).
  2463. */
  2464. public static RotationAxis(axis: Vector3, angle: number): Quaternion {
  2465. return Quaternion.RotationAxisToRef(axis, angle, new Quaternion());
  2466. }
  2467. /**
  2468. * Sets the passed quaternion "result" from the passed axis (Vector3) and angle in radians (float).
  2469. */
  2470. public static RotationAxisToRef(axis: Vector3, angle: number, result: Quaternion): Quaternion {
  2471. var sin = Math.sin(angle / 2);
  2472. axis.normalize();
  2473. result.w = Math.cos(angle / 2);
  2474. result.x = axis.x * sin;
  2475. result.y = axis.y * sin;
  2476. result.z = axis.z * sin;
  2477. return result;
  2478. }
  2479. /**
  2480. * Retuns a new Quaternion set from the starting index of the passed array.
  2481. */
  2482. public static FromArray(array: number[], offset?: number): Quaternion {
  2483. if (!offset) {
  2484. offset = 0;
  2485. }
  2486. return new Quaternion(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  2487. }
  2488. /**
  2489. * Returns a new Quaternion set from the passed Euler float angles (y, x, z).
  2490. */
  2491. public static RotationYawPitchRoll(yaw: number, pitch: number, roll: number): Quaternion {
  2492. var q = new Quaternion();
  2493. Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, q);
  2494. return q;
  2495. }
  2496. /**
  2497. * Sets the passed quaternion "result" from the passed float Euler angles (y, x, z).
  2498. */
  2499. public static RotationYawPitchRollToRef(yaw: number, pitch: number, roll: number, result: Quaternion): void {
  2500. // Produces a quaternion from Euler angles in the z-y-x orientation (Tait-Bryan angles)
  2501. var halfRoll = roll * 0.5;
  2502. var halfPitch = pitch * 0.5;
  2503. var halfYaw = yaw * 0.5;
  2504. var sinRoll = Math.sin(halfRoll);
  2505. var cosRoll = Math.cos(halfRoll);
  2506. var sinPitch = Math.sin(halfPitch);
  2507. var cosPitch = Math.cos(halfPitch);
  2508. var sinYaw = Math.sin(halfYaw);
  2509. var cosYaw = Math.cos(halfYaw);
  2510. result.x = (cosYaw * sinPitch * cosRoll) + (sinYaw * cosPitch * sinRoll);
  2511. result.y = (sinYaw * cosPitch * cosRoll) - (cosYaw * sinPitch * sinRoll);
  2512. result.z = (cosYaw * cosPitch * sinRoll) - (sinYaw * sinPitch * cosRoll);
  2513. result.w = (cosYaw * cosPitch * cosRoll) + (sinYaw * sinPitch * sinRoll);
  2514. }
  2515. /**
  2516. * Returns a new Quaternion from the passed float Euler angles expressed in z-x-z orientation
  2517. */
  2518. public static RotationAlphaBetaGamma(alpha: number, beta: number, gamma: number): Quaternion {
  2519. var result = new Quaternion();
  2520. Quaternion.RotationAlphaBetaGammaToRef(alpha, beta, gamma, result);
  2521. return result;
  2522. }
  2523. /**
  2524. * Sets the passed quaternion "result" from the passed float Euler angles expressed in z-x-z orientation
  2525. */
  2526. public static RotationAlphaBetaGammaToRef(alpha: number, beta: number, gamma: number, result: Quaternion): void {
  2527. // Produces a quaternion from Euler angles in the z-x-z orientation
  2528. var halfGammaPlusAlpha = (gamma + alpha) * 0.5;
  2529. var halfGammaMinusAlpha = (gamma - alpha) * 0.5;
  2530. var halfBeta = beta * 0.5;
  2531. result.x = Math.cos(halfGammaMinusAlpha) * Math.sin(halfBeta);
  2532. result.y = Math.sin(halfGammaMinusAlpha) * Math.sin(halfBeta);
  2533. result.z = Math.sin(halfGammaPlusAlpha) * Math.cos(halfBeta);
  2534. result.w = Math.cos(halfGammaPlusAlpha) * Math.cos(halfBeta);
  2535. }
  2536. /**
  2537. * Returns a new Quaternion as the quaternion rotation value to reach the target (axis1, axis2, axis3) orientation as a rotated XYZ system.
  2538. * cf to Vector3.RotationFromAxis() documentation.
  2539. * Note : axis1, axis2 and axis3 are normalized during this operation.
  2540. */
  2541. public static RotationQuaternionFromAxis(axis1: Vector3, axis2: Vector3, axis3: Vector3, ref: Quaternion): Quaternion {
  2542. var quat = new Quaternion(0.0, 0.0, 0.0, 0.0);
  2543. Quaternion.RotationQuaternionFromAxisToRef(axis1, axis2, axis3, quat);
  2544. return quat;
  2545. }
  2546. /**
  2547. * Sets the passed quaternion "ref" with the quaternion rotation value to reach the target (axis1, axis2, axis3) orientation as a rotated XYZ system.
  2548. * cf to Vector3.RotationFromAxis() documentation.
  2549. * Note : axis1, axis2 and axis3 are normalized during this operation.
  2550. */
  2551. public static RotationQuaternionFromAxisToRef(axis1: Vector3, axis2: Vector3, axis3: Vector3, ref: Quaternion): void {
  2552. var rotMat = MathTmp.Matrix[0];
  2553. BABYLON.Matrix.FromXYZAxesToRef(axis1.normalize(), axis2.normalize(), axis3.normalize(), rotMat);
  2554. BABYLON.Quaternion.FromRotationMatrixToRef(rotMat, ref);
  2555. }
  2556. public static Slerp(left: Quaternion, right: Quaternion, amount: number): Quaternion {
  2557. var result = Quaternion.Identity();
  2558. Quaternion.SlerpToRef(left, right, amount, result);
  2559. return result;
  2560. }
  2561. public static SlerpToRef(left: Quaternion, right: Quaternion, amount: number, result: Quaternion): void {
  2562. var num2;
  2563. var num3;
  2564. var num = amount;
  2565. var num4 = (((left.x * right.x) + (left.y * right.y)) + (left.z * right.z)) + (left.w * right.w);
  2566. var flag = false;
  2567. if (num4 < 0) {
  2568. flag = true;
  2569. num4 = -num4;
  2570. }
  2571. if (num4 > 0.999999) {
  2572. num3 = 1 - num;
  2573. num2 = flag ? -num : num;
  2574. }
  2575. else {
  2576. var num5 = Math.acos(num4);
  2577. var num6 = (1.0 / Math.sin(num5));
  2578. num3 = (Math.sin((1.0 - num) * num5)) * num6;
  2579. num2 = flag ? ((-Math.sin(num * num5)) * num6) : ((Math.sin(num * num5)) * num6);
  2580. }
  2581. result.x = (num3 * left.x) + (num2 * right.x);
  2582. result.y = (num3 * left.y) + (num2 * right.y);
  2583. result.z = (num3 * left.z) + (num2 * right.z);
  2584. result.w = (num3 * left.w) + (num2 * right.w);
  2585. }
  2586. }
  2587. export class Matrix {
  2588. private static _tempQuaternion: Quaternion = new Quaternion();
  2589. private static _xAxis: Vector3 = Vector3.Zero();
  2590. private static _yAxis: Vector3 = Vector3.Zero();
  2591. private static _zAxis: Vector3 = Vector3.Zero();
  2592. private static _updateFlagSeed = 0;
  2593. public updateFlag: number;
  2594. public m: Float32Array = new Float32Array(16);
  2595. public _markAsUpdated() {
  2596. this.updateFlag = Matrix._updateFlagSeed++;
  2597. }
  2598. public constructor() {
  2599. this._markAsUpdated();
  2600. }
  2601. // Properties
  2602. /**
  2603. * Boolean : True is the matrix is the identity matrix
  2604. */
  2605. public isIdentity(): boolean {
  2606. if (this.m[0] !== 1.0 || this.m[5] !== 1.0 || this.m[10] !== 1.0 || this.m[15] !== 1.0)
  2607. return false;
  2608. if (this.m[1] !== 0.0 || this.m[2] !== 0.0 || this.m[3] !== 0.0 ||
  2609. this.m[4] !== 0.0 || this.m[6] !== 0.0 || this.m[7] !== 0.0 ||
  2610. this.m[8] !== 0.0 || this.m[9] !== 0.0 || this.m[11] !== 0.0 ||
  2611. this.m[12] !== 0.0 || this.m[13] !== 0.0 || this.m[14] !== 0.0)
  2612. return false;
  2613. return true;
  2614. }
  2615. /**
  2616. * Returns the matrix determinant (float).
  2617. */
  2618. public determinant(): number {
  2619. var temp1 = (this.m[10] * this.m[15]) - (this.m[11] * this.m[14]);
  2620. var temp2 = (this.m[9] * this.m[15]) - (this.m[11] * this.m[13]);
  2621. var temp3 = (this.m[9] * this.m[14]) - (this.m[10] * this.m[13]);
  2622. var temp4 = (this.m[8] * this.m[15]) - (this.m[11] * this.m[12]);
  2623. var temp5 = (this.m[8] * this.m[14]) - (this.m[10] * this.m[12]);
  2624. var temp6 = (this.m[8] * this.m[13]) - (this.m[9] * this.m[12]);
  2625. return ((((this.m[0] * (((this.m[5] * temp1) - (this.m[6] * temp2)) + (this.m[7] * temp3))) - (this.m[1] * (((this.m[4] * temp1) -
  2626. (this.m[6] * temp4)) + (this.m[7] * temp5)))) + (this.m[2] * (((this.m[4] * temp2) - (this.m[5] * temp4)) + (this.m[7] * temp6)))) -
  2627. (this.m[3] * (((this.m[4] * temp3) - (this.m[5] * temp5)) + (this.m[6] * temp6))));
  2628. }
  2629. // Methods
  2630. /**
  2631. * Returns the matrix underlying array.
  2632. */
  2633. public toArray(): Float32Array {
  2634. return this.m;
  2635. }
  2636. /**
  2637. * Returns the matrix underlying array.
  2638. */
  2639. public asArray(): Float32Array {
  2640. return this.toArray();
  2641. }
  2642. /**
  2643. * Inverts in place the Matrix.
  2644. * Returns the Matrix inverted.
  2645. */
  2646. public invert(): Matrix {
  2647. this.invertToRef(this);
  2648. return this;
  2649. }
  2650. /**
  2651. * Sets all the matrix elements to zero.
  2652. * Returns the Matrix.
  2653. */
  2654. public reset(): Matrix {
  2655. for (var index = 0; index < 16; index++) {
  2656. this.m[index] = 0.0;
  2657. }
  2658. this._markAsUpdated();
  2659. return this;
  2660. }
  2661. /**
  2662. * Returns a new Matrix as the addition result of the current Matrix and the passed one.
  2663. */
  2664. public add(other: Matrix): Matrix {
  2665. var result = new Matrix();
  2666. this.addToRef(other, result);
  2667. return result;
  2668. }
  2669. /**
  2670. * Sets the passed matrix "result" with the ddition result of the current Matrix and the passed one.
  2671. * Returns the Matrix.
  2672. */
  2673. public addToRef(other: Matrix, result: Matrix): Matrix {
  2674. for (var index = 0; index < 16; index++) {
  2675. result.m[index] = this.m[index] + other.m[index];
  2676. }
  2677. result._markAsUpdated();
  2678. return this;
  2679. }
  2680. /**
  2681. * Adds in place the passed matrix to the current Matrix.
  2682. * Returns the updated Matrix.
  2683. */
  2684. public addToSelf(other: Matrix): Matrix {
  2685. for (var index = 0; index < 16; index++) {
  2686. this.m[index] += other.m[index];
  2687. }
  2688. this._markAsUpdated();
  2689. return this;
  2690. }
  2691. /**
  2692. * Sets the passed matrix with the current inverted Matrix.
  2693. * Returns the unmodified current Matrix.
  2694. */
  2695. public invertToRef(other: Matrix): Matrix {
  2696. var l1 = this.m[0];
  2697. var l2 = this.m[1];
  2698. var l3 = this.m[2];
  2699. var l4 = this.m[3];
  2700. var l5 = this.m[4];
  2701. var l6 = this.m[5];
  2702. var l7 = this.m[6];
  2703. var l8 = this.m[7];
  2704. var l9 = this.m[8];
  2705. var l10 = this.m[9];
  2706. var l11 = this.m[10];
  2707. var l12 = this.m[11];
  2708. var l13 = this.m[12];
  2709. var l14 = this.m[13];
  2710. var l15 = this.m[14];
  2711. var l16 = this.m[15];
  2712. var l17 = (l11 * l16) - (l12 * l15);
  2713. var l18 = (l10 * l16) - (l12 * l14);
  2714. var l19 = (l10 * l15) - (l11 * l14);
  2715. var l20 = (l9 * l16) - (l12 * l13);
  2716. var l21 = (l9 * l15) - (l11 * l13);
  2717. var l22 = (l9 * l14) - (l10 * l13);
  2718. var l23 = ((l6 * l17) - (l7 * l18)) + (l8 * l19);
  2719. var l24 = -(((l5 * l17) - (l7 * l20)) + (l8 * l21));
  2720. var l25 = ((l5 * l18) - (l6 * l20)) + (l8 * l22);
  2721. var l26 = -(((l5 * l19) - (l6 * l21)) + (l7 * l22));
  2722. var l27 = 1.0 / ((((l1 * l23) + (l2 * l24)) + (l3 * l25)) + (l4 * l26));
  2723. var l28 = (l7 * l16) - (l8 * l15);
  2724. var l29 = (l6 * l16) - (l8 * l14);
  2725. var l30 = (l6 * l15) - (l7 * l14);
  2726. var l31 = (l5 * l16) - (l8 * l13);
  2727. var l32 = (l5 * l15) - (l7 * l13);
  2728. var l33 = (l5 * l14) - (l6 * l13);
  2729. var l34 = (l7 * l12) - (l8 * l11);
  2730. var l35 = (l6 * l12) - (l8 * l10);
  2731. var l36 = (l6 * l11) - (l7 * l10);
  2732. var l37 = (l5 * l12) - (l8 * l9);
  2733. var l38 = (l5 * l11) - (l7 * l9);
  2734. var l39 = (l5 * l10) - (l6 * l9);
  2735. other.m[0] = l23 * l27;
  2736. other.m[4] = l24 * l27;
  2737. other.m[8] = l25 * l27;
  2738. other.m[12] = l26 * l27;
  2739. other.m[1] = -(((l2 * l17) - (l3 * l18)) + (l4 * l19)) * l27;
  2740. other.m[5] = (((l1 * l17) - (l3 * l20)) + (l4 * l21)) * l27;
  2741. other.m[9] = -(((l1 * l18) - (l2 * l20)) + (l4 * l22)) * l27;
  2742. other.m[13] = (((l1 * l19) - (l2 * l21)) + (l3 * l22)) * l27;
  2743. other.m[2] = (((l2 * l28) - (l3 * l29)) + (l4 * l30)) * l27;
  2744. other.m[6] = -(((l1 * l28) - (l3 * l31)) + (l4 * l32)) * l27;
  2745. other.m[10] = (((l1 * l29) - (l2 * l31)) + (l4 * l33)) * l27;
  2746. other.m[14] = -(((l1 * l30) - (l2 * l32)) + (l3 * l33)) * l27;
  2747. other.m[3] = -(((l2 * l34) - (l3 * l35)) + (l4 * l36)) * l27;
  2748. other.m[7] = (((l1 * l34) - (l3 * l37)) + (l4 * l38)) * l27;
  2749. other.m[11] = -(((l1 * l35) - (l2 * l37)) + (l4 * l39)) * l27;
  2750. other.m[15] = (((l1 * l36) - (l2 * l38)) + (l3 * l39)) * l27;
  2751. other._markAsUpdated();
  2752. return this;
  2753. }
  2754. /**
  2755. * Inserts the translation vector (using 3 x floats) in the current Matrix.
  2756. * Returns the updated Matrix.
  2757. */
  2758. public setTranslationFromFloats(x: number, y: number, z: number): Matrix {
  2759. this.m[12] = x;
  2760. this.m[13] = y;
  2761. this.m[14] = z;
  2762. this._markAsUpdated();
  2763. return this;
  2764. }
  2765. /**
  2766. * Inserts the translation vector in the current Matrix.
  2767. * Returns the updated Matrix.
  2768. */
  2769. public setTranslation(vector3: Vector3): Matrix {
  2770. this.m[12] = vector3.x;
  2771. this.m[13] = vector3.y;
  2772. this.m[14] = vector3.z;
  2773. this._markAsUpdated();
  2774. return this;
  2775. }
  2776. /**
  2777. * Returns a new Vector3 as the extracted translation from the Matrix.
  2778. */
  2779. public getTranslation(): Vector3 {
  2780. return new Vector3(this.m[12], this.m[13], this.m[14]);
  2781. }
  2782. /**
  2783. * Fill a Vector3 with the extracted translation from the Matrix.
  2784. */
  2785. public getTranslationToRef(result: Vector3): Matrix {
  2786. result.x = this.m[12];
  2787. result.y = this.m[13];
  2788. result.z = this.m[14];
  2789. return this;
  2790. }
  2791. /**
  2792. * Remove rotation and scaling part from the Matrix.
  2793. * Returns the updated Matrix.
  2794. */
  2795. public removeRotationAndScaling(): Matrix {
  2796. this.setRowFromFloats(0, 1, 0, 0, 0);
  2797. this.setRowFromFloats(1, 0, 1, 0, 0);
  2798. this.setRowFromFloats(2, 0, 0, 1, 0);
  2799. return this;
  2800. }
  2801. /**
  2802. * Returns a new Matrix set with the multiplication result of the current Matrix and the passed one.
  2803. */
  2804. public multiply(other: Matrix): Matrix {
  2805. var result = new Matrix();
  2806. this.multiplyToRef(other, result);
  2807. return result;
  2808. }
  2809. /**
  2810. * Updates the current Matrix from the passed one values.
  2811. * Returns the updated Matrix.
  2812. */
  2813. public copyFrom(other: Matrix): Matrix {
  2814. for (var index = 0; index < 16; index++) {
  2815. this.m[index] = other.m[index];
  2816. }
  2817. this._markAsUpdated();
  2818. return this;
  2819. }
  2820. /**
  2821. * Populates the passed array from the starting index with the Matrix values.
  2822. * Returns the Matrix.
  2823. */
  2824. public copyToArray(array: Float32Array, offset: number = 0): Matrix {
  2825. for (var index = 0; index < 16; index++) {
  2826. array[offset + index] = this.m[index];
  2827. }
  2828. return this;
  2829. }
  2830. /**
  2831. * Sets the passed matrix "result" with the multiplication result of the current Matrix and the passed one.
  2832. */
  2833. public multiplyToRef(other: Matrix, result: Matrix): Matrix {
  2834. this.multiplyToArray(other, result.m, 0);
  2835. result._markAsUpdated();
  2836. return this;
  2837. }
  2838. /**
  2839. * Sets the Float32Array "result" from the passed index "offset" with the multiplication result of the current Matrix and the passed one.
  2840. */
  2841. public multiplyToArray(other: Matrix, result: Float32Array, offset: number): Matrix {
  2842. var tm0 = this.m[0];
  2843. var tm1 = this.m[1];
  2844. var tm2 = this.m[2];
  2845. var tm3 = this.m[3];
  2846. var tm4 = this.m[4];
  2847. var tm5 = this.m[5];
  2848. var tm6 = this.m[6];
  2849. var tm7 = this.m[7];
  2850. var tm8 = this.m[8];
  2851. var tm9 = this.m[9];
  2852. var tm10 = this.m[10];
  2853. var tm11 = this.m[11];
  2854. var tm12 = this.m[12];
  2855. var tm13 = this.m[13];
  2856. var tm14 = this.m[14];
  2857. var tm15 = this.m[15];
  2858. var om0 = other.m[0];
  2859. var om1 = other.m[1];
  2860. var om2 = other.m[2];
  2861. var om3 = other.m[3];
  2862. var om4 = other.m[4];
  2863. var om5 = other.m[5];
  2864. var om6 = other.m[6];
  2865. var om7 = other.m[7];
  2866. var om8 = other.m[8];
  2867. var om9 = other.m[9];
  2868. var om10 = other.m[10];
  2869. var om11 = other.m[11];
  2870. var om12 = other.m[12];
  2871. var om13 = other.m[13];
  2872. var om14 = other.m[14];
  2873. var om15 = other.m[15];
  2874. result[offset] = tm0 * om0 + tm1 * om4 + tm2 * om8 + tm3 * om12;
  2875. result[offset + 1] = tm0 * om1 + tm1 * om5 + tm2 * om9 + tm3 * om13;
  2876. result[offset + 2] = tm0 * om2 + tm1 * om6 + tm2 * om10 + tm3 * om14;
  2877. result[offset + 3] = tm0 * om3 + tm1 * om7 + tm2 * om11 + tm3 * om15;
  2878. result[offset + 4] = tm4 * om0 + tm5 * om4 + tm6 * om8 + tm7 * om12;
  2879. result[offset + 5] = tm4 * om1 + tm5 * om5 + tm6 * om9 + tm7 * om13;
  2880. result[offset + 6] = tm4 * om2 + tm5 * om6 + tm6 * om10 + tm7 * om14;
  2881. result[offset + 7] = tm4 * om3 + tm5 * om7 + tm6 * om11 + tm7 * om15;
  2882. result[offset + 8] = tm8 * om0 + tm9 * om4 + tm10 * om8 + tm11 * om12;
  2883. result[offset + 9] = tm8 * om1 + tm9 * om5 + tm10 * om9 + tm11 * om13;
  2884. result[offset + 10] = tm8 * om2 + tm9 * om6 + tm10 * om10 + tm11 * om14;
  2885. result[offset + 11] = tm8 * om3 + tm9 * om7 + tm10 * om11 + tm11 * om15;
  2886. result[offset + 12] = tm12 * om0 + tm13 * om4 + tm14 * om8 + tm15 * om12;
  2887. result[offset + 13] = tm12 * om1 + tm13 * om5 + tm14 * om9 + tm15 * om13;
  2888. result[offset + 14] = tm12 * om2 + tm13 * om6 + tm14 * om10 + tm15 * om14;
  2889. result[offset + 15] = tm12 * om3 + tm13 * om7 + tm14 * om11 + tm15 * om15;
  2890. return this;
  2891. }
  2892. /**
  2893. * Boolean : True is the current Matrix and the passed one values are strictly equal.
  2894. */
  2895. public equals(value: Matrix): boolean {
  2896. return value &&
  2897. (this.m[0] === value.m[0] && this.m[1] === value.m[1] && this.m[2] === value.m[2] && this.m[3] === value.m[3] &&
  2898. this.m[4] === value.m[4] && this.m[5] === value.m[5] && this.m[6] === value.m[6] && this.m[7] === value.m[7] &&
  2899. this.m[8] === value.m[8] && this.m[9] === value.m[9] && this.m[10] === value.m[10] && this.m[11] === value.m[11] &&
  2900. this.m[12] === value.m[12] && this.m[13] === value.m[13] && this.m[14] === value.m[14] && this.m[15] === value.m[15]);
  2901. }
  2902. /**
  2903. * Returns a new Matrix from the current Matrix.
  2904. */
  2905. public clone(): Matrix {
  2906. return Matrix.FromValues(this.m[0], this.m[1], this.m[2], this.m[3],
  2907. this.m[4], this.m[5], this.m[6], this.m[7],
  2908. this.m[8], this.m[9], this.m[10], this.m[11],
  2909. this.m[12], this.m[13], this.m[14], this.m[15]);
  2910. }
  2911. /**
  2912. * Returns the string "Matrix"
  2913. */
  2914. public getClassName(): string {
  2915. return "Matrix";
  2916. }
  2917. /**
  2918. * Returns the Matrix hash code.
  2919. */
  2920. public getHashCode(): number {
  2921. let hash = this.m[0] || 0;
  2922. for (let i = 1; i < 16; i++) {
  2923. hash = (hash * 397) ^ (this.m[i] || 0);
  2924. }
  2925. return hash;
  2926. }
  2927. /**
  2928. * Decomposes the current Matrix into :
  2929. * - a scale vector3 passed as a reference to update,
  2930. * - a rotation quaternion passed as a reference to update,
  2931. * - a translation vector3 passed as a reference to update.
  2932. * Returns the boolean `true`.
  2933. */
  2934. public decompose(scale: Vector3, rotation: Quaternion, translation: Vector3): boolean {
  2935. translation.x = this.m[12];
  2936. translation.y = this.m[13];
  2937. translation.z = this.m[14];
  2938. var xs = MathTools.Sign(this.m[0] * this.m[1] * this.m[2] * this.m[3]) < 0 ? -1 : 1;
  2939. var ys = MathTools.Sign(this.m[4] * this.m[5] * this.m[6] * this.m[7]) < 0 ? -1 : 1;
  2940. var zs = MathTools.Sign(this.m[8] * this.m[9] * this.m[10] * this.m[11]) < 0 ? -1 : 1;
  2941. scale.x = xs * Math.sqrt(this.m[0] * this.m[0] + this.m[1] * this.m[1] + this.m[2] * this.m[2]);
  2942. scale.y = ys * Math.sqrt(this.m[4] * this.m[4] + this.m[5] * this.m[5] + this.m[6] * this.m[6]);
  2943. scale.z = zs * Math.sqrt(this.m[8] * this.m[8] + this.m[9] * this.m[9] + this.m[10] * this.m[10]);
  2944. if (scale.x === 0 || scale.y === 0 || scale.z === 0) {
  2945. rotation.x = 0;
  2946. rotation.y = 0;
  2947. rotation.z = 0;
  2948. rotation.w = 1;
  2949. return false;
  2950. }
  2951. Matrix.FromValuesToRef(
  2952. this.m[0] / scale.x, this.m[1] / scale.x, this.m[2] / scale.x, 0,
  2953. this.m[4] / scale.y, this.m[5] / scale.y, this.m[6] / scale.y, 0,
  2954. this.m[8] / scale.z, this.m[9] / scale.z, this.m[10] / scale.z, 0,
  2955. 0, 0, 0, 1, MathTmp.Matrix[0]);
  2956. Quaternion.FromRotationMatrixToRef(MathTmp.Matrix[0], rotation);
  2957. return true;
  2958. }
  2959. /**
  2960. * Returns a new Matrix as the extracted rotation matrix from the current one.
  2961. */
  2962. public getRotationMatrix(): Matrix {
  2963. var result = Matrix.Identity();
  2964. this.getRotationMatrixToRef(result);
  2965. return result;
  2966. }
  2967. /**
  2968. * Extracts the rotation matrix from the current one and sets it as the passed "result".
  2969. * Returns the current Matrix.
  2970. */
  2971. public getRotationMatrixToRef(result: Matrix): Matrix {
  2972. var m = this.m;
  2973. var xs = m[0] * m[1] * m[2] * m[3] < 0 ? -1 : 1;
  2974. var ys = m[4] * m[5] * m[6] * m[7] < 0 ? -1 : 1;
  2975. var zs = m[8] * m[9] * m[10] * m[11] < 0 ? -1 : 1;
  2976. var sx = xs * Math.sqrt(m[0] * m[0] + m[1] * m[1] + m[2] * m[2]);
  2977. var sy = ys * Math.sqrt(m[4] * m[4] + m[5] * m[5] + m[6] * m[6]);
  2978. var sz = zs * Math.sqrt(m[8] * m[8] + m[9] * m[9] + m[10] * m[10]);
  2979. Matrix.FromValuesToRef(
  2980. m[0] / sx, m[1] / sx, m[2] / sx, 0,
  2981. m[4] / sy, m[5] / sy, m[6] / sy, 0,
  2982. m[8] / sz, m[9] / sz, m[10] / sz, 0,
  2983. 0, 0, 0, 1, result);
  2984. return this;
  2985. }
  2986. // Statics
  2987. /**
  2988. * Returns a new Matrix set from the starting index of the passed array.
  2989. */
  2990. public static FromArray(array: number[], offset?: number): Matrix {
  2991. var result = new Matrix();
  2992. if (!offset) {
  2993. offset = 0;
  2994. }
  2995. Matrix.FromArrayToRef(array, offset, result);
  2996. return result;
  2997. }
  2998. /**
  2999. * Sets the passed "result" matrix from the starting index of the passed array.
  3000. */
  3001. public static FromArrayToRef(array: number[], offset: number, result: Matrix) {
  3002. for (var index = 0; index < 16; index++) {
  3003. result.m[index] = array[index + offset];
  3004. }
  3005. result._markAsUpdated();
  3006. }
  3007. /**
  3008. * Sets the passed "result" matrix from the starting index of the passed Float32Array by multiplying each element by the float "scale".
  3009. */
  3010. public static FromFloat32ArrayToRefScaled(array: Float32Array, offset: number, scale: number, result: Matrix) {
  3011. for (var index = 0; index < 16; index++) {
  3012. result.m[index] = array[index + offset] * scale;
  3013. }
  3014. result._markAsUpdated();
  3015. }
  3016. /**
  3017. * Sets the passed matrix "result" with the 16 passed floats.
  3018. */
  3019. public static FromValuesToRef(initialM11: number, initialM12: number, initialM13: number, initialM14: number,
  3020. initialM21: number, initialM22: number, initialM23: number, initialM24: number,
  3021. initialM31: number, initialM32: number, initialM33: number, initialM34: number,
  3022. initialM41: number, initialM42: number, initialM43: number, initialM44: number, result: Matrix): void {
  3023. result.m[0] = initialM11;
  3024. result.m[1] = initialM12;
  3025. result.m[2] = initialM13;
  3026. result.m[3] = initialM14;
  3027. result.m[4] = initialM21;
  3028. result.m[5] = initialM22;
  3029. result.m[6] = initialM23;
  3030. result.m[7] = initialM24;
  3031. result.m[8] = initialM31;
  3032. result.m[9] = initialM32;
  3033. result.m[10] = initialM33;
  3034. result.m[11] = initialM34;
  3035. result.m[12] = initialM41;
  3036. result.m[13] = initialM42;
  3037. result.m[14] = initialM43;
  3038. result.m[15] = initialM44;
  3039. result._markAsUpdated();
  3040. }
  3041. /**
  3042. * Returns the index-th row of the current matrix as a new Vector4.
  3043. */
  3044. public getRow(index: number): Vector4 {
  3045. if (index < 0 || index > 3) {
  3046. return null;
  3047. }
  3048. var i = index * 4;
  3049. return new Vector4(this.m[i + 0], this.m[i + 1], this.m[i + 2], this.m[i + 3]);
  3050. }
  3051. /**
  3052. * Sets the index-th row of the current matrix with the passed Vector4 values.
  3053. * Returns the updated Matrix.
  3054. */
  3055. public setRow(index: number, row: Vector4): Matrix {
  3056. if (index < 0 || index > 3) {
  3057. return this;
  3058. }
  3059. var i = index * 4;
  3060. this.m[i + 0] = row.x;
  3061. this.m[i + 1] = row.y;
  3062. this.m[i + 2] = row.z;
  3063. this.m[i + 3] = row.w;
  3064. this._markAsUpdated();
  3065. return this;
  3066. }
  3067. /**
  3068. * Sets the index-th row of the current matrix with the passed 4 x float values.
  3069. * Returns the updated Matrix.
  3070. */
  3071. public setRowFromFloats(index: number, x: number, y: number, z: number, w: number): Matrix {
  3072. if (index < 0 || index > 3) {
  3073. return this;
  3074. }
  3075. var i = index * 4;
  3076. this.m[i + 0] = x;
  3077. this.m[i + 1] = y;
  3078. this.m[i + 2] = z;
  3079. this.m[i + 3] = w;
  3080. this._markAsUpdated();
  3081. return this;
  3082. }
  3083. /**
  3084. * Returns a new Matrix set from the 16 passed floats.
  3085. */
  3086. public static FromValues(initialM11: number, initialM12: number, initialM13: number, initialM14: number,
  3087. initialM21: number, initialM22: number, initialM23: number, initialM24: number,
  3088. initialM31: number, initialM32: number, initialM33: number, initialM34: number,
  3089. initialM41: number, initialM42: number, initialM43: number, initialM44: number): Matrix {
  3090. var result = new Matrix();
  3091. result.m[0] = initialM11;
  3092. result.m[1] = initialM12;
  3093. result.m[2] = initialM13;
  3094. result.m[3] = initialM14;
  3095. result.m[4] = initialM21;
  3096. result.m[5] = initialM22;
  3097. result.m[6] = initialM23;
  3098. result.m[7] = initialM24;
  3099. result.m[8] = initialM31;
  3100. result.m[9] = initialM32;
  3101. result.m[10] = initialM33;
  3102. result.m[11] = initialM34;
  3103. result.m[12] = initialM41;
  3104. result.m[13] = initialM42;
  3105. result.m[14] = initialM43;
  3106. result.m[15] = initialM44;
  3107. return result;
  3108. }
  3109. /**
  3110. * Returns a new Matrix composed by the passed scale (vector3), rotation (quaternion) and translation (vector3).
  3111. */
  3112. public static Compose(scale: Vector3, rotation: Quaternion, translation: Vector3): Matrix {
  3113. var result = Matrix.Identity();
  3114. Matrix.ComposeToRef(scale, rotation, translation, result);
  3115. return result;
  3116. }
  3117. /**
  3118. * Update a Matrix with values composed by the passed scale (vector3), rotation (quaternion) and translation (vector3).
  3119. */
  3120. public static ComposeToRef(scale: Vector3, rotation: Quaternion, translation: Vector3, result: Matrix): void {
  3121. Matrix.FromValuesToRef(scale.x, 0, 0, 0,
  3122. 0, scale.y, 0, 0,
  3123. 0, 0, scale.z, 0,
  3124. 0, 0, 0, 1, MathTmp.Matrix[1]);
  3125. rotation.toRotationMatrix(MathTmp.Matrix[0]);
  3126. MathTmp.Matrix[1].multiplyToRef(MathTmp.Matrix[0], result);
  3127. result.setTranslation(translation);
  3128. }
  3129. /**
  3130. * Returns a new indentity Matrix.
  3131. */
  3132. public static Identity(): Matrix {
  3133. return Matrix.FromValues(1.0, 0.0, 0.0, 0.0,
  3134. 0.0, 1.0, 0.0, 0.0,
  3135. 0.0, 0.0, 1.0, 0.0,
  3136. 0.0, 0.0, 0.0, 1.0);
  3137. }
  3138. /**
  3139. * Sets the passed "result" as an identity matrix.
  3140. */
  3141. public static IdentityToRef(result: Matrix): void {
  3142. Matrix.FromValuesToRef(1.0, 0.0, 0.0, 0.0,
  3143. 0.0, 1.0, 0.0, 0.0,
  3144. 0.0, 0.0, 1.0, 0.0,
  3145. 0.0, 0.0, 0.0, 1.0, result);
  3146. }
  3147. /**
  3148. * Returns a new zero Matrix.
  3149. */
  3150. public static Zero(): Matrix {
  3151. return Matrix.FromValues(0.0, 0.0, 0.0, 0.0,
  3152. 0.0, 0.0, 0.0, 0.0,
  3153. 0.0, 0.0, 0.0, 0.0,
  3154. 0.0, 0.0, 0.0, 0.0);
  3155. }
  3156. /**
  3157. * Returns a new rotation matrix for "angle" radians around the X axis.
  3158. */
  3159. public static RotationX(angle: number): Matrix {
  3160. var result = new Matrix();
  3161. Matrix.RotationXToRef(angle, result);
  3162. return result;
  3163. }
  3164. /**
  3165. * Returns a new Matrix as the passed inverted one.
  3166. */
  3167. public static Invert(source: Matrix): Matrix {
  3168. var result = new Matrix();
  3169. source.invertToRef(result);
  3170. return result;
  3171. }
  3172. /**
  3173. * Sets the passed matrix "result" as a rotation matrix for "angle" radians around the X axis.
  3174. */
  3175. public static RotationXToRef(angle: number, result: Matrix): void {
  3176. var s = Math.sin(angle);
  3177. var c = Math.cos(angle);
  3178. result.m[0] = 1.0;
  3179. result.m[15] = 1.0;
  3180. result.m[5] = c;
  3181. result.m[10] = c;
  3182. result.m[9] = -s;
  3183. result.m[6] = s;
  3184. result.m[1] = 0.0;
  3185. result.m[2] = 0.0;
  3186. result.m[3] = 0.0;
  3187. result.m[4] = 0.0;
  3188. result.m[7] = 0.0;
  3189. result.m[8] = 0.0;
  3190. result.m[11] = 0.0;
  3191. result.m[12] = 0.0;
  3192. result.m[13] = 0.0;
  3193. result.m[14] = 0.0;
  3194. result._markAsUpdated();
  3195. }
  3196. /**
  3197. * Returns a new rotation matrix for "angle" radians around the Y axis.
  3198. */
  3199. public static RotationY(angle: number): Matrix {
  3200. var result = new Matrix();
  3201. Matrix.RotationYToRef(angle, result);
  3202. return result;
  3203. }
  3204. /**
  3205. * Sets the passed matrix "result" as a rotation matrix for "angle" radians around the Y axis.
  3206. */
  3207. public static RotationYToRef(angle: number, result: Matrix): void {
  3208. var s = Math.sin(angle);
  3209. var c = Math.cos(angle);
  3210. result.m[5] = 1.0;
  3211. result.m[15] = 1.0;
  3212. result.m[0] = c;
  3213. result.m[2] = -s;
  3214. result.m[8] = s;
  3215. result.m[10] = c;
  3216. result.m[1] = 0.0;
  3217. result.m[3] = 0.0;
  3218. result.m[4] = 0.0;
  3219. result.m[6] = 0.0;
  3220. result.m[7] = 0.0;
  3221. result.m[9] = 0.0;
  3222. result.m[11] = 0.0;
  3223. result.m[12] = 0.0;
  3224. result.m[13] = 0.0;
  3225. result.m[14] = 0.0;
  3226. result._markAsUpdated();
  3227. }
  3228. /**
  3229. * Returns a new rotation matrix for "angle" radians around the Z axis.
  3230. */
  3231. public static RotationZ(angle: number): Matrix {
  3232. var result = new Matrix();
  3233. Matrix.RotationZToRef(angle, result);
  3234. return result;
  3235. }
  3236. /**
  3237. * Sets the passed matrix "result" as a rotation matrix for "angle" radians around the Z axis.
  3238. */
  3239. public static RotationZToRef(angle: number, result: Matrix): void {
  3240. var s = Math.sin(angle);
  3241. var c = Math.cos(angle);
  3242. result.m[10] = 1.0;
  3243. result.m[15] = 1.0;
  3244. result.m[0] = c;
  3245. result.m[1] = s;
  3246. result.m[4] = -s;
  3247. result.m[5] = c;
  3248. result.m[2] = 0.0;
  3249. result.m[3] = 0.0;
  3250. result.m[6] = 0.0;
  3251. result.m[7] = 0.0;
  3252. result.m[8] = 0.0;
  3253. result.m[9] = 0.0;
  3254. result.m[11] = 0.0;
  3255. result.m[12] = 0.0;
  3256. result.m[13] = 0.0;
  3257. result.m[14] = 0.0;
  3258. result._markAsUpdated();
  3259. }
  3260. /**
  3261. * Returns a new rotation matrix for "angle" radians around the passed axis.
  3262. */
  3263. public static RotationAxis(axis: Vector3, angle: number): Matrix {
  3264. var result = Matrix.Zero();
  3265. Matrix.RotationAxisToRef(axis, angle, result);
  3266. return result;
  3267. }
  3268. /**
  3269. * Sets the passed matrix "result" as a rotation matrix for "angle" radians around the passed axis.
  3270. */
  3271. public static RotationAxisToRef(axis: Vector3, angle: number, result: Matrix): void {
  3272. var s = Math.sin(-angle);
  3273. var c = Math.cos(-angle);
  3274. var c1 = 1 - c;
  3275. axis.normalize();
  3276. result.m[0] = (axis.x * axis.x) * c1 + c;
  3277. result.m[1] = (axis.x * axis.y) * c1 - (axis.z * s);
  3278. result.m[2] = (axis.x * axis.z) * c1 + (axis.y * s);
  3279. result.m[3] = 0.0;
  3280. result.m[4] = (axis.y * axis.x) * c1 + (axis.z * s);
  3281. result.m[5] = (axis.y * axis.y) * c1 + c;
  3282. result.m[6] = (axis.y * axis.z) * c1 - (axis.x * s);
  3283. result.m[7] = 0.0;
  3284. result.m[8] = (axis.z * axis.x) * c1 - (axis.y * s);
  3285. result.m[9] = (axis.z * axis.y) * c1 + (axis.x * s);
  3286. result.m[10] = (axis.z * axis.z) * c1 + c;
  3287. result.m[11] = 0.0;
  3288. result.m[15] = 1.0;
  3289. result._markAsUpdated();
  3290. }
  3291. /**
  3292. * Returns a new Matrix as a rotation matrix from the Euler angles (y, x, z).
  3293. */
  3294. public static RotationYawPitchRoll(yaw: number, pitch: number, roll: number): Matrix {
  3295. var result = new Matrix();
  3296. Matrix.RotationYawPitchRollToRef(yaw, pitch, roll, result);
  3297. return result;
  3298. }
  3299. /**
  3300. * Sets the passed matrix "result" as a rotation matrix from the Euler angles (y, x, z).
  3301. */
  3302. public static RotationYawPitchRollToRef(yaw: number, pitch: number, roll: number, result: Matrix): void {
  3303. Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, this._tempQuaternion);
  3304. this._tempQuaternion.toRotationMatrix(result);
  3305. }
  3306. /**
  3307. * Returns a new Matrix as a scaling matrix from the passed floats (x, y, z).
  3308. */
  3309. public static Scaling(x: number, y: number, z: number): Matrix {
  3310. var result = Matrix.Zero();
  3311. Matrix.ScalingToRef(x, y, z, result);
  3312. return result;
  3313. }
  3314. /**
  3315. * Sets the passed matrix "result" as a scaling matrix from the passed floats (x, y, z).
  3316. */
  3317. public static ScalingToRef(x: number, y: number, z: number, result: Matrix): void {
  3318. result.m[0] = x;
  3319. result.m[1] = 0.0;
  3320. result.m[2] = 0.0;
  3321. result.m[3] = 0.0;
  3322. result.m[4] = 0.0;
  3323. result.m[5] = y;
  3324. result.m[6] = 0.0;
  3325. result.m[7] = 0.0;
  3326. result.m[8] = 0.0;
  3327. result.m[9] = 0.0;
  3328. result.m[10] = z;
  3329. result.m[11] = 0.0;
  3330. result.m[12] = 0.0;
  3331. result.m[13] = 0.0;
  3332. result.m[14] = 0.0;
  3333. result.m[15] = 1.0;
  3334. result._markAsUpdated();
  3335. }
  3336. /**
  3337. * Returns a new Matrix as a translation matrix from the passed floats (x, y, z).
  3338. */
  3339. public static Translation(x: number, y: number, z: number): Matrix {
  3340. var result = Matrix.Identity();
  3341. Matrix.TranslationToRef(x, y, z, result);
  3342. return result;
  3343. }
  3344. /**
  3345. * Sets the passed matrix "result" as a translation matrix from the passed floats (x, y, z).
  3346. */
  3347. public static TranslationToRef(x: number, y: number, z: number, result: Matrix): void {
  3348. Matrix.FromValuesToRef(1.0, 0.0, 0.0, 0.0,
  3349. 0.0, 1.0, 0.0, 0.0,
  3350. 0.0, 0.0, 1.0, 0.0,
  3351. x, y, z, 1.0, result);
  3352. }
  3353. /**
  3354. * Returns a new Matrix whose values are the interpolated values for "gradien" (float) between the ones of the matrices "startValue" and "endValue".
  3355. */
  3356. public static Lerp(startValue: Matrix, endValue: Matrix, gradient: number): Matrix {
  3357. var result = Matrix.Zero();
  3358. for (var index = 0; index < 16; index++) {
  3359. result.m[index] = startValue.m[index] * (1.0 - gradient) + endValue.m[index] * gradient;
  3360. }
  3361. result._markAsUpdated();
  3362. return result;
  3363. }
  3364. /**
  3365. * Returns a new Matrix whose values are computed by :
  3366. * - decomposing the the "startValue" and "endValue" matrices into their respective scale, rotation and translation matrices,
  3367. * - interpolating for "gradient" (float) the values between each of these decomposed matrices between the start and the end,
  3368. * - recomposing a new matrix from these 3 interpolated scale, rotation and translation matrices.
  3369. */
  3370. public static DecomposeLerp(startValue: Matrix, endValue: Matrix, gradient: number): Matrix {
  3371. var startScale = new Vector3(0, 0, 0);
  3372. var startRotation = new Quaternion();
  3373. var startTranslation = new Vector3(0, 0, 0);
  3374. startValue.decompose(startScale, startRotation, startTranslation);
  3375. var endScale = new Vector3(0, 0, 0);
  3376. var endRotation = new Quaternion();
  3377. var endTranslation = new Vector3(0, 0, 0);
  3378. endValue.decompose(endScale, endRotation, endTranslation);
  3379. var resultScale = Vector3.Lerp(startScale, endScale, gradient);
  3380. var resultRotation = Quaternion.Slerp(startRotation, endRotation, gradient);
  3381. var resultTranslation = Vector3.Lerp(startTranslation, endTranslation, gradient);
  3382. return Matrix.Compose(resultScale, resultRotation, resultTranslation);
  3383. }
  3384. /**
  3385. * Returns a new rotation Matrix used to rotate a mesh so as it looks at the target Vector3, from the eye Vector3, the UP vector3 being orientated like "up".
  3386. * This methods works for a Left-Handed system.
  3387. */
  3388. public static LookAtLH(eye: Vector3, target: Vector3, up: Vector3): Matrix {
  3389. var result = Matrix.Zero();
  3390. Matrix.LookAtLHToRef(eye, target, up, result);
  3391. return result;
  3392. }
  3393. /**
  3394. * Sets the passed "result" Matrix as a rotation matrix used to rotate a mesh so as it looks at the target Vector3, from the eye Vector3, the UP vector3 being orientated like "up".
  3395. * This methods works for a Left-Handed system.
  3396. */
  3397. public static LookAtLHToRef(eye: Vector3, target: Vector3, up: Vector3, result: Matrix): void {
  3398. // Z axis
  3399. target.subtractToRef(eye, this._zAxis);
  3400. this._zAxis.normalize();
  3401. // X axis
  3402. Vector3.CrossToRef(up, this._zAxis, this._xAxis);
  3403. if (this._xAxis.lengthSquared() === 0) {
  3404. this._xAxis.x = 1.0;
  3405. } else {
  3406. this._xAxis.normalize();
  3407. }
  3408. // Y axis
  3409. Vector3.CrossToRef(this._zAxis, this._xAxis, this._yAxis);
  3410. this._yAxis.normalize();
  3411. // Eye angles
  3412. var ex = -Vector3.Dot(this._xAxis, eye);
  3413. var ey = -Vector3.Dot(this._yAxis, eye);
  3414. var ez = -Vector3.Dot(this._zAxis, eye);
  3415. return Matrix.FromValuesToRef(this._xAxis.x, this._yAxis.x, this._zAxis.x, 0,
  3416. this._xAxis.y, this._yAxis.y, this._zAxis.y, 0,
  3417. this._xAxis.z, this._yAxis.z, this._zAxis.z, 0,
  3418. ex, ey, ez, 1, result);
  3419. }
  3420. /**
  3421. * Returns a new rotation Matrix used to rotate a mesh so as it looks at the target Vector3, from the eye Vector3, the UP vector3 being orientated like "up".
  3422. * This methods works for a Right-Handed system.
  3423. */
  3424. public static LookAtRH(eye: Vector3, target: Vector3, up: Vector3): Matrix {
  3425. var result = Matrix.Zero();
  3426. Matrix.LookAtRHToRef(eye, target, up, result);
  3427. return result;
  3428. }
  3429. /**
  3430. * Sets the passed "result" Matrix as a rotation matrix used to rotate a mesh so as it looks at the target Vector3, from the eye Vector3, the UP vector3 being orientated like "up".
  3431. * This methods works for a Left-Handed system.
  3432. */
  3433. public static LookAtRHToRef(eye: Vector3, target: Vector3, up: Vector3, result: Matrix): void {
  3434. // Z axis
  3435. eye.subtractToRef(target, this._zAxis);
  3436. this._zAxis.normalize();
  3437. // X axis
  3438. Vector3.CrossToRef(up, this._zAxis, this._xAxis);
  3439. if (this._xAxis.lengthSquared() === 0) {
  3440. this._xAxis.x = 1.0;
  3441. } else {
  3442. this._xAxis.normalize();
  3443. }
  3444. // Y axis
  3445. Vector3.CrossToRef(this._zAxis, this._xAxis, this._yAxis);
  3446. this._yAxis.normalize();
  3447. // Eye angles
  3448. var ex = -Vector3.Dot(this._xAxis, eye);
  3449. var ey = -Vector3.Dot(this._yAxis, eye);
  3450. var ez = -Vector3.Dot(this._zAxis, eye);
  3451. return Matrix.FromValuesToRef(this._xAxis.x, this._yAxis.x, this._zAxis.x, 0,
  3452. this._xAxis.y, this._yAxis.y, this._zAxis.y, 0,
  3453. this._xAxis.z, this._yAxis.z, this._zAxis.z, 0,
  3454. ex, ey, ez, 1, result);
  3455. }
  3456. /**
  3457. * Returns a new Matrix as a left-handed orthographic projection matrix computed from the passed floats : width and height of the projection plane, z near and far limits.
  3458. */
  3459. public static OrthoLH(width: number, height: number, znear: number, zfar: number): Matrix {
  3460. var matrix = Matrix.Zero();
  3461. Matrix.OrthoLHToRef(width, height, znear, zfar, matrix);
  3462. return matrix;
  3463. }
  3464. /**
  3465. * Sets the passed matrix "result" as a left-handed orthographic projection matrix computed from the passed floats : width and height of the projection plane, z near and far limits.
  3466. */
  3467. public static OrthoLHToRef(width: number, height: number, znear: number, zfar: number, result: Matrix): void {
  3468. let n = znear;
  3469. let f = zfar;
  3470. let a = 2.0 / width;
  3471. let b = 2.0 / height;
  3472. let c = 2.0 / (f - n);
  3473. let d = -(f + n) / (f - n);
  3474. BABYLON.Matrix.FromValuesToRef(
  3475. a, 0.0, 0.0, 0.0,
  3476. 0.0, b, 0.0, 0.0,
  3477. 0.0, 0.0, c, 0.0,
  3478. 0.0, 0.0, d, 1.0,
  3479. result
  3480. );
  3481. }
  3482. /**
  3483. * Returns a new Matrix as a left-handed orthographic projection matrix computed from the passed floats : left, right, top and bottom being the coordinates of the projection plane, z near and far limits.
  3484. */
  3485. public static OrthoOffCenterLH(left: number, right: number, bottom: number, top: number, znear: number, zfar: number): Matrix {
  3486. var matrix = Matrix.Zero();
  3487. Matrix.OrthoOffCenterLHToRef(left, right, bottom, top, znear, zfar, matrix);
  3488. return matrix;
  3489. }
  3490. /**
  3491. * Sets the passed matrix "result" as a left-handed orthographic projection matrix computed from the passed floats : left, right, top and bottom being the coordinates of the projection plane, z near and far limits.
  3492. */
  3493. public static OrthoOffCenterLHToRef(left: number, right: number, bottom: number, top: number, znear: number, zfar: number, result: Matrix): void {
  3494. let n = znear;
  3495. let f = zfar;
  3496. let a = 2.0 / (right - left);
  3497. let b = 2.0 / (top - bottom);
  3498. let c = 2.0 / (f - n);
  3499. let d = -(f + n) / (f - n);
  3500. let i0 = (left + right) / (left - right);
  3501. let i1 = (top + bottom) / (bottom - top);
  3502. BABYLON.Matrix.FromValuesToRef(
  3503. a, 0.0, 0.0, 0.0,
  3504. 0.0, b, 0.0, 0.0,
  3505. 0.0, 0.0, c, 0.0,
  3506. i0, i1, d, 1.0,
  3507. result
  3508. );
  3509. }
  3510. /**
  3511. * Returns a new Matrix as a right-handed orthographic projection matrix computed from the passed floats : left, right, top and bottom being the coordinates of the projection plane, z near and far limits.
  3512. */
  3513. public static OrthoOffCenterRH(left: number, right: number, bottom: number, top: number, znear: number, zfar: number): Matrix {
  3514. var matrix = Matrix.Zero();
  3515. Matrix.OrthoOffCenterRHToRef(left, right, bottom, top, znear, zfar, matrix);
  3516. return matrix;
  3517. }
  3518. /**
  3519. * Sets the passed matrix "result" as a right-handed orthographic projection matrix computed from the passed floats : left, right, top and bottom being the coordinates of the projection plane, z near and far limits.
  3520. */
  3521. public static OrthoOffCenterRHToRef(left: number, right, bottom: number, top: number, znear: number, zfar: number, result: Matrix): void {
  3522. Matrix.OrthoOffCenterLHToRef(left, right, bottom, top, znear, zfar, result);
  3523. result.m[10] *= -1.0;
  3524. }
  3525. /**
  3526. * Returns a new Matrix as a left-handed perspective projection matrix computed from the passed floats : width and height of the projection plane, z near and far limits.
  3527. */
  3528. public static PerspectiveLH(width: number, height: number, znear: number, zfar: number): Matrix {
  3529. var matrix = Matrix.Zero();
  3530. let n = znear;
  3531. let f = zfar;
  3532. let a = 2.0 * n / width;
  3533. let b = 2.0 * n / height;
  3534. let c = (f + n) / (f - n);
  3535. let d = -2.0 * f * n / (f - n);
  3536. BABYLON.Matrix.FromValuesToRef(
  3537. a, 0.0, 0.0, 0.0,
  3538. 0.0, b, 0.0, 0.0,
  3539. 0.0, 0.0, c, 1.0,
  3540. 0.0, 0.0, d, 0.0,
  3541. matrix
  3542. );
  3543. return matrix;
  3544. }
  3545. /**
  3546. * Returns a new Matrix as a left-handed perspective projection matrix computed from the passed floats : vertical angle of view (fov), width/height ratio (aspect), z near and far limits.
  3547. */
  3548. public static PerspectiveFovLH(fov: number, aspect: number, znear: number, zfar: number): Matrix {
  3549. var matrix = Matrix.Zero();
  3550. Matrix.PerspectiveFovLHToRef(fov, aspect, znear, zfar, matrix);
  3551. return matrix;
  3552. }
  3553. /**
  3554. * Sets the passed matrix "result" as a left-handed perspective projection matrix computed from the passed floats : vertical angle of view (fov), width/height ratio (aspect), z near and far limits.
  3555. */
  3556. public static PerspectiveFovLHToRef(fov: number, aspect: number, znear: number, zfar: number, result: Matrix, isVerticalFovFixed = true): void {
  3557. let n = znear;
  3558. let f = zfar;
  3559. let t = 1.0 / (Math.tan(fov * 0.5));
  3560. let a = isVerticalFovFixed ? (t / aspect) : t;
  3561. let b = isVerticalFovFixed ? t : (t * aspect);
  3562. let c = (f + n) / (f - n);
  3563. let d = -2.0 * f * n / (f - n);
  3564. BABYLON.Matrix.FromValuesToRef(
  3565. a, 0.0, 0.0, 0.0,
  3566. 0.0, b, 0.0, 0.0,
  3567. 0.0, 0.0, c, 1.0,
  3568. 0.0, 0.0, d, 0.0,
  3569. result
  3570. );
  3571. }
  3572. /**
  3573. * Returns a new Matrix as a right-handed perspective projection matrix computed from the passed floats : vertical angle of view (fov), width/height ratio (aspect), z near and far limits.
  3574. */
  3575. public static PerspectiveFovRH(fov: number, aspect: number, znear: number, zfar: number): Matrix {
  3576. var matrix = Matrix.Zero();
  3577. Matrix.PerspectiveFovRHToRef(fov, aspect, znear, zfar, matrix);
  3578. return matrix;
  3579. }
  3580. /**
  3581. * Sets the passed matrix "result" as a right-handed perspective projection matrix computed from the passed floats : vertical angle of view (fov), width/height ratio (aspect), z near and far limits.
  3582. */
  3583. public static PerspectiveFovRHToRef(fov: number, aspect: number, znear: number, zfar: number, result: Matrix, isVerticalFovFixed = true): void {
  3584. //alternatively this could be expressed as:
  3585. // m = PerspectiveFovLHToRef
  3586. // m[10] *= -1.0;
  3587. // m[11] *= -1.0;
  3588. let n = znear;
  3589. let f = zfar;
  3590. let t = 1.0 / (Math.tan(fov * 0.5));
  3591. let a = isVerticalFovFixed ? (t / aspect) : t;
  3592. let b = isVerticalFovFixed ? t : (t * aspect);
  3593. let c = -(f + n) / (f - n);
  3594. let d = -2 * f * n / (f - n);
  3595. BABYLON.Matrix.FromValuesToRef(
  3596. a, 0.0, 0.0, 0.0,
  3597. 0.0, b, 0.0, 0.0,
  3598. 0.0, 0.0, c, -1.0,
  3599. 0.0, 0.0, d, 0.0,
  3600. result
  3601. );
  3602. }
  3603. /**
  3604. * Sets the passed matrix "result" as a left-handed perspective projection matrix for WebVR computed from the passed floats : vertical angle of view (fov), width/height ratio (aspect), z near and far limits.
  3605. */
  3606. public static PerspectiveFovWebVRToRef(fov, znear: number, zfar: number, result: Matrix, rightHanded = false): void {
  3607. var rightHandedFactor = rightHanded ? -1 : 1;
  3608. var upTan = Math.tan(fov.upDegrees * Math.PI / 180.0);
  3609. var downTan = Math.tan(fov.downDegrees * Math.PI / 180.0);
  3610. var leftTan = Math.tan(fov.leftDegrees * Math.PI / 180.0);
  3611. var rightTan = Math.tan(fov.rightDegrees * Math.PI / 180.0);
  3612. var xScale = 2.0 / (leftTan + rightTan);
  3613. var yScale = 2.0 / (upTan + downTan);
  3614. result.m[0] = xScale;
  3615. result.m[1] = result.m[2] = result.m[3] = result.m[4] = 0.0;
  3616. result.m[5] = yScale;
  3617. result.m[6] = result.m[7] = 0.0;
  3618. result.m[8] = ((leftTan - rightTan) * xScale * 0.5)// * rightHandedFactor;
  3619. result.m[9] = -((upTan - downTan) * yScale * 0.5)// * rightHandedFactor;
  3620. //result.m[10] = -(znear + zfar) / (zfar - znear) * rightHandedFactor;
  3621. result.m[10] = -zfar / (znear - zfar);
  3622. result.m[11] = 1.0 * rightHandedFactor;
  3623. result.m[12] = result.m[13] = result.m[15] = 0.0;
  3624. result.m[14] = -(2.0 * zfar * znear) / (zfar - znear);
  3625. // result.m[14] = (znear * zfar) / (znear - zfar);
  3626. result._markAsUpdated();
  3627. }
  3628. /**
  3629. * Returns the final transformation matrix : world * view * projection * viewport
  3630. */
  3631. public static GetFinalMatrix(viewport: Viewport, world: Matrix, view: Matrix, projection: Matrix, zmin: number, zmax: number): Matrix {
  3632. var cw = viewport.width;
  3633. var ch = viewport.height;
  3634. var cx = viewport.x;
  3635. var cy = viewport.y;
  3636. var viewportMatrix = Matrix.FromValues(cw / 2.0, 0.0, 0.0, 0.0,
  3637. 0.0, -ch / 2.0, 0.0, 0.0,
  3638. 0.0, 0.0, zmax - zmin, 0.0,
  3639. cx + cw / 2.0, ch / 2.0 + cy, zmin, 1);
  3640. return world.multiply(view).multiply(projection).multiply(viewportMatrix);
  3641. }
  3642. /**
  3643. * Returns a new Float32Array array with 4 elements : the 2x2 matrix extracted from the passed Matrix.
  3644. */
  3645. public static GetAsMatrix2x2(matrix: Matrix): Float32Array {
  3646. return new Float32Array([
  3647. matrix.m[0], matrix.m[1],
  3648. matrix.m[4], matrix.m[5]
  3649. ]);
  3650. }
  3651. /**
  3652. * Returns a new Float32Array array with 9 elements : the 3x3 matrix extracted from the passed Matrix.
  3653. */
  3654. public static GetAsMatrix3x3(matrix: Matrix): Float32Array {
  3655. return new Float32Array([
  3656. matrix.m[0], matrix.m[1], matrix.m[2],
  3657. matrix.m[4], matrix.m[5], matrix.m[6],
  3658. matrix.m[8], matrix.m[9], matrix.m[10]
  3659. ]);
  3660. }
  3661. /**
  3662. * Compute the transpose of the passed Matrix.
  3663. * Returns a new Matrix.
  3664. */
  3665. public static Transpose(matrix: Matrix): Matrix {
  3666. var result = new Matrix();
  3667. result.m[0] = matrix.m[0];
  3668. result.m[1] = matrix.m[4];
  3669. result.m[2] = matrix.m[8];
  3670. result.m[3] = matrix.m[12];
  3671. result.m[4] = matrix.m[1];
  3672. result.m[5] = matrix.m[5];
  3673. result.m[6] = matrix.m[9];
  3674. result.m[7] = matrix.m[13];
  3675. result.m[8] = matrix.m[2];
  3676. result.m[9] = matrix.m[6];
  3677. result.m[10] = matrix.m[10];
  3678. result.m[11] = matrix.m[14];
  3679. result.m[12] = matrix.m[3];
  3680. result.m[13] = matrix.m[7];
  3681. result.m[14] = matrix.m[11];
  3682. result.m[15] = matrix.m[15];
  3683. return result;
  3684. }
  3685. /**
  3686. * Returns a new Matrix as the reflection matrix across the passed plane.
  3687. */
  3688. public static Reflection(plane: Plane): Matrix {
  3689. var matrix = new Matrix();
  3690. Matrix.ReflectionToRef(plane, matrix);
  3691. return matrix;
  3692. }
  3693. /**
  3694. * Sets the passed matrix "result" as the reflection matrix across the passed plane.
  3695. */
  3696. public static ReflectionToRef(plane: Plane, result: Matrix): void {
  3697. plane.normalize();
  3698. var x = plane.normal.x;
  3699. var y = plane.normal.y;
  3700. var z = plane.normal.z;
  3701. var temp = -2 * x;
  3702. var temp2 = -2 * y;
  3703. var temp3 = -2 * z;
  3704. result.m[0] = (temp * x) + 1;
  3705. result.m[1] = temp2 * x;
  3706. result.m[2] = temp3 * x;
  3707. result.m[3] = 0.0;
  3708. result.m[4] = temp * y;
  3709. result.m[5] = (temp2 * y) + 1;
  3710. result.m[6] = temp3 * y;
  3711. result.m[7] = 0.0;
  3712. result.m[8] = temp * z;
  3713. result.m[9] = temp2 * z;
  3714. result.m[10] = (temp3 * z) + 1;
  3715. result.m[11] = 0.0;
  3716. result.m[12] = temp * plane.d;
  3717. result.m[13] = temp2 * plane.d;
  3718. result.m[14] = temp3 * plane.d;
  3719. result.m[15] = 1.0;
  3720. result._markAsUpdated();
  3721. }
  3722. /**
  3723. * Sets the passed matrix "mat" as a rotation matrix composed from the 3 passed left handed axis.
  3724. */
  3725. public static FromXYZAxesToRef(xaxis: Vector3, yaxis: Vector3, zaxis: Vector3, result: Matrix) {
  3726. result.m[0] = xaxis.x;
  3727. result.m[1] = xaxis.y;
  3728. result.m[2] = xaxis.z;
  3729. result.m[3] = 0.0;
  3730. result.m[4] = yaxis.x;
  3731. result.m[5] = yaxis.y;
  3732. result.m[6] = yaxis.z;
  3733. result.m[7] = 0.0;
  3734. result.m[8] = zaxis.x;
  3735. result.m[9] = zaxis.y;
  3736. result.m[10] = zaxis.z;
  3737. result.m[11] = 0.0;
  3738. result.m[12] = 0.0;
  3739. result.m[13] = 0.0;
  3740. result.m[14] = 0.0;
  3741. result.m[15] = 1.0;
  3742. result._markAsUpdated();
  3743. }
  3744. /**
  3745. * Sets the passed matrix "result" as a rotation matrix according to the passed quaternion.
  3746. */
  3747. public static FromQuaternionToRef(quat: Quaternion, result: Matrix) {
  3748. var xx = quat.x * quat.x;
  3749. var yy = quat.y * quat.y;
  3750. var zz = quat.z * quat.z;
  3751. var xy = quat.x * quat.y;
  3752. var zw = quat.z * quat.w;
  3753. var zx = quat.z * quat.x;
  3754. var yw = quat.y * quat.w;
  3755. var yz = quat.y * quat.z;
  3756. var xw = quat.x * quat.w;
  3757. result.m[0] = 1.0 - (2.0 * (yy + zz));
  3758. result.m[1] = 2.0 * (xy + zw);
  3759. result.m[2] = 2.0 * (zx - yw);
  3760. result.m[3] = 0.0;
  3761. result.m[4] = 2.0 * (xy - zw);
  3762. result.m[5] = 1.0 - (2.0 * (zz + xx));
  3763. result.m[6] = 2.0 * (yz + xw);
  3764. result.m[7] = 0.0;
  3765. result.m[8] = 2.0 * (zx + yw);
  3766. result.m[9] = 2.0 * (yz - xw);
  3767. result.m[10] = 1.0 - (2.0 * (yy + xx));
  3768. result.m[11] = 0.0;
  3769. result.m[12] = 0.0;
  3770. result.m[13] = 0.0;
  3771. result.m[14] = 0.0;
  3772. result.m[15] = 1.0;
  3773. result._markAsUpdated();
  3774. }
  3775. }
  3776. export class Plane {
  3777. public normal: Vector3;
  3778. public d: number;
  3779. /**
  3780. * Creates a Plane object according to the passed floats a, b, c, d and the plane equation : ax + by + cz + d = 0
  3781. */
  3782. constructor(a: number, b: number, c: number, d: number) {
  3783. this.normal = new Vector3(a, b, c);
  3784. this.d = d;
  3785. }
  3786. /**
  3787. * Returns the plane coordinates as a new array of 4 elements [a, b, c, d].
  3788. */
  3789. public asArray(): number[] {
  3790. return [this.normal.x, this.normal.y, this.normal.z, this.d];
  3791. }
  3792. // Methods
  3793. /**
  3794. * Returns a new plane copied from the current Plane.
  3795. */
  3796. public clone(): Plane {
  3797. return new Plane(this.normal.x, this.normal.y, this.normal.z, this.d);
  3798. }
  3799. /**
  3800. * Returns the string "Plane".
  3801. */
  3802. public getClassName(): string {
  3803. return "Plane";
  3804. }
  3805. /**
  3806. * Returns the Plane hash code.
  3807. */
  3808. public getHashCode(): number {
  3809. let hash = this.normal.getHashCode();
  3810. hash = (hash * 397) ^ (this.d || 0);
  3811. return hash;
  3812. }
  3813. /**
  3814. * Normalize the current Plane in place.
  3815. * Returns the updated Plane.
  3816. */
  3817. public normalize(): Plane {
  3818. var norm = (Math.sqrt((this.normal.x * this.normal.x) + (this.normal.y * this.normal.y) + (this.normal.z * this.normal.z)));
  3819. var magnitude = 0.0;
  3820. if (norm !== 0) {
  3821. magnitude = 1.0 / norm;
  3822. }
  3823. this.normal.x *= magnitude;
  3824. this.normal.y *= magnitude;
  3825. this.normal.z *= magnitude;
  3826. this.d *= magnitude;
  3827. return this;
  3828. }
  3829. /**
  3830. * Returns a new Plane as the result of the transformation of the current Plane by the passed matrix.
  3831. */
  3832. public transform(transformation: Matrix): Plane {
  3833. var transposedMatrix = Matrix.Transpose(transformation);
  3834. var x = this.normal.x;
  3835. var y = this.normal.y;
  3836. var z = this.normal.z;
  3837. var d = this.d;
  3838. var normalX = (((x * transposedMatrix.m[0]) + (y * transposedMatrix.m[1])) + (z * transposedMatrix.m[2])) + (d * transposedMatrix.m[3]);
  3839. var normalY = (((x * transposedMatrix.m[4]) + (y * transposedMatrix.m[5])) + (z * transposedMatrix.m[6])) + (d * transposedMatrix.m[7]);
  3840. var normalZ = (((x * transposedMatrix.m[8]) + (y * transposedMatrix.m[9])) + (z * transposedMatrix.m[10])) + (d * transposedMatrix.m[11]);
  3841. var finalD = (((x * transposedMatrix.m[12]) + (y * transposedMatrix.m[13])) + (z * transposedMatrix.m[14])) + (d * transposedMatrix.m[15]);
  3842. return new Plane(normalX, normalY, normalZ, finalD);
  3843. }
  3844. /**
  3845. * Returns the dot product (float) of the point coordinates and the plane normal.
  3846. */
  3847. public dotCoordinate(point): number {
  3848. return ((((this.normal.x * point.x) + (this.normal.y * point.y)) + (this.normal.z * point.z)) + this.d);
  3849. }
  3850. /**
  3851. * Updates the current Plane from the plane defined by the three passed points.
  3852. * Returns the updated Plane.
  3853. */
  3854. public copyFromPoints(point1: Vector3, point2: Vector3, point3: Vector3): Plane {
  3855. var x1 = point2.x - point1.x;
  3856. var y1 = point2.y - point1.y;
  3857. var z1 = point2.z - point1.z;
  3858. var x2 = point3.x - point1.x;
  3859. var y2 = point3.y - point1.y;
  3860. var z2 = point3.z - point1.z;
  3861. var yz = (y1 * z2) - (z1 * y2);
  3862. var xz = (z1 * x2) - (x1 * z2);
  3863. var xy = (x1 * y2) - (y1 * x2);
  3864. var pyth = (Math.sqrt((yz * yz) + (xz * xz) + (xy * xy)));
  3865. var invPyth;
  3866. if (pyth !== 0) {
  3867. invPyth = 1.0 / pyth;
  3868. }
  3869. else {
  3870. invPyth = 0.0;
  3871. }
  3872. this.normal.x = yz * invPyth;
  3873. this.normal.y = xz * invPyth;
  3874. this.normal.z = xy * invPyth;
  3875. this.d = -((this.normal.x * point1.x) + (this.normal.y * point1.y) + (this.normal.z * point1.z));
  3876. return this;
  3877. }
  3878. /**
  3879. * Boolean : True is the vector "direction" is the same side than the plane normal.
  3880. */
  3881. public isFrontFacingTo(direction: Vector3, epsilon: number): boolean {
  3882. var dot = Vector3.Dot(this.normal, direction);
  3883. return (dot <= epsilon);
  3884. }
  3885. /**
  3886. * Returns the signed distance (float) from the passed point to the Plane.
  3887. */
  3888. public signedDistanceTo(point: Vector3): number {
  3889. return Vector3.Dot(point, this.normal) + this.d;
  3890. }
  3891. // Statics
  3892. /**
  3893. * Returns a new Plane from the passed array.
  3894. */
  3895. static FromArray(array: number[]): Plane {
  3896. return new Plane(array[0], array[1], array[2], array[3]);
  3897. }
  3898. /**
  3899. * Returns a new Plane defined by the three passed points.
  3900. */
  3901. static FromPoints(point1, point2, point3): Plane {
  3902. var result = new Plane(0.0, 0.0, 0.0, 0.0);
  3903. result.copyFromPoints(point1, point2, point3);
  3904. return result;
  3905. }
  3906. /**
  3907. * Returns a new Plane the normal vector to this plane at the passed origin point.
  3908. * Note : the vector "normal" is updated because normalized.
  3909. */
  3910. static FromPositionAndNormal(origin: Vector3, normal: Vector3): Plane {
  3911. var result = new Plane(0.0, 0.0, 0.0, 0.0);
  3912. normal.normalize();
  3913. result.normal = normal;
  3914. result.d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  3915. return result;
  3916. }
  3917. /**
  3918. * Returns the signed distance between the plane defined by the normal vector at the "origin"" point and the passed other point.
  3919. */
  3920. static SignedDistanceToPlaneFromPositionAndNormal(origin: Vector3, normal: Vector3, point: Vector3): number {
  3921. var d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  3922. return Vector3.Dot(point, normal) + d;
  3923. }
  3924. }
  3925. export class Viewport {
  3926. /**
  3927. * Creates a Viewport object located at (x, y) and sized (width, height).
  3928. */
  3929. constructor(public x: number, public y: number, public width: number, public height: number) {
  3930. }
  3931. public toGlobal(renderWidth: number, renderHeight: number): Viewport {
  3932. return new Viewport(this.x * renderWidth, this.y * renderHeight, this.width * renderWidth, this.height * renderHeight);
  3933. }
  3934. /**
  3935. * Returns a new Viewport copied from the current one.
  3936. */
  3937. public clone(): Viewport {
  3938. return new Viewport(this.x, this.y, this.width, this.height);
  3939. }
  3940. }
  3941. export class Frustum {
  3942. /**
  3943. * Returns a new array of 6 Frustum planes computed by the passed transformation matrix.
  3944. */
  3945. public static GetPlanes(transform: Matrix): Plane[] {
  3946. var frustumPlanes = [];
  3947. for (var index = 0; index < 6; index++) {
  3948. frustumPlanes.push(new Plane(0.0, 0.0, 0.0, 0.0));
  3949. }
  3950. Frustum.GetPlanesToRef(transform, frustumPlanes);
  3951. return frustumPlanes;
  3952. }
  3953. /**
  3954. * Sets the passed array "frustumPlanes" with the 6 Frustum planes computed by the passed transformation matrix.
  3955. */
  3956. public static GetPlanesToRef(transform: Matrix, frustumPlanes: Plane[]): void {
  3957. // Near
  3958. frustumPlanes[0].normal.x = transform.m[3] + transform.m[2];
  3959. frustumPlanes[0].normal.y = transform.m[7] + transform.m[6];
  3960. frustumPlanes[0].normal.z = transform.m[11] + transform.m[10];
  3961. frustumPlanes[0].d = transform.m[15] + transform.m[14];
  3962. frustumPlanes[0].normalize();
  3963. // Far
  3964. frustumPlanes[1].normal.x = transform.m[3] - transform.m[2];
  3965. frustumPlanes[1].normal.y = transform.m[7] - transform.m[6];
  3966. frustumPlanes[1].normal.z = transform.m[11] - transform.m[10];
  3967. frustumPlanes[1].d = transform.m[15] - transform.m[14];
  3968. frustumPlanes[1].normalize();
  3969. // Left
  3970. frustumPlanes[2].normal.x = transform.m[3] + transform.m[0];
  3971. frustumPlanes[2].normal.y = transform.m[7] + transform.m[4];
  3972. frustumPlanes[2].normal.z = transform.m[11] + transform.m[8];
  3973. frustumPlanes[2].d = transform.m[15] + transform.m[12];
  3974. frustumPlanes[2].normalize();
  3975. // Right
  3976. frustumPlanes[3].normal.x = transform.m[3] - transform.m[0];
  3977. frustumPlanes[3].normal.y = transform.m[7] - transform.m[4];
  3978. frustumPlanes[3].normal.z = transform.m[11] - transform.m[8];
  3979. frustumPlanes[3].d = transform.m[15] - transform.m[12];
  3980. frustumPlanes[3].normalize();
  3981. // Top
  3982. frustumPlanes[4].normal.x = transform.m[3] - transform.m[1];
  3983. frustumPlanes[4].normal.y = transform.m[7] - transform.m[5];
  3984. frustumPlanes[4].normal.z = transform.m[11] - transform.m[9];
  3985. frustumPlanes[4].d = transform.m[15] - transform.m[13];
  3986. frustumPlanes[4].normalize();
  3987. // Bottom
  3988. frustumPlanes[5].normal.x = transform.m[3] + transform.m[1];
  3989. frustumPlanes[5].normal.y = transform.m[7] + transform.m[5];
  3990. frustumPlanes[5].normal.z = transform.m[11] + transform.m[9];
  3991. frustumPlanes[5].d = transform.m[15] + transform.m[13];
  3992. frustumPlanes[5].normalize();
  3993. }
  3994. }
  3995. export enum Space {
  3996. LOCAL = 0,
  3997. WORLD = 1,
  3998. BONE = 2
  3999. }
  4000. export class Axis {
  4001. public static X: Vector3 = new Vector3(1.0, 0.0, 0.0);
  4002. public static Y: Vector3 = new Vector3(0.0, 1.0, 0.0);
  4003. public static Z: Vector3 = new Vector3(0.0, 0.0, 1.0);
  4004. };
  4005. export class BezierCurve {
  4006. /**
  4007. * Returns the cubic Bezier interpolated value (float) at "t" (float) from the passed x1, y1, x2, y2 floats.
  4008. */
  4009. public static interpolate(t: number, x1: number, y1: number, x2: number, y2: number): number {
  4010. // Extract X (which is equal to time here)
  4011. var f0 = 1 - 3 * x2 + 3 * x1;
  4012. var f1 = 3 * x2 - 6 * x1;
  4013. var f2 = 3 * x1;
  4014. var refinedT = t;
  4015. for (var i = 0; i < 5; i++) {
  4016. var refinedT2 = refinedT * refinedT;
  4017. var refinedT3 = refinedT2 * refinedT;
  4018. var x = f0 * refinedT3 + f1 * refinedT2 + f2 * refinedT;
  4019. var slope = 1.0 / (3.0 * f0 * refinedT2 + 2.0 * f1 * refinedT + f2);
  4020. refinedT -= (x - t) * slope;
  4021. refinedT = Math.min(1, Math.max(0, refinedT));
  4022. }
  4023. // Resolve cubic bezier for the given x
  4024. return 3 * Math.pow(1 - refinedT, 2) * refinedT * y1 +
  4025. 3 * (1 - refinedT) * Math.pow(refinedT, 2) * y2 +
  4026. Math.pow(refinedT, 3);
  4027. }
  4028. }
  4029. export enum Orientation {
  4030. CW = 0,
  4031. CCW = 1
  4032. }
  4033. export class Angle {
  4034. private _radians: number;
  4035. /**
  4036. * Creates an Angle object of "radians" radians (float).
  4037. */
  4038. constructor(radians: number) {
  4039. this._radians = radians;
  4040. if (this._radians < 0.0) this._radians += (2.0 * Math.PI);
  4041. }
  4042. /**
  4043. * Returns the Angle value in degrees (float).
  4044. */
  4045. public degrees = () => this._radians * 180.0 / Math.PI;
  4046. /**
  4047. * Returns the Angle value in radians (float).
  4048. */
  4049. public radians = () => this._radians;
  4050. /**
  4051. * Returns a new Angle object valued with the angle value in radians between the two passed vectors.
  4052. */
  4053. public static BetweenTwoPoints(a: Vector2, b: Vector2): Angle {
  4054. var delta = b.subtract(a);
  4055. var theta = Math.atan2(delta.y, delta.x);
  4056. return new Angle(theta);
  4057. }
  4058. /**
  4059. * Returns a new Angle object from the passed float in radians.
  4060. */
  4061. public static FromRadians(radians: number): Angle {
  4062. return new Angle(radians);
  4063. }
  4064. /**
  4065. * Returns a new Angle object from the passed float in degrees.
  4066. */
  4067. public static FromDegrees(degrees: number): Angle {
  4068. return new Angle(degrees * Math.PI / 180.0);
  4069. }
  4070. }
  4071. export class Arc2 {
  4072. centerPoint: Vector2;
  4073. radius: number;
  4074. angle: Angle;
  4075. startAngle: Angle;
  4076. orientation: Orientation;
  4077. /**
  4078. * Creates an Arc object from the three passed points : start, middle and end.
  4079. */
  4080. constructor(public startPoint: Vector2, public midPoint: Vector2, public endPoint: Vector2) {
  4081. var temp = Math.pow(midPoint.x, 2) + Math.pow(midPoint.y, 2);
  4082. var startToMid = (Math.pow(startPoint.x, 2) + Math.pow(startPoint.y, 2) - temp) / 2.;
  4083. var midToEnd = (temp - Math.pow(endPoint.x, 2) - Math.pow(endPoint.y, 2)) / 2.;
  4084. var det = (startPoint.x - midPoint.x) * (midPoint.y - endPoint.y) - (midPoint.x - endPoint.x) * (startPoint.y - midPoint.y);
  4085. this.centerPoint = new Vector2(
  4086. (startToMid * (midPoint.y - endPoint.y) - midToEnd * (startPoint.y - midPoint.y)) / det,
  4087. ((startPoint.x - midPoint.x) * midToEnd - (midPoint.x - endPoint.x) * startToMid) / det
  4088. );
  4089. this.radius = this.centerPoint.subtract(this.startPoint).length();
  4090. this.startAngle = Angle.BetweenTwoPoints(this.centerPoint, this.startPoint);
  4091. var a1 = this.startAngle.degrees();
  4092. var a2 = Angle.BetweenTwoPoints(this.centerPoint, this.midPoint).degrees();
  4093. var a3 = Angle.BetweenTwoPoints(this.centerPoint, this.endPoint).degrees();
  4094. // angles correction
  4095. if (a2 - a1 > +180.0) a2 -= 360.0;
  4096. if (a2 - a1 < -180.0) a2 += 360.0;
  4097. if (a3 - a2 > +180.0) a3 -= 360.0;
  4098. if (a3 - a2 < -180.0) a3 += 360.0;
  4099. this.orientation = (a2 - a1) < 0 ? Orientation.CW : Orientation.CCW;
  4100. this.angle = Angle.FromDegrees(this.orientation === Orientation.CW ? a1 - a3 : a3 - a1);
  4101. }
  4102. }
  4103. export class Path2 {
  4104. private _points = new Array<Vector2>();
  4105. private _length = 0.0;
  4106. public closed = false;
  4107. /**
  4108. * Creates a Path2 object from the starting 2D coordinates x and y.
  4109. */
  4110. constructor(x: number, y: number) {
  4111. this._points.push(new Vector2(x, y));
  4112. }
  4113. /**
  4114. * Adds a new segment until the passed coordinates (x, y) to the current Path2.
  4115. * Returns the updated Path2.
  4116. */
  4117. public addLineTo(x: number, y: number): Path2 {
  4118. if (closed) {
  4119. //Tools.Error("cannot add lines to closed paths");
  4120. return this;
  4121. }
  4122. var newPoint = new Vector2(x, y);
  4123. var previousPoint = this._points[this._points.length - 1];
  4124. this._points.push(newPoint);
  4125. this._length += newPoint.subtract(previousPoint).length();
  4126. return this;
  4127. }
  4128. /**
  4129. * Adds _numberOfSegments_ segments according to the arc definition (middle point coordinates, end point coordinates, the arc start point being the current Path2 last point) to the current Path2.
  4130. * Returns the updated Path2.
  4131. */
  4132. public addArcTo(midX: number, midY: number, endX: number, endY: number, numberOfSegments = 36): Path2 {
  4133. if (closed) {
  4134. //Tools.Error("cannot add arcs to closed paths");
  4135. return this;
  4136. }
  4137. var startPoint = this._points[this._points.length - 1];
  4138. var midPoint = new Vector2(midX, midY);
  4139. var endPoint = new Vector2(endX, endY);
  4140. var arc = new Arc2(startPoint, midPoint, endPoint);
  4141. var increment = arc.angle.radians() / numberOfSegments;
  4142. if (arc.orientation === Orientation.CW) increment *= -1;
  4143. var currentAngle = arc.startAngle.radians() + increment;
  4144. for (var i = 0; i < numberOfSegments; i++) {
  4145. var x = Math.cos(currentAngle) * arc.radius + arc.centerPoint.x;
  4146. var y = Math.sin(currentAngle) * arc.radius + arc.centerPoint.y;
  4147. this.addLineTo(x, y);
  4148. currentAngle += increment;
  4149. }
  4150. return this;
  4151. }
  4152. /**
  4153. * Closes the Path2.
  4154. * Returns the Path2.
  4155. */
  4156. public close(): Path2 {
  4157. this.closed = true;
  4158. return this;
  4159. }
  4160. /**
  4161. * Returns the Path2 total length (float).
  4162. */
  4163. public length(): number {
  4164. var result = this._length;
  4165. if (!this.closed) {
  4166. var lastPoint = this._points[this._points.length - 1];
  4167. var firstPoint = this._points[0];
  4168. result += (firstPoint.subtract(lastPoint).length());
  4169. }
  4170. return result;
  4171. }
  4172. /**
  4173. * Returns the Path2 internal array of points.
  4174. */
  4175. public getPoints(): Vector2[] {
  4176. return this._points;
  4177. }
  4178. /**
  4179. * Returns a new Vector2 located at a percentage of the Path2 total length on this path.
  4180. */
  4181. public getPointAtLengthPosition(normalizedLengthPosition: number): Vector2 {
  4182. if (normalizedLengthPosition < 0 || normalizedLengthPosition > 1) {
  4183. //Tools.Error("normalized length position should be between 0 and 1.");
  4184. return Vector2.Zero();
  4185. }
  4186. var lengthPosition = normalizedLengthPosition * this.length();
  4187. var previousOffset = 0;
  4188. for (var i = 0; i < this._points.length; i++) {
  4189. var j = (i + 1) % this._points.length;
  4190. var a = this._points[i];
  4191. var b = this._points[j];
  4192. var bToA = b.subtract(a);
  4193. var nextOffset = (bToA.length() + previousOffset);
  4194. if (lengthPosition >= previousOffset && lengthPosition <= nextOffset) {
  4195. var dir = bToA.normalize();
  4196. var localOffset = lengthPosition - previousOffset;
  4197. return new Vector2(
  4198. a.x + (dir.x * localOffset),
  4199. a.y + (dir.y * localOffset)
  4200. );
  4201. }
  4202. previousOffset = nextOffset;
  4203. }
  4204. //Tools.Error("internal error");
  4205. return Vector2.Zero();
  4206. }
  4207. /**
  4208. * Returns a new Path2 starting at the coordinates (x, y).
  4209. */
  4210. public static StartingAt(x: number, y: number): Path2 {
  4211. return new Path2(x, y);
  4212. }
  4213. }
  4214. export class Path3D {
  4215. private _curve = new Array<Vector3>();
  4216. private _distances = new Array<number>();
  4217. private _tangents = new Array<Vector3>();
  4218. private _normals = new Array<Vector3>();
  4219. private _binormals = new Array<Vector3>();
  4220. private _raw: boolean;
  4221. /**
  4222. * new Path3D(path, normal, raw)
  4223. * Creates a Path3D. A Path3D is a logical math object, so not a mesh.
  4224. * please read the description in the tutorial : http://doc.babylonjs.com/tutorials/How_to_use_Path3D
  4225. * path : an array of Vector3, the curve axis of the Path3D
  4226. * normal (optional) : Vector3, the first wanted normal to the curve. Ex (0, 1, 0) for a vertical normal.
  4227. * raw (optional, default false) : boolean, if true the returned Path3D isn't normalized. Useful to depict path acceleration or speed.
  4228. */
  4229. constructor(public path: Vector3[], firstNormal?: Vector3, raw?: boolean) {
  4230. for (var p = 0; p < path.length; p++) {
  4231. this._curve[p] = path[p].clone(); // hard copy
  4232. }
  4233. this._raw = raw || false;
  4234. this._compute(firstNormal);
  4235. }
  4236. /**
  4237. * Returns the Path3D array of successive Vector3 designing its curve.
  4238. */
  4239. public getCurve(): Vector3[] {
  4240. return this._curve;
  4241. }
  4242. /**
  4243. * Returns an array populated with tangent vectors on each Path3D curve point.
  4244. */
  4245. public getTangents(): Vector3[] {
  4246. return this._tangents;
  4247. }
  4248. /**
  4249. * Returns an array populated with normal vectors on each Path3D curve point.
  4250. */
  4251. public getNormals(): Vector3[] {
  4252. return this._normals;
  4253. }
  4254. /**
  4255. * Returns an array populated with binormal vectors on each Path3D curve point.
  4256. */
  4257. public getBinormals(): Vector3[] {
  4258. return this._binormals;
  4259. }
  4260. /**
  4261. * Returns an array populated with distances (float) of the i-th point from the first curve point.
  4262. */
  4263. public getDistances(): number[] {
  4264. return this._distances;
  4265. }
  4266. /**
  4267. * Forces the Path3D tangent, normal, binormal and distance recomputation.
  4268. * Returns the same object updated.
  4269. */
  4270. public update(path: Vector3[], firstNormal?: Vector3): Path3D {
  4271. for (var p = 0; p < path.length; p++) {
  4272. this._curve[p].x = path[p].x;
  4273. this._curve[p].y = path[p].y;
  4274. this._curve[p].z = path[p].z;
  4275. }
  4276. this._compute(firstNormal);
  4277. return this;
  4278. }
  4279. // private function compute() : computes tangents, normals and binormals
  4280. private _compute(firstNormal) {
  4281. var l = this._curve.length;
  4282. // first and last tangents
  4283. this._tangents[0] = this._getFirstNonNullVector(0);
  4284. if (!this._raw) {
  4285. this._tangents[0].normalize();
  4286. }
  4287. this._tangents[l - 1] = this._curve[l - 1].subtract(this._curve[l - 2]);
  4288. if (!this._raw) {
  4289. this._tangents[l - 1].normalize();
  4290. }
  4291. // normals and binormals at first point : arbitrary vector with _normalVector()
  4292. var tg0 = this._tangents[0];
  4293. var pp0 = this._normalVector(this._curve[0], tg0, firstNormal);
  4294. this._normals[0] = pp0;
  4295. if (!this._raw) {
  4296. this._normals[0].normalize();
  4297. }
  4298. this._binormals[0] = Vector3.Cross(tg0, this._normals[0]);
  4299. if (!this._raw) {
  4300. this._binormals[0].normalize();
  4301. }
  4302. this._distances[0] = 0.0;
  4303. // normals and binormals : next points
  4304. var prev: Vector3; // previous vector (segment)
  4305. var cur: Vector3; // current vector (segment)
  4306. var curTang: Vector3; // current tangent
  4307. // previous normal
  4308. var prevBinor: Vector3; // previous binormal
  4309. for (var i = 1; i < l; i++) {
  4310. // tangents
  4311. prev = this._getLastNonNullVector(i);
  4312. if (i < l - 1) {
  4313. cur = this._getFirstNonNullVector(i);
  4314. this._tangents[i] = prev.add(cur);
  4315. this._tangents[i].normalize();
  4316. }
  4317. this._distances[i] = this._distances[i - 1] + prev.length();
  4318. // normals and binormals
  4319. // http://www.cs.cmu.edu/afs/andrew/scs/cs/15-462/web/old/asst2camera.html
  4320. curTang = this._tangents[i];
  4321. prevBinor = this._binormals[i - 1];
  4322. this._normals[i] = Vector3.Cross(prevBinor, curTang);
  4323. if (!this._raw) {
  4324. this._normals[i].normalize();
  4325. }
  4326. this._binormals[i] = Vector3.Cross(curTang, this._normals[i]);
  4327. if (!this._raw) {
  4328. this._binormals[i].normalize();
  4329. }
  4330. }
  4331. }
  4332. // private function getFirstNonNullVector(index)
  4333. // returns the first non null vector from index : curve[index + N].subtract(curve[index])
  4334. private _getFirstNonNullVector(index: number): Vector3 {
  4335. var i = 1;
  4336. var nNVector: Vector3 = this._curve[index + i].subtract(this._curve[index]);
  4337. while (nNVector.length() === 0 && index + i + 1 < this._curve.length) {
  4338. i++;
  4339. nNVector = this._curve[index + i].subtract(this._curve[index]);
  4340. }
  4341. return nNVector;
  4342. }
  4343. // private function getLastNonNullVector(index)
  4344. // returns the last non null vector from index : curve[index].subtract(curve[index - N])
  4345. private _getLastNonNullVector(index: number): Vector3 {
  4346. var i = 1;
  4347. var nLVector: Vector3 = this._curve[index].subtract(this._curve[index - i]);
  4348. while (nLVector.length() === 0 && index > i + 1) {
  4349. i++;
  4350. nLVector = this._curve[index].subtract(this._curve[index - i]);
  4351. }
  4352. return nLVector;
  4353. }
  4354. // private function normalVector(v0, vt, va) :
  4355. // returns an arbitrary point in the plane defined by the point v0 and the vector vt orthogonal to this plane
  4356. // if va is passed, it returns the va projection on the plane orthogonal to vt at the point v0
  4357. private _normalVector(v0: Vector3, vt: Vector3, va: Vector3): Vector3 {
  4358. var normal0: Vector3;
  4359. var tgl = vt.length();
  4360. if (tgl === 0.0) {
  4361. tgl = 1.0;
  4362. }
  4363. if (va === undefined || va === null) {
  4364. var point: Vector3;
  4365. if (!MathTools.WithinEpsilon(Math.abs(vt.y) / tgl, 1.0, Epsilon)) { // search for a point in the plane
  4366. point = new Vector3(0.0, -1.0, 0.0);
  4367. }
  4368. else if (!MathTools.WithinEpsilon(Math.abs(vt.x) / tgl, 1.0, Epsilon)) {
  4369. point = new Vector3(1.0, 0.0, 0.0);
  4370. }
  4371. else if (!MathTools.WithinEpsilon(Math.abs(vt.z) / tgl, 1.0, Epsilon)) {
  4372. point = new Vector3(0.0, 0.0, 1.0);
  4373. }
  4374. normal0 = Vector3.Cross(vt, point);
  4375. }
  4376. else {
  4377. normal0 = Vector3.Cross(vt, va);
  4378. Vector3.CrossToRef(normal0, vt, normal0);
  4379. }
  4380. normal0.normalize();
  4381. return normal0;
  4382. }
  4383. }
  4384. export class Curve3 {
  4385. private _points: Vector3[];
  4386. private _length: number = 0.0;
  4387. /**
  4388. * Returns a Curve3 object along a Quadratic Bezier curve : http://doc.babylonjs.com/tutorials/How_to_use_Curve3#quadratic-bezier-curve
  4389. * @param v0 (Vector3) the origin point of the Quadratic Bezier
  4390. * @param v1 (Vector3) the control point
  4391. * @param v2 (Vector3) the end point of the Quadratic Bezier
  4392. * @param nbPoints (integer) the wanted number of points in the curve
  4393. */
  4394. public static CreateQuadraticBezier(v0: Vector3, v1: Vector3, v2: Vector3, nbPoints: number): Curve3 {
  4395. nbPoints = nbPoints > 2 ? nbPoints : 3;
  4396. var bez = new Array<Vector3>();
  4397. var equation = (t: number, val0: number, val1: number, val2: number) => {
  4398. var res = (1.0 - t) * (1.0 - t) * val0 + 2.0 * t * (1.0 - t) * val1 + t * t * val2;
  4399. return res;
  4400. }
  4401. for (var i = 0; i <= nbPoints; i++) {
  4402. 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)));
  4403. }
  4404. return new Curve3(bez);
  4405. }
  4406. /**
  4407. * Returns a Curve3 object along a Cubic Bezier curve : http://doc.babylonjs.com/tutorials/How_to_use_Curve3#cubic-bezier-curve
  4408. * @param v0 (Vector3) the origin point of the Cubic Bezier
  4409. * @param v1 (Vector3) the first control point
  4410. * @param v2 (Vector3) the second control point
  4411. * @param v3 (Vector3) the end point of the Cubic Bezier
  4412. * @param nbPoints (integer) the wanted number of points in the curve
  4413. */
  4414. public static CreateCubicBezier(v0: Vector3, v1: Vector3, v2: Vector3, v3: Vector3, nbPoints: number): Curve3 {
  4415. nbPoints = nbPoints > 3 ? nbPoints : 4;
  4416. var bez = new Array<Vector3>();
  4417. var equation = (t: number, val0: number, val1: number, val2: number, val3: number) => {
  4418. var res = (1.0 - t) * (1.0 - t) * (1.0 - t) * val0 + 3.0 * t * (1.0 - t) * (1.0 - t) * val1 + 3.0 * t * t * (1.0 - t) * val2 + t * t * t * val3;
  4419. return res;
  4420. }
  4421. for (var i = 0; i <= nbPoints; i++) {
  4422. 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)));
  4423. }
  4424. return new Curve3(bez);
  4425. }
  4426. /**
  4427. * Returns a Curve3 object along a Hermite Spline curve : http://doc.babylonjs.com/tutorials/How_to_use_Curve3#hermite-spline
  4428. * @param p1 (Vector3) the origin point of the Hermite Spline
  4429. * @param t1 (Vector3) the tangent vector at the origin point
  4430. * @param p2 (Vector3) the end point of the Hermite Spline
  4431. * @param t2 (Vector3) the tangent vector at the end point
  4432. * @param nbPoints (integer) the wanted number of points in the curve
  4433. */
  4434. public static CreateHermiteSpline(p1: Vector3, t1: Vector3, p2: Vector3, t2: Vector3, nbPoints: number): Curve3 {
  4435. var hermite = new Array<Vector3>();
  4436. var step = 1.0 / nbPoints;
  4437. for (var i = 0; i <= nbPoints; i++) {
  4438. hermite.push(Vector3.Hermite(p1, t1, p2, t2, i * step));
  4439. }
  4440. return new Curve3(hermite);
  4441. }
  4442. /**
  4443. * Returns a Curve3 object along a CatmullRom Spline curve :
  4444. * @param points (array of Vector3) the points the spline must pass through. At least, four points required.
  4445. * @param nbPoints (integer) the wanted number of points between each curve control points.
  4446. */
  4447. public static CreateCatmullRomSpline(points: Vector3[], nbPoints: number): Curve3 {
  4448. var totalPoints = new Array<Vector3>();
  4449. totalPoints.push(points[0].clone());
  4450. Array.prototype.push.apply(totalPoints, points);
  4451. totalPoints.push(points[points.length - 1].clone());
  4452. var catmullRom = new Array<Vector3>();
  4453. var step = 1.0 / nbPoints;
  4454. for (var i = 0; i < totalPoints.length - 3; i++) {
  4455. var amount = 0.0;
  4456. for (var c = 0; c < nbPoints; c++) {
  4457. catmullRom.push(Vector3.CatmullRom(totalPoints[i], totalPoints[i + 1], totalPoints[i + 2], totalPoints[i + 3], amount));
  4458. amount += step
  4459. }
  4460. }
  4461. i--;
  4462. catmullRom.push(Vector3.CatmullRom(totalPoints[i], totalPoints[i + 1], totalPoints[i + 2], totalPoints[i + 3], amount));
  4463. return new Curve3(catmullRom);
  4464. }
  4465. /**
  4466. * A Curve3 object is a logical object, so not a mesh, to handle curves in the 3D geometric space.
  4467. * A Curve3 is designed from a series of successive Vector3.
  4468. * Tuto : http://doc.babylonjs.com/tutorials/How_to_use_Curve3#curve3-object
  4469. */
  4470. constructor(points: Vector3[]) {
  4471. this._points = points;
  4472. this._length = this._computeLength(points);
  4473. }
  4474. /**
  4475. * Returns the Curve3 stored array of successive Vector3
  4476. */
  4477. public getPoints() {
  4478. return this._points;
  4479. }
  4480. /**
  4481. * Returns the computed length (float) of the curve.
  4482. */
  4483. public length() {
  4484. return this._length;
  4485. }
  4486. /**
  4487. * Returns a new instance of Curve3 object : var curve = curveA.continue(curveB);
  4488. * This new Curve3 is built by translating and sticking the curveB at the end of the curveA.
  4489. * curveA and curveB keep unchanged.
  4490. */
  4491. public continue(curve: Curve3): Curve3 {
  4492. var lastPoint = this._points[this._points.length - 1];
  4493. var continuedPoints = this._points.slice();
  4494. var curvePoints = curve.getPoints();
  4495. for (var i = 1; i < curvePoints.length; i++) {
  4496. continuedPoints.push(curvePoints[i].subtract(curvePoints[0]).add(lastPoint));
  4497. }
  4498. var continuedCurve = new Curve3(continuedPoints);
  4499. return continuedCurve;
  4500. }
  4501. private _computeLength(path: Vector3[]): number {
  4502. var l = 0;
  4503. for (var i = 1; i < path.length; i++) {
  4504. l += (path[i].subtract(path[i - 1])).length();
  4505. }
  4506. return l;
  4507. }
  4508. }
  4509. // SphericalHarmonics
  4510. export class SphericalHarmonics {
  4511. public L00: Vector3 = Vector3.Zero();
  4512. public L1_1: Vector3 = Vector3.Zero();
  4513. public L10: Vector3 = Vector3.Zero();
  4514. public L11: Vector3 = Vector3.Zero();
  4515. public L2_2: Vector3 = Vector3.Zero();
  4516. public L2_1: Vector3 = Vector3.Zero();
  4517. public L20: Vector3 = Vector3.Zero();
  4518. public L21: Vector3 = Vector3.Zero();
  4519. public L22: Vector3 = Vector3.Zero();
  4520. public addLight(direction: Vector3, color: Color3, deltaSolidAngle: number): void {
  4521. var colorVector = new Vector3(color.r, color.g, color.b);
  4522. var c = colorVector.scale(deltaSolidAngle);
  4523. this.L00 = this.L00.add(c.scale(0.282095));
  4524. this.L1_1 = this.L1_1.add(c.scale(0.488603 * direction.y));
  4525. this.L10 = this.L10.add(c.scale(0.488603 * direction.z));
  4526. this.L11 = this.L11.add(c.scale(0.488603 * direction.x));
  4527. this.L2_2 = this.L2_2.add(c.scale(1.092548 * direction.x * direction.y));
  4528. this.L2_1 = this.L2_1.add(c.scale(1.092548 * direction.y * direction.z));
  4529. this.L21 = this.L21.add(c.scale(1.092548 * direction.x * direction.z));
  4530. this.L20 = this.L20.add(c.scale(0.315392 * (3.0 * direction.z * direction.z - 1.0)));
  4531. this.L22 = this.L22.add(c.scale(0.546274 * (direction.x * direction.x - direction.y * direction.y)));
  4532. }
  4533. public scale(scale: number): void {
  4534. this.L00 = this.L00.scale(scale);
  4535. this.L1_1 = this.L1_1.scale(scale);
  4536. this.L10 = this.L10.scale(scale);
  4537. this.L11 = this.L11.scale(scale);
  4538. this.L2_2 = this.L2_2.scale(scale);
  4539. this.L2_1 = this.L2_1.scale(scale);
  4540. this.L20 = this.L20.scale(scale);
  4541. this.L21 = this.L21.scale(scale);
  4542. this.L22 = this.L22.scale(scale);
  4543. }
  4544. }
  4545. // SphericalPolynomial
  4546. export class SphericalPolynomial {
  4547. public x: Vector3 = Vector3.Zero();
  4548. public y: Vector3 = Vector3.Zero();
  4549. public z: Vector3 = Vector3.Zero();
  4550. public xx: Vector3 = Vector3.Zero();
  4551. public yy: Vector3 = Vector3.Zero();
  4552. public zz: Vector3 = Vector3.Zero();
  4553. public xy: Vector3 = Vector3.Zero();
  4554. public yz: Vector3 = Vector3.Zero();
  4555. public zx: Vector3 = Vector3.Zero();
  4556. public addAmbient(color: Color3): void {
  4557. var colorVector = new Vector3(color.r, color.g, color.b);
  4558. this.xx = this.xx.add(colorVector);
  4559. this.yy = this.yy.add(colorVector);
  4560. this.zz = this.zz.add(colorVector);
  4561. }
  4562. public static getSphericalPolynomialFromHarmonics(harmonics: SphericalHarmonics): SphericalPolynomial {
  4563. var result = new SphericalPolynomial();
  4564. result.x = harmonics.L11.scale(1.02333);
  4565. result.y = harmonics.L1_1.scale(1.02333);
  4566. result.z = harmonics.L10.scale(1.02333);
  4567. result.xx = harmonics.L00.scale(0.886277).subtract(harmonics.L20.scale(0.247708)).add(harmonics.L22.scale(0.429043));
  4568. result.yy = harmonics.L00.scale(0.886277).subtract(harmonics.L20.scale(0.247708)).subtract(harmonics.L22.scale(0.429043));
  4569. result.zz = harmonics.L00.scale(0.886277).add(harmonics.L20.scale(0.495417));
  4570. result.yz = harmonics.L2_1.scale(0.858086);
  4571. result.zx = harmonics.L21.scale(0.858086);
  4572. result.xy = harmonics.L2_2.scale(0.858086);
  4573. return result;
  4574. }
  4575. }
  4576. // Vertex formats
  4577. export class PositionNormalVertex {
  4578. constructor(public position: Vector3 = Vector3.Zero(), public normal: Vector3 = Vector3.Up()) {
  4579. }
  4580. public clone(): PositionNormalVertex {
  4581. return new PositionNormalVertex(this.position.clone(), this.normal.clone());
  4582. }
  4583. }
  4584. export class PositionNormalTextureVertex {
  4585. constructor(public position: Vector3 = Vector3.Zero(), public normal: Vector3 = Vector3.Up(), public uv: Vector2 = Vector2.Zero()) {
  4586. }
  4587. public clone(): PositionNormalTextureVertex {
  4588. return new PositionNormalTextureVertex(this.position.clone(), this.normal.clone(), this.uv.clone());
  4589. }
  4590. }
  4591. // Temporary pre-allocated objects for engine internal use
  4592. // usage in any internal function :
  4593. // var tmp = Tmp.Vector3[0]; <= gets access to the first pre-created Vector3
  4594. // There's a Tmp array per object type : int, float, Vector2, Vector3, Vector4, Quaternion, Matrix
  4595. export class Tmp {
  4596. public static Color3: Color3[] = [Color3.Black(), Color3.Black(), Color3.Black()];
  4597. public static Vector2: Vector2[] = [Vector2.Zero(), Vector2.Zero(), Vector2.Zero()]; // 3 temp Vector2 at once should be enough
  4598. public static Vector3: Vector3[] = [Vector3.Zero(), Vector3.Zero(), Vector3.Zero(),
  4599. Vector3.Zero(), Vector3.Zero(), Vector3.Zero(), Vector3.Zero(), Vector3.Zero(), Vector3.Zero()]; // 9 temp Vector3 at once should be enough
  4600. public static Vector4: Vector4[] = [Vector4.Zero(), Vector4.Zero(), Vector4.Zero()]; // 3 temp Vector4 at once should be enough
  4601. public static Quaternion: Quaternion[] = [Quaternion.Zero(), Quaternion.Zero()]; // 2 temp Quaternion at once should be enough
  4602. public static Matrix: Matrix[] = [Matrix.Zero(), Matrix.Zero(),
  4603. Matrix.Zero(), Matrix.Zero(),
  4604. Matrix.Zero(), Matrix.Zero(),
  4605. Matrix.Zero(), Matrix.Zero()]; // 6 temp Matrices at once should be enough
  4606. }
  4607. // Same as Tmp but not exported to keep it onyl for math functions to avoid conflicts
  4608. class MathTmp {
  4609. public static Vector3: Vector3[] = [Vector3.Zero()];
  4610. public static Matrix: Matrix[] = [Matrix.Zero(), Matrix.Zero()];
  4611. public static Quaternion: Quaternion[] = [Quaternion.Zero()];
  4612. }
  4613. }