12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151 |
- /**
- * @author supereggbert / http://www.paulbrunt.co.uk/
- * @author mrdoob / http://mrdoob.com/
- * @author alteredq / http://alteredqualia.com/
- * @author szimek / https://github.com/szimek/
- */
- THREE.WebGLRenderer = function ( parameters ) {
- console.log( 'THREE.WebGLRenderer', THREE.REVISION );
- parameters = parameters || {};
- var _canvas = parameters.canvas !== undefined ? parameters.canvas : document.createElement( 'canvas' ),
- _context = parameters.context !== undefined ? parameters.context : null,
- _width = _canvas.width,
- _height = _canvas.height,
- pixelRatio = 1,
- _precision = parameters.precision !== undefined ? parameters.precision : 'highp',
- _alpha = parameters.alpha !== undefined ? parameters.alpha : false,
- _depth = parameters.depth !== undefined ? parameters.depth : true,
- _stencil = parameters.stencil !== undefined ? parameters.stencil : true,
- _antialias = parameters.antialias !== undefined ? parameters.antialias : false,
- _premultipliedAlpha = parameters.premultipliedAlpha !== undefined ? parameters.premultipliedAlpha : true,
- _preserveDrawingBuffer = parameters.preserveDrawingBuffer !== undefined ? parameters.preserveDrawingBuffer : false,
- _logarithmicDepthBuffer = parameters.logarithmicDepthBuffer !== undefined ? parameters.logarithmicDepthBuffer : false,
- _clearColor = new THREE.Color( 0x000000 ),
- _clearAlpha = 0;
- var lights = [];
- var opaqueObjects = [];
- var transparentObjects = [];
- var sprites = [];
- var lensFlares = [];
- // public properties
- this.domElement = _canvas;
- this.context = null;
- // clearing
- this.autoClear = true;
- this.autoClearColor = true;
- this.autoClearDepth = true;
- this.autoClearStencil = true;
- // scene graph
- this.sortObjects = true;
- // physically based shading
- this.gammaFactor = 2.0; // for backwards compatibility
- this.gammaInput = false;
- this.gammaOutput = false;
- // morphs
- this.maxMorphTargets = 8;
- this.maxMorphNormals = 4;
- // flags
- this.autoScaleCubemaps = true;
- // info
- this.info = {
- memory: {
- programs: 0,
- geometries: 0,
- textures: 0
- },
- render: {
- calls: 0,
- vertices: 0,
- faces: 0,
- points: 0
- }
- };
- // internal properties
- var _this = this,
- _programs = [],
- // internal state cache
- _currentProgram = null,
- _currentFramebuffer = null,
- _currentMaterialId = - 1,
- _currentGeometryProgram = '',
- _currentCamera = null,
- _usedTextureUnits = 0,
- _viewportX = 0,
- _viewportY = 0,
- _viewportWidth = _canvas.width,
- _viewportHeight = _canvas.height,
- _currentWidth = 0,
- _currentHeight = 0,
- // frustum
- _frustum = new THREE.Frustum(),
- // camera matrices cache
- _projScreenMatrix = new THREE.Matrix4(),
- _vector3 = new THREE.Vector3(),
- // light arrays cache
- _direction = new THREE.Vector3(),
- _lightsNeedUpdate = true,
- _lights = {
- ambient: [ 0, 0, 0 ],
- directional: { length: 0, colors:[], positions: [] },
- point: { length: 0, colors: [], positions: [], distances: [], decays: [] },
- spot: { length: 0, colors: [], positions: [], distances: [], directions: [], anglesCos: [], exponents: [], decays: [] },
- hemi: { length: 0, skyColors: [], groundColors: [], positions: [] }
- };
- // initialize
- var _gl;
- try {
- var attributes = {
- alpha: _alpha,
- depth: _depth,
- stencil: _stencil,
- antialias: _antialias,
- premultipliedAlpha: _premultipliedAlpha,
- preserveDrawingBuffer: _preserveDrawingBuffer
- };
- _gl = _context || _canvas.getContext( 'webgl', attributes ) || _canvas.getContext( 'experimental-webgl', attributes );
- if ( _gl === null ) {
- if ( _canvas.getContext( 'webgl') !== null ) {
- throw 'Error creating WebGL context with your selected attributes.';
- } else {
- throw 'Error creating WebGL context.';
- }
- }
- _canvas.addEventListener( 'webglcontextlost', function ( event ) {
- event.preventDefault();
- resetGLState();
- setDefaultGLState();
- objects.objects = {};
- }, false);
- } catch ( error ) {
- THREE.error( 'THREE.WebGLRenderer: ' + error );
- }
- var state = new THREE.WebGLState( _gl, paramThreeToGL );
- if ( _gl.getShaderPrecisionFormat === undefined ) {
- _gl.getShaderPrecisionFormat = function () {
- return {
- 'rangeMin': 1,
- 'rangeMax': 1,
- 'precision': 1
- };
- }
- }
- var extensions = new THREE.WebGLExtensions( _gl );
- var objects = new THREE.WebGLObjects( _gl, this.info, extensions );
- extensions.get( 'OES_texture_float' );
- extensions.get( 'OES_texture_float_linear' );
- extensions.get( 'OES_texture_half_float' );
- extensions.get( 'OES_texture_half_float_linear' );
- extensions.get( 'OES_standard_derivatives' );
- extensions.get( 'ANGLE_instanced_arrays' );
- if ( _logarithmicDepthBuffer ) {
- extensions.get( 'EXT_frag_depth' );
- }
- //
- var glClearColor = function ( r, g, b, a ) {
- if ( _premultipliedAlpha === true ) {
- r *= a; g *= a; b *= a;
- }
- _gl.clearColor( r, g, b, a );
- };
- var setDefaultGLState = function () {
- _gl.clearColor( 0, 0, 0, 1 );
- _gl.clearDepth( 1 );
- _gl.clearStencil( 0 );
- _gl.enable( _gl.DEPTH_TEST );
- _gl.depthFunc( _gl.LEQUAL );
- _gl.frontFace( _gl.CCW );
- _gl.cullFace( _gl.BACK );
- _gl.enable( _gl.CULL_FACE );
- _gl.enable( _gl.BLEND );
- _gl.blendEquation( _gl.FUNC_ADD );
- _gl.blendFunc( _gl.SRC_ALPHA, _gl.ONE_MINUS_SRC_ALPHA );
- _gl.viewport( _viewportX, _viewportY, _viewportWidth, _viewportHeight );
- glClearColor( _clearColor.r, _clearColor.g, _clearColor.b, _clearAlpha );
- };
- var resetGLState = function () {
- _currentProgram = null;
- _currentCamera = null;
- _currentGeometryProgram = '';
- _currentMaterialId = - 1;
- _lightsNeedUpdate = true;
- state.reset();
- };
- setDefaultGLState();
- this.context = _gl;
- this.extensions = extensions;
- this.state = state;
- // shadow map
- var shadowMap = new THREE.WebGLShadowMap( this, lights, objects );
- this.shadowMap = shadowMap;
- // GPU capabilities
- var _maxTextures = _gl.getParameter( _gl.MAX_TEXTURE_IMAGE_UNITS );
- var _maxVertexTextures = _gl.getParameter( _gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS );
- var _maxTextureSize = _gl.getParameter( _gl.MAX_TEXTURE_SIZE );
- var _maxCubemapSize = _gl.getParameter( _gl.MAX_CUBE_MAP_TEXTURE_SIZE );
- var _supportsVertexTextures = _maxVertexTextures > 0;
- var _supportsBoneTextures = _supportsVertexTextures && extensions.get( 'OES_texture_float' );
- var _supportsInstancedArrays = extensions.get( 'ANGLE_instanced_arrays' );
- //
- var _vertexShaderPrecisionHighpFloat = _gl.getShaderPrecisionFormat( _gl.VERTEX_SHADER, _gl.HIGH_FLOAT );
- var _vertexShaderPrecisionMediumpFloat = _gl.getShaderPrecisionFormat( _gl.VERTEX_SHADER, _gl.MEDIUM_FLOAT );
- var _fragmentShaderPrecisionHighpFloat = _gl.getShaderPrecisionFormat( _gl.FRAGMENT_SHADER, _gl.HIGH_FLOAT );
- var _fragmentShaderPrecisionMediumpFloat = _gl.getShaderPrecisionFormat( _gl.FRAGMENT_SHADER, _gl.MEDIUM_FLOAT );
- var getCompressedTextureFormats = ( function () {
- var array;
- return function () {
- if ( array !== undefined ) {
- return array;
- }
- array = [];
- if ( extensions.get( 'WEBGL_compressed_texture_pvrtc' ) || extensions.get( 'WEBGL_compressed_texture_s3tc' ) ) {
- var formats = _gl.getParameter( _gl.COMPRESSED_TEXTURE_FORMATS );
- for ( var i = 0; i < formats.length; i ++ ) {
- array.push( formats[ i ] );
- }
- }
- return array;
- };
- } )();
- // clamp precision to maximum available
- var highpAvailable = _vertexShaderPrecisionHighpFloat.precision > 0 && _fragmentShaderPrecisionHighpFloat.precision > 0;
- var mediumpAvailable = _vertexShaderPrecisionMediumpFloat.precision > 0 && _fragmentShaderPrecisionMediumpFloat.precision > 0;
- if ( _precision === 'highp' && ! highpAvailable ) {
- if ( mediumpAvailable ) {
- _precision = 'mediump';
- THREE.warn( 'THREE.WebGLRenderer: highp not supported, using mediump.' );
- } else {
- _precision = 'lowp';
- THREE.warn( 'THREE.WebGLRenderer: highp and mediump not supported, using lowp.' );
- }
- }
- if ( _precision === 'mediump' && ! mediumpAvailable ) {
- _precision = 'lowp';
- THREE.warn( 'THREE.WebGLRenderer: mediump not supported, using lowp.' );
- }
- // Plugins
- var spritePlugin = new THREE.SpritePlugin( this, sprites );
- var lensFlarePlugin = new THREE.LensFlarePlugin( this, lensFlares );
- // API
- this.getContext = function () {
- return _gl;
- };
- this.forceContextLoss = function () {
- extensions.get( 'WEBGL_lose_context' ).loseContext();
- };
- this.supportsVertexTextures = function () {
- return _supportsVertexTextures;
- };
- this.supportsInstancedArrays = function () {
- return _supportsInstancedArrays;
- };
- this.supportsFloatTextures = function () {
- return extensions.get( 'OES_texture_float' );
- };
- this.supportsHalfFloatTextures = function () {
- return extensions.get( 'OES_texture_half_float' );
- };
- this.supportsStandardDerivatives = function () {
- return extensions.get( 'OES_standard_derivatives' );
- };
- this.supportsCompressedTextureS3TC = function () {
- return extensions.get( 'WEBGL_compressed_texture_s3tc' );
- };
- this.supportsCompressedTexturePVRTC = function () {
- return extensions.get( 'WEBGL_compressed_texture_pvrtc' );
- };
- this.supportsBlendMinMax = function () {
- return extensions.get( 'EXT_blend_minmax' );
- };
- this.getMaxAnisotropy = ( function () {
- var value;
- return function () {
- if ( value !== undefined ) {
- return value;
- }
- var extension = extensions.get( 'EXT_texture_filter_anisotropic' );
- value = extension !== null ? _gl.getParameter( extension.MAX_TEXTURE_MAX_ANISOTROPY_EXT ) : 0;
- return value;
- }
- } )();
- this.getPrecision = function () {
- return _precision;
- };
- this.getPixelRatio = function () {
- return pixelRatio;
- };
- this.setPixelRatio = function ( value ) {
- pixelRatio = value;
- };
- this.getSize = function () {
- return {
- width: _width,
- height: _height
- };
- };
- this.setSize = function ( width, height, updateStyle ) {
- _width = width;
- _height = height;
- _canvas.width = width * pixelRatio;
- _canvas.height = height * pixelRatio;
- if ( updateStyle !== false ) {
- _canvas.style.width = width + 'px';
- _canvas.style.height = height + 'px';
- }
- this.setViewport( 0, 0, width, height );
- };
- this.setViewport = function ( x, y, width, height ) {
- _viewportX = x * pixelRatio;
- _viewportY = y * pixelRatio;
- _viewportWidth = width * pixelRatio;
- _viewportHeight = height * pixelRatio;
- _gl.viewport( _viewportX, _viewportY, _viewportWidth, _viewportHeight );
- };
- this.setScissor = function ( x, y, width, height ) {
- _gl.scissor(
- x * pixelRatio,
- y * pixelRatio,
- width * pixelRatio,
- height * pixelRatio
- );
- };
- this.enableScissorTest = function ( enable ) {
- enable ? _gl.enable( _gl.SCISSOR_TEST ) : _gl.disable( _gl.SCISSOR_TEST );
- };
- // Clearing
- this.getClearColor = function () {
- return _clearColor;
- };
- this.setClearColor = function ( color, alpha ) {
- _clearColor.set( color );
- _clearAlpha = alpha !== undefined ? alpha : 1;
- glClearColor( _clearColor.r, _clearColor.g, _clearColor.b, _clearAlpha );
- };
- this.getClearAlpha = function () {
- return _clearAlpha;
- };
- this.setClearAlpha = function ( alpha ) {
- _clearAlpha = alpha;
- glClearColor( _clearColor.r, _clearColor.g, _clearColor.b, _clearAlpha );
- };
- this.clear = function ( color, depth, stencil ) {
- var bits = 0;
- if ( color === undefined || color ) bits |= _gl.COLOR_BUFFER_BIT;
- if ( depth === undefined || depth ) bits |= _gl.DEPTH_BUFFER_BIT;
- if ( stencil === undefined || stencil ) bits |= _gl.STENCIL_BUFFER_BIT;
- _gl.clear( bits );
- };
- this.clearColor = function () {
- _gl.clear( _gl.COLOR_BUFFER_BIT );
- };
- this.clearDepth = function () {
- _gl.clear( _gl.DEPTH_BUFFER_BIT );
- };
- this.clearStencil = function () {
- _gl.clear( _gl.STENCIL_BUFFER_BIT );
- };
- this.clearTarget = function ( renderTarget, color, depth, stencil ) {
- this.setRenderTarget( renderTarget );
- this.clear( color, depth, stencil );
- };
- // Reset
- this.resetGLState = resetGLState;
- // Events
- var onTextureDispose = function ( event ) {
- var texture = event.target;
- texture.removeEventListener( 'dispose', onTextureDispose );
- deallocateTexture( texture );
- _this.info.memory.textures --;
- };
- var onRenderTargetDispose = function ( event ) {
- var renderTarget = event.target;
- renderTarget.removeEventListener( 'dispose', onRenderTargetDispose );
- deallocateRenderTarget( renderTarget );
- _this.info.memory.textures --;
- };
- var onMaterialDispose = function ( event ) {
- var material = event.target;
- material.removeEventListener( 'dispose', onMaterialDispose );
- deallocateMaterial( material );
- };
- // Buffer deallocation
- var deallocateTexture = function ( texture ) {
- if ( texture.image && texture.image.__webglTextureCube ) {
- // cube texture
- _gl.deleteTexture( texture.image.__webglTextureCube );
- delete texture.image.__webglTextureCube;
- } else {
- // 2D texture
- if ( texture.__webglInit === undefined ) return;
- _gl.deleteTexture( texture.__webglTexture );
- delete texture.__webglTexture;
- delete texture.__webglInit;
- }
- };
- var deallocateRenderTarget = function ( renderTarget ) {
- if ( ! renderTarget || renderTarget.__webglTexture === undefined ) return;
- _gl.deleteTexture( renderTarget.__webglTexture );
- delete renderTarget.__webglTexture;
- if ( renderTarget instanceof THREE.WebGLRenderTargetCube ) {
- for ( var i = 0; i < 6; i ++ ) {
- _gl.deleteFramebuffer( renderTarget.__webglFramebuffer[ i ] );
- _gl.deleteRenderbuffer( renderTarget.__webglRenderbuffer[ i ] );
- }
- } else {
- _gl.deleteFramebuffer( renderTarget.__webglFramebuffer );
- _gl.deleteRenderbuffer( renderTarget.__webglRenderbuffer );
- }
- delete renderTarget.__webglFramebuffer;
- delete renderTarget.__webglRenderbuffer;
- };
- var deallocateMaterial = function ( material ) {
- var program = material.program.program;
- if ( program === undefined ) return;
- material.program = undefined;
- // only deallocate GL program if this was the last use of shared program
- // assumed there is only single copy of any program in the _programs list
- // (that's how it's constructed)
- var i, il, programInfo;
- var deleteProgram = false;
- for ( i = 0, il = _programs.length; i < il; i ++ ) {
- programInfo = _programs[ i ];
- if ( programInfo.program === program ) {
- programInfo.usedTimes --;
- if ( programInfo.usedTimes === 0 ) {
- deleteProgram = true;
- }
- break;
- }
- }
- if ( deleteProgram === true ) {
- // avoid using array.splice, this is costlier than creating new array from scratch
- var newPrograms = [];
- for ( i = 0, il = _programs.length; i < il; i ++ ) {
- programInfo = _programs[ i ];
- if ( programInfo.program !== program ) {
- newPrograms.push( programInfo );
- }
- }
- _programs = newPrograms;
- _gl.deleteProgram( program );
- _this.info.memory.programs --;
- }
- };
- // Buffer rendering
- this.renderBufferImmediate = function ( object, program, material ) {
- state.initAttributes();
- if ( object.hasPositions && ! object.__webglVertexBuffer ) object.__webglVertexBuffer = _gl.createBuffer();
- if ( object.hasNormals && ! object.__webglNormalBuffer ) object.__webglNormalBuffer = _gl.createBuffer();
- if ( object.hasUvs && ! object.__webglUvBuffer ) object.__webglUvBuffer = _gl.createBuffer();
- if ( object.hasColors && ! object.__webglColorBuffer ) object.__webglColorBuffer = _gl.createBuffer();
- if ( object.hasPositions ) {
- _gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webglVertexBuffer );
- _gl.bufferData( _gl.ARRAY_BUFFER, object.positionArray, _gl.DYNAMIC_DRAW );
- state.enableAttribute( program.attributes.position );
- _gl.vertexAttribPointer( program.attributes.position, 3, _gl.FLOAT, false, 0, 0 );
- }
- if ( object.hasNormals ) {
- _gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webglNormalBuffer );
- if ( material instanceof THREE.MeshPhongMaterial === false && material.shading === THREE.FlatShading ) {
- var nx, ny, nz,
- nax, nbx, ncx, nay, nby, ncy, naz, nbz, ncz,
- normalArray,
- i, il = object.count * 3;
- for ( i = 0; i < il; i += 9 ) {
- normalArray = object.normalArray;
- nax = normalArray[ i ];
- nay = normalArray[ i + 1 ];
- naz = normalArray[ i + 2 ];
- nbx = normalArray[ i + 3 ];
- nby = normalArray[ i + 4 ];
- nbz = normalArray[ i + 5 ];
- ncx = normalArray[ i + 6 ];
- ncy = normalArray[ i + 7 ];
- ncz = normalArray[ i + 8 ];
- nx = ( nax + nbx + ncx ) / 3;
- ny = ( nay + nby + ncy ) / 3;
- nz = ( naz + nbz + ncz ) / 3;
- normalArray[ i ] = nx;
- normalArray[ i + 1 ] = ny;
- normalArray[ i + 2 ] = nz;
- normalArray[ i + 3 ] = nx;
- normalArray[ i + 4 ] = ny;
- normalArray[ i + 5 ] = nz;
- normalArray[ i + 6 ] = nx;
- normalArray[ i + 7 ] = ny;
- normalArray[ i + 8 ] = nz;
- }
- }
- _gl.bufferData( _gl.ARRAY_BUFFER, object.normalArray, _gl.DYNAMIC_DRAW );
- state.enableAttribute( program.attributes.normal );
- _gl.vertexAttribPointer( program.attributes.normal, 3, _gl.FLOAT, false, 0, 0 );
- }
- if ( object.hasUvs && material.map ) {
- _gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webglUvBuffer );
- _gl.bufferData( _gl.ARRAY_BUFFER, object.uvArray, _gl.DYNAMIC_DRAW );
- state.enableAttribute( program.attributes.uv );
- _gl.vertexAttribPointer( program.attributes.uv, 2, _gl.FLOAT, false, 0, 0 );
- }
- if ( object.hasColors && material.vertexColors !== THREE.NoColors ) {
- _gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webglColorBuffer );
- _gl.bufferData( _gl.ARRAY_BUFFER, object.colorArray, _gl.DYNAMIC_DRAW );
- state.enableAttribute( program.attributes.color );
- _gl.vertexAttribPointer( program.attributes.color, 3, _gl.FLOAT, false, 0, 0 );
- }
- state.disableUnusedAttributes();
- _gl.drawArrays( _gl.TRIANGLES, 0, object.count );
- object.count = 0;
- };
- function setupVertexAttributes( material, program, geometry, startIndex ) {
- var extension;
- if ( geometry instanceof THREE.InstancedBufferGeometry ) {
- extension = extensions.get( 'ANGLE_instanced_arrays' );
- if ( extension === null ) {
- THREE.error( 'THREE.WebGLRenderer.setupVertexAttributes: using THREE.InstancedBufferGeometry but hardware does not support extension ANGLE_instanced_arrays.' );
- return;
- }
- }
- var geometryAttributes = geometry.attributes;
- var programAttributes = program.attributes;
- var programAttributesKeys = program.attributesKeys;
- for ( var i = 0, l = programAttributesKeys.length; i < l; i ++ ) {
- var key = programAttributesKeys[ i ];
- var programAttribute = programAttributes[ key ];
- if ( programAttribute >= 0 ) {
- var geometryAttribute = geometryAttributes[ key ];
- if ( geometryAttribute !== undefined ) {
- var size = geometryAttribute.itemSize;
- state.enableAttribute( programAttribute );
- if ( geometryAttribute instanceof THREE.InterleavedBufferAttribute ) {
- var data = geometryAttribute.data;
- var stride = data.stride;
- var offset = geometryAttribute.offset;
- _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryAttribute.data.buffer );
- _gl.vertexAttribPointer( programAttribute, size, _gl.FLOAT, false, stride * data.array.BYTES_PER_ELEMENT, ( startIndex * stride + offset ) * data.array.BYTES_PER_ELEMENT );
- if ( data instanceof THREE.InstancedInterleavedBuffer ) {
- if ( extension === null ) {
- THREE.error( 'THREE.WebGLRenderer.setupVertexAttributes: using THREE.InstancedBufferAttribute but hardware does not support extension ANGLE_instanced_arrays.' );
- return;
- }
- extension.vertexAttribDivisorANGLE( programAttribute, data.meshPerAttribute );
- if ( geometry.maxInstancedCount === undefined ) {
- geometry.maxInstancedCount = data.meshPerAttribute * ( data.array.length / data.stride );
- }
- }
- } else {
- _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryAttribute.buffer );
- _gl.vertexAttribPointer( programAttribute, size, _gl.FLOAT, false, 0, startIndex * size * 4 ); // 4 bytes per Float32
- if ( geometryAttribute instanceof THREE.InstancedBufferAttribute ) {
- if ( extension === null ) {
- THREE.error( 'THREE.WebGLRenderer.setupVertexAttributes: using THREE.InstancedBufferAttribute but hardware does not support extension ANGLE_instanced_arrays.' );
- return;
- }
- extension.vertexAttribDivisorANGLE( programAttribute, geometryAttribute.meshPerAttribute );
- if ( geometry.maxInstancedCount === undefined ) {
- geometry.maxInstancedCount = geometryAttribute.meshPerAttribute * ( geometryAttribute.array.length / geometryAttribute.itemSize );
- }
- }
- }
- } else if ( material.defaultAttributeValues !== undefined ) {
- if ( material.defaultAttributeValues[ key ].length === 2 ) {
- _gl.vertexAttrib2fv( programAttribute, material.defaultAttributeValues[ key ] );
- } else if ( material.defaultAttributeValues[ key ].length === 3 ) {
- _gl.vertexAttrib3fv( programAttribute, material.defaultAttributeValues[ key ] );
- }
- }
- }
- }
- state.disableUnusedAttributes();
- }
- this.renderBufferDirect = function ( camera, lights, fog, material, geometry, object ) {
- if ( material.visible === false ) return;
- objects.update( object );
- var program = setProgram( camera, lights, fog, material, object );
- var updateBuffers = false,
- wireframeBit = material.wireframe ? 1 : 0,
- geometryProgram = 'direct_' + geometry.id + '_' + program.id + '_' + wireframeBit;
- if ( geometryProgram !== _currentGeometryProgram ) {
- _currentGeometryProgram = geometryProgram;
- updateBuffers = true;
- }
- if ( updateBuffers ) {
- state.initAttributes();
- }
- // render mesh
- if ( object instanceof THREE.Mesh ) {
- var mode = material.wireframe === true ? _gl.LINES : _gl.TRIANGLES;
- var index = geometry.attributes.index;
- if ( index ) {
- // indexed triangles
- var type, size;
- if ( index.array instanceof Uint32Array && extensions.get( 'OES_element_index_uint' ) ) {
- type = _gl.UNSIGNED_INT;
- size = 4;
- } else {
- type = _gl.UNSIGNED_SHORT;
- size = 2;
- }
- var offsets = geometry.offsets;
- if ( offsets.length === 0 ) {
- if ( updateBuffers ) {
- setupVertexAttributes( material, program, geometry, 0 );
- _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, index.buffer );
- }
- if ( geometry instanceof THREE.InstancedBufferGeometry && geometry.maxInstancedCount > 0 ) {
- var extension = extensions.get( 'ANGLE_instanced_arrays' );
- if ( extension === null ) {
- THREE.error( 'THREE.WebGLRenderer.setupVertexAttributes: using THREE.InstancedBufferGeometry but hardware does not support extension ANGLE_instanced_arrays.' );
- return;
- }
- extension.drawElementsInstancedANGLE( mode, index.array.length, type, 0, geometry.maxInstancedCount ); // Draw the instanced meshes
- } else {
- _gl.drawElements( mode, index.array.length, type, 0 );
- }
- _this.info.render.calls ++;
- _this.info.render.vertices += index.array.length; // not really true, here vertices can be shared
- _this.info.render.faces += index.array.length / 3;
- } else {
- // if there is more than 1 chunk
- // must set attribute pointers to use new offsets for each chunk
- // even if geometry and materials didn't change
- updateBuffers = true;
- for ( var i = 0, il = offsets.length; i < il; i ++ ) {
- var startIndex = offsets[ i ].index;
- if ( updateBuffers ) {
- setupVertexAttributes( material, program, geometry, startIndex );
- _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, index.buffer );
- }
- // render indexed triangles
- if ( geometry instanceof THREE.InstancedBufferGeometry && offsets[i].instances > 0 ) {
- var extension = extensions.get( 'ANGLE_instanced_arrays' );
- if ( extension === null ) {
- THREE.error( 'THREE.WebGLRenderer.setupVertexAttributes: using THREE.InstancedBufferGeometry but hardware does not support extension ANGLE_instanced_arrays.' );
- return;
- }
- extension.drawElementsInstancedANGLE( mode, offsets[i].count, type, offsets[i].start * size, offsets[i].count, type, offsets[i].instances ); // Draw the instanced meshes
- } else {
- _gl.drawElements( mode, offsets[ i ].count, type, offsets[ i ].start * size );
- }
- _this.info.render.calls ++;
- _this.info.render.vertices += offsets[ i ].count; // not really true, here vertices can be shared
- _this.info.render.faces += offsets[ i ].count / 3;
- }
- }
- } else {
- // non-indexed triangles
- if ( updateBuffers ) {
- setupVertexAttributes( material, program, geometry, 0 );
- }
- var position = geometry.attributes[ 'position' ];
- // render non-indexed triangles
- if ( geometry instanceof THREE.InstancedBufferGeometry && geometry.maxInstancedCount > 0 ) {
- var extension = extensions.get( 'ANGLE_instanced_arrays' );
- if ( extension === null ) {
- THREE.error( 'THREE.WebGLRenderer.setupVertexAttributes: using THREE.InstancedBufferGeometry but hardware does not support extension ANGLE_instanced_arrays.' );
- return;
- }
- if ( position instanceof THREE.InterleavedBufferAttribute ) {
- extension.drawArraysInstancedANGLE( mode, 0, position.data.array.length / position.data.stride, geometry.maxInstancedCount ); // Draw the instanced meshes
- } else {
- extension.drawArraysInstancedANGLE( mode, 0, position.array.length / position.itemSize, geometry.maxInstancedCount ); // Draw the instanced meshes
- }
- } else {
- if ( position instanceof THREE.InterleavedBufferAttribute ) {
- _gl.drawArrays( mode, 0, position.data.array.length / position.data.stride );
- } else {
- _gl.drawArrays( mode, 0, position.array.length / position.itemSize );
- }
- }
- _this.info.render.calls ++;
- _this.info.render.vertices += position.array.length / position.itemSize;
- _this.info.render.faces += position.array.length / ( 3 * position.itemSize );
- }
- } else if ( object instanceof THREE.PointCloud ) {
- // render particles
- var mode = _gl.POINTS;
- var index = geometry.attributes.index;
- if ( index ) {
- // indexed points
- var type, size;
- if ( index.array instanceof Uint32Array && extensions.get( 'OES_element_index_uint' ) ) {
- type = _gl.UNSIGNED_INT;
- size = 4;
- } else {
- type = _gl.UNSIGNED_SHORT;
- size = 2;
- }
- var offsets = geometry.offsets;
- if ( offsets.length === 0 ) {
- if ( updateBuffers ) {
- setupVertexAttributes( material, program, geometry, 0 );
- _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, index.buffer );
- }
- _gl.drawElements( mode, index.array.length, type, 0);
- _this.info.render.calls ++;
- _this.info.render.points += index.array.length;
- } else {
- // if there is more than 1 chunk
- // must set attribute pointers to use new offsets for each chunk
- // even if geometry and materials didn't change
- if ( offsets.length > 1 ) updateBuffers = true;
- for ( var i = 0, il = offsets.length; i < il; i ++ ) {
- var startIndex = offsets[ i ].index;
- if ( updateBuffers ) {
- setupVertexAttributes( material, program, geometry, startIndex );
- _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, index.buffer );
- }
- // render indexed points
- _gl.drawElements( mode, offsets[ i ].count, type, offsets[ i ].start * size );
- _this.info.render.calls ++;
- _this.info.render.points += offsets[ i ].count;
- }
- }
- } else {
- // non-indexed points
- if ( updateBuffers ) {
- setupVertexAttributes( material, program, geometry, 0 );
- }
- var position = geometry.attributes.position;
- var offsets = geometry.offsets;
- if ( offsets.length === 0 ) {
- _gl.drawArrays( mode, 0, position.array.length / 3 );
- _this.info.render.calls ++;
- _this.info.render.points += position.array.length / 3;
- } else {
- for ( var i = 0, il = offsets.length; i < il; i ++ ) {
- _gl.drawArrays( mode, offsets[ i ].index, offsets[ i ].count );
- _this.info.render.calls ++;
- _this.info.render.points += offsets[ i ].count;
- }
- }
- }
- } else if ( object instanceof THREE.Line ) {
- var mode = ( object.mode === THREE.LineStrip ) ? _gl.LINE_STRIP : _gl.LINES;
- // In case user is not using Line*Material by mistake
- var lineWidth = material.linewidth !== undefined ? material.linewidth : 1;
- state.setLineWidth( lineWidth * pixelRatio );
- var index = geometry.attributes.index;
- if ( index ) {
- // indexed lines
- var type, size;
- if ( index.array instanceof Uint32Array ) {
- type = _gl.UNSIGNED_INT;
- size = 4;
- } else {
- type = _gl.UNSIGNED_SHORT;
- size = 2;
- }
- var offsets = geometry.offsets;
- if ( offsets.length === 0 ) {
- if ( updateBuffers ) {
- setupVertexAttributes( material, program, geometry, 0 );
- _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, index.buffer );
- }
- _gl.drawElements( mode, index.array.length, type, 0 ); // 2 bytes per Uint16Array
- _this.info.render.calls ++;
- _this.info.render.vertices += index.array.length; // not really true, here vertices can be shared
- } else {
- // if there is more than 1 chunk
- // must set attribute pointers to use new offsets for each chunk
- // even if geometry and materials didn't change
- if ( offsets.length > 1 ) updateBuffers = true;
- for ( var i = 0, il = offsets.length; i < il; i ++ ) {
- var startIndex = offsets[ i ].index;
- if ( updateBuffers ) {
- setupVertexAttributes( material, program, geometry, startIndex );
- _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, index.buffer );
- }
- // render indexed lines
- _gl.drawElements( mode, offsets[ i ].count, type, offsets[ i ].start * size ); // 2 bytes per Uint16Array
- _this.info.render.calls ++;
- _this.info.render.vertices += offsets[ i ].count; // not really true, here vertices can be shared
- }
- }
- } else {
- // non-indexed lines
- if ( updateBuffers ) {
- setupVertexAttributes( material, program, geometry, 0 );
- }
- var position = geometry.attributes.position;
- var offsets = geometry.offsets;
- if ( offsets.length === 0 ) {
- _gl.drawArrays( mode, 0, position.array.length / 3 );
- _this.info.render.calls ++;
- _this.info.render.vertices += position.array.length / 3;
- } else {
- for ( var i = 0, il = offsets.length; i < il; i ++ ) {
- _gl.drawArrays( mode, offsets[ i ].index, offsets[ i ].count );
- _this.info.render.calls ++;
- _this.info.render.vertices += offsets[ i ].count;
- }
- }
- }
- }
- };
- function setupMorphTargets ( material, geometryGroup, object ) {
- // set base
- var attributes = material.program.attributes;
- if ( object.morphTargetBase !== - 1 && attributes.position >= 0 ) {
- _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphTargetsBuffers[ object.morphTargetBase ] );
- state.enableAttribute( attributes.position );
- _gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
- } else if ( attributes.position >= 0 ) {
- _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglVertexBuffer );
- state.enableAttribute( attributes.position );
- _gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
- }
- if ( object.morphTargetForcedOrder.length ) {
- // set forced order
- var m = 0;
- var order = object.morphTargetForcedOrder;
- var influences = object.morphTargetInfluences;
- var attribute;
- while ( m < material.numSupportedMorphTargets && m < order.length ) {
- attribute = attributes[ 'morphTarget' + m ];
- if ( attribute >= 0 ) {
- _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphTargetsBuffers[ order[ m ] ] );
- state.enableAttribute( attribute );
- _gl.vertexAttribPointer( attribute, 3, _gl.FLOAT, false, 0, 0 );
- }
- attribute = attributes[ 'morphNormal' + m ];
- if ( attribute >= 0 && material.morphNormals ) {
- _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphNormalsBuffers[ order[ m ] ] );
- state.enableAttribute( attribute );
- _gl.vertexAttribPointer( attribute, 3, _gl.FLOAT, false, 0, 0 );
- }
- object.__webglMorphTargetInfluences[ m ] = influences[ order[ m ] ];
- m ++;
- }
- } else {
- // find the most influencing
- var activeInfluenceIndices = [];
- var influences = object.morphTargetInfluences;
- var morphTargets = object.geometry.morphTargets;
- if ( influences.length > morphTargets.length ) {
- THREE.warn( 'THREE.WebGLRenderer: Influences array is bigger than morphTargets array.' );
- influences.length = morphTargets.length;
- }
- for ( var i = 0, il = influences.length; i < il; i ++ ) {
- var influence = influences[ i ];
- activeInfluenceIndices.push( [ influence, i ] );
- }
- if ( activeInfluenceIndices.length > material.numSupportedMorphTargets ) {
- activeInfluenceIndices.sort( numericalSort );
- activeInfluenceIndices.length = material.numSupportedMorphTargets;
- } else if ( activeInfluenceIndices.length > material.numSupportedMorphNormals ) {
- activeInfluenceIndices.sort( numericalSort );
- } else if ( activeInfluenceIndices.length === 0 ) {
- activeInfluenceIndices.push( [ 0, 0 ] );
- }
- var attribute;
- for ( var m = 0, ml = material.numSupportedMorphTargets; m < ml; m ++ ) {
- if ( activeInfluenceIndices[ m ] ) {
- var influenceIndex = activeInfluenceIndices[ m ][ 1 ];
- attribute = attributes[ 'morphTarget' + m ];
- if ( attribute >= 0 ) {
- _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphTargetsBuffers[ influenceIndex ] );
- state.enableAttribute( attribute );
- _gl.vertexAttribPointer( attribute, 3, _gl.FLOAT, false, 0, 0 );
- }
- attribute = attributes[ 'morphNormal' + m ];
- if ( attribute >= 0 && material.morphNormals ) {
- _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphNormalsBuffers[ influenceIndex ] );
- state.enableAttribute( attribute );
- _gl.vertexAttribPointer( attribute, 3, _gl.FLOAT, false, 0, 0 );
- }
- object.__webglMorphTargetInfluences[ m ] = influences[ influenceIndex ];
- } else {
- /*
- _gl.vertexAttribPointer( attributes[ "morphTarget" + m ], 3, _gl.FLOAT, false, 0, 0 );
- if ( material.morphNormals ) {
- _gl.vertexAttribPointer( attributes[ "morphNormal" + m ], 3, _gl.FLOAT, false, 0, 0 );
- }
- */
- object.__webglMorphTargetInfluences[ m ] = 0;
- }
- }
- }
- // load updated influences uniform
- if ( material.program.uniforms.morphTargetInfluences !== null ) {
- _gl.uniform1fv( material.program.uniforms.morphTargetInfluences, object.__webglMorphTargetInfluences );
- }
- }
- // Sorting
- function painterSortStable ( a, b ) {
- if ( a.object.renderOrder !== b.object.renderOrder ) {
- return a.object.renderOrder - b.object.renderOrder;
- } else if ( a.material.id !== b.material.id ) {
- return a.material.id - b.material.id;
- } else if ( a.z !== b.z ) {
- return a.z - b.z;
- } else {
- return a.id - b.id;
- }
- }
- function reversePainterSortStable ( a, b ) {
- if ( a.object.renderOrder !== b.object.renderOrder ) {
- return a.object.renderOrder - b.object.renderOrder;
- } if ( a.z !== b.z ) {
- return b.z - a.z;
- } else {
- return a.id - b.id;
- }
- }
- function numericalSort ( a, b ) {
- return b[ 0 ] - a[ 0 ];
- }
- // Rendering
- this.render = function ( scene, camera, renderTarget, forceClear ) {
- if ( camera instanceof THREE.Camera === false ) {
- THREE.error( 'THREE.WebGLRenderer.render: camera is not an instance of THREE.Camera.' );
- return;
- }
- var fog = scene.fog;
- // reset caching for this frame
- _currentGeometryProgram = '';
- _currentMaterialId = - 1;
- _currentCamera = null;
- _lightsNeedUpdate = true;
- // update scene graph
- if ( scene.autoUpdate === true ) scene.updateMatrixWorld();
- // update camera matrices and frustum
- if ( camera.parent === undefined ) camera.updateMatrixWorld();
- // update Skeleton objects
- scene.traverse( function ( object ) {
- if ( object instanceof THREE.SkinnedMesh ) {
- object.skeleton.update();
- }
- } );
- camera.matrixWorldInverse.getInverse( camera.matrixWorld );
- _projScreenMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse );
- _frustum.setFromMatrix( _projScreenMatrix );
- lights.length = 0;
- opaqueObjects.length = 0;
- transparentObjects.length = 0;
- sprites.length = 0;
- lensFlares.length = 0;
- projectObject( scene );
- if ( _this.sortObjects === true ) {
- opaqueObjects.sort( painterSortStable );
- transparentObjects.sort( reversePainterSortStable );
- }
- //
- shadowMap.render( scene, camera );
- //
- _this.info.render.calls = 0;
- _this.info.render.vertices = 0;
- _this.info.render.faces = 0;
- _this.info.render.points = 0;
- this.setRenderTarget( renderTarget );
- if ( this.autoClear || forceClear ) {
- this.clear( this.autoClearColor, this.autoClearDepth, this.autoClearStencil );
- }
- // set matrices for immediate objects
- for ( var i = 0, il = objects.objectsImmediate.length; i < il; i ++ ) {
- var webglObject = objects.objectsImmediate[ i ];
- var object = webglObject.object;
- if ( object.visible ) {
- setupMatrices( object, camera );
- unrollImmediateBufferMaterial( webglObject );
- }
- }
- if ( scene.overrideMaterial ) {
- var overrideMaterial = scene.overrideMaterial;
- setMaterial( overrideMaterial );
- renderObjects( opaqueObjects, camera, lights, fog, overrideMaterial );
- renderObjects( transparentObjects, camera, lights, fog, overrideMaterial );
- renderObjectsImmediate( objects.objectsImmediate, '', camera, lights, fog, overrideMaterial );
- } else {
- // opaque pass (front-to-back order)
- state.setBlending( THREE.NoBlending );
- renderObjects( opaqueObjects, camera, lights, fog, null );
- renderObjectsImmediate( objects.objectsImmediate, 'opaque', camera, lights, fog, null );
- // transparent pass (back-to-front order)
- renderObjects( transparentObjects, camera, lights, fog, null );
- renderObjectsImmediate( objects.objectsImmediate, 'transparent', camera, lights, fog, null );
- }
- // custom render plugins (post pass)
- spritePlugin.render( scene, camera );
- lensFlarePlugin.render( scene, camera, _currentWidth, _currentHeight );
- // Generate mipmap if we're using any kind of mipmap filtering
- if ( renderTarget && renderTarget.generateMipmaps && renderTarget.minFilter !== THREE.NearestFilter && renderTarget.minFilter !== THREE.LinearFilter ) {
- updateRenderTargetMipmap( renderTarget );
- }
- // Ensure depth buffer writing is enabled so it can be cleared on next render
- state.setDepthTest( true );
- state.setDepthWrite( true );
- state.setColorWrite( true );
- // _gl.finish();
- };
- function projectObject( object ) {
- if ( object.visible === false ) return;
- if ( object instanceof THREE.Scene || object instanceof THREE.Group ) {
- // skip
- } else {
- objects.init( object );
- if ( object instanceof THREE.Light ) {
- lights.push( object );
- } else if ( object instanceof THREE.Sprite ) {
- sprites.push( object );
- } else if ( object instanceof THREE.LensFlare ) {
- lensFlares.push( object );
- } else {
- var webglObject = objects.objects[ object.id ];
- if ( webglObject && ( object.frustumCulled === false || _frustum.intersectsObject( object ) === true ) ) {
- unrollBufferMaterial( webglObject );
- webglObject.render = true;
- if ( _this.sortObjects === true ) {
- _vector3.setFromMatrixPosition( object.matrixWorld );
- _vector3.applyProjection( _projScreenMatrix );
- webglObject.z = _vector3.z;
- }
- }
- }
- }
- for ( var i = 0, l = object.children.length; i < l; i ++ ) {
- projectObject( object.children[ i ] );
- }
- }
- function renderObjects( renderList, camera, lights, fog, overrideMaterial ) {
- var material;
- for ( var i = 0, l = renderList.length; i < l; i ++ ) {
- var webglObject = renderList[ i ];
- var object = webglObject.object;
- var buffer = webglObject.buffer;
- setupMatrices( object, camera );
- if ( overrideMaterial ) {
- material = overrideMaterial;
- } else {
- material = webglObject.material;
- if ( ! material ) continue;
- setMaterial( material );
- }
- _this.setMaterialFaces( material );
- _this.renderBufferDirect( camera, lights, fog, material, buffer, object );
- }
- }
- function renderObjectsImmediate ( renderList, materialType, camera, lights, fog, overrideMaterial ) {
- var material;
- for ( var i = 0, l = renderList.length; i < l; i ++ ) {
- var webglObject = renderList[ i ];
- var object = webglObject.object;
- if ( object.visible ) {
- if ( overrideMaterial ) {
- material = overrideMaterial;
- } else {
- material = webglObject[ materialType ];
- if ( ! material ) continue;
- setMaterial( material );
- }
- _this.renderImmediateObject( camera, lights, fog, material, object );
- }
- }
- }
- this.renderImmediateObject = function ( camera, lights, fog, material, object ) {
- var program = setProgram( camera, lights, fog, material, object );
- _currentGeometryProgram = '';
- _this.setMaterialFaces( material );
- if ( object.immediateRenderCallback ) {
- object.immediateRenderCallback( program, _gl, _frustum );
- } else {
- object.render( function ( object ) { _this.renderBufferImmediate( object, program, material ); } );
- }
- };
- function unrollImmediateBufferMaterial ( globject ) {
- var object = globject.object,
- material = object.material;
- if ( material.transparent ) {
- globject.transparent = material;
- globject.opaque = null;
- } else {
- globject.opaque = material;
- globject.transparent = null;
- }
- }
- function unrollBufferMaterial ( globject ) {
- var object = globject.object;
- var buffer = globject.buffer;
- var geometry = object.geometry;
- var material = object.material;
- if ( material ) {
- globject.material = material;
- if ( material.transparent ) {
- transparentObjects.push( globject );
- } else {
- opaqueObjects.push( globject );
- }
- }
- }
- // Materials
- var shaderIDs = {
- MeshDepthMaterial: 'depth',
- MeshNormalMaterial: 'normal',
- MeshBasicMaterial: 'basic',
- MeshLambertMaterial: 'lambert',
- MeshPhongMaterial: 'phong',
- LineBasicMaterial: 'basic',
- LineDashedMaterial: 'dashed',
- PointCloudMaterial: 'particle_basic'
- };
- function initMaterial( material, lights, fog, object ) {
- var shaderID = shaderIDs[ material.type ];
- // heuristics to create shader parameters according to lights in the scene
- // (not to blow over maxLights budget)
- var maxLightCount = allocateLights( lights );
- var maxShadows = allocateShadows( lights );
- var maxBones = allocateBones( object );
- var parameters = {
- precision: _precision,
- supportsVertexTextures: _supportsVertexTextures,
- map: !! material.map,
- envMap: !! material.envMap,
- envMapMode: material.envMap && material.envMap.mapping,
- lightMap: !! material.lightMap,
- aoMap: !! material.aoMap,
- bumpMap: !! material.bumpMap,
- normalMap: !! material.normalMap,
- specularMap: !! material.specularMap,
- alphaMap: !! material.alphaMap,
- combine: material.combine,
- vertexColors: material.vertexColors,
- fog: fog,
- useFog: material.fog,
- fogExp: fog instanceof THREE.FogExp2,
- flatShading: material.shading === THREE.FlatShading,
- sizeAttenuation: material.sizeAttenuation,
- logarithmicDepthBuffer: _logarithmicDepthBuffer,
- skinning: material.skinning,
- maxBones: maxBones,
- useVertexTexture: _supportsBoneTextures && object && object.skeleton && object.skeleton.useVertexTexture,
- morphTargets: material.morphTargets,
- morphNormals: material.morphNormals,
- maxMorphTargets: _this.maxMorphTargets,
- maxMorphNormals: _this.maxMorphNormals,
- maxDirLights: maxLightCount.directional,
- maxPointLights: maxLightCount.point,
- maxSpotLights: maxLightCount.spot,
- maxHemiLights: maxLightCount.hemi,
- maxShadows: maxShadows,
- shadowMapEnabled: shadowMap.enabled && object.receiveShadow && maxShadows > 0,
- shadowMapType: shadowMap.type,
- shadowMapDebug: shadowMap.debug,
- shadowMapCascade: shadowMap.cascade,
- alphaTest: material.alphaTest,
- metal: material.metal,
- wrapAround: material.wrapAround,
- doubleSided: material.side === THREE.DoubleSide,
- flipSided: material.side === THREE.BackSide
- };
- // Generate code
- var chunks = [];
- if ( shaderID ) {
- chunks.push( shaderID );
- } else {
- chunks.push( material.fragmentShader );
- chunks.push( material.vertexShader );
- }
- if ( material.defines !== undefined ) {
- for ( var name in material.defines ) {
- chunks.push( name );
- chunks.push( material.defines[ name ] );
- }
- }
- for ( var name in parameters ) {
- chunks.push( name );
- chunks.push( parameters[ name ] );
- }
- var code = chunks.join();
- if ( !material.program ) {
- // new material
- material.addEventListener( 'dispose', onMaterialDispose );
- } else if ( material.program.code !== code ) {
- // changed glsl or parameters
- deallocateMaterial( material );
- } else {
- // same glsl and parameters
- return;
- }
- if ( shaderID ) {
- var shader = THREE.ShaderLib[ shaderID ];
- material.__webglShader = {
- uniforms: THREE.UniformsUtils.clone( shader.uniforms ),
- vertexShader: shader.vertexShader,
- fragmentShader: shader.fragmentShader
- }
- } else {
- material.__webglShader = {
- uniforms: material.uniforms,
- vertexShader: material.vertexShader,
- fragmentShader: material.fragmentShader
- }
- }
- var program;
- // Check if code has been already compiled
- for ( var p = 0, pl = _programs.length; p < pl; p ++ ) {
- var programInfo = _programs[ p ];
- if ( programInfo.code === code ) {
- program = programInfo;
- program.usedTimes ++;
- break;
- }
- }
- if ( program === undefined ) {
- program = new THREE.WebGLProgram( _this, code, material, parameters );
- _programs.push( program );
- _this.info.memory.programs = _programs.length;
- }
- material.program = program;
- var attributes = program.attributes;
- if ( material.morphTargets ) {
- material.numSupportedMorphTargets = 0;
- var id, base = 'morphTarget';
- for ( var i = 0; i < _this.maxMorphTargets; i ++ ) {
- id = base + i;
- if ( attributes[ id ] >= 0 ) {
- material.numSupportedMorphTargets ++;
- }
- }
- }
- if ( material.morphNormals ) {
- material.numSupportedMorphNormals = 0;
- var id, base = 'morphNormal';
- for ( i = 0; i < _this.maxMorphNormals; i ++ ) {
- id = base + i;
- if ( attributes[ id ] >= 0 ) {
- material.numSupportedMorphNormals ++;
- }
- }
- }
- material.uniformsList = [];
- for ( var u in material.__webglShader.uniforms ) {
- var location = material.program.uniforms[ u ];
- if ( location ) {
- material.uniformsList.push( [ material.__webglShader.uniforms[ u ], location ] );
- }
- }
- }
- function setMaterial( material ) {
- if ( material.transparent === true ) {
- state.setBlending( material.blending, material.blendEquation, material.blendSrc, material.blendDst, material.blendEquationAlpha, material.blendSrcAlpha, material.blendDstAlpha );
- } else {
- state.setBlending( THREE.NoBlending );
- }
- state.setDepthFunc( material.depthFunc );
- state.setDepthTest( material.depthTest );
- state.setDepthWrite( material.depthWrite );
- state.setColorWrite( material.colorWrite );
- state.setPolygonOffset( material.polygonOffset, material.polygonOffsetFactor, material.polygonOffsetUnits );
- }
- function setProgram( camera, lights, fog, material, object ) {
- _usedTextureUnits = 0;
- if ( material.needsUpdate ) {
- initMaterial( material, lights, fog, object );
- material.needsUpdate = false;
- }
- if ( material.morphTargets ) {
- if ( ! object.__webglMorphTargetInfluences ) {
- object.__webglMorphTargetInfluences = new Float32Array( _this.maxMorphTargets );
- }
- }
- var refreshProgram = false;
- var refreshMaterial = false;
- var refreshLights = false;
- var program = material.program,
- p_uniforms = program.uniforms,
- m_uniforms = material.__webglShader.uniforms;
- if ( program.id !== _currentProgram ) {
- _gl.useProgram( program.program );
- _currentProgram = program.id;
- refreshProgram = true;
- refreshMaterial = true;
- refreshLights = true;
- }
- if ( material.id !== _currentMaterialId ) {
- if ( _currentMaterialId === -1 ) refreshLights = true;
- _currentMaterialId = material.id;
- refreshMaterial = true;
- }
- if ( refreshProgram || camera !== _currentCamera ) {
- _gl.uniformMatrix4fv( p_uniforms.projectionMatrix, false, camera.projectionMatrix.elements );
- if ( _logarithmicDepthBuffer ) {
- _gl.uniform1f( p_uniforms.logDepthBufFC, 2.0 / ( Math.log( camera.far + 1.0 ) / Math.LN2 ) );
- }
- if ( camera !== _currentCamera ) _currentCamera = camera;
- // load material specific uniforms
- // (shader material also gets them for the sake of genericity)
- if ( material instanceof THREE.ShaderMaterial ||
- material instanceof THREE.MeshPhongMaterial ||
- material.envMap ) {
- if ( p_uniforms.cameraPosition !== null ) {
- _vector3.setFromMatrixPosition( camera.matrixWorld );
- _gl.uniform3f( p_uniforms.cameraPosition, _vector3.x, _vector3.y, _vector3.z );
- }
- }
- if ( material instanceof THREE.MeshPhongMaterial ||
- material instanceof THREE.MeshLambertMaterial ||
- material instanceof THREE.MeshBasicMaterial ||
- material instanceof THREE.ShaderMaterial ||
- material.skinning ) {
- if ( p_uniforms.viewMatrix !== null ) {
- _gl.uniformMatrix4fv( p_uniforms.viewMatrix, false, camera.matrixWorldInverse.elements );
- }
- }
- }
- // skinning uniforms must be set even if material didn't change
- // auto-setting of texture unit for bone texture must go before other textures
- // not sure why, but otherwise weird things happen
- if ( material.skinning ) {
- if ( object.bindMatrix && p_uniforms.bindMatrix !== null ) {
- _gl.uniformMatrix4fv( p_uniforms.bindMatrix, false, object.bindMatrix.elements );
- }
- if ( object.bindMatrixInverse && p_uniforms.bindMatrixInverse !== null ) {
- _gl.uniformMatrix4fv( p_uniforms.bindMatrixInverse, false, object.bindMatrixInverse.elements );
- }
- if ( _supportsBoneTextures && object.skeleton && object.skeleton.useVertexTexture ) {
- if ( p_uniforms.boneTexture !== null ) {
- var textureUnit = getTextureUnit();
- _gl.uniform1i( p_uniforms.boneTexture, textureUnit );
- _this.setTexture( object.skeleton.boneTexture, textureUnit );
- }
- if ( p_uniforms.boneTextureWidth !== null ) {
- _gl.uniform1i( p_uniforms.boneTextureWidth, object.skeleton.boneTextureWidth );
- }
- if ( p_uniforms.boneTextureHeight !== null ) {
- _gl.uniform1i( p_uniforms.boneTextureHeight, object.skeleton.boneTextureHeight );
- }
- } else if ( object.skeleton && object.skeleton.boneMatrices ) {
- if ( p_uniforms.boneGlobalMatrices !== null ) {
- _gl.uniformMatrix4fv( p_uniforms.boneGlobalMatrices, false, object.skeleton.boneMatrices );
- }
- }
- }
- if ( refreshMaterial ) {
- // refresh uniforms common to several materials
- if ( fog && material.fog ) {
- refreshUniformsFog( m_uniforms, fog );
- }
- if ( material instanceof THREE.MeshPhongMaterial ||
- material instanceof THREE.MeshLambertMaterial ||
- material.lights ) {
- if ( _lightsNeedUpdate ) {
- refreshLights = true;
- setupLights( lights );
- _lightsNeedUpdate = false;
- }
- if ( refreshLights ) {
- refreshUniformsLights( m_uniforms, _lights );
- markUniformsLightsNeedsUpdate( m_uniforms, true );
- } else {
- markUniformsLightsNeedsUpdate( m_uniforms, false );
- }
- }
- if ( material instanceof THREE.MeshBasicMaterial ||
- material instanceof THREE.MeshLambertMaterial ||
- material instanceof THREE.MeshPhongMaterial ) {
- refreshUniformsCommon( m_uniforms, material );
- }
- // refresh single material specific uniforms
- if ( material instanceof THREE.LineBasicMaterial ) {
- refreshUniformsLine( m_uniforms, material );
- } else if ( material instanceof THREE.LineDashedMaterial ) {
- refreshUniformsLine( m_uniforms, material );
- refreshUniformsDash( m_uniforms, material );
- } else if ( material instanceof THREE.PointCloudMaterial ) {
- refreshUniformsParticle( m_uniforms, material );
- } else if ( material instanceof THREE.MeshPhongMaterial ) {
- refreshUniformsPhong( m_uniforms, material );
- } else if ( material instanceof THREE.MeshLambertMaterial ) {
- refreshUniformsLambert( m_uniforms, material );
- } else if ( material instanceof THREE.MeshDepthMaterial ) {
- m_uniforms.mNear.value = camera.near;
- m_uniforms.mFar.value = camera.far;
- m_uniforms.opacity.value = material.opacity;
- } else if ( material instanceof THREE.MeshNormalMaterial ) {
- m_uniforms.opacity.value = material.opacity;
- }
- if ( object.receiveShadow && ! material._shadowPass ) {
- refreshUniformsShadow( m_uniforms, lights );
- }
- // load common uniforms
- loadUniformsGeneric( material.uniformsList );
- }
- loadUniformsMatrices( p_uniforms, object );
- if ( p_uniforms.modelMatrix !== null ) {
- _gl.uniformMatrix4fv( p_uniforms.modelMatrix, false, object.matrixWorld.elements );
- }
- return program;
- }
- // Uniforms (refresh uniforms objects)
- function refreshUniformsCommon ( uniforms, material ) {
- uniforms.opacity.value = material.opacity;
- uniforms.diffuse.value = material.color;
- uniforms.map.value = material.map;
- uniforms.lightMap.value = material.lightMap;
- uniforms.lightMapIntensity.value = material.lightMapIntensity;
- uniforms.aoMap.value = material.aoMap;
- uniforms.aoMapIntensity.value = material.aoMapIntensity;
- uniforms.specularMap.value = material.specularMap;
- uniforms.alphaMap.value = material.alphaMap;
- if ( material.bumpMap ) {
- uniforms.bumpMap.value = material.bumpMap;
- uniforms.bumpScale.value = material.bumpScale;
- }
- if ( material.normalMap ) {
- uniforms.normalMap.value = material.normalMap;
- uniforms.normalScale.value.copy( material.normalScale );
- }
- // uv repeat and offset setting priorities
- // 1. color map
- // 2. specular map
- // 3. normal map
- // 4. bump map
- // 5. alpha map
- var uvScaleMap;
- if ( material.map ) {
- uvScaleMap = material.map;
- } else if ( material.specularMap ) {
- uvScaleMap = material.specularMap;
- } else if ( material.normalMap ) {
- uvScaleMap = material.normalMap;
- } else if ( material.bumpMap ) {
- uvScaleMap = material.bumpMap;
- } else if ( material.alphaMap ) {
- uvScaleMap = material.alphaMap;
- }
- if ( uvScaleMap !== undefined ) {
- var offset = uvScaleMap.offset;
- var repeat = uvScaleMap.repeat;
- uniforms.offsetRepeat.value.set( offset.x, offset.y, repeat.x, repeat.y );
- }
- uniforms.envMap.value = material.envMap;
- uniforms.flipEnvMap.value = ( material.envMap instanceof THREE.WebGLRenderTargetCube ) ? 1 : - 1;
- uniforms.reflectivity.value = material.reflectivity;
- uniforms.refractionRatio.value = material.refractionRatio;
- }
- function refreshUniformsLine ( uniforms, material ) {
- uniforms.diffuse.value = material.color;
- uniforms.opacity.value = material.opacity;
- }
- function refreshUniformsDash ( uniforms, material ) {
- uniforms.dashSize.value = material.dashSize;
- uniforms.totalSize.value = material.dashSize + material.gapSize;
- uniforms.scale.value = material.scale;
- }
- function refreshUniformsParticle ( uniforms, material ) {
- uniforms.psColor.value = material.color;
- uniforms.opacity.value = material.opacity;
- uniforms.size.value = material.size;
- uniforms.scale.value = _canvas.height / 2.0; // TODO: Cache this.
- uniforms.map.value = material.map;
- if ( material.map !== null ) {
- var offset = material.map.offset;
- var repeat = material.map.repeat;
- uniforms.offsetRepeat.value.set( offset.x, offset.y, repeat.x, repeat.y );
- }
- }
- function refreshUniformsFog ( uniforms, fog ) {
- uniforms.fogColor.value = fog.color;
- if ( fog instanceof THREE.Fog ) {
- uniforms.fogNear.value = fog.near;
- uniforms.fogFar.value = fog.far;
- } else if ( fog instanceof THREE.FogExp2 ) {
- uniforms.fogDensity.value = fog.density;
- }
- }
- function refreshUniformsPhong ( uniforms, material ) {
- uniforms.shininess.value = material.shininess;
- uniforms.emissive.value = material.emissive;
- uniforms.specular.value = material.specular;
- if ( material.wrapAround ) {
- uniforms.wrapRGB.value.copy( material.wrapRGB );
- }
- }
- function refreshUniformsLambert ( uniforms, material ) {
- uniforms.emissive.value = material.emissive;
- if ( material.wrapAround ) {
- uniforms.wrapRGB.value.copy( material.wrapRGB );
- }
- }
- function refreshUniformsLights ( uniforms, lights ) {
- uniforms.ambientLightColor.value = lights.ambient;
- uniforms.directionalLightColor.value = lights.directional.colors;
- uniforms.directionalLightDirection.value = lights.directional.positions;
- uniforms.pointLightColor.value = lights.point.colors;
- uniforms.pointLightPosition.value = lights.point.positions;
- uniforms.pointLightDistance.value = lights.point.distances;
- uniforms.pointLightDecay.value = lights.point.decays;
- uniforms.spotLightColor.value = lights.spot.colors;
- uniforms.spotLightPosition.value = lights.spot.positions;
- uniforms.spotLightDistance.value = lights.spot.distances;
- uniforms.spotLightDirection.value = lights.spot.directions;
- uniforms.spotLightAngleCos.value = lights.spot.anglesCos;
- uniforms.spotLightExponent.value = lights.spot.exponents;
- uniforms.spotLightDecay.value = lights.spot.decays;
- uniforms.hemisphereLightSkyColor.value = lights.hemi.skyColors;
- uniforms.hemisphereLightGroundColor.value = lights.hemi.groundColors;
- uniforms.hemisphereLightDirection.value = lights.hemi.positions;
- }
- // If uniforms are marked as clean, they don't need to be loaded to the GPU.
- function markUniformsLightsNeedsUpdate ( uniforms, value ) {
- uniforms.ambientLightColor.needsUpdate = value;
- uniforms.directionalLightColor.needsUpdate = value;
- uniforms.directionalLightDirection.needsUpdate = value;
- uniforms.pointLightColor.needsUpdate = value;
- uniforms.pointLightPosition.needsUpdate = value;
- uniforms.pointLightDistance.needsUpdate = value;
- uniforms.pointLightDecay.needsUpdate = value;
- uniforms.spotLightColor.needsUpdate = value;
- uniforms.spotLightPosition.needsUpdate = value;
- uniforms.spotLightDistance.needsUpdate = value;
- uniforms.spotLightDirection.needsUpdate = value;
- uniforms.spotLightAngleCos.needsUpdate = value;
- uniforms.spotLightExponent.needsUpdate = value;
- uniforms.spotLightDecay.needsUpdate = value;
- uniforms.hemisphereLightSkyColor.needsUpdate = value;
- uniforms.hemisphereLightGroundColor.needsUpdate = value;
- uniforms.hemisphereLightDirection.needsUpdate = value;
- }
- function refreshUniformsShadow ( uniforms, lights ) {
- if ( uniforms.shadowMatrix ) {
- var j = 0;
- for ( var i = 0, il = lights.length; i < il; i ++ ) {
- var light = lights[ i ];
- if ( ! light.castShadow ) continue;
- if ( light instanceof THREE.SpotLight || ( light instanceof THREE.DirectionalLight && ! light.shadowCascade ) ) {
- uniforms.shadowMap.value[ j ] = light.shadowMap;
- uniforms.shadowMapSize.value[ j ] = light.shadowMapSize;
- uniforms.shadowMatrix.value[ j ] = light.shadowMatrix;
- uniforms.shadowDarkness.value[ j ] = light.shadowDarkness;
- uniforms.shadowBias.value[ j ] = light.shadowBias;
- j ++;
- }
- }
- }
- }
- // Uniforms (load to GPU)
- function loadUniformsMatrices ( uniforms, object ) {
- _gl.uniformMatrix4fv( uniforms.modelViewMatrix, false, object._modelViewMatrix.elements );
- if ( uniforms.normalMatrix ) {
- _gl.uniformMatrix3fv( uniforms.normalMatrix, false, object._normalMatrix.elements );
- }
- }
- function getTextureUnit() {
- var textureUnit = _usedTextureUnits;
- if ( textureUnit >= _maxTextures ) {
- THREE.warn( 'WebGLRenderer: trying to use ' + textureUnit + ' texture units while this GPU supports only ' + _maxTextures );
- }
- _usedTextureUnits += 1;
- return textureUnit;
- }
- function loadUniformsGeneric ( uniforms ) {
- var texture, textureUnit, offset;
- for ( var j = 0, jl = uniforms.length; j < jl; j ++ ) {
- var uniform = uniforms[ j ][ 0 ];
- // needsUpdate property is not added to all uniforms.
- if ( uniform.needsUpdate === false ) continue;
- var type = uniform.type;
- var value = uniform.value;
- var location = uniforms[ j ][ 1 ];
- switch ( type ) {
- case '1i':
- _gl.uniform1i( location, value );
- break;
- case '1f':
- _gl.uniform1f( location, value );
- break;
- case '2f':
- _gl.uniform2f( location, value[ 0 ], value[ 1 ] );
- break;
- case '3f':
- _gl.uniform3f( location, value[ 0 ], value[ 1 ], value[ 2 ] );
- break;
- case '4f':
- _gl.uniform4f( location, value[ 0 ], value[ 1 ], value[ 2 ], value[ 3 ] );
- break;
- case '1iv':
- _gl.uniform1iv( location, value );
- break;
- case '3iv':
- _gl.uniform3iv( location, value );
- break;
- case '1fv':
- _gl.uniform1fv( location, value );
- break;
- case '2fv':
- _gl.uniform2fv( location, value );
- break;
- case '3fv':
- _gl.uniform3fv( location, value );
- break;
- case '4fv':
- _gl.uniform4fv( location, value );
- break;
- case 'Matrix3fv':
- _gl.uniformMatrix3fv( location, false, value );
- break;
- case 'Matrix4fv':
- _gl.uniformMatrix4fv( location, false, value );
- break;
- //
- case 'i':
- // single integer
- _gl.uniform1i( location, value );
- break;
- case 'f':
- // single float
- _gl.uniform1f( location, value );
- break;
- case 'v2':
- // single THREE.Vector2
- _gl.uniform2f( location, value.x, value.y );
- break;
- case 'v3':
- // single THREE.Vector3
- _gl.uniform3f( location, value.x, value.y, value.z );
- break;
- case 'v4':
- // single THREE.Vector4
- _gl.uniform4f( location, value.x, value.y, value.z, value.w );
- break;
- case 'c':
- // single THREE.Color
- _gl.uniform3f( location, value.r, value.g, value.b );
- break;
- case 'iv1':
- // flat array of integers (JS or typed array)
- _gl.uniform1iv( location, value );
- break;
- case 'iv':
- // flat array of integers with 3 x N size (JS or typed array)
- _gl.uniform3iv( location, value );
- break;
- case 'fv1':
- // flat array of floats (JS or typed array)
- _gl.uniform1fv( location, value );
- break;
- case 'fv':
- // flat array of floats with 3 x N size (JS or typed array)
- _gl.uniform3fv( location, value );
- break;
- case 'v2v':
- // array of THREE.Vector2
- if ( uniform._array === undefined ) {
- uniform._array = new Float32Array( 2 * value.length );
- }
- for ( var i = 0, il = value.length; i < il; i ++ ) {
- offset = i * 2;
- uniform._array[ offset + 0 ] = value[ i ].x;
- uniform._array[ offset + 1 ] = value[ i ].y;
- }
- _gl.uniform2fv( location, uniform._array );
- break;
- case 'v3v':
- // array of THREE.Vector3
- if ( uniform._array === undefined ) {
- uniform._array = new Float32Array( 3 * value.length );
- }
- for ( var i = 0, il = value.length; i < il; i ++ ) {
- offset = i * 3;
- uniform._array[ offset + 0 ] = value[ i ].x;
- uniform._array[ offset + 1 ] = value[ i ].y;
- uniform._array[ offset + 2 ] = value[ i ].z;
- }
- _gl.uniform3fv( location, uniform._array );
- break;
- case 'v4v':
- // array of THREE.Vector4
- if ( uniform._array === undefined ) {
- uniform._array = new Float32Array( 4 * value.length );
- }
- for ( var i = 0, il = value.length; i < il; i ++ ) {
- offset = i * 4;
- uniform._array[ offset + 0 ] = value[ i ].x;
- uniform._array[ offset + 1 ] = value[ i ].y;
- uniform._array[ offset + 2 ] = value[ i ].z;
- uniform._array[ offset + 3 ] = value[ i ].w;
- }
- _gl.uniform4fv( location, uniform._array );
- break;
- case 'm3':
- // single THREE.Matrix3
- _gl.uniformMatrix3fv( location, false, value.elements );
- break;
- case 'm3v':
- // array of THREE.Matrix3
- if ( uniform._array === undefined ) {
- uniform._array = new Float32Array( 9 * value.length );
- }
- for ( var i = 0, il = value.length; i < il; i ++ ) {
- value[ i ].flattenToArrayOffset( uniform._array, i * 9 );
- }
- _gl.uniformMatrix3fv( location, false, uniform._array );
- break;
- case 'm4':
- // single THREE.Matrix4
- _gl.uniformMatrix4fv( location, false, value.elements );
- break;
- case 'm4v':
- // array of THREE.Matrix4
- if ( uniform._array === undefined ) {
- uniform._array = new Float32Array( 16 * value.length );
- }
- for ( var i = 0, il = value.length; i < il; i ++ ) {
- value[ i ].flattenToArrayOffset( uniform._array, i * 16 );
- }
- _gl.uniformMatrix4fv( location, false, uniform._array );
- break;
- case 't':
- // single THREE.Texture (2d or cube)
- texture = value;
- textureUnit = getTextureUnit();
- _gl.uniform1i( location, textureUnit );
- if ( ! texture ) continue;
- if ( texture instanceof THREE.CubeTexture ||
- ( texture.image instanceof Array && texture.image.length === 6 ) ) { // CompressedTexture can have Array in image :/
- setCubeTexture( texture, textureUnit );
- } else if ( texture instanceof THREE.WebGLRenderTargetCube ) {
- setCubeTextureDynamic( texture, textureUnit );
- } else {
- _this.setTexture( texture, textureUnit );
- }
- break;
- case 'tv':
- // array of THREE.Texture (2d)
- if ( uniform._array === undefined ) {
- uniform._array = [];
- }
- for ( var i = 0, il = uniform.value.length; i < il; i ++ ) {
- uniform._array[ i ] = getTextureUnit();
- }
- _gl.uniform1iv( location, uniform._array );
- for ( var i = 0, il = uniform.value.length; i < il; i ++ ) {
- texture = uniform.value[ i ];
- textureUnit = uniform._array[ i ];
- if ( ! texture ) continue;
- _this.setTexture( texture, textureUnit );
- }
- break;
- default:
- THREE.warn( 'THREE.WebGLRenderer: Unknown uniform type: ' + type );
- }
- }
- }
- function setupMatrices ( object, camera ) {
- object._modelViewMatrix.multiplyMatrices( camera.matrixWorldInverse, object.matrixWorld );
- object._normalMatrix.getNormalMatrix( object._modelViewMatrix );
- }
- function setColorLinear( array, offset, color, intensity ) {
- array[ offset + 0 ] = color.r * intensity;
- array[ offset + 1 ] = color.g * intensity;
- array[ offset + 2 ] = color.b * intensity;
- }
- function setupLights ( lights ) {
- var l, ll, light,
- r = 0, g = 0, b = 0,
- color, skyColor, groundColor,
- intensity,
- distance,
- zlights = _lights,
- dirColors = zlights.directional.colors,
- dirPositions = zlights.directional.positions,
- pointColors = zlights.point.colors,
- pointPositions = zlights.point.positions,
- pointDistances = zlights.point.distances,
- pointDecays = zlights.point.decays,
- spotColors = zlights.spot.colors,
- spotPositions = zlights.spot.positions,
- spotDistances = zlights.spot.distances,
- spotDirections = zlights.spot.directions,
- spotAnglesCos = zlights.spot.anglesCos,
- spotExponents = zlights.spot.exponents,
- spotDecays = zlights.spot.decays,
- hemiSkyColors = zlights.hemi.skyColors,
- hemiGroundColors = zlights.hemi.groundColors,
- hemiPositions = zlights.hemi.positions,
- dirLength = 0,
- pointLength = 0,
- spotLength = 0,
- hemiLength = 0,
- dirCount = 0,
- pointCount = 0,
- spotCount = 0,
- hemiCount = 0,
- dirOffset = 0,
- pointOffset = 0,
- spotOffset = 0,
- hemiOffset = 0;
- for ( l = 0, ll = lights.length; l < ll; l ++ ) {
- light = lights[ l ];
- if ( light.onlyShadow ) continue;
- color = light.color;
- intensity = light.intensity;
- distance = light.distance;
- if ( light instanceof THREE.AmbientLight ) {
- if ( ! light.visible ) continue;
- r += color.r;
- g += color.g;
- b += color.b;
- } else if ( light instanceof THREE.DirectionalLight ) {
- dirCount += 1;
- if ( ! light.visible ) continue;
- _direction.setFromMatrixPosition( light.matrixWorld );
- _vector3.setFromMatrixPosition( light.target.matrixWorld );
- _direction.sub( _vector3 );
- _direction.normalize();
- dirOffset = dirLength * 3;
- dirPositions[ dirOffset + 0 ] = _direction.x;
- dirPositions[ dirOffset + 1 ] = _direction.y;
- dirPositions[ dirOffset + 2 ] = _direction.z;
- setColorLinear( dirColors, dirOffset, color, intensity );
- dirLength += 1;
- } else if ( light instanceof THREE.PointLight ) {
- pointCount += 1;
- if ( ! light.visible ) continue;
- pointOffset = pointLength * 3;
- setColorLinear( pointColors, pointOffset, color, intensity );
- _vector3.setFromMatrixPosition( light.matrixWorld );
- pointPositions[ pointOffset + 0 ] = _vector3.x;
- pointPositions[ pointOffset + 1 ] = _vector3.y;
- pointPositions[ pointOffset + 2 ] = _vector3.z;
- // distance is 0 if decay is 0, because there is no attenuation at all.
- pointDistances[ pointLength ] = distance;
- pointDecays[ pointLength ] = ( light.distance === 0 ) ? 0.0 : light.decay;
- pointLength += 1;
- } else if ( light instanceof THREE.SpotLight ) {
- spotCount += 1;
- if ( ! light.visible ) continue;
- spotOffset = spotLength * 3;
- setColorLinear( spotColors, spotOffset, color, intensity );
- _direction.setFromMatrixPosition( light.matrixWorld );
- spotPositions[ spotOffset + 0 ] = _direction.x;
- spotPositions[ spotOffset + 1 ] = _direction.y;
- spotPositions[ spotOffset + 2 ] = _direction.z;
- spotDistances[ spotLength ] = distance;
- _vector3.setFromMatrixPosition( light.target.matrixWorld );
- _direction.sub( _vector3 );
- _direction.normalize();
- spotDirections[ spotOffset + 0 ] = _direction.x;
- spotDirections[ spotOffset + 1 ] = _direction.y;
- spotDirections[ spotOffset + 2 ] = _direction.z;
- spotAnglesCos[ spotLength ] = Math.cos( light.angle );
- spotExponents[ spotLength ] = light.exponent;
- spotDecays[ spotLength ] = ( light.distance === 0 ) ? 0.0 : light.decay;
- spotLength += 1;
- } else if ( light instanceof THREE.HemisphereLight ) {
- hemiCount += 1;
- if ( ! light.visible ) continue;
- _direction.setFromMatrixPosition( light.matrixWorld );
- _direction.normalize();
- hemiOffset = hemiLength * 3;
- hemiPositions[ hemiOffset + 0 ] = _direction.x;
- hemiPositions[ hemiOffset + 1 ] = _direction.y;
- hemiPositions[ hemiOffset + 2 ] = _direction.z;
- skyColor = light.color;
- groundColor = light.groundColor;
- setColorLinear( hemiSkyColors, hemiOffset, skyColor, intensity );
- setColorLinear( hemiGroundColors, hemiOffset, groundColor, intensity );
- hemiLength += 1;
- }
- }
- // null eventual remains from removed lights
- // (this is to avoid if in shader)
- for ( l = dirLength * 3, ll = Math.max( dirColors.length, dirCount * 3 ); l < ll; l ++ ) dirColors[ l ] = 0.0;
- for ( l = pointLength * 3, ll = Math.max( pointColors.length, pointCount * 3 ); l < ll; l ++ ) pointColors[ l ] = 0.0;
- for ( l = spotLength * 3, ll = Math.max( spotColors.length, spotCount * 3 ); l < ll; l ++ ) spotColors[ l ] = 0.0;
- for ( l = hemiLength * 3, ll = Math.max( hemiSkyColors.length, hemiCount * 3 ); l < ll; l ++ ) hemiSkyColors[ l ] = 0.0;
- for ( l = hemiLength * 3, ll = Math.max( hemiGroundColors.length, hemiCount * 3 ); l < ll; l ++ ) hemiGroundColors[ l ] = 0.0;
- zlights.directional.length = dirLength;
- zlights.point.length = pointLength;
- zlights.spot.length = spotLength;
- zlights.hemi.length = hemiLength;
- zlights.ambient[ 0 ] = r;
- zlights.ambient[ 1 ] = g;
- zlights.ambient[ 2 ] = b;
- }
- // GL state setting
- this.setFaceCulling = function ( cullFace, frontFaceDirection ) {
- if ( cullFace === THREE.CullFaceNone ) {
- _gl.disable( _gl.CULL_FACE );
- } else {
- if ( frontFaceDirection === THREE.FrontFaceDirectionCW ) {
- _gl.frontFace( _gl.CW );
- } else {
- _gl.frontFace( _gl.CCW );
- }
- if ( cullFace === THREE.CullFaceBack ) {
- _gl.cullFace( _gl.BACK );
- } else if ( cullFace === THREE.CullFaceFront ) {
- _gl.cullFace( _gl.FRONT );
- } else {
- _gl.cullFace( _gl.FRONT_AND_BACK );
- }
- _gl.enable( _gl.CULL_FACE );
- }
- };
- this.setMaterialFaces = function ( material ) {
- state.setDoubleSided( material.side === THREE.DoubleSide );
- state.setFlipSided( material.side === THREE.BackSide );
- };
- // Textures
- function setTextureParameters ( textureType, texture, isImagePowerOfTwo ) {
- var extension;
- if ( isImagePowerOfTwo ) {
- _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_S, paramThreeToGL( texture.wrapS ) );
- _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_T, paramThreeToGL( texture.wrapT ) );
- _gl.texParameteri( textureType, _gl.TEXTURE_MAG_FILTER, paramThreeToGL( texture.magFilter ) );
- _gl.texParameteri( textureType, _gl.TEXTURE_MIN_FILTER, paramThreeToGL( texture.minFilter ) );
- } else {
- _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_S, _gl.CLAMP_TO_EDGE );
- _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_T, _gl.CLAMP_TO_EDGE );
- if ( texture.wrapS !== THREE.ClampToEdgeWrapping || texture.wrapT !== THREE.ClampToEdgeWrapping ) {
- THREE.warn( 'THREE.WebGLRenderer: Texture is not power of two. Texture.wrapS and Texture.wrapT should be set to THREE.ClampToEdgeWrapping. ( ' + texture.sourceFile + ' )' );
- }
- _gl.texParameteri( textureType, _gl.TEXTURE_MAG_FILTER, filterFallback( texture.magFilter ) );
- _gl.texParameteri( textureType, _gl.TEXTURE_MIN_FILTER, filterFallback( texture.minFilter ) );
- if ( texture.minFilter !== THREE.NearestFilter && texture.minFilter !== THREE.LinearFilter ) {
- THREE.warn( 'THREE.WebGLRenderer: Texture is not power of two. Texture.minFilter should be set to THREE.NearestFilter or THREE.LinearFilter. ( ' + texture.sourceFile + ' )' );
- }
- }
- extension = extensions.get( 'EXT_texture_filter_anisotropic' );
- if ( extension && texture.type !== THREE.FloatType && texture.type !== THREE.HalfFloatType ) {
- if ( texture.anisotropy > 1 || texture.__currentAnisotropy ) {
- _gl.texParameterf( textureType, extension.TEXTURE_MAX_ANISOTROPY_EXT, Math.min( texture.anisotropy, _this.getMaxAnisotropy() ) );
- texture.__currentAnisotropy = texture.anisotropy;
- }
- }
- }
- this.uploadTexture = function ( texture, slot ) {
- if ( texture.__webglInit === undefined ) {
- texture.__webglInit = true;
- texture.addEventListener( 'dispose', onTextureDispose );
- texture.__webglTexture = _gl.createTexture();
- _this.info.memory.textures ++;
- }
- _gl.activeTexture( _gl.TEXTURE0 + slot );
- _gl.bindTexture( _gl.TEXTURE_2D, texture.__webglTexture );
- _gl.pixelStorei( _gl.UNPACK_FLIP_Y_WEBGL, texture.flipY );
- _gl.pixelStorei( _gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, texture.premultiplyAlpha );
- _gl.pixelStorei( _gl.UNPACK_ALIGNMENT, texture.unpackAlignment );
- texture.image = clampToMaxSize( texture.image, _maxTextureSize );
- var image = texture.image,
- isImagePowerOfTwo = THREE.Math.isPowerOfTwo( image.width ) && THREE.Math.isPowerOfTwo( image.height ),
- glFormat = paramThreeToGL( texture.format ),
- glType = paramThreeToGL( texture.type );
- setTextureParameters( _gl.TEXTURE_2D, texture, isImagePowerOfTwo );
- var mipmap, mipmaps = texture.mipmaps;
- if ( texture instanceof THREE.DataTexture ) {
- // use manually created mipmaps if available
- // if there are no manual mipmaps
- // set 0 level mipmap and then use GL to generate other mipmap levels
- if ( mipmaps.length > 0 && isImagePowerOfTwo ) {
- for ( var i = 0, il = mipmaps.length; i < il; i ++ ) {
- mipmap = mipmaps[ i ];
- _gl.texImage2D( _gl.TEXTURE_2D, i, glFormat, mipmap.width, mipmap.height, 0, glFormat, glType, mipmap.data );
- }
- texture.generateMipmaps = false;
- } else {
- _gl.texImage2D( _gl.TEXTURE_2D, 0, glFormat, image.width, image.height, 0, glFormat, glType, image.data );
- }
- } else if ( texture instanceof THREE.CompressedTexture ) {
- for ( var i = 0, il = mipmaps.length; i < il; i ++ ) {
- mipmap = mipmaps[ i ];
- if ( texture.format !== THREE.RGBAFormat && texture.format !== THREE.RGBFormat ) {
- if ( getCompressedTextureFormats().indexOf( glFormat ) > -1 ) {
- _gl.compressedTexImage2D( _gl.TEXTURE_2D, i, glFormat, mipmap.width, mipmap.height, 0, mipmap.data );
- } else {
- THREE.warn( "THREE.WebGLRenderer: Attempt to load unsupported compressed texture format in .uploadTexture()" );
- }
- } else {
- _gl.texImage2D( _gl.TEXTURE_2D, i, glFormat, mipmap.width, mipmap.height, 0, glFormat, glType, mipmap.data );
- }
- }
- } else { // regular Texture (image, video, canvas)
- // use manually created mipmaps if available
- // if there are no manual mipmaps
- // set 0 level mipmap and then use GL to generate other mipmap levels
- if ( mipmaps.length > 0 && isImagePowerOfTwo ) {
- for ( var i = 0, il = mipmaps.length; i < il; i ++ ) {
- mipmap = mipmaps[ i ];
- _gl.texImage2D( _gl.TEXTURE_2D, i, glFormat, glFormat, glType, mipmap );
- }
- texture.generateMipmaps = false;
- } else {
- _gl.texImage2D( _gl.TEXTURE_2D, 0, glFormat, glFormat, glType, texture.image );
- }
- }
- if ( texture.generateMipmaps && isImagePowerOfTwo ) _gl.generateMipmap( _gl.TEXTURE_2D );
- texture.needsUpdate = false;
- if ( texture.onUpdate ) texture.onUpdate( texture );
- };
- this.setTexture = function ( texture, slot ) {
- if ( texture.needsUpdate === true ) {
- var image = texture.image;
- if ( image.complete === false ) {
- THREE.warn( 'THREE.WebGLRenderer: Texture marked for update but image is incomplete', texture );
- return;
- }
- _this.uploadTexture( texture, slot );
- return;
- }
- _gl.activeTexture( _gl.TEXTURE0 + slot );
- _gl.bindTexture( _gl.TEXTURE_2D, texture.__webglTexture );
- };
- function clampToMaxSize ( image, maxSize ) {
- if ( image.width > maxSize || image.height > maxSize ) {
- // Warning: Scaling through the canvas will only work with images that use
- // premultiplied alpha.
- var scale = maxSize / Math.max( image.width, image.height );
- var canvas = document.createElement( 'canvas' );
- canvas.width = Math.floor( image.width * scale );
- canvas.height = Math.floor( image.height * scale );
- var context = canvas.getContext( '2d' );
- context.drawImage( image, 0, 0, image.width, image.height, 0, 0, canvas.width, canvas.height );
- THREE.warn( 'THREE.WebGLRenderer: image is too big (' + image.width + 'x' + image.height + '). Resized to ' + canvas.width + 'x' + canvas.height, image );
- return canvas;
- }
- return image;
- }
- function setCubeTexture ( texture, slot ) {
- if ( texture.image.length === 6 ) {
- if ( texture.needsUpdate ) {
- if ( ! texture.image.__webglTextureCube ) {
- texture.addEventListener( 'dispose', onTextureDispose );
- texture.image.__webglTextureCube = _gl.createTexture();
- _this.info.memory.textures ++;
- }
- _gl.activeTexture( _gl.TEXTURE0 + slot );
- _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, texture.image.__webglTextureCube );
- _gl.pixelStorei( _gl.UNPACK_FLIP_Y_WEBGL, texture.flipY );
- var isCompressed = texture instanceof THREE.CompressedTexture;
- var isDataTexture = texture.image[ 0 ] instanceof THREE.DataTexture;
- var cubeImage = [];
- for ( var i = 0; i < 6; i ++ ) {
- if ( _this.autoScaleCubemaps && ! isCompressed && ! isDataTexture ) {
- cubeImage[ i ] = clampToMaxSize( texture.image[ i ], _maxCubemapSize );
- } else {
- cubeImage[ i ] = isDataTexture ? texture.image[ i ].image : texture.image[ i ];
- }
- }
- var image = cubeImage[ 0 ],
- isImagePowerOfTwo = THREE.Math.isPowerOfTwo( image.width ) && THREE.Math.isPowerOfTwo( image.height ),
- glFormat = paramThreeToGL( texture.format ),
- glType = paramThreeToGL( texture.type );
- setTextureParameters( _gl.TEXTURE_CUBE_MAP, texture, isImagePowerOfTwo );
- for ( var i = 0; i < 6; i ++ ) {
- if ( ! isCompressed ) {
- if ( isDataTexture ) {
- _gl.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, glFormat, cubeImage[ i ].width, cubeImage[ i ].height, 0, glFormat, glType, cubeImage[ i ].data );
- } else {
- _gl.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, glFormat, glFormat, glType, cubeImage[ i ] );
- }
- } else {
- var mipmap, mipmaps = cubeImage[ i ].mipmaps;
- for ( var j = 0, jl = mipmaps.length; j < jl; j ++ ) {
- mipmap = mipmaps[ j ];
- if ( texture.format !== THREE.RGBAFormat && texture.format !== THREE.RGBFormat ) {
- if ( getCompressedTextureFormats().indexOf( glFormat ) > -1 ) {
- _gl.compressedTexImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, j, glFormat, mipmap.width, mipmap.height, 0, mipmap.data );
- } else {
- THREE.warn( "THREE.WebGLRenderer: Attempt to load unsupported compressed texture format in .setCubeTexture()" );
- }
- } else {
- _gl.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, j, glFormat, mipmap.width, mipmap.height, 0, glFormat, glType, mipmap.data );
- }
- }
- }
- }
- if ( texture.generateMipmaps && isImagePowerOfTwo ) {
- _gl.generateMipmap( _gl.TEXTURE_CUBE_MAP );
- }
- texture.needsUpdate = false;
- if ( texture.onUpdate ) texture.onUpdate( texture );
- } else {
- _gl.activeTexture( _gl.TEXTURE0 + slot );
- _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, texture.image.__webglTextureCube );
- }
- }
- }
- function setCubeTextureDynamic ( texture, slot ) {
- _gl.activeTexture( _gl.TEXTURE0 + slot );
- _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, texture.__webglTexture );
- }
- // Render targets
- function setupFrameBuffer ( framebuffer, renderTarget, textureTarget ) {
- _gl.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
- _gl.framebufferTexture2D( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, textureTarget, renderTarget.__webglTexture, 0 );
- }
- function setupRenderBuffer ( renderbuffer, renderTarget ) {
- _gl.bindRenderbuffer( _gl.RENDERBUFFER, renderbuffer );
- if ( renderTarget.depthBuffer && ! renderTarget.stencilBuffer ) {
- _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.DEPTH_COMPONENT16, renderTarget.width, renderTarget.height );
- _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer );
- /* For some reason this is not working. Defaulting to RGBA4.
- } else if ( ! renderTarget.depthBuffer && renderTarget.stencilBuffer ) {
- _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.STENCIL_INDEX8, renderTarget.width, renderTarget.height );
- _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.STENCIL_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer );
- */
- } else if ( renderTarget.depthBuffer && renderTarget.stencilBuffer ) {
- _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.DEPTH_STENCIL, renderTarget.width, renderTarget.height );
- _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_STENCIL_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer );
- } else {
- _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.RGBA4, renderTarget.width, renderTarget.height );
- }
- }
- this.setRenderTarget = function ( renderTarget ) {
- var isCube = ( renderTarget instanceof THREE.WebGLRenderTargetCube );
- if ( renderTarget && renderTarget.__webglFramebuffer === undefined ) {
- if ( renderTarget.depthBuffer === undefined ) renderTarget.depthBuffer = true;
- if ( renderTarget.stencilBuffer === undefined ) renderTarget.stencilBuffer = true;
- renderTarget.addEventListener( 'dispose', onRenderTargetDispose );
- renderTarget.__webglTexture = _gl.createTexture();
- _this.info.memory.textures ++;
- // Setup texture, create render and frame buffers
- var isTargetPowerOfTwo = THREE.Math.isPowerOfTwo( renderTarget.width ) && THREE.Math.isPowerOfTwo( renderTarget.height ),
- glFormat = paramThreeToGL( renderTarget.format ),
- glType = paramThreeToGL( renderTarget.type );
- if ( isCube ) {
- renderTarget.__webglFramebuffer = [];
- renderTarget.__webglRenderbuffer = [];
- _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, renderTarget.__webglTexture );
- setTextureParameters( _gl.TEXTURE_CUBE_MAP, renderTarget, isTargetPowerOfTwo );
- for ( var i = 0; i < 6; i ++ ) {
- renderTarget.__webglFramebuffer[ i ] = _gl.createFramebuffer();
- renderTarget.__webglRenderbuffer[ i ] = _gl.createRenderbuffer();
- _gl.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, glFormat, renderTarget.width, renderTarget.height, 0, glFormat, glType, null );
- setupFrameBuffer( renderTarget.__webglFramebuffer[ i ], renderTarget, _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i );
- setupRenderBuffer( renderTarget.__webglRenderbuffer[ i ], renderTarget );
- }
- if ( renderTarget.generateMipmaps && isTargetPowerOfTwo ) _gl.generateMipmap( _gl.TEXTURE_CUBE_MAP );
- } else {
- renderTarget.__webglFramebuffer = _gl.createFramebuffer();
- if ( renderTarget.shareDepthFrom ) {
- renderTarget.__webglRenderbuffer = renderTarget.shareDepthFrom.__webglRenderbuffer;
- } else {
- renderTarget.__webglRenderbuffer = _gl.createRenderbuffer();
- }
- _gl.bindTexture( _gl.TEXTURE_2D, renderTarget.__webglTexture );
- setTextureParameters( _gl.TEXTURE_2D, renderTarget, isTargetPowerOfTwo );
- _gl.texImage2D( _gl.TEXTURE_2D, 0, glFormat, renderTarget.width, renderTarget.height, 0, glFormat, glType, null );
- setupFrameBuffer( renderTarget.__webglFramebuffer, renderTarget, _gl.TEXTURE_2D );
- if ( renderTarget.shareDepthFrom ) {
- if ( renderTarget.depthBuffer && ! renderTarget.stencilBuffer ) {
- _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_ATTACHMENT, _gl.RENDERBUFFER, renderTarget.__webglRenderbuffer );
- } else if ( renderTarget.depthBuffer && renderTarget.stencilBuffer ) {
- _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_STENCIL_ATTACHMENT, _gl.RENDERBUFFER, renderTarget.__webglRenderbuffer );
- }
- } else {
- setupRenderBuffer( renderTarget.__webglRenderbuffer, renderTarget );
- }
- if ( renderTarget.generateMipmaps && isTargetPowerOfTwo ) _gl.generateMipmap( _gl.TEXTURE_2D );
- }
- // Release everything
- if ( isCube ) {
- _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, null );
- } else {
- _gl.bindTexture( _gl.TEXTURE_2D, null );
- }
- _gl.bindRenderbuffer( _gl.RENDERBUFFER, null );
- _gl.bindFramebuffer( _gl.FRAMEBUFFER, null );
- }
- var framebuffer, width, height, vx, vy;
- if ( renderTarget ) {
- if ( isCube ) {
- framebuffer = renderTarget.__webglFramebuffer[ renderTarget.activeCubeFace ];
- } else {
- framebuffer = renderTarget.__webglFramebuffer;
- }
- width = renderTarget.width;
- height = renderTarget.height;
- vx = 0;
- vy = 0;
- } else {
- framebuffer = null;
- width = _viewportWidth;
- height = _viewportHeight;
- vx = _viewportX;
- vy = _viewportY;
- }
- if ( framebuffer !== _currentFramebuffer ) {
- _gl.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
- _gl.viewport( vx, vy, width, height );
- _currentFramebuffer = framebuffer;
- }
- _currentWidth = width;
- _currentHeight = height;
- };
- this.readRenderTargetPixels = function( renderTarget, x, y, width, height, buffer ) {
- if ( ! ( renderTarget instanceof THREE.WebGLRenderTarget ) ) {
- THREE.error( 'THREE.WebGLRenderer.readRenderTargetPixels: renderTarget is not THREE.WebGLRenderTarget.' );
- return;
- }
- if ( renderTarget.__webglFramebuffer ) {
- if ( renderTarget.format !== THREE.RGBAFormat ) {
- THREE.error( 'THREE.WebGLRenderer.readRenderTargetPixels: renderTarget is not in RGBA format. readPixels can read only RGBA format.' );
- return;
- }
- var restore = false;
- if ( renderTarget.__webglFramebuffer !== _currentFramebuffer ) {
- _gl.bindFramebuffer( _gl.FRAMEBUFFER, renderTarget.__webglFramebuffer );
- restore = true;
- }
- if ( _gl.checkFramebufferStatus( _gl.FRAMEBUFFER ) === _gl.FRAMEBUFFER_COMPLETE ) {
- _gl.readPixels( x, y, width, height, _gl.RGBA, _gl.UNSIGNED_BYTE, buffer );
- } else {
- THREE.error( 'THREE.WebGLRenderer.readRenderTargetPixels: readPixels from renderTarget failed. Framebuffer not complete.' );
- }
- if ( restore ) {
- _gl.bindFramebuffer( _gl.FRAMEBUFFER, _currentFramebuffer );
- }
- }
- };
- function updateRenderTargetMipmap ( renderTarget ) {
- if ( renderTarget instanceof THREE.WebGLRenderTargetCube ) {
- _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, renderTarget.__webglTexture );
- _gl.generateMipmap( _gl.TEXTURE_CUBE_MAP );
- _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, null );
- } else {
- _gl.bindTexture( _gl.TEXTURE_2D, renderTarget.__webglTexture );
- _gl.generateMipmap( _gl.TEXTURE_2D );
- _gl.bindTexture( _gl.TEXTURE_2D, null );
- }
- }
- // Fallback filters for non-power-of-2 textures
- function filterFallback ( f ) {
- if ( f === THREE.NearestFilter || f === THREE.NearestMipMapNearestFilter || f === THREE.NearestMipMapLinearFilter ) {
- return _gl.NEAREST;
- }
- return _gl.LINEAR;
- }
- // Map three.js constants to WebGL constants
- function paramThreeToGL ( p ) {
- var extension;
- if ( p === THREE.RepeatWrapping ) return _gl.REPEAT;
- if ( p === THREE.ClampToEdgeWrapping ) return _gl.CLAMP_TO_EDGE;
- if ( p === THREE.MirroredRepeatWrapping ) return _gl.MIRRORED_REPEAT;
- if ( p === THREE.NearestFilter ) return _gl.NEAREST;
- if ( p === THREE.NearestMipMapNearestFilter ) return _gl.NEAREST_MIPMAP_NEAREST;
- if ( p === THREE.NearestMipMapLinearFilter ) return _gl.NEAREST_MIPMAP_LINEAR;
- if ( p === THREE.LinearFilter ) return _gl.LINEAR;
- if ( p === THREE.LinearMipMapNearestFilter ) return _gl.LINEAR_MIPMAP_NEAREST;
- if ( p === THREE.LinearMipMapLinearFilter ) return _gl.LINEAR_MIPMAP_LINEAR;
- if ( p === THREE.UnsignedByteType ) return _gl.UNSIGNED_BYTE;
- if ( p === THREE.UnsignedShort4444Type ) return _gl.UNSIGNED_SHORT_4_4_4_4;
- if ( p === THREE.UnsignedShort5551Type ) return _gl.UNSIGNED_SHORT_5_5_5_1;
- if ( p === THREE.UnsignedShort565Type ) return _gl.UNSIGNED_SHORT_5_6_5;
- if ( p === THREE.ByteType ) return _gl.BYTE;
- if ( p === THREE.ShortType ) return _gl.SHORT;
- if ( p === THREE.UnsignedShortType ) return _gl.UNSIGNED_SHORT;
- if ( p === THREE.IntType ) return _gl.INT;
- if ( p === THREE.UnsignedIntType ) return _gl.UNSIGNED_INT;
- if ( p === THREE.FloatType ) return _gl.FLOAT;
- extension = extensions.get( 'OES_texture_half_float' );
- if ( extension !== null ) {
- if ( p === THREE.HalfFloatType ) return extension.HALF_FLOAT_OES;
- }
- if ( p === THREE.AlphaFormat ) return _gl.ALPHA;
- if ( p === THREE.RGBFormat ) return _gl.RGB;
- if ( p === THREE.RGBAFormat ) return _gl.RGBA;
- if ( p === THREE.LuminanceFormat ) return _gl.LUMINANCE;
- if ( p === THREE.LuminanceAlphaFormat ) return _gl.LUMINANCE_ALPHA;
- if ( p === THREE.AddEquation ) return _gl.FUNC_ADD;
- if ( p === THREE.SubtractEquation ) return _gl.FUNC_SUBTRACT;
- if ( p === THREE.ReverseSubtractEquation ) return _gl.FUNC_REVERSE_SUBTRACT;
- if ( p === THREE.ZeroFactor ) return _gl.ZERO;
- if ( p === THREE.OneFactor ) return _gl.ONE;
- if ( p === THREE.SrcColorFactor ) return _gl.SRC_COLOR;
- if ( p === THREE.OneMinusSrcColorFactor ) return _gl.ONE_MINUS_SRC_COLOR;
- if ( p === THREE.SrcAlphaFactor ) return _gl.SRC_ALPHA;
- if ( p === THREE.OneMinusSrcAlphaFactor ) return _gl.ONE_MINUS_SRC_ALPHA;
- if ( p === THREE.DstAlphaFactor ) return _gl.DST_ALPHA;
- if ( p === THREE.OneMinusDstAlphaFactor ) return _gl.ONE_MINUS_DST_ALPHA;
- if ( p === THREE.DstColorFactor ) return _gl.DST_COLOR;
- if ( p === THREE.OneMinusDstColorFactor ) return _gl.ONE_MINUS_DST_COLOR;
- if ( p === THREE.SrcAlphaSaturateFactor ) return _gl.SRC_ALPHA_SATURATE;
- extension = extensions.get( 'WEBGL_compressed_texture_s3tc' );
- if ( extension !== null ) {
- if ( p === THREE.RGB_S3TC_DXT1_Format ) return extension.COMPRESSED_RGB_S3TC_DXT1_EXT;
- if ( p === THREE.RGBA_S3TC_DXT1_Format ) return extension.COMPRESSED_RGBA_S3TC_DXT1_EXT;
- if ( p === THREE.RGBA_S3TC_DXT3_Format ) return extension.COMPRESSED_RGBA_S3TC_DXT3_EXT;
- if ( p === THREE.RGBA_S3TC_DXT5_Format ) return extension.COMPRESSED_RGBA_S3TC_DXT5_EXT;
- }
- extension = extensions.get( 'WEBGL_compressed_texture_pvrtc' );
- if ( extension !== null ) {
- if ( p === THREE.RGB_PVRTC_4BPPV1_Format ) return extension.COMPRESSED_RGB_PVRTC_4BPPV1_IMG;
- if ( p === THREE.RGB_PVRTC_2BPPV1_Format ) return extension.COMPRESSED_RGB_PVRTC_2BPPV1_IMG;
- if ( p === THREE.RGBA_PVRTC_4BPPV1_Format ) return extension.COMPRESSED_RGBA_PVRTC_4BPPV1_IMG;
- if ( p === THREE.RGBA_PVRTC_2BPPV1_Format ) return extension.COMPRESSED_RGBA_PVRTC_2BPPV1_IMG;
- }
- extension = extensions.get( 'EXT_blend_minmax' );
- if ( extension !== null ) {
- if ( p === THREE.MinEquation ) return extension.MIN_EXT;
- if ( p === THREE.MaxEquation ) return extension.MAX_EXT;
- }
- return 0;
- }
- // Allocations
- function allocateBones ( object ) {
- if ( _supportsBoneTextures && object && object.skeleton && object.skeleton.useVertexTexture ) {
- return 1024;
- } else {
- // default for when object is not specified
- // ( for example when prebuilding shader to be used with multiple objects )
- //
- // - leave some extra space for other uniforms
- // - limit here is ANGLE's 254 max uniform vectors
- // (up to 54 should be safe)
- var nVertexUniforms = _gl.getParameter( _gl.MAX_VERTEX_UNIFORM_VECTORS );
- var nVertexMatrices = Math.floor( ( nVertexUniforms - 20 ) / 4 );
- var maxBones = nVertexMatrices;
- if ( object !== undefined && object instanceof THREE.SkinnedMesh ) {
- maxBones = Math.min( object.skeleton.bones.length, maxBones );
- if ( maxBones < object.skeleton.bones.length ) {
- THREE.warn( 'WebGLRenderer: too many bones - ' + object.skeleton.bones.length + ', this GPU supports just ' + maxBones + ' (try OpenGL instead of ANGLE)' );
- }
- }
- return maxBones;
- }
- }
- function allocateLights( lights ) {
- var dirLights = 0;
- var pointLights = 0;
- var spotLights = 0;
- var hemiLights = 0;
- for ( var l = 0, ll = lights.length; l < ll; l ++ ) {
- var light = lights[ l ];
- if ( light.onlyShadow || light.visible === false ) continue;
- if ( light instanceof THREE.DirectionalLight ) dirLights ++;
- if ( light instanceof THREE.PointLight ) pointLights ++;
- if ( light instanceof THREE.SpotLight ) spotLights ++;
- if ( light instanceof THREE.HemisphereLight ) hemiLights ++;
- }
- return { 'directional': dirLights, 'point': pointLights, 'spot': spotLights, 'hemi': hemiLights };
- }
- function allocateShadows( lights ) {
- var maxShadows = 0;
- for ( var l = 0, ll = lights.length; l < ll; l ++ ) {
- var light = lights[ l ];
- if ( ! light.castShadow ) continue;
- if ( light instanceof THREE.SpotLight ) maxShadows ++;
- if ( light instanceof THREE.DirectionalLight && ! light.shadowCascade ) maxShadows ++;
- }
- return maxShadows;
- }
- // DEPRECATED
- this.initMaterial = function () {
- THREE.warn( 'THREE.WebGLRenderer: .initMaterial() has been removed.' );
- };
- this.addPrePlugin = function () {
- THREE.warn( 'THREE.WebGLRenderer: .addPrePlugin() has been removed.' );
- };
- this.addPostPlugin = function () {
- THREE.warn( 'THREE.WebGLRenderer: .addPostPlugin() has been removed.' );
- };
- this.updateShadowMap = function () {
- THREE.warn( 'THREE.WebGLRenderer: .updateShadowMap() has been removed.' );
- };
- Object.defineProperties( this, {
- shadowMapEnabled: {
- get: function () {
- return shadowMap.enabled;
- },
- set: function ( value ) {
- THREE.warn( 'THREE.WebGLRenderer: .shadowMapEnabled is now .shadowMap.enabled.' );
- shadowMap.enabled = value;
- }
- },
- shadowMapType: {
- get: function () {
- return shadowMap.type;
- },
- set: function ( value ) {
- THREE.warn( 'THREE.WebGLRenderer: .shadowMapType is now .shadowMap.type.' );
- shadowMap.type = value;
- }
- },
- shadowMapCullFace: {
- get: function () {
- return shadowMap.cullFace;
- },
- set: function ( value ) {
- THREE.warn( 'THREE.WebGLRenderer: .shadowMapCullFace is now .shadowMap.cullFace.' );
- shadowMap.cullFace = value;
- }
- },
- shadowMapDebug: {
- get: function () {
- return shadowMap.debug;
- },
- set: function ( value ) {
- THREE.warn( 'THREE.WebGLRenderer: .shadowMapDebug is now .shadowMap.debug.' );
- shadowMap.debug = value;
- }
- },
- shadowMapCascade: {
- get: function () {
- return shadowMap.cascade;
- },
- set: function ( value ) {
- THREE.warn( 'THREE.WebGLRenderer: .shadowMapCascade is now .shadowMap.cascade.' );
- shadowMap.cascade = value;
- }
- }
- } );
- };
|