gl-matrix.js 113 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292
  1. /**
  2. * @fileoverview gl-matrix - High performance matrix and vector operations
  3. * @author Brandon Jones
  4. * @author Colin MacKenzie IV
  5. * @version 2.2.2
  6. */
  7. /* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved.
  8. Redistribution and use in source and binary forms, with or without modification,
  9. are permitted provided that the following conditions are met:
  10. * Redistributions of source code must retain the above copyright notice, this
  11. list of conditions and the following disclaimer.
  12. * Redistributions in binary form must reproduce the above copyright notice,
  13. this list of conditions and the following disclaimer in the documentation
  14. and/or other materials provided with the distribution.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  16. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  19. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  22. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
  25. (function(_global) {
  26. "use strict";
  27. var shim = {};
  28. if (typeof(exports) === 'undefined') {
  29. if(typeof define == 'function' && typeof define.amd == 'object' && define.amd) {
  30. shim.exports = {};
  31. define(function() {
  32. return shim.exports;
  33. });
  34. } else {
  35. // gl-matrix lives in a browser, define its namespaces in global
  36. shim.exports = typeof(window) !== 'undefined' ? window : _global;
  37. }
  38. }
  39. else {
  40. // gl-matrix lives in commonjs, define its namespaces in exports
  41. shim.exports = exports;
  42. }
  43. (function(exports) {
  44. /* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved.
  45. Redistribution and use in source and binary forms, with or without modification,
  46. are permitted provided that the following conditions are met:
  47. * Redistributions of source code must retain the above copyright notice, this
  48. list of conditions and the following disclaimer.
  49. * Redistributions in binary form must reproduce the above copyright notice,
  50. this list of conditions and the following disclaimer in the documentation
  51. and/or other materials provided with the distribution.
  52. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  53. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  54. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  55. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  56. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  57. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  58. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  59. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  60. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  61. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
  62. if(!GLMAT_EPSILON) {
  63. var GLMAT_EPSILON = 0.000001;
  64. }
  65. if(!GLMAT_ARRAY_TYPE) {
  66. var GLMAT_ARRAY_TYPE = (typeof Float32Array !== 'undefined') ? Float32Array : Array;
  67. }
  68. if(!GLMAT_RANDOM) {
  69. var GLMAT_RANDOM = Math.random;
  70. }
  71. /**
  72. * @class Common utilities
  73. * @name glMatrix
  74. */
  75. var glMatrix = {};
  76. /**
  77. * Sets the type of array used when creating new vectors and matrices
  78. *
  79. * @param {Type} type Array type, such as Float32Array or Array
  80. */
  81. glMatrix.setMatrixArrayType = function(type) {
  82. GLMAT_ARRAY_TYPE = type;
  83. }
  84. if(typeof(exports) !== 'undefined') {
  85. exports.glMatrix = glMatrix;
  86. }
  87. var degree = Math.PI / 180;
  88. /**
  89. * Convert Degree To Radian
  90. *
  91. * @param {Number} Angle in Degrees
  92. */
  93. glMatrix.toRadian = function(a){
  94. return a * degree;
  95. }
  96. ;
  97. /* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved.
  98. Redistribution and use in source and binary forms, with or without modification,
  99. are permitted provided that the following conditions are met:
  100. * Redistributions of source code must retain the above copyright notice, this
  101. list of conditions and the following disclaimer.
  102. * Redistributions in binary form must reproduce the above copyright notice,
  103. this list of conditions and the following disclaimer in the documentation
  104. and/or other materials provided with the distribution.
  105. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  106. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  107. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  108. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  109. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  110. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  111. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  112. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  113. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  114. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
  115. /**
  116. * @class 2 Dimensional Vector
  117. * @name vec2
  118. */
  119. var vec2 = {};
  120. /**
  121. * Creates a new, empty vec2
  122. *
  123. * @returns {vec2} a new 2D vector
  124. */
  125. vec2.create = function() {
  126. var out = new GLMAT_ARRAY_TYPE(2);
  127. out[0] = 0;
  128. out[1] = 0;
  129. return out;
  130. };
  131. /**
  132. * Creates a new vec2 initialized with values from an existing vector
  133. *
  134. * @param {vec2} a vector to clone
  135. * @returns {vec2} a new 2D vector
  136. */
  137. vec2.clone = function(a) {
  138. var out = new GLMAT_ARRAY_TYPE(2);
  139. out[0] = a[0];
  140. out[1] = a[1];
  141. return out;
  142. };
  143. /**
  144. * Creates a new vec2 initialized with the given values
  145. *
  146. * @param {Number} x X component
  147. * @param {Number} y Y component
  148. * @returns {vec2} a new 2D vector
  149. */
  150. vec2.fromValues = function(x, y) {
  151. var out = new GLMAT_ARRAY_TYPE(2);
  152. out[0] = x;
  153. out[1] = y;
  154. return out;
  155. };
  156. /**
  157. * Copy the values from one vec2 to another
  158. *
  159. * @param {vec2} out the receiving vector
  160. * @param {vec2} a the source vector
  161. * @returns {vec2} out
  162. */
  163. vec2.copy = function(out, a) {
  164. out[0] = a[0];
  165. out[1] = a[1];
  166. return out;
  167. };
  168. /**
  169. * Set the components of a vec2 to the given values
  170. *
  171. * @param {vec2} out the receiving vector
  172. * @param {Number} x X component
  173. * @param {Number} y Y component
  174. * @returns {vec2} out
  175. */
  176. vec2.set = function(out, x, y) {
  177. out[0] = x;
  178. out[1] = y;
  179. return out;
  180. };
  181. /**
  182. * Adds two vec2's
  183. *
  184. * @param {vec2} out the receiving vector
  185. * @param {vec2} a the first operand
  186. * @param {vec2} b the second operand
  187. * @returns {vec2} out
  188. */
  189. vec2.add = function(out, a, b) {
  190. out[0] = a[0] + b[0];
  191. out[1] = a[1] + b[1];
  192. return out;
  193. };
  194. /**
  195. * Subtracts vector b from vector a
  196. *
  197. * @param {vec2} out the receiving vector
  198. * @param {vec2} a the first operand
  199. * @param {vec2} b the second operand
  200. * @returns {vec2} out
  201. */
  202. vec2.subtract = function(out, a, b) {
  203. out[0] = a[0] - b[0];
  204. out[1] = a[1] - b[1];
  205. return out;
  206. };
  207. /**
  208. * Alias for {@link vec2.subtract}
  209. * @function
  210. */
  211. vec2.sub = vec2.subtract;
  212. /**
  213. * Multiplies two vec2's
  214. *
  215. * @param {vec2} out the receiving vector
  216. * @param {vec2} a the first operand
  217. * @param {vec2} b the second operand
  218. * @returns {vec2} out
  219. */
  220. vec2.multiply = function(out, a, b) {
  221. out[0] = a[0] * b[0];
  222. out[1] = a[1] * b[1];
  223. return out;
  224. };
  225. /**
  226. * Alias for {@link vec2.multiply}
  227. * @function
  228. */
  229. vec2.mul = vec2.multiply;
  230. /**
  231. * Divides two vec2's
  232. *
  233. * @param {vec2} out the receiving vector
  234. * @param {vec2} a the first operand
  235. * @param {vec2} b the second operand
  236. * @returns {vec2} out
  237. */
  238. vec2.divide = function(out, a, b) {
  239. out[0] = a[0] / b[0];
  240. out[1] = a[1] / b[1];
  241. return out;
  242. };
  243. /**
  244. * Alias for {@link vec2.divide}
  245. * @function
  246. */
  247. vec2.div = vec2.divide;
  248. /**
  249. * Returns the minimum of two vec2's
  250. *
  251. * @param {vec2} out the receiving vector
  252. * @param {vec2} a the first operand
  253. * @param {vec2} b the second operand
  254. * @returns {vec2} out
  255. */
  256. vec2.min = function(out, a, b) {
  257. out[0] = Math.min(a[0], b[0]);
  258. out[1] = Math.min(a[1], b[1]);
  259. return out;
  260. };
  261. /**
  262. * Returns the maximum of two vec2's
  263. *
  264. * @param {vec2} out the receiving vector
  265. * @param {vec2} a the first operand
  266. * @param {vec2} b the second operand
  267. * @returns {vec2} out
  268. */
  269. vec2.max = function(out, a, b) {
  270. out[0] = Math.max(a[0], b[0]);
  271. out[1] = Math.max(a[1], b[1]);
  272. return out;
  273. };
  274. /**
  275. * Scales a vec2 by a scalar number
  276. *
  277. * @param {vec2} out the receiving vector
  278. * @param {vec2} a the vector to scale
  279. * @param {Number} b amount to scale the vector by
  280. * @returns {vec2} out
  281. */
  282. vec2.scale = function(out, a, b) {
  283. out[0] = a[0] * b;
  284. out[1] = a[1] * b;
  285. return out;
  286. };
  287. /**
  288. * Adds two vec2's after scaling the second operand by a scalar value
  289. *
  290. * @param {vec2} out the receiving vector
  291. * @param {vec2} a the first operand
  292. * @param {vec2} b the second operand
  293. * @param {Number} scale the amount to scale b by before adding
  294. * @returns {vec2} out
  295. */
  296. vec2.scaleAndAdd = function(out, a, b, scale) {
  297. out[0] = a[0] + (b[0] * scale);
  298. out[1] = a[1] + (b[1] * scale);
  299. return out;
  300. };
  301. /**
  302. * Calculates the euclidian distance between two vec2's
  303. *
  304. * @param {vec2} a the first operand
  305. * @param {vec2} b the second operand
  306. * @returns {Number} distance between a and b
  307. */
  308. vec2.distance = function(a, b) {
  309. var x = b[0] - a[0],
  310. y = b[1] - a[1];
  311. return Math.sqrt(x*x + y*y);
  312. };
  313. /**
  314. * Alias for {@link vec2.distance}
  315. * @function
  316. */
  317. vec2.dist = vec2.distance;
  318. /**
  319. * Calculates the squared euclidian distance between two vec2's
  320. *
  321. * @param {vec2} a the first operand
  322. * @param {vec2} b the second operand
  323. * @returns {Number} squared distance between a and b
  324. */
  325. vec2.squaredDistance = function(a, b) {
  326. var x = b[0] - a[0],
  327. y = b[1] - a[1];
  328. return x*x + y*y;
  329. };
  330. /**
  331. * Alias for {@link vec2.squaredDistance}
  332. * @function
  333. */
  334. vec2.sqrDist = vec2.squaredDistance;
  335. /**
  336. * Calculates the length of a vec2
  337. *
  338. * @param {vec2} a vector to calculate length of
  339. * @returns {Number} length of a
  340. */
  341. vec2.length = function (a) {
  342. var x = a[0],
  343. y = a[1];
  344. return Math.sqrt(x*x + y*y);
  345. };
  346. /**
  347. * Alias for {@link vec2.length}
  348. * @function
  349. */
  350. vec2.len = vec2.length;
  351. /**
  352. * Calculates the squared length of a vec2
  353. *
  354. * @param {vec2} a vector to calculate squared length of
  355. * @returns {Number} squared length of a
  356. */
  357. vec2.squaredLength = function (a) {
  358. var x = a[0],
  359. y = a[1];
  360. return x*x + y*y;
  361. };
  362. /**
  363. * Alias for {@link vec2.squaredLength}
  364. * @function
  365. */
  366. vec2.sqrLen = vec2.squaredLength;
  367. /**
  368. * Negates the components of a vec2
  369. *
  370. * @param {vec2} out the receiving vector
  371. * @param {vec2} a vector to negate
  372. * @returns {vec2} out
  373. */
  374. vec2.negate = function(out, a) {
  375. out[0] = -a[0];
  376. out[1] = -a[1];
  377. return out;
  378. };
  379. /**
  380. * Returns the inverse of the components of a vec2
  381. *
  382. * @param {vec2} out the receiving vector
  383. * @param {vec2} a vector to invert
  384. * @returns {vec2} out
  385. */
  386. vec2.inverse = function(out, a) {
  387. out[0] = 1.0 / a[0];
  388. out[1] = 1.0 / a[1];
  389. return out;
  390. };
  391. /**
  392. * Normalize a vec2
  393. *
  394. * @param {vec2} out the receiving vector
  395. * @param {vec2} a vector to normalize
  396. * @returns {vec2} out
  397. */
  398. vec2.normalize = function(out, a) {
  399. var x = a[0],
  400. y = a[1];
  401. var len = x*x + y*y;
  402. if (len > 0) {
  403. //TODO: evaluate use of glm_invsqrt here?
  404. len = 1 / Math.sqrt(len);
  405. out[0] = a[0] * len;
  406. out[1] = a[1] * len;
  407. }
  408. return out;
  409. };
  410. /**
  411. * Calculates the dot product of two vec2's
  412. *
  413. * @param {vec2} a the first operand
  414. * @param {vec2} b the second operand
  415. * @returns {Number} dot product of a and b
  416. */
  417. vec2.dot = function (a, b) {
  418. return a[0] * b[0] + a[1] * b[1];
  419. };
  420. /**
  421. * Computes the cross product of two vec2's
  422. * Note that the cross product must by definition produce a 3D vector
  423. *
  424. * @param {vec3} out the receiving vector
  425. * @param {vec2} a the first operand
  426. * @param {vec2} b the second operand
  427. * @returns {vec3} out
  428. */
  429. vec2.cross = function(out, a, b) {
  430. var z = a[0] * b[1] - a[1] * b[0];
  431. out[0] = out[1] = 0;
  432. out[2] = z;
  433. return out;
  434. };
  435. /**
  436. * Performs a linear interpolation between two vec2's
  437. *
  438. * @param {vec2} out the receiving vector
  439. * @param {vec2} a the first operand
  440. * @param {vec2} b the second operand
  441. * @param {Number} t interpolation amount between the two inputs
  442. * @returns {vec2} out
  443. */
  444. vec2.lerp = function (out, a, b, t) {
  445. var ax = a[0],
  446. ay = a[1];
  447. out[0] = ax + t * (b[0] - ax);
  448. out[1] = ay + t * (b[1] - ay);
  449. return out;
  450. };
  451. /**
  452. * Generates a random vector with the given scale
  453. *
  454. * @param {vec2} out the receiving vector
  455. * @param {Number} [scale] Length of the resulting vector. If ommitted, a unit vector will be returned
  456. * @returns {vec2} out
  457. */
  458. vec2.random = function (out, scale) {
  459. scale = scale || 1.0;
  460. var r = GLMAT_RANDOM() * 2.0 * Math.PI;
  461. out[0] = Math.cos(r) * scale;
  462. out[1] = Math.sin(r) * scale;
  463. return out;
  464. };
  465. /**
  466. * Transforms the vec2 with a mat2
  467. *
  468. * @param {vec2} out the receiving vector
  469. * @param {vec2} a the vector to transform
  470. * @param {mat2} m matrix to transform with
  471. * @returns {vec2} out
  472. */
  473. vec2.transformMat2 = function(out, a, m) {
  474. var x = a[0],
  475. y = a[1];
  476. out[0] = m[0] * x + m[2] * y;
  477. out[1] = m[1] * x + m[3] * y;
  478. return out;
  479. };
  480. /**
  481. * Transforms the vec2 with a mat2d
  482. *
  483. * @param {vec2} out the receiving vector
  484. * @param {vec2} a the vector to transform
  485. * @param {mat2d} m matrix to transform with
  486. * @returns {vec2} out
  487. */
  488. vec2.transformMat2d = function(out, a, m) {
  489. var x = a[0],
  490. y = a[1];
  491. out[0] = m[0] * x + m[2] * y + m[4];
  492. out[1] = m[1] * x + m[3] * y + m[5];
  493. return out;
  494. };
  495. /**
  496. * Transforms the vec2 with a mat3
  497. * 3rd vector component is implicitly '1'
  498. *
  499. * @param {vec2} out the receiving vector
  500. * @param {vec2} a the vector to transform
  501. * @param {mat3} m matrix to transform with
  502. * @returns {vec2} out
  503. */
  504. vec2.transformMat3 = function(out, a, m) {
  505. var x = a[0],
  506. y = a[1];
  507. out[0] = m[0] * x + m[3] * y + m[6];
  508. out[1] = m[1] * x + m[4] * y + m[7];
  509. return out;
  510. };
  511. /**
  512. * Transforms the vec2 with a mat4
  513. * 3rd vector component is implicitly '0'
  514. * 4th vector component is implicitly '1'
  515. *
  516. * @param {vec2} out the receiving vector
  517. * @param {vec2} a the vector to transform
  518. * @param {mat4} m matrix to transform with
  519. * @returns {vec2} out
  520. */
  521. vec2.transformMat4 = function(out, a, m) {
  522. var x = a[0],
  523. y = a[1];
  524. out[0] = m[0] * x + m[4] * y + m[12];
  525. out[1] = m[1] * x + m[5] * y + m[13];
  526. return out;
  527. };
  528. /**
  529. * Perform some operation over an array of vec2s.
  530. *
  531. * @param {Array} a the array of vectors to iterate over
  532. * @param {Number} stride Number of elements between the start of each vec2. If 0 assumes tightly packed
  533. * @param {Number} offset Number of elements to skip at the beginning of the array
  534. * @param {Number} count Number of vec2s to iterate over. If 0 iterates over entire array
  535. * @param {Function} fn Function to call for each vector in the array
  536. * @param {Object} [arg] additional argument to pass to fn
  537. * @returns {Array} a
  538. * @function
  539. */
  540. vec2.forEach = (function() {
  541. var vec = vec2.create();
  542. return function(a, stride, offset, count, fn, arg) {
  543. var i, l;
  544. if(!stride) {
  545. stride = 2;
  546. }
  547. if(!offset) {
  548. offset = 0;
  549. }
  550. if(count) {
  551. l = Math.min((count * stride) + offset, a.length);
  552. } else {
  553. l = a.length;
  554. }
  555. for(i = offset; i < l; i += stride) {
  556. vec[0] = a[i]; vec[1] = a[i+1];
  557. fn(vec, vec, arg);
  558. a[i] = vec[0]; a[i+1] = vec[1];
  559. }
  560. return a;
  561. };
  562. })();
  563. /**
  564. * Returns a string representation of a vector
  565. *
  566. * @param {vec2} vec vector to represent as a string
  567. * @returns {String} string representation of the vector
  568. */
  569. vec2.str = function (a) {
  570. return 'vec2(' + a[0] + ', ' + a[1] + ')';
  571. };
  572. if(typeof(exports) !== 'undefined') {
  573. exports.vec2 = vec2;
  574. }
  575. ;
  576. /* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved.
  577. Redistribution and use in source and binary forms, with or without modification,
  578. are permitted provided that the following conditions are met:
  579. * Redistributions of source code must retain the above copyright notice, this
  580. list of conditions and the following disclaimer.
  581. * Redistributions in binary form must reproduce the above copyright notice,
  582. this list of conditions and the following disclaimer in the documentation
  583. and/or other materials provided with the distribution.
  584. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  585. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  586. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  587. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  588. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  589. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  590. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  591. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  592. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  593. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
  594. /**
  595. * @class 3 Dimensional Vector
  596. * @name vec3
  597. */
  598. var vec3 = {};
  599. /**
  600. * Creates a new, empty vec3
  601. *
  602. * @returns {vec3} a new 3D vector
  603. */
  604. vec3.create = function() {
  605. var out = new GLMAT_ARRAY_TYPE(3);
  606. out[0] = 0;
  607. out[1] = 0;
  608. out[2] = 0;
  609. return out;
  610. };
  611. /**
  612. * Creates a new vec3 initialized with values from an existing vector
  613. *
  614. * @param {vec3} a vector to clone
  615. * @returns {vec3} a new 3D vector
  616. */
  617. vec3.clone = function(a) {
  618. var out = new GLMAT_ARRAY_TYPE(3);
  619. out[0] = a[0];
  620. out[1] = a[1];
  621. out[2] = a[2];
  622. return out;
  623. };
  624. /**
  625. * Creates a new vec3 initialized with the given values
  626. *
  627. * @param {Number} x X component
  628. * @param {Number} y Y component
  629. * @param {Number} z Z component
  630. * @returns {vec3} a new 3D vector
  631. */
  632. vec3.fromValues = function(x, y, z) {
  633. var out = new GLMAT_ARRAY_TYPE(3);
  634. out[0] = x;
  635. out[1] = y;
  636. out[2] = z;
  637. return out;
  638. };
  639. /**
  640. * Copy the values from one vec3 to another
  641. *
  642. * @param {vec3} out the receiving vector
  643. * @param {vec3} a the source vector
  644. * @returns {vec3} out
  645. */
  646. vec3.copy = function(out, a) {
  647. out[0] = a[0];
  648. out[1] = a[1];
  649. out[2] = a[2];
  650. return out;
  651. };
  652. /**
  653. * Set the components of a vec3 to the given values
  654. *
  655. * @param {vec3} out the receiving vector
  656. * @param {Number} x X component
  657. * @param {Number} y Y component
  658. * @param {Number} z Z component
  659. * @returns {vec3} out
  660. */
  661. vec3.set = function(out, x, y, z) {
  662. out[0] = x;
  663. out[1] = y;
  664. out[2] = z;
  665. return out;
  666. };
  667. /**
  668. * Adds two vec3's
  669. *
  670. * @param {vec3} out the receiving vector
  671. * @param {vec3} a the first operand
  672. * @param {vec3} b the second operand
  673. * @returns {vec3} out
  674. */
  675. vec3.add = function(out, a, b) {
  676. out[0] = a[0] + b[0];
  677. out[1] = a[1] + b[1];
  678. out[2] = a[2] + b[2];
  679. return out;
  680. };
  681. /**
  682. * Subtracts vector b from vector a
  683. *
  684. * @param {vec3} out the receiving vector
  685. * @param {vec3} a the first operand
  686. * @param {vec3} b the second operand
  687. * @returns {vec3} out
  688. */
  689. vec3.subtract = function(out, a, b) {
  690. out[0] = a[0] - b[0];
  691. out[1] = a[1] - b[1];
  692. out[2] = a[2] - b[2];
  693. return out;
  694. };
  695. /**
  696. * Alias for {@link vec3.subtract}
  697. * @function
  698. */
  699. vec3.sub = vec3.subtract;
  700. /**
  701. * Multiplies two vec3's
  702. *
  703. * @param {vec3} out the receiving vector
  704. * @param {vec3} a the first operand
  705. * @param {vec3} b the second operand
  706. * @returns {vec3} out
  707. */
  708. vec3.multiply = function(out, a, b) {
  709. out[0] = a[0] * b[0];
  710. out[1] = a[1] * b[1];
  711. out[2] = a[2] * b[2];
  712. return out;
  713. };
  714. /**
  715. * Alias for {@link vec3.multiply}
  716. * @function
  717. */
  718. vec3.mul = vec3.multiply;
  719. /**
  720. * Divides two vec3's
  721. *
  722. * @param {vec3} out the receiving vector
  723. * @param {vec3} a the first operand
  724. * @param {vec3} b the second operand
  725. * @returns {vec3} out
  726. */
  727. vec3.divide = function(out, a, b) {
  728. out[0] = a[0] / b[0];
  729. out[1] = a[1] / b[1];
  730. out[2] = a[2] / b[2];
  731. return out;
  732. };
  733. /**
  734. * Alias for {@link vec3.divide}
  735. * @function
  736. */
  737. vec3.div = vec3.divide;
  738. /**
  739. * Returns the minimum of two vec3's
  740. *
  741. * @param {vec3} out the receiving vector
  742. * @param {vec3} a the first operand
  743. * @param {vec3} b the second operand
  744. * @returns {vec3} out
  745. */
  746. vec3.min = function(out, a, b) {
  747. out[0] = Math.min(a[0], b[0]);
  748. out[1] = Math.min(a[1], b[1]);
  749. out[2] = Math.min(a[2], b[2]);
  750. return out;
  751. };
  752. /**
  753. * Returns the maximum of two vec3's
  754. *
  755. * @param {vec3} out the receiving vector
  756. * @param {vec3} a the first operand
  757. * @param {vec3} b the second operand
  758. * @returns {vec3} out
  759. */
  760. vec3.max = function(out, a, b) {
  761. out[0] = Math.max(a[0], b[0]);
  762. out[1] = Math.max(a[1], b[1]);
  763. out[2] = Math.max(a[2], b[2]);
  764. return out;
  765. };
  766. /**
  767. * Scales a vec3 by a scalar number
  768. *
  769. * @param {vec3} out the receiving vector
  770. * @param {vec3} a the vector to scale
  771. * @param {Number} b amount to scale the vector by
  772. * @returns {vec3} out
  773. */
  774. vec3.scale = function(out, a, b) {
  775. out[0] = a[0] * b;
  776. out[1] = a[1] * b;
  777. out[2] = a[2] * b;
  778. return out;
  779. };
  780. /**
  781. * Adds two vec3's after scaling the second operand by a scalar value
  782. *
  783. * @param {vec3} out the receiving vector
  784. * @param {vec3} a the first operand
  785. * @param {vec3} b the second operand
  786. * @param {Number} scale the amount to scale b by before adding
  787. * @returns {vec3} out
  788. */
  789. vec3.scaleAndAdd = function(out, a, b, scale) {
  790. out[0] = a[0] + (b[0] * scale);
  791. out[1] = a[1] + (b[1] * scale);
  792. out[2] = a[2] + (b[2] * scale);
  793. return out;
  794. };
  795. /**
  796. * Calculates the euclidian distance between two vec3's
  797. *
  798. * @param {vec3} a the first operand
  799. * @param {vec3} b the second operand
  800. * @returns {Number} distance between a and b
  801. */
  802. vec3.distance = function(a, b) {
  803. var x = b[0] - a[0],
  804. y = b[1] - a[1],
  805. z = b[2] - a[2];
  806. return Math.sqrt(x*x + y*y + z*z);
  807. };
  808. /**
  809. * Alias for {@link vec3.distance}
  810. * @function
  811. */
  812. vec3.dist = vec3.distance;
  813. /**
  814. * Calculates the squared euclidian distance between two vec3's
  815. *
  816. * @param {vec3} a the first operand
  817. * @param {vec3} b the second operand
  818. * @returns {Number} squared distance between a and b
  819. */
  820. vec3.squaredDistance = function(a, b) {
  821. var x = b[0] - a[0],
  822. y = b[1] - a[1],
  823. z = b[2] - a[2];
  824. return x*x + y*y + z*z;
  825. };
  826. /**
  827. * Alias for {@link vec3.squaredDistance}
  828. * @function
  829. */
  830. vec3.sqrDist = vec3.squaredDistance;
  831. /**
  832. * Calculates the length of a vec3
  833. *
  834. * @param {vec3} a vector to calculate length of
  835. * @returns {Number} length of a
  836. */
  837. vec3.length = function (a) {
  838. var x = a[0],
  839. y = a[1],
  840. z = a[2];
  841. return Math.sqrt(x*x + y*y + z*z);
  842. };
  843. /**
  844. * Alias for {@link vec3.length}
  845. * @function
  846. */
  847. vec3.len = vec3.length;
  848. /**
  849. * Calculates the squared length of a vec3
  850. *
  851. * @param {vec3} a vector to calculate squared length of
  852. * @returns {Number} squared length of a
  853. */
  854. vec3.squaredLength = function (a) {
  855. var x = a[0],
  856. y = a[1],
  857. z = a[2];
  858. return x*x + y*y + z*z;
  859. };
  860. /**
  861. * Alias for {@link vec3.squaredLength}
  862. * @function
  863. */
  864. vec3.sqrLen = vec3.squaredLength;
  865. /**
  866. * Negates the components of a vec3
  867. *
  868. * @param {vec3} out the receiving vector
  869. * @param {vec3} a vector to negate
  870. * @returns {vec3} out
  871. */
  872. vec3.negate = function(out, a) {
  873. out[0] = -a[0];
  874. out[1] = -a[1];
  875. out[2] = -a[2];
  876. return out;
  877. };
  878. /**
  879. * Returns the inverse of the components of a vec3
  880. *
  881. * @param {vec3} out the receiving vector
  882. * @param {vec3} a vector to invert
  883. * @returns {vec3} out
  884. */
  885. vec3.inverse = function(out, a) {
  886. out[0] = 1.0 / a[0];
  887. out[1] = 1.0 / a[1];
  888. out[2] = 1.0 / a[2];
  889. return out;
  890. };
  891. /**
  892. * Normalize a vec3
  893. *
  894. * @param {vec3} out the receiving vector
  895. * @param {vec3} a vector to normalize
  896. * @returns {vec3} out
  897. */
  898. vec3.normalize = function(out, a) {
  899. var x = a[0],
  900. y = a[1],
  901. z = a[2];
  902. var len = x*x + y*y + z*z;
  903. if (len > 0) {
  904. //TODO: evaluate use of glm_invsqrt here?
  905. len = 1 / Math.sqrt(len);
  906. out[0] = a[0] * len;
  907. out[1] = a[1] * len;
  908. out[2] = a[2] * len;
  909. }
  910. return out;
  911. };
  912. /**
  913. * Calculates the dot product of two vec3's
  914. *
  915. * @param {vec3} a the first operand
  916. * @param {vec3} b the second operand
  917. * @returns {Number} dot product of a and b
  918. */
  919. vec3.dot = function (a, b) {
  920. return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
  921. };
  922. /**
  923. * Computes the cross product of two vec3's
  924. *
  925. * @param {vec3} out the receiving vector
  926. * @param {vec3} a the first operand
  927. * @param {vec3} b the second operand
  928. * @returns {vec3} out
  929. */
  930. vec3.cross = function(out, a, b) {
  931. var ax = a[0], ay = a[1], az = a[2],
  932. bx = b[0], by = b[1], bz = b[2];
  933. out[0] = ay * bz - az * by;
  934. out[1] = az * bx - ax * bz;
  935. out[2] = ax * by - ay * bx;
  936. return out;
  937. };
  938. /**
  939. * Performs a linear interpolation between two vec3's
  940. *
  941. * @param {vec3} out the receiving vector
  942. * @param {vec3} a the first operand
  943. * @param {vec3} b the second operand
  944. * @param {Number} t interpolation amount between the two inputs
  945. * @returns {vec3} out
  946. */
  947. vec3.lerp = function (out, a, b, t) {
  948. var ax = a[0],
  949. ay = a[1],
  950. az = a[2];
  951. out[0] = ax + t * (b[0] - ax);
  952. out[1] = ay + t * (b[1] - ay);
  953. out[2] = az + t * (b[2] - az);
  954. return out;
  955. };
  956. /**
  957. * Generates a random vector with the given scale
  958. *
  959. * @param {vec3} out the receiving vector
  960. * @param {Number} [scale] Length of the resulting vector. If ommitted, a unit vector will be returned
  961. * @returns {vec3} out
  962. */
  963. vec3.random = function (out, scale) {
  964. scale = scale || 1.0;
  965. var r = GLMAT_RANDOM() * 2.0 * Math.PI;
  966. var z = (GLMAT_RANDOM() * 2.0) - 1.0;
  967. var zScale = Math.sqrt(1.0-z*z) * scale;
  968. out[0] = Math.cos(r) * zScale;
  969. out[1] = Math.sin(r) * zScale;
  970. out[2] = z * scale;
  971. return out;
  972. };
  973. /**
  974. * Transforms the vec3 with a mat4.
  975. * 4th vector component is implicitly '1'
  976. *
  977. * @param {vec3} out the receiving vector
  978. * @param {vec3} a the vector to transform
  979. * @param {mat4} m matrix to transform with
  980. * @returns {vec3} out
  981. */
  982. vec3.transformMat4 = function(out, a, m) {
  983. var x = a[0], y = a[1], z = a[2],
  984. w = m[3] * x + m[7] * y + m[11] * z + m[15];
  985. w = w || 1.0;
  986. out[0] = (m[0] * x + m[4] * y + m[8] * z + m[12]) / w;
  987. out[1] = (m[1] * x + m[5] * y + m[9] * z + m[13]) / w;
  988. out[2] = (m[2] * x + m[6] * y + m[10] * z + m[14]) / w;
  989. return out;
  990. };
  991. /**
  992. * Transforms the vec3 with a mat3.
  993. *
  994. * @param {vec3} out the receiving vector
  995. * @param {vec3} a the vector to transform
  996. * @param {mat4} m the 3x3 matrix to transform with
  997. * @returns {vec3} out
  998. */
  999. vec3.transformMat3 = function(out, a, m) {
  1000. var x = a[0], y = a[1], z = a[2];
  1001. out[0] = x * m[0] + y * m[3] + z * m[6];
  1002. out[1] = x * m[1] + y * m[4] + z * m[7];
  1003. out[2] = x * m[2] + y * m[5] + z * m[8];
  1004. return out;
  1005. };
  1006. /**
  1007. * Transforms the vec3 with a quat
  1008. *
  1009. * @param {vec3} out the receiving vector
  1010. * @param {vec3} a the vector to transform
  1011. * @param {quat} q quaternion to transform with
  1012. * @returns {vec3} out
  1013. */
  1014. vec3.transformQuat = function(out, a, q) {
  1015. // benchmarks: http://jsperf.com/quaternion-transform-vec3-implementations
  1016. var x = a[0], y = a[1], z = a[2],
  1017. qx = q[0], qy = q[1], qz = q[2], qw = q[3],
  1018. // calculate quat * vec
  1019. ix = qw * x + qy * z - qz * y,
  1020. iy = qw * y + qz * x - qx * z,
  1021. iz = qw * z + qx * y - qy * x,
  1022. iw = -qx * x - qy * y - qz * z;
  1023. // calculate result * inverse quat
  1024. out[0] = ix * qw + iw * -qx + iy * -qz - iz * -qy;
  1025. out[1] = iy * qw + iw * -qy + iz * -qx - ix * -qz;
  1026. out[2] = iz * qw + iw * -qz + ix * -qy - iy * -qx;
  1027. return out;
  1028. };
  1029. /**
  1030. * Rotate a 3D vector around the x-axis
  1031. * @param {vec3} out The receiving vec3
  1032. * @param {vec3} a The vec3 point to rotate
  1033. * @param {vec3} b The origin of the rotation
  1034. * @param {Number} c The angle of rotation
  1035. * @returns {vec3} out
  1036. */
  1037. vec3.rotateX = function(out, a, b, c){
  1038. var p = [], r=[];
  1039. //Translate point to the origin
  1040. p[0] = a[0] - b[0];
  1041. p[1] = a[1] - b[1];
  1042. p[2] = a[2] - b[2];
  1043. //perform rotation
  1044. r[0] = p[0];
  1045. r[1] = p[1]*Math.cos(c) - p[2]*Math.sin(c);
  1046. r[2] = p[1]*Math.sin(c) + p[2]*Math.cos(c);
  1047. //translate to correct position
  1048. out[0] = r[0] + b[0];
  1049. out[1] = r[1] + b[1];
  1050. out[2] = r[2] + b[2];
  1051. return out;
  1052. };
  1053. /**
  1054. * Rotate a 3D vector around the y-axis
  1055. * @param {vec3} out The receiving vec3
  1056. * @param {vec3} a The vec3 point to rotate
  1057. * @param {vec3} b The origin of the rotation
  1058. * @param {Number} c The angle of rotation
  1059. * @returns {vec3} out
  1060. */
  1061. vec3.rotateY = function(out, a, b, c){
  1062. var p = [], r=[];
  1063. //Translate point to the origin
  1064. p[0] = a[0] - b[0];
  1065. p[1] = a[1] - b[1];
  1066. p[2] = a[2] - b[2];
  1067. //perform rotation
  1068. r[0] = p[2]*Math.sin(c) + p[0]*Math.cos(c);
  1069. r[1] = p[1];
  1070. r[2] = p[2]*Math.cos(c) - p[0]*Math.sin(c);
  1071. //translate to correct position
  1072. out[0] = r[0] + b[0];
  1073. out[1] = r[1] + b[1];
  1074. out[2] = r[2] + b[2];
  1075. return out;
  1076. };
  1077. /**
  1078. * Rotate a 3D vector around the z-axis
  1079. * @param {vec3} out The receiving vec3
  1080. * @param {vec3} a The vec3 point to rotate
  1081. * @param {vec3} b The origin of the rotation
  1082. * @param {Number} c The angle of rotation
  1083. * @returns {vec3} out
  1084. */
  1085. vec3.rotateZ = function(out, a, b, c){
  1086. var p = [], r=[];
  1087. //Translate point to the origin
  1088. p[0] = a[0] - b[0];
  1089. p[1] = a[1] - b[1];
  1090. p[2] = a[2] - b[2];
  1091. //perform rotation
  1092. r[0] = p[0]*Math.cos(c) - p[1]*Math.sin(c);
  1093. r[1] = p[0]*Math.sin(c) + p[1]*Math.cos(c);
  1094. r[2] = p[2];
  1095. //translate to correct position
  1096. out[0] = r[0] + b[0];
  1097. out[1] = r[1] + b[1];
  1098. out[2] = r[2] + b[2];
  1099. return out;
  1100. };
  1101. /**
  1102. * Perform some operation over an array of vec3s.
  1103. *
  1104. * @param {Array} a the array of vectors to iterate over
  1105. * @param {Number} stride Number of elements between the start of each vec3. If 0 assumes tightly packed
  1106. * @param {Number} offset Number of elements to skip at the beginning of the array
  1107. * @param {Number} count Number of vec3s to iterate over. If 0 iterates over entire array
  1108. * @param {Function} fn Function to call for each vector in the array
  1109. * @param {Object} [arg] additional argument to pass to fn
  1110. * @returns {Array} a
  1111. * @function
  1112. */
  1113. vec3.forEach = (function() {
  1114. var vec = vec3.create();
  1115. return function(a, stride, offset, count, fn, arg) {
  1116. var i, l;
  1117. if(!stride) {
  1118. stride = 3;
  1119. }
  1120. if(!offset) {
  1121. offset = 0;
  1122. }
  1123. if(count) {
  1124. l = Math.min((count * stride) + offset, a.length);
  1125. } else {
  1126. l = a.length;
  1127. }
  1128. for(i = offset; i < l; i += stride) {
  1129. vec[0] = a[i]; vec[1] = a[i+1]; vec[2] = a[i+2];
  1130. fn(vec, vec, arg);
  1131. a[i] = vec[0]; a[i+1] = vec[1]; a[i+2] = vec[2];
  1132. }
  1133. return a;
  1134. };
  1135. })();
  1136. /**
  1137. * Returns a string representation of a vector
  1138. *
  1139. * @param {vec3} vec vector to represent as a string
  1140. * @returns {String} string representation of the vector
  1141. */
  1142. vec3.str = function (a) {
  1143. return 'vec3(' + a[0] + ', ' + a[1] + ', ' + a[2] + ')';
  1144. };
  1145. if(typeof(exports) !== 'undefined') {
  1146. exports.vec3 = vec3;
  1147. }
  1148. ;
  1149. /* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved.
  1150. Redistribution and use in source and binary forms, with or without modification,
  1151. are permitted provided that the following conditions are met:
  1152. * Redistributions of source code must retain the above copyright notice, this
  1153. list of conditions and the following disclaimer.
  1154. * Redistributions in binary form must reproduce the above copyright notice,
  1155. this list of conditions and the following disclaimer in the documentation
  1156. and/or other materials provided with the distribution.
  1157. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  1158. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  1159. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  1160. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  1161. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  1162. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  1163. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  1164. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  1165. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  1166. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
  1167. /**
  1168. * @class 4 Dimensional Vector
  1169. * @name vec4
  1170. */
  1171. var vec4 = {};
  1172. /**
  1173. * Creates a new, empty vec4
  1174. *
  1175. * @returns {vec4} a new 4D vector
  1176. */
  1177. vec4.create = function() {
  1178. var out = new GLMAT_ARRAY_TYPE(4);
  1179. out[0] = 0;
  1180. out[1] = 0;
  1181. out[2] = 0;
  1182. out[3] = 0;
  1183. return out;
  1184. };
  1185. /**
  1186. * Creates a new vec4 initialized with values from an existing vector
  1187. *
  1188. * @param {vec4} a vector to clone
  1189. * @returns {vec4} a new 4D vector
  1190. */
  1191. vec4.clone = function(a) {
  1192. var out = new GLMAT_ARRAY_TYPE(4);
  1193. out[0] = a[0];
  1194. out[1] = a[1];
  1195. out[2] = a[2];
  1196. out[3] = a[3];
  1197. return out;
  1198. };
  1199. /**
  1200. * Creates a new vec4 initialized with the given values
  1201. *
  1202. * @param {Number} x X component
  1203. * @param {Number} y Y component
  1204. * @param {Number} z Z component
  1205. * @param {Number} w W component
  1206. * @returns {vec4} a new 4D vector
  1207. */
  1208. vec4.fromValues = function(x, y, z, w) {
  1209. var out = new GLMAT_ARRAY_TYPE(4);
  1210. out[0] = x;
  1211. out[1] = y;
  1212. out[2] = z;
  1213. out[3] = w;
  1214. return out;
  1215. };
  1216. /**
  1217. * Copy the values from one vec4 to another
  1218. *
  1219. * @param {vec4} out the receiving vector
  1220. * @param {vec4} a the source vector
  1221. * @returns {vec4} out
  1222. */
  1223. vec4.copy = function(out, a) {
  1224. out[0] = a[0];
  1225. out[1] = a[1];
  1226. out[2] = a[2];
  1227. out[3] = a[3];
  1228. return out;
  1229. };
  1230. /**
  1231. * Set the components of a vec4 to the given values
  1232. *
  1233. * @param {vec4} out the receiving vector
  1234. * @param {Number} x X component
  1235. * @param {Number} y Y component
  1236. * @param {Number} z Z component
  1237. * @param {Number} w W component
  1238. * @returns {vec4} out
  1239. */
  1240. vec4.set = function(out, x, y, z, w) {
  1241. out[0] = x;
  1242. out[1] = y;
  1243. out[2] = z;
  1244. out[3] = w;
  1245. return out;
  1246. };
  1247. /**
  1248. * Adds two vec4's
  1249. *
  1250. * @param {vec4} out the receiving vector
  1251. * @param {vec4} a the first operand
  1252. * @param {vec4} b the second operand
  1253. * @returns {vec4} out
  1254. */
  1255. vec4.add = function(out, a, b) {
  1256. out[0] = a[0] + b[0];
  1257. out[1] = a[1] + b[1];
  1258. out[2] = a[2] + b[2];
  1259. out[3] = a[3] + b[3];
  1260. return out;
  1261. };
  1262. /**
  1263. * Subtracts vector b from vector a
  1264. *
  1265. * @param {vec4} out the receiving vector
  1266. * @param {vec4} a the first operand
  1267. * @param {vec4} b the second operand
  1268. * @returns {vec4} out
  1269. */
  1270. vec4.subtract = function(out, a, b) {
  1271. out[0] = a[0] - b[0];
  1272. out[1] = a[1] - b[1];
  1273. out[2] = a[2] - b[2];
  1274. out[3] = a[3] - b[3];
  1275. return out;
  1276. };
  1277. /**
  1278. * Alias for {@link vec4.subtract}
  1279. * @function
  1280. */
  1281. vec4.sub = vec4.subtract;
  1282. /**
  1283. * Multiplies two vec4's
  1284. *
  1285. * @param {vec4} out the receiving vector
  1286. * @param {vec4} a the first operand
  1287. * @param {vec4} b the second operand
  1288. * @returns {vec4} out
  1289. */
  1290. vec4.multiply = function(out, a, b) {
  1291. out[0] = a[0] * b[0];
  1292. out[1] = a[1] * b[1];
  1293. out[2] = a[2] * b[2];
  1294. out[3] = a[3] * b[3];
  1295. return out;
  1296. };
  1297. /**
  1298. * Alias for {@link vec4.multiply}
  1299. * @function
  1300. */
  1301. vec4.mul = vec4.multiply;
  1302. /**
  1303. * Divides two vec4's
  1304. *
  1305. * @param {vec4} out the receiving vector
  1306. * @param {vec4} a the first operand
  1307. * @param {vec4} b the second operand
  1308. * @returns {vec4} out
  1309. */
  1310. vec4.divide = function(out, a, b) {
  1311. out[0] = a[0] / b[0];
  1312. out[1] = a[1] / b[1];
  1313. out[2] = a[2] / b[2];
  1314. out[3] = a[3] / b[3];
  1315. return out;
  1316. };
  1317. /**
  1318. * Alias for {@link vec4.divide}
  1319. * @function
  1320. */
  1321. vec4.div = vec4.divide;
  1322. /**
  1323. * Returns the minimum of two vec4's
  1324. *
  1325. * @param {vec4} out the receiving vector
  1326. * @param {vec4} a the first operand
  1327. * @param {vec4} b the second operand
  1328. * @returns {vec4} out
  1329. */
  1330. vec4.min = function(out, a, b) {
  1331. out[0] = Math.min(a[0], b[0]);
  1332. out[1] = Math.min(a[1], b[1]);
  1333. out[2] = Math.min(a[2], b[2]);
  1334. out[3] = Math.min(a[3], b[3]);
  1335. return out;
  1336. };
  1337. /**
  1338. * Returns the maximum of two vec4's
  1339. *
  1340. * @param {vec4} out the receiving vector
  1341. * @param {vec4} a the first operand
  1342. * @param {vec4} b the second operand
  1343. * @returns {vec4} out
  1344. */
  1345. vec4.max = function(out, a, b) {
  1346. out[0] = Math.max(a[0], b[0]);
  1347. out[1] = Math.max(a[1], b[1]);
  1348. out[2] = Math.max(a[2], b[2]);
  1349. out[3] = Math.max(a[3], b[3]);
  1350. return out;
  1351. };
  1352. /**
  1353. * Scales a vec4 by a scalar number
  1354. *
  1355. * @param {vec4} out the receiving vector
  1356. * @param {vec4} a the vector to scale
  1357. * @param {Number} b amount to scale the vector by
  1358. * @returns {vec4} out
  1359. */
  1360. vec4.scale = function(out, a, b) {
  1361. out[0] = a[0] * b;
  1362. out[1] = a[1] * b;
  1363. out[2] = a[2] * b;
  1364. out[3] = a[3] * b;
  1365. return out;
  1366. };
  1367. /**
  1368. * Adds two vec4's after scaling the second operand by a scalar value
  1369. *
  1370. * @param {vec4} out the receiving vector
  1371. * @param {vec4} a the first operand
  1372. * @param {vec4} b the second operand
  1373. * @param {Number} scale the amount to scale b by before adding
  1374. * @returns {vec4} out
  1375. */
  1376. vec4.scaleAndAdd = function(out, a, b, scale) {
  1377. out[0] = a[0] + (b[0] * scale);
  1378. out[1] = a[1] + (b[1] * scale);
  1379. out[2] = a[2] + (b[2] * scale);
  1380. out[3] = a[3] + (b[3] * scale);
  1381. return out;
  1382. };
  1383. /**
  1384. * Calculates the euclidian distance between two vec4's
  1385. *
  1386. * @param {vec4} a the first operand
  1387. * @param {vec4} b the second operand
  1388. * @returns {Number} distance between a and b
  1389. */
  1390. vec4.distance = function(a, b) {
  1391. var x = b[0] - a[0],
  1392. y = b[1] - a[1],
  1393. z = b[2] - a[2],
  1394. w = b[3] - a[3];
  1395. return Math.sqrt(x*x + y*y + z*z + w*w);
  1396. };
  1397. /**
  1398. * Alias for {@link vec4.distance}
  1399. * @function
  1400. */
  1401. vec4.dist = vec4.distance;
  1402. /**
  1403. * Calculates the squared euclidian distance between two vec4's
  1404. *
  1405. * @param {vec4} a the first operand
  1406. * @param {vec4} b the second operand
  1407. * @returns {Number} squared distance between a and b
  1408. */
  1409. vec4.squaredDistance = function(a, b) {
  1410. var x = b[0] - a[0],
  1411. y = b[1] - a[1],
  1412. z = b[2] - a[2],
  1413. w = b[3] - a[3];
  1414. return x*x + y*y + z*z + w*w;
  1415. };
  1416. /**
  1417. * Alias for {@link vec4.squaredDistance}
  1418. * @function
  1419. */
  1420. vec4.sqrDist = vec4.squaredDistance;
  1421. /**
  1422. * Calculates the length of a vec4
  1423. *
  1424. * @param {vec4} a vector to calculate length of
  1425. * @returns {Number} length of a
  1426. */
  1427. vec4.length = function (a) {
  1428. var x = a[0],
  1429. y = a[1],
  1430. z = a[2],
  1431. w = a[3];
  1432. return Math.sqrt(x*x + y*y + z*z + w*w);
  1433. };
  1434. /**
  1435. * Alias for {@link vec4.length}
  1436. * @function
  1437. */
  1438. vec4.len = vec4.length;
  1439. /**
  1440. * Calculates the squared length of a vec4
  1441. *
  1442. * @param {vec4} a vector to calculate squared length of
  1443. * @returns {Number} squared length of a
  1444. */
  1445. vec4.squaredLength = function (a) {
  1446. var x = a[0],
  1447. y = a[1],
  1448. z = a[2],
  1449. w = a[3];
  1450. return x*x + y*y + z*z + w*w;
  1451. };
  1452. /**
  1453. * Alias for {@link vec4.squaredLength}
  1454. * @function
  1455. */
  1456. vec4.sqrLen = vec4.squaredLength;
  1457. /**
  1458. * Negates the components of a vec4
  1459. *
  1460. * @param {vec4} out the receiving vector
  1461. * @param {vec4} a vector to negate
  1462. * @returns {vec4} out
  1463. */
  1464. vec4.negate = function(out, a) {
  1465. out[0] = -a[0];
  1466. out[1] = -a[1];
  1467. out[2] = -a[2];
  1468. out[3] = -a[3];
  1469. return out;
  1470. };
  1471. /**
  1472. * Returns the inverse of the components of a vec4
  1473. *
  1474. * @param {vec4} out the receiving vector
  1475. * @param {vec4} a vector to invert
  1476. * @returns {vec4} out
  1477. */
  1478. vec4.inverse = function(out, a) {
  1479. out[0] = 1.0 / a[0];
  1480. out[1] = 1.0 / a[1];
  1481. out[2] = 1.0 / a[2];
  1482. out[3] = 1.0 / a[3];
  1483. return out;
  1484. };
  1485. /**
  1486. * Normalize a vec4
  1487. *
  1488. * @param {vec4} out the receiving vector
  1489. * @param {vec4} a vector to normalize
  1490. * @returns {vec4} out
  1491. */
  1492. vec4.normalize = function(out, a) {
  1493. var x = a[0],
  1494. y = a[1],
  1495. z = a[2],
  1496. w = a[3];
  1497. var len = x*x + y*y + z*z + w*w;
  1498. if (len > 0) {
  1499. len = 1 / Math.sqrt(len);
  1500. out[0] = a[0] * len;
  1501. out[1] = a[1] * len;
  1502. out[2] = a[2] * len;
  1503. out[3] = a[3] * len;
  1504. }
  1505. return out;
  1506. };
  1507. /**
  1508. * Calculates the dot product of two vec4's
  1509. *
  1510. * @param {vec4} a the first operand
  1511. * @param {vec4} b the second operand
  1512. * @returns {Number} dot product of a and b
  1513. */
  1514. vec4.dot = function (a, b) {
  1515. return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3];
  1516. };
  1517. /**
  1518. * Performs a linear interpolation between two vec4's
  1519. *
  1520. * @param {vec4} out the receiving vector
  1521. * @param {vec4} a the first operand
  1522. * @param {vec4} b the second operand
  1523. * @param {Number} t interpolation amount between the two inputs
  1524. * @returns {vec4} out
  1525. */
  1526. vec4.lerp = function (out, a, b, t) {
  1527. var ax = a[0],
  1528. ay = a[1],
  1529. az = a[2],
  1530. aw = a[3];
  1531. out[0] = ax + t * (b[0] - ax);
  1532. out[1] = ay + t * (b[1] - ay);
  1533. out[2] = az + t * (b[2] - az);
  1534. out[3] = aw + t * (b[3] - aw);
  1535. return out;
  1536. };
  1537. /**
  1538. * Generates a random vector with the given scale
  1539. *
  1540. * @param {vec4} out the receiving vector
  1541. * @param {Number} [scale] Length of the resulting vector. If ommitted, a unit vector will be returned
  1542. * @returns {vec4} out
  1543. */
  1544. vec4.random = function (out, scale) {
  1545. scale = scale || 1.0;
  1546. //TODO: This is a pretty awful way of doing this. Find something better.
  1547. out[0] = GLMAT_RANDOM();
  1548. out[1] = GLMAT_RANDOM();
  1549. out[2] = GLMAT_RANDOM();
  1550. out[3] = GLMAT_RANDOM();
  1551. vec4.normalize(out, out);
  1552. vec4.scale(out, out, scale);
  1553. return out;
  1554. };
  1555. /**
  1556. * Transforms the vec4 with a mat4.
  1557. *
  1558. * @param {vec4} out the receiving vector
  1559. * @param {vec4} a the vector to transform
  1560. * @param {mat4} m matrix to transform with
  1561. * @returns {vec4} out
  1562. */
  1563. vec4.transformMat4 = function(out, a, m) {
  1564. var x = a[0], y = a[1], z = a[2], w = a[3];
  1565. out[0] = m[0] * x + m[4] * y + m[8] * z + m[12] * w;
  1566. out[1] = m[1] * x + m[5] * y + m[9] * z + m[13] * w;
  1567. out[2] = m[2] * x + m[6] * y + m[10] * z + m[14] * w;
  1568. out[3] = m[3] * x + m[7] * y + m[11] * z + m[15] * w;
  1569. return out;
  1570. };
  1571. /**
  1572. * Transforms the vec4 with a quat
  1573. *
  1574. * @param {vec4} out the receiving vector
  1575. * @param {vec4} a the vector to transform
  1576. * @param {quat} q quaternion to transform with
  1577. * @returns {vec4} out
  1578. */
  1579. vec4.transformQuat = function(out, a, q) {
  1580. var x = a[0], y = a[1], z = a[2],
  1581. qx = q[0], qy = q[1], qz = q[2], qw = q[3],
  1582. // calculate quat * vec
  1583. ix = qw * x + qy * z - qz * y,
  1584. iy = qw * y + qz * x - qx * z,
  1585. iz = qw * z + qx * y - qy * x,
  1586. iw = -qx * x - qy * y - qz * z;
  1587. // calculate result * inverse quat
  1588. out[0] = ix * qw + iw * -qx + iy * -qz - iz * -qy;
  1589. out[1] = iy * qw + iw * -qy + iz * -qx - ix * -qz;
  1590. out[2] = iz * qw + iw * -qz + ix * -qy - iy * -qx;
  1591. return out;
  1592. };
  1593. /**
  1594. * Perform some operation over an array of vec4s.
  1595. *
  1596. * @param {Array} a the array of vectors to iterate over
  1597. * @param {Number} stride Number of elements between the start of each vec4. If 0 assumes tightly packed
  1598. * @param {Number} offset Number of elements to skip at the beginning of the array
  1599. * @param {Number} count Number of vec4s to iterate over. If 0 iterates over entire array
  1600. * @param {Function} fn Function to call for each vector in the array
  1601. * @param {Object} [arg] additional argument to pass to fn
  1602. * @returns {Array} a
  1603. * @function
  1604. */
  1605. vec4.forEach = (function() {
  1606. var vec = vec4.create();
  1607. return function(a, stride, offset, count, fn, arg) {
  1608. var i, l;
  1609. if(!stride) {
  1610. stride = 4;
  1611. }
  1612. if(!offset) {
  1613. offset = 0;
  1614. }
  1615. if(count) {
  1616. l = Math.min((count * stride) + offset, a.length);
  1617. } else {
  1618. l = a.length;
  1619. }
  1620. for(i = offset; i < l; i += stride) {
  1621. vec[0] = a[i]; vec[1] = a[i+1]; vec[2] = a[i+2]; vec[3] = a[i+3];
  1622. fn(vec, vec, arg);
  1623. a[i] = vec[0]; a[i+1] = vec[1]; a[i+2] = vec[2]; a[i+3] = vec[3];
  1624. }
  1625. return a;
  1626. };
  1627. })();
  1628. /**
  1629. * Returns a string representation of a vector
  1630. *
  1631. * @param {vec4} vec vector to represent as a string
  1632. * @returns {String} string representation of the vector
  1633. */
  1634. vec4.str = function (a) {
  1635. return 'vec4(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + a[3] + ')';
  1636. };
  1637. if(typeof(exports) !== 'undefined') {
  1638. exports.vec4 = vec4;
  1639. }
  1640. ;
  1641. /* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved.
  1642. Redistribution and use in source and binary forms, with or without modification,
  1643. are permitted provided that the following conditions are met:
  1644. * Redistributions of source code must retain the above copyright notice, this
  1645. list of conditions and the following disclaimer.
  1646. * Redistributions in binary form must reproduce the above copyright notice,
  1647. this list of conditions and the following disclaimer in the documentation
  1648. and/or other materials provided with the distribution.
  1649. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  1650. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  1651. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  1652. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  1653. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  1654. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  1655. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  1656. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  1657. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  1658. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
  1659. /**
  1660. * @class 2x2 Matrix
  1661. * @name mat2
  1662. */
  1663. var mat2 = {};
  1664. /**
  1665. * Creates a new identity mat2
  1666. *
  1667. * @returns {mat2} a new 2x2 matrix
  1668. */
  1669. mat2.create = function() {
  1670. var out = new GLMAT_ARRAY_TYPE(4);
  1671. out[0] = 1;
  1672. out[1] = 0;
  1673. out[2] = 0;
  1674. out[3] = 1;
  1675. return out;
  1676. };
  1677. /**
  1678. * Creates a new mat2 initialized with values from an existing matrix
  1679. *
  1680. * @param {mat2} a matrix to clone
  1681. * @returns {mat2} a new 2x2 matrix
  1682. */
  1683. mat2.clone = function(a) {
  1684. var out = new GLMAT_ARRAY_TYPE(4);
  1685. out[0] = a[0];
  1686. out[1] = a[1];
  1687. out[2] = a[2];
  1688. out[3] = a[3];
  1689. return out;
  1690. };
  1691. /**
  1692. * Copy the values from one mat2 to another
  1693. *
  1694. * @param {mat2} out the receiving matrix
  1695. * @param {mat2} a the source matrix
  1696. * @returns {mat2} out
  1697. */
  1698. mat2.copy = function(out, a) {
  1699. out[0] = a[0];
  1700. out[1] = a[1];
  1701. out[2] = a[2];
  1702. out[3] = a[3];
  1703. return out;
  1704. };
  1705. /**
  1706. * Set a mat2 to the identity matrix
  1707. *
  1708. * @param {mat2} out the receiving matrix
  1709. * @returns {mat2} out
  1710. */
  1711. mat2.identity = function(out) {
  1712. out[0] = 1;
  1713. out[1] = 0;
  1714. out[2] = 0;
  1715. out[3] = 1;
  1716. return out;
  1717. };
  1718. /**
  1719. * Transpose the values of a mat2
  1720. *
  1721. * @param {mat2} out the receiving matrix
  1722. * @param {mat2} a the source matrix
  1723. * @returns {mat2} out
  1724. */
  1725. mat2.transpose = function(out, a) {
  1726. // If we are transposing ourselves we can skip a few steps but have to cache some values
  1727. if (out === a) {
  1728. var a1 = a[1];
  1729. out[1] = a[2];
  1730. out[2] = a1;
  1731. } else {
  1732. out[0] = a[0];
  1733. out[1] = a[2];
  1734. out[2] = a[1];
  1735. out[3] = a[3];
  1736. }
  1737. return out;
  1738. };
  1739. /**
  1740. * Inverts a mat2
  1741. *
  1742. * @param {mat2} out the receiving matrix
  1743. * @param {mat2} a the source matrix
  1744. * @returns {mat2} out
  1745. */
  1746. mat2.invert = function(out, a) {
  1747. var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3],
  1748. // Calculate the determinant
  1749. det = a0 * a3 - a2 * a1;
  1750. if (!det) {
  1751. return null;
  1752. }
  1753. det = 1.0 / det;
  1754. out[0] = a3 * det;
  1755. out[1] = -a1 * det;
  1756. out[2] = -a2 * det;
  1757. out[3] = a0 * det;
  1758. return out;
  1759. };
  1760. /**
  1761. * Calculates the adjugate of a mat2
  1762. *
  1763. * @param {mat2} out the receiving matrix
  1764. * @param {mat2} a the source matrix
  1765. * @returns {mat2} out
  1766. */
  1767. mat2.adjoint = function(out, a) {
  1768. // Caching this value is nessecary if out == a
  1769. var a0 = a[0];
  1770. out[0] = a[3];
  1771. out[1] = -a[1];
  1772. out[2] = -a[2];
  1773. out[3] = a0;
  1774. return out;
  1775. };
  1776. /**
  1777. * Calculates the determinant of a mat2
  1778. *
  1779. * @param {mat2} a the source matrix
  1780. * @returns {Number} determinant of a
  1781. */
  1782. mat2.determinant = function (a) {
  1783. return a[0] * a[3] - a[2] * a[1];
  1784. };
  1785. /**
  1786. * Multiplies two mat2's
  1787. *
  1788. * @param {mat2} out the receiving matrix
  1789. * @param {mat2} a the first operand
  1790. * @param {mat2} b the second operand
  1791. * @returns {mat2} out
  1792. */
  1793. mat2.multiply = function (out, a, b) {
  1794. var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3];
  1795. var b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3];
  1796. out[0] = a0 * b0 + a2 * b1;
  1797. out[1] = a1 * b0 + a3 * b1;
  1798. out[2] = a0 * b2 + a2 * b3;
  1799. out[3] = a1 * b2 + a3 * b3;
  1800. return out;
  1801. };
  1802. /**
  1803. * Alias for {@link mat2.multiply}
  1804. * @function
  1805. */
  1806. mat2.mul = mat2.multiply;
  1807. /**
  1808. * Rotates a mat2 by the given angle
  1809. *
  1810. * @param {mat2} out the receiving matrix
  1811. * @param {mat2} a the matrix to rotate
  1812. * @param {Number} rad the angle to rotate the matrix by
  1813. * @returns {mat2} out
  1814. */
  1815. mat2.rotate = function (out, a, rad) {
  1816. var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3],
  1817. s = Math.sin(rad),
  1818. c = Math.cos(rad);
  1819. out[0] = a0 * c + a2 * s;
  1820. out[1] = a1 * c + a3 * s;
  1821. out[2] = a0 * -s + a2 * c;
  1822. out[3] = a1 * -s + a3 * c;
  1823. return out;
  1824. };
  1825. /**
  1826. * Scales the mat2 by the dimensions in the given vec2
  1827. *
  1828. * @param {mat2} out the receiving matrix
  1829. * @param {mat2} a the matrix to rotate
  1830. * @param {vec2} v the vec2 to scale the matrix by
  1831. * @returns {mat2} out
  1832. **/
  1833. mat2.scale = function(out, a, v) {
  1834. var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3],
  1835. v0 = v[0], v1 = v[1];
  1836. out[0] = a0 * v0;
  1837. out[1] = a1 * v0;
  1838. out[2] = a2 * v1;
  1839. out[3] = a3 * v1;
  1840. return out;
  1841. };
  1842. /**
  1843. * Returns a string representation of a mat2
  1844. *
  1845. * @param {mat2} mat matrix to represent as a string
  1846. * @returns {String} string representation of the matrix
  1847. */
  1848. mat2.str = function (a) {
  1849. return 'mat2(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + a[3] + ')';
  1850. };
  1851. /**
  1852. * Returns Frobenius norm of a mat2
  1853. *
  1854. * @param {mat2} a the matrix to calculate Frobenius norm of
  1855. * @returns {Number} Frobenius norm
  1856. */
  1857. mat2.frob = function (a) {
  1858. return(Math.sqrt(Math.pow(a[0], 2) + Math.pow(a[1], 2) + Math.pow(a[2], 2) + Math.pow(a[3], 2)))
  1859. };
  1860. /**
  1861. * Returns L, D and U matrices (Lower triangular, Diagonal and Upper triangular) by factorizing the input matrix
  1862. * @param {mat2} L the lower triangular matrix
  1863. * @param {mat2} D the diagonal matrix
  1864. * @param {mat2} U the upper triangular matrix
  1865. * @param {mat2} a the input matrix to factorize
  1866. */
  1867. mat2.LDU = function (L, D, U, a) {
  1868. L[2] = a[2]/a[0];
  1869. U[0] = a[0];
  1870. U[1] = a[1];
  1871. U[3] = a[3] - L[2] * U[1];
  1872. return [L, D, U];
  1873. };
  1874. if(typeof(exports) !== 'undefined') {
  1875. exports.mat2 = mat2;
  1876. }
  1877. ;
  1878. /* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved.
  1879. Redistribution and use in source and binary forms, with or without modification,
  1880. are permitted provided that the following conditions are met:
  1881. * Redistributions of source code must retain the above copyright notice, this
  1882. list of conditions and the following disclaimer.
  1883. * Redistributions in binary form must reproduce the above copyright notice,
  1884. this list of conditions and the following disclaimer in the documentation
  1885. and/or other materials provided with the distribution.
  1886. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  1887. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  1888. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  1889. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  1890. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  1891. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  1892. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  1893. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  1894. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  1895. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
  1896. /**
  1897. * @class 2x3 Matrix
  1898. * @name mat2d
  1899. *
  1900. * @description
  1901. * A mat2d contains six elements defined as:
  1902. * <pre>
  1903. * [a, c, tx,
  1904. * b, d, ty]
  1905. * </pre>
  1906. * This is a short form for the 3x3 matrix:
  1907. * <pre>
  1908. * [a, c, tx,
  1909. * b, d, ty,
  1910. * 0, 0, 1]
  1911. * </pre>
  1912. * The last row is ignored so the array is shorter and operations are faster.
  1913. */
  1914. var mat2d = {};
  1915. /**
  1916. * Creates a new identity mat2d
  1917. *
  1918. * @returns {mat2d} a new 2x3 matrix
  1919. */
  1920. mat2d.create = function() {
  1921. var out = new GLMAT_ARRAY_TYPE(6);
  1922. out[0] = 1;
  1923. out[1] = 0;
  1924. out[2] = 0;
  1925. out[3] = 1;
  1926. out[4] = 0;
  1927. out[5] = 0;
  1928. return out;
  1929. };
  1930. /**
  1931. * Creates a new mat2d initialized with values from an existing matrix
  1932. *
  1933. * @param {mat2d} a matrix to clone
  1934. * @returns {mat2d} a new 2x3 matrix
  1935. */
  1936. mat2d.clone = function(a) {
  1937. var out = new GLMAT_ARRAY_TYPE(6);
  1938. out[0] = a[0];
  1939. out[1] = a[1];
  1940. out[2] = a[2];
  1941. out[3] = a[3];
  1942. out[4] = a[4];
  1943. out[5] = a[5];
  1944. return out;
  1945. };
  1946. /**
  1947. * Copy the values from one mat2d to another
  1948. *
  1949. * @param {mat2d} out the receiving matrix
  1950. * @param {mat2d} a the source matrix
  1951. * @returns {mat2d} out
  1952. */
  1953. mat2d.copy = function(out, a) {
  1954. out[0] = a[0];
  1955. out[1] = a[1];
  1956. out[2] = a[2];
  1957. out[3] = a[3];
  1958. out[4] = a[4];
  1959. out[5] = a[5];
  1960. return out;
  1961. };
  1962. /**
  1963. * Set a mat2d to the identity matrix
  1964. *
  1965. * @param {mat2d} out the receiving matrix
  1966. * @returns {mat2d} out
  1967. */
  1968. mat2d.identity = function(out) {
  1969. out[0] = 1;
  1970. out[1] = 0;
  1971. out[2] = 0;
  1972. out[3] = 1;
  1973. out[4] = 0;
  1974. out[5] = 0;
  1975. return out;
  1976. };
  1977. /**
  1978. * Inverts a mat2d
  1979. *
  1980. * @param {mat2d} out the receiving matrix
  1981. * @param {mat2d} a the source matrix
  1982. * @returns {mat2d} out
  1983. */
  1984. mat2d.invert = function(out, a) {
  1985. var aa = a[0], ab = a[1], ac = a[2], ad = a[3],
  1986. atx = a[4], aty = a[5];
  1987. var det = aa * ad - ab * ac;
  1988. if(!det){
  1989. return null;
  1990. }
  1991. det = 1.0 / det;
  1992. out[0] = ad * det;
  1993. out[1] = -ab * det;
  1994. out[2] = -ac * det;
  1995. out[3] = aa * det;
  1996. out[4] = (ac * aty - ad * atx) * det;
  1997. out[5] = (ab * atx - aa * aty) * det;
  1998. return out;
  1999. };
  2000. /**
  2001. * Calculates the determinant of a mat2d
  2002. *
  2003. * @param {mat2d} a the source matrix
  2004. * @returns {Number} determinant of a
  2005. */
  2006. mat2d.determinant = function (a) {
  2007. return a[0] * a[3] - a[1] * a[2];
  2008. };
  2009. /**
  2010. * Multiplies two mat2d's
  2011. *
  2012. * @param {mat2d} out the receiving matrix
  2013. * @param {mat2d} a the first operand
  2014. * @param {mat2d} b the second operand
  2015. * @returns {mat2d} out
  2016. */
  2017. mat2d.multiply = function (out, a, b) {
  2018. var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], a4 = a[4], a5 = a[5],
  2019. b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3], b4 = b[4], b5 = b[5];
  2020. out[0] = a0 * b0 + a2 * b1;
  2021. out[1] = a1 * b0 + a3 * b1;
  2022. out[2] = a0 * b2 + a2 * b3;
  2023. out[3] = a1 * b2 + a3 * b3;
  2024. out[4] = a0 * b4 + a2 * b5 + a4;
  2025. out[5] = a1 * b4 + a3 * b5 + a5;
  2026. return out;
  2027. };
  2028. /**
  2029. * Alias for {@link mat2d.multiply}
  2030. * @function
  2031. */
  2032. mat2d.mul = mat2d.multiply;
  2033. /**
  2034. * Rotates a mat2d by the given angle
  2035. *
  2036. * @param {mat2d} out the receiving matrix
  2037. * @param {mat2d} a the matrix to rotate
  2038. * @param {Number} rad the angle to rotate the matrix by
  2039. * @returns {mat2d} out
  2040. */
  2041. mat2d.rotate = function (out, a, rad) {
  2042. var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], a4 = a[4], a5 = a[5],
  2043. s = Math.sin(rad),
  2044. c = Math.cos(rad);
  2045. out[0] = a0 * c + a2 * s;
  2046. out[1] = a1 * c + a3 * s;
  2047. out[2] = a0 * -s + a2 * c;
  2048. out[3] = a1 * -s + a3 * c;
  2049. out[4] = a4;
  2050. out[5] = a5;
  2051. return out;
  2052. };
  2053. /**
  2054. * Scales the mat2d by the dimensions in the given vec2
  2055. *
  2056. * @param {mat2d} out the receiving matrix
  2057. * @param {mat2d} a the matrix to translate
  2058. * @param {vec2} v the vec2 to scale the matrix by
  2059. * @returns {mat2d} out
  2060. **/
  2061. mat2d.scale = function(out, a, v) {
  2062. var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], a4 = a[4], a5 = a[5],
  2063. v0 = v[0], v1 = v[1];
  2064. out[0] = a0 * v0;
  2065. out[1] = a1 * v0;
  2066. out[2] = a2 * v1;
  2067. out[3] = a3 * v1;
  2068. out[4] = a4;
  2069. out[5] = a5;
  2070. return out;
  2071. };
  2072. /**
  2073. * Translates the mat2d by the dimensions in the given vec2
  2074. *
  2075. * @param {mat2d} out the receiving matrix
  2076. * @param {mat2d} a the matrix to translate
  2077. * @param {vec2} v the vec2 to translate the matrix by
  2078. * @returns {mat2d} out
  2079. **/
  2080. mat2d.translate = function(out, a, v) {
  2081. var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], a4 = a[4], a5 = a[5],
  2082. v0 = v[0], v1 = v[1];
  2083. out[0] = a0;
  2084. out[1] = a1;
  2085. out[2] = a2;
  2086. out[3] = a3;
  2087. out[4] = a0 * v0 + a2 * v1 + a4;
  2088. out[5] = a1 * v0 + a3 * v1 + a5;
  2089. return out;
  2090. };
  2091. /**
  2092. * Returns a string representation of a mat2d
  2093. *
  2094. * @param {mat2d} a matrix to represent as a string
  2095. * @returns {String} string representation of the matrix
  2096. */
  2097. mat2d.str = function (a) {
  2098. return 'mat2d(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' +
  2099. a[3] + ', ' + a[4] + ', ' + a[5] + ')';
  2100. };
  2101. /**
  2102. * Returns Frobenius norm of a mat2d
  2103. *
  2104. * @param {mat2d} a the matrix to calculate Frobenius norm of
  2105. * @returns {Number} Frobenius norm
  2106. */
  2107. mat2d.frob = function (a) {
  2108. return(Math.sqrt(Math.pow(a[0], 2) + Math.pow(a[1], 2) + Math.pow(a[2], 2) + Math.pow(a[3], 2) + Math.pow(a[4], 2) + Math.pow(a[5], 2) + 1))
  2109. };
  2110. if(typeof(exports) !== 'undefined') {
  2111. exports.mat2d = mat2d;
  2112. }
  2113. ;
  2114. /* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved.
  2115. Redistribution and use in source and binary forms, with or without modification,
  2116. are permitted provided that the following conditions are met:
  2117. * Redistributions of source code must retain the above copyright notice, this
  2118. list of conditions and the following disclaimer.
  2119. * Redistributions in binary form must reproduce the above copyright notice,
  2120. this list of conditions and the following disclaimer in the documentation
  2121. and/or other materials provided with the distribution.
  2122. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  2123. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  2124. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  2125. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  2126. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  2127. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  2128. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  2129. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  2130. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  2131. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
  2132. /**
  2133. * @class 3x3 Matrix
  2134. * @name mat3
  2135. */
  2136. var mat3 = {};
  2137. /**
  2138. * Creates a new identity mat3
  2139. *
  2140. * @returns {mat3} a new 3x3 matrix
  2141. */
  2142. mat3.create = function() {
  2143. var out = new GLMAT_ARRAY_TYPE(9);
  2144. out[0] = 1;
  2145. out[1] = 0;
  2146. out[2] = 0;
  2147. out[3] = 0;
  2148. out[4] = 1;
  2149. out[5] = 0;
  2150. out[6] = 0;
  2151. out[7] = 0;
  2152. out[8] = 1;
  2153. return out;
  2154. };
  2155. /**
  2156. * Copies the upper-left 3x3 values into the given mat3.
  2157. *
  2158. * @param {mat3} out the receiving 3x3 matrix
  2159. * @param {mat4} a the source 4x4 matrix
  2160. * @returns {mat3} out
  2161. */
  2162. mat3.fromMat4 = function(out, a) {
  2163. out[0] = a[0];
  2164. out[1] = a[1];
  2165. out[2] = a[2];
  2166. out[3] = a[4];
  2167. out[4] = a[5];
  2168. out[5] = a[6];
  2169. out[6] = a[8];
  2170. out[7] = a[9];
  2171. out[8] = a[10];
  2172. return out;
  2173. };
  2174. /**
  2175. * Creates a new mat3 initialized with values from an existing matrix
  2176. *
  2177. * @param {mat3} a matrix to clone
  2178. * @returns {mat3} a new 3x3 matrix
  2179. */
  2180. mat3.clone = function(a) {
  2181. var out = new GLMAT_ARRAY_TYPE(9);
  2182. out[0] = a[0];
  2183. out[1] = a[1];
  2184. out[2] = a[2];
  2185. out[3] = a[3];
  2186. out[4] = a[4];
  2187. out[5] = a[5];
  2188. out[6] = a[6];
  2189. out[7] = a[7];
  2190. out[8] = a[8];
  2191. return out;
  2192. };
  2193. /**
  2194. * Copy the values from one mat3 to another
  2195. *
  2196. * @param {mat3} out the receiving matrix
  2197. * @param {mat3} a the source matrix
  2198. * @returns {mat3} out
  2199. */
  2200. mat3.copy = function(out, a) {
  2201. out[0] = a[0];
  2202. out[1] = a[1];
  2203. out[2] = a[2];
  2204. out[3] = a[3];
  2205. out[4] = a[4];
  2206. out[5] = a[5];
  2207. out[6] = a[6];
  2208. out[7] = a[7];
  2209. out[8] = a[8];
  2210. return out;
  2211. };
  2212. /**
  2213. * Set a mat3 to the identity matrix
  2214. *
  2215. * @param {mat3} out the receiving matrix
  2216. * @returns {mat3} out
  2217. */
  2218. mat3.identity = function(out) {
  2219. out[0] = 1;
  2220. out[1] = 0;
  2221. out[2] = 0;
  2222. out[3] = 0;
  2223. out[4] = 1;
  2224. out[5] = 0;
  2225. out[6] = 0;
  2226. out[7] = 0;
  2227. out[8] = 1;
  2228. return out;
  2229. };
  2230. /**
  2231. * Transpose the values of a mat3
  2232. *
  2233. * @param {mat3} out the receiving matrix
  2234. * @param {mat3} a the source matrix
  2235. * @returns {mat3} out
  2236. */
  2237. mat3.transpose = function(out, a) {
  2238. // If we are transposing ourselves we can skip a few steps but have to cache some values
  2239. if (out === a) {
  2240. var a01 = a[1], a02 = a[2], a12 = a[5];
  2241. out[1] = a[3];
  2242. out[2] = a[6];
  2243. out[3] = a01;
  2244. out[5] = a[7];
  2245. out[6] = a02;
  2246. out[7] = a12;
  2247. } else {
  2248. out[0] = a[0];
  2249. out[1] = a[3];
  2250. out[2] = a[6];
  2251. out[3] = a[1];
  2252. out[4] = a[4];
  2253. out[5] = a[7];
  2254. out[6] = a[2];
  2255. out[7] = a[5];
  2256. out[8] = a[8];
  2257. }
  2258. return out;
  2259. };
  2260. /**
  2261. * Inverts a mat3
  2262. *
  2263. * @param {mat3} out the receiving matrix
  2264. * @param {mat3} a the source matrix
  2265. * @returns {mat3} out
  2266. */
  2267. mat3.invert = function(out, a) {
  2268. var a00 = a[0], a01 = a[1], a02 = a[2],
  2269. a10 = a[3], a11 = a[4], a12 = a[5],
  2270. a20 = a[6], a21 = a[7], a22 = a[8],
  2271. b01 = a22 * a11 - a12 * a21,
  2272. b11 = -a22 * a10 + a12 * a20,
  2273. b21 = a21 * a10 - a11 * a20,
  2274. // Calculate the determinant
  2275. det = a00 * b01 + a01 * b11 + a02 * b21;
  2276. if (!det) {
  2277. return null;
  2278. }
  2279. det = 1.0 / det;
  2280. out[0] = b01 * det;
  2281. out[1] = (-a22 * a01 + a02 * a21) * det;
  2282. out[2] = (a12 * a01 - a02 * a11) * det;
  2283. out[3] = b11 * det;
  2284. out[4] = (a22 * a00 - a02 * a20) * det;
  2285. out[5] = (-a12 * a00 + a02 * a10) * det;
  2286. out[6] = b21 * det;
  2287. out[7] = (-a21 * a00 + a01 * a20) * det;
  2288. out[8] = (a11 * a00 - a01 * a10) * det;
  2289. return out;
  2290. };
  2291. /**
  2292. * Calculates the adjugate of a mat3
  2293. *
  2294. * @param {mat3} out the receiving matrix
  2295. * @param {mat3} a the source matrix
  2296. * @returns {mat3} out
  2297. */
  2298. mat3.adjoint = function(out, a) {
  2299. var a00 = a[0], a01 = a[1], a02 = a[2],
  2300. a10 = a[3], a11 = a[4], a12 = a[5],
  2301. a20 = a[6], a21 = a[7], a22 = a[8];
  2302. out[0] = (a11 * a22 - a12 * a21);
  2303. out[1] = (a02 * a21 - a01 * a22);
  2304. out[2] = (a01 * a12 - a02 * a11);
  2305. out[3] = (a12 * a20 - a10 * a22);
  2306. out[4] = (a00 * a22 - a02 * a20);
  2307. out[5] = (a02 * a10 - a00 * a12);
  2308. out[6] = (a10 * a21 - a11 * a20);
  2309. out[7] = (a01 * a20 - a00 * a21);
  2310. out[8] = (a00 * a11 - a01 * a10);
  2311. return out;
  2312. };
  2313. /**
  2314. * Calculates the determinant of a mat3
  2315. *
  2316. * @param {mat3} a the source matrix
  2317. * @returns {Number} determinant of a
  2318. */
  2319. mat3.determinant = function (a) {
  2320. var a00 = a[0], a01 = a[1], a02 = a[2],
  2321. a10 = a[3], a11 = a[4], a12 = a[5],
  2322. a20 = a[6], a21 = a[7], a22 = a[8];
  2323. return a00 * (a22 * a11 - a12 * a21) + a01 * (-a22 * a10 + a12 * a20) + a02 * (a21 * a10 - a11 * a20);
  2324. };
  2325. /**
  2326. * Multiplies two mat3's
  2327. *
  2328. * @param {mat3} out the receiving matrix
  2329. * @param {mat3} a the first operand
  2330. * @param {mat3} b the second operand
  2331. * @returns {mat3} out
  2332. */
  2333. mat3.multiply = function (out, a, b) {
  2334. var a00 = a[0], a01 = a[1], a02 = a[2],
  2335. a10 = a[3], a11 = a[4], a12 = a[5],
  2336. a20 = a[6], a21 = a[7], a22 = a[8],
  2337. b00 = b[0], b01 = b[1], b02 = b[2],
  2338. b10 = b[3], b11 = b[4], b12 = b[5],
  2339. b20 = b[6], b21 = b[7], b22 = b[8];
  2340. out[0] = b00 * a00 + b01 * a10 + b02 * a20;
  2341. out[1] = b00 * a01 + b01 * a11 + b02 * a21;
  2342. out[2] = b00 * a02 + b01 * a12 + b02 * a22;
  2343. out[3] = b10 * a00 + b11 * a10 + b12 * a20;
  2344. out[4] = b10 * a01 + b11 * a11 + b12 * a21;
  2345. out[5] = b10 * a02 + b11 * a12 + b12 * a22;
  2346. out[6] = b20 * a00 + b21 * a10 + b22 * a20;
  2347. out[7] = b20 * a01 + b21 * a11 + b22 * a21;
  2348. out[8] = b20 * a02 + b21 * a12 + b22 * a22;
  2349. return out;
  2350. };
  2351. /**
  2352. * Alias for {@link mat3.multiply}
  2353. * @function
  2354. */
  2355. mat3.mul = mat3.multiply;
  2356. /**
  2357. * Translate a mat3 by the given vector
  2358. *
  2359. * @param {mat3} out the receiving matrix
  2360. * @param {mat3} a the matrix to translate
  2361. * @param {vec2} v vector to translate by
  2362. * @returns {mat3} out
  2363. */
  2364. mat3.translate = function(out, a, v) {
  2365. var a00 = a[0], a01 = a[1], a02 = a[2],
  2366. a10 = a[3], a11 = a[4], a12 = a[5],
  2367. a20 = a[6], a21 = a[7], a22 = a[8],
  2368. x = v[0], y = v[1];
  2369. out[0] = a00;
  2370. out[1] = a01;
  2371. out[2] = a02;
  2372. out[3] = a10;
  2373. out[4] = a11;
  2374. out[5] = a12;
  2375. out[6] = x * a00 + y * a10 + a20;
  2376. out[7] = x * a01 + y * a11 + a21;
  2377. out[8] = x * a02 + y * a12 + a22;
  2378. return out;
  2379. };
  2380. /**
  2381. * Rotates a mat3 by the given angle
  2382. *
  2383. * @param {mat3} out the receiving matrix
  2384. * @param {mat3} a the matrix to rotate
  2385. * @param {Number} rad the angle to rotate the matrix by
  2386. * @returns {mat3} out
  2387. */
  2388. mat3.rotate = function (out, a, rad) {
  2389. var a00 = a[0], a01 = a[1], a02 = a[2],
  2390. a10 = a[3], a11 = a[4], a12 = a[5],
  2391. a20 = a[6], a21 = a[7], a22 = a[8],
  2392. s = Math.sin(rad),
  2393. c = Math.cos(rad);
  2394. out[0] = c * a00 + s * a10;
  2395. out[1] = c * a01 + s * a11;
  2396. out[2] = c * a02 + s * a12;
  2397. out[3] = c * a10 - s * a00;
  2398. out[4] = c * a11 - s * a01;
  2399. out[5] = c * a12 - s * a02;
  2400. out[6] = a20;
  2401. out[7] = a21;
  2402. out[8] = a22;
  2403. return out;
  2404. };
  2405. /**
  2406. * Scales the mat3 by the dimensions in the given vec2
  2407. *
  2408. * @param {mat3} out the receiving matrix
  2409. * @param {mat3} a the matrix to rotate
  2410. * @param {vec2} v the vec2 to scale the matrix by
  2411. * @returns {mat3} out
  2412. **/
  2413. mat3.scale = function(out, a, v) {
  2414. var x = v[0], y = v[1];
  2415. out[0] = x * a[0];
  2416. out[1] = x * a[1];
  2417. out[2] = x * a[2];
  2418. out[3] = y * a[3];
  2419. out[4] = y * a[4];
  2420. out[5] = y * a[5];
  2421. out[6] = a[6];
  2422. out[7] = a[7];
  2423. out[8] = a[8];
  2424. return out;
  2425. };
  2426. /**
  2427. * Copies the values from a mat2d into a mat3
  2428. *
  2429. * @param {mat3} out the receiving matrix
  2430. * @param {mat2d} a the matrix to copy
  2431. * @returns {mat3} out
  2432. **/
  2433. mat3.fromMat2d = function(out, a) {
  2434. out[0] = a[0];
  2435. out[1] = a[1];
  2436. out[2] = 0;
  2437. out[3] = a[2];
  2438. out[4] = a[3];
  2439. out[5] = 0;
  2440. out[6] = a[4];
  2441. out[7] = a[5];
  2442. out[8] = 1;
  2443. return out;
  2444. };
  2445. /**
  2446. * Calculates a 3x3 matrix from the given quaternion
  2447. *
  2448. * @param {mat3} out mat3 receiving operation result
  2449. * @param {quat} q Quaternion to create matrix from
  2450. *
  2451. * @returns {mat3} out
  2452. */
  2453. mat3.fromQuat = function (out, q) {
  2454. var x = q[0], y = q[1], z = q[2], w = q[3],
  2455. x2 = x + x,
  2456. y2 = y + y,
  2457. z2 = z + z,
  2458. xx = x * x2,
  2459. yx = y * x2,
  2460. yy = y * y2,
  2461. zx = z * x2,
  2462. zy = z * y2,
  2463. zz = z * z2,
  2464. wx = w * x2,
  2465. wy = w * y2,
  2466. wz = w * z2;
  2467. out[0] = 1 - yy - zz;
  2468. out[3] = yx - wz;
  2469. out[6] = zx + wy;
  2470. out[1] = yx + wz;
  2471. out[4] = 1 - xx - zz;
  2472. out[7] = zy - wx;
  2473. out[2] = zx - wy;
  2474. out[5] = zy + wx;
  2475. out[8] = 1 - xx - yy;
  2476. return out;
  2477. };
  2478. /**
  2479. * Calculates a 3x3 normal matrix (transpose inverse) from the 4x4 matrix
  2480. *
  2481. * @param {mat3} out mat3 receiving operation result
  2482. * @param {mat4} a Mat4 to derive the normal matrix from
  2483. *
  2484. * @returns {mat3} out
  2485. */
  2486. mat3.normalFromMat4 = function (out, a) {
  2487. var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3],
  2488. a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7],
  2489. a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11],
  2490. a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15],
  2491. b00 = a00 * a11 - a01 * a10,
  2492. b01 = a00 * a12 - a02 * a10,
  2493. b02 = a00 * a13 - a03 * a10,
  2494. b03 = a01 * a12 - a02 * a11,
  2495. b04 = a01 * a13 - a03 * a11,
  2496. b05 = a02 * a13 - a03 * a12,
  2497. b06 = a20 * a31 - a21 * a30,
  2498. b07 = a20 * a32 - a22 * a30,
  2499. b08 = a20 * a33 - a23 * a30,
  2500. b09 = a21 * a32 - a22 * a31,
  2501. b10 = a21 * a33 - a23 * a31,
  2502. b11 = a22 * a33 - a23 * a32,
  2503. // Calculate the determinant
  2504. det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
  2505. if (!det) {
  2506. return null;
  2507. }
  2508. det = 1.0 / det;
  2509. out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det;
  2510. out[1] = (a12 * b08 - a10 * b11 - a13 * b07) * det;
  2511. out[2] = (a10 * b10 - a11 * b08 + a13 * b06) * det;
  2512. out[3] = (a02 * b10 - a01 * b11 - a03 * b09) * det;
  2513. out[4] = (a00 * b11 - a02 * b08 + a03 * b07) * det;
  2514. out[5] = (a01 * b08 - a00 * b10 - a03 * b06) * det;
  2515. out[6] = (a31 * b05 - a32 * b04 + a33 * b03) * det;
  2516. out[7] = (a32 * b02 - a30 * b05 - a33 * b01) * det;
  2517. out[8] = (a30 * b04 - a31 * b02 + a33 * b00) * det;
  2518. return out;
  2519. };
  2520. /**
  2521. * Returns a string representation of a mat3
  2522. *
  2523. * @param {mat3} mat matrix to represent as a string
  2524. * @returns {String} string representation of the matrix
  2525. */
  2526. mat3.str = function (a) {
  2527. return 'mat3(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' +
  2528. a[3] + ', ' + a[4] + ', ' + a[5] + ', ' +
  2529. a[6] + ', ' + a[7] + ', ' + a[8] + ')';
  2530. };
  2531. /**
  2532. * Returns Frobenius norm of a mat3
  2533. *
  2534. * @param {mat3} a the matrix to calculate Frobenius norm of
  2535. * @returns {Number} Frobenius norm
  2536. */
  2537. mat3.frob = function (a) {
  2538. return(Math.sqrt(Math.pow(a[0], 2) + Math.pow(a[1], 2) + Math.pow(a[2], 2) + Math.pow(a[3], 2) + Math.pow(a[4], 2) + Math.pow(a[5], 2) + Math.pow(a[6], 2) + Math.pow(a[7], 2) + Math.pow(a[8], 2)))
  2539. };
  2540. if(typeof(exports) !== 'undefined') {
  2541. exports.mat3 = mat3;
  2542. }
  2543. ;
  2544. /* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved.
  2545. Redistribution and use in source and binary forms, with or without modification,
  2546. are permitted provided that the following conditions are met:
  2547. * Redistributions of source code must retain the above copyright notice, this
  2548. list of conditions and the following disclaimer.
  2549. * Redistributions in binary form must reproduce the above copyright notice,
  2550. this list of conditions and the following disclaimer in the documentation
  2551. and/or other materials provided with the distribution.
  2552. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  2553. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  2554. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  2555. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  2556. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  2557. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  2558. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  2559. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  2560. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  2561. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
  2562. /**
  2563. * @class 4x4 Matrix
  2564. * @name mat4
  2565. */
  2566. var mat4 = {};
  2567. /**
  2568. * Creates a new identity mat4
  2569. *
  2570. * @returns {mat4} a new 4x4 matrix
  2571. */
  2572. mat4.create = function() {
  2573. var out = new GLMAT_ARRAY_TYPE(16);
  2574. out[0] = 1;
  2575. out[1] = 0;
  2576. out[2] = 0;
  2577. out[3] = 0;
  2578. out[4] = 0;
  2579. out[5] = 1;
  2580. out[6] = 0;
  2581. out[7] = 0;
  2582. out[8] = 0;
  2583. out[9] = 0;
  2584. out[10] = 1;
  2585. out[11] = 0;
  2586. out[12] = 0;
  2587. out[13] = 0;
  2588. out[14] = 0;
  2589. out[15] = 1;
  2590. return out;
  2591. };
  2592. /**
  2593. * Creates a new mat4 initialized with values from an existing matrix
  2594. *
  2595. * @param {mat4} a matrix to clone
  2596. * @returns {mat4} a new 4x4 matrix
  2597. */
  2598. mat4.clone = function(a) {
  2599. var out = new GLMAT_ARRAY_TYPE(16);
  2600. out[0] = a[0];
  2601. out[1] = a[1];
  2602. out[2] = a[2];
  2603. out[3] = a[3];
  2604. out[4] = a[4];
  2605. out[5] = a[5];
  2606. out[6] = a[6];
  2607. out[7] = a[7];
  2608. out[8] = a[8];
  2609. out[9] = a[9];
  2610. out[10] = a[10];
  2611. out[11] = a[11];
  2612. out[12] = a[12];
  2613. out[13] = a[13];
  2614. out[14] = a[14];
  2615. out[15] = a[15];
  2616. return out;
  2617. };
  2618. /**
  2619. * Copy the values from one mat4 to another
  2620. *
  2621. * @param {mat4} out the receiving matrix
  2622. * @param {mat4} a the source matrix
  2623. * @returns {mat4} out
  2624. */
  2625. mat4.copy = function(out, a) {
  2626. out[0] = a[0];
  2627. out[1] = a[1];
  2628. out[2] = a[2];
  2629. out[3] = a[3];
  2630. out[4] = a[4];
  2631. out[5] = a[5];
  2632. out[6] = a[6];
  2633. out[7] = a[7];
  2634. out[8] = a[8];
  2635. out[9] = a[9];
  2636. out[10] = a[10];
  2637. out[11] = a[11];
  2638. out[12] = a[12];
  2639. out[13] = a[13];
  2640. out[14] = a[14];
  2641. out[15] = a[15];
  2642. return out;
  2643. };
  2644. /**
  2645. * Set a mat4 to the identity matrix
  2646. *
  2647. * @param {mat4} out the receiving matrix
  2648. * @returns {mat4} out
  2649. */
  2650. mat4.identity = function(out) {
  2651. out[0] = 1;
  2652. out[1] = 0;
  2653. out[2] = 0;
  2654. out[3] = 0;
  2655. out[4] = 0;
  2656. out[5] = 1;
  2657. out[6] = 0;
  2658. out[7] = 0;
  2659. out[8] = 0;
  2660. out[9] = 0;
  2661. out[10] = 1;
  2662. out[11] = 0;
  2663. out[12] = 0;
  2664. out[13] = 0;
  2665. out[14] = 0;
  2666. out[15] = 1;
  2667. return out;
  2668. };
  2669. /**
  2670. * Transpose the values of a mat4
  2671. *
  2672. * @param {mat4} out the receiving matrix
  2673. * @param {mat4} a the source matrix
  2674. * @returns {mat4} out
  2675. */
  2676. mat4.transpose = function(out, a) {
  2677. // If we are transposing ourselves we can skip a few steps but have to cache some values
  2678. if (out === a) {
  2679. var a01 = a[1], a02 = a[2], a03 = a[3],
  2680. a12 = a[6], a13 = a[7],
  2681. a23 = a[11];
  2682. out[1] = a[4];
  2683. out[2] = a[8];
  2684. out[3] = a[12];
  2685. out[4] = a01;
  2686. out[6] = a[9];
  2687. out[7] = a[13];
  2688. out[8] = a02;
  2689. out[9] = a12;
  2690. out[11] = a[14];
  2691. out[12] = a03;
  2692. out[13] = a13;
  2693. out[14] = a23;
  2694. } else {
  2695. out[0] = a[0];
  2696. out[1] = a[4];
  2697. out[2] = a[8];
  2698. out[3] = a[12];
  2699. out[4] = a[1];
  2700. out[5] = a[5];
  2701. out[6] = a[9];
  2702. out[7] = a[13];
  2703. out[8] = a[2];
  2704. out[9] = a[6];
  2705. out[10] = a[10];
  2706. out[11] = a[14];
  2707. out[12] = a[3];
  2708. out[13] = a[7];
  2709. out[14] = a[11];
  2710. out[15] = a[15];
  2711. }
  2712. return out;
  2713. };
  2714. /**
  2715. * Inverts a mat4
  2716. *
  2717. * @param {mat4} out the receiving matrix
  2718. * @param {mat4} a the source matrix
  2719. * @returns {mat4} out
  2720. */
  2721. mat4.invert = function(out, a) {
  2722. var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3],
  2723. a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7],
  2724. a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11],
  2725. a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15],
  2726. b00 = a00 * a11 - a01 * a10,
  2727. b01 = a00 * a12 - a02 * a10,
  2728. b02 = a00 * a13 - a03 * a10,
  2729. b03 = a01 * a12 - a02 * a11,
  2730. b04 = a01 * a13 - a03 * a11,
  2731. b05 = a02 * a13 - a03 * a12,
  2732. b06 = a20 * a31 - a21 * a30,
  2733. b07 = a20 * a32 - a22 * a30,
  2734. b08 = a20 * a33 - a23 * a30,
  2735. b09 = a21 * a32 - a22 * a31,
  2736. b10 = a21 * a33 - a23 * a31,
  2737. b11 = a22 * a33 - a23 * a32,
  2738. // Calculate the determinant
  2739. det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
  2740. if (!det) {
  2741. return null;
  2742. }
  2743. det = 1.0 / det;
  2744. out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det;
  2745. out[1] = (a02 * b10 - a01 * b11 - a03 * b09) * det;
  2746. out[2] = (a31 * b05 - a32 * b04 + a33 * b03) * det;
  2747. out[3] = (a22 * b04 - a21 * b05 - a23 * b03) * det;
  2748. out[4] = (a12 * b08 - a10 * b11 - a13 * b07) * det;
  2749. out[5] = (a00 * b11 - a02 * b08 + a03 * b07) * det;
  2750. out[6] = (a32 * b02 - a30 * b05 - a33 * b01) * det;
  2751. out[7] = (a20 * b05 - a22 * b02 + a23 * b01) * det;
  2752. out[8] = (a10 * b10 - a11 * b08 + a13 * b06) * det;
  2753. out[9] = (a01 * b08 - a00 * b10 - a03 * b06) * det;
  2754. out[10] = (a30 * b04 - a31 * b02 + a33 * b00) * det;
  2755. out[11] = (a21 * b02 - a20 * b04 - a23 * b00) * det;
  2756. out[12] = (a11 * b07 - a10 * b09 - a12 * b06) * det;
  2757. out[13] = (a00 * b09 - a01 * b07 + a02 * b06) * det;
  2758. out[14] = (a31 * b01 - a30 * b03 - a32 * b00) * det;
  2759. out[15] = (a20 * b03 - a21 * b01 + a22 * b00) * det;
  2760. return out;
  2761. };
  2762. /**
  2763. * Calculates the adjugate of a mat4
  2764. *
  2765. * @param {mat4} out the receiving matrix
  2766. * @param {mat4} a the source matrix
  2767. * @returns {mat4} out
  2768. */
  2769. mat4.adjoint = function(out, a) {
  2770. var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3],
  2771. a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7],
  2772. a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11],
  2773. a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15];
  2774. out[0] = (a11 * (a22 * a33 - a23 * a32) - a21 * (a12 * a33 - a13 * a32) + a31 * (a12 * a23 - a13 * a22));
  2775. out[1] = -(a01 * (a22 * a33 - a23 * a32) - a21 * (a02 * a33 - a03 * a32) + a31 * (a02 * a23 - a03 * a22));
  2776. out[2] = (a01 * (a12 * a33 - a13 * a32) - a11 * (a02 * a33 - a03 * a32) + a31 * (a02 * a13 - a03 * a12));
  2777. out[3] = -(a01 * (a12 * a23 - a13 * a22) - a11 * (a02 * a23 - a03 * a22) + a21 * (a02 * a13 - a03 * a12));
  2778. out[4] = -(a10 * (a22 * a33 - a23 * a32) - a20 * (a12 * a33 - a13 * a32) + a30 * (a12 * a23 - a13 * a22));
  2779. out[5] = (a00 * (a22 * a33 - a23 * a32) - a20 * (a02 * a33 - a03 * a32) + a30 * (a02 * a23 - a03 * a22));
  2780. out[6] = -(a00 * (a12 * a33 - a13 * a32) - a10 * (a02 * a33 - a03 * a32) + a30 * (a02 * a13 - a03 * a12));
  2781. out[7] = (a00 * (a12 * a23 - a13 * a22) - a10 * (a02 * a23 - a03 * a22) + a20 * (a02 * a13 - a03 * a12));
  2782. out[8] = (a10 * (a21 * a33 - a23 * a31) - a20 * (a11 * a33 - a13 * a31) + a30 * (a11 * a23 - a13 * a21));
  2783. out[9] = -(a00 * (a21 * a33 - a23 * a31) - a20 * (a01 * a33 - a03 * a31) + a30 * (a01 * a23 - a03 * a21));
  2784. out[10] = (a00 * (a11 * a33 - a13 * a31) - a10 * (a01 * a33 - a03 * a31) + a30 * (a01 * a13 - a03 * a11));
  2785. out[11] = -(a00 * (a11 * a23 - a13 * a21) - a10 * (a01 * a23 - a03 * a21) + a20 * (a01 * a13 - a03 * a11));
  2786. out[12] = -(a10 * (a21 * a32 - a22 * a31) - a20 * (a11 * a32 - a12 * a31) + a30 * (a11 * a22 - a12 * a21));
  2787. out[13] = (a00 * (a21 * a32 - a22 * a31) - a20 * (a01 * a32 - a02 * a31) + a30 * (a01 * a22 - a02 * a21));
  2788. out[14] = -(a00 * (a11 * a32 - a12 * a31) - a10 * (a01 * a32 - a02 * a31) + a30 * (a01 * a12 - a02 * a11));
  2789. out[15] = (a00 * (a11 * a22 - a12 * a21) - a10 * (a01 * a22 - a02 * a21) + a20 * (a01 * a12 - a02 * a11));
  2790. return out;
  2791. };
  2792. /**
  2793. * Calculates the determinant of a mat4
  2794. *
  2795. * @param {mat4} a the source matrix
  2796. * @returns {Number} determinant of a
  2797. */
  2798. mat4.determinant = function (a) {
  2799. var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3],
  2800. a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7],
  2801. a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11],
  2802. a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15],
  2803. b00 = a00 * a11 - a01 * a10,
  2804. b01 = a00 * a12 - a02 * a10,
  2805. b02 = a00 * a13 - a03 * a10,
  2806. b03 = a01 * a12 - a02 * a11,
  2807. b04 = a01 * a13 - a03 * a11,
  2808. b05 = a02 * a13 - a03 * a12,
  2809. b06 = a20 * a31 - a21 * a30,
  2810. b07 = a20 * a32 - a22 * a30,
  2811. b08 = a20 * a33 - a23 * a30,
  2812. b09 = a21 * a32 - a22 * a31,
  2813. b10 = a21 * a33 - a23 * a31,
  2814. b11 = a22 * a33 - a23 * a32;
  2815. // Calculate the determinant
  2816. return b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
  2817. };
  2818. /**
  2819. * Multiplies two mat4's
  2820. *
  2821. * @param {mat4} out the receiving matrix
  2822. * @param {mat4} a the first operand
  2823. * @param {mat4} b the second operand
  2824. * @returns {mat4} out
  2825. */
  2826. mat4.multiply = function (out, a, b) {
  2827. var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3],
  2828. a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7],
  2829. a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11],
  2830. a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15];
  2831. // Cache only the current line of the second matrix
  2832. var b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3];
  2833. out[0] = b0*a00 + b1*a10 + b2*a20 + b3*a30;
  2834. out[1] = b0*a01 + b1*a11 + b2*a21 + b3*a31;
  2835. out[2] = b0*a02 + b1*a12 + b2*a22 + b3*a32;
  2836. out[3] = b0*a03 + b1*a13 + b2*a23 + b3*a33;
  2837. b0 = b[4]; b1 = b[5]; b2 = b[6]; b3 = b[7];
  2838. out[4] = b0*a00 + b1*a10 + b2*a20 + b3*a30;
  2839. out[5] = b0*a01 + b1*a11 + b2*a21 + b3*a31;
  2840. out[6] = b0*a02 + b1*a12 + b2*a22 + b3*a32;
  2841. out[7] = b0*a03 + b1*a13 + b2*a23 + b3*a33;
  2842. b0 = b[8]; b1 = b[9]; b2 = b[10]; b3 = b[11];
  2843. out[8] = b0*a00 + b1*a10 + b2*a20 + b3*a30;
  2844. out[9] = b0*a01 + b1*a11 + b2*a21 + b3*a31;
  2845. out[10] = b0*a02 + b1*a12 + b2*a22 + b3*a32;
  2846. out[11] = b0*a03 + b1*a13 + b2*a23 + b3*a33;
  2847. b0 = b[12]; b1 = b[13]; b2 = b[14]; b3 = b[15];
  2848. out[12] = b0*a00 + b1*a10 + b2*a20 + b3*a30;
  2849. out[13] = b0*a01 + b1*a11 + b2*a21 + b3*a31;
  2850. out[14] = b0*a02 + b1*a12 + b2*a22 + b3*a32;
  2851. out[15] = b0*a03 + b1*a13 + b2*a23 + b3*a33;
  2852. return out;
  2853. };
  2854. /**
  2855. * Alias for {@link mat4.multiply}
  2856. * @function
  2857. */
  2858. mat4.mul = mat4.multiply;
  2859. /**
  2860. * Translate a mat4 by the given vector
  2861. *
  2862. * @param {mat4} out the receiving matrix
  2863. * @param {mat4} a the matrix to translate
  2864. * @param {vec3} v vector to translate by
  2865. * @returns {mat4} out
  2866. */
  2867. mat4.translate = function (out, a, v) {
  2868. var x = v[0], y = v[1], z = v[2],
  2869. a00, a01, a02, a03,
  2870. a10, a11, a12, a13,
  2871. a20, a21, a22, a23;
  2872. if (a === out) {
  2873. out[12] = a[0] * x + a[4] * y + a[8] * z + a[12];
  2874. out[13] = a[1] * x + a[5] * y + a[9] * z + a[13];
  2875. out[14] = a[2] * x + a[6] * y + a[10] * z + a[14];
  2876. out[15] = a[3] * x + a[7] * y + a[11] * z + a[15];
  2877. } else {
  2878. a00 = a[0]; a01 = a[1]; a02 = a[2]; a03 = a[3];
  2879. a10 = a[4]; a11 = a[5]; a12 = a[6]; a13 = a[7];
  2880. a20 = a[8]; a21 = a[9]; a22 = a[10]; a23 = a[11];
  2881. out[0] = a00; out[1] = a01; out[2] = a02; out[3] = a03;
  2882. out[4] = a10; out[5] = a11; out[6] = a12; out[7] = a13;
  2883. out[8] = a20; out[9] = a21; out[10] = a22; out[11] = a23;
  2884. out[12] = a00 * x + a10 * y + a20 * z + a[12];
  2885. out[13] = a01 * x + a11 * y + a21 * z + a[13];
  2886. out[14] = a02 * x + a12 * y + a22 * z + a[14];
  2887. out[15] = a03 * x + a13 * y + a23 * z + a[15];
  2888. }
  2889. return out;
  2890. };
  2891. /**
  2892. * Scales the mat4 by the dimensions in the given vec3
  2893. *
  2894. * @param {mat4} out the receiving matrix
  2895. * @param {mat4} a the matrix to scale
  2896. * @param {vec3} v the vec3 to scale the matrix by
  2897. * @returns {mat4} out
  2898. **/
  2899. mat4.scale = function(out, a, v) {
  2900. var x = v[0], y = v[1], z = v[2];
  2901. out[0] = a[0] * x;
  2902. out[1] = a[1] * x;
  2903. out[2] = a[2] * x;
  2904. out[3] = a[3] * x;
  2905. out[4] = a[4] * y;
  2906. out[5] = a[5] * y;
  2907. out[6] = a[6] * y;
  2908. out[7] = a[7] * y;
  2909. out[8] = a[8] * z;
  2910. out[9] = a[9] * z;
  2911. out[10] = a[10] * z;
  2912. out[11] = a[11] * z;
  2913. out[12] = a[12];
  2914. out[13] = a[13];
  2915. out[14] = a[14];
  2916. out[15] = a[15];
  2917. return out;
  2918. };
  2919. /**
  2920. * Rotates a mat4 by the given angle
  2921. *
  2922. * @param {mat4} out the receiving matrix
  2923. * @param {mat4} a the matrix to rotate
  2924. * @param {Number} rad the angle to rotate the matrix by
  2925. * @param {vec3} axis the axis to rotate around
  2926. * @returns {mat4} out
  2927. */
  2928. mat4.rotate = function (out, a, rad, axis) {
  2929. var x = axis[0], y = axis[1], z = axis[2],
  2930. len = Math.sqrt(x * x + y * y + z * z),
  2931. s, c, t,
  2932. a00, a01, a02, a03,
  2933. a10, a11, a12, a13,
  2934. a20, a21, a22, a23,
  2935. b00, b01, b02,
  2936. b10, b11, b12,
  2937. b20, b21, b22;
  2938. if (Math.abs(len) < GLMAT_EPSILON) { return null; }
  2939. len = 1 / len;
  2940. x *= len;
  2941. y *= len;
  2942. z *= len;
  2943. s = Math.sin(rad);
  2944. c = Math.cos(rad);
  2945. t = 1 - c;
  2946. a00 = a[0]; a01 = a[1]; a02 = a[2]; a03 = a[3];
  2947. a10 = a[4]; a11 = a[5]; a12 = a[6]; a13 = a[7];
  2948. a20 = a[8]; a21 = a[9]; a22 = a[10]; a23 = a[11];
  2949. // Construct the elements of the rotation matrix
  2950. b00 = x * x * t + c; b01 = y * x * t + z * s; b02 = z * x * t - y * s;
  2951. b10 = x * y * t - z * s; b11 = y * y * t + c; b12 = z * y * t + x * s;
  2952. b20 = x * z * t + y * s; b21 = y * z * t - x * s; b22 = z * z * t + c;
  2953. // Perform rotation-specific matrix multiplication
  2954. out[0] = a00 * b00 + a10 * b01 + a20 * b02;
  2955. out[1] = a01 * b00 + a11 * b01 + a21 * b02;
  2956. out[2] = a02 * b00 + a12 * b01 + a22 * b02;
  2957. out[3] = a03 * b00 + a13 * b01 + a23 * b02;
  2958. out[4] = a00 * b10 + a10 * b11 + a20 * b12;
  2959. out[5] = a01 * b10 + a11 * b11 + a21 * b12;
  2960. out[6] = a02 * b10 + a12 * b11 + a22 * b12;
  2961. out[7] = a03 * b10 + a13 * b11 + a23 * b12;
  2962. out[8] = a00 * b20 + a10 * b21 + a20 * b22;
  2963. out[9] = a01 * b20 + a11 * b21 + a21 * b22;
  2964. out[10] = a02 * b20 + a12 * b21 + a22 * b22;
  2965. out[11] = a03 * b20 + a13 * b21 + a23 * b22;
  2966. if (a !== out) { // If the source and destination differ, copy the unchanged last row
  2967. out[12] = a[12];
  2968. out[13] = a[13];
  2969. out[14] = a[14];
  2970. out[15] = a[15];
  2971. }
  2972. return out;
  2973. };
  2974. /**
  2975. * Rotates a matrix by the given angle around the X axis
  2976. *
  2977. * @param {mat4} out the receiving matrix
  2978. * @param {mat4} a the matrix to rotate
  2979. * @param {Number} rad the angle to rotate the matrix by
  2980. * @returns {mat4} out
  2981. */
  2982. mat4.rotateX = function (out, a, rad) {
  2983. var s = Math.sin(rad),
  2984. c = Math.cos(rad),
  2985. a10 = a[4],
  2986. a11 = a[5],
  2987. a12 = a[6],
  2988. a13 = a[7],
  2989. a20 = a[8],
  2990. a21 = a[9],
  2991. a22 = a[10],
  2992. a23 = a[11];
  2993. if (a !== out) { // If the source and destination differ, copy the unchanged rows
  2994. out[0] = a[0];
  2995. out[1] = a[1];
  2996. out[2] = a[2];
  2997. out[3] = a[3];
  2998. out[12] = a[12];
  2999. out[13] = a[13];
  3000. out[14] = a[14];
  3001. out[15] = a[15];
  3002. }
  3003. // Perform axis-specific matrix multiplication
  3004. out[4] = a10 * c + a20 * s;
  3005. out[5] = a11 * c + a21 * s;
  3006. out[6] = a12 * c + a22 * s;
  3007. out[7] = a13 * c + a23 * s;
  3008. out[8] = a20 * c - a10 * s;
  3009. out[9] = a21 * c - a11 * s;
  3010. out[10] = a22 * c - a12 * s;
  3011. out[11] = a23 * c - a13 * s;
  3012. return out;
  3013. };
  3014. /**
  3015. * Rotates a matrix by the given angle around the Y axis
  3016. *
  3017. * @param {mat4} out the receiving matrix
  3018. * @param {mat4} a the matrix to rotate
  3019. * @param {Number} rad the angle to rotate the matrix by
  3020. * @returns {mat4} out
  3021. */
  3022. mat4.rotateY = function (out, a, rad) {
  3023. var s = Math.sin(rad),
  3024. c = Math.cos(rad),
  3025. a00 = a[0],
  3026. a01 = a[1],
  3027. a02 = a[2],
  3028. a03 = a[3],
  3029. a20 = a[8],
  3030. a21 = a[9],
  3031. a22 = a[10],
  3032. a23 = a[11];
  3033. if (a !== out) { // If the source and destination differ, copy the unchanged rows
  3034. out[4] = a[4];
  3035. out[5] = a[5];
  3036. out[6] = a[6];
  3037. out[7] = a[7];
  3038. out[12] = a[12];
  3039. out[13] = a[13];
  3040. out[14] = a[14];
  3041. out[15] = a[15];
  3042. }
  3043. // Perform axis-specific matrix multiplication
  3044. out[0] = a00 * c - a20 * s;
  3045. out[1] = a01 * c - a21 * s;
  3046. out[2] = a02 * c - a22 * s;
  3047. out[3] = a03 * c - a23 * s;
  3048. out[8] = a00 * s + a20 * c;
  3049. out[9] = a01 * s + a21 * c;
  3050. out[10] = a02 * s + a22 * c;
  3051. out[11] = a03 * s + a23 * c;
  3052. return out;
  3053. };
  3054. /**
  3055. * Rotates a matrix by the given angle around the Z axis
  3056. *
  3057. * @param {mat4} out the receiving matrix
  3058. * @param {mat4} a the matrix to rotate
  3059. * @param {Number} rad the angle to rotate the matrix by
  3060. * @returns {mat4} out
  3061. */
  3062. mat4.rotateZ = function (out, a, rad) {
  3063. var s = Math.sin(rad),
  3064. c = Math.cos(rad),
  3065. a00 = a[0],
  3066. a01 = a[1],
  3067. a02 = a[2],
  3068. a03 = a[3],
  3069. a10 = a[4],
  3070. a11 = a[5],
  3071. a12 = a[6],
  3072. a13 = a[7];
  3073. if (a !== out) { // If the source and destination differ, copy the unchanged last row
  3074. out[8] = a[8];
  3075. out[9] = a[9];
  3076. out[10] = a[10];
  3077. out[11] = a[11];
  3078. out[12] = a[12];
  3079. out[13] = a[13];
  3080. out[14] = a[14];
  3081. out[15] = a[15];
  3082. }
  3083. // Perform axis-specific matrix multiplication
  3084. out[0] = a00 * c + a10 * s;
  3085. out[1] = a01 * c + a11 * s;
  3086. out[2] = a02 * c + a12 * s;
  3087. out[3] = a03 * c + a13 * s;
  3088. out[4] = a10 * c - a00 * s;
  3089. out[5] = a11 * c - a01 * s;
  3090. out[6] = a12 * c - a02 * s;
  3091. out[7] = a13 * c - a03 * s;
  3092. return out;
  3093. };
  3094. /**
  3095. * Creates a matrix from a quaternion rotation and vector translation
  3096. * This is equivalent to (but much faster than):
  3097. *
  3098. * mat4.identity(dest);
  3099. * mat4.translate(dest, vec);
  3100. * var quatMat = mat4.create();
  3101. * quat4.toMat4(quat, quatMat);
  3102. * mat4.multiply(dest, quatMat);
  3103. *
  3104. * @param {mat4} out mat4 receiving operation result
  3105. * @param {quat4} q Rotation quaternion
  3106. * @param {vec3} v Translation vector
  3107. * @returns {mat4} out
  3108. */
  3109. mat4.fromRotationTranslation = function (out, q, v) {
  3110. // Quaternion math
  3111. var x = q[0], y = q[1], z = q[2], w = q[3],
  3112. x2 = x + x,
  3113. y2 = y + y,
  3114. z2 = z + z,
  3115. xx = x * x2,
  3116. xy = x * y2,
  3117. xz = x * z2,
  3118. yy = y * y2,
  3119. yz = y * z2,
  3120. zz = z * z2,
  3121. wx = w * x2,
  3122. wy = w * y2,
  3123. wz = w * z2;
  3124. out[0] = 1 - (yy + zz);
  3125. out[1] = xy + wz;
  3126. out[2] = xz - wy;
  3127. out[3] = 0;
  3128. out[4] = xy - wz;
  3129. out[5] = 1 - (xx + zz);
  3130. out[6] = yz + wx;
  3131. out[7] = 0;
  3132. out[8] = xz + wy;
  3133. out[9] = yz - wx;
  3134. out[10] = 1 - (xx + yy);
  3135. out[11] = 0;
  3136. out[12] = v[0];
  3137. out[13] = v[1];
  3138. out[14] = v[2];
  3139. out[15] = 1;
  3140. return out;
  3141. };
  3142. mat4.fromQuat = function (out, q) {
  3143. var x = q[0], y = q[1], z = q[2], w = q[3],
  3144. x2 = x + x,
  3145. y2 = y + y,
  3146. z2 = z + z,
  3147. xx = x * x2,
  3148. yx = y * x2,
  3149. yy = y * y2,
  3150. zx = z * x2,
  3151. zy = z * y2,
  3152. zz = z * z2,
  3153. wx = w * x2,
  3154. wy = w * y2,
  3155. wz = w * z2;
  3156. out[0] = 1 - yy - zz;
  3157. out[1] = yx + wz;
  3158. out[2] = zx - wy;
  3159. out[3] = 0;
  3160. out[4] = yx - wz;
  3161. out[5] = 1 - xx - zz;
  3162. out[6] = zy + wx;
  3163. out[7] = 0;
  3164. out[8] = zx + wy;
  3165. out[9] = zy - wx;
  3166. out[10] = 1 - xx - yy;
  3167. out[11] = 0;
  3168. out[12] = 0;
  3169. out[13] = 0;
  3170. out[14] = 0;
  3171. out[15] = 1;
  3172. return out;
  3173. };
  3174. /**
  3175. * Generates a frustum matrix with the given bounds
  3176. *
  3177. * @param {mat4} out mat4 frustum matrix will be written into
  3178. * @param {Number} left Left bound of the frustum
  3179. * @param {Number} right Right bound of the frustum
  3180. * @param {Number} bottom Bottom bound of the frustum
  3181. * @param {Number} top Top bound of the frustum
  3182. * @param {Number} near Near bound of the frustum
  3183. * @param {Number} far Far bound of the frustum
  3184. * @returns {mat4} out
  3185. */
  3186. mat4.frustum = function (out, left, right, bottom, top, near, far) {
  3187. var rl = 1 / (right - left),
  3188. tb = 1 / (top - bottom),
  3189. nf = 1 / (near - far);
  3190. out[0] = (near * 2) * rl;
  3191. out[1] = 0;
  3192. out[2] = 0;
  3193. out[3] = 0;
  3194. out[4] = 0;
  3195. out[5] = (near * 2) * tb;
  3196. out[6] = 0;
  3197. out[7] = 0;
  3198. out[8] = (right + left) * rl;
  3199. out[9] = (top + bottom) * tb;
  3200. out[10] = (far + near) * nf;
  3201. out[11] = -1;
  3202. out[12] = 0;
  3203. out[13] = 0;
  3204. out[14] = (far * near * 2) * nf;
  3205. out[15] = 0;
  3206. return out;
  3207. };
  3208. /**
  3209. * Generates a perspective projection matrix with the given bounds
  3210. *
  3211. * @param {mat4} out mat4 frustum matrix will be written into
  3212. * @param {number} fovy Vertical field of view in radians
  3213. * @param {number} aspect Aspect ratio. typically viewport width/height
  3214. * @param {number} near Near bound of the frustum
  3215. * @param {number} far Far bound of the frustum
  3216. * @returns {mat4} out
  3217. */
  3218. mat4.perspective = function (out, fovy, aspect, near, far) {
  3219. var f = 1.0 / Math.tan(fovy / 2),
  3220. nf = 1 / (near - far);
  3221. out[0] = f / aspect;
  3222. out[1] = 0;
  3223. out[2] = 0;
  3224. out[3] = 0;
  3225. out[4] = 0;
  3226. out[5] = f;
  3227. out[6] = 0;
  3228. out[7] = 0;
  3229. out[8] = 0;
  3230. out[9] = 0;
  3231. out[10] = (far + near) * nf;
  3232. out[11] = -1;
  3233. out[12] = 0;
  3234. out[13] = 0;
  3235. out[14] = (2 * far * near) * nf;
  3236. out[15] = 0;
  3237. return out;
  3238. };
  3239. /**
  3240. * Generates a orthogonal projection matrix with the given bounds
  3241. *
  3242. * @param {mat4} out mat4 frustum matrix will be written into
  3243. * @param {number} left Left bound of the frustum
  3244. * @param {number} right Right bound of the frustum
  3245. * @param {number} bottom Bottom bound of the frustum
  3246. * @param {number} top Top bound of the frustum
  3247. * @param {number} near Near bound of the frustum
  3248. * @param {number} far Far bound of the frustum
  3249. * @returns {mat4} out
  3250. */
  3251. mat4.ortho = function (out, left, right, bottom, top, near, far) {
  3252. var lr = 1 / (left - right),
  3253. bt = 1 / (bottom - top),
  3254. nf = 1 / (near - far);
  3255. out[0] = -2 * lr;
  3256. out[1] = 0;
  3257. out[2] = 0;
  3258. out[3] = 0;
  3259. out[4] = 0;
  3260. out[5] = -2 * bt;
  3261. out[6] = 0;
  3262. out[7] = 0;
  3263. out[8] = 0;
  3264. out[9] = 0;
  3265. out[10] = 2 * nf;
  3266. out[11] = 0;
  3267. out[12] = (left + right) * lr;
  3268. out[13] = (top + bottom) * bt;
  3269. out[14] = (far + near) * nf;
  3270. out[15] = 1;
  3271. return out;
  3272. };
  3273. /**
  3274. * Generates a look-at matrix with the given eye position, focal point, and up axis
  3275. *
  3276. * @param {mat4} out mat4 frustum matrix will be written into
  3277. * @param {vec3} eye Position of the viewer
  3278. * @param {vec3} center Point the viewer is looking at
  3279. * @param {vec3} up vec3 pointing up
  3280. * @returns {mat4} out
  3281. */
  3282. mat4.lookAt = function (out, eye, center, up) {
  3283. var x0, x1, x2, y0, y1, y2, z0, z1, z2, len,
  3284. eyex = eye[0],
  3285. eyey = eye[1],
  3286. eyez = eye[2],
  3287. upx = up[0],
  3288. upy = up[1],
  3289. upz = up[2],
  3290. centerx = center[0],
  3291. centery = center[1],
  3292. centerz = center[2];
  3293. if (Math.abs(eyex - centerx) < GLMAT_EPSILON &&
  3294. Math.abs(eyey - centery) < GLMAT_EPSILON &&
  3295. Math.abs(eyez - centerz) < GLMAT_EPSILON) {
  3296. return mat4.identity(out);
  3297. }
  3298. z0 = eyex - centerx;
  3299. z1 = eyey - centery;
  3300. z2 = eyez - centerz;
  3301. len = 1 / Math.sqrt(z0 * z0 + z1 * z1 + z2 * z2);
  3302. z0 *= len;
  3303. z1 *= len;
  3304. z2 *= len;
  3305. x0 = upy * z2 - upz * z1;
  3306. x1 = upz * z0 - upx * z2;
  3307. x2 = upx * z1 - upy * z0;
  3308. len = Math.sqrt(x0 * x0 + x1 * x1 + x2 * x2);
  3309. if (!len) {
  3310. x0 = 0;
  3311. x1 = 0;
  3312. x2 = 0;
  3313. } else {
  3314. len = 1 / len;
  3315. x0 *= len;
  3316. x1 *= len;
  3317. x2 *= len;
  3318. }
  3319. y0 = z1 * x2 - z2 * x1;
  3320. y1 = z2 * x0 - z0 * x2;
  3321. y2 = z0 * x1 - z1 * x0;
  3322. len = Math.sqrt(y0 * y0 + y1 * y1 + y2 * y2);
  3323. if (!len) {
  3324. y0 = 0;
  3325. y1 = 0;
  3326. y2 = 0;
  3327. } else {
  3328. len = 1 / len;
  3329. y0 *= len;
  3330. y1 *= len;
  3331. y2 *= len;
  3332. }
  3333. out[0] = x0;
  3334. out[1] = y0;
  3335. out[2] = z0;
  3336. out[3] = 0;
  3337. out[4] = x1;
  3338. out[5] = y1;
  3339. out[6] = z1;
  3340. out[7] = 0;
  3341. out[8] = x2;
  3342. out[9] = y2;
  3343. out[10] = z2;
  3344. out[11] = 0;
  3345. out[12] = -(x0 * eyex + x1 * eyey + x2 * eyez);
  3346. out[13] = -(y0 * eyex + y1 * eyey + y2 * eyez);
  3347. out[14] = -(z0 * eyex + z1 * eyey + z2 * eyez);
  3348. out[15] = 1;
  3349. return out;
  3350. };
  3351. /**
  3352. * Returns a string representation of a mat4
  3353. *
  3354. * @param {mat4} mat matrix to represent as a string
  3355. * @returns {String} string representation of the matrix
  3356. */
  3357. mat4.str = function (a) {
  3358. return 'mat4(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + a[3] + ', ' +
  3359. a[4] + ', ' + a[5] + ', ' + a[6] + ', ' + a[7] + ', ' +
  3360. a[8] + ', ' + a[9] + ', ' + a[10] + ', ' + a[11] + ', ' +
  3361. a[12] + ', ' + a[13] + ', ' + a[14] + ', ' + a[15] + ')';
  3362. };
  3363. /**
  3364. * Returns Frobenius norm of a mat4
  3365. *
  3366. * @param {mat4} a the matrix to calculate Frobenius norm of
  3367. * @returns {Number} Frobenius norm
  3368. */
  3369. mat4.frob = function (a) {
  3370. return(Math.sqrt(Math.pow(a[0], 2) + Math.pow(a[1], 2) + Math.pow(a[2], 2) + Math.pow(a[3], 2) + Math.pow(a[4], 2) + Math.pow(a[5], 2) + Math.pow(a[6], 2) + Math.pow(a[7], 2) + Math.pow(a[8], 2) + Math.pow(a[9], 2) + Math.pow(a[10], 2) + Math.pow(a[11], 2) + Math.pow(a[12], 2) + Math.pow(a[13], 2) + Math.pow(a[14], 2) + Math.pow(a[15], 2) ))
  3371. };
  3372. if(typeof(exports) !== 'undefined') {
  3373. exports.mat4 = mat4;
  3374. }
  3375. ;
  3376. /* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved.
  3377. Redistribution and use in source and binary forms, with or without modification,
  3378. are permitted provided that the following conditions are met:
  3379. * Redistributions of source code must retain the above copyright notice, this
  3380. list of conditions and the following disclaimer.
  3381. * Redistributions in binary form must reproduce the above copyright notice,
  3382. this list of conditions and the following disclaimer in the documentation
  3383. and/or other materials provided with the distribution.
  3384. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  3385. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  3386. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  3387. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  3388. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  3389. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  3390. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  3391. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  3392. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  3393. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
  3394. /**
  3395. * @class Quaternion
  3396. * @name quat
  3397. */
  3398. var quat = {};
  3399. /**
  3400. * Creates a new identity quat
  3401. *
  3402. * @returns {quat} a new quaternion
  3403. */
  3404. quat.create = function() {
  3405. var out = new GLMAT_ARRAY_TYPE(4);
  3406. out[0] = 0;
  3407. out[1] = 0;
  3408. out[2] = 0;
  3409. out[3] = 1;
  3410. return out;
  3411. };
  3412. /**
  3413. * Sets a quaternion to represent the shortest rotation from one
  3414. * vector to another.
  3415. *
  3416. * Both vectors are assumed to be unit length.
  3417. *
  3418. * @param {quat} out the receiving quaternion.
  3419. * @param {vec3} a the initial vector
  3420. * @param {vec3} b the destination vector
  3421. * @returns {quat} out
  3422. */
  3423. quat.rotationTo = (function() {
  3424. var tmpvec3 = vec3.create();
  3425. var xUnitVec3 = vec3.fromValues(1,0,0);
  3426. var yUnitVec3 = vec3.fromValues(0,1,0);
  3427. return function(out, a, b) {
  3428. var dot = vec3.dot(a, b);
  3429. if (dot < -0.999999) {
  3430. vec3.cross(tmpvec3, xUnitVec3, a);
  3431. if (vec3.length(tmpvec3) < 0.000001)
  3432. vec3.cross(tmpvec3, yUnitVec3, a);
  3433. vec3.normalize(tmpvec3, tmpvec3);
  3434. quat.setAxisAngle(out, tmpvec3, Math.PI);
  3435. return out;
  3436. } else if (dot > 0.999999) {
  3437. out[0] = 0;
  3438. out[1] = 0;
  3439. out[2] = 0;
  3440. out[3] = 1;
  3441. return out;
  3442. } else {
  3443. vec3.cross(tmpvec3, a, b);
  3444. out[0] = tmpvec3[0];
  3445. out[1] = tmpvec3[1];
  3446. out[2] = tmpvec3[2];
  3447. out[3] = 1 + dot;
  3448. return quat.normalize(out, out);
  3449. }
  3450. };
  3451. })();
  3452. /**
  3453. * Sets the specified quaternion with values corresponding to the given
  3454. * axes. Each axis is a vec3 and is expected to be unit length and
  3455. * perpendicular to all other specified axes.
  3456. *
  3457. * @param {vec3} view the vector representing the viewing direction
  3458. * @param {vec3} right the vector representing the local "right" direction
  3459. * @param {vec3} up the vector representing the local "up" direction
  3460. * @returns {quat} out
  3461. */
  3462. quat.setAxes = (function() {
  3463. var matr = mat3.create();
  3464. return function(out, view, right, up) {
  3465. matr[0] = right[0];
  3466. matr[3] = right[1];
  3467. matr[6] = right[2];
  3468. matr[1] = up[0];
  3469. matr[4] = up[1];
  3470. matr[7] = up[2];
  3471. matr[2] = -view[0];
  3472. matr[5] = -view[1];
  3473. matr[8] = -view[2];
  3474. return quat.normalize(out, quat.fromMat3(out, matr));
  3475. };
  3476. })();
  3477. /**
  3478. * Creates a new quat initialized with values from an existing quaternion
  3479. *
  3480. * @param {quat} a quaternion to clone
  3481. * @returns {quat} a new quaternion
  3482. * @function
  3483. */
  3484. quat.clone = vec4.clone;
  3485. /**
  3486. * Creates a new quat initialized with the given values
  3487. *
  3488. * @param {Number} x X component
  3489. * @param {Number} y Y component
  3490. * @param {Number} z Z component
  3491. * @param {Number} w W component
  3492. * @returns {quat} a new quaternion
  3493. * @function
  3494. */
  3495. quat.fromValues = vec4.fromValues;
  3496. /**
  3497. * Copy the values from one quat to another
  3498. *
  3499. * @param {quat} out the receiving quaternion
  3500. * @param {quat} a the source quaternion
  3501. * @returns {quat} out
  3502. * @function
  3503. */
  3504. quat.copy = vec4.copy;
  3505. /**
  3506. * Set the components of a quat to the given values
  3507. *
  3508. * @param {quat} out the receiving quaternion
  3509. * @param {Number} x X component
  3510. * @param {Number} y Y component
  3511. * @param {Number} z Z component
  3512. * @param {Number} w W component
  3513. * @returns {quat} out
  3514. * @function
  3515. */
  3516. quat.set = vec4.set;
  3517. /**
  3518. * Set a quat to the identity quaternion
  3519. *
  3520. * @param {quat} out the receiving quaternion
  3521. * @returns {quat} out
  3522. */
  3523. quat.identity = function(out) {
  3524. out[0] = 0;
  3525. out[1] = 0;
  3526. out[2] = 0;
  3527. out[3] = 1;
  3528. return out;
  3529. };
  3530. /**
  3531. * Sets a quat from the given angle and rotation axis,
  3532. * then returns it.
  3533. *
  3534. * @param {quat} out the receiving quaternion
  3535. * @param {vec3} axis the axis around which to rotate
  3536. * @param {Number} rad the angle in radians
  3537. * @returns {quat} out
  3538. **/
  3539. quat.setAxisAngle = function(out, axis, rad) {
  3540. rad = rad * 0.5;
  3541. var s = Math.sin(rad);
  3542. out[0] = s * axis[0];
  3543. out[1] = s * axis[1];
  3544. out[2] = s * axis[2];
  3545. out[3] = Math.cos(rad);
  3546. return out;
  3547. };
  3548. /**
  3549. * Adds two quat's
  3550. *
  3551. * @param {quat} out the receiving quaternion
  3552. * @param {quat} a the first operand
  3553. * @param {quat} b the second operand
  3554. * @returns {quat} out
  3555. * @function
  3556. */
  3557. quat.add = vec4.add;
  3558. /**
  3559. * Multiplies two quat's
  3560. *
  3561. * @param {quat} out the receiving quaternion
  3562. * @param {quat} a the first operand
  3563. * @param {quat} b the second operand
  3564. * @returns {quat} out
  3565. */
  3566. quat.multiply = function(out, a, b) {
  3567. var ax = a[0], ay = a[1], az = a[2], aw = a[3],
  3568. bx = b[0], by = b[1], bz = b[2], bw = b[3];
  3569. out[0] = ax * bw + aw * bx + ay * bz - az * by;
  3570. out[1] = ay * bw + aw * by + az * bx - ax * bz;
  3571. out[2] = az * bw + aw * bz + ax * by - ay * bx;
  3572. out[3] = aw * bw - ax * bx - ay * by - az * bz;
  3573. return out;
  3574. };
  3575. /**
  3576. * Alias for {@link quat.multiply}
  3577. * @function
  3578. */
  3579. quat.mul = quat.multiply;
  3580. /**
  3581. * Scales a quat by a scalar number
  3582. *
  3583. * @param {quat} out the receiving vector
  3584. * @param {quat} a the vector to scale
  3585. * @param {Number} b amount to scale the vector by
  3586. * @returns {quat} out
  3587. * @function
  3588. */
  3589. quat.scale = vec4.scale;
  3590. /**
  3591. * Rotates a quaternion by the given angle about the X axis
  3592. *
  3593. * @param {quat} out quat receiving operation result
  3594. * @param {quat} a quat to rotate
  3595. * @param {number} rad angle (in radians) to rotate
  3596. * @returns {quat} out
  3597. */
  3598. quat.rotateX = function (out, a, rad) {
  3599. rad *= 0.5;
  3600. var ax = a[0], ay = a[1], az = a[2], aw = a[3],
  3601. bx = Math.sin(rad), bw = Math.cos(rad);
  3602. out[0] = ax * bw + aw * bx;
  3603. out[1] = ay * bw + az * bx;
  3604. out[2] = az * bw - ay * bx;
  3605. out[3] = aw * bw - ax * bx;
  3606. return out;
  3607. };
  3608. /**
  3609. * Rotates a quaternion by the given angle about the Y axis
  3610. *
  3611. * @param {quat} out quat receiving operation result
  3612. * @param {quat} a quat to rotate
  3613. * @param {number} rad angle (in radians) to rotate
  3614. * @returns {quat} out
  3615. */
  3616. quat.rotateY = function (out, a, rad) {
  3617. rad *= 0.5;
  3618. var ax = a[0], ay = a[1], az = a[2], aw = a[3],
  3619. by = Math.sin(rad), bw = Math.cos(rad);
  3620. out[0] = ax * bw - az * by;
  3621. out[1] = ay * bw + aw * by;
  3622. out[2] = az * bw + ax * by;
  3623. out[3] = aw * bw - ay * by;
  3624. return out;
  3625. };
  3626. /**
  3627. * Rotates a quaternion by the given angle about the Z axis
  3628. *
  3629. * @param {quat} out quat receiving operation result
  3630. * @param {quat} a quat to rotate
  3631. * @param {number} rad angle (in radians) to rotate
  3632. * @returns {quat} out
  3633. */
  3634. quat.rotateZ = function (out, a, rad) {
  3635. rad *= 0.5;
  3636. var ax = a[0], ay = a[1], az = a[2], aw = a[3],
  3637. bz = Math.sin(rad), bw = Math.cos(rad);
  3638. out[0] = ax * bw + ay * bz;
  3639. out[1] = ay * bw - ax * bz;
  3640. out[2] = az * bw + aw * bz;
  3641. out[3] = aw * bw - az * bz;
  3642. return out;
  3643. };
  3644. /**
  3645. * Calculates the W component of a quat from the X, Y, and Z components.
  3646. * Assumes that quaternion is 1 unit in length.
  3647. * Any existing W component will be ignored.
  3648. *
  3649. * @param {quat} out the receiving quaternion
  3650. * @param {quat} a quat to calculate W component of
  3651. * @returns {quat} out
  3652. */
  3653. quat.calculateW = function (out, a) {
  3654. var x = a[0], y = a[1], z = a[2];
  3655. out[0] = x;
  3656. out[1] = y;
  3657. out[2] = z;
  3658. out[3] = Math.sqrt(Math.abs(1.0 - x * x - y * y - z * z));
  3659. return out;
  3660. };
  3661. /**
  3662. * Calculates the dot product of two quat's
  3663. *
  3664. * @param {quat} a the first operand
  3665. * @param {quat} b the second operand
  3666. * @returns {Number} dot product of a and b
  3667. * @function
  3668. */
  3669. quat.dot = vec4.dot;
  3670. /**
  3671. * Performs a linear interpolation between two quat's
  3672. *
  3673. * @param {quat} out the receiving quaternion
  3674. * @param {quat} a the first operand
  3675. * @param {quat} b the second operand
  3676. * @param {Number} t interpolation amount between the two inputs
  3677. * @returns {quat} out
  3678. * @function
  3679. */
  3680. quat.lerp = vec4.lerp;
  3681. /**
  3682. * Performs a spherical linear interpolation between two quat
  3683. *
  3684. * @param {quat} out the receiving quaternion
  3685. * @param {quat} a the first operand
  3686. * @param {quat} b the second operand
  3687. * @param {Number} t interpolation amount between the two inputs
  3688. * @returns {quat} out
  3689. */
  3690. quat.slerp = function (out, a, b, t) {
  3691. // benchmarks:
  3692. // http://jsperf.com/quaternion-slerp-implementations
  3693. var ax = a[0], ay = a[1], az = a[2], aw = a[3],
  3694. bx = b[0], by = b[1], bz = b[2], bw = b[3];
  3695. var omega, cosom, sinom, scale0, scale1;
  3696. // calc cosine
  3697. cosom = ax * bx + ay * by + az * bz + aw * bw;
  3698. // adjust signs (if necessary)
  3699. if ( cosom < 0.0 ) {
  3700. cosom = -cosom;
  3701. bx = - bx;
  3702. by = - by;
  3703. bz = - bz;
  3704. bw = - bw;
  3705. }
  3706. // calculate coefficients
  3707. if ( (1.0 - cosom) > 0.000001 ) {
  3708. // standard case (slerp)
  3709. omega = Math.acos(cosom);
  3710. sinom = Math.sin(omega);
  3711. scale0 = Math.sin((1.0 - t) * omega) / sinom;
  3712. scale1 = Math.sin(t * omega) / sinom;
  3713. } else {
  3714. // "from" and "to" quaternions are very close
  3715. // ... so we can do a linear interpolation
  3716. scale0 = 1.0 - t;
  3717. scale1 = t;
  3718. }
  3719. // calculate final values
  3720. out[0] = scale0 * ax + scale1 * bx;
  3721. out[1] = scale0 * ay + scale1 * by;
  3722. out[2] = scale0 * az + scale1 * bz;
  3723. out[3] = scale0 * aw + scale1 * bw;
  3724. return out;
  3725. };
  3726. /**
  3727. * Calculates the inverse of a quat
  3728. *
  3729. * @param {quat} out the receiving quaternion
  3730. * @param {quat} a quat to calculate inverse of
  3731. * @returns {quat} out
  3732. */
  3733. quat.invert = function(out, a) {
  3734. var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3],
  3735. dot = a0*a0 + a1*a1 + a2*a2 + a3*a3,
  3736. invDot = dot ? 1.0/dot : 0;
  3737. // TODO: Would be faster to return [0,0,0,0] immediately if dot == 0
  3738. out[0] = -a0*invDot;
  3739. out[1] = -a1*invDot;
  3740. out[2] = -a2*invDot;
  3741. out[3] = a3*invDot;
  3742. return out;
  3743. };
  3744. /**
  3745. * Calculates the conjugate of a quat
  3746. * If the quaternion is normalized, this function is faster than quat.inverse and produces the same result.
  3747. *
  3748. * @param {quat} out the receiving quaternion
  3749. * @param {quat} a quat to calculate conjugate of
  3750. * @returns {quat} out
  3751. */
  3752. quat.conjugate = function (out, a) {
  3753. out[0] = -a[0];
  3754. out[1] = -a[1];
  3755. out[2] = -a[2];
  3756. out[3] = a[3];
  3757. return out;
  3758. };
  3759. /**
  3760. * Calculates the length of a quat
  3761. *
  3762. * @param {quat} a vector to calculate length of
  3763. * @returns {Number} length of a
  3764. * @function
  3765. */
  3766. quat.length = vec4.length;
  3767. /**
  3768. * Alias for {@link quat.length}
  3769. * @function
  3770. */
  3771. quat.len = quat.length;
  3772. /**
  3773. * Calculates the squared length of a quat
  3774. *
  3775. * @param {quat} a vector to calculate squared length of
  3776. * @returns {Number} squared length of a
  3777. * @function
  3778. */
  3779. quat.squaredLength = vec4.squaredLength;
  3780. /**
  3781. * Alias for {@link quat.squaredLength}
  3782. * @function
  3783. */
  3784. quat.sqrLen = quat.squaredLength;
  3785. /**
  3786. * Normalize a quat
  3787. *
  3788. * @param {quat} out the receiving quaternion
  3789. * @param {quat} a quaternion to normalize
  3790. * @returns {quat} out
  3791. * @function
  3792. */
  3793. quat.normalize = vec4.normalize;
  3794. /**
  3795. * Creates a quaternion from the given 3x3 rotation matrix.
  3796. *
  3797. * NOTE: The resultant quaternion is not normalized, so you should be sure
  3798. * to renormalize the quaternion yourself where necessary.
  3799. *
  3800. * @param {quat} out the receiving quaternion
  3801. * @param {mat3} m rotation matrix
  3802. * @returns {quat} out
  3803. * @function
  3804. */
  3805. quat.fromMat3 = function(out, m) {
  3806. // Algorithm in Ken Shoemake's article in 1987 SIGGRAPH course notes
  3807. // article "Quaternion Calculus and Fast Animation".
  3808. var fTrace = m[0] + m[4] + m[8];
  3809. var fRoot;
  3810. if ( fTrace > 0.0 ) {
  3811. // |w| > 1/2, may as well choose w > 1/2
  3812. fRoot = Math.sqrt(fTrace + 1.0); // 2w
  3813. out[3] = 0.5 * fRoot;
  3814. fRoot = 0.5/fRoot; // 1/(4w)
  3815. out[0] = (m[5]-m[7])*fRoot;
  3816. out[1] = (m[6]-m[2])*fRoot;
  3817. out[2] = (m[1]-m[3])*fRoot;
  3818. } else {
  3819. // |w| <= 1/2
  3820. var i = 0;
  3821. if ( m[4] > m[0] )
  3822. i = 1;
  3823. if ( m[8] > m[i*3+i] )
  3824. i = 2;
  3825. var j = (i+1)%3;
  3826. var k = (i+2)%3;
  3827. fRoot = Math.sqrt(m[i*3+i]-m[j*3+j]-m[k*3+k] + 1.0);
  3828. out[i] = 0.5 * fRoot;
  3829. fRoot = 0.5 / fRoot;
  3830. out[3] = (m[j*3+k] - m[k*3+j]) * fRoot;
  3831. out[j] = (m[j*3+i] + m[i*3+j]) * fRoot;
  3832. out[k] = (m[k*3+i] + m[i*3+k]) * fRoot;
  3833. }
  3834. return out;
  3835. };
  3836. /**
  3837. * Returns a string representation of a quatenion
  3838. *
  3839. * @param {quat} vec vector to represent as a string
  3840. * @returns {String} string representation of the vector
  3841. */
  3842. quat.str = function (a) {
  3843. return 'quat(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + a[3] + ')';
  3844. };
  3845. if(typeof(exports) !== 'undefined') {
  3846. exports.quat = quat;
  3847. }
  3848. ;
  3849. })(shim.exports);
  3850. })(this);