1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331 |
- (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){
- /*
- * Copyright 2015 Google Inc. All Rights Reserved.
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * The base class for all VR devices.
- */
- function VRDevice() {
- this.hardwareUnitId = 'webvr-polyfill hardwareUnitId';
- this.deviceId = 'webvr-polyfill deviceId';
- this.deviceName = 'webvr-polyfill deviceName';
- }
- /**
- * The base class for all VR HMD devices.
- */
- function HMDVRDevice() {
- }
- HMDVRDevice.prototype = new VRDevice();
- /**
- * The base class for all VR position sensor devices.
- */
- function PositionSensorVRDevice() {
- }
- PositionSensorVRDevice.prototype = new VRDevice();
- module.exports.VRDevice = VRDevice;
- module.exports.HMDVRDevice = HMDVRDevice;
- module.exports.PositionSensorVRDevice = PositionSensorVRDevice;
- },{}],2:[function(_dereq_,module,exports){
- /*
- * Copyright 2015 Google Inc. All Rights Reserved.
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var HMDVRDevice = _dereq_('./base.js').HMDVRDevice;
- // Constants from vrtoolkit: https://github.com/googlesamples/cardboard-java.
- var DEFAULT_INTERPUPILLARY_DISTANCE = 0.06;
- var DEFAULT_FIELD_OF_VIEW = 40;
- var Eye = {
- LEFT: 'left',
- RIGHT: 'right'
- };
- /**
- * The HMD itself, providing rendering parameters.
- */
- function CardboardHMDVRDevice() {
- // From com/google/vrtoolkit/cardboard/FieldOfView.java.
- this.setMonocularFieldOfView_(DEFAULT_FIELD_OF_VIEW);
- // Set display constants.
- this.setInterpupillaryDistance(DEFAULT_INTERPUPILLARY_DISTANCE);
- }
- CardboardHMDVRDevice.prototype = new HMDVRDevice();
- CardboardHMDVRDevice.prototype.getEyeParameters = function(whichEye) {
- var eyeTranslation;
- var fieldOfView;
- var renderRect;
- if (whichEye == Eye.LEFT) {
- eyeTranslation = this.eyeTranslationLeft;
- fieldOfView = this.fieldOfViewLeft;
- renderRect = this.renderRectLeft;
- } else if (whichEye == Eye.RIGHT) {
- eyeTranslation = this.eyeTranslationRight;
- fieldOfView = this.fieldOfViewRight;
- renderRect = this.renderRectRight;
- } else {
- console.error('Invalid eye provided: %s', whichEye);
- return null;
- }
- return {
- recommendedFieldOfView: fieldOfView,
- eyeTranslation: eyeTranslation,
- renderRect: renderRect
- };
- };
- /**
- * Sets the field of view for both eyes. This is according to WebVR spec:
- *
- * @param {FieldOfView} opt_fovLeft Field of view of the left eye.
- * @param {FieldOfView} opt_fovRight Field of view of the right eye.
- * @param {Number} opt_zNear The near plane.
- * @param {Number} opt_zFar The far plane.
- *
- * http://mozvr.github.io/webvr-spec/webvr.html#dom-hmdvrdevice-setfieldofviewleftfov-rightfov-znear-zfar
- */
- CardboardHMDVRDevice.prototype.setFieldOfView =
- function(opt_fovLeft, opt_fovRight, opt_zNear, opt_zFar) {
- if (opt_fovLeft) {
- this.fieldOfViewLeft = opt_fovLeft;
- }
- if (opt_fovRight) {
- this.fieldOfViewRight = opt_fovRight;
- }
- if (opt_zNear) {
- this.zNear = opt_zNear;
- }
- if (opt_zFar) {
- this.zFar = opt_zFar;
- }
- };
- /**
- * Changes the interpupillary distance of the rendered scene. This is useful for
- * changing Cardboard viewers.
- *
- * Possibly a useful addition to the WebVR spec?
- *
- * @param {Number} ipd Distance between eyes.
- */
- CardboardHMDVRDevice.prototype.setInterpupillaryDistance = function(ipd) {
- this.eyeTranslationLeft = {
- x: ipd * -0.5,
- y: 0,
- z: 0
- };
- this.eyeTranslationRight = {
- x: ipd * 0.5,
- y: 0,
- z: 0
- };
- };
- /**
- * Changes the render rect (ie. viewport) where each eye is rendered. Again,
- * useful for changing Cardboard viewers.
- *
- * @param {Rect} opt_rectLeft Viewport for left eye.
- * @param {Rect} opt_rectRight Viewport for right eye.
- */
- CardboardHMDVRDevice.prototype.setRenderRect = function(opt_rectLeft, opt_rectRight) {
- if (opt_rectLeft) {
- this.renderRectLeft = opt_rectLeft;
- }
- if (opt_rectRight) {
- this.renderRectRight = opt_rectRight;
- }
- };
- /**
- * Sets a symmetrical field of view for both eyes, with just one angle.
- *
- * @param {Number} angle Angle in degrees of left, right, top and bottom for
- * both eyes.
- */
- CardboardHMDVRDevice.prototype.setMonocularFieldOfView_ = function(angle) {
- this.setFieldOfView(this.createSymmetricalFieldOfView_(angle),
- this.createSymmetricalFieldOfView_(angle));
- };
- CardboardHMDVRDevice.prototype.createSymmetricalFieldOfView_ = function(angle) {
- return {
- upDegrees: angle,
- downDegrees: angle,
- leftDegrees: angle,
- rightDegrees: angle
- };
- };
- module.exports = CardboardHMDVRDevice;
- },{"./base.js":1}],3:[function(_dereq_,module,exports){
- /*
- * Copyright 2015 Google Inc. All Rights Reserved.
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * TODO: Fix up all "new THREE" instantiations to improve performance.
- */
- var SensorSample = _dereq_('./sensor-sample.js');
- var THREE = _dereq_('./three-math.js');
- var Util = _dereq_('./util.js');
- var DEBUG = false;
- /**
- * An implementation of a simple complementary filter, which fuses gyroscope and
- * accelerometer data from the 'devicemotion' event.
- *
- * Accelerometer data is very noisy, but stable over the long term.
- * Gyroscope data is smooth, but tends to drift over the long term.
- *
- * This fusion is relatively simple:
- * 1. Get orientation estimates from accelerometer by applying a low-pass filter
- * on that data.
- * 2. Get orientation estimates from gyroscope by integrating over time.
- * 3. Combine the two estimates, weighing (1) in the long term, but (2) for the
- * short term.
- */
- function ComplementaryFilter(kFilter) {
- this.kFilter = kFilter;
- // Raw sensor measurements.
- this.currentAccelMeasurement = new SensorSample();
- this.currentGyroMeasurement = new SensorSample();
- this.previousGyroMeasurement = new SensorSample();
- // Current filter orientation
- this.filterQ = new THREE.Quaternion();
- this.previousFilterQ = new THREE.Quaternion();
- // Orientation based on the accelerometer.
- this.accelQ = new THREE.Quaternion();
- // Whether or not the orientation has been initialized.
- this.isOrientationInitialized = false;
- // Running estimate of gravity based on the current orientation.
- this.estimatedGravity = new THREE.Vector3();
- // Measured gravity based on accelerometer.
- this.measuredGravity = new THREE.Vector3();
- // Debug only quaternion of gyro-based orientation.
- this.gyroIntegralQ = new THREE.Quaternion();
- }
- ComplementaryFilter.prototype.addAccelMeasurement = function(vector, timestampS) {
- this.currentAccelMeasurement.set(vector, timestampS);
- };
- ComplementaryFilter.prototype.addGyroMeasurement = function(vector, timestampS) {
- this.currentGyroMeasurement.set(vector, timestampS);
- var deltaT = timestampS - this.previousGyroMeasurement.timestampS;
- if (Util.isTimestampDeltaValid(deltaT)) {
- this.run_();
- }
-
- this.previousGyroMeasurement.copy(this.currentGyroMeasurement);
- };
- ComplementaryFilter.prototype.run_ = function() {
- if (!this.isOrientationInitialized) {
- this.accelQ = this.accelToQuaternion_(this.currentAccelMeasurement.sample);
- this.previousFilterQ.copy(this.accelQ);
- this.isOrientationInitialized = true;
- return;
- }
- var deltaT = this.currentGyroMeasurement.timestampS -
- this.previousGyroMeasurement.timestampS;
- // Convert gyro rotation vector to a quaternion delta.
- var gyroDeltaQ = this.gyroToQuaternionDelta_(this.currentGyroMeasurement.sample, deltaT);
- this.gyroIntegralQ.multiply(gyroDeltaQ);
- // filter_1 = K * (filter_0 + gyro * dT) + (1 - K) * accel.
- this.filterQ.copy(this.previousFilterQ);
- this.filterQ.multiply(gyroDeltaQ);
- // Calculate the delta between the current estimated gravity and the real
- // gravity vector from accelerometer.
- var invFilterQ = new THREE.Quaternion();
- invFilterQ.copy(this.filterQ);
- invFilterQ.inverse();
- this.estimatedGravity.set(0, 0, -1);
- this.estimatedGravity.applyQuaternion(invFilterQ);
- this.estimatedGravity.normalize();
- this.measuredGravity.copy(this.currentAccelMeasurement.sample);
- this.measuredGravity.normalize();
- // Compare estimated gravity with measured gravity, get the delta quaternion
- // between the two.
- var deltaQ = new THREE.Quaternion();
- deltaQ.setFromUnitVectors(this.estimatedGravity, this.measuredGravity);
- deltaQ.inverse();
- if (DEBUG) {
- console.log('Delta: %d deg, G_est: (%s, %s, %s), G_meas: (%s, %s, %s)',
- THREE.Math.radToDeg(Util.getQuaternionAngle(deltaQ)),
- (this.estimatedGravity.x).toFixed(1),
- (this.estimatedGravity.y).toFixed(1),
- (this.estimatedGravity.z).toFixed(1),
- (this.measuredGravity.x).toFixed(1),
- (this.measuredGravity.y).toFixed(1),
- (this.measuredGravity.z).toFixed(1));
- }
- // Calculate the SLERP target: current orientation plus the measured-estimated
- // quaternion delta.
- var targetQ = new THREE.Quaternion();
- targetQ.copy(this.filterQ);
- targetQ.multiply(deltaQ);
- // SLERP factor: 0 is pure gyro, 1 is pure accel.
- this.filterQ.slerp(targetQ, 1 - this.kFilter);
- this.previousFilterQ.copy(this.filterQ);
- };
- ComplementaryFilter.prototype.getOrientation = function() {
- return this.filterQ;
- };
- ComplementaryFilter.prototype.accelToQuaternion_ = function(accel) {
- var normAccel = new THREE.Vector3();
- normAccel.copy(accel);
- normAccel.normalize();
- var quat = new THREE.Quaternion();
- quat.setFromUnitVectors(new THREE.Vector3(0, 0, -1), normAccel);
- quat.inverse();
- return quat;
- };
- ComplementaryFilter.prototype.gyroToQuaternionDelta_ = function(gyro, dt) {
- // Extract axis and angle from the gyroscope data.
- var quat = new THREE.Quaternion();
- var axis = new THREE.Vector3();
- axis.copy(gyro);
- axis.normalize();
- quat.setFromAxisAngle(axis, gyro.length() * dt);
- return quat;
- };
- module.exports = ComplementaryFilter;
- },{"./sensor-sample.js":8,"./three-math.js":9,"./util.js":11}],4:[function(_dereq_,module,exports){
- /*
- * Copyright 2015 Google Inc. All Rights Reserved.
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var PositionSensorVRDevice = _dereq_('./base.js').PositionSensorVRDevice;
- var ComplementaryFilter = _dereq_('./complementary-filter.js');
- var PosePredictor = _dereq_('./pose-predictor.js');
- var TouchPanner = _dereq_('./touch-panner.js');
- var THREE = _dereq_('./three-math.js');
- var Util = _dereq_('./util.js');
- /**
- * The positional sensor, implemented using DeviceMotion APIs.
- */
- function FusionPositionSensorVRDevice() {
- this.deviceId = 'webvr-polyfill:fused';
- this.deviceName = 'VR Position Device (webvr-polyfill:fused)';
- this.accelerometer = new THREE.Vector3();
- this.gyroscope = new THREE.Vector3();
- window.addEventListener('devicemotion', this.onDeviceMotionChange_.bind(this));
- window.addEventListener('orientationchange', this.onScreenOrientationChange_.bind(this));
- this.filter = new ComplementaryFilter(WebVRConfig.K_FILTER || 0.98);
- this.posePredictor = new PosePredictor(WebVRConfig.PREDICTION_TIME_S || 0.040);
- this.touchPanner = new TouchPanner();
- this.filterToWorldQ = new THREE.Quaternion();
- // Set the filter to world transform, depending on OS.
- if (Util.isIOS()) {
- this.filterToWorldQ.setFromAxisAngle(new THREE.Vector3(1, 0, 0), Math.PI/2);
- } else {
- this.filterToWorldQ.setFromAxisAngle(new THREE.Vector3(1, 0, 0), -Math.PI/2);
- }
- this.worldToScreenQ = new THREE.Quaternion();
- this.setScreenTransform_();
- // Keep track of a reset transform for resetSensor.
- this.resetQ = new THREE.Quaternion();
- this.isFirefoxAndroid = Util.isFirefoxAndroid();
- this.isIOS = Util.isIOS();
- }
- FusionPositionSensorVRDevice.prototype = new PositionSensorVRDevice();
- /**
- * Returns {orientation: {x,y,z,w}, position: null}.
- * Position is not supported since we can't do 6DOF.
- */
- FusionPositionSensorVRDevice.prototype.getState = function() {
- return {
- hasOrientation: true,
- orientation: this.getOrientation(),
- hasPosition: false,
- position: null
- }
- };
- FusionPositionSensorVRDevice.prototype.getOrientation = function() {
- // Convert from filter space to the the same system used by the
- // deviceorientation event.
- var orientation = this.filter.getOrientation();
- // Predict orientation.
- this.predictedQ = this.posePredictor.getPrediction(orientation, this.gyroscope, this.previousTimestampS);
- // Convert to THREE coordinate system: -Z forward, Y up, X right.
- var out = new THREE.Quaternion();
- out.copy(this.filterToWorldQ);
- out.multiply(this.resetQ);
- if (!WebVRConfig.TOUCH_PANNER_DISABLED) {
- out.multiply(this.touchPanner.getOrientation());
- }
- out.multiply(this.predictedQ);
- out.multiply(this.worldToScreenQ);
- // Handle the yaw-only case.
- if (WebVRConfig.YAW_ONLY) {
- // Make a quaternion that only turns around the Y-axis.
- out.x = 0;
- out.z = 0;
- out.normalize();
- }
- return out;
- };
- FusionPositionSensorVRDevice.prototype.resetSensor = function() {
- var euler = new THREE.Euler();
- euler.setFromQuaternion(this.filter.getOrientation());
- var yaw = euler.y;
- console.log('resetSensor with yaw: %f', yaw);
- this.resetQ.setFromAxisAngle(new THREE.Vector3(0, 0, 1), -yaw);
- if (!WebVRConfig.TOUCH_PANNER_DISABLED) {
- this.touchPanner.resetSensor();
- }
- };
- FusionPositionSensorVRDevice.prototype.onDeviceMotionChange_ = function(deviceMotion) {
- var accGravity = deviceMotion.accelerationIncludingGravity;
- var rotRate = deviceMotion.rotationRate;
- var timestampS = deviceMotion.timeStamp / 1000;
- // Firefox Android timeStamp returns one thousandth of a millisecond.
- if (this.isFirefoxAndroid) {
- timestampS /= 1000;
- }
- var deltaS = timestampS - this.previousTimestampS;
- if (deltaS <= Util.MIN_TIMESTEP || deltaS > Util.MAX_TIMESTEP) {
- console.warn('Invalid timestamps detected. Time step between successive ' +
- 'gyroscope sensor samples is very small or not monotonic');
- this.previousTimestampS = timestampS;
- return;
- }
- this.accelerometer.set(-accGravity.x, -accGravity.y, -accGravity.z);
- this.gyroscope.set(rotRate.alpha, rotRate.beta, rotRate.gamma);
- // With iOS and Firefox Android, rotationRate is reported in degrees,
- // so we first convert to radians.
- if (this.isIOS || this.isFirefoxAndroid) {
- this.gyroscope.multiplyScalar(Math.PI / 180);
- }
- this.filter.addAccelMeasurement(this.accelerometer, timestampS);
- this.filter.addGyroMeasurement(this.gyroscope, timestampS);
- this.previousTimestampS = timestampS;
- };
- FusionPositionSensorVRDevice.prototype.onScreenOrientationChange_ =
- function(screenOrientation) {
- this.setScreenTransform_();
- };
- FusionPositionSensorVRDevice.prototype.setScreenTransform_ = function() {
- this.worldToScreenQ.set(0, 0, 0, 1);
- switch (window.orientation) {
- case 0:
- break;
- case 90:
- this.worldToScreenQ.setFromAxisAngle(new THREE.Vector3(0, 0, 1), -Math.PI/2);
- break;
- case -90:
- this.worldToScreenQ.setFromAxisAngle(new THREE.Vector3(0, 0, 1), Math.PI/2);
- break;
- case 180:
- // TODO.
- break;
- }
- };
- module.exports = FusionPositionSensorVRDevice;
- },{"./base.js":1,"./complementary-filter.js":3,"./pose-predictor.js":7,"./three-math.js":9,"./touch-panner.js":10,"./util.js":11}],5:[function(_dereq_,module,exports){
- /*
- * Copyright 2015 Google Inc. All Rights Reserved.
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var WebVRPolyfill = _dereq_('./webvr-polyfill.js');
- // Initialize a WebVRConfig just in case.
- window.WebVRConfig = window.WebVRConfig || {};
- new WebVRPolyfill();
- },{"./webvr-polyfill.js":12}],6:[function(_dereq_,module,exports){
- /*
- * Copyright 2015 Google Inc. All Rights Reserved.
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var PositionSensorVRDevice = _dereq_('./base.js').PositionSensorVRDevice;
- var THREE = _dereq_('./three-math.js');
- var Util = _dereq_('./util.js');
- // How much to rotate per key stroke.
- var KEY_SPEED = 0.15;
- var KEY_ANIMATION_DURATION = 80;
- // How much to rotate for mouse events.
- var MOUSE_SPEED_X = 0.5;
- var MOUSE_SPEED_Y = 0.3;
- /**
- * A virtual position sensor, implemented using keyboard and
- * mouse APIs. This is designed as for desktops/laptops where no Device*
- * events work.
- */
- function MouseKeyboardPositionSensorVRDevice() {
- this.deviceId = 'webvr-polyfill:mouse-keyboard';
- this.deviceName = 'VR Position Device (webvr-polyfill:mouse-keyboard)';
- // Attach to mouse and keyboard events.
- window.addEventListener('keydown', this.onKeyDown_.bind(this));
- window.addEventListener('mousemove', this.onMouseMove_.bind(this));
- window.addEventListener('mousedown', this.onMouseDown_.bind(this));
- window.addEventListener('mouseup', this.onMouseUp_.bind(this));
- this.phi = 0;
- this.theta = 0;
- // Variables for keyboard-based rotation animation.
- this.targetAngle = null;
- // State variables for calculations.
- this.euler = new THREE.Euler();
- this.orientation = new THREE.Quaternion();
- // Variables for mouse-based rotation.
- this.rotateStart = new THREE.Vector2();
- this.rotateEnd = new THREE.Vector2();
- this.rotateDelta = new THREE.Vector2();
- }
- MouseKeyboardPositionSensorVRDevice.prototype = new PositionSensorVRDevice();
- /**
- * Returns {orientation: {x,y,z,w}, position: null}.
- * Position is not supported for parity with other PositionSensors.
- */
- MouseKeyboardPositionSensorVRDevice.prototype.getState = function() {
- this.euler.set(this.phi, this.theta, 0, 'YXZ');
- this.orientation.setFromEuler(this.euler);
- return {
- hasOrientation: true,
- orientation: this.orientation,
- hasPosition: false,
- position: null
- }
- };
- MouseKeyboardPositionSensorVRDevice.prototype.onKeyDown_ = function(e) {
- // Track WASD and arrow keys.
- if (e.keyCode == 38) { // Up key.
- this.animatePhi_(this.phi + KEY_SPEED);
- } else if (e.keyCode == 39) { // Right key.
- this.animateTheta_(this.theta - KEY_SPEED);
- } else if (e.keyCode == 40) { // Down key.
- this.animatePhi_(this.phi - KEY_SPEED);
- } else if (e.keyCode == 37) { // Left key.
- this.animateTheta_(this.theta + KEY_SPEED);
- }
- };
- MouseKeyboardPositionSensorVRDevice.prototype.animateTheta_ = function(targetAngle) {
- this.animateKeyTransitions_('theta', targetAngle);
- };
- MouseKeyboardPositionSensorVRDevice.prototype.animatePhi_ = function(targetAngle) {
- // Prevent looking too far up or down.
- targetAngle = Util.clamp(targetAngle, -Math.PI/2, Math.PI/2);
- this.animateKeyTransitions_('phi', targetAngle);
- };
- /**
- * Start an animation to transition an angle from one value to another.
- */
- MouseKeyboardPositionSensorVRDevice.prototype.animateKeyTransitions_ = function(angleName, targetAngle) {
- // If an animation is currently running, cancel it.
- if (this.angleAnimation) {
- clearInterval(this.angleAnimation);
- }
- var startAngle = this[angleName];
- var startTime = new Date();
- // Set up an interval timer to perform the animation.
- this.angleAnimation = setInterval(function() {
- // Once we're finished the animation, we're done.
- var elapsed = new Date() - startTime;
- if (elapsed >= KEY_ANIMATION_DURATION) {
- this[angleName] = targetAngle;
- clearInterval(this.angleAnimation);
- return;
- }
- // Linearly interpolate the angle some amount.
- var percent = elapsed / KEY_ANIMATION_DURATION;
- this[angleName] = startAngle + (targetAngle - startAngle) * percent;
- }.bind(this), 1000/60);
- };
- MouseKeyboardPositionSensorVRDevice.prototype.onMouseDown_ = function(e) {
- this.rotateStart.set(e.clientX, e.clientY);
- this.isDragging = true;
- };
- // Very similar to https://gist.github.com/mrflix/8351020
- MouseKeyboardPositionSensorVRDevice.prototype.onMouseMove_ = function(e) {
- if (!this.isDragging && !this.isPointerLocked_()) {
- return;
- }
- // Support pointer lock API.
- if (this.isPointerLocked_()) {
- var movementX = e.movementX || e.mozMovementX || 0;
- var movementY = e.movementY || e.mozMovementY || 0;
- this.rotateEnd.set(this.rotateStart.x - movementX, this.rotateStart.y - movementY);
- } else {
- this.rotateEnd.set(e.clientX, e.clientY);
- }
- // Calculate how much we moved in mouse space.
- this.rotateDelta.subVectors(this.rotateEnd, this.rotateStart);
- this.rotateStart.copy(this.rotateEnd);
- // Keep track of the cumulative euler angles.
- var element = document.body;
- this.phi += 2 * Math.PI * this.rotateDelta.y / element.clientHeight * MOUSE_SPEED_Y;
- this.theta += 2 * Math.PI * this.rotateDelta.x / element.clientWidth * MOUSE_SPEED_X;
- // Prevent looking too far up or down.
- this.phi = Util.clamp(this.phi, -Math.PI/2, Math.PI/2);
- };
- MouseKeyboardPositionSensorVRDevice.prototype.onMouseUp_ = function(e) {
- this.isDragging = false;
- };
- MouseKeyboardPositionSensorVRDevice.prototype.isPointerLocked_ = function() {
- var el = document.pointerLockElement || document.mozPointerLockElement ||
- document.webkitPointerLockElement;
- return el !== undefined;
- };
- MouseKeyboardPositionSensorVRDevice.prototype.resetSensor = function() {
- console.error('Not implemented yet.');
- };
- module.exports = MouseKeyboardPositionSensorVRDevice;
- },{"./base.js":1,"./three-math.js":9,"./util.js":11}],7:[function(_dereq_,module,exports){
- /*
- * Copyright 2015 Google Inc. All Rights Reserved.
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var THREE = _dereq_('./three-math.js');
- var DEBUG = false;
- /**
- * Given an orientation and the gyroscope data, predicts the future orientation
- * of the head. This makes rendering appear faster.
- *
- * Also see: http://msl.cs.uiuc.edu/~lavalle/papers/LavYerKatAnt14.pdf
- *
- * @param {Number} predictionTimeS time from head movement to the appearance of
- * the corresponding image.
- */
- function PosePredictor(predictionTimeS) {
- this.predictionTimeS = predictionTimeS;
- // The quaternion corresponding to the previous state.
- this.previousQ = new THREE.Quaternion();
- // Previous time a prediction occurred.
- this.previousTimestampS = null;
- // The delta quaternion that adjusts the current pose.
- this.deltaQ = new THREE.Quaternion();
- // The output quaternion.
- this.outQ = new THREE.Quaternion();
- }
- PosePredictor.prototype.getPrediction = function(currentQ, gyro, timestampS) {
- if (!this.previousTimestampS) {
- this.previousQ.copy(currentQ);
- this.previousTimestampS = timestampS;
- return currentQ;
- }
- // Calculate axis and angle based on gyroscope rotation rate data.
- var axis = new THREE.Vector3();
- axis.copy(gyro);
- axis.normalize();
- var angularSpeed = gyro.length();
- // If we're rotating slowly, don't do prediction.
- if (angularSpeed < THREE.Math.degToRad(20)) {
- if (DEBUG) {
- console.log('Moving slowly, at %s deg/s: no prediction',
- THREE.Math.radToDeg(angularSpeed).toFixed(1));
- }
- this.outQ.copy(currentQ);
- this.previousQ.copy(currentQ);
- return this.outQ;
- }
- // Get the predicted angle based on the time delta and latency.
- var deltaT = timestampS - this.previousTimestampS;
- var predictAngle = angularSpeed * this.predictionTimeS;
- this.deltaQ.setFromAxisAngle(axis, predictAngle);
- this.outQ.copy(this.previousQ);
- this.outQ.multiply(this.deltaQ);
- this.previousQ.copy(currentQ);
- return this.outQ;
- };
- module.exports = PosePredictor;
- },{"./three-math.js":9}],8:[function(_dereq_,module,exports){
- function SensorSample(sample, timestampS) {
- this.set(sample, timestampS);
- };
- SensorSample.prototype.set = function(sample, timestampS) {
- this.sample = sample;
- this.timestampS = timestampS;
- };
- SensorSample.prototype.copy = function(sensorSample) {
- this.set(sensorSample.sample, sensorSample.timestampS);
- };
- module.exports = SensorSample;
- },{}],9:[function(_dereq_,module,exports){
- /*
- * A subset of THREE.js, providing mostly quaternion and euler-related
- * operations, manually lifted from
- * https://github.com/mrdoob/three.js/tree/master/src/math, as of 9c30286b38df039fca389989ff06ea1c15d6bad1
- */
- // Only use if the real THREE is not provided.
- var THREE = window.THREE || {};
- // If some piece of THREE is missing, fill it in here.
- if (!THREE.Quaternion || !THREE.Vector3 || !THREE.Vector2 || !THREE.Euler || !THREE.Math) {
- console.log('No THREE.js found.');
- /*** START Quaternion ***/
- /**
- * @author mikael emtinger / http://gomo.se/
- * @author alteredq / http://alteredqualia.com/
- * @author WestLangley / http://github.com/WestLangley
- * @author bhouston / http://exocortex.com
- */
- THREE.Quaternion = function ( x, y, z, w ) {
- this._x = x || 0;
- this._y = y || 0;
- this._z = z || 0;
- this._w = ( w !== undefined ) ? w : 1;
- };
- THREE.Quaternion.prototype = {
- constructor: THREE.Quaternion,
- _x: 0,_y: 0, _z: 0, _w: 0,
- get x () {
- return this._x;
- },
- set x ( value ) {
- this._x = value;
- this.onChangeCallback();
- },
- get y () {
- return this._y;
- },
- set y ( value ) {
- this._y = value;
- this.onChangeCallback();
- },
- get z () {
- return this._z;
- },
- set z ( value ) {
- this._z = value;
- this.onChangeCallback();
- },
- get w () {
- return this._w;
- },
- set w ( value ) {
- this._w = value;
- this.onChangeCallback();
- },
- set: function ( x, y, z, w ) {
- this._x = x;
- this._y = y;
- this._z = z;
- this._w = w;
- this.onChangeCallback();
- return this;
- },
- copy: function ( quaternion ) {
- this._x = quaternion.x;
- this._y = quaternion.y;
- this._z = quaternion.z;
- this._w = quaternion.w;
- this.onChangeCallback();
- return this;
- },
- setFromEuler: function ( euler, update ) {
- if ( euler instanceof THREE.Euler === false ) {
- throw new Error( 'THREE.Quaternion: .setFromEuler() now expects a Euler rotation rather than a Vector3 and order.' );
- }
- // http://www.mathworks.com/matlabcentral/fileexchange/
- // 20696-function-to-convert-between-dcm-euler-angles-quaternions-and-euler-vectors/
- // content/SpinCalc.m
- var c1 = Math.cos( euler._x / 2 );
- var c2 = Math.cos( euler._y / 2 );
- var c3 = Math.cos( euler._z / 2 );
- var s1 = Math.sin( euler._x / 2 );
- var s2 = Math.sin( euler._y / 2 );
- var s3 = Math.sin( euler._z / 2 );
- if ( euler.order === 'XYZ' ) {
- this._x = s1 * c2 * c3 + c1 * s2 * s3;
- this._y = c1 * s2 * c3 - s1 * c2 * s3;
- this._z = c1 * c2 * s3 + s1 * s2 * c3;
- this._w = c1 * c2 * c3 - s1 * s2 * s3;
- } else if ( euler.order === 'YXZ' ) {
- this._x = s1 * c2 * c3 + c1 * s2 * s3;
- this._y = c1 * s2 * c3 - s1 * c2 * s3;
- this._z = c1 * c2 * s3 - s1 * s2 * c3;
- this._w = c1 * c2 * c3 + s1 * s2 * s3;
- } else if ( euler.order === 'ZXY' ) {
- this._x = s1 * c2 * c3 - c1 * s2 * s3;
- this._y = c1 * s2 * c3 + s1 * c2 * s3;
- this._z = c1 * c2 * s3 + s1 * s2 * c3;
- this._w = c1 * c2 * c3 - s1 * s2 * s3;
- } else if ( euler.order === 'ZYX' ) {
- this._x = s1 * c2 * c3 - c1 * s2 * s3;
- this._y = c1 * s2 * c3 + s1 * c2 * s3;
- this._z = c1 * c2 * s3 - s1 * s2 * c3;
- this._w = c1 * c2 * c3 + s1 * s2 * s3;
- } else if ( euler.order === 'YZX' ) {
- this._x = s1 * c2 * c3 + c1 * s2 * s3;
- this._y = c1 * s2 * c3 + s1 * c2 * s3;
- this._z = c1 * c2 * s3 - s1 * s2 * c3;
- this._w = c1 * c2 * c3 - s1 * s2 * s3;
- } else if ( euler.order === 'XZY' ) {
- this._x = s1 * c2 * c3 - c1 * s2 * s3;
- this._y = c1 * s2 * c3 - s1 * c2 * s3;
- this._z = c1 * c2 * s3 + s1 * s2 * c3;
- this._w = c1 * c2 * c3 + s1 * s2 * s3;
- }
- if ( update !== false ) this.onChangeCallback();
- return this;
- },
- setFromAxisAngle: function ( axis, angle ) {
- // http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToQuaternion/index.htm
- // assumes axis is normalized
- var halfAngle = angle / 2, s = Math.sin( halfAngle );
- this._x = axis.x * s;
- this._y = axis.y * s;
- this._z = axis.z * s;
- this._w = Math.cos( halfAngle );
- this.onChangeCallback();
- return this;
- },
- setFromRotationMatrix: function ( m ) {
- // http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm
- // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
- var te = m.elements,
- m11 = te[ 0 ], m12 = te[ 4 ], m13 = te[ 8 ],
- m21 = te[ 1 ], m22 = te[ 5 ], m23 = te[ 9 ],
- m31 = te[ 2 ], m32 = te[ 6 ], m33 = te[ 10 ],
- trace = m11 + m22 + m33,
- s;
- if ( trace > 0 ) {
- s = 0.5 / Math.sqrt( trace + 1.0 );
- this._w = 0.25 / s;
- this._x = ( m32 - m23 ) * s;
- this._y = ( m13 - m31 ) * s;
- this._z = ( m21 - m12 ) * s;
- } else if ( m11 > m22 && m11 > m33 ) {
- s = 2.0 * Math.sqrt( 1.0 + m11 - m22 - m33 );
- this._w = ( m32 - m23 ) / s;
- this._x = 0.25 * s;
- this._y = ( m12 + m21 ) / s;
- this._z = ( m13 + m31 ) / s;
- } else if ( m22 > m33 ) {
- s = 2.0 * Math.sqrt( 1.0 + m22 - m11 - m33 );
- this._w = ( m13 - m31 ) / s;
- this._x = ( m12 + m21 ) / s;
- this._y = 0.25 * s;
- this._z = ( m23 + m32 ) / s;
- } else {
- s = 2.0 * Math.sqrt( 1.0 + m33 - m11 - m22 );
- this._w = ( m21 - m12 ) / s;
- this._x = ( m13 + m31 ) / s;
- this._y = ( m23 + m32 ) / s;
- this._z = 0.25 * s;
- }
- this.onChangeCallback();
- return this;
- },
- setFromUnitVectors: function () {
- // http://lolengine.net/blog/2014/02/24/quaternion-from-two-vectors-final
- // assumes direction vectors vFrom and vTo are normalized
- var v1, r;
- var EPS = 0.000001;
- return function ( vFrom, vTo ) {
- if ( v1 === undefined ) v1 = new THREE.Vector3();
- r = vFrom.dot( vTo ) + 1;
- if ( r < EPS ) {
- r = 0;
- if ( Math.abs( vFrom.x ) > Math.abs( vFrom.z ) ) {
- v1.set( - vFrom.y, vFrom.x, 0 );
- } else {
- v1.set( 0, - vFrom.z, vFrom.y );
- }
- } else {
- v1.crossVectors( vFrom, vTo );
- }
- this._x = v1.x;
- this._y = v1.y;
- this._z = v1.z;
- this._w = r;
- this.normalize();
- return this;
- }
- }(),
- inverse: function () {
- this.conjugate().normalize();
- return this;
- },
- conjugate: function () {
- this._x *= - 1;
- this._y *= - 1;
- this._z *= - 1;
- this.onChangeCallback();
- return this;
- },
- dot: function ( v ) {
- return this._x * v._x + this._y * v._y + this._z * v._z + this._w * v._w;
- },
- lengthSq: function () {
- return this._x * this._x + this._y * this._y + this._z * this._z + this._w * this._w;
- },
- length: function () {
- return Math.sqrt( this._x * this._x + this._y * this._y + this._z * this._z + this._w * this._w );
- },
- normalize: function () {
- var l = this.length();
- if ( l === 0 ) {
- this._x = 0;
- this._y = 0;
- this._z = 0;
- this._w = 1;
- } else {
- l = 1 / l;
- this._x = this._x * l;
- this._y = this._y * l;
- this._z = this._z * l;
- this._w = this._w * l;
- }
- this.onChangeCallback();
- return this;
- },
- multiply: function ( q, p ) {
- if ( p !== undefined ) {
- console.warn( 'THREE.Quaternion: .multiply() now only accepts one argument. Use .multiplyQuaternions( a, b ) instead.' );
- return this.multiplyQuaternions( q, p );
- }
- return this.multiplyQuaternions( this, q );
- },
- multiplyQuaternions: function ( a, b ) {
- // from http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/code/index.htm
- var qax = a._x, qay = a._y, qaz = a._z, qaw = a._w;
- var qbx = b._x, qby = b._y, qbz = b._z, qbw = b._w;
- this._x = qax * qbw + qaw * qbx + qay * qbz - qaz * qby;
- this._y = qay * qbw + qaw * qby + qaz * qbx - qax * qbz;
- this._z = qaz * qbw + qaw * qbz + qax * qby - qay * qbx;
- this._w = qaw * qbw - qax * qbx - qay * qby - qaz * qbz;
- this.onChangeCallback();
- return this;
- },
- multiplyVector3: function ( vector ) {
- console.warn( 'THREE.Quaternion: .multiplyVector3() has been removed. Use is now vector.applyQuaternion( quaternion ) instead.' );
- return vector.applyQuaternion( this );
- },
- slerp: function ( qb, t ) {
- if ( t === 0 ) return this;
- if ( t === 1 ) return this.copy( qb );
- var x = this._x, y = this._y, z = this._z, w = this._w;
- // http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/
- var cosHalfTheta = w * qb._w + x * qb._x + y * qb._y + z * qb._z;
- if ( cosHalfTheta < 0 ) {
- this._w = - qb._w;
- this._x = - qb._x;
- this._y = - qb._y;
- this._z = - qb._z;
- cosHalfTheta = - cosHalfTheta;
- } else {
- this.copy( qb );
- }
- if ( cosHalfTheta >= 1.0 ) {
- this._w = w;
- this._x = x;
- this._y = y;
- this._z = z;
- return this;
- }
- var halfTheta = Math.acos( cosHalfTheta );
- var sinHalfTheta = Math.sqrt( 1.0 - cosHalfTheta * cosHalfTheta );
- if ( Math.abs( sinHalfTheta ) < 0.001 ) {
- this._w = 0.5 * ( w + this._w );
- this._x = 0.5 * ( x + this._x );
- this._y = 0.5 * ( y + this._y );
- this._z = 0.5 * ( z + this._z );
- return this;
- }
- var ratioA = Math.sin( ( 1 - t ) * halfTheta ) / sinHalfTheta,
- ratioB = Math.sin( t * halfTheta ) / sinHalfTheta;
- this._w = ( w * ratioA + this._w * ratioB );
- this._x = ( x * ratioA + this._x * ratioB );
- this._y = ( y * ratioA + this._y * ratioB );
- this._z = ( z * ratioA + this._z * ratioB );
- this.onChangeCallback();
- return this;
- },
- equals: function ( quaternion ) {
- return ( quaternion._x === this._x ) && ( quaternion._y === this._y ) && ( quaternion._z === this._z ) && ( quaternion._w === this._w );
- },
- fromArray: function ( array, offset ) {
- if ( offset === undefined ) offset = 0;
- this._x = array[ offset ];
- this._y = array[ offset + 1 ];
- this._z = array[ offset + 2 ];
- this._w = array[ offset + 3 ];
- this.onChangeCallback();
- return this;
- },
- toArray: function ( array, offset ) {
- if ( array === undefined ) array = [];
- if ( offset === undefined ) offset = 0;
- array[ offset ] = this._x;
- array[ offset + 1 ] = this._y;
- array[ offset + 2 ] = this._z;
- array[ offset + 3 ] = this._w;
- return array;
- },
- onChange: function ( callback ) {
- this.onChangeCallback = callback;
- return this;
- },
- onChangeCallback: function () {},
- clone: function () {
- return new THREE.Quaternion( this._x, this._y, this._z, this._w );
- }
- };
- THREE.Quaternion.slerp = function ( qa, qb, qm, t ) {
- return qm.copy( qa ).slerp( qb, t );
- }
- /*** END Quaternion ***/
- /*** START Vector2 ***/
- /**
- * @author mrdoob / http://mrdoob.com/
- * @author philogb / http://blog.thejit.org/
- * @author egraether / http://egraether.com/
- * @author zz85 / http://www.lab4games.net/zz85/blog
- */
- THREE.Vector2 = function ( x, y ) {
- this.x = x || 0;
- this.y = y || 0;
- };
- THREE.Vector2.prototype = {
- constructor: THREE.Vector2,
- set: function ( x, y ) {
- this.x = x;
- this.y = y;
- return this;
- },
- setX: function ( x ) {
- this.x = x;
- return this;
- },
- setY: function ( y ) {
- this.y = y;
- return this;
- },
- setComponent: function ( index, value ) {
- switch ( index ) {
- case 0: this.x = value; break;
- case 1: this.y = value; break;
- default: throw new Error( 'index is out of range: ' + index );
- }
- },
- getComponent: function ( index ) {
- switch ( index ) {
- case 0: return this.x;
- case 1: return this.y;
- default: throw new Error( 'index is out of range: ' + index );
- }
- },
- copy: function ( v ) {
- this.x = v.x;
- this.y = v.y;
- return this;
- },
- add: function ( v, w ) {
- if ( w !== undefined ) {
- console.warn( 'THREE.Vector2: .add() now only accepts one argument. Use .addVectors( a, b ) instead.' );
- return this.addVectors( v, w );
- }
- this.x += v.x;
- this.y += v.y;
- return this;
- },
- addVectors: function ( a, b ) {
- this.x = a.x + b.x;
- this.y = a.y + b.y;
- return this;
- },
- addScalar: function ( s ) {
- this.x += s;
- this.y += s;
- return this;
- },
- sub: function ( v, w ) {
- if ( w !== undefined ) {
- console.warn( 'THREE.Vector2: .sub() now only accepts one argument. Use .subVectors( a, b ) instead.' );
- return this.subVectors( v, w );
- }
- this.x -= v.x;
- this.y -= v.y;
- return this;
- },
- subVectors: function ( a, b ) {
- this.x = a.x - b.x;
- this.y = a.y - b.y;
- return this;
- },
- multiply: function ( v ) {
- this.x *= v.x;
- this.y *= v.y;
- return this;
- },
- multiplyScalar: function ( s ) {
- this.x *= s;
- this.y *= s;
- return this;
- },
- divide: function ( v ) {
- this.x /= v.x;
- this.y /= v.y;
- return this;
- },
- divideScalar: function ( scalar ) {
- if ( scalar !== 0 ) {
- var invScalar = 1 / scalar;
- this.x *= invScalar;
- this.y *= invScalar;
- } else {
- this.x = 0;
- this.y = 0;
- }
- return this;
- },
- min: function ( v ) {
- if ( this.x > v.x ) {
- this.x = v.x;
- }
- if ( this.y > v.y ) {
- this.y = v.y;
- }
- return this;
- },
- max: function ( v ) {
- if ( this.x < v.x ) {
- this.x = v.x;
- }
- if ( this.y < v.y ) {
- this.y = v.y;
- }
- return this;
- },
- clamp: function ( min, max ) {
- // This function assumes min < max, if this assumption isn't true it will not operate correctly
- if ( this.x < min.x ) {
- this.x = min.x;
- } else if ( this.x > max.x ) {
- this.x = max.x;
- }
- if ( this.y < min.y ) {
- this.y = min.y;
- } else if ( this.y > max.y ) {
- this.y = max.y;
- }
- return this;
- },
- clampScalar: ( function () {
- var min, max;
- return function ( minVal, maxVal ) {
- if ( min === undefined ) {
- min = new THREE.Vector2();
- max = new THREE.Vector2();
- }
- min.set( minVal, minVal );
- max.set( maxVal, maxVal );
- return this.clamp( min, max );
- };
- } )(),
- floor: function () {
- this.x = Math.floor( this.x );
- this.y = Math.floor( this.y );
- return this;
- },
- ceil: function () {
- this.x = Math.ceil( this.x );
- this.y = Math.ceil( this.y );
- return this;
- },
- round: function () {
- this.x = Math.round( this.x );
- this.y = Math.round( this.y );
- return this;
- },
- roundToZero: function () {
- this.x = ( this.x < 0 ) ? Math.ceil( this.x ) : Math.floor( this.x );
- this.y = ( this.y < 0 ) ? Math.ceil( this.y ) : Math.floor( this.y );
- return this;
- },
- negate: function () {
- this.x = - this.x;
- this.y = - this.y;
- return this;
- },
- dot: function ( v ) {
- return this.x * v.x + this.y * v.y;
- },
- lengthSq: function () {
- return this.x * this.x + this.y * this.y;
- },
- length: function () {
- return Math.sqrt( this.x * this.x + this.y * this.y );
- },
- normalize: function () {
- return this.divideScalar( this.length() );
- },
- distanceTo: function ( v ) {
- return Math.sqrt( this.distanceToSquared( v ) );
- },
- distanceToSquared: function ( v ) {
- var dx = this.x - v.x, dy = this.y - v.y;
- return dx * dx + dy * dy;
- },
- setLength: function ( l ) {
- var oldLength = this.length();
- if ( oldLength !== 0 && l !== oldLength ) {
- this.multiplyScalar( l / oldLength );
- }
- return this;
- },
- lerp: function ( v, alpha ) {
- this.x += ( v.x - this.x ) * alpha;
- this.y += ( v.y - this.y ) * alpha;
- return this;
- },
- equals: function ( v ) {
- return ( ( v.x === this.x ) && ( v.y === this.y ) );
- },
- fromArray: function ( array, offset ) {
- if ( offset === undefined ) offset = 0;
- this.x = array[ offset ];
- this.y = array[ offset + 1 ];
- return this;
- },
- toArray: function ( array, offset ) {
- if ( array === undefined ) array = [];
- if ( offset === undefined ) offset = 0;
- array[ offset ] = this.x;
- array[ offset + 1 ] = this.y;
- return array;
- },
- fromAttribute: function ( attribute, index, offset ) {
- if ( offset === undefined ) offset = 0;
- index = index * attribute.itemSize + offset;
- this.x = attribute.array[ index ];
- this.y = attribute.array[ index + 1 ];
- return this;
- },
- clone: function () {
- return new THREE.Vector2( this.x, this.y );
- }
- };
- /*** END Vector2 ***/
- /*** START Vector3 ***/
- /**
- * @author mrdoob / http://mrdoob.com/
- * @author *kile / http://kile.stravaganza.org/
- * @author philogb / http://blog.thejit.org/
- * @author mikael emtinger / http://gomo.se/
- * @author egraether / http://egraether.com/
- * @author WestLangley / http://github.com/WestLangley
- */
- THREE.Vector3 = function ( x, y, z ) {
- this.x = x || 0;
- this.y = y || 0;
- this.z = z || 0;
- };
- THREE.Vector3.prototype = {
- constructor: THREE.Vector3,
- set: function ( x, y, z ) {
- this.x = x;
- this.y = y;
- this.z = z;
- return this;
- },
- setX: function ( x ) {
- this.x = x;
- return this;
- },
- setY: function ( y ) {
- this.y = y;
- return this;
- },
- setZ: function ( z ) {
- this.z = z;
- return this;
- },
- setComponent: function ( index, value ) {
- switch ( index ) {
- case 0: this.x = value; break;
- case 1: this.y = value; break;
- case 2: this.z = value; break;
- default: throw new Error( 'index is out of range: ' + index );
- }
- },
- getComponent: function ( index ) {
- switch ( index ) {
- case 0: return this.x;
- case 1: return this.y;
- case 2: return this.z;
- default: throw new Error( 'index is out of range: ' + index );
- }
- },
- copy: function ( v ) {
- this.x = v.x;
- this.y = v.y;
- this.z = v.z;
- return this;
- },
- add: function ( v, w ) {
- if ( w !== undefined ) {
- console.warn( 'THREE.Vector3: .add() now only accepts one argument. Use .addVectors( a, b ) instead.' );
- return this.addVectors( v, w );
- }
- this.x += v.x;
- this.y += v.y;
- this.z += v.z;
- return this;
- },
- addScalar: function ( s ) {
- this.x += s;
- this.y += s;
- this.z += s;
- return this;
- },
- addVectors: function ( a, b ) {
- this.x = a.x + b.x;
- this.y = a.y + b.y;
- this.z = a.z + b.z;
- return this;
- },
- sub: function ( v, w ) {
- if ( w !== undefined ) {
- console.warn( 'THREE.Vector3: .sub() now only accepts one argument. Use .subVectors( a, b ) instead.' );
- return this.subVectors( v, w );
- }
- this.x -= v.x;
- this.y -= v.y;
- this.z -= v.z;
- return this;
- },
- subVectors: function ( a, b ) {
- this.x = a.x - b.x;
- this.y = a.y - b.y;
- this.z = a.z - b.z;
- return this;
- },
- multiply: function ( v, w ) {
- if ( w !== undefined ) {
- console.warn( 'THREE.Vector3: .multiply() now only accepts one argument. Use .multiplyVectors( a, b ) instead.' );
- return this.multiplyVectors( v, w );
- }
- this.x *= v.x;
- this.y *= v.y;
- this.z *= v.z;
- return this;
- },
- multiplyScalar: function ( scalar ) {
- this.x *= scalar;
- this.y *= scalar;
- this.z *= scalar;
- return this;
- },
- multiplyVectors: function ( a, b ) {
- this.x = a.x * b.x;
- this.y = a.y * b.y;
- this.z = a.z * b.z;
- return this;
- },
- applyEuler: function () {
- var quaternion;
- return function ( euler ) {
- if ( euler instanceof THREE.Euler === false ) {
- console.error( 'THREE.Vector3: .applyEuler() now expects a Euler rotation rather than a Vector3 and order.' );
- }
- if ( quaternion === undefined ) quaternion = new THREE.Quaternion();
- this.applyQuaternion( quaternion.setFromEuler( euler ) );
- return this;
- };
- }(),
- applyAxisAngle: function () {
- var quaternion;
- return function ( axis, angle ) {
- if ( quaternion === undefined ) quaternion = new THREE.Quaternion();
- this.applyQuaternion( quaternion.setFromAxisAngle( axis, angle ) );
- return this;
- };
- }(),
- applyMatrix3: function ( m ) {
- var x = this.x;
- var y = this.y;
- var z = this.z;
- var e = m.elements;
- this.x = e[ 0 ] * x + e[ 3 ] * y + e[ 6 ] * z;
- this.y = e[ 1 ] * x + e[ 4 ] * y + e[ 7 ] * z;
- this.z = e[ 2 ] * x + e[ 5 ] * y + e[ 8 ] * z;
- return this;
- },
- applyMatrix4: function ( m ) {
- // input: THREE.Matrix4 affine matrix
- var x = this.x, y = this.y, z = this.z;
- var e = m.elements;
- this.x = e[ 0 ] * x + e[ 4 ] * y + e[ 8 ] * z + e[ 12 ];
- this.y = e[ 1 ] * x + e[ 5 ] * y + e[ 9 ] * z + e[ 13 ];
- this.z = e[ 2 ] * x + e[ 6 ] * y + e[ 10 ] * z + e[ 14 ];
- return this;
- },
- applyProjection: function ( m ) {
- // input: THREE.Matrix4 projection matrix
- var x = this.x, y = this.y, z = this.z;
- var e = m.elements;
- var d = 1 / ( e[ 3 ] * x + e[ 7 ] * y + e[ 11 ] * z + e[ 15 ] ); // perspective divide
- this.x = ( e[ 0 ] * x + e[ 4 ] * y + e[ 8 ] * z + e[ 12 ] ) * d;
- this.y = ( e[ 1 ] * x + e[ 5 ] * y + e[ 9 ] * z + e[ 13 ] ) * d;
- this.z = ( e[ 2 ] * x + e[ 6 ] * y + e[ 10 ] * z + e[ 14 ] ) * d;
- return this;
- },
- applyQuaternion: function ( q ) {
- var x = this.x;
- var y = this.y;
- var z = this.z;
- var qx = q.x;
- var qy = q.y;
- var qz = q.z;
- var qw = q.w;
- // calculate quat * vector
- var ix = qw * x + qy * z - qz * y;
- var iy = qw * y + qz * x - qx * z;
- var iz = qw * z + qx * y - qy * x;
- var iw = - qx * x - qy * y - qz * z;
- // calculate result * inverse quat
- this.x = ix * qw + iw * - qx + iy * - qz - iz * - qy;
- this.y = iy * qw + iw * - qy + iz * - qx - ix * - qz;
- this.z = iz * qw + iw * - qz + ix * - qy - iy * - qx;
- return this;
- },
- project: function () {
- var matrix;
- return function ( camera ) {
- if ( matrix === undefined ) matrix = new THREE.Matrix4();
- matrix.multiplyMatrices( camera.projectionMatrix, matrix.getInverse( camera.matrixWorld ) );
- return this.applyProjection( matrix );
- };
- }(),
- unproject: function () {
- var matrix;
- return function ( camera ) {
- if ( matrix === undefined ) matrix = new THREE.Matrix4();
- matrix.multiplyMatrices( camera.matrixWorld, matrix.getInverse( camera.projectionMatrix ) );
- return this.applyProjection( matrix );
- };
- }(),
- transformDirection: function ( m ) {
- // input: THREE.Matrix4 affine matrix
- // vector interpreted as a direction
- var x = this.x, y = this.y, z = this.z;
- var e = m.elements;
- this.x = e[ 0 ] * x + e[ 4 ] * y + e[ 8 ] * z;
- this.y = e[ 1 ] * x + e[ 5 ] * y + e[ 9 ] * z;
- this.z = e[ 2 ] * x + e[ 6 ] * y + e[ 10 ] * z;
- this.normalize();
- return this;
- },
- divide: function ( v ) {
- this.x /= v.x;
- this.y /= v.y;
- this.z /= v.z;
- return this;
- },
- divideScalar: function ( scalar ) {
- if ( scalar !== 0 ) {
- var invScalar = 1 / scalar;
- this.x *= invScalar;
- this.y *= invScalar;
- this.z *= invScalar;
- } else {
- this.x = 0;
- this.y = 0;
- this.z = 0;
- }
- return this;
- },
- min: function ( v ) {
- if ( this.x > v.x ) {
- this.x = v.x;
- }
- if ( this.y > v.y ) {
- this.y = v.y;
- }
- if ( this.z > v.z ) {
- this.z = v.z;
- }
- return this;
- },
- max: function ( v ) {
- if ( this.x < v.x ) {
- this.x = v.x;
- }
- if ( this.y < v.y ) {
- this.y = v.y;
- }
- if ( this.z < v.z ) {
- this.z = v.z;
- }
- return this;
- },
- clamp: function ( min, max ) {
- // This function assumes min < max, if this assumption isn't true it will not operate correctly
- if ( this.x < min.x ) {
- this.x = min.x;
- } else if ( this.x > max.x ) {
- this.x = max.x;
- }
- if ( this.y < min.y ) {
- this.y = min.y;
- } else if ( this.y > max.y ) {
- this.y = max.y;
- }
- if ( this.z < min.z ) {
- this.z = min.z;
- } else if ( this.z > max.z ) {
- this.z = max.z;
- }
- return this;
- },
- clampScalar: ( function () {
- var min, max;
- return function ( minVal, maxVal ) {
- if ( min === undefined ) {
- min = new THREE.Vector3();
- max = new THREE.Vector3();
- }
- min.set( minVal, minVal, minVal );
- max.set( maxVal, maxVal, maxVal );
- return this.clamp( min, max );
- };
- } )(),
- floor: function () {
- this.x = Math.floor( this.x );
- this.y = Math.floor( this.y );
- this.z = Math.floor( this.z );
- return this;
- },
- ceil: function () {
- this.x = Math.ceil( this.x );
- this.y = Math.ceil( this.y );
- this.z = Math.ceil( this.z );
- return this;
- },
- round: function () {
- this.x = Math.round( this.x );
- this.y = Math.round( this.y );
- this.z = Math.round( this.z );
- return this;
- },
- roundToZero: function () {
- this.x = ( this.x < 0 ) ? Math.ceil( this.x ) : Math.floor( this.x );
- this.y = ( this.y < 0 ) ? Math.ceil( this.y ) : Math.floor( this.y );
- this.z = ( this.z < 0 ) ? Math.ceil( this.z ) : Math.floor( this.z );
- return this;
- },
- negate: function () {
- this.x = - this.x;
- this.y = - this.y;
- this.z = - this.z;
- return this;
- },
- dot: function ( v ) {
- return this.x * v.x + this.y * v.y + this.z * v.z;
- },
- lengthSq: function () {
- return this.x * this.x + this.y * this.y + this.z * this.z;
- },
- length: function () {
- return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z );
- },
- lengthManhattan: function () {
- return Math.abs( this.x ) + Math.abs( this.y ) + Math.abs( this.z );
- },
- normalize: function () {
- return this.divideScalar( this.length() );
- },
- setLength: function ( l ) {
- var oldLength = this.length();
- if ( oldLength !== 0 && l !== oldLength ) {
- this.multiplyScalar( l / oldLength );
- }
- return this;
- },
- lerp: function ( v, alpha ) {
- this.x += ( v.x - this.x ) * alpha;
- this.y += ( v.y - this.y ) * alpha;
- this.z += ( v.z - this.z ) * alpha;
- return this;
- },
- cross: function ( v, w ) {
- if ( w !== undefined ) {
- console.warn( 'THREE.Vector3: .cross() now only accepts one argument. Use .crossVectors( a, b ) instead.' );
- return this.crossVectors( v, w );
- }
- var x = this.x, y = this.y, z = this.z;
- this.x = y * v.z - z * v.y;
- this.y = z * v.x - x * v.z;
- this.z = x * v.y - y * v.x;
- return this;
- },
- crossVectors: function ( a, b ) {
- var ax = a.x, ay = a.y, az = a.z;
- var bx = b.x, by = b.y, bz = b.z;
- this.x = ay * bz - az * by;
- this.y = az * bx - ax * bz;
- this.z = ax * by - ay * bx;
- return this;
- },
- projectOnVector: function () {
- var v1, dot;
- return function ( vector ) {
- if ( v1 === undefined ) v1 = new THREE.Vector3();
- v1.copy( vector ).normalize();
- dot = this.dot( v1 );
- return this.copy( v1 ).multiplyScalar( dot );
- };
- }(),
- projectOnPlane: function () {
- var v1;
- return function ( planeNormal ) {
- if ( v1 === undefined ) v1 = new THREE.Vector3();
- v1.copy( this ).projectOnVector( planeNormal );
- return this.sub( v1 );
- }
- }(),
- reflect: function () {
- // reflect incident vector off plane orthogonal to normal
- // normal is assumed to have unit length
- var v1;
- return function ( normal ) {
- if ( v1 === undefined ) v1 = new THREE.Vector3();
- return this.sub( v1.copy( normal ).multiplyScalar( 2 * this.dot( normal ) ) );
- }
- }(),
- angleTo: function ( v ) {
- var theta = this.dot( v ) / ( this.length() * v.length() );
- // clamp, to handle numerical problems
- return Math.acos( THREE.Math.clamp( theta, - 1, 1 ) );
- },
- distanceTo: function ( v ) {
- return Math.sqrt( this.distanceToSquared( v ) );
- },
- distanceToSquared: function ( v ) {
- var dx = this.x - v.x;
- var dy = this.y - v.y;
- var dz = this.z - v.z;
- return dx * dx + dy * dy + dz * dz;
- },
- setEulerFromRotationMatrix: function ( m, order ) {
- console.error( 'THREE.Vector3: .setEulerFromRotationMatrix() has been removed. Use Euler.setFromRotationMatrix() instead.' );
- },
- setEulerFromQuaternion: function ( q, order ) {
- console.error( 'THREE.Vector3: .setEulerFromQuaternion() has been removed. Use Euler.setFromQuaternion() instead.' );
- },
- getPositionFromMatrix: function ( m ) {
- console.warn( 'THREE.Vector3: .getPositionFromMatrix() has been renamed to .setFromMatrixPosition().' );
- return this.setFromMatrixPosition( m );
- },
- getScaleFromMatrix: function ( m ) {
- console.warn( 'THREE.Vector3: .getScaleFromMatrix() has been renamed to .setFromMatrixScale().' );
- return this.setFromMatrixScale( m );
- },
- getColumnFromMatrix: function ( index, matrix ) {
- console.warn( 'THREE.Vector3: .getColumnFromMatrix() has been renamed to .setFromMatrixColumn().' );
- return this.setFromMatrixColumn( index, matrix );
- },
- setFromMatrixPosition: function ( m ) {
- this.x = m.elements[ 12 ];
- this.y = m.elements[ 13 ];
- this.z = m.elements[ 14 ];
- return this;
- },
- setFromMatrixScale: function ( m ) {
- var sx = this.set( m.elements[ 0 ], m.elements[ 1 ], m.elements[ 2 ] ).length();
- var sy = this.set( m.elements[ 4 ], m.elements[ 5 ], m.elements[ 6 ] ).length();
- var sz = this.set( m.elements[ 8 ], m.elements[ 9 ], m.elements[ 10 ] ).length();
- this.x = sx;
- this.y = sy;
- this.z = sz;
- return this;
- },
- setFromMatrixColumn: function ( index, matrix ) {
- var offset = index * 4;
- var me = matrix.elements;
- this.x = me[ offset ];
- this.y = me[ offset + 1 ];
- this.z = me[ offset + 2 ];
- return this;
- },
- equals: function ( v ) {
- return ( ( v.x === this.x ) && ( v.y === this.y ) && ( v.z === this.z ) );
- },
- fromArray: function ( array, offset ) {
- if ( offset === undefined ) offset = 0;
- this.x = array[ offset ];
- this.y = array[ offset + 1 ];
- this.z = array[ offset + 2 ];
- return this;
- },
- toArray: function ( array, offset ) {
- if ( array === undefined ) array = [];
- if ( offset === undefined ) offset = 0;
- array[ offset ] = this.x;
- array[ offset + 1 ] = this.y;
- array[ offset + 2 ] = this.z;
- return array;
- },
- fromAttribute: function ( attribute, index, offset ) {
- if ( offset === undefined ) offset = 0;
- index = index * attribute.itemSize + offset;
- this.x = attribute.array[ index ];
- this.y = attribute.array[ index + 1 ];
- this.z = attribute.array[ index + 2 ];
- return this;
- },
- clone: function () {
- return new THREE.Vector3( this.x, this.y, this.z );
- }
- };
- /*** END Vector3 ***/
- /*** START Euler ***/
- /**
- * @author mrdoob / http://mrdoob.com/
- * @author WestLangley / http://github.com/WestLangley
- * @author bhouston / http://exocortex.com
- */
- THREE.Euler = function ( x, y, z, order ) {
- this._x = x || 0;
- this._y = y || 0;
- this._z = z || 0;
- this._order = order || THREE.Euler.DefaultOrder;
- };
- THREE.Euler.RotationOrders = [ 'XYZ', 'YZX', 'ZXY', 'XZY', 'YXZ', 'ZYX' ];
- THREE.Euler.DefaultOrder = 'XYZ';
- THREE.Euler.prototype = {
- constructor: THREE.Euler,
- _x: 0, _y: 0, _z: 0, _order: THREE.Euler.DefaultOrder,
- get x () {
- return this._x;
- },
- set x ( value ) {
- this._x = value;
- this.onChangeCallback();
- },
- get y () {
- return this._y;
- },
- set y ( value ) {
- this._y = value;
- this.onChangeCallback();
- },
- get z () {
- return this._z;
- },
- set z ( value ) {
- this._z = value;
- this.onChangeCallback();
- },
- get order () {
- return this._order;
- },
- set order ( value ) {
- this._order = value;
- this.onChangeCallback();
- },
- set: function ( x, y, z, order ) {
- this._x = x;
- this._y = y;
- this._z = z;
- this._order = order || this._order;
- this.onChangeCallback();
- return this;
- },
- copy: function ( euler ) {
- this._x = euler._x;
- this._y = euler._y;
- this._z = euler._z;
- this._order = euler._order;
- this.onChangeCallback();
- return this;
- },
- setFromRotationMatrix: function ( m, order, update ) {
- var clamp = THREE.Math.clamp;
- // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
- var te = m.elements;
- var m11 = te[ 0 ], m12 = te[ 4 ], m13 = te[ 8 ];
- var m21 = te[ 1 ], m22 = te[ 5 ], m23 = te[ 9 ];
- var m31 = te[ 2 ], m32 = te[ 6 ], m33 = te[ 10 ];
- order = order || this._order;
- if ( order === 'XYZ' ) {
- this._y = Math.asin( clamp( m13, - 1, 1 ) );
- if ( Math.abs( m13 ) < 0.99999 ) {
- this._x = Math.atan2( - m23, m33 );
- this._z = Math.atan2( - m12, m11 );
- } else {
- this._x = Math.atan2( m32, m22 );
- this._z = 0;
- }
- } else if ( order === 'YXZ' ) {
- this._x = Math.asin( - clamp( m23, - 1, 1 ) );
- if ( Math.abs( m23 ) < 0.99999 ) {
- this._y = Math.atan2( m13, m33 );
- this._z = Math.atan2( m21, m22 );
- } else {
- this._y = Math.atan2( - m31, m11 );
- this._z = 0;
- }
- } else if ( order === 'ZXY' ) {
- this._x = Math.asin( clamp( m32, - 1, 1 ) );
- if ( Math.abs( m32 ) < 0.99999 ) {
- this._y = Math.atan2( - m31, m33 );
- this._z = Math.atan2( - m12, m22 );
- } else {
- this._y = 0;
- this._z = Math.atan2( m21, m11 );
- }
- } else if ( order === 'ZYX' ) {
- this._y = Math.asin( - clamp( m31, - 1, 1 ) );
- if ( Math.abs( m31 ) < 0.99999 ) {
- this._x = Math.atan2( m32, m33 );
- this._z = Math.atan2( m21, m11 );
- } else {
- this._x = 0;
- this._z = Math.atan2( - m12, m22 );
- }
- } else if ( order === 'YZX' ) {
- this._z = Math.asin( clamp( m21, - 1, 1 ) );
- if ( Math.abs( m21 ) < 0.99999 ) {
- this._x = Math.atan2( - m23, m22 );
- this._y = Math.atan2( - m31, m11 );
- } else {
- this._x = 0;
- this._y = Math.atan2( m13, m33 );
- }
- } else if ( order === 'XZY' ) {
- this._z = Math.asin( - clamp( m12, - 1, 1 ) );
- if ( Math.abs( m12 ) < 0.99999 ) {
- this._x = Math.atan2( m32, m22 );
- this._y = Math.atan2( m13, m11 );
- } else {
- this._x = Math.atan2( - m23, m33 );
- this._y = 0;
- }
- } else {
- console.warn( 'THREE.Euler: .setFromRotationMatrix() given unsupported order: ' + order )
- }
- this._order = order;
- if ( update !== false ) this.onChangeCallback();
- return this;
- },
- setFromQuaternion: function () {
- var matrix;
- return function ( q, order, update ) {
- if ( matrix === undefined ) matrix = new THREE.Matrix4();
- matrix.makeRotationFromQuaternion( q );
- this.setFromRotationMatrix( matrix, order, update );
- return this;
- };
- }(),
- setFromVector3: function ( v, order ) {
- return this.set( v.x, v.y, v.z, order || this._order );
- },
- reorder: function () {
- // WARNING: this discards revolution information -bhouston
- var q = new THREE.Quaternion();
- return function ( newOrder ) {
- q.setFromEuler( this );
- this.setFromQuaternion( q, newOrder );
- };
- }(),
- equals: function ( euler ) {
- return ( euler._x === this._x ) && ( euler._y === this._y ) && ( euler._z === this._z ) && ( euler._order === this._order );
- },
- fromArray: function ( array ) {
- this._x = array[ 0 ];
- this._y = array[ 1 ];
- this._z = array[ 2 ];
- if ( array[ 3 ] !== undefined ) this._order = array[ 3 ];
- this.onChangeCallback();
- return this;
- },
- toArray: function () {
- return [ this._x, this._y, this._z, this._order ];
- },
- toVector3: function ( optionalResult ) {
- if ( optionalResult ) {
- return optionalResult.set( this._x, this._y, this._z );
- } else {
- return new THREE.Vector3( this._x, this._y, this._z );
- }
- },
- onChange: function ( callback ) {
- this.onChangeCallback = callback;
- return this;
- },
- onChangeCallback: function () {},
- clone: function () {
- return new THREE.Euler( this._x, this._y, this._z, this._order );
- }
- };
- /*** END Euler ***/
- /*** START Math ***/
- /**
- * @author alteredq / http://alteredqualia.com/
- * @author mrdoob / http://mrdoob.com/
- */
- THREE.Math = {
- generateUUID: function () {
- // http://www.broofa.com/Tools/Math.uuid.htm
- var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split( '' );
- var uuid = new Array( 36 );
- var rnd = 0, r;
- return function () {
- for ( var i = 0; i < 36; i ++ ) {
- if ( i == 8 || i == 13 || i == 18 || i == 23 ) {
- uuid[ i ] = '-';
- } else if ( i == 14 ) {
- uuid[ i ] = '4';
- } else {
- if ( rnd <= 0x02 ) rnd = 0x2000000 + ( Math.random() * 0x1000000 ) | 0;
- r = rnd & 0xf;
- rnd = rnd >> 4;
- uuid[ i ] = chars[ ( i == 19 ) ? ( r & 0x3 ) | 0x8 : r ];
- }
- }
- return uuid.join( '' );
- };
- }(),
- // Clamp value to range <a, b>
- clamp: function ( x, a, b ) {
- return ( x < a ) ? a : ( ( x > b ) ? b : x );
- },
- // Clamp value to range <a, inf)
- clampBottom: function ( x, a ) {
- return x < a ? a : x;
- },
- // Linear mapping from range <a1, a2> to range <b1, b2>
- mapLinear: function ( x, a1, a2, b1, b2 ) {
- return b1 + ( x - a1 ) * ( b2 - b1 ) / ( a2 - a1 );
- },
- // http://en.wikipedia.org/wiki/Smoothstep
- smoothstep: function ( x, min, max ) {
- if ( x <= min ) return 0;
- if ( x >= max ) return 1;
- x = ( x - min ) / ( max - min );
- return x * x * ( 3 - 2 * x );
- },
- smootherstep: function ( x, min, max ) {
- if ( x <= min ) return 0;
- if ( x >= max ) return 1;
- x = ( x - min ) / ( max - min );
- return x * x * x * ( x * ( x * 6 - 15 ) + 10 );
- },
- // Random float from <0, 1> with 16 bits of randomness
- // (standard Math.random() creates repetitive patterns when applied over larger space)
- random16: function () {
- return ( 65280 * Math.random() + 255 * Math.random() ) / 65535;
- },
- // Random integer from <low, high> interval
- randInt: function ( low, high ) {
- return Math.floor( this.randFloat( low, high ) );
- },
- // Random float from <low, high> interval
- randFloat: function ( low, high ) {
- return low + Math.random() * ( high - low );
- },
- // Random float from <-range/2, range/2> interval
- randFloatSpread: function ( range ) {
- return range * ( 0.5 - Math.random() );
- },
- degToRad: function () {
- var degreeToRadiansFactor = Math.PI / 180;
- return function ( degrees ) {
- return degrees * degreeToRadiansFactor;
- };
- }(),
- radToDeg: function () {
- var radianToDegreesFactor = 180 / Math.PI;
- return function ( radians ) {
- return radians * radianToDegreesFactor;
- };
- }(),
- isPowerOfTwo: function ( value ) {
- return ( value & ( value - 1 ) ) === 0 && value !== 0;
- },
- nextPowerOfTwo: function ( value ) {
- value --;
- value |= value >> 1;
- value |= value >> 2;
- value |= value >> 4;
- value |= value >> 8;
- value |= value >> 16;
- value ++;
- return value;
- }
- };
- /*** END Math ***/
- }
- module.exports = THREE;
- },{}],10:[function(_dereq_,module,exports){
- /*
- * Copyright 2015 Google Inc. All Rights Reserved.
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var THREE = _dereq_('./three-math.js');
- var Util = _dereq_('./util.js');
- var ROTATE_SPEED = 0.5;
- /**
- * Provides a quaternion responsible for pre-panning the scene before further
- * transformations due to device sensors.
- */
- function TouchPanner() {
- window.addEventListener('touchstart', this.onTouchStart_.bind(this));
- window.addEventListener('touchmove', this.onTouchMove_.bind(this));
- window.addEventListener('touchend', this.onTouchEnd_.bind(this));
- this.isTouching = false;
- this.rotateStart = new THREE.Vector2();
- this.rotateEnd = new THREE.Vector2();
- this.rotateDelta = new THREE.Vector2();
- this.theta = 0;
- this.orientation = new THREE.Quaternion();
- }
- TouchPanner.prototype.getOrientation = function() {
- this.orientation.setFromEuler(new THREE.Euler(0, 0, this.theta));
- return this.orientation;
- };
- TouchPanner.prototype.resetSensor = function() {
- this.theta = 0;
- };
- TouchPanner.prototype.onTouchStart_ = function(e) {
- // Only respond if there is exactly one touch.
- if (e.touches.length != 1) {
- return;
- }
- this.rotateStart.set(e.touches[0].pageX, e.touches[0].pageY);
- this.isTouching = true;
- };
- TouchPanner.prototype.onTouchMove_ = function(e) {
- if (!this.isTouching) {
- return;
- }
- this.rotateEnd.set(e.touches[0].pageX, e.touches[0].pageY);
- this.rotateDelta.subVectors(this.rotateEnd, this.rotateStart);
- this.rotateStart.copy(this.rotateEnd);
- // On iOS, direction is inverted.
- if (Util.isIOS()) {
- this.rotateDelta.x *= -1;
- }
- var element = document.body;
- this.theta += 2 * Math.PI * this.rotateDelta.x / element.clientWidth * ROTATE_SPEED;
- };
- TouchPanner.prototype.onTouchEnd_ = function(e) {
- this.isTouching = false;
- };
- module.exports = TouchPanner;
- },{"./three-math.js":9,"./util.js":11}],11:[function(_dereq_,module,exports){
- /*
- * Copyright 2015 Google Inc. All Rights Reserved.
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var Util = window.Util || {};
- Util.MIN_TIMESTEP = 0.001;
- Util.MAX_TIMESTEP = 1;
- Util.clamp = function(value, min, max) {
- return Math.min(Math.max(min, value), max);
- };
- Util.isIOS = function() {
- return /iPad|iPhone|iPod/.test(navigator.platform);
- };
- Util.isFirefoxAndroid = function() {
- return navigator.userAgent.indexOf('Firefox') !== -1 && navigator.userAgent.indexOf('Android') !== -1;
- }
- // Helper method to validate the time steps of sensor timestamps.
- Util.isTimestampDeltaValid = function(timestampDeltaS) {
- if (isNaN(timestampDeltaS)) {
- return false;
- }
- if (timestampDeltaS <= Util.MIN_TIMESTEP) {
- return false;
- }
- if (timestampDeltaS > Util.MAX_TIMESTEP) {
- return false;
- }
- return true;
- }
- module.exports = Util;
- },{}],12:[function(_dereq_,module,exports){
- /*
- * Copyright 2015 Google Inc. All Rights Reserved.
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var CardboardHMDVRDevice = _dereq_('./cardboard-hmd-vr-device.js');
- //var OrientationPositionSensorVRDevice = require('./orientation-position-sensor-vr-device.js');
- var FusionPositionSensorVRDevice = _dereq_('./fusion-position-sensor-vr-device.js');
- var MouseKeyboardPositionSensorVRDevice = _dereq_('./mouse-keyboard-position-sensor-vr-device.js');
- // Uncomment to add positional tracking via webcam.
- //var WebcamPositionSensorVRDevice = require('./webcam-position-sensor-vr-device.js');
- var HMDVRDevice = _dereq_('./base.js').HMDVRDevice;
- var PositionSensorVRDevice = _dereq_('./base.js').PositionSensorVRDevice;
- function WebVRPolyfill() {
- this.devices = [];
- if (!this.isWebVRAvailable()) {
- this.enablePolyfill();
- }
- }
- WebVRPolyfill.prototype.isWebVRAvailable = function() {
- return ('getVRDevices' in navigator) || ('mozGetVRDevices' in navigator);
- };
- WebVRPolyfill.prototype.enablePolyfill = function() {
- // Initialize our virtual VR devices.
- if (this.isCardboardCompatible()) {
- this.devices.push(new CardboardHMDVRDevice());
- }
- // Polyfill using the right position sensor.
- if (this.isMobile()) {
- //this.devices.push(new OrientationPositionSensorVRDevice());
- this.devices.push(new FusionPositionSensorVRDevice());
- } else {
- if (!WebVRConfig.MOUSE_KEYBOARD_CONTROLS_DISABLED) {
- this.devices.push(new MouseKeyboardPositionSensorVRDevice());
- }
- // Uncomment to add positional tracking via webcam.
- //this.devices.push(new WebcamPositionSensorVRDevice());
- }
- // Provide navigator.getVRDevices.
- navigator.getVRDevices = this.getVRDevices.bind(this);
- // Provide the CardboardHMDVRDevice and PositionSensorVRDevice objects.
- window.HMDVRDevice = HMDVRDevice;
- window.PositionSensorVRDevice = PositionSensorVRDevice;
- };
- WebVRPolyfill.prototype.getVRDevices = function() {
- var devices = this.devices;
- return new Promise(function(resolve, reject) {
- try {
- resolve(devices);
- } catch (e) {
- reject(e);
- }
- });
- };
- /**
- * Determine if a device is mobile.
- */
- WebVRPolyfill.prototype.isMobile = function() {
- return /Android/i.test(navigator.userAgent) ||
- /iPhone|iPad|iPod/i.test(navigator.userAgent);
- };
- WebVRPolyfill.prototype.isCardboardCompatible = function() {
- // For now, support all iOS and Android devices.
- // Also enable the WebVRConfig.FORCE_VR flag for debugging.
- return this.isMobile() || WebVRConfig.FORCE_ENABLE_VR;
- };
- module.exports = WebVRPolyfill;
- },{"./base.js":1,"./cardboard-hmd-vr-device.js":2,"./fusion-position-sensor-vr-device.js":4,"./mouse-keyboard-position-sensor-vr-device.js":6}]},{},[5]);
|