babylon.math.ts 203 KB

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