babylon.math.ts 205 KB

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