123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360 |
- /**
- * @author supereggbert / http://www.paulbrunt.co.uk/
- * @author mrdoob / http://mrdoob.com/
- * @author alteredq / http://alteredqualia.com/
- * @author szimek / https://github.com/szimek/
- * @author gero3 / https://github.com/gero3/
- */
- THREE.WebGLRenderer = THREE.WebGLRenderer2 = function ( parameters ) {
- console.log( 'THREE.WebGLRenderer', THREE.REVISION );
- parameters = parameters || {};
- var info = {
- memory: {
- programs: 0,
- geometries: 0,
- textures: 0
- },
- render: {
- calls: 0,
- vertices: 0,
- faces: 0,
- points: 0
- }
- };
- var renderer = new THREE.WebGLRenderer2.LowLevelRenderer(parameters);
- var meshRenderer = new THREE.WebGLRenderer2.MeshRenderer(renderer, info);
- var particleRenderer = new THREE.WebGLRenderer2.ParticleRenderer(renderer, info);
- var lineRenderer = new THREE.WebGLRenderer2.LineRenderer(renderer, info);
- var ribbonRenderer = new THREE.WebGLRenderer2.RibbonRenderer(renderer, info);
-
- var shaderBuilder = new THREE.WebGLRenderer2.ShaderBuilder(renderer, info);
-
- // clearing
- this.autoClear = true;
- this.autoClearColor = true;
- this.autoClearDepth = true;
- this.autoClearStencil = true;
- // scene graph
- this.sortObjects = true;
- this.autoUpdateObjects = true;
- this.autoUpdateScene = true;
- // physically based shading
- this.gammaInput = false;
- this.gammaOutput = false;
- this.physicallyBasedShading = false;
- // shadow map
- this.shadowMapEnabled = false;
- this.shadowMapAutoUpdate = true;
- this.shadowMapType = THREE.PCFShadowMap;
- this.shadowMapCullFace = THREE.CullFaceFront;
- this.shadowMapDebug = false;
- this.shadowMapCascade = false;
- // morphs
- this.maxMorphTargets = 8;
- this.maxMorphNormals = 4;
- // flags
- this.autoScaleCubemaps = true;
- // custom render plugins
- this.renderPluginsPre = [];
- this.renderPluginsPost = [];
- // info
- this.info = info;
- // internal properties
- var _this = this,
- // internal state cache
- _currentProgram = null,
- _currentFramebuffer = null,
- _currentMaterialId = -1,
- _currentGeometryGroupHash = null,
- _currentCamera = null,
- _geometryGroupCounter = 0,
- _usedTextureUnits = 0,
- // GL state
- _viewportX = 0,
- _viewportY = 0,
- _viewportWidth = 0,
- _viewportHeight = 0,
- _currentWidth = 0,
- _currentHeight = 0,
- _enabledAttributes = {},
- // frustum
- _frustum = new THREE.Frustum(),
- // camera matrices cache
- _projScreenMatrix = new THREE.Matrix4(),
- _projScreenMatrixPS = 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: [] },
- spot: { length: 0, colors: [], positions: [], distances: [], directions: [], anglesCos: [], exponents: [] },
- hemi: { length: 0, skyColors: [], groundColors: [], positions: [] }
- };
- // initialize
- this.context = renderer.getContext();
- this.domElement = renderer.getDomElement();
- this.getPrecision = renderer.getPrecision;
- // low level API
- this.getPrecision = renderer.getPrecision;
- this.getContext = renderer.getContext;
- this.supportsVertexTextures = renderer.supportsVertexTextures;
- this.supportsFloatTextures = renderer.supportsFloatTextures;
- this.supportsStandardDerivatives = renderer.supportsStandardDerivatives;
- this.supportsCompressedTextureS3TC = renderer.supportsCompressedTextureS3TC;
- this.getMaxAnisotropy = renderer.getMaxAnisotropy;
- this.setSize = renderer.setSize;
- this.setViewport = renderer.setViewport;
- this.setScissor = renderer.setScissor;
- this.enableScissorTest = renderer.enableScissorTest;
- this.setDepthWrite = renderer.setDepthWrite;
- this.setDepthTest = renderer.setDepthTest;
- this.setRenderTarget = renderer.setRenderTarget;
- this.setBlending = renderer.setBlending;
- this.setTexture = renderer.setTexture;
- this.setMaterialFaces = renderer.setMaterialFaces;
- this.setFaceCulling = renderer.setFaceCulling;
-
- // Clearing
- this.setClearColorHex = renderer.setClearColorHex;
- this.setClearColor = renderer.setClearColor;
- this.getClearColor = renderer.getClearColor;
- this.getClearAlpha = renderer.getClearAlpha;
- this.clear = renderer.clear;
- this.clearTarget = renderer.clearTarget;
- // Plugins
- this.addPostPlugin = function ( plugin ) {
- plugin.init( this );
- this.renderPluginsPost.push( plugin );
- };
- this.addPrePlugin = function ( plugin ) {
- plugin.init( this );
- this.renderPluginsPre.push( plugin );
- };
- // Rendering
- this.updateShadowMap = function ( scene, camera ) {
- _currentProgram = null;
- _currentGeometryGroupHash = -1;
- _currentMaterialId = -1;
- _lightsNeedUpdate = true;
-
- renderer.resetState();
- this.shadowMapPlugin.update( scene, camera );
- };
- // Events
- var onGeometryDispose = function ( event ) {
- var geometry = event.target;
- geometry.removeEventListener( 'dispose', onGeometryDispose );
- deallocateGeometry( geometry );
- _this.info.memory.geometries --;
- };
- 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 deallocateGeometry = function ( geometry ) {
- var m,ml;
- geometry.__webglInit = undefined;
- if ( geometry.__webglVertexBuffer !== undefined ) renderer.deleteBuffer( geometry.__webglVertexBuffer );
- if ( geometry.__webglNormalBuffer !== undefined ) renderer.deleteBuffer( geometry.__webglNormalBuffer );
- if ( geometry.__webglTangentBuffer !== undefined ) renderer.deleteBuffer( geometry.__webglTangentBuffer );
- if ( geometry.__webglColorBuffer !== undefined ) renderer.deleteBuffer( geometry.__webglColorBuffer );
- if ( geometry.__webglUVBuffer !== undefined ) renderer.deleteBuffer( geometry.__webglUVBuffer );
- if ( geometry.__webglUV2Buffer !== undefined ) renderer.deleteBuffer( geometry.__webglUV2Buffer );
- if ( geometry.__webglSkinIndicesBuffer !== undefined ) renderer.deleteBuffer( geometry.__webglSkinIndicesBuffer );
- if ( geometry.__webglSkinWeightsBuffer !== undefined ) renderer.deleteBuffer( geometry.__webglSkinWeightsBuffer );
- if ( geometry.__webglFaceBuffer !== undefined ) renderer.deleteBuffer( geometry.__webglFaceBuffer );
- if ( geometry.__webglLineBuffer !== undefined ) renderer.deleteBuffer( geometry.__webglLineBuffer );
- if ( geometry.__webglLineDistanceBuffer !== undefined ) renderer.deleteBuffer( geometry.__webglLineDistanceBuffer );
- // geometry groups
- if ( geometry.geometryGroups !== undefined ) {
- for ( var g in geometry.geometryGroups ) {
- var geometryGroup = geometry.geometryGroups[ g ];
- if ( geometryGroup.numMorphTargets !== undefined ) {
- for ( m = 0, ml = geometryGroup.numMorphTargets; m < ml; m ++ ) {
- renderer.deleteBuffer( geometryGroup.__webglMorphTargetsBuffers[ m ] );
- }
- }
- if ( geometryGroup.numMorphNormals !== undefined ) {
- for ( m = 0, ml = geometryGroup.numMorphNormals; m < ml; m ++ ) {
- renderer.deleteBuffer( geometryGroup.__webglMorphNormalsBuffers[ m ] );
- }
- }
- deleteCustomAttributesBuffers( geometryGroup );
- }
- }
- deleteCustomAttributesBuffers( geometry );
- };
- var deallocateTexture = function ( texture ) {
- if ( texture.image && texture.image.__webglTextureCube ) {
- // cube texture
- renderer.deleteTexture( texture.image.__webglTextureCube );
- } else {
- // 2D texture
- if ( ! texture.__webglInit ) return;
- texture.__webglInit = false;
- renderer.deleteTexture( texture.__webglTexture );
- }
- };
- var deallocateRenderTarget = function ( renderTarget ) {
- if ( !renderTarget || ! renderTarget.__webglTexture ) return;
- renderer.deleteTexture( renderTarget.__webglTexture );
- if ( renderTarget instanceof THREE.WebGLRenderTargetCube ) {
- for ( var i = 0; i < 6; i ++ ) {
- renderer.deleteFramebuffer( renderTarget.__webglFramebuffer[ i ] );
- renderer.deleteRenderbuffer( renderTarget.__webglRenderbuffer[ i ] );
- }
- } else {
- renderer.deleteFramebuffer( renderTarget.__webglFramebuffer );
- renderer.deleteRenderbuffer( renderTarget.__webglRenderbuffer );
- }
- };
- var deallocateMaterial = function ( material ) {
- var program = material.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)
-
- shaderBuilder.removeProgram(program)
- };
- function deleteCustomAttributesBuffers( geometry ) {
- if ( geometry.__webglCustomAttributesList ) {
- for ( var id in geometry.__webglCustomAttributesList ) {
- renderer.deleteBuffer( geometry.__webglCustomAttributesList[ id ].buffer );
- }
- }
- };
- // Buffer initialization
- function initCustomAttributes ( geometry, object ) {
- var nvertices = geometry.vertices.length;
- var material = object.material;
- if ( material.attributes ) {
- if ( geometry.__webglCustomAttributesList === undefined ) {
- geometry.__webglCustomAttributesList = [];
- }
- for ( var a in material.attributes ) {
- var attribute = material.attributes[ a ];
- if ( !attribute.__webglInitialized || attribute.createUniqueBuffers ) {
- attribute.__webglInitialized = true;
- var size = 1; // "f" and "i"
- if ( attribute.type === "v2" ) size = 2;
- else if ( attribute.type === "v3" ) size = 3;
- else if ( attribute.type === "v4" ) size = 4;
- else if ( attribute.type === "c" ) size = 3;
- attribute.size = size;
- attribute.array = new Float32Array( nvertices * size );
- attribute.buffer = renderer.createBuffer();
- attribute.buffer.belongsToAttribute = a;
- attribute.needsUpdate = true;
- }
- geometry.__webglCustomAttributesList.push( attribute );
- }
- }
- };
-
- function getBufferMaterial( object, geometryGroup ) {
- return object.material instanceof THREE.MeshFaceMaterial
- ? object.material.materials[ geometryGroup.materialIndex ]
- : object.material;
- };
- //
- function initDirectBuffers( geometry ) {
- var a, attribute;
- for ( a in geometry.attributes ) {
- attribute = geometry.attributes[ a ];
- attribute.buffer = renderer.createBuffer();
-
- if ( a === "index" ) {
- renderer.setStaticIndexBuffer(attribute.buffer,attribute.array);
- } else {
- renderer.setStaticArrayBuffer(attribute.buffer,attribute.array);
- }
- }
- };
- // Buffer setting
- function setDirectBuffers ( geometry, dispose ) {
- var attributes = geometry.attributes;
- var index = attributes[ "index" ];
- var position = attributes[ "position" ];
- var normal = attributes[ "normal" ];
- var uv = attributes[ "uv" ];
- var color = attributes[ "color" ];
- var tangent = attributes[ "tangent" ];
- if ( geometry.elementsNeedUpdate && index !== undefined ) {
-
- renderer.setDynamicIndexBuffer( index.buffer, index.array);
- }
- if ( geometry.verticesNeedUpdate && position !== undefined ) {
- renderer.setDynamicArrayBuffer( position.buffer, position.array);
- }
- if ( geometry.normalsNeedUpdate && normal !== undefined ) {
- renderer.setDynamicArrayBuffer( normal.buffer, normal.array);
- }
- if ( geometry.uvsNeedUpdate && uv !== undefined ) {
- renderer.setDynamicArrayBuffer( uv.buffer, uv.array);
- }
- if ( geometry.colorsNeedUpdate && color !== undefined ) {
- renderer.setDynamicArrayBuffer( color.buffer, color.array);
- }
- if ( geometry.tangentsNeedUpdate && tangent !== undefined ) {
- renderer.setDynamicArrayBuffer( tangent.buffer, tangent.array);
- }
- if ( dispose ) {
- for ( var i in geometry.attributes ) {
- delete geometry.attributes[ i ].array;
- }
- }
- };
- // Buffer rendering
- this.renderBufferImmediate = function ( object, program, material ) {
- if ( object.hasPositions && ! object.__webglVertexBuffer ) object.__webglVertexBuffer = renderer.createBuffer();
- if ( object.hasNormals && ! object.__webglNormalBuffer ) object.__webglNormalBuffer = renderer.createBuffer();
- if ( object.hasUvs && ! object.__webglUvBuffer ) object.__webglUvBuffer = renderer.createBuffer();
- if ( object.hasColors && ! object.__webglColorBuffer ) object.__webglColorBuffer = renderer.createBuffer();
- if ( object.hasPositions ) {
- renderer.setDynamicArrayBuffer( object.__webglVertexBuffer, object.positionArray);
- renderer.setFloatAttribute(program.attributes.position, object.__webglVertexBuffer, 3, 0);
- }
- if ( object.hasNormals ) {
- if ( 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;
- }
- }
-
- renderer.setDynamicArrayBuffer( object.__webglNormalBuffer, object.normalArray);
- renderer.setFloatAttribute(program.attributes.normal, object.__webglNormalBuffer, 3, 0);
- }
- if ( object.hasUvs && material.map ) {
-
- renderer.setDynamicArrayBuffer( object.__webglUvBuffer, object.uvArray);
- renderer.setFloatAttribute(program.attributes.uv, object.__webglUvBuffer, 2, 0);
- }
- if ( object.hasColors && material.vertexColors !== THREE.NoColors ) {
-
- renderer.setDynamicArrayBuffer( object.__webglColorBuffer, object.colorArray);
- renderer.setFloatAttribute(program.attributes.color, object.__webglColorBuffer, 3, 0);
- }
- renderer.drawTriangles(object.count );
- object.count = 0;
- };
- this.renderBufferDirect = function ( camera, lights, fog, material, geometry, object ) {
- if ( material.visible === false ) return;
- var program, attributes, linewidth, primitives, a, attribute;
- program = setProgram( camera, lights, fog, material, object );
- attributes = program.attributes;
- var updateBuffers = false,
- wireframeBit = material.wireframe ? 1 : 0,
- geometryHash = ( geometry.id * 0xffffff ) + ( program.id * 2 ) + wireframeBit;
- if ( geometryHash !== _currentGeometryGroupHash ) {
- _currentGeometryGroupHash = geometryHash;
- updateBuffers = true;
- }
- if ( updateBuffers ) {
- renderer.disableAttributes();
- }
- // render mesh
- if ( object instanceof THREE.Mesh ) {
- var index = geometry.attributes[ "index" ];
- // indexed triangles
- if ( index ) {
- var offsets = geometry.offsets;
- // 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 ) {
- // vertices
- var position = geometry.attributes[ "position" ];
- var positionSize = position.itemSize;
- renderer.setFloatAttribute(attributes.position , position.buffer, positionSize, startIndex * positionSize * 4);
- // normals
- var normal = geometry.attributes[ "normal" ];
- if ( attributes.normal >= 0 && normal ) {
- var normalSize = normal.itemSize;
- renderer.setFloatAttribute(attributes.normal , normal.buffer, normalSize, startIndex * normalSize * 4);
- }
- // uvs
- var uv = geometry.attributes[ "uv" ];
- if ( attributes.uv >= 0 && uv ) {
- var uvSize = uv.itemSize;
- renderer.setFloatAttribute(attributes.uv , uv.buffer, uvSize, startIndex * uvSize * 4);
- }
- // colors
- var color = geometry.attributes[ "color" ];
- if ( attributes.color >= 0 && color ) {
- var colorSize = color.itemSize;
- renderer.setFloatAttribute(attributes.color , color.buffer, colorSize, startIndex * colorSize * 4);
- }
- // tangents
- var tangent = geometry.attributes[ "tangent" ];
- if ( attributes.tangent >= 0 && tangent ) {
- var tangentSize = tangent.itemSize;
- renderer.setFloatAttribute(attributes.tangent , tangent.buffer, tangentSize, startIndex * tangentSize * 4);
- }
- }
- // render indexed triangles
-
- renderer.drawTriangleElements(index.buffer, offsets[ i ].count, offsets[ i ].start * 2);
- _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;
- }
- // non-indexed triangles
- } else {
- if ( updateBuffers ) {
- // vertices
- var position = geometry.attributes[ "position" ];
- var positionSize = position.itemSize;
- renderer.setFloatAttribute(attributes.position , position.buffer, positionSize, 0);
- // normals
- var normal = geometry.attributes[ "normal" ];
- if ( attributes.normal >= 0 && normal ) {
- var normalSize = normal.itemSize;
- renderer.setFloatAttribute(attributes.normal , normal.buffer, normalSize, 0);
- }
- // uvs
- var uv = geometry.attributes[ "uv" ];
- if ( attributes.uv >= 0 && uv ) {
- var uvSize = uv.itemSize;
- renderer.setFloatAttribute(attributes.uv , uv.buffer, uvSize, 0);
- }
- // colors
- var color = geometry.attributes[ "color" ];
- if ( attributes.color >= 0 && color ) {
- var colorSize = color.itemSize;
- renderer.setFloatAttribute(attributes.color , color.buffer, colorSize, 0);
- }
- // tangents
- var tangent = geometry.attributes[ "tangent" ];
- if ( attributes.tangent >= 0 && tangent ) {
- var tangentSize = tangent.itemSize;
- renderer.setFloatAttribute(attributes.tangent , tangent.buffer, tangentSize, 0);
- }
- }
- // render non-indexed triangles
- renderer.drawTriangles( position.numItems / 3)
- _this.info.render.calls ++;
- _this.info.render.vertices += position.numItems / 3;
- _this.info.render.faces += position.numItems / 3 / 3;
- }
- // render particles
- } else if ( object instanceof THREE.ParticleSystem ) {
- if ( updateBuffers ) {
- // vertices
- var position = geometry.attributes[ "position" ];
- var positionSize = position.itemSize;
- renderer.setFloatAttribute(attributes.position , position.buffer, positionSize, 0);
- // colors
- var color = geometry.attributes[ "color" ];
- if ( attributes.color >= 0 && color ) {
- var colorSize = color.itemSize;
- renderer.setFloatAttribute(attributes.color , color.buffer, colorSize, 0);
- }
- // render particles
- renderer.drawPoints(position.numItems / 3);
- _this.info.render.calls ++;
- _this.info.render.points += position.numItems / 3;
- }
- } else if ( object instanceof THREE.Line ) {
- if ( updateBuffers ) {
- // vertices
- var position = geometry.attributes[ "position" ];
- var positionSize = position.itemSize;
- renderer.setFloatAttribute(attributes.position , position.buffer, positionSize, 0);
- // colors
- var color = geometry.attributes[ "color" ];
- if ( attributes.color >= 0 && color ) {
- var colorSize = color.itemSize;
- renderer.setFloatAttribute(attributes.color , color.buffer, colorSize, 0);
- }
- // render lines
- renderer.drawLineStrip(position.numItems / 3);
- _this.info.render.calls ++;
- _this.info.render.points += position.numItems;
- }
- }
- };
- this.renderBuffer = function ( camera, lights, fog, material, geometryGroup, object ) {
- if ( material.visible === false ) return;
- var program, attributes, linewidth, primitives, a, attribute, i, il;
- program = setProgram( camera, lights, fog, material, object );
- attributes = program.attributes;
- var updateBuffers = false,
- wireframeBit = material.wireframe ? 1 : 0,
- geometryGroupHash = ( geometryGroup.id * 0xffffff ) + ( program.id * 2 ) + wireframeBit;
- if ( geometryGroupHash !== _currentGeometryGroupHash ) {
- _currentGeometryGroupHash = geometryGroupHash;
- updateBuffers = true;
- }
- if ( updateBuffers ) {
- renderer.disableAttributes();
- }
- // vertices
- if ( !material.morphTargets && attributes.position >= 0 ) {
- if ( updateBuffers ) {
-
- renderer.setFloatAttribute(attributes.position , geometryGroup.__webglVertexBuffer, 3, 0);
- }
- } else {
- if ( object.morphTargetBase ) {
- setupMorphTargets( material, geometryGroup, object );
- }
- }
- if ( updateBuffers ) {
- // custom attributes
- // Use the per-geometryGroup custom attribute arrays which are setup in initMeshBuffers
- if ( geometryGroup.__webglCustomAttributesList ) {
- for ( i = 0, il = geometryGroup.__webglCustomAttributesList.length; i < il; i ++ ) {
- attribute = geometryGroup.__webglCustomAttributesList[ i ];
- if ( attributes[ attribute.buffer.belongsToAttribute ] >= 0 ) {
-
- renderer.setFloatAttribute(attributes[ attribute.buffer.belongsToAttribute ] , attribute.buffer, attribute.size, 0);
- }
- }
- }
- // colors
- if ( attributes.color >= 0 ) {
- renderer.setFloatAttribute(attributes.color , geometryGroup.__webglColorBuffer,3, 0);
- }
- // normals
- if ( attributes.normal >= 0 ) {
- renderer.setFloatAttribute(attributes.normal, geometryGroup.__webglNormalBuffer, 3, 0);
- }
- // tangents
- if ( attributes.tangent >= 0 ) {
- renderer.setFloatAttribute(attributes.tangent, geometryGroup.__webglTangentBuffer, 4, 0);
- }
- // uvs
- if ( attributes.uv >= 0 ) {
- renderer.setFloatAttribute(attributes.uv, geometryGroup.__webglUVBuffer, 2, 0);
- }
- if ( attributes.uv2 >= 0 ) {
- renderer.setFloatAttribute(attributes.uv2, geometryGroup.__webglUV2Buffer, 2, 0);
- }
- if ( material.skinning &&
- attributes.skinIndex >= 0 && attributes.skinWeight >= 0 ) {
- renderer.setFloatAttribute(attributes.skinIndex, geometryGroup.__webglSkinIndicesBuffer, 4, 0);
- renderer.setFloatAttribute(attributes.skinWeight, geometryGroup.__webglSkinWeightsBuffer, 4, 0);
- }
- // line distances
- if ( attributes.lineDistance >= 0 ) {
-
- renderer.setFloatAttribute(attributes.lineDistance, geometryGroup.__webglLineDistanceBuffer, 1, 0);
- }
- }
- // render mesh
- if ( object instanceof THREE.Mesh ) {
- // wireframe
- if ( material.wireframe ) {
- renderer.setLineWidth( material.wireframeLinewidth );
- renderer.drawLineElements(geometryGroup.__webglLineBuffer,geometryGroup.__webglLineCount,0);
- // triangles
- } else {
- renderer.drawTriangleElements( geometryGroup.__webglFaceBuffer, geometryGroup.__webglFaceCount, 0);
- }
- _this.info.render.calls ++;
- _this.info.render.vertices += geometryGroup.__webglFaceCount;
- _this.info.render.faces += geometryGroup.__webglFaceCount / 3;
- // render lines
- } else if ( object instanceof THREE.Line ) {
- renderer.setLineWidth( material.linewidth );
-
- if (object.type === THREE.LineStrip) {
-
- renderer.drawLineStrip(geometryGroup.__webglLineCount);
-
- } else {
-
- renderer.drawLines(geometryGroup.__webglLineCount);
-
- }
- _this.info.render.calls ++;
- // render particles
- } else if ( object instanceof THREE.ParticleSystem ) {
-
- renderer.drawPoints(geometryGroup.__webglParticleCount);
- _this.info.render.calls ++;
- _this.info.render.points += geometryGroup.__webglParticleCount;
- // render ribbon
- } else if ( object instanceof THREE.Ribbon ) {
- renderer.drawTriangleStrip(geometryGroup.__webglVertexCount);
- _this.info.render.calls ++;
- }
- };
- function setupMorphTargets ( material, geometryGroup, object ) {
- // set base
- var attributes = material.program.attributes;
- if ( object.morphTargetBase !== -1 && attributes.position >= 0 ) {
- renderer.setFloatAttribute(attributes.position, geometryGroup.__webglMorphTargetsBuffers[ object.morphTargetBase ], 3, 0);
- } else if ( attributes.position >= 0 ) {
- renderer.setFloatAttribute(attributes.position, geometryGroup.__webglVertexBuffer, 3, 0);
- }
- if ( object.morphTargetForcedOrder.length ) {
- // set forced order
- var m = 0;
- var order = object.morphTargetForcedOrder;
- var influences = object.morphTargetInfluences;
- while ( m < material.numSupportedMorphTargets && m < order.length ) {
- if ( attributes[ "morphTarget" + m ] >= 0 ) {
- renderer.setFloatAttribute(attributes[ "morphTarget" + m ], geometryGroup.__webglMorphTargetsBuffers[ order[ m ] ], 3, 0);
- }
- if ( attributes[ "morphNormal" + m ] >= 0 && material.morphNormals ) {
- renderer.setFloatAttribute(attributes[ "morphNormal" + m ], geometryGroup.__webglMorphNormalsBuffers[ order[ m ] ], 3, 0);
- }
- object.__webglMorphTargetInfluences[ m ] = influences[ order[ m ] ];
- m ++;
- }
- } else {
- // find the most influencing
- var influence, activeInfluenceIndices = [];
- var influences = object.morphTargetInfluences;
- var i, il = influences.length;
- for ( i = 0; i < il; i ++ ) {
- influence = influences[ i ];
- if ( influence > 0 ) {
- 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 influenceIndex, m = 0;
- while ( m < material.numSupportedMorphTargets ) {
- if ( activeInfluenceIndices[ m ] ) {
- influenceIndex = activeInfluenceIndices[ m ][ 1 ];
- if ( attributes[ "morphTarget" + m ] >= 0 ) {
-
- renderer.setFloatAttribute(attributes[ "morphTarget" + m ], geometryGroup.__webglMorphTargetsBuffers[ influenceIndex ], 3, 0);
- }
- if ( attributes[ "morphNormal" + m ] >= 0 && material.morphNormals ) {
-
- renderer.setFloatAttribute(attributes[ "morphNormal" + m ], geometryGroup.__webglMorphNormalsBuffers[ influenceIndex ], 3, 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;
- }
- m ++;
- }
- }
- // load updated influences uniform
- if ( material.program.uniforms.morphTargetInfluences !== null ) {
- renderer.uniform1fv( material.program.uniforms.morphTargetInfluences, object.__webglMorphTargetInfluences );
- }
- };
- // Sorting
- function painterSortStable ( a, b ) {
- if ( a.z !== b.z ) {
- return b.z - a.z;
- } else {
- return b.id - a.id;
- }
- };
- function numericalSort ( a, b ) {
- return b[ 0 ] - a[ 0 ];
- };
- // Rendering
- this.render = function ( scene, camera, renderTarget, forceClear ) {
- if ( camera instanceof THREE.Camera === false ) {
- console.error( 'THREE.WebGLRenderer.render: camera is not an instance of THREE.Camera.' );
- return;
- }
- var i, il,
- webglObject, object,
- renderList,
- lights = scene.__lights,
- fog = scene.fog;
- // reset caching for this frame
- _currentMaterialId = -1;
- _lightsNeedUpdate = true;
- // update scene graph
- if ( this.autoUpdateScene ) scene.updateMatrixWorld();
- // update camera matrices and frustum
- if ( camera.parent === undefined ) camera.updateMatrixWorld();
- camera.matrixWorldInverse.getInverse( camera.matrixWorld );
- _projScreenMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse );
- _frustum.setFromMatrix( _projScreenMatrix );
- // update WebGL objects
- if ( this.autoUpdateObjects ) this.initWebGLObjects( scene );
- // custom render plugins (pre pass)
- renderPlugins( this.renderPluginsPre, scene, camera );
- //
- _this.info.render.calls = 0;
- _this.info.render.vertices = 0;
- _this.info.render.faces = 0;
- _this.info.render.points = 0;
- renderer.setRenderTarget( renderTarget );
- if ( this.autoClear || forceClear ) {
- this.clear( this.autoClearColor, this.autoClearDepth, this.autoClearStencil );
- }
- // set matrices for regular objects (frustum culled)
- renderList = scene.__webglObjects;
- for ( i = 0, il = renderList.length; i < il; i ++ ) {
- webglObject = renderList[ i ];
- object = webglObject.object;
- webglObject.render = false;
- if ( object.visible ) {
- if ( ! ( object instanceof THREE.Mesh || object instanceof THREE.ParticleSystem ) || ! ( object.frustumCulled ) || _frustum.intersectsObject( object ) ) {
- setupMatrices( object, camera );
- unrollBufferMaterial( webglObject );
- webglObject.render = true;
- if ( this.sortObjects === true ) {
- if ( object.renderDepth !== null ) {
- webglObject.z = object.renderDepth;
- } else {
- _vector3.copy( object.matrixWorld.getPosition() );
- _vector3.projectPoint(_projScreenMatrix);
- webglObject.z = _vector3.z;
- }
- webglObject.id = object.id;
- }
- }
- }
- }
- if ( this.sortObjects ) {
- renderList.sort( painterSortStable );
- }
- // set matrices for immediate objects
- renderList = scene.__webglObjectsImmediate;
- for ( i = 0, il = renderList.length; i < il; i ++ ) {
- webglObject = renderList[ i ];
- object = webglObject.object;
- if ( object.visible ) {
- setupMatrices( object, camera );
- unrollImmediateBufferMaterial( webglObject );
- }
- }
- if ( scene.overrideMaterial ) {
- var material = scene.overrideMaterial;
- renderer.setBlending( material.blending, material.blendEquation, material.blendSrc, material.blendDst );
- renderer.setDepthTest( material.depthTest );
- renderer.setDepthWrite( material.depthWrite );
- renderer.setPolygonOffset( material.polygonOffset, material.polygonOffsetFactor, material.polygonOffsetUnits );
- renderObjects( scene.__webglObjects, false, "", camera, lights, fog, true, material );
- renderObjectsImmediate( scene.__webglObjectsImmediate, "", camera, lights, fog, false, material );
- } else {
- var material = null;
- // opaque pass (front-to-back order)
- renderer.setBlending( THREE.NoBlending );
- renderObjects( scene.__webglObjects, true, "opaque", camera, lights, fog, false, material );
- renderObjectsImmediate( scene.__webglObjectsImmediate, "opaque", camera, lights, fog, false, material );
- // transparent pass (back-to-front order)
- renderObjects( scene.__webglObjects, false, "transparent", camera, lights, fog, true, material );
- renderObjectsImmediate( scene.__webglObjectsImmediate, "transparent", camera, lights, fog, true, material );
- }
- // custom render plugins (post pass)
- renderPlugins( this.renderPluginsPost, scene, camera );
- // Generate mipmap if we're using any kind of mipmap filtering
- if ( renderTarget && renderTarget.generateMipmaps && renderTarget.minFilter !== THREE.NearestFilter && renderTarget.minFilter !== THREE.LinearFilter ) {
- renderer.updateRenderTargetMipmap( renderTarget );
- }
- // Ensure depth buffer writing is enabled so it can be cleared on next render
- renderer.setDepthTest( true );
- renderer.setDepthWrite( true );
- // _gl.finish();
- };
- function renderPlugins( plugins, scene, camera ) {
- if ( ! plugins.length ) return;
- for ( var i = 0, il = plugins.length; i < il; i ++ ) {
- // reset state for plugin (to start from clean slate)
- _currentProgram = null;
- _currentCamera = null;
- _currentGeometryGroupHash = -1;
- _currentMaterialId = -1;
- _lightsNeedUpdate = true;
- renderer.resetState();
- plugins[ i ].render( scene, camera, renderer.getCurrentWidth(), renderer.getCurrentWidth() );
- // reset state after plugin (anything could have changed)
- _currentProgram = null;
- _currentCamera = null;
- _currentGeometryGroupHash = -1;
- _currentMaterialId = -1;
- _lightsNeedUpdate = true;
- renderer.resetState();
- }
- };
- function renderObjects ( renderList, reverse, materialType, camera, lights, fog, useBlending, overrideMaterial ) {
- var webglObject, object, buffer, material, start, end, delta;
- if ( reverse ) {
- start = renderList.length - 1;
- end = -1;
- delta = -1;
- } else {
- start = 0;
- end = renderList.length;
- delta = 1;
- }
- for ( var i = start; i !== end; i += delta ) {
- webglObject = renderList[ i ];
- if ( webglObject.render ) {
- object = webglObject.object;
- buffer = webglObject.buffer;
- if ( overrideMaterial ) {
- material = overrideMaterial;
- } else {
- material = webglObject[ materialType ];
- if ( ! material ) continue;
- if ( useBlending ) renderer.setBlending( material.blending, material.blendEquation, material.blendSrc, material.blendDst );
- renderer.setDepthTest( material.depthTest );
- renderer.setDepthWrite( material.depthWrite );
- renderer.setPolygonOffset( material.polygonOffset, material.polygonOffsetFactor, material.polygonOffsetUnits );
- }
- renderer.setMaterialFaces( material );
- if ( buffer instanceof THREE.BufferGeometry ) {
- _this.renderBufferDirect( camera, lights, fog, material, buffer, object );
- } else {
- _this.renderBuffer( camera, lights, fog, material, buffer, object );
- }
- }
- }
- };
- function renderObjectsImmediate ( renderList, materialType, camera, lights, fog, useBlending, overrideMaterial ) {
- var webglObject, object, material, program;
- for ( var i = 0, il = renderList.length; i < il; i ++ ) {
- webglObject = renderList[ i ];
- object = webglObject.object;
- if ( object.visible ) {
- if ( overrideMaterial ) {
- material = overrideMaterial;
- } else {
- material = webglObject[ materialType ];
- if ( ! material ) continue;
- if ( useBlending ) renderer.setBlending( material.blending, material.blendEquation, material.blendSrc, material.blendDst );
- renderer.setDepthTest( material.depthTest );
- renderer.setDepthWrite( material.depthWrite );
- renderer.setPolygonOffset( material.polygonOffset, material.polygonOffsetFactor, material.polygonOffsetUnits );
- }
- _this.renderImmediateObject( camera, lights, fog, material, object );
- }
- }
- };
- this.renderImmediateObject = function ( camera, lights, fog, material, object ) {
- var program = setProgram( camera, lights, fog, material, object );
- _currentGeometryGroupHash = -1;
- renderer.setMaterialFaces( material );
- if ( object.immediateRenderCallback ) {
- object.immediateRenderCallback( program, renderer.getContext(), _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,
- buffer = globject.buffer,
- material, materialIndex, meshMaterial;
- meshMaterial = object.material;
- if ( meshMaterial instanceof THREE.MeshFaceMaterial ) {
- materialIndex = buffer.materialIndex;
- material = meshMaterial.materials[ materialIndex ];
- if ( material.transparent ) {
- globject.transparent = material;
- globject.opaque = null;
- } else {
- globject.opaque = material;
- globject.transparent = null;
- }
- } else {
- material = meshMaterial;
- if ( material ) {
- if ( material.transparent ) {
- globject.transparent = material;
- globject.opaque = null;
- } else {
- globject.opaque = material;
- globject.transparent = null;
- }
- }
- }
- };
- // Geometry splitting
- function sortFacesByMaterial ( geometry, material ) {
- var f, fl, face, materialIndex, vertices,
- groupHash, hash_map = {};
- var numMorphTargets = geometry.morphTargets.length;
- var numMorphNormals = geometry.morphNormals.length;
- var usesFaceMaterial = material instanceof THREE.MeshFaceMaterial;
- geometry.geometryGroups = {};
- for ( f = 0, fl = geometry.faces.length; f < fl; f ++ ) {
- face = geometry.faces[ f ];
- materialIndex = usesFaceMaterial ? face.materialIndex : 0;
- if ( hash_map[ materialIndex ] === undefined ) {
- hash_map[ materialIndex ] = { 'hash': materialIndex, 'counter': 0 };
- }
- groupHash = hash_map[ materialIndex ].hash + '_' + hash_map[ materialIndex ].counter;
- if ( geometry.geometryGroups[ groupHash ] === undefined ) {
- geometry.geometryGroups[ groupHash ] = { 'faces3': [], 'faces4': [], 'materialIndex': materialIndex, 'vertices': 0, 'numMorphTargets': numMorphTargets, 'numMorphNormals': numMorphNormals };
- }
- vertices = face instanceof THREE.Face3 ? 3 : 4;
- if ( geometry.geometryGroups[ groupHash ].vertices + vertices > 65535 ) {
- hash_map[ materialIndex ].counter += 1;
- groupHash = hash_map[ materialIndex ].hash + '_' + hash_map[ materialIndex ].counter;
- if ( geometry.geometryGroups[ groupHash ] === undefined ) {
- geometry.geometryGroups[ groupHash ] = { 'faces3': [], 'faces4': [], 'materialIndex': materialIndex, 'vertices': 0, 'numMorphTargets': numMorphTargets, 'numMorphNormals': numMorphNormals };
- }
- }
- if ( face instanceof THREE.Face3 ) {
- geometry.geometryGroups[ groupHash ].faces3.push( f );
- } else {
- geometry.geometryGroups[ groupHash ].faces4.push( f );
- }
- geometry.geometryGroups[ groupHash ].vertices += vertices;
- }
- geometry.geometryGroupsList = [];
- for ( var g in geometry.geometryGroups ) {
- geometry.geometryGroups[ g ].id = _geometryGroupCounter ++;
- geometry.geometryGroupsList.push( geometry.geometryGroups[ g ] );
- }
- };
- // Objects refresh
- this.initWebGLObjects = function ( scene ) {
- if ( !scene.__webglObjects ) {
- scene.__webglObjects = [];
- scene.__webglObjectsImmediate = [];
- scene.__webglSprites = [];
- scene.__webglFlares = [];
- }
- while ( scene.__objectsAdded.length ) {
- addObject( scene.__objectsAdded[ 0 ], scene );
- scene.__objectsAdded.splice( 0, 1 );
- }
- while ( scene.__objectsRemoved.length ) {
- removeObject( scene.__objectsRemoved[ 0 ], scene );
- scene.__objectsRemoved.splice( 0, 1 );
- }
- // update must be called after objects adding / removal
- for ( var o = 0, ol = scene.__webglObjects.length; o < ol; o ++ ) {
- updateObject( scene.__webglObjects[ o ].object );
- }
- };
- // Objects adding
- function addObject ( object, scene ) {
- var g, geometry, material, geometryGroup;
- if ( ! object.__webglInit ) {
- object.__webglInit = true;
- object._modelViewMatrix = new THREE.Matrix4();
- object._normalMatrix = new THREE.Matrix3();
- if ( object.geometry !== undefined && object.geometry.__webglInit === undefined ) {
- object.geometry.__webglInit = true;
- object.geometry.addEventListener( 'dispose', onGeometryDispose );
- }
- if ( object instanceof THREE.Mesh ) {
- geometry = object.geometry;
- material = object.material;
- if ( geometry instanceof THREE.Geometry ) {
- if ( geometry.geometryGroups === undefined ) {
- sortFacesByMaterial( geometry, material );
- }
- // create separate VBOs per geometry chunk
- for ( g in geometry.geometryGroups ) {
- geometryGroup = geometry.geometryGroups[ g ];
- // initialise VBO on the first access
- if ( ! geometryGroup.__webglVertexBuffer ) {
- meshRenderer.createBuffers( geometryGroup );
- meshRenderer.initBuffers( geometryGroup, object );
- geometry.verticesNeedUpdate = true;
- geometry.morphTargetsNeedUpdate = true;
- geometry.elementsNeedUpdate = true;
- geometry.uvsNeedUpdate = true;
- geometry.normalsNeedUpdate = true;
- geometry.tangentsNeedUpdate = true;
- geometry.colorsNeedUpdate = true;
- }
- }
- } else if ( geometry instanceof THREE.BufferGeometry ) {
- initDirectBuffers( geometry );
- }
- } else if ( object instanceof THREE.Ribbon ) {
- geometry = object.geometry;
- if ( ! geometry.__webglVertexBuffer ) {
- ribbonRenderer.createBuffers( geometry );
- ribbonRenderer.initBuffers( geometry, object );
- geometry.verticesNeedUpdate = true;
- geometry.colorsNeedUpdate = true;
- geometry.normalsNeedUpdate = true;
- }
- } else if ( object instanceof THREE.Line ) {
-
-
- geometry = object.geometry;
- if ( ! geometry.__webglVertexBuffer ) {
- if ( geometry instanceof THREE.Geometry ) {
- lineRenderer.createBuffers( geometry );
- lineRenderer.initBuffers( geometry, object );
- geometry.verticesNeedUpdate = true;
- geometry.colorsNeedUpdate = true;
- geometry.lineDistancesNeedUpdate = true;
- } else if ( geometry instanceof THREE.BufferGeometry ) {
- initDirectBuffers( geometry );
- }
- }
- } else if ( object instanceof THREE.ParticleSystem ) {
- geometry = object.geometry;
- if ( ! geometry.__webglVertexBuffer ) {
- if ( geometry instanceof THREE.Geometry ) {
- particleRenderer.createBuffers( geometry );
- particleRenderer.initBuffers( geometry, object );
- geometry.verticesNeedUpdate = true;
- geometry.colorsNeedUpdate = true;
- } else if ( geometry instanceof THREE.BufferGeometry ) {
- initDirectBuffers( geometry );
- }
- }
- }
- }
- if ( ! object.__webglActive ) {
- if ( object instanceof THREE.Mesh ) {
- geometry = object.geometry;
- if ( geometry instanceof THREE.BufferGeometry ) {
- addBuffer( scene.__webglObjects, geometry, object );
- } else if ( geometry instanceof THREE.Geometry ) {
- for ( g in geometry.geometryGroups ) {
- geometryGroup = geometry.geometryGroups[ g ];
- addBuffer( scene.__webglObjects, geometryGroup, object );
- }
- }
- } else if ( object instanceof THREE.Ribbon ||
- object instanceof THREE.Line ||
- object instanceof THREE.ParticleSystem ) {
- geometry = object.geometry;
- addBuffer( scene.__webglObjects, geometry, object );
- } else if ( object instanceof THREE.ImmediateRenderObject || object.immediateRenderCallback ) {
- addBufferImmediate( scene.__webglObjectsImmediate, object );
- } else if ( object instanceof THREE.Sprite ) {
- scene.__webglSprites.push( object );
- } else if ( object instanceof THREE.LensFlare ) {
- scene.__webglFlares.push( object );
- }
- object.__webglActive = true;
- }
- };
- function addBuffer ( objlist, buffer, object ) {
- objlist.push(
- {
- buffer: buffer,
- object: object,
- opaque: null,
- transparent: null
- }
- );
- };
- function addBufferImmediate ( objlist, object ) {
- objlist.push(
- {
- object: object,
- opaque: null,
- transparent: null
- }
- );
- };
- // Objects updates
- function updateObject ( object ) {
- var geometry = object.geometry,
- geometryGroup, customAttributesDirty, material;
- if ( object instanceof THREE.Mesh ) {
- if ( geometry instanceof THREE.BufferGeometry ) {
- if ( geometry.verticesNeedUpdate || geometry.elementsNeedUpdate ||
- geometry.uvsNeedUpdate || geometry.normalsNeedUpdate ||
- geometry.colorsNeedUpdate || geometry.tangentsNeedUpdate ) {
- setDirectBuffers( geometry, !geometry.dynamic );
- }
- geometry.verticesNeedUpdate = false;
- geometry.elementsNeedUpdate = false;
- geometry.uvsNeedUpdate = false;
- geometry.normalsNeedUpdate = false;
- geometry.colorsNeedUpdate = false;
- geometry.tangentsNeedUpdate = false;
- } else {
- // check all geometry groups
- for( var i = 0, il = geometry.geometryGroupsList.length; i < il; i ++ ) {
- geometryGroup = geometry.geometryGroupsList[ i ];
- material = getBufferMaterial( object, geometryGroup );
- if ( geometry.buffersNeedUpdate ) {
- meshRenderer.initBuffers( geometryGroup, object );
- }
- customAttributesDirty = material.attributes && areCustomAttributesDirty( material );
- if ( geometry.verticesNeedUpdate || geometry.morphTargetsNeedUpdate || geometry.elementsNeedUpdate ||
- geometry.uvsNeedUpdate || geometry.normalsNeedUpdate ||
- geometry.colorsNeedUpdate || geometry.tangentsNeedUpdate || customAttributesDirty ) {
- meshRenderer.setBuffers( geometryGroup, object, !geometry.dynamic, material );
- }
- }
- geometry.verticesNeedUpdate = false;
- geometry.morphTargetsNeedUpdate = false;
- geometry.elementsNeedUpdate = false;
- geometry.uvsNeedUpdate = false;
- geometry.normalsNeedUpdate = false;
- geometry.colorsNeedUpdate = false;
- geometry.tangentsNeedUpdate = false;
- geometry.buffersNeedUpdate = false;
- material.attributes && clearCustomAttributes( material );
- }
- } else if ( object instanceof THREE.Ribbon ) {
- material = getBufferMaterial( object, geometry );
- customAttributesDirty = material.attributes && areCustomAttributesDirty( material );
- if ( geometry.verticesNeedUpdate || geometry.colorsNeedUpdate || geometry.normalsNeedUpdate || customAttributesDirty ) {
- ribbonRenderer.setBuffers( geometry);
- }
- geometry.verticesNeedUpdate = false;
- geometry.colorsNeedUpdate = false;
- geometry.normalsNeedUpdate = false;
- material.attributes && clearCustomAttributes( material );
- } else if ( object instanceof THREE.Line ) {
-
- if ( geometry instanceof THREE.BufferGeometry ) {
- if ( geometry.verticesNeedUpdate || geometry.colorsNeedUpdate ) {
- setDirectBuffers( geometry, !geometry.dynamic );
- }
- geometry.verticesNeedUpdate = false;
- geometry.colorsNeedUpdate = false;
- } else {
- material = getBufferMaterial( object, geometry );
-
- customAttributesDirty = material.attributes && areCustomAttributesDirty( material );
-
- if ( geometry.verticesNeedUpdate || geometry.colorsNeedUpdate || geometry.lineDistancesNeedUpdate || customAttributesDirty ) {
-
- lineRenderer.setBuffers( geometry);
-
- }
-
- geometry.verticesNeedUpdate = false;
- geometry.colorsNeedUpdate = false;
- geometry.lineDistancesNeedUpdate = false;
-
- material.attributes && clearCustomAttributes( material );
- }
-
- } else if ( object instanceof THREE.ParticleSystem ) {
- if ( geometry instanceof THREE.BufferGeometry ) {
- if ( geometry.verticesNeedUpdate || geometry.colorsNeedUpdate ) {
- setDirectBuffers( geometry, !geometry.dynamic );
- }
- geometry.verticesNeedUpdate = false;
- geometry.colorsNeedUpdate = false;
- } else {
- material = getBufferMaterial( object, geometry );
- customAttributesDirty = material.attributes && areCustomAttributesDirty( material );
- if ( geometry.verticesNeedUpdate || geometry.colorsNeedUpdate || object.sortParticles || customAttributesDirty ) {
- particleRenderer.setBuffers( geometry, object, _projScreenMatrix);
- }
- geometry.verticesNeedUpdate = false;
- geometry.colorsNeedUpdate = false;
- material.attributes && clearCustomAttributes( material );
- }
- }
- };
- // Objects updates - custom attributes check
- function areCustomAttributesDirty ( material ) {
- for ( var a in material.attributes ) {
- if ( material.attributes[ a ].needsUpdate ) return true;
- }
- return false;
- };
- function clearCustomAttributes ( material ) {
- for ( var a in material.attributes ) {
- material.attributes[ a ].needsUpdate = false;
- }
- };
- // Objects removal
- function removeObject ( object, scene ) {
- if ( object instanceof THREE.Mesh ||
- object instanceof THREE.ParticleSystem ||
- object instanceof THREE.Ribbon ||
- object instanceof THREE.Line ) {
- removeInstances( scene.__webglObjects, object );
- } else if ( object instanceof THREE.Sprite ) {
- removeInstancesDirect( scene.__webglSprites, object );
- } else if ( object instanceof THREE.LensFlare ) {
- removeInstancesDirect( scene.__webglFlares, object );
- } else if ( object instanceof THREE.ImmediateRenderObject || object.immediateRenderCallback ) {
- removeInstances( scene.__webglObjectsImmediate, object );
- }
- object.__webglActive = false;
- };
- function removeInstances ( objlist, object ) {
- for ( var o = objlist.length - 1; o >= 0; o -- ) {
- if ( objlist[ o ].object === object ) {
- objlist.splice( o, 1 );
- }
- }
- };
- function removeInstancesDirect ( objlist, object ) {
- for ( var o = objlist.length - 1; o >= 0; o -- ) {
- if ( objlist[ o ] === object ) {
- objlist.splice( o, 1 );
- }
- }
- };
- // Materials
- this.initMaterial = function ( material, lights, fog, object ) {
- material.addEventListener( 'dispose', onMaterialDispose );
- var u, a, identifiers, i, parameters, maxLightCount, maxBones, maxShadows, shaderID;
- if ( material instanceof THREE.MeshDepthMaterial ) {
- shaderID = 'depth';
- } else if ( material instanceof THREE.MeshNormalMaterial ) {
- shaderID = 'normal';
- } else if ( material instanceof THREE.MeshBasicMaterial ) {
- shaderID = 'basic';
- } else if ( material instanceof THREE.MeshLambertMaterial ) {
- shaderID = 'lambert';
- } else if ( material instanceof THREE.MeshPhongMaterial ) {
- shaderID = 'phong';
- } else if ( material instanceof THREE.LineBasicMaterial ) {
- shaderID = 'basic';
- } else if ( material instanceof THREE.LineDashedMaterial ) {
- shaderID = 'dashed';
- } else if ( material instanceof THREE.ParticleBasicMaterial ) {
- shaderID = 'particle_basic';
- }
- if ( shaderID ) {
- setMaterialShaders( material, THREE.ShaderLib[ shaderID ] );
- }
- // heuristics to create shader parameters according to lights in the scene
- // (not to blow over maxLights budget)
- maxLightCount = allocateLights( lights );
- maxShadows = allocateShadows( lights );
- maxBones = allocateBones( object );
- parameters = {
- map: !!material.map,
- envMap: !!material.envMap,
- lightMap: !!material.lightMap,
- bumpMap: !!material.bumpMap,
- normalMap: !!material.normalMap,
- specularMap: !!material.specularMap,
- vertexColors: material.vertexColors,
- fog: fog,
- useFog: material.fog,
- fogExp: fog instanceof THREE.FogExp2,
- sizeAttenuation: material.sizeAttenuation,
- skinning: material.skinning,
- maxBones: maxBones,
- useVertexTexture: renderer.supportsBoneTextures && object && object.useVertexTexture,
- boneTextureWidth: object && object.boneTextureWidth,
- boneTextureHeight: object && object.boneTextureHeight,
- 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: this.shadowMapEnabled && object.receiveShadow,
- shadowMapType: this.shadowMapType,
- shadowMapDebug: this.shadowMapDebug,
- shadowMapCascade: this.shadowMapCascade,
- alphaTest: material.alphaTest,
- metal: material.metal,
- perPixel: material.perPixel,
- wrapAround: material.wrapAround,
- doubleSided: material.side === THREE.DoubleSide,
- flipSided: material.side === THREE.BackSide,
-
- gammaInput : this.gammaInput,
- gammaOutput : this.gammaOutput ,
- physicallyBasedShading : this.physicallyBasedShading
- };
- material.program = shaderBuilder.buildProgram( shaderID, material.fragmentShader, material.vertexShader, material.uniforms, material.attributes, material.defines, parameters );
- var attributes = material.program.attributes;
- if ( material.morphTargets ) {
- material.numSupportedMorphTargets = 0;
- var id, base = "morphTarget";
- for ( 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 ( u in material.uniforms ) {
- material.uniformsList.push( [ material.uniforms[ u ], u ] );
- }
- };
- function setMaterialShaders( material, shaders ) {
- material.uniforms = THREE.UniformsUtils.clone( shaders.uniforms );
- material.vertexShader = shaders.vertexShader;
- material.fragmentShader = shaders.fragmentShader;
- };
- function setProgram( camera, lights, fog, material, object ) {
- _usedTextureUnits = 0;
- if ( material.needsUpdate ) {
- if ( material.program ) deallocateMaterial( material );
- _this.initMaterial( material, lights, fog, object );
- material.needsUpdate = false;
- }
- if ( material.morphTargets ) {
- if ( ! object.__webglMorphTargetInfluences ) {
- object.__webglMorphTargetInfluences = new Float32Array( _this.maxMorphTargets );
- }
- }
- var refreshMaterial = false;
- var program = material.program,
- p_uniforms = program.uniforms,
- m_uniforms = material.uniforms;
- if ( program !== _currentProgram ) {
- renderer.useProgram( program );
- _currentProgram = program;
- refreshMaterial = true;
- }
- if ( material.id !== _currentMaterialId ) {
- _currentMaterialId = material.id;
- refreshMaterial = true;
- }
- if ( refreshMaterial || camera !== _currentCamera ) {
- renderer.uniformMatrix4fv( p_uniforms.projectionMatrix, camera.projectionMatrix.elements );
- if ( camera !== _currentCamera ) _currentCamera = camera;
- }
- // 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 ( renderer.supportsBoneTextures && object.useVertexTexture ) {
- if ( p_uniforms.boneTexture !== null ) {
- var textureUnit = getTextureUnit();
- renderer.uniform1i( p_uniforms.boneTexture, textureUnit );
- renderer.setTexture( object.boneTexture, textureUnit );
- }
- } else {
- if ( p_uniforms.boneGlobalMatrices !== null ) {
- renderer.uniformMatrix4fv( p_uniforms.boneGlobalMatrices, object.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 ) {
- setupLights( program, lights );
- _lightsNeedUpdate = false;
- }
- refreshUniformsLights( m_uniforms, _lights );
- }
- 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.ParticleBasicMaterial ) {
- 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( program, material.uniformsList );
- // 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 ) {
- var position = camera.matrixWorld.getPosition();
- renderer.uniform3f( p_uniforms.cameraPosition, position.x, position.y, position.z );
- }
- }
- if ( material instanceof THREE.MeshPhongMaterial ||
- material instanceof THREE.MeshLambertMaterial ||
- material instanceof THREE.ShaderMaterial ||
- material.skinning ) {
- if ( p_uniforms.viewMatrix !== null ) {
- renderer.uniformMatrix4fv( p_uniforms.viewMatrix, camera.matrixWorldInverse.elements );
- }
- }
- }
- loadUniformsMatrices( p_uniforms, object );
- if ( p_uniforms.modelMatrix !== null ) {
- renderer.uniformMatrix4fv( p_uniforms.modelMatrix, object.matrixWorld.elements );
- }
- return program;
- };
- // Uniforms (refresh uniforms objects)
- function refreshUniformsCommon ( uniforms, material ) {
- uniforms.opacity.value = material.opacity;
- if ( _this.gammaInput ) {
- uniforms.diffuse.value.copyGammaToLinear( material.color );
- } else {
- uniforms.diffuse.value = material.color;
- }
- uniforms.map.value = material.map;
- uniforms.lightMap.value = material.lightMap;
- uniforms.specularMap.value = material.specularMap;
- 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
- 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;
- }
- 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;
- if ( _this.gammaInput ) {
- //uniforms.reflectivity.value = material.reflectivity * material.reflectivity;
- uniforms.reflectivity.value = material.reflectivity;
- } else {
- uniforms.reflectivity.value = material.reflectivity;
- }
- uniforms.refractionRatio.value = material.refractionRatio;
- uniforms.combine.value = material.combine;
- uniforms.useRefract.value = material.envMap && material.envMap.mapping instanceof THREE.CubeRefractionMapping;
- };
- 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 = renderer.getDomElement().height / 2.0; // TODO: Cache this.
- uniforms.map.value = material.map;
- };
- 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;
- if ( _this.gammaInput ) {
- uniforms.ambient.value.copyGammaToLinear( material.ambient );
- uniforms.emissive.value.copyGammaToLinear( material.emissive );
- uniforms.specular.value.copyGammaToLinear( material.specular );
- } else {
- uniforms.ambient.value = material.ambient;
- uniforms.emissive.value = material.emissive;
- uniforms.specular.value = material.specular;
- }
- if ( material.wrapAround ) {
- uniforms.wrapRGB.value.copy( material.wrapRGB );
- }
- };
- function refreshUniformsLambert ( uniforms, material ) {
- if ( _this.gammaInput ) {
- uniforms.ambient.value.copyGammaToLinear( material.ambient );
- uniforms.emissive.value.copyGammaToLinear( material.emissive );
- } else {
- uniforms.ambient.value = material.ambient;
- 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.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.hemisphereLightSkyColor.value = lights.hemi.skyColors;
- uniforms.hemisphereLightGroundColor.value = lights.hemi.groundColors;
- uniforms.hemisphereLightDirection.value = lights.hemi.positions;
- };
- 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 ) {
- renderer.uniformMatrix4fv( uniforms.modelViewMatrix, object._modelViewMatrix.elements );
- if ( uniforms.normalMatrix ) {
- renderer.uniformMatrix3fv( uniforms.normalMatrix, object._normalMatrix.elements );
- }
- };
- function getTextureUnit() {
- var textureUnit = _usedTextureUnits;
- if ( textureUnit >= renderer.maxTextures ) {
- console.warn( "WebGLRenderer: trying to use " + textureUnit + " texture units while this GPU supports only " + renderer.maxTextures );
- }
- _usedTextureUnits += 1;
- return textureUnit;
- };
- function loadUniformsGeneric ( program, uniforms ) {
- var uniform, value, type, location, texture, textureUnit, i, il, j, jl, offset;
- for ( j = 0, jl = uniforms.length; j < jl; j ++ ) {
- location = program.uniforms[ uniforms[ j ][ 1 ] ];
- if ( !location ) continue;
- uniform = uniforms[ j ][ 0 ];
- type = uniform.type;
- value = uniform.value;
- if ( type === "i" ) { // single integer
- renderer.uniform1i( location, value );
- } else if ( type === "f" ) { // single float
- renderer.uniform1f( location, value );
- } else if ( type === "v2" ) { // single THREE.Vector2
- renderer.uniform2f( location, value.x, value.y );
- } else if ( type === "v3" ) { // single THREE.Vector3
- renderer.uniform3f( location, value.x, value.y, value.z );
- } else if ( type === "v4" ) { // single THREE.Vector4
- renderer.uniform4f( location, value.x, value.y, value.z, value.w );
- } else if ( type === "c" ) { // single THREE.Color
- renderer.uniform3f( location, value.r, value.g, value.b );
- } else if ( type === "iv1" ) { // flat array of integers (JS or typed array)
- renderer.uniform1iv( location, value );
- } else if ( type === "iv" ) { // flat array of integers with 3 x N size (JS or typed array)
- renderer.uniform3iv( location, value );
- } else if ( type === "fv1" ) { // flat array of floats (JS or typed array)
- renderer.uniform1fv( location, value );
- } else if ( type === "fv" ) { // flat array of floats with 3 x N size (JS or typed array)
- renderer.uniform3fv( location, value );
- } else if ( type === "v2v" ) { // array of THREE.Vector2
- if ( uniform._array === undefined ) {
- uniform._array = new Float32Array( 2 * value.length );
- }
- for ( i = 0, il = value.length; i < il; i ++ ) {
- offset = i * 2;
- uniform._array[ offset ] = value[ i ].x;
- uniform._array[ offset + 1 ] = value[ i ].y;
- }
- renderer.uniform2fv( location, uniform._array );
- } else if ( type === "v3v" ) { // array of THREE.Vector3
- if ( uniform._array === undefined ) {
- uniform._array = new Float32Array( 3 * value.length );
- }
- for ( i = 0, il = value.length; i < il; i ++ ) {
- offset = i * 3;
- uniform._array[ offset ] = value[ i ].x;
- uniform._array[ offset + 1 ] = value[ i ].y;
- uniform._array[ offset + 2 ] = value[ i ].z;
- }
- renderer.uniform3fv( location, uniform._array );
- } else if ( type === "v4v" ) { // array of THREE.Vector4
- if ( uniform._array === undefined ) {
- uniform._array = new Float32Array( 4 * value.length );
- }
- for ( i = 0, il = value.length; i < il; i ++ ) {
- offset = i * 4;
- uniform._array[ offset ] = value[ i ].x;
- uniform._array[ offset + 1 ] = value[ i ].y;
- uniform._array[ offset + 2 ] = value[ i ].z;
- uniform._array[ offset + 3 ] = value[ i ].w;
- }
- renderer.uniform4fv( location, uniform._array );
- } else if ( type === "m4") { // single THREE.Matrix4
- if ( uniform._array === undefined ) {
- uniform._array = new Float32Array( 16 );
- }
- value.flattenToArray( uniform._array );
- renderer.uniformMatrix4fv( location, uniform._array );
- } else if ( type === "m4v" ) { // array of THREE.Matrix4
- if ( uniform._array === undefined ) {
- uniform._array = new Float32Array( 16 * value.length );
- }
- for ( i = 0, il = value.length; i < il; i ++ ) {
- value[ i ].flattenToArrayOffset( uniform._array, i * 16 );
- }
- renderer.uniformMatrix4fv( location, uniform._array );
- } else if ( type === "t" ) { // single THREE.Texture (2d or cube)
- texture = value;
- textureUnit = getTextureUnit();
- renderer.uniform1i( location, textureUnit );
- if ( !texture ) continue;
- if ( texture.image instanceof Array && texture.image.length === 6 ) {
- renderer.setCubeTexture( texture, textureUnit );
- } else if ( texture instanceof THREE.WebGLRenderTargetCube ) {
- renderer.setCubeTextureDynamic( texture, textureUnit );
- } else {
- renderer.setTexture( texture, textureUnit );
- }
- } else if ( type === "tv" ) { // array of THREE.Texture (2d)
- if ( uniform._array === undefined ) {
- uniform._array = [];
- }
- for( i = 0, il = uniform.value.length; i < il; i ++ ) {
- uniform._array[ i ] = getTextureUnit();
- }
- renderer.uniform1iv( location, uniform._array );
- for( i = 0, il = uniform.value.length; i < il; i ++ ) {
- texture = uniform.value[ i ];
- textureUnit = uniform._array[ i ];
- if ( !texture ) continue;
- renderer.setTexture( texture, textureUnit );
- }
- }
- }
- };
- function setupMatrices ( object, camera ) {
- object._modelViewMatrix.multiplyMatrices( camera.matrixWorldInverse, object.matrixWorld );
- object._normalMatrix.getInverse( object._modelViewMatrix );
- object._normalMatrix.transpose();
- };
- //
- function setColorGamma( array, offset, color, intensitySq ) {
- array[ offset ] = color.r * color.r * intensitySq;
- array[ offset + 1 ] = color.g * color.g * intensitySq;
- array[ offset + 2 ] = color.b * color.b * intensitySq;
- };
- function setColorLinear( array, offset, color, intensity ) {
- array[ offset ] = color.r * intensity;
- array[ offset + 1 ] = color.g * intensity;
- array[ offset + 2 ] = color.b * intensity;
- };
- function setupLights ( program, lights ) {
- var l, ll, light, n,
- r = 0, g = 0, b = 0,
- color, skyColor, groundColor,
- intensity, intensitySq,
- position,
- distance,
- zlights = _lights,
- dirColors = zlights.directional.colors,
- dirPositions = zlights.directional.positions,
- pointColors = zlights.point.colors,
- pointPositions = zlights.point.positions,
- pointDistances = zlights.point.distances,
- spotColors = zlights.spot.colors,
- spotPositions = zlights.spot.positions,
- spotDistances = zlights.spot.distances,
- spotDirections = zlights.spot.directions,
- spotAnglesCos = zlights.spot.anglesCos,
- spotExponents = zlights.spot.exponents,
- 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;
- if ( _this.gammaInput ) {
- r += color.r * color.r;
- g += color.g * color.g;
- b += color.b * color.b;
- } else {
- r += color.r;
- g += color.g;
- b += color.b;
- }
- } else if ( light instanceof THREE.DirectionalLight ) {
- dirCount += 1;
- if ( ! light.visible ) continue;
- _direction.copy( light.matrixWorld.getPosition() );
- _direction.sub( light.target.matrixWorld.getPosition() );
- _direction.normalize();
- // skip lights with undefined direction
- // these create troubles in OpenGL (making pixel black)
- if ( _direction.x === 0 && _direction.y === 0 && _direction.z === 0 ) continue;
- dirOffset = dirLength * 3;
- dirPositions[ dirOffset ] = _direction.x;
- dirPositions[ dirOffset + 1 ] = _direction.y;
- dirPositions[ dirOffset + 2 ] = _direction.z;
- if ( _this.gammaInput ) {
- setColorGamma( dirColors, dirOffset, color, intensity * intensity );
- } else {
- setColorLinear( dirColors, dirOffset, color, intensity );
- }
- dirLength += 1;
- } else if ( light instanceof THREE.PointLight ) {
- pointCount += 1;
- if ( ! light.visible ) continue;
- pointOffset = pointLength * 3;
- if ( _this.gammaInput ) {
- setColorGamma( pointColors, pointOffset, color, intensity * intensity );
- } else {
- setColorLinear( pointColors, pointOffset, color, intensity );
- }
- position = light.matrixWorld.getPosition();
- pointPositions[ pointOffset ] = position.x;
- pointPositions[ pointOffset + 1 ] = position.y;
- pointPositions[ pointOffset + 2 ] = position.z;
- pointDistances[ pointLength ] = distance;
- pointLength += 1;
- } else if ( light instanceof THREE.SpotLight ) {
- spotCount += 1;
- if ( ! light.visible ) continue;
- spotOffset = spotLength * 3;
- if ( _this.gammaInput ) {
- setColorGamma( spotColors, spotOffset, color, intensity * intensity );
- } else {
- setColorLinear( spotColors, spotOffset, color, intensity );
- }
- position = light.matrixWorld.getPosition();
- spotPositions[ spotOffset ] = position.x;
- spotPositions[ spotOffset + 1 ] = position.y;
- spotPositions[ spotOffset + 2 ] = position.z;
- spotDistances[ spotLength ] = distance;
- _direction.copy( position );
- _direction.sub( light.target.matrixWorld.getPosition() );
- _direction.normalize();
- spotDirections[ spotOffset ] = _direction.x;
- spotDirections[ spotOffset + 1 ] = _direction.y;
- spotDirections[ spotOffset + 2 ] = _direction.z;
- spotAnglesCos[ spotLength ] = Math.cos( light.angle );
- spotExponents[ spotLength ] = light.exponent;
- spotLength += 1;
- } else if ( light instanceof THREE.HemisphereLight ) {
- hemiCount += 1;
- if ( ! light.visible ) continue;
- _direction.copy( light.matrixWorld.getPosition() );
- _direction.normalize();
- // skip lights with undefined direction
- // these create troubles in OpenGL (making pixel black)
- if ( _direction.x === 0 && _direction.y === 0 && _direction.z === 0 ) continue;
- hemiOffset = hemiLength * 3;
- hemiPositions[ hemiOffset ] = _direction.x;
- hemiPositions[ hemiOffset + 1 ] = _direction.y;
- hemiPositions[ hemiOffset + 2 ] = _direction.z;
- skyColor = light.color;
- groundColor = light.groundColor;
- if ( _this.gammaInput ) {
- intensitySq = intensity * intensity;
- setColorGamma( hemiSkyColors, hemiOffset, skyColor, intensitySq );
- setColorGamma( hemiGroundColors, hemiOffset, groundColor, intensitySq );
- } else {
- 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;
- };
- // Allocations
- function allocateBones ( object ) {
- if ( renderer.supportsBoneTextures && object && object.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 = renderer.maxVertexUniformVectors
- var nVertexMatrices = Math.floor( ( nVertexUniforms - 20 ) / 4 );
- var maxBones = nVertexMatrices;
- if ( object !== undefined && object instanceof THREE.SkinnedMesh ) {
- maxBones = Math.min( object.bones.length, maxBones );
- if ( maxBones < object.bones.length ) {
- console.warn( "WebGLRenderer: too many bones - " + object.bones.length + ", this GPU supports just " + maxBones + " (try OpenGL instead of ANGLE)" );
- }
- }
- return maxBones;
- }
- };
- function allocateLights ( lights ) {
- var l, ll, light, dirLights, pointLights, spotLights, hemiLights;
- dirLights = pointLights = spotLights = hemiLights = 0;
- for ( l = 0, ll = lights.length; l < ll; l ++ ) {
- light = lights[ l ];
- if ( light.onlyShadow ) 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 l, ll, light, maxShadows = 0;
- for ( l = 0, ll = lights.length; l < ll; l++ ) {
- light = lights[ l ];
- if ( ! light.castShadow ) continue;
- if ( light instanceof THREE.SpotLight ) maxShadows ++;
- if ( light instanceof THREE.DirectionalLight && ! light.shadowCascade ) maxShadows ++;
- }
- return maxShadows;
- };
- // default plugins (order is important)
- this.shadowMapPlugin = new THREE.ShadowMapPlugin();
- this.addPrePlugin( this.shadowMapPlugin );
- this.addPostPlugin( new THREE.SpritePlugin() );
- this.addPostPlugin( new THREE.LensFlarePlugin() );
- };
|