1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526 |
- module BABYLON {
- declare var SIMD;
- export class Color3 {
- constructor(public r: number = 0, public g: number = 0, public b: number = 0) {
- }
- public toString(): string {
- return "{R: " + this.r + " G:" + this.g + " B:" + this.b + "}";
- }
- // Operators
- public toArray(array: number[], index?: number): Color3 {
- if (index === undefined) {
- index = 0;
- }
- array[index] = this.r;
- array[index + 1] = this.g;
- array[index + 2] = this.b;
- return this;
- }
- public toColor4(alpha = 1): Color4 {
- return new Color4(this.r, this.g, this.b, alpha);
- }
- public asArray(): number[] {
- var result = [];
- this.toArray(result, 0);
- return result;
- }
- public toLuminance(): number {
- return this.r * 0.3 + this.g * 0.59 + this.b * 0.11;
- }
- public multiply(otherColor: Color3): Color3 {
- return new Color3(this.r * otherColor.r, this.g * otherColor.g, this.b * otherColor.b);
- }
- public multiplyToRef(otherColor: Color3, result: Color3): Color3 {
- result.r = this.r * otherColor.r;
- result.g = this.g * otherColor.g;
- result.b = this.b * otherColor.b;
- return this;
- }
- public equals(otherColor: Color3): boolean {
- return otherColor && this.r === otherColor.r && this.g === otherColor.g && this.b === otherColor.b;
- }
- public scale(scale: number): Color3 {
- return new Color3(this.r * scale, this.g * scale, this.b * scale);
- }
- public scaleToRef(scale: number, result: Color3): Color3 {
- result.r = this.r * scale;
- result.g = this.g * scale;
- result.b = this.b * scale;
- return this;
- }
- public add(otherColor: Color3): Color3 {
- return new Color3(this.r + otherColor.r, this.g + otherColor.g, this.b + otherColor.b);
- }
- public addToRef(otherColor: Color3, result: Color3): Color3 {
- result.r = this.r + otherColor.r;
- result.g = this.g + otherColor.g;
- result.b = this.b + otherColor.b;
- return this;
- }
- public subtract(otherColor: Color3): Color3 {
- return new Color3(this.r - otherColor.r, this.g - otherColor.g, this.b - otherColor.b);
- }
- public subtractToRef(otherColor: Color3, result: Color3): Color3 {
- result.r = this.r - otherColor.r;
- result.g = this.g - otherColor.g;
- result.b = this.b - otherColor.b;
- return this;
- }
- public clone(): Color3 {
- return new Color3(this.r, this.g, this.b);
- }
- public copyFrom(source: Color3): Color3 {
- this.r = source.r;
- this.g = source.g;
- this.b = source.b;
- return this;
- }
- public copyFromFloats(r: number, g: number, b: number): Color3 {
- this.r = r;
- this.g = g;
- this.b = b;
- return this;
- }
- // Statics
- public static FromArray(array: number[], offset: number = 0): Color3 {
- return new Color3(array[offset], array[offset + 1], array[offset + 2]);
- }
- public static FromInts(r: number, g: number, b: number): Color3 {
- return new Color3(r / 255.0, g / 255.0, b / 255.0);
- }
- public static Lerp(start: Color3, end: Color3, amount: number): Color3 {
- var r = start.r + ((end.r - start.r) * amount);
- var g = start.g + ((end.g - start.g) * amount);
- var b = start.b + ((end.b - start.b) * amount);
- return new Color3(r, g, b);
- }
- public static Red(): Color3 { return new Color3(1, 0, 0); }
- public static Green(): Color3 { return new Color3(0, 1, 0); }
- public static Blue(): Color3 { return new Color3(0, 0, 1); }
- public static Black(): Color3 { return new Color3(0, 0, 0); }
- public static White(): Color3 { return new Color3(1, 1, 1); }
- public static Purple(): Color3 { return new Color3(0.5, 0, 0.5); }
- public static Magenta(): Color3 { return new Color3(1, 0, 1); }
- public static Yellow(): Color3 { return new Color3(1, 1, 0); }
- public static Gray(): Color3 { return new Color3(0.5, 0.5, 0.5); }
- }
- export class Color4 {
- constructor(public r: number, public g: number, public b: number, public a: number) {
- }
- // Operators
- public addInPlace(right): Color4 {
- this.r += right.r;
- this.g += right.g;
- this.b += right.b;
- this.a += right.a;
- return this;
- }
- public asArray(): number[] {
- var result = [];
- this.toArray(result, 0);
- return result;
- }
- public toArray(array: number[], index?: number): Color4 {
- if (index === undefined) {
- index = 0;
- }
- array[index] = this.r;
- array[index + 1] = this.g;
- array[index + 2] = this.b;
- array[index + 3] = this.a;
- return this;
- }
- public add(right: Color4): Color4 {
- return new Color4(this.r + right.r, this.g + right.g, this.b + right.b, this.a + right.a);
- }
- public subtract(right: Color4): Color4 {
- return new Color4(this.r - right.r, this.g - right.g, this.b - right.b, this.a - right.a);
- }
- public subtractToRef(right: Color4, result: Color4): Color4 {
- result.r = this.r - right.r;
- result.g = this.g - right.g;
- result.b = this.b - right.b;
- result.a = this.a - right.a;
- return this;
- }
- public scale(scale: number): Color4 {
- return new Color4(this.r * scale, this.g * scale, this.b * scale, this.a * scale);
- }
- public scaleToRef(scale: number, result: Color4): Color4 {
- result.r = this.r * scale;
- result.g = this.g * scale;
- result.b = this.b * scale;
- result.a = this.a * scale;
- return this;
- }
- public toString(): string {
- return "{R: " + this.r + " G:" + this.g + " B:" + this.b + " A:" + this.a + "}";
- }
- public clone(): Color4 {
- return new Color4(this.r, this.g, this.b, this.a);
- }
- public copyFrom(source: Color4): Color4 {
- this.r = source.r;
- this.g = source.g;
- this.b = source.b;
- this.a = source.a;
- return this;
- }
- // Statics
- public static Lerp(left: Color4, right: Color4, amount: number): Color4 {
- var result = new Color4(0, 0, 0, 0);
- Color4.LerpToRef(left, right, amount, result);
- return result;
- }
- public static LerpToRef(left: Color4, right: Color4, amount: number, result: Color4): void {
- result.r = left.r + (right.r - left.r) * amount;
- result.g = left.g + (right.g - left.g) * amount;
- result.b = left.b + (right.b - left.b) * amount;
- result.a = left.a + (right.a - left.a) * amount;
- }
- public static FromArray(array: number[], offset: number = 0): Color4 {
- return new Color4(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
- }
- public static FromInts(r: number, g: number, b: number, a: number): Color4 {
- return new Color4(r / 255.0, g / 255.0, b / 255.0, a / 255.0);
- }
- }
- export class Vector2 {
- constructor(public x: number, public y: number) {
- }
- public toString(): string {
- return "{X: " + this.x + " Y:" + this.y + "}";
- }
- // Operators
- public toArray(array: number[], index: number = 0): Vector2 {
- array[index] = this.x;
- array[index + 1] = this.y;
- return this;
- }
- public asArray(): number[] {
- var result = [];
- this.toArray(result, 0);
- return result;
- }
- public copyFrom(source: Vector2): Vector2 {
- this.x = source.x;
- this.y = source.y;
- return this;
- }
- public copyFromFloats(x: number, y: number): Vector2 {
- this.x = x;
- this.y = y;
- return this;
- }
- public add(otherVector: Vector2): Vector2 {
- return new Vector2(this.x + otherVector.x, this.y + otherVector.y);
- }
- public addVector3(otherVector: Vector3): Vector2 {
- return new Vector2(this.x + otherVector.x, this.y + otherVector.y);
- }
- public subtract(otherVector: Vector2): Vector2 {
- return new Vector2(this.x - otherVector.x, this.y - otherVector.y);
- }
- public subtractInPlace(otherVector: Vector2): Vector2 {
- this.x -= otherVector.x;
- this.y -= otherVector.y;
- return this;
- }
- public multiplyInPlace(otherVector: Vector2): Vector2 {
- this.x *= otherVector.x;
- this.y *= otherVector.y;
- return this;
- }
- public multiply(otherVector: Vector2): Vector2 {
- return new Vector2(this.x * otherVector.x, this.y * otherVector.y);
- }
- public multiplyToRef(otherVector: Vector2, result: Vector2): Vector2 {
- result.x = this.x * otherVector.x;
- result.y = this.y * otherVector.y;
- return this;
- }
- public multiplyByFloats(x: number, y: number): Vector2 {
- return new Vector2(this.x * x, this.y * y);
- }
- public divide(otherVector: Vector2): Vector2 {
- return new Vector2(this.x / otherVector.x, this.y / otherVector.y);
- }
- public divideToRef(otherVector: Vector2, result: Vector2): Vector2 {
- result.x = this.x / otherVector.x;
- result.y = this.y / otherVector.y;
- return this;
- }
- public negate(): Vector2 {
- return new Vector2(-this.x, -this.y);
- }
- public scaleInPlace(scale: number): Vector2 {
- this.x *= scale;
- this.y *= scale;
- return this;
- }
- public scale(scale: number): Vector2 {
- return new Vector2(this.x * scale, this.y * scale);
- }
- public equals(otherVector: Vector2): boolean {
- return otherVector && this.x === otherVector.x && this.y === otherVector.y;
- }
- // Properties
- public length(): number {
- return Math.sqrt(this.x * this.x + this.y * this.y);
- }
- public lengthSquared(): number {
- return (this.x * this.x + this.y * this.y);
- }
- // Methods
- public normalize(): Vector2 {
- var len = this.length();
- if (len === 0)
- return this;
- var num = 1.0 / len;
- this.x *= num;
- this.y *= num;
- return this;
- }
- public clone(): Vector2 {
- return new Vector2(this.x, this.y);
- }
- // Statics
- public static Zero(): Vector2 {
- return new Vector2(0, 0);
- }
- public static FromArray(array: number[], offset: number = 0): Vector2 {
- return new Vector2(array[offset], array[offset + 1]);
- }
- public static FromArrayToRef(array: number[], offset: number, result: Vector2): void {
- result.x = array[offset];
- result.y = array[offset + 1];
- }
- public static CatmullRom(value1: Vector2, value2: Vector2, value3: Vector2, value4: Vector2, amount: number): Vector2 {
- var squared = amount * amount;
- var cubed = amount * squared;
- var x = 0.5 * ((((2.0 * value2.x) + ((-value1.x + value3.x) * amount)) +
- (((((2.0 * value1.x) - (5.0 * value2.x)) + (4.0 * value3.x)) - value4.x) * squared)) +
- ((((-value1.x + (3.0 * value2.x)) - (3.0 * value3.x)) + value4.x) * cubed));
- var y = 0.5 * ((((2.0 * value2.y) + ((-value1.y + value3.y) * amount)) +
- (((((2.0 * value1.y) - (5.0 * value2.y)) + (4.0 * value3.y)) - value4.y) * squared)) +
- ((((-value1.y + (3.0 * value2.y)) - (3.0 * value3.y)) + value4.y) * cubed));
- return new Vector2(x, y);
- }
- public static Clamp(value: Vector2, min: Vector2, max: Vector2): Vector2 {
- var x = value.x;
- x = (x > max.x) ? max.x : x;
- x = (x < min.x) ? min.x : x;
- var y = value.y;
- y = (y > max.y) ? max.y : y;
- y = (y < min.y) ? min.y : y;
- return new Vector2(x, y);
- }
- public static Hermite(value1: Vector2, tangent1: Vector2, value2: Vector2, tangent2: Vector2, amount: number): Vector2 {
- var squared = amount * amount;
- var cubed = amount * squared;
- var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
- var part2 = (-2.0 * cubed) + (3.0 * squared);
- var part3 = (cubed - (2.0 * squared)) + amount;
- var part4 = cubed - squared;
- var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
- var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
- return new Vector2(x, y);
- }
- public static Lerp(start: Vector2, end: Vector2, amount: number): Vector2 {
- var x = start.x + ((end.x - start.x) * amount);
- var y = start.y + ((end.y - start.y) * amount);
- return new Vector2(x, y);
- }
- public static Dot(left: Vector2, right: Vector2): number {
- return left.x * right.x + left.y * right.y;
- }
- public static Normalize(vector: Vector2): Vector2 {
- var newVector = vector.clone();
- newVector.normalize();
- return newVector;
- }
- public static Minimize(left: Vector2, right: Vector2): Vector2 {
- var x = (left.x < right.x) ? left.x : right.x;
- var y = (left.y < right.y) ? left.y : right.y;
- return new Vector2(x, y);
- }
- public static Maximize(left: Vector2, right: Vector2): Vector2 {
- var x = (left.x > right.x) ? left.x : right.x;
- var y = (left.y > right.y) ? left.y : right.y;
- return new Vector2(x, y);
- }
- public static Transform(vector: Vector2, transformation: Matrix): Vector2 {
- var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]);
- var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]);
- return new Vector2(x, y);
- }
- public static Distance(value1: Vector2, value2: Vector2): number {
- return Math.sqrt(Vector2.DistanceSquared(value1, value2));
- }
- public static DistanceSquared(value1: Vector2, value2: Vector2): number {
- var x = value1.x - value2.x;
- var y = value1.y - value2.y;
- return (x * x) + (y * y);
- }
- }
- export class Vector3 {
- constructor(public x: number, public y: number, public z: number) {
- }
- public toString(): string {
- return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + "}";
- }
- // Operators
- public asArray(): number[] {
- var result = [];
- this.toArray(result, 0);
- return result;
- }
- public toArray(array: number[], index: number = 0): Vector3 {
- array[index] = this.x;
- array[index + 1] = this.y;
- array[index + 2] = this.z;
- return this;
- }
- public toQuaternion(): Quaternion {
- var result = new Quaternion(0, 0, 0, 1);
- var cosxPlusz = Math.cos((this.x + this.z) * 0.5);
- var sinxPlusz = Math.sin((this.x + this.z) * 0.5);
- var coszMinusx = Math.cos((this.z - this.x) * 0.5);
- var sinzMinusx = Math.sin((this.z - this.x) * 0.5);
- var cosy = Math.cos(this.y * 0.5);
- var siny = Math.sin(this.y * 0.5);
- result.x = coszMinusx * siny;
- result.y = -sinzMinusx * siny;
- result.z = sinxPlusz * cosy;
- result.w = cosxPlusz * cosy;
- return result;
- }
- public addInPlace(otherVector: Vector3): Vector3 {
- this.x += otherVector.x;
- this.y += otherVector.y;
- this.z += otherVector.z;
- return this;
- }
- public add(otherVector: Vector3): Vector3 {
- return new Vector3(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z);
- }
- public addToRef(otherVector: Vector3, result: Vector3): Vector3 {
- result.x = this.x + otherVector.x;
- result.y = this.y + otherVector.y;
- result.z = this.z + otherVector.z;
- return this;
- }
- public subtractInPlace(otherVector: Vector3): Vector3 {
- this.x -= otherVector.x;
- this.y -= otherVector.y;
- this.z -= otherVector.z;
- return this;
- }
- public subtract(otherVector: Vector3): Vector3 {
- return new Vector3(this.x - otherVector.x, this.y - otherVector.y, this.z - otherVector.z);
- }
- public subtractToRef(otherVector: Vector3, result: Vector3): Vector3 {
- result.x = this.x - otherVector.x;
- result.y = this.y - otherVector.y;
- result.z = this.z - otherVector.z;
- return this;
- }
- public subtractFromFloats(x: number, y: number, z: number): Vector3 {
- return new Vector3(this.x - x, this.y - y, this.z - z);
- }
- public subtractFromFloatsToRef(x: number, y: number, z: number, result: Vector3): Vector3 {
- result.x = this.x - x;
- result.y = this.y - y;
- result.z = this.z - z;
- return this;
- }
- public negate(): Vector3 {
- return new Vector3(-this.x, -this.y, -this.z);
- }
- public scaleInPlace(scale: number): Vector3 {
- this.x *= scale;
- this.y *= scale;
- this.z *= scale;
- return this;
- }
- public scale(scale: number): Vector3 {
- return new Vector3(this.x * scale, this.y * scale, this.z * scale);
- }
- public scaleToRef(scale: number, result: Vector3) {
- result.x = this.x * scale;
- result.y = this.y * scale;
- result.z = this.z * scale;
- }
- public equals(otherVector: Vector3): boolean {
- return otherVector && this.x === otherVector.x && this.y === otherVector.y && this.z === otherVector.z;
- }
- public equalsWithEpsilon(otherVector: Vector3): boolean {
- return Math.abs(this.x - otherVector.x) < Engine.Epsilon &&
- Math.abs(this.y - otherVector.y) < Engine.Epsilon &&
- Math.abs(this.z - otherVector.z) < Engine.Epsilon;
- }
- public equalsToFloats(x: number, y: number, z: number): boolean {
- return this.x === x && this.y === y && this.z === z;
- }
- public multiplyInPlace(otherVector: Vector3): Vector3 {
- this.x *= otherVector.x;
- this.y *= otherVector.y;
- this.z *= otherVector.z;
- return this;
- }
- public multiply(otherVector: Vector3): Vector3 {
- return new Vector3(this.x * otherVector.x, this.y * otherVector.y, this.z * otherVector.z);
- }
- public multiplyToRef(otherVector: Vector3, result: Vector3): Vector3 {
- result.x = this.x * otherVector.x;
- result.y = this.y * otherVector.y;
- result.z = this.z * otherVector.z;
- return this;
- }
- public multiplyByFloats(x: number, y: number, z: number): Vector3 {
- return new Vector3(this.x * x, this.y * y, this.z * z);
- }
- public divide(otherVector: Vector3): Vector3 {
- return new Vector3(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z);
- }
- public divideToRef(otherVector: Vector3, result: Vector3): Vector3 {
- result.x = this.x / otherVector.x;
- result.y = this.y / otherVector.y;
- result.z = this.z / otherVector.z;
- return this;
- }
- public MinimizeInPlace(other: Vector3): Vector3 {
- if (other.x < this.x) this.x = other.x;
- if (other.y < this.y) this.y = other.y;
- if (other.z < this.z) this.z = other.z;
- return this;
- }
- public MaximizeInPlace(other: Vector3): Vector3 {
- if (other.x > this.x) this.x = other.x;
- if (other.y > this.y) this.y = other.y;
- if (other.z > this.z) this.z = other.z;
- return this;
- }
- // Properties
- public length(): number {
- return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
- }
- public lengthSquared(): number {
- return (this.x * this.x + this.y * this.y + this.z * this.z);
- }
- // Methods
- public normalize(): Vector3 {
- var len = this.length();
- if (len === 0)
- return this;
- var num = 1.0 / len;
- this.x *= num;
- this.y *= num;
- this.z *= num;
- return this;
- }
- public clone(): Vector3 {
- return new Vector3(this.x, this.y, this.z);
- }
- public copyFrom(source: Vector3): Vector3 {
- this.x = source.x;
- this.y = source.y;
- this.z = source.z;
- return this;
- }
- public copyFromFloats(x: number, y: number, z: number): Vector3 {
- this.x = x;
- this.y = y;
- this.z = z;
- return this;
- }
- // Statics
- public static GetClipFactor(vector0: Vector3, vector1: Vector3, axis: Vector3, size) {
- var d0 = Vector3.Dot(vector0, axis) - size;
- var d1 = Vector3.Dot(vector1, axis) - size;
- var s = d0 / (d0 - d1);
- return s;
- }
- public static FromArray(array: number[], offset?: number): Vector3 {
- if (!offset) {
- offset = 0;
- }
- return new Vector3(array[offset], array[offset + 1], array[offset + 2]);
- }
- public static FromArrayToRef(array: number[], offset: number, result: Vector3): void {
- result.x = array[offset];
- result.y = array[offset + 1];
- result.z = array[offset + 2];
- }
- public static FromFloatArrayToRef(array: Float32Array, offset: number, result: Vector3): void {
- result.x = array[offset];
- result.y = array[offset + 1];
- result.z = array[offset + 2];
- }
- public static FromFloatsToRef(x: number, y: number, z: number, result: Vector3): void {
- result.x = x;
- result.y = y;
- result.z = z;
- }
- public static Zero(): Vector3 {
- return new Vector3(0, 0, 0);
- }
- public static Up(): Vector3 {
- return new Vector3(0, 1.0, 0);
- }
- public static TransformCoordinates(vector: Vector3, transformation: Matrix): Vector3 {
- var result = Vector3.Zero();
- Vector3.TransformCoordinatesToRef(vector, transformation, result);
- return result;
- }
- public static TransformCoordinatesToRef(vector: Vector3, transformation: Matrix, result: Vector3): void {
- var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + (vector.z * transformation.m[8]) + transformation.m[12];
- var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + (vector.z * transformation.m[9]) + transformation.m[13];
- var z = (vector.x * transformation.m[2]) + (vector.y * transformation.m[6]) + (vector.z * transformation.m[10]) + transformation.m[14];
- var w = (vector.x * transformation.m[3]) + (vector.y * transformation.m[7]) + (vector.z * transformation.m[11]) + transformation.m[15];
- result.x = x / w;
- result.y = y / w;
- result.z = z / w;
- }
- public static TransformCoordinatesFromFloatsToRef(x: number, y: number, z: number, transformation: Matrix, result: Vector3): void {
- var rx = (x * transformation.m[0]) + (y * transformation.m[4]) + (z * transformation.m[8]) + transformation.m[12];
- var ry = (x * transformation.m[1]) + (y * transformation.m[5]) + (z * transformation.m[9]) + transformation.m[13];
- var rz = (x * transformation.m[2]) + (y * transformation.m[6]) + (z * transformation.m[10]) + transformation.m[14];
- var rw = (x * transformation.m[3]) + (y * transformation.m[7]) + (z * transformation.m[11]) + transformation.m[15];
- result.x = rx / rw;
- result.y = ry / rw;
- result.z = rz / rw;
- }
- public static TransformCoordinatesToRefSIMD(vector: Vector3, transformation: Matrix, result: Vector3): void {
- var v = SIMD.float32x4.loadXYZ((<any>vector)._data, 0);
- var m0 = SIMD.float32x4.load(transformation.m, 0);
- var m1 = SIMD.float32x4.load(transformation.m, 4);
- var m2 = SIMD.float32x4.load(transformation.m, 8);
- var m3 = SIMD.float32x4.load(transformation.m, 12);
- var r = SIMD.float32x4.add(SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(v, 0, 0, 0, 0), m0), SIMD.float32x4.mul(SIMD.float32x4.swizzle(v, 1, 1, 1, 1), m1)), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(v, 2, 2, 2, 2), m2), m3));
- r = SIMD.float32x4.div(r, SIMD.float32x4.swizzle(r, 3, 3, 3, 3));
- SIMD.float32x4.storeXYZ((<any>result)._data, 0, r);
- }
- public static TransformCoordinatesFromFloatsToRefSIMD(x: number, y: number, z: number, transformation: Matrix, result: Vector3): void {
- var v0 = SIMD.float32x4.splat(x);
- var v1 = SIMD.float32x4.splat(y);
- var v2 = SIMD.float32x4.splat(z);
- var m0 = SIMD.float32x4.load(transformation.m, 0);
- var m1 = SIMD.float32x4.load(transformation.m, 4);
- var m2 = SIMD.float32x4.load(transformation.m, 8);
- var m3 = SIMD.float32x4.load(transformation.m, 12);
- var r = SIMD.float32x4.add(SIMD.float32x4.add(SIMD.float32x4.mul(v0, m0), SIMD.float32x4.mul(v1, m1)), SIMD.float32x4.add(SIMD.float32x4.mul(v2, m2), m3));
- r = SIMD.float32x4.div(r, SIMD.float32x4.swizzle(r, 3, 3, 3, 3));
- SIMD.float32x4.storeXYZ((<any>result)._data, 0, r);
- }
- public static TransformNormal(vector: Vector3, transformation: Matrix): Vector3 {
- var result = Vector3.Zero();
- Vector3.TransformNormalToRef(vector, transformation, result);
- return result;
- }
- public static TransformNormalToRef(vector: Vector3, transformation: Matrix, result: Vector3): void {
- result.x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + (vector.z * transformation.m[8]);
- result.y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + (vector.z * transformation.m[9]);
- result.z = (vector.x * transformation.m[2]) + (vector.y * transformation.m[6]) + (vector.z * transformation.m[10]);
- }
- public static TransformNormalFromFloatsToRef(x: number, y: number, z: number, transformation: Matrix, result: Vector3): void {
- result.x = (x * transformation.m[0]) + (y * transformation.m[4]) + (z * transformation.m[8]);
- result.y = (x * transformation.m[1]) + (y * transformation.m[5]) + (z * transformation.m[9]);
- result.z = (x * transformation.m[2]) + (y * transformation.m[6]) + (z * transformation.m[10]);
- }
- public static CatmullRom(value1: Vector3, value2: Vector3, value3: Vector3, value4: Vector3, amount: number): Vector3 {
- var squared = amount * amount;
- var cubed = amount * squared;
- var x = 0.5 * ((((2.0 * value2.x) + ((-value1.x + value3.x) * amount)) +
- (((((2.0 * value1.x) - (5.0 * value2.x)) + (4.0 * value3.x)) - value4.x) * squared)) +
- ((((-value1.x + (3.0 * value2.x)) - (3.0 * value3.x)) + value4.x) * cubed));
- var y = 0.5 * ((((2.0 * value2.y) + ((-value1.y + value3.y) * amount)) +
- (((((2.0 * value1.y) - (5.0 * value2.y)) + (4.0 * value3.y)) - value4.y) * squared)) +
- ((((-value1.y + (3.0 * value2.y)) - (3.0 * value3.y)) + value4.y) * cubed));
- var z = 0.5 * ((((2.0 * value2.z) + ((-value1.z + value3.z) * amount)) +
- (((((2.0 * value1.z) - (5.0 * value2.z)) + (4.0 * value3.z)) - value4.z) * squared)) +
- ((((-value1.z + (3.0 * value2.z)) - (3.0 * value3.z)) + value4.z) * cubed));
- return new Vector3(x, y, z);
- }
- public static Clamp(value: Vector3, min: Vector3, max: Vector3): Vector3 {
- var x = value.x;
- x = (x > max.x) ? max.x : x;
- x = (x < min.x) ? min.x : x;
- var y = value.y;
- y = (y > max.y) ? max.y : y;
- y = (y < min.y) ? min.y : y;
- var z = value.z;
- z = (z > max.z) ? max.z : z;
- z = (z < min.z) ? min.z : z;
- return new Vector3(x, y, z);
- }
- public static Hermite(value1: Vector3, tangent1: Vector3, value2: Vector3, tangent2: Vector3, amount: number): Vector3 {
- var squared = amount * amount;
- var cubed = amount * squared;
- var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
- var part2 = (-2.0 * cubed) + (3.0 * squared);
- var part3 = (cubed - (2.0 * squared)) + amount;
- var part4 = cubed - squared;
- var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
- var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
- var z = (((value1.z * part1) + (value2.z * part2)) + (tangent1.z * part3)) + (tangent2.z * part4);
- return new Vector3(x, y, z);
- }
- public static Lerp(start: Vector3, end: Vector3, amount: number): Vector3 {
- var x = start.x + ((end.x - start.x) * amount);
- var y = start.y + ((end.y - start.y) * amount);
- var z = start.z + ((end.z - start.z) * amount);
- return new Vector3(x, y, z);
- }
- public static Dot(left: Vector3, right: Vector3): number {
- return (left.x * right.x + left.y * right.y + left.z * right.z);
- }
- public static Cross(left: Vector3, right: Vector3): Vector3 {
- var result = Vector3.Zero();
- Vector3.CrossToRef(left, right, result);
- return result;
- }
- public static CrossToRef(left: Vector3, right: Vector3, result: Vector3): void {
- result.x = left.y * right.z - left.z * right.y;
- result.y = left.z * right.x - left.x * right.z;
- result.z = left.x * right.y - left.y * right.x;
- }
- public static Normalize(vector: Vector3): Vector3 {
- var result = Vector3.Zero();
- Vector3.NormalizeToRef(vector, result);
- return result;
- }
- public static NormalizeToRef(vector: Vector3, result: Vector3): void {
- result.copyFrom(vector);
- result.normalize();
- }
- public static Project(vector: Vector3, world: Matrix, transform: Matrix, viewport: Viewport): Vector3 {
- var cw = viewport.width;
- var ch = viewport.height;
- var cx = viewport.x;
- var cy = viewport.y;
- var viewportMatrix = Matrix.FromValues(
- cw / 2.0, 0, 0, 0,
- 0, -ch / 2.0, 0, 0,
- 0, 0, 1, 0,
- cx + cw / 2.0, ch / 2.0 + cy, 0, 1);
- var finalMatrix = world.multiply(transform).multiply(viewportMatrix);
- return Vector3.TransformCoordinates(vector, finalMatrix);
- }
- public static UnprojectFromTransform(source: Vector3, viewportWidth: number, viewportHeight: number, world: Matrix, transform: Matrix): Vector3 {
- var matrix = world.multiply(transform);
- matrix.invert();
- source.x = source.x / viewportWidth * 2 - 1;
- source.y = -(source.y / viewportHeight * 2 - 1);
- var vector = Vector3.TransformCoordinates(source, matrix);
- var num = source.x * matrix.m[3] + source.y * matrix.m[7] + source.z * matrix.m[11] + matrix.m[15];
- if (Tools.WithinEpsilon(num, 1.0)) {
- vector = vector.scale(1.0 / num);
- }
- return vector;
- }
- public static Unproject(source: Vector3, viewportWidth: number, viewportHeight: number, world: Matrix, view: Matrix, projection: Matrix): Vector3 {
- var matrix = world.multiply(view).multiply(projection);
- matrix.invert();
- source.x = source.x / viewportWidth * 2 - 1;
- source.y = -(source.y / viewportHeight * 2 - 1);
- var vector = Vector3.TransformCoordinates(source, matrix);
- var num = source.x * matrix.m[3] + source.y * matrix.m[7] + source.z * matrix.m[11] + matrix.m[15];
- if (Tools.WithinEpsilon(num, 1.0)) {
- vector = vector.scale(1.0 / num);
- }
- return vector;
- }
- public static Minimize(left: Vector3, right: Vector3): Vector3 {
- var min = left.clone();
- min.MinimizeInPlace(right);
- return min;
- }
- public static Maximize(left: Vector3, right: Vector3): Vector3 {
- var max = left.clone();
- max.MaximizeInPlace(right);
- return max;
- }
- public static Distance(value1: Vector3, value2: Vector3): number {
- return Math.sqrt(Vector3.DistanceSquared(value1, value2));
- }
- public static DistanceSquared(value1: Vector3, value2: Vector3): number {
- var x = value1.x - value2.x;
- var y = value1.y - value2.y;
- var z = value1.z - value2.z;
- return (x * x) + (y * y) + (z * z);
- }
- public static Center(value1: Vector3, value2: Vector3): Vector3 {
- var center = value1.add(value2);
- center.scaleInPlace(0.5);
- return center;
- }
- }
- //Vector4 class created for EulerAngle class conversion to Quaternion
- export class Vector4 {
- constructor(public x: number, public y: number, public z: number, public w: number) { }
- public toString(): string {
- return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + "W:" + this.w + "}";
- }
- // Operators
- public asArray(): number[] {
- var result = [];
- this.toArray(result, 0);
- return result;
- }
- public toArray(array: number[], index?: number): Vector4 {
- if (index === undefined) {
- index = 0;
- }
- array[index] = this.x;
- array[index + 1] = this.y;
- array[index + 2] = this.z;
- array[index + 3] = this.w;
- return this;
- }
- public addInPlace(otherVector: Vector4): Vector4 {
- this.x += otherVector.x;
- this.y += otherVector.y;
- this.z += otherVector.z;
- this.w += otherVector.w;
- return this;
- }
- public add(otherVector: Vector4): Vector4 {
- return new Vector4(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z, this.w + otherVector.w);
- }
- public addToRef(otherVector: Vector4, result: Vector4): Vector4 {
- result.x = this.x + otherVector.x;
- result.y = this.y + otherVector.y;
- result.z = this.z + otherVector.z;
- result.w = this.w + otherVector.w;
- return this;
- }
- public subtractInPlace(otherVector: Vector4): Vector4 {
- this.x -= otherVector.x;
- this.y -= otherVector.y;
- this.z -= otherVector.z;
- this.w -= otherVector.w;
- return this;
- }
- public subtract(otherVector: Vector4): Vector4 {
- return new Vector4(this.x - otherVector.x, this.y - otherVector.y, this.z - otherVector.z, this.w - otherVector.w);
- }
- public subtractToRef(otherVector: Vector4, result: Vector4): Vector4 {
- result.x = this.x - otherVector.x;
- result.y = this.y - otherVector.y;
- result.z = this.z - otherVector.z;
- result.w = this.w - otherVector.w;
- return this;
- }
- public subtractFromFloats(x: number, y: number, z: number, w: number): Vector4 {
- return new Vector4(this.x - x, this.y - y, this.z - z, this.w - w);
- }
- public subtractFromFloatsToRef(x: number, y: number, z: number, w: number, result: Vector4): Vector4 {
- result.x = this.x - x;
- result.y = this.y - y;
- result.z = this.z - z;
- result.w = this.w - w;
- return this;
- }
- public negate(): Vector4 {
- return new Vector4(-this.x, -this.y, -this.z, -this.w);
- }
- public scaleInPlace(scale: number): Vector4 {
- this.x *= scale;
- this.y *= scale;
- this.z *= scale;
- this.w *= scale;
- return this;
- }
- public scale(scale: number): Vector4 {
- return new Vector4(this.x * scale, this.y * scale, this.z * scale, this.w * scale);
- }
- public scaleToRef(scale: number, result: Vector4) {
- result.x = this.x * scale;
- result.y = this.y * scale;
- result.z = this.z * scale;
- result.w = this.w * scale;
- }
- public equals(otherVector: Vector4): boolean {
- return otherVector && this.x === otherVector.x && this.y === otherVector.y && this.z === otherVector.z && this.w === otherVector.w;
- }
- public equalsWithEpsilon(otherVector: Vector4): boolean {
- return Math.abs(this.x - otherVector.x) < Engine.Epsilon &&
- Math.abs(this.y - otherVector.y) < Engine.Epsilon &&
- Math.abs(this.z - otherVector.z) < Engine.Epsilon &&
- Math.abs(this.w - otherVector.w) < Engine.Epsilon;
- }
- public equalsToFloats(x: number, y: number, z: number, w: number): boolean {
- return this.x === x && this.y === y && this.z === z && this.w === w;
- }
- public multiplyInPlace(otherVector: Vector4): Vector4 {
- this.x *= otherVector.x;
- this.y *= otherVector.y;
- this.z *= otherVector.z;
- this.w *= otherVector.w;
- return this;
- }
- public multiply(otherVector: Vector4): Vector4 {
- return new Vector4(this.x * otherVector.x, this.y * otherVector.y, this.z * otherVector.z, this.w * otherVector.w);
- }
- public multiplyToRef(otherVector: Vector4, result: Vector4): Vector4 {
- result.x = this.x * otherVector.x;
- result.y = this.y * otherVector.y;
- result.z = this.z * otherVector.z;
- result.w = this.w * otherVector.w;
- return this;
- }
- public multiplyByFloats(x: number, y: number, z: number, w: number): Vector4 {
- return new Vector4(this.x * x, this.y * y, this.z * z, this.w * w);
- }
- public divide(otherVector: Vector4): Vector4 {
- return new Vector4(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z, this.w / otherVector.w);
- }
- public divideToRef(otherVector: Vector4, result: Vector4): Vector4 {
- result.x = this.x / otherVector.x;
- result.y = this.y / otherVector.y;
- result.z = this.z / otherVector.z;
- result.w = this.w / otherVector.w;
- return this;
- }
- public MinimizeInPlace(other: Vector4): Vector4 {
- if (other.x < this.x) this.x = other.x;
- if (other.y < this.y) this.y = other.y;
- if (other.z < this.z) this.z = other.z;
- if (other.w < this.w) this.w = other.w;
- return this;
- }
- public MaximizeInPlace(other: Vector4): Vector4 {
- if (other.x > this.x) this.x = other.x;
- if (other.y > this.y) this.y = other.y;
- if (other.z > this.z) this.z = other.z;
- if (other.w > this.w) this.w = other.w;
- return this;
- }
- // Properties
- public length(): number {
- return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
- }
- public lengthSquared(): number {
- return (this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
- }
- // Methods
- public normalize(): Vector4 {
- var len = this.length();
- if (len === 0)
- return this;
- var num = 1.0 / len;
- this.x *= num;
- this.y *= num;
- this.z *= num;
- this.w *= num;
- return this;
- }
- public clone(): Vector4 {
- return new Vector4(this.x, this.y, this.z, this.w);
- }
- public copyFrom(source: Vector4): Vector4 {
- this.x = source.x;
- this.y = source.y;
- this.z = source.z;
- this.w = source.w;
- return this;
- }
- public copyFromFloats(x: number, y: number, z: number, w: number): Vector4 {
- this.x = x;
- this.y = y;
- this.z = z;
- this.w = w;
- return this;
- }
- // Statics
- public static FromArray(array: number[], offset?: number): Vector4 {
- if (!offset) {
- offset = 0;
- }
- return new Vector4(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
- }
- public static FromArrayToRef(array: number[], offset: number, result: Vector4): void {
- result.x = array[offset];
- result.y = array[offset + 1];
- result.z = array[offset + 2];
- result.w = array[offset + 3];
- }
- public static FromFloatArrayToRef(array: Float32Array, offset: number, result: Vector4): void {
- result.x = array[offset];
- result.y = array[offset + 1];
- result.z = array[offset + 2];
- result.w = array[offset + 3];
- }
- public static FromFloatsToRef(x: number, y: number, z: number, w: number, result: Vector4): void {
- result.x = x;
- result.y = y;
- result.z = z;
- result.w = w;
- }
- public static Zero(): Vector4 {
- return new Vector4(0, 0, 0, 0);
- }
- public static Normalize(vector: Vector4): Vector4 {
- var result = Vector4.Zero();
- Vector4.NormalizeToRef(vector, result);
- return result;
- }
- public static NormalizeToRef(vector: Vector4, result: Vector4): void {
- result.copyFrom(vector);
- result.normalize();
- }
- public static Minimize(left: Vector4, right: Vector4): Vector4 {
- var min = left.clone();
- min.MinimizeInPlace(right);
- return min;
- }
- public static Maximize(left: Vector4, right: Vector4): Vector4 {
- var max = left.clone();
- max.MaximizeInPlace(right);
- return max;
- }
- public static Distance(value1: Vector4, value2: Vector4): number {
- return Math.sqrt(Vector4.DistanceSquared(value1, value2));
- }
- public static DistanceSquared(value1: Vector4, value2: Vector4): number {
- var x = value1.x - value2.x;
- var y = value1.y - value2.y;
- var z = value1.z - value2.z;
- var w = value1.w - value2.w;
- return (x * x) + (y * y) + (z * z) + (w * w);
- }
- public static Center(value1: Vector4, value2: Vector4): Vector4 {
- var center = value1.add(value2);
- center.scaleInPlace(0.5);
- return center;
- }
- }
- export class Quaternion {
- constructor(public x: number = 0, public y: number = 0, public z: number = 0, public w: number = 1) {
- }
- public toString(): string {
- return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + " W:" + this.w + "}";
- }
- public asArray(): number[] {
- return [this.x, this.y, this.z, this.w];
- }
- public equals(otherQuaternion: Quaternion): boolean {
- return otherQuaternion && this.x === otherQuaternion.x && this.y === otherQuaternion.y && this.z === otherQuaternion.z && this.w === otherQuaternion.w;
- }
- public clone(): Quaternion {
- return new Quaternion(this.x, this.y, this.z, this.w);
- }
- public copyFrom(other: Quaternion): Quaternion {
- this.x = other.x;
- this.y = other.y;
- this.z = other.z;
- this.w = other.w;
- return this;
- }
- public copyFromFloats(x: number, y: number, z: number, w: number): Quaternion {
- this.x = x;
- this.y = y;
- this.z = z;
- this.w = w;
- return this;
- }
- public add(other: Quaternion): Quaternion {
- return new Quaternion(this.x + other.x, this.y + other.y, this.z + other.z, this.w + other.w);
- }
- public subtract(other: Quaternion): Quaternion {
- return new Quaternion(this.x - other.x, this.y - other.y, this.z - other.z, this.w - other.w);
- }
- public scale(value: number): Quaternion {
- return new Quaternion(this.x * value, this.y * value, this.z * value, this.w * value);
- }
- public multiply(q1: Quaternion): Quaternion {
- var result = new Quaternion(0, 0, 0, 1.0);
- this.multiplyToRef(q1, result);
- return result;
- }
- public multiplyToRef(q1: Quaternion, result: Quaternion): Quaternion {
- result.x = this.x * q1.w + this.y * q1.z - this.z * q1.y + this.w * q1.x;
- result.y = -this.x * q1.z + this.y * q1.w + this.z * q1.x + this.w * q1.y;
- result.z = this.x * q1.y - this.y * q1.x + this.z * q1.w + this.w * q1.z;
- result.w = -this.x * q1.x - this.y * q1.y - this.z * q1.z + this.w * q1.w;
- return this;
- }
- public length(): number {
- return Math.sqrt((this.x * this.x) + (this.y * this.y) + (this.z * this.z) + (this.w * this.w));
- }
- public normalize(): Quaternion {
- var length = 1.0 / this.length();
- this.x *= length;
- this.y *= length;
- this.z *= length;
- this.w *= length;
- return this;
- }
- public toEulerAngles(): Vector3 {
- var result = Vector3.Zero();
- this.toEulerAnglesToRef(result);
- return result;
- }
- public toEulerAnglesToRef(result: Vector3): Quaternion {
- //result is an EulerAngles in the in the z-x-z convention
- var qx = this.x;
- var qy = this.y;
- var qz = this.z;
- var qw = this.w;
- var qxy = qx * qy;
- var qxz = qx * qz;
- var qwy = qw * qy;
- var qwz = qw * qz;
- var qwx = qw * qx;
- var qyz = qy * qz;
- var sqx = qx * qx;
- var sqy = qy * qy;
- var determinant = sqx + sqy;
- if (determinant !== 0.000 && determinant !== 1.000) {
- result.x = Math.atan2(qxz + qwy, qwx - qyz);
- result.y = Math.acos(1 - 2 * determinant);
- result.z = Math.atan2(qxz - qwy, qwx + qyz);
- } else {
- if (determinant === 0.0) {
- result.x = 0.0;
- result.y = 0.0;
- result.z = Math.atan2(qxy - qwz, 0.5 - sqy - qz * qz); //actually, degeneracy gives us choice with x+z=Math.atan2(qxy-qwz,0.5-sqy-qz*qz)
- } else //determinant == 1.000
- {
- result.x = Math.atan2(qxy - qwz, 0.5 - sqy - qz * qz); //actually, degeneracy gives us choice with x-z=Math.atan2(qxy-qwz,0.5-sqy-qz*qz)
- result.y = Math.PI;
- result.z = 0.0;
- }
- }
- return this;
- }
- public toRotationMatrix(result: Matrix): Quaternion {
- var xx = this.x * this.x;
- var yy = this.y * this.y;
- var zz = this.z * this.z;
- var xy = this.x * this.y;
- var zw = this.z * this.w;
- var zx = this.z * this.x;
- var yw = this.y * this.w;
- var yz = this.y * this.z;
- var xw = this.x * this.w;
- result.m[0] = 1.0 - (2.0 * (yy + zz));
- result.m[1] = 2.0 * (xy + zw);
- result.m[2] = 2.0 * (zx - yw);
- result.m[3] = 0;
- result.m[4] = 2.0 * (xy - zw);
- result.m[5] = 1.0 - (2.0 * (zz + xx));
- result.m[6] = 2.0 * (yz + xw);
- result.m[7] = 0;
- result.m[8] = 2.0 * (zx + yw);
- result.m[9] = 2.0 * (yz - xw);
- result.m[10] = 1.0 - (2.0 * (yy + xx));
- result.m[11] = 0;
- result.m[12] = 0;
- result.m[13] = 0;
- result.m[14] = 0;
- result.m[15] = 1.0;
- return this;
- }
- public fromRotationMatrix(matrix: Matrix): Quaternion {
- Quaternion.FromRotationMatrixToRef(matrix, this);
- return this;
- }
- // Statics
- public static FromRotationMatrix(matrix: Matrix): Quaternion {
- var result = new Quaternion();
- Quaternion.FromRotationMatrixToRef(matrix, result);
- return result;
- }
- public static FromRotationMatrixToRef(matrix: Matrix, result: Quaternion): void {
- var data = matrix.m;
- var m11 = data[0], m12 = data[4], m13 = data[8];
- var m21 = data[1], m22 = data[5], m23 = data[9];
- var m31 = data[2], m32 = data[6], m33 = data[10];
- var trace = m11 + m22 + m33;
- var s;
- if (trace > 0) {
- s = 0.5 / Math.sqrt(trace + 1.0);
- result.w = 0.25 / s;
- result.x = (m32 - m23) * s;
- result.y = (m13 - m31) * s;
- result.z = (m21 - m12) * s;
- } else if (m11 > m22 && m11 > m33) {
- s = 2.0 * Math.sqrt(1.0 + m11 - m22 - m33);
- result.w = (m32 - m23) / s;
- result.x = 0.25 * s;
- result.y = (m12 + m21) / s;
- result.z = (m13 + m31) / s;
- } else if (m22 > m33) {
- s = 2.0 * Math.sqrt(1.0 + m22 - m11 - m33);
- result.w = (m13 - m31) / s;
- result.x = (m12 + m21) / s;
- result.y = 0.25 * s;
- result.z = (m23 + m32) / s;
- } else {
- s = 2.0 * Math.sqrt(1.0 + m33 - m11 - m22);
- result.w = (m21 - m12) / s;
- result.x = (m13 + m31) / s;
- result.y = (m23 + m32) / s;
- result.z = 0.25 * s;
- }
- }
- public static Inverse(q: Quaternion): Quaternion {
- return new Quaternion(-q.x, -q.y, -q.z, q.w);
- }
- public static Identity(): Quaternion {
- return new Quaternion(0, 0, 0, 1);
- }
- public static RotationAxis(axis: Vector3, angle: number): Quaternion {
- var result = new Quaternion();
- var sin = Math.sin(angle / 2);
- result.w = Math.cos(angle / 2);
- result.x = axis.x * sin;
- result.y = axis.y * sin;
- result.z = axis.z * sin;
- return result;
- }
- public static FromArray(array: number[], offset?: number): Quaternion {
- if (!offset) {
- offset = 0;
- }
- return new Quaternion(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
- }
- public static RotationYawPitchRoll(yaw: number, pitch: number, roll: number): Quaternion {
- var result = new Quaternion();
- Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, result);
- return result;
- }
- public static RotationYawPitchRollToRef(yaw: number, pitch: number, roll: number, result: Quaternion): void {
- // Produces a quaternion from Euler angles in the z-y-x orientation (Tait-Bryan angles)
- var halfRoll = roll * 0.5;
- var halfPitch = pitch * 0.5;
- var halfYaw = yaw * 0.5;
- var sinRoll = Math.sin(halfRoll);
- var cosRoll = Math.cos(halfRoll);
- var sinPitch = Math.sin(halfPitch);
- var cosPitch = Math.cos(halfPitch);
- var sinYaw = Math.sin(halfYaw);
- var cosYaw = Math.cos(halfYaw);
- result.x = (cosYaw * sinPitch * cosRoll) + (sinYaw * cosPitch * sinRoll);
- result.y = (sinYaw * cosPitch * cosRoll) - (cosYaw * sinPitch * sinRoll);
- result.z = (cosYaw * cosPitch * sinRoll) - (sinYaw * sinPitch * cosRoll);
- result.w = (cosYaw * cosPitch * cosRoll) + (sinYaw * sinPitch * sinRoll);
- }
- public static RotationAlphaBetaGamma(alpha: number, beta: number, gamma: number): Quaternion {
- var result = new Quaternion();
- Quaternion.RotationAlphaBetaGammaToRef(alpha, beta, gamma, result);
- return result;
- }
- public static RotationAlphaBetaGammaToRef(alpha: number, beta: number, gamma: number, result: Quaternion): void {
- // Produces a quaternion from Euler angles in the z-x-z orientation
- var halfGammaPlusAlpha = (gamma + alpha) * 0.5;
- var halfGammaMinusAlpha = (gamma - alpha) * 0.5;
- var halfBeta = beta * 0.5;
- result.x = Math.cos(halfGammaMinusAlpha) * Math.sin(halfBeta);
- result.y = Math.sin(halfGammaMinusAlpha) * Math.sin(halfBeta);
- result.z = Math.sin(halfGammaPlusAlpha) * Math.cos(halfBeta);
- result.w = Math.cos(halfGammaPlusAlpha) * Math.cos(halfBeta);
- }
- public static Slerp(left: Quaternion, right: Quaternion, amount: number): Quaternion {
- var num2;
- var num3;
- var num = amount;
- var num4 = (((left.x * right.x) + (left.y * right.y)) + (left.z * right.z)) + (left.w * right.w);
- var flag = false;
- if (num4 < 0) {
- flag = true;
- num4 = -num4;
- }
- if (num4 > 0.999999) {
- num3 = 1 - num;
- num2 = flag ? -num : num;
- }
- else {
- var num5 = Math.acos(num4);
- var num6 = (1.0 / Math.sin(num5));
- num3 = (Math.sin((1.0 - num) * num5)) * num6;
- num2 = flag ? ((-Math.sin(num * num5)) * num6) : ((Math.sin(num * num5)) * num6);
- }
- return new Quaternion((num3 * left.x) + (num2 * right.x),(num3 * left.y) + (num2 * right.y),(num3 * left.z) + (num2 * right.z),(num3 * left.w) + (num2 * right.w));
- }
- }
- export class Matrix {
- private static _tempQuaternion: Quaternion = new Quaternion();
- private static _xAxis: Vector3 = Vector3.Zero();
- private static _yAxis: Vector3 = Vector3.Zero();
- private static _zAxis: Vector3 = Vector3.Zero();
- public m: Float32Array = new Float32Array(16);
- // Properties
- public isIdentity(): boolean {
- if (this.m[0] !== 1.0 || this.m[5] !== 1.0 || this.m[10] !== 1.0 || this.m[15] !== 1.0)
- return false;
- if (this.m[1] !== 0.0 || this.m[2] !== 0.0 || this.m[3] !== 0.0 ||
- this.m[4] !== 0.0 || this.m[6] !== 0.0 || this.m[7] !== 0.0 ||
- this.m[8] !== 0.0 || this.m[9] !== 0.0 || this.m[11] !== 0.0 ||
- this.m[12] !== 0.0 || this.m[13] !== 0.0 || this.m[14] !== 0.0)
- return false;
- return true;
- }
- public determinant(): number {
- var temp1 = (this.m[10] * this.m[15]) - (this.m[11] * this.m[14]);
- var temp2 = (this.m[9] * this.m[15]) - (this.m[11] * this.m[13]);
- var temp3 = (this.m[9] * this.m[14]) - (this.m[10] * this.m[13]);
- var temp4 = (this.m[8] * this.m[15]) - (this.m[11] * this.m[12]);
- var temp5 = (this.m[8] * this.m[14]) - (this.m[10] * this.m[12]);
- var temp6 = (this.m[8] * this.m[13]) - (this.m[9] * this.m[12]);
- return ((((this.m[0] * (((this.m[5] * temp1) - (this.m[6] * temp2)) + (this.m[7] * temp3))) - (this.m[1] * (((this.m[4] * temp1) -
- (this.m[6] * temp4)) + (this.m[7] * temp5)))) + (this.m[2] * (((this.m[4] * temp2) - (this.m[5] * temp4)) + (this.m[7] * temp6)))) -
- (this.m[3] * (((this.m[4] * temp3) - (this.m[5] * temp5)) + (this.m[6] * temp6))));
- }
- // Methods
- public toArray(): Float32Array {
- return this.m;
- }
- public asArray(): Float32Array {
- return this.toArray();
- }
- public invert(): Matrix {
- this.invertToRef(this);
- return this;
- }
- public invertToRef(other: Matrix): Matrix {
- var l1 = this.m[0];
- var l2 = this.m[1];
- var l3 = this.m[2];
- var l4 = this.m[3];
- var l5 = this.m[4];
- var l6 = this.m[5];
- var l7 = this.m[6];
- var l8 = this.m[7];
- var l9 = this.m[8];
- var l10 = this.m[9];
- var l11 = this.m[10];
- var l12 = this.m[11];
- var l13 = this.m[12];
- var l14 = this.m[13];
- var l15 = this.m[14];
- var l16 = this.m[15];
- var l17 = (l11 * l16) - (l12 * l15);
- var l18 = (l10 * l16) - (l12 * l14);
- var l19 = (l10 * l15) - (l11 * l14);
- var l20 = (l9 * l16) - (l12 * l13);
- var l21 = (l9 * l15) - (l11 * l13);
- var l22 = (l9 * l14) - (l10 * l13);
- var l23 = ((l6 * l17) - (l7 * l18)) + (l8 * l19);
- var l24 = -(((l5 * l17) - (l7 * l20)) + (l8 * l21));
- var l25 = ((l5 * l18) - (l6 * l20)) + (l8 * l22);
- var l26 = -(((l5 * l19) - (l6 * l21)) + (l7 * l22));
- var l27 = 1.0 / ((((l1 * l23) + (l2 * l24)) + (l3 * l25)) + (l4 * l26));
- var l28 = (l7 * l16) - (l8 * l15);
- var l29 = (l6 * l16) - (l8 * l14);
- var l30 = (l6 * l15) - (l7 * l14);
- var l31 = (l5 * l16) - (l8 * l13);
- var l32 = (l5 * l15) - (l7 * l13);
- var l33 = (l5 * l14) - (l6 * l13);
- var l34 = (l7 * l12) - (l8 * l11);
- var l35 = (l6 * l12) - (l8 * l10);
- var l36 = (l6 * l11) - (l7 * l10);
- var l37 = (l5 * l12) - (l8 * l9);
- var l38 = (l5 * l11) - (l7 * l9);
- var l39 = (l5 * l10) - (l6 * l9);
- other.m[0] = l23 * l27;
- other.m[4] = l24 * l27;
- other.m[8] = l25 * l27;
- other.m[12] = l26 * l27;
- other.m[1] = -(((l2 * l17) - (l3 * l18)) + (l4 * l19)) * l27;
- other.m[5] = (((l1 * l17) - (l3 * l20)) + (l4 * l21)) * l27;
- other.m[9] = -(((l1 * l18) - (l2 * l20)) + (l4 * l22)) * l27;
- other.m[13] = (((l1 * l19) - (l2 * l21)) + (l3 * l22)) * l27;
- other.m[2] = (((l2 * l28) - (l3 * l29)) + (l4 * l30)) * l27;
- other.m[6] = -(((l1 * l28) - (l3 * l31)) + (l4 * l32)) * l27;
- other.m[10] = (((l1 * l29) - (l2 * l31)) + (l4 * l33)) * l27;
- other.m[14] = -(((l1 * l30) - (l2 * l32)) + (l3 * l33)) * l27;
- other.m[3] = -(((l2 * l34) - (l3 * l35)) + (l4 * l36)) * l27;
- other.m[7] = (((l1 * l34) - (l3 * l37)) + (l4 * l38)) * l27;
- other.m[11] = -(((l1 * l35) - (l2 * l37)) + (l4 * l39)) * l27;
- other.m[15] = (((l1 * l36) - (l2 * l38)) + (l3 * l39)) * l27;
- return this;
- }
- public invertToRefSIMD(other: Matrix): Matrix {
- var src = this.m;
- var dest = other.m;
- var row0, row1, row2, row3;
- var tmp1;
- var minor0, minor1, minor2, minor3;
- var det;
- // Load the 4 rows
- var src0 = SIMD.float32x4.load(src, 0);
- var src1 = SIMD.float32x4.load(src, 4);
- var src2 = SIMD.float32x4.load(src, 8);
- var src3 = SIMD.float32x4.load(src, 12);
- // Transpose the source matrix. Sort of. Not a true transpose operation
- tmp1 = SIMD.float32x4.shuffle(src0, src1, 0, 1, 4, 5);
- row1 = SIMD.float32x4.shuffle(src2, src3, 0, 1, 4, 5);
- row0 = SIMD.float32x4.shuffle(tmp1, row1, 0, 2, 4, 6);
- row1 = SIMD.float32x4.shuffle(row1, tmp1, 1, 3, 5, 7);
- tmp1 = SIMD.float32x4.shuffle(src0, src1, 2, 3, 6, 7);
- row3 = SIMD.float32x4.shuffle(src2, src3, 2, 3, 6, 7);
- row2 = SIMD.float32x4.shuffle(tmp1, row3, 0, 2, 4, 6);
- row3 = SIMD.float32x4.shuffle(row3, tmp1, 1, 3, 5, 7);
- // This is a true transposition, but it will lead to an incorrect result
- //tmp1 = SIMD.float32x4.shuffle(src0, src1, 0, 1, 4, 5);
- //tmp2 = SIMD.float32x4.shuffle(src2, src3, 0, 1, 4, 5);
- //row0 = SIMD.float32x4.shuffle(tmp1, tmp2, 0, 2, 4, 6);
- //row1 = SIMD.float32x4.shuffle(tmp1, tmp2, 1, 3, 5, 7);
- //tmp1 = SIMD.float32x4.shuffle(src0, src1, 2, 3, 6, 7);
- //tmp2 = SIMD.float32x4.shuffle(src2, src3, 2, 3, 6, 7);
- //row2 = SIMD.float32x4.shuffle(tmp1, tmp2, 0, 2, 4, 6);
- //row3 = SIMD.float32x4.shuffle(tmp1, tmp2, 1, 3, 5, 7);
- // ----
- tmp1 = SIMD.float32x4.mul(row2, row3);
- tmp1 = SIMD.float32x4.swizzle(tmp1, 1, 0, 3, 2); // 0xB1 = 10110001
- minor0 = SIMD.float32x4.mul(row1, tmp1);
- minor1 = SIMD.float32x4.mul(row0, tmp1);
- tmp1 = SIMD.float32x4.swizzle(tmp1, 2, 3, 0, 1); // 0x4E = 01001110
- minor0 = SIMD.float32x4.sub(SIMD.float32x4.mul(row1, tmp1), minor0);
- minor1 = SIMD.float32x4.sub(SIMD.float32x4.mul(row0, tmp1), minor1);
- minor1 = SIMD.float32x4.swizzle(minor1, 2, 3, 0, 1); // 0x4E = 01001110
- // ----
- tmp1 = SIMD.float32x4.mul(row1, row2);
- tmp1 = SIMD.float32x4.swizzle(tmp1, 1, 0, 3, 2); // 0xB1 = 10110001
- minor0 = SIMD.float32x4.add(SIMD.float32x4.mul(row3, tmp1), minor0);
- minor3 = SIMD.float32x4.mul(row0, tmp1);
- tmp1 = SIMD.float32x4.swizzle(tmp1, 2, 3, 0, 1); // 0x4E = 01001110
- minor0 = SIMD.float32x4.sub(minor0, SIMD.float32x4.mul(row3, tmp1));
- minor3 = SIMD.float32x4.sub(SIMD.float32x4.mul(row0, tmp1), minor3);
- minor3 = SIMD.float32x4.swizzle(minor3, 2, 3, 0, 1); // 0x4E = 01001110
- // ----
- tmp1 = SIMD.float32x4.mul(SIMD.float32x4.swizzle(row1, 2, 3, 0, 1), row3); // 0x4E = 01001110
- tmp1 = SIMD.float32x4.swizzle(tmp1, 1, 0, 3, 2); // 0xB1 = 10110001
- row2 = SIMD.float32x4.swizzle(row2, 2, 3, 0, 1); // 0x4E = 01001110
- minor0 = SIMD.float32x4.add(SIMD.float32x4.mul(row2, tmp1), minor0);
- minor2 = SIMD.float32x4.mul(row0, tmp1);
- tmp1 = SIMD.float32x4.swizzle(tmp1, 2, 3, 0, 1); // 0x4E = 01001110
- minor0 = SIMD.float32x4.sub(minor0, SIMD.float32x4.mul(row2, tmp1));
- minor2 = SIMD.float32x4.sub(SIMD.float32x4.mul(row0, tmp1), minor2);
- minor2 = SIMD.float32x4.swizzle(minor2, 2, 3, 0, 1); // 0x4E = 01001110
- // ----
- tmp1 = SIMD.float32x4.mul(row0, row1);
- tmp1 = SIMD.float32x4.swizzle(tmp1, 1, 0, 3, 2); // 0xB1 = 10110001
- minor2 = SIMD.float32x4.add(SIMD.float32x4.mul(row3, tmp1), minor2);
- minor3 = SIMD.float32x4.sub(SIMD.float32x4.mul(row2, tmp1), minor3);
- tmp1 = SIMD.float32x4.swizzle(tmp1, 2, 3, 0, 1); // 0x4E = 01001110
- minor2 = SIMD.float32x4.sub(SIMD.float32x4.mul(row3, tmp1), minor2);
- minor3 = SIMD.float32x4.sub(minor3, SIMD.float32x4.mul(row2, tmp1));
- // ----
- tmp1 = SIMD.float32x4.mul(row0, row3);
- tmp1 = SIMD.float32x4.swizzle(tmp1, 1, 0, 3, 2); // 0xB1 = 10110001
- minor1 = SIMD.float32x4.sub(minor1, SIMD.float32x4.mul(row2, tmp1));
- minor2 = SIMD.float32x4.add(SIMD.float32x4.mul(row1, tmp1), minor2);
- tmp1 = SIMD.float32x4.swizzle(tmp1, 2, 3, 0, 1); // 0x4E = 01001110
- minor1 = SIMD.float32x4.add(SIMD.float32x4.mul(row2, tmp1), minor1);
- minor2 = SIMD.float32x4.sub(minor2, SIMD.float32x4.mul(row1, tmp1));
- // ----
- tmp1 = SIMD.float32x4.mul(row0, row2);
- tmp1 = SIMD.float32x4.swizzle(tmp1, 1, 0, 3, 2); // 0xB1 = 10110001
- minor1 = SIMD.float32x4.add(SIMD.float32x4.mul(row3, tmp1), minor1);
- minor3 = SIMD.float32x4.sub(minor3, SIMD.float32x4.mul(row1, tmp1));
- tmp1 = SIMD.float32x4.swizzle(tmp1, 2, 3, 0, 1); // 0x4E = 01001110
- minor1 = SIMD.float32x4.sub(minor1, SIMD.float32x4.mul(row3, tmp1));
- minor3 = SIMD.float32x4.add(SIMD.float32x4.mul(row1, tmp1), minor3);
- // Compute determinant
- det = SIMD.float32x4.mul(row0, minor0);
- det = SIMD.float32x4.add(SIMD.float32x4.swizzle(det, 2, 3, 0, 1), det); // 0x4E = 01001110
- det = SIMD.float32x4.add(SIMD.float32x4.swizzle(det, 1, 0, 3, 2), det); // 0xB1 = 10110001
- tmp1 = SIMD.float32x4.reciprocal(det);
- det = SIMD.float32x4.sub(SIMD.float32x4.add(tmp1, tmp1), SIMD.float32x4.mul(det, SIMD.float32x4.mul(tmp1, tmp1)));
- det = SIMD.float32x4.swizzle(det, 0, 0, 0, 0);
- // These shuffles aren't necessary if the faulty transposition is done
- // up at the top of this function.
- //minor0 = SIMD.float32x4.swizzle(minor0, 2, 1, 0, 3);
- //minor1 = SIMD.float32x4.swizzle(minor1, 2, 1, 0, 3);
- //minor2 = SIMD.float32x4.swizzle(minor2, 2, 1, 0, 3);
- //minor3 = SIMD.float32x4.swizzle(minor3, 2, 1, 0, 3);
- // Compute final values by multiplying with 1/det
- minor0 = SIMD.float32x4.mul(det, minor0);
- minor1 = SIMD.float32x4.mul(det, minor1);
- minor2 = SIMD.float32x4.mul(det, minor2);
- minor3 = SIMD.float32x4.mul(det, minor3);
- SIMD.float32x4.store(dest, 0, minor0);
- SIMD.float32x4.store(dest, 4, minor1);
- SIMD.float32x4.store(dest, 8, minor2);
- SIMD.float32x4.store(dest, 12, minor3);
- return this;
- }
- public setTranslation(vector3: Vector3): Matrix {
- this.m[12] = vector3.x;
- this.m[13] = vector3.y;
- this.m[14] = vector3.z;
- return this;
- }
- public multiply(other: Matrix): Matrix {
- var result = new Matrix();
- this.multiplyToRef(other, result);
- return result;
- }
- public copyFrom(other: Matrix): Matrix {
- for (var index = 0; index < 16; index++) {
- this.m[index] = other.m[index];
- }
- return this;
- }
- public copyToArray(array: Float32Array, offset: number = 0): Matrix {
- for (var index = 0; index < 16; index++) {
- array[offset + index] = this.m[index];
- }
- return this;
- }
- public multiplyToRef(other: Matrix, result: Matrix): Matrix {
- this.multiplyToArray(other, result.m, 0);
- return this;
- }
- public multiplyToArray(other: Matrix, result: Float32Array, offset: number): Matrix {
- var tm0 = this.m[0];
- var tm1 = this.m[1];
- var tm2 = this.m[2];
- var tm3 = this.m[3];
- var tm4 = this.m[4];
- var tm5 = this.m[5];
- var tm6 = this.m[6];
- var tm7 = this.m[7];
- var tm8 = this.m[8];
- var tm9 = this.m[9];
- var tm10 = this.m[10];
- var tm11 = this.m[11];
- var tm12 = this.m[12];
- var tm13 = this.m[13];
- var tm14 = this.m[14];
- var tm15 = this.m[15];
- var om0 = other.m[0];
- var om1 = other.m[1];
- var om2 = other.m[2];
- var om3 = other.m[3];
- var om4 = other.m[4];
- var om5 = other.m[5];
- var om6 = other.m[6];
- var om7 = other.m[7];
- var om8 = other.m[8];
- var om9 = other.m[9];
- var om10 = other.m[10];
- var om11 = other.m[11];
- var om12 = other.m[12];
- var om13 = other.m[13];
- var om14 = other.m[14];
- var om15 = other.m[15];
- result[offset] = tm0 * om0 + tm1 * om4 + tm2 * om8 + tm3 * om12;
- result[offset + 1] = tm0 * om1 + tm1 * om5 + tm2 * om9 + tm3 * om13;
- result[offset + 2] = tm0 * om2 + tm1 * om6 + tm2 * om10 + tm3 * om14;
- result[offset + 3] = tm0 * om3 + tm1 * om7 + tm2 * om11 + tm3 * om15;
- result[offset + 4] = tm4 * om0 + tm5 * om4 + tm6 * om8 + tm7 * om12;
- result[offset + 5] = tm4 * om1 + tm5 * om5 + tm6 * om9 + tm7 * om13;
- result[offset + 6] = tm4 * om2 + tm5 * om6 + tm6 * om10 + tm7 * om14;
- result[offset + 7] = tm4 * om3 + tm5 * om7 + tm6 * om11 + tm7 * om15;
- result[offset + 8] = tm8 * om0 + tm9 * om4 + tm10 * om8 + tm11 * om12;
- result[offset + 9] = tm8 * om1 + tm9 * om5 + tm10 * om9 + tm11 * om13;
- result[offset + 10] = tm8 * om2 + tm9 * om6 + tm10 * om10 + tm11 * om14;
- result[offset + 11] = tm8 * om3 + tm9 * om7 + tm10 * om11 + tm11 * om15;
- result[offset + 12] = tm12 * om0 + tm13 * om4 + tm14 * om8 + tm15 * om12;
- result[offset + 13] = tm12 * om1 + tm13 * om5 + tm14 * om9 + tm15 * om13;
- result[offset + 14] = tm12 * om2 + tm13 * om6 + tm14 * om10 + tm15 * om14;
- result[offset + 15] = tm12 * om3 + tm13 * om7 + tm14 * om11 + tm15 * om15;
- return this;
- }
- public multiplyToArraySIMD(other: Matrix, result: Matrix, offset = 0): void {
- var tm = this.m;
- var om = other.m;
- var om0 = SIMD.float32x4.load(om, 0);
- var om1 = SIMD.float32x4.load(om, 4);
- var om2 = SIMD.float32x4.load(om, 8);
- var om3 = SIMD.float32x4.load(om, 12);
- var tm0 = SIMD.float32x4.load(tm, 0);
- SIMD.float32x4.store(result, offset + 0, SIMD.float32x4.add(
- SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm0, 0, 0, 0, 0), om0),
- SIMD.float32x4.add(
- SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm0, 1, 1, 1, 1), om1),
- SIMD.float32x4.add(
- SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm0, 2, 2, 2, 2), om2),
- SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm0, 3, 3, 3, 3), om3)))));
- var tm1 = SIMD.float32x4.load(tm, 4);
- SIMD.float32x4.store(result, offset + 4, SIMD.float32x4.add(
- SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm1, 0, 0, 0, 0), om0),
- SIMD.float32x4.add(
- SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm1, 1, 1, 1, 1), om1),
- SIMD.float32x4.add(
- SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm1, 2, 2, 2, 2), om2),
- SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm1, 3, 3, 3, 3), om3)))));
- var tm2 = SIMD.float32x4.load(tm, 8);
- SIMD.float32x4.store(result, offset + 8, SIMD.float32x4.add(
- SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm2, 0, 0, 0, 0), om0),
- SIMD.float32x4.add(
- SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm2, 1, 1, 1, 1), om1),
- SIMD.float32x4.add(
- SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm2, 2, 2, 2, 2), om2),
- SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm2, 3, 3, 3, 3), om3)))));
- var tm3 = SIMD.float32x4.load(tm, 12);
- SIMD.float32x4.store(result, offset + 12, SIMD.float32x4.add(
- SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm3, 0, 0, 0, 0), om0),
- SIMD.float32x4.add(
- SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm3, 1, 1, 1, 1), om1),
- SIMD.float32x4.add(
- SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm3, 2, 2, 2, 2), om2),
- SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm3, 3, 3, 3, 3), om3)))));
- }
- public equals(value: Matrix): boolean {
- return value &&
- (this.m[0] === value.m[0] && this.m[1] === value.m[1] && this.m[2] === value.m[2] && this.m[3] === value.m[3] &&
- this.m[4] === value.m[4] && this.m[5] === value.m[5] && this.m[6] === value.m[6] && this.m[7] === value.m[7] &&
- this.m[8] === value.m[8] && this.m[9] === value.m[9] && this.m[10] === value.m[10] && this.m[11] === value.m[11] &&
- this.m[12] === value.m[12] && this.m[13] === value.m[13] && this.m[14] === value.m[14] && this.m[15] === value.m[15]);
- }
- public clone(): Matrix {
- return Matrix.FromValues(this.m[0], this.m[1], this.m[2], this.m[3],
- this.m[4], this.m[5], this.m[6], this.m[7],
- this.m[8], this.m[9], this.m[10], this.m[11],
- this.m[12], this.m[13], this.m[14], this.m[15]);
- }
- public decompose(scale: Vector3, rotation: Quaternion, translation: Vector3): boolean {
- translation.x = this.m[12];
- translation.y = this.m[13];
- translation.z = this.m[14];
- var xs = Tools.Sign(this.m[0] * this.m[1] * this.m[2] * this.m[3]) < 0 ? -1 : 1;
- var ys = Tools.Sign(this.m[4] * this.m[5] * this.m[6] * this.m[7]) < 0 ? -1 : 1;
- var zs = Tools.Sign(this.m[8] * this.m[9] * this.m[10] * this.m[11]) < 0 ? -1 : 1;
- scale.x = xs * Math.sqrt(this.m[0] * this.m[0] + this.m[1] * this.m[1] + this.m[2] * this.m[2]);
- scale.y = ys * Math.sqrt(this.m[4] * this.m[4] + this.m[5] * this.m[5] + this.m[6] * this.m[6]);
- scale.z = zs * Math.sqrt(this.m[8] * this.m[8] + this.m[9] * this.m[9] + this.m[10] * this.m[10]);
- if (scale.x === 0 || scale.y === 0 || scale.z === 0) {
- rotation.x = 0;
- rotation.y = 0;
- rotation.z = 0;
- rotation.w = 1;
- return false;
- }
- var rotationMatrix = Matrix.FromValues(
- this.m[0] / scale.x, this.m[1] / scale.x, this.m[2] / scale.x, 0,
- this.m[4] / scale.y, this.m[5] / scale.y, this.m[6] / scale.y, 0,
- this.m[8] / scale.z, this.m[9] / scale.z, this.m[10] / scale.z, 0,
- 0, 0, 0, 1);
- Quaternion.FromRotationMatrixToRef(rotationMatrix, rotation);
- return true;
- }
- // Statics
- public static FromArray(array: number[], offset?: number): Matrix {
- var result = new Matrix();
- if (!offset) {
- offset = 0;
- }
- Matrix.FromArrayToRef(array, offset, result);
- return result;
- }
- public static FromArrayToRef(array: number[], offset: number, result: Matrix) {
- for (var index = 0; index < 16; index++) {
- result.m[index] = array[index + offset];
- }
- }
- public static FromValuesToRef(initialM11: number, initialM12: number, initialM13: number, initialM14: number,
- initialM21: number, initialM22: number, initialM23: number, initialM24: number,
- initialM31: number, initialM32: number, initialM33: number, initialM34: number,
- initialM41: number, initialM42: number, initialM43: number, initialM44: number, result: Matrix): void {
- result.m[0] = initialM11;
- result.m[1] = initialM12;
- result.m[2] = initialM13;
- result.m[3] = initialM14;
- result.m[4] = initialM21;
- result.m[5] = initialM22;
- result.m[6] = initialM23;
- result.m[7] = initialM24;
- result.m[8] = initialM31;
- result.m[9] = initialM32;
- result.m[10] = initialM33;
- result.m[11] = initialM34;
- result.m[12] = initialM41;
- result.m[13] = initialM42;
- result.m[14] = initialM43;
- result.m[15] = initialM44;
- }
- public static FromValues(initialM11: number, initialM12: number, initialM13: number, initialM14: number,
- initialM21: number, initialM22: number, initialM23: number, initialM24: number,
- initialM31: number, initialM32: number, initialM33: number, initialM34: number,
- initialM41: number, initialM42: number, initialM43: number, initialM44: number): Matrix {
- var result = new Matrix();
- result.m[0] = initialM11;
- result.m[1] = initialM12;
- result.m[2] = initialM13;
- result.m[3] = initialM14;
- result.m[4] = initialM21;
- result.m[5] = initialM22;
- result.m[6] = initialM23;
- result.m[7] = initialM24;
- result.m[8] = initialM31;
- result.m[9] = initialM32;
- result.m[10] = initialM33;
- result.m[11] = initialM34;
- result.m[12] = initialM41;
- result.m[13] = initialM42;
- result.m[14] = initialM43;
- result.m[15] = initialM44;
- return result;
- }
- public static Compose(scale: Vector3, rotation: Quaternion, translation: Vector3): Matrix {
- var result = Matrix.FromValues(scale.x, 0, 0, 0,
- 0, scale.y, 0, 0,
- 0, 0, scale.z, 0,
- 0, 0, 0, 1);
- var rotationMatrix = Matrix.Identity();
- rotation.toRotationMatrix(rotationMatrix);
- result = result.multiply(rotationMatrix);
- result.setTranslation(translation);
- return result;
- }
- public static Identity(): Matrix {
- return Matrix.FromValues(1.0, 0, 0, 0,
- 0, 1.0, 0, 0,
- 0, 0, 1.0, 0,
- 0, 0, 0, 1.0);
- }
- public static IdentityToRef(result: Matrix): void {
- Matrix.FromValuesToRef(1.0, 0, 0, 0,
- 0, 1.0, 0, 0,
- 0, 0, 1.0, 0,
- 0, 0, 0, 1.0, result);
- }
- public static Zero(): Matrix {
- return Matrix.FromValues(0, 0, 0, 0,
- 0, 0, 0, 0,
- 0, 0, 0, 0,
- 0, 0, 0, 0);
- }
- public static RotationX(angle: number): Matrix {
- var result = new Matrix();
- Matrix.RotationXToRef(angle, result);
- return result;
- }
- public static Invert(source: Matrix): Matrix {
- var result = new Matrix();
- source.invertToRef(result);
- return result;
- }
- public static RotationXToRef(angle: number, result: Matrix): void {
- var s = Math.sin(angle);
- var c = Math.cos(angle);
- result.m[0] = 1.0;
- result.m[15] = 1.0;
- result.m[5] = c;
- result.m[10] = c;
- result.m[9] = -s;
- result.m[6] = s;
- result.m[1] = 0;
- result.m[2] = 0;
- result.m[3] = 0;
- result.m[4] = 0;
- result.m[7] = 0;
- result.m[8] = 0;
- result.m[11] = 0;
- result.m[12] = 0;
- result.m[13] = 0;
- result.m[14] = 0;
- }
- public static RotationY(angle: number): Matrix {
- var result = new Matrix();
- Matrix.RotationYToRef(angle, result);
- return result;
- }
- public static RotationYToRef(angle: number, result: Matrix): void {
- var s = Math.sin(angle);
- var c = Math.cos(angle);
- result.m[5] = 1.0;
- result.m[15] = 1.0;
- result.m[0] = c;
- result.m[2] = -s;
- result.m[8] = s;
- result.m[10] = c;
- result.m[1] = 0;
- result.m[3] = 0;
- result.m[4] = 0;
- result.m[6] = 0;
- result.m[7] = 0;
- result.m[9] = 0;
- result.m[11] = 0;
- result.m[12] = 0;
- result.m[13] = 0;
- result.m[14] = 0;
- }
- public static RotationZ(angle: number): Matrix {
- var result = new Matrix();
- Matrix.RotationZToRef(angle, result);
- return result;
- }
- public static RotationZToRef(angle: number, result: Matrix): void {
- var s = Math.sin(angle);
- var c = Math.cos(angle);
- result.m[10] = 1.0;
- result.m[15] = 1.0;
- result.m[0] = c;
- result.m[1] = s;
- result.m[4] = -s;
- result.m[5] = c;
- result.m[2] = 0;
- result.m[3] = 0;
- result.m[6] = 0;
- result.m[7] = 0;
- result.m[8] = 0;
- result.m[9] = 0;
- result.m[11] = 0;
- result.m[12] = 0;
- result.m[13] = 0;
- result.m[14] = 0;
- }
- public static RotationAxis(axis: Vector3, angle: number): Matrix {
- var s = Math.sin(-angle);
- var c = Math.cos(-angle);
- var c1 = 1 - c;
- axis.normalize();
- var result = Matrix.Zero();
- result.m[0] = (axis.x * axis.x) * c1 + c;
- result.m[1] = (axis.x * axis.y) * c1 - (axis.z * s);
- result.m[2] = (axis.x * axis.z) * c1 + (axis.y * s);
- result.m[3] = 0.0;
- result.m[4] = (axis.y * axis.x) * c1 + (axis.z * s);
- result.m[5] = (axis.y * axis.y) * c1 + c;
- result.m[6] = (axis.y * axis.z) * c1 - (axis.x * s);
- result.m[7] = 0.0;
- result.m[8] = (axis.z * axis.x) * c1 - (axis.y * s);
- result.m[9] = (axis.z * axis.y) * c1 + (axis.x * s);
- result.m[10] = (axis.z * axis.z) * c1 + c;
- result.m[11] = 0.0;
- result.m[15] = 1.0;
- return result;
- }
- public static RotationYawPitchRoll(yaw: number, pitch: number, roll: number): Matrix {
- var result = new Matrix();
- Matrix.RotationYawPitchRollToRef(yaw, pitch, roll, result);
- return result;
- }
- public static RotationYawPitchRollToRef(yaw: number, pitch: number, roll: number, result: Matrix): void {
- Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, this._tempQuaternion);
- this._tempQuaternion.toRotationMatrix(result);
- }
- public static Scaling(x: number, y: number, z: number): Matrix {
- var result = Matrix.Zero();
- Matrix.ScalingToRef(x, y, z, result);
- return result;
- }
- public static ScalingToRef(x: number, y: number, z: number, result: Matrix): void {
- result.m[0] = x;
- result.m[1] = 0;
- result.m[2] = 0;
- result.m[3] = 0;
- result.m[4] = 0;
- result.m[5] = y;
- result.m[6] = 0;
- result.m[7] = 0;
- result.m[8] = 0;
- result.m[9] = 0;
- result.m[10] = z;
- result.m[11] = 0;
- result.m[12] = 0;
- result.m[13] = 0;
- result.m[14] = 0;
- result.m[15] = 1.0;
- }
- public static Translation(x: number, y: number, z: number): Matrix {
- var result = Matrix.Identity();
- Matrix.TranslationToRef(x, y, z, result);
- return result;
- }
- public static TranslationToRef(x: number, y: number, z: number, result: Matrix): void {
- Matrix.FromValuesToRef(1.0, 0, 0, 0,
- 0, 1.0, 0, 0,
- 0, 0, 1.0, 0,
- x, y, z, 1.0, result);
- }
- public static LookAtLH(eye: Vector3, target: Vector3, up: Vector3): Matrix {
- var result = Matrix.Zero();
- Matrix.LookAtLHToRef(eye, target, up, result);
- return result;
- }
- public static LookAtLHToRef(eye: Vector3, target: Vector3, up: Vector3, result: Matrix): void {
- // Z axis
- target.subtractToRef(eye, this._zAxis);
- this._zAxis.normalize();
- // X axis
- Vector3.CrossToRef(up, this._zAxis, this._xAxis);
- this._xAxis.normalize();
- // Y axis
- Vector3.CrossToRef(this._zAxis, this._xAxis, this._yAxis);
- this._yAxis.normalize();
- // Eye angles
- var ex = -Vector3.Dot(this._xAxis, eye);
- var ey = -Vector3.Dot(this._yAxis, eye);
- var ez = -Vector3.Dot(this._zAxis, eye);
- return Matrix.FromValuesToRef(this._xAxis.x, this._yAxis.x, this._zAxis.x, 0,
- this._xAxis.y, this._yAxis.y, this._zAxis.y, 0,
- this._xAxis.z, this._yAxis.z, this._zAxis.z, 0,
- ex, ey, ez, 1, result);
- }
- public static LookAtLHToRefSIMD(eyeRef: Vector3, targetRef: Vector3, upRef: Vector3, result: Matrix): void {
- var out = result.m;
- var center = SIMD.float32x4(targetRef.x, targetRef.y, targetRef.z, 0);
- var eye = SIMD.float32x4(eyeRef.x, eyeRef.y, eyeRef.z, 0);
- var up = SIMD.float32x4(upRef.x, upRef.y, upRef.z, 0);
- // cc.kmVec3Subtract(f, pCenter, pEye);
- var f = SIMD.float32x4.sub(center, eye);
- // cc.kmVec3Normalize(f, f);
- var tmp = SIMD.float32x4.mul(f, f);
- tmp = SIMD.float32x4.add(tmp, SIMD.float32x4.add(SIMD.float32x4.swizzle(tmp, 1, 2, 0, 3), SIMD.float32x4.swizzle(tmp, 2, 0, 1, 3)));
- f = SIMD.float32x4.mul(f, SIMD.float32x4.reciprocalSqrtApproximation(tmp));
- // cc.kmVec3Assign(up, pUp);
- // cc.kmVec3Normalize(up, up);
- tmp = SIMD.float32x4.mul(up, up);
- tmp = SIMD.float32x4.add(tmp, SIMD.float32x4.add(SIMD.float32x4.swizzle(tmp, 1, 2, 0, 3), SIMD.float32x4.swizzle(tmp, 2, 0, 1, 3)));
- up = SIMD.float32x4.mul(up, SIMD.float32x4.reciprocalSqrtApproximation(tmp));
- // cc.kmVec3Cross(s, f, up);
- var s = SIMD.float32x4.sub(SIMD.float32x4.mul(SIMD.float32x4.swizzle(f, 1, 2, 0, 3), SIMD.float32x4.swizzle(up, 2, 0, 1, 3)), SIMD.float32x4.mul(SIMD.float32x4.swizzle(f, 2, 0, 1, 3), SIMD.float32x4.swizzle(up, 1, 2, 0, 3)));
- // cc.kmVec3Normalize(s, s);
- tmp = SIMD.float32x4.mul(s, s);
- tmp = SIMD.float32x4.add(tmp, SIMD.float32x4.add(SIMD.float32x4.swizzle(tmp, 1, 2, 0, 3), SIMD.float32x4.swizzle(tmp, 2, 0, 1, 3)));
- s = SIMD.float32x4.mul(s, SIMD.float32x4.reciprocalSqrtApproximation(tmp));
- // cc.kmVec3Cross(u, s, f);
- var u = SIMD.float32x4.sub(SIMD.float32x4.mul(SIMD.float32x4.swizzle(s, 1, 2, 0, 3), SIMD.float32x4.swizzle(f, 2, 0, 1, 3)), SIMD.float32x4.mul(SIMD.float32x4.swizzle(s, 2, 0, 1, 3), SIMD.float32x4.swizzle(f, 1, 2, 0, 3)));
- // cc.kmVec3Normalize(s, s);
- tmp = SIMD.float32x4.mul(s, s);
- tmp = SIMD.float32x4.add(tmp, SIMD.float32x4.add(SIMD.float32x4.swizzle(tmp, 1, 2, 0, 3), SIMD.float32x4.swizzle(tmp, 2, 0, 1, 3)));
- s = SIMD.float32x4.mul(s, SIMD.float32x4.reciprocalSqrtApproximation(tmp));
- var zero = SIMD.float32x4.splat(0.0);
- s = SIMD.float32x4.neg(s);
- var tmp01 = SIMD.float32x4.shuffle(s, u, 0, 1, 4, 5);
- var tmp23 = SIMD.float32x4.shuffle(f, zero, 0, 1, 4, 5);
- var a0 = SIMD.float32x4.shuffle(tmp01, tmp23, 0, 2, 4, 6);
- var a1 = SIMD.float32x4.shuffle(tmp01, tmp23, 1, 3, 5, 7);
- tmp01 = SIMD.float32x4.shuffle(s, u, 2, 3, 6, 7);
- tmp23 = SIMD.float32x4.shuffle(f, zero, 2, 3, 6, 7);
- var a2 = SIMD.float32x4.shuffle(tmp01, tmp23, 0, 2, 4, 6);
- var a3 = SIMD.float32x4(0.0, 0.0, 0.0, 1.0);
- var b0 = SIMD.float32x4(1.0, 0.0, 0.0, 0.0);
- var b1 = SIMD.float32x4(0.0, 1.0, 0.0, 0.0);
- var b2 = SIMD.float32x4(0.0, 0.0, 1.0, 0.0);
- var b3 = SIMD.float32x4.neg(eye);
- b3 = SIMD.float32x4.withW(b3, 1.0);
- SIMD.float32x4.store(out, 0, SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b0, 0, 0, 0, 0), a0), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b0, 1, 1, 1, 1), a1), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b0, 2, 2, 2, 2), a2), SIMD.float32x4.mul(SIMD.float32x4.swizzle(b0, 3, 3, 3, 3), a3)))));
- SIMD.float32x4.store(out, 4, SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b1, 0, 0, 0, 0), a0), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b1, 1, 1, 1, 1), a1), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b1, 2, 2, 2, 2), a2), SIMD.float32x4.mul(SIMD.float32x4.swizzle(b1, 3, 3, 3, 3), a3)))));
- SIMD.float32x4.store(out, 8, SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b2, 0, 0, 0, 0), a0), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b2, 1, 1, 1, 1), a1), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b2, 2, 2, 2, 2), a2), SIMD.float32x4.mul(SIMD.float32x4.swizzle(b2, 3, 3, 3, 3), a3)))));
- SIMD.float32x4.store(out, 12, SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b3, 0, 0, 0, 0), a0), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b3, 1, 1, 1, 1), a1), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b3, 2, 2, 2, 2), a2), SIMD.float32x4.mul(SIMD.float32x4.swizzle(b3, 3, 3, 3, 3), a3)))));
- }
- public static OrthoLH(width: number, height: number, znear: number, zfar: number): Matrix {
- var matrix = Matrix.Zero();
- Matrix.OrthoLHToRef(width, height, znear, zfar, matrix);
- return matrix;
- }
- public static OrthoLHToRef(width: number, height: number, znear: number, zfar: number, result: Matrix): void {
- var hw = 2.0 / width;
- var hh = 2.0 / height;
- var id = 1.0 / (zfar - znear);
- var nid = znear / (znear - zfar);
- Matrix.FromValuesToRef(hw, 0, 0, 0,
- 0, hh, 0, 0,
- 0, 0, id, 0,
- 0, 0, nid, 1, result);
- }
- public static OrthoOffCenterLH(left: number, right: number, bottom: number, top: number, znear: number, zfar: number): Matrix {
- var matrix = Matrix.Zero();
- Matrix.OrthoOffCenterLHToRef(left, right, bottom, top, znear, zfar, matrix);
- return matrix;
- }
- public static OrthoOffCenterLHToRef(left: number, right, bottom: number, top: number, znear: number, zfar: number, result: Matrix): void {
- result.m[0] = 2.0 / (right - left);
- result.m[1] = result.m[2] = result.m[3] = 0;
- result.m[5] = 2.0 / (top - bottom);
- result.m[4] = result.m[6] = result.m[7] = 0;
- result.m[10] = -1.0 / (znear - zfar);
- result.m[8] = result.m[9] = result.m[11] = 0;
- result.m[12] = (left + right) / (left - right);
- result.m[13] = (top + bottom) / (bottom - top);
- result.m[14] = znear / (znear - zfar);
- result.m[15] = 1.0;
- }
- public static PerspectiveLH(width: number, height: number, znear: number, zfar: number): Matrix {
- var matrix = Matrix.Zero();
- matrix.m[0] = (2.0 * znear) / width;
- matrix.m[1] = matrix.m[2] = matrix.m[3] = 0.0;
- matrix.m[5] = (2.0 * znear) / height;
- matrix.m[4] = matrix.m[6] = matrix.m[7] = 0.0;
- matrix.m[10] = -zfar / (znear - zfar);
- matrix.m[8] = matrix.m[9] = 0.0;
- matrix.m[11] = 1.0;
- matrix.m[12] = matrix.m[13] = matrix.m[15] = 0.0;
- matrix.m[14] = (znear * zfar) / (znear - zfar);
- return matrix;
- }
- public static PerspectiveFovLH(fov: number, aspect: number, znear: number, zfar: number): Matrix {
- var matrix = Matrix.Zero();
- Matrix.PerspectiveFovLHToRef(fov, aspect, znear, zfar, matrix);
- return matrix;
- }
- public static PerspectiveFovLHToRef(fov: number, aspect: number, znear: number, zfar: number, result: Matrix, fovMode = Camera.FOVMODE_VERTICAL_FIXED): void {
- var tan = 1.0 / (Math.tan(fov * 0.5));
- var v_fixed = (fovMode === Camera.FOVMODE_VERTICAL_FIXED);
- if (v_fixed) {
- result.m[0] = tan / aspect;
- }
- else {
- result.m[0] = tan;
- }
- result.m[1] = result.m[2] = result.m[3] = 0.0;
- if (v_fixed) {
- result.m[5] = tan;
- }
- else {
- result.m[5] = tan * aspect;
- }
- result.m[4] = result.m[6] = result.m[7] = 0.0;
- result.m[8] = result.m[9] = 0.0;
- result.m[10] = -zfar / (znear - zfar);
- result.m[11] = 1.0;
- result.m[12] = result.m[13] = result.m[15] = 0.0;
- result.m[14] = (znear * zfar) / (znear - zfar);
- }
- public static GetFinalMatrix(viewport: Viewport, world: Matrix, view: Matrix, projection: Matrix, zmin: number, zmax: number): Matrix {
- var cw = viewport.width;
- var ch = viewport.height;
- var cx = viewport.x;
- var cy = viewport.y;
- var viewportMatrix = Matrix.FromValues(cw / 2.0, 0, 0, 0,
- 0, -ch / 2.0, 0, 0,
- 0, 0, zmax - zmin, 0,
- cx + cw / 2.0, ch / 2.0 + cy, zmin, 1);
- return world.multiply(view).multiply(projection).multiply(viewportMatrix);
- }
- public static Transpose(matrix: Matrix): Matrix {
- var result = new Matrix();
- result.m[0] = matrix.m[0];
- result.m[1] = matrix.m[4];
- result.m[2] = matrix.m[8];
- result.m[3] = matrix.m[12];
- result.m[4] = matrix.m[1];
- result.m[5] = matrix.m[5];
- result.m[6] = matrix.m[9];
- result.m[7] = matrix.m[13];
- result.m[8] = matrix.m[2];
- result.m[9] = matrix.m[6];
- result.m[10] = matrix.m[10];
- result.m[11] = matrix.m[14];
- result.m[12] = matrix.m[3];
- result.m[13] = matrix.m[7];
- result.m[14] = matrix.m[11];
- result.m[15] = matrix.m[15];
- return result;
- }
- public static Reflection(plane: Plane): Matrix {
- var matrix = new Matrix();
- Matrix.ReflectionToRef(plane, matrix);
- return matrix;
- }
- public static ReflectionToRef(plane: Plane, result: Matrix): void {
- plane.normalize();
- var x = plane.normal.x;
- var y = plane.normal.y;
- var z = plane.normal.z;
- var temp = -2 * x;
- var temp2 = -2 * y;
- var temp3 = -2 * z;
- result.m[0] = (temp * x) + 1;
- result.m[1] = temp2 * x;
- result.m[2] = temp3 * x;
- result.m[3] = 0.0;
- result.m[4] = temp * y;
- result.m[5] = (temp2 * y) + 1;
- result.m[6] = temp3 * y;
- result.m[7] = 0.0;
- result.m[8] = temp * z;
- result.m[9] = temp2 * z;
- result.m[10] = (temp3 * z) + 1;
- result.m[11] = 0.0;
- result.m[12] = temp * plane.d;
- result.m[13] = temp2 * plane.d;
- result.m[14] = temp3 * plane.d;
- result.m[15] = 1.0;
- }
- }
- export class Plane {
- public normal: Vector3;
- public d: number;
- constructor(a: number, b: number, c: number, d: number) {
- this.normal = new Vector3(a, b, c);
- this.d = d;
- }
- public asArray(): number[] {
- return [this.normal.x, this.normal.y, this.normal.z, this.d];
- }
- // Methods
- public clone(): Plane {
- return new Plane(this.normal.x, this.normal.y, this.normal.z, this.d);
- }
- public normalize(): Plane {
- var norm = (Math.sqrt((this.normal.x * this.normal.x) + (this.normal.y * this.normal.y) + (this.normal.z * this.normal.z)));
- var magnitude = 0;
- if (norm !== 0) {
- magnitude = 1.0 / norm;
- }
- this.normal.x *= magnitude;
- this.normal.y *= magnitude;
- this.normal.z *= magnitude;
- this.d *= magnitude;
- return this;
- }
- public transform(transformation: Matrix): Plane {
- var transposedMatrix = Matrix.Transpose(transformation);
- var x = this.normal.x;
- var y = this.normal.y;
- var z = this.normal.z;
- var d = this.d;
- var normalX = (((x * transposedMatrix.m[0]) + (y * transposedMatrix.m[1])) + (z * transposedMatrix.m[2])) + (d * transposedMatrix.m[3]);
- var normalY = (((x * transposedMatrix.m[4]) + (y * transposedMatrix.m[5])) + (z * transposedMatrix.m[6])) + (d * transposedMatrix.m[7]);
- var normalZ = (((x * transposedMatrix.m[8]) + (y * transposedMatrix.m[9])) + (z * transposedMatrix.m[10])) + (d * transposedMatrix.m[11]);
- var finalD = (((x * transposedMatrix.m[12]) + (y * transposedMatrix.m[13])) + (z * transposedMatrix.m[14])) + (d * transposedMatrix.m[15]);
- return new Plane(normalX, normalY, normalZ, finalD);
- }
- public dotCoordinate(point): number {
- return ((((this.normal.x * point.x) + (this.normal.y * point.y)) + (this.normal.z * point.z)) + this.d);
- }
- public copyFromPoints(point1: Vector3, point2: Vector3, point3: Vector3): Plane {
- var x1 = point2.x - point1.x;
- var y1 = point2.y - point1.y;
- var z1 = point2.z - point1.z;
- var x2 = point3.x - point1.x;
- var y2 = point3.y - point1.y;
- var z2 = point3.z - point1.z;
- var yz = (y1 * z2) - (z1 * y2);
- var xz = (z1 * x2) - (x1 * z2);
- var xy = (x1 * y2) - (y1 * x2);
- var pyth = (Math.sqrt((yz * yz) + (xz * xz) + (xy * xy)));
- var invPyth;
- if (pyth !== 0) {
- invPyth = 1.0 / pyth;
- }
- else {
- invPyth = 0;
- }
- this.normal.x = yz * invPyth;
- this.normal.y = xz * invPyth;
- this.normal.z = xy * invPyth;
- this.d = -((this.normal.x * point1.x) + (this.normal.y * point1.y) + (this.normal.z * point1.z));
- return this;
- }
- public isFrontFacingTo(direction: Vector3, epsilon: number): boolean {
- var dot = Vector3.Dot(this.normal, direction);
- return (dot <= epsilon);
- }
- public signedDistanceTo(point: Vector3): number {
- return Vector3.Dot(point, this.normal) + this.d;
- }
- // Statics
- static FromArray(array: number[]): Plane {
- return new Plane(array[0], array[1], array[2], array[3]);
- }
- static FromPoints(point1, point2, point3): Plane {
- var result = new Plane(0, 0, 0, 0);
- result.copyFromPoints(point1, point2, point3);
- return result;
- }
- static FromPositionAndNormal(origin: Vector3, normal: Vector3): Plane {
- var result = new Plane(0, 0, 0, 0);
- normal.normalize();
- result.normal = normal;
- result.d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
- return result;
- }
- static SignedDistanceToPlaneFromPositionAndNormal(origin: Vector3, normal: Vector3, point: Vector3): number {
- var d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
- return Vector3.Dot(point, normal) + d;
- }
- }
- export class Viewport {
- constructor(public x: number, public y: number, public width: number, public height: number) {
- }
- public toGlobal(engine): Viewport {
- var width = engine.getRenderWidth();
- var height = engine.getRenderHeight();
- return new Viewport(this.x * width, this.y * height, this.width * width, this.height * height);
- }
- }
- export class Frustum {
- public static GetPlanes(transform: Matrix): Plane[] {
- var frustumPlanes = [];
- for (var index = 0; index < 6; index++) {
- frustumPlanes.push(new Plane(0, 0, 0, 0));
- }
- Frustum.GetPlanesToRef(transform, frustumPlanes);
- return frustumPlanes;
- }
- public static GetPlanesToRef(transform: Matrix, frustumPlanes: Plane[]): void {
- // Near
- frustumPlanes[0].normal.x = transform.m[3] + transform.m[2];
- frustumPlanes[0].normal.y = transform.m[7] + transform.m[6];
- frustumPlanes[0].normal.z = transform.m[10] + transform.m[10];
- frustumPlanes[0].d = transform.m[15] + transform.m[14];
- frustumPlanes[0].normalize();
- // Far
- frustumPlanes[1].normal.x = transform.m[3] - transform.m[2];
- frustumPlanes[1].normal.y = transform.m[7] - transform.m[6];
- frustumPlanes[1].normal.z = transform.m[11] - transform.m[10];
- frustumPlanes[1].d = transform.m[15] - transform.m[14];
- frustumPlanes[1].normalize();
- // Left
- frustumPlanes[2].normal.x = transform.m[3] + transform.m[0];
- frustumPlanes[2].normal.y = transform.m[7] + transform.m[4];
- frustumPlanes[2].normal.z = transform.m[11] + transform.m[8];
- frustumPlanes[2].d = transform.m[15] + transform.m[12];
- frustumPlanes[2].normalize();
- // Right
- frustumPlanes[3].normal.x = transform.m[3] - transform.m[0];
- frustumPlanes[3].normal.y = transform.m[7] - transform.m[4];
- frustumPlanes[3].normal.z = transform.m[11] - transform.m[8];
- frustumPlanes[3].d = transform.m[15] - transform.m[12];
- frustumPlanes[3].normalize();
- // Top
- frustumPlanes[4].normal.x = transform.m[3] - transform.m[1];
- frustumPlanes[4].normal.y = transform.m[7] - transform.m[5];
- frustumPlanes[4].normal.z = transform.m[11] - transform.m[9];
- frustumPlanes[4].d = transform.m[15] - transform.m[13];
- frustumPlanes[4].normalize();
- // Bottom
- frustumPlanes[5].normal.x = transform.m[3] + transform.m[1];
- frustumPlanes[5].normal.y = transform.m[7] + transform.m[5];
- frustumPlanes[5].normal.z = transform.m[11] + transform.m[9];
- frustumPlanes[5].d = transform.m[15] + transform.m[13];
- frustumPlanes[5].normalize();
- }
- }
- export class Ray {
- private _edge1: Vector3;
- private _edge2: Vector3;
- private _pvec: Vector3;
- private _tvec: Vector3;
- private _qvec: Vector3;
- constructor(public origin: Vector3, public direction: Vector3, public length: number = Number.MAX_VALUE) {
- }
- // Methods
- public intersectsBoxMinMax(minimum: Vector3, maximum: Vector3): boolean {
- var d = 0.0;
- var maxValue = Number.MAX_VALUE;
- if (Math.abs(this.direction.x) < 0.0000001) {
- if (this.origin.x < minimum.x || this.origin.x > maximum.x) {
- return false;
- }
- }
- else {
- var inv = 1.0 / this.direction.x;
- var min = (minimum.x - this.origin.x) * inv;
- var max = (maximum.x - this.origin.x) * inv;
- if (max === -Infinity) {
- max = Infinity;
- }
- if (min > max) {
- var temp = min;
- min = max;
- max = temp;
- }
- d = Math.max(min, d);
- maxValue = Math.min(max, maxValue);
- if (d > maxValue) {
- return false;
- }
- }
- if (Math.abs(this.direction.y) < 0.0000001) {
- if (this.origin.y < minimum.y || this.origin.y > maximum.y) {
- return false;
- }
- }
- else {
- inv = 1.0 / this.direction.y;
- min = (minimum.y - this.origin.y) * inv;
- max = (maximum.y - this.origin.y) * inv;
- if (max === -Infinity) {
- max = Infinity;
- }
- if (min > max) {
- temp = min;
- min = max;
- max = temp;
- }
- d = Math.max(min, d);
- maxValue = Math.min(max, maxValue);
- if (d > maxValue) {
- return false;
- }
- }
- if (Math.abs(this.direction.z) < 0.0000001) {
- if (this.origin.z < minimum.z || this.origin.z > maximum.z) {
- return false;
- }
- }
- else {
- inv = 1.0 / this.direction.z;
- min = (minimum.z - this.origin.z) * inv;
- max = (maximum.z - this.origin.z) * inv;
- if (max === -Infinity) {
- max = Infinity;
- }
- if (min > max) {
- temp = min;
- min = max;
- max = temp;
- }
- d = Math.max(min, d);
- maxValue = Math.min(max, maxValue);
- if (d > maxValue) {
- return false;
- }
- }
- return true;
- }
- public intersectsBox(box: BoundingBox): boolean {
- return this.intersectsBoxMinMax(box.minimum, box.maximum);
- }
- public intersectsSphere(sphere): boolean {
- var x = sphere.center.x - this.origin.x;
- var y = sphere.center.y - this.origin.y;
- var z = sphere.center.z - this.origin.z;
- var pyth = (x * x) + (y * y) + (z * z);
- var rr = sphere.radius * sphere.radius;
- if (pyth <= rr) {
- return true;
- }
- var dot = (x * this.direction.x) + (y * this.direction.y) + (z * this.direction.z);
- if (dot < 0.0) {
- return false;
- }
- var temp = pyth - (dot * dot);
- return temp <= rr;
- }
- public intersectsTriangle(vertex0: Vector3, vertex1: Vector3, vertex2: Vector3): IntersectionInfo {
- if (!this._edge1) {
- this._edge1 = Vector3.Zero();
- this._edge2 = Vector3.Zero();
- this._pvec = Vector3.Zero();
- this._tvec = Vector3.Zero();
- this._qvec = Vector3.Zero();
- }
- vertex1.subtractToRef(vertex0, this._edge1);
- vertex2.subtractToRef(vertex0, this._edge2);
- Vector3.CrossToRef(this.direction, this._edge2, this._pvec);
- var det = Vector3.Dot(this._edge1, this._pvec);
- if (det === 0) {
- return null;
- }
- var invdet = 1 / det;
- this.origin.subtractToRef(vertex0, this._tvec);
- var bu = Vector3.Dot(this._tvec, this._pvec) * invdet;
- if (bu < 0 || bu > 1.0) {
- return null;
- }
- Vector3.CrossToRef(this._tvec, this._edge1, this._qvec);
- var bv = Vector3.Dot(this.direction, this._qvec) * invdet;
- if (bv < 0 || bu + bv > 1.0) {
- return null;
- }
- //check if the distance is longer than the predefined length.
- var distance = Vector3.Dot(this._edge2, this._qvec) * invdet;
- if (distance > this.length) {
- return null;
- }
- return new IntersectionInfo(bu, bv, distance);
- }
- // Statics
- public static CreateNew(x: number, y: number, viewportWidth: number, viewportHeight: number, world: Matrix, view: Matrix, projection: Matrix): Ray {
- var start = Vector3.Unproject(new Vector3(x, y, 0), viewportWidth, viewportHeight, world, view, projection);
- var end = Vector3.Unproject(new Vector3(x, y, 1), viewportWidth, viewportHeight, world, view, projection);
- var direction = end.subtract(start);
- direction.normalize();
- return new Ray(start, direction);
- }
- /**
- * Function will create a new transformed ray starting from origin and ending at the end point. Ray's length will be set, and ray will be
- * transformed to the given world matrix.
- * @param origin The origin point
- * @param end The end point
- * @param world a matrix to transform the ray to. Default is the identity matrix.
- */
- public static CreateNewFromTo(origin: Vector3, end: Vector3, world: Matrix = Matrix.Identity()): Ray {
- var direction = end.subtract(origin);
- var length = Math.sqrt((direction.x * direction.x) + (direction.y * direction.y) + (direction.z * direction.z));
- direction.normalize();
- return Ray.Transform(new Ray(origin, direction, length), world);
- }
- public static Transform(ray: Ray, matrix: Matrix): Ray {
- var newOrigin = Vector3.TransformCoordinates(ray.origin, matrix);
- var newDirection = Vector3.TransformNormal(ray.direction, matrix);
- return new Ray(newOrigin, newDirection, ray.length);
- }
- }
- export enum Space {
- LOCAL = 0,
- WORLD = 1
- }
- export class Axis {
- public static X: Vector3 = new Vector3(1, 0, 0);
- public static Y: Vector3 = new Vector3(0, 1, 0);
- public static Z: Vector3 = new Vector3(0, 0, 1);
- };
- export class BezierCurve {
- public static interpolate(t: number, x1: number, y1: number, x2: number, y2: number): number {
- // Extract X (which is equal to time here)
- var f0 = 1 - 3 * x2 + 3 * x1;
- var f1 = 3 * x2 - 6 * x1;
- var f2 = 3 * x1;
- var refinedT = t;
- for (var i = 0; i < 5; i++) {
- var refinedT2 = refinedT * refinedT;
- var refinedT3 = refinedT2 * refinedT;
- var x = f0 * refinedT3 + f1 * refinedT2 + f2 * refinedT;
- var slope = 1.0 / (3.0 * f0 * refinedT2 + 2.0 * f1 * refinedT + f2);
- refinedT -= (x - t) * slope;
- refinedT = Math.min(1, Math.max(0, refinedT));
- }
- // Resolve cubic bezier for the given x
- return 3 * Math.pow(1 - refinedT, 2) * refinedT * y1 +
- 3 * (1 - refinedT) * Math.pow(refinedT, 2) * y2 +
- Math.pow(refinedT, 3);
- }
- }
- export enum Orientation {
- CW = 0,
- CCW = 1
- }
- export class Angle {
- private _radians: number;
- constructor(radians: number) {
- this._radians = radians;
- if (this._radians < 0) this._radians += (2 * Math.PI);
- }
- public degrees = () => this._radians * 180 / Math.PI;
- public radians = () => this._radians;
- public static BetweenTwoPoints(a: Vector2, b: Vector2): Angle {
- var delta = b.subtract(a);
- var theta = Math.atan2(delta.y, delta.x);
- return new Angle(theta);
- }
- public static FromRadians(radians: number): Angle {
- return new Angle(radians);
- }
- public static FromDegrees(degrees: number): Angle {
- return new Angle(degrees * Math.PI / 180);
- }
- }
- export class Arc2 {
- centerPoint: Vector2;
- radius: number;
- angle: Angle;
- startAngle: Angle;
- orientation: Orientation;
- constructor(public startPoint: Vector2, public midPoint: Vector2, public endPoint: Vector2) {
- var temp = Math.pow(midPoint.x, 2) + Math.pow(midPoint.y, 2);
- var startToMid = (Math.pow(startPoint.x, 2) + Math.pow(startPoint.y, 2) - temp) / 2.;
- var midToEnd = (temp - Math.pow(endPoint.x, 2) - Math.pow(endPoint.y, 2)) / 2.;
- var det = (startPoint.x - midPoint.x) * (midPoint.y - endPoint.y) - (midPoint.x - endPoint.x) * (startPoint.y - midPoint.y);
- this.centerPoint = new Vector2(
- (startToMid * (midPoint.y - endPoint.y) - midToEnd * (startPoint.y - midPoint.y)) / det,
- ((startPoint.x - midPoint.x) * midToEnd - (midPoint.x - endPoint.x) * startToMid) / det
- );
- this.radius = this.centerPoint.subtract(this.startPoint).length();
- this.startAngle = Angle.BetweenTwoPoints(this.centerPoint, this.startPoint);
- var a1 = this.startAngle.degrees();
- var a2 = Angle.BetweenTwoPoints(this.centerPoint, this.midPoint).degrees();
- var a3 = Angle.BetweenTwoPoints(this.centerPoint, this.endPoint).degrees();
- // angles correction
- if (a2 - a1 > +180.0) a2 -= 360.0;
- if (a2 - a1 < -180.0) a2 += 360.0;
- if (a3 - a2 > +180.0) a3 -= 360.0;
- if (a3 - a2 < -180.0) a3 += 360.0;
- this.orientation = (a2 - a1) < 0 ? Orientation.CW : Orientation.CCW;
- this.angle = Angle.FromDegrees(this.orientation === Orientation.CW ? a1 - a3 : a3 - a1);
- }
- }
- export class PathCursor {
- private _onchange = new Array<(cursor: PathCursor) => void>();
- value: number = 0;
- animations = new Array<Animation>();
- constructor(private path: Path2) {
- }
- public getPoint(): Vector3 {
- var point = this.path.getPointAtLengthPosition(this.value);
- return new Vector3(point.x, 0, point.y);
- }
- public moveAhead(step: number = 0.002): PathCursor {
- this.move(step);
- return this;
- }
- public moveBack(step: number = 0.002): PathCursor {
- this.move(-step);
- return this;
- }
- public move(step: number): PathCursor {
- if (Math.abs(step) > 1) {
- throw "step size should be less than 1.";
- }
- this.value += step;
- this.ensureLimits();
- this.raiseOnChange();
- return this;
- }
- private ensureLimits(): PathCursor {
- while (this.value > 1) {
- this.value -= 1;
- }
- while (this.value < 0) {
- this.value += 1;
- }
- return this;
- }
- // used by animation engine
- private markAsDirty(propertyName: string): PathCursor {
- this.ensureLimits();
- this.raiseOnChange();
- return this;
- }
- private raiseOnChange(): PathCursor {
- this._onchange.forEach(f => f(this));
- return this;
- }
- public onchange(f: (cursor: PathCursor) => void): PathCursor {
- this._onchange.push(f);
- return this;
- }
- }
- export class Path2 {
- private _points = new Array<Vector2>();
- private _length = 0;
- public closed = false;
- constructor(x: number, y: number) {
- this._points.push(new Vector2(x, y));
- }
- public addLineTo(x: number, y: number): Path2 {
- if (closed) {
- Tools.Error("cannot add lines to closed paths");
- return this;
- }
- var newPoint = new Vector2(x, y);
- var previousPoint = this._points[this._points.length - 1];
- this._points.push(newPoint);
- this._length += newPoint.subtract(previousPoint).length();
- return this;
- }
- public addArcTo(midX: number, midY: number, endX: number, endY: number, numberOfSegments = 36): Path2 {
- if (closed) {
- Tools.Error("cannot add arcs to closed paths");
- return this;
- }
- var startPoint = this._points[this._points.length - 1];
- var midPoint = new Vector2(midX, midY);
- var endPoint = new Vector2(endX, endY);
- var arc = new Arc2(startPoint, midPoint, endPoint);
- var increment = arc.angle.radians() / numberOfSegments;
- if (arc.orientation === Orientation.CW) increment *= -1;
- var currentAngle = arc.startAngle.radians() + increment;
- for (var i = 0; i < numberOfSegments; i++) {
- var x = Math.cos(currentAngle) * arc.radius + arc.centerPoint.x;
- var y = Math.sin(currentAngle) * arc.radius + arc.centerPoint.y;
- this.addLineTo(x, y);
- currentAngle += increment;
- }
- return this;
- }
- public close(): Path2 {
- this.closed = true;
- return this;
- }
- public length(): number {
- var result = this._length;
- if (!this.closed) {
- var lastPoint = this._points[this._points.length - 1];
- var firstPoint = this._points[0];
- result += (firstPoint.subtract(lastPoint).length());
- }
- return result;
- }
- public getPoints(): Vector2[] {
- return this._points;
- }
- public getPointAtLengthPosition(normalizedLengthPosition: number): Vector2 {
- if (normalizedLengthPosition < 0 || normalizedLengthPosition > 1) {
- Tools.Error("normalized length position should be between 0 and 1.");
- return Vector2.Zero();
- }
- var lengthPosition = normalizedLengthPosition * this.length();
- var previousOffset = 0;
- for (var i = 0; i < this._points.length; i++) {
- var j = (i + 1) % this._points.length;
- var a = this._points[i];
- var b = this._points[j];
- var bToA = b.subtract(a);
- var nextOffset = (bToA.length() + previousOffset);
- if (lengthPosition >= previousOffset && lengthPosition <= nextOffset) {
- var dir = bToA.normalize();
- var localOffset = lengthPosition - previousOffset;
- return new Vector2(
- a.x + (dir.x * localOffset),
- a.y + (dir.y * localOffset)
- );
- }
- previousOffset = nextOffset;
- }
- Tools.Error("internal error");
- return Vector2.Zero();
- }
- public static StartingAt(x: number, y: number): Path2 {
- return new Path2(x, y);
- }
- }
- export class Path3D {
- private _curve = new Array<Vector3>();
- private _distances = new Array<number>();
- private _tangents = new Array<Vector3>();
- private _normals = new Array<Vector3>();
- private _binormals = new Array<Vector3>();
- constructor(public path: Vector3[]) {
- this._curve = path.slice(); // copy array
- this._compute();
- }
- public getCurve(): Vector3[] {
- return this._curve;
- }
- public getTangents(): Vector3[] {
- return this._tangents;
- }
- public getNormals(): Vector3[] {
- return this._normals;
- }
- public getBinormals(): Vector3[] {
- return this._binormals;
- }
- public getDistances(): number[] {
- return this._distances;
- }
- public update(path: Vector3[]): Path3D {
- for (var i = 0; i < path.length; i++) {
- this._curve[i] = path[i];
- }
- this._compute();
- return this;
- }
- // private function compute() : computes tangents, normals and binormals
- private _compute() {
- var l = this._curve.length;
- // first and last tangents
- this._tangents[0] = this._curve[1].subtract(this._curve[0]);
- this._tangents[0].normalize();
- this._tangents[l - 1] = this._curve[l - 1].subtract(this._curve[l - 2]);
- this._tangents[l - 1].normalize();
-
- // normals and binormals at first point : arbitrary vector with _normalVector()
- var tg0 = this._tangents[0];
- var pp0 = this._normalVector(this._curve[0], tg0);
- this._normals[0] = pp0;
- this._normals[0].normalize();
- this._binormals[0] = Vector3.Cross(tg0, this._normals[0]);
- this._normals[0].normalize();
- this._distances[0] = 0;
- // normals and binormals : next points
- var prev: Vector3; // previous vector (segment)
- var cur: Vector3; // current vector (segment)
- var curTang: Vector3; // current tangent
- var prevNorm: Vector3; // previous normal
- var prevBinor: Vector3; // previous binormal
- for (var i = 1; i < l; i++) {
- // tangents
- prev = this._curve[i].subtract(this._curve[i - 1]);
- if (i < l - 1) {
- cur = this._curve[i + 1].subtract(this._curve[i]);
- this._tangents[i] = prev.add(cur);
- this._tangents[i].normalize();
- }
- this._distances[i] = this._distances[i - 1] + prev.length();
-
- // normals and binormals
- // http://www.cs.cmu.edu/afs/andrew/scs/cs/15-462/web/old/asst2camera.html
- curTang = this._tangents[i];
- prevNorm = this._normals[i - 1];
- prevBinor = this._binormals[i - 1];
- this._normals[i] = Vector3.Cross(prevBinor, curTang);
- this._normals[i].normalize();
- this._binormals[i] = Vector3.Cross(curTang, this._normals[i]);
- this._binormals[i].normalize();
- }
- }
- // private function normalVector(v0, vt) :
- // returns an arbitrary point in the plane defined by the point v0 and the vector vt orthogonal to this plane
- private _normalVector(v0: Vector3, vt: Vector3): Vector3 {
- var point: Vector3;
- if (vt.x !== 1) { // search for a point in the plane
- point = new Vector3(1, 0, 0);
- }
- else if (vt.y !== 1) {
- point = new Vector3(0, 1, 0);
- }
- else if (vt.z !== 1) {
- point = new Vector3(0, 0, 1);
- }
- var normal0: Vector3 = Vector3.Cross(vt, point);
- normal0.normalize();
- return normal0;
- }
- }
- export class Curve3 {
- private _points: Vector3[];
- // QuadraticBezier(origin_V3, control_V3, destination_V3 )
- public static CreateQuadraticBezier(v0: Vector3, v1: Vector3, v2: Vector3, nbPoints: number): Curve3 {
- nbPoints = nbPoints > 2 ? nbPoints : 3;
- var bez = new Array<Vector3>();
- var step = 1 / nbPoints;
- var equation = (t: number, val0: number, val1: number, val2: number) => {
- var res = (1 - t) * (1 - t) * val0 + 2 * t * (1 - t) * val1 + t * t * val2;
- return res;
- }
- for (var i = 0; i <= 1; i += step) {
- bez.push(new Vector3(equation(i, v0.x, v1.x, v2.x), equation(i, v0.y, v1.y, v2.y), equation(i, v0.z, v1.z, v2.z)));
- }
- return new Curve3(bez);
- }
- // CubicBezier(origin_V3, control1_V3, control2_V3, destination_V3)
- public static CreateCubicBezier(v0: Vector3, v1: Vector3, v2: Vector3, v3: Vector3, nbPoints: number): Curve3 {
- nbPoints = nbPoints > 3 ? nbPoints : 4;
- var bez = new Array<Vector3>();
- var step = 1 / nbPoints;
- var equation = (t: number, val0: number, val1: number, val2: number, val3: number) => {
- var res = (1 - t) * (1 - t) * (1 - t) * val0 + 3 * t * (1 - t) * (1 - t) * val1 + 3 * t * t * (1 - t) * val2 + t * t * t * val3;
- return res;
- }
- for (var i = 0; i <= 1; i += step) {
- bez.push(new Vector3(equation(i, v0.x, v1.x, v2.x, v3.x), equation(i, v0.y, v1.y, v2.y, v3.y), equation(i, v0.z, v1.z, v2.z, v3.z)));
- }
- return new Curve3(bez);
- }
- constructor(points: Vector3[]) {
- this._points = points;
- }
- public getPoints() {
- return this._points;
- }
- public continue(curve: Curve3): Curve3 {
- var lastPoint = this._points[this._points.length - 1];
- var continuedPoints = this._points.slice();
- var curvePoints = curve.getPoints();
- for (var i = 1; i < curvePoints.length; i++) {
- continuedPoints.push(curvePoints[i].add(lastPoint));
- }
- return new Curve3(continuedPoints);
- }
- }
- // Vertex formats
- export class PositionNormalVertex {
- constructor(public position: Vector3 = Vector3.Zero(), public normal: Vector3 = Vector3.Up()) {
- }
- public clone(): PositionNormalVertex {
- return new PositionNormalVertex(this.position.clone(), this.normal.clone());
- }
- }
- export class PositionNormalTextureVertex {
- constructor(public position: Vector3 = Vector3.Zero(), public normal: Vector3 = Vector3.Up(), public uv: Vector2 = Vector2.Zero()) {
- }
- public clone(): PositionNormalTextureVertex {
- return new PositionNormalTextureVertex(this.position.clone(), this.normal.clone(), this.uv.clone());
- }
- }
- // SIMD
- var previousMultiplyToArray = Matrix.prototype.multiplyToArray;
- var previousInvertToRef = Matrix.prototype.invertToRef;
- var previousLookAtLHToRef = Matrix.LookAtLHToRef;
- var previousTransformCoordinatesToRef = Vector3.TransformCoordinatesToRef;
- var previousTransformCoordinatesFromFloatsToRef = Vector3.TransformCoordinatesFromFloatsToRef;
- export class SIMDHelper {
- private static _isEnabled = false;
- public static get IsEnabled(): boolean {
- return SIMDHelper._isEnabled;
- }
- public static DisableSIMD(): void {
- // Replace functions
- Matrix.prototype.multiplyToArray = <any>previousMultiplyToArray;
- Matrix.prototype.invertToRef = <any>previousInvertToRef;
- Matrix.LookAtLHToRef = <any>previousLookAtLHToRef;
- Vector3.TransformCoordinatesToRef = <any>previousTransformCoordinatesToRef;
- Vector3.TransformCoordinatesFromFloatsToRef = <any>previousTransformCoordinatesFromFloatsToRef;
- SIMDHelper._isEnabled = false;
- }
- public static EnableSIMD(): void {
- if (window.SIMD === undefined) {
- return;
- }
- // Replace functions
- Matrix.prototype.multiplyToArray = <any>Matrix.prototype.multiplyToArraySIMD;
- Matrix.prototype.invertToRef = <any>Matrix.prototype.invertToRefSIMD;
- Matrix.LookAtLHToRef = <any>Matrix.LookAtLHToRefSIMD;
- Vector3.TransformCoordinatesToRef = <any>Vector3.TransformCoordinatesToRefSIMD;
- Vector3.TransformCoordinatesFromFloatsToRef = <any>Vector3.TransformCoordinatesFromFloatsToRefSIMD;
- Object.defineProperty(BABYLON.Vector3.prototype, "x", {
- get: function () { return this._data[0]; },
- set: function (value: number) {
- if (!this._data) {
- this._data = new Float32Array(3);
- }
- this._data[0] = value;
- }
- });
- Object.defineProperty(BABYLON.Vector3.prototype, "y", {
- get: function () { return this._data[1]; },
- set: function (value: number) {
- this._data[1] = value;
- }
- });
- Object.defineProperty(BABYLON.Vector3.prototype, "z", {
- get: function () { return this._data[2]; },
- set: function (value: number) {
- this._data[2] = value;
- }
- });
- SIMDHelper._isEnabled = true;
- }
- }
-
- if (window.SIMD !== undefined) {
- SIMDHelper.EnableSIMD();
- }
- }
|