123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575 |
- import Animation from './Animation.js';
- import RenderObjects from './RenderObjects.js';
- import Attributes from './Attributes.js';
- import Geometries from './Geometries.js';
- import Info from './Info.js';
- import Pipelines from './Pipelines.js';
- import Bindings from './Bindings.js';
- import RenderLists from './RenderLists.js';
- import RenderContexts from './RenderContexts.js';
- import Textures from './Textures.js';
- import Background from './Background.js';
- import Nodes from './nodes/Nodes.js';
- import Color4 from './Color4.js';
- import ClippingContext from './ClippingContext.js';
- import { Scene, Frustum, Matrix4, Vector2, Vector3, Vector4, DoubleSide, BackSide, FrontSide, SRGBColorSpace, NoColorSpace, NoToneMapping, LinearFilter, LinearSRGBColorSpace, RenderTarget, HalfFloatType, RGBAFormat } from 'three';
- import { NodeMaterial } from '../../nodes/Nodes.js';
- import QuadMesh from '../../objects/QuadMesh.js';
- import RenderBundles from './RenderBundles.js';
- const _scene = new Scene();
- const _drawingBufferSize = new Vector2();
- const _screen = new Vector4();
- const _frustum = new Frustum();
- const _projScreenMatrix = new Matrix4();
- const _vector3 = new Vector3();
- const _quad = new QuadMesh( new NodeMaterial() );
- class Renderer {
- constructor( backend, parameters = {} ) {
- this.isRenderer = true;
- //
- const {
- logarithmicDepthBuffer = false,
- alpha = true
- } = parameters;
- // public
- this.domElement = backend.getDomElement();
- this.backend = backend;
- this.autoClear = true;
- this.autoClearColor = true;
- this.autoClearDepth = true;
- this.autoClearStencil = true;
- this.alpha = alpha;
- this.logarithmicDepthBuffer = logarithmicDepthBuffer;
- this.outputColorSpace = SRGBColorSpace;
- this.toneMapping = NoToneMapping;
- this.toneMappingExposure = 1.0;
- this.sortObjects = true;
- this.depth = true;
- this.stencil = false;
- this.clippingPlanes = [];
- this.info = new Info();
- // nodes
- this.toneMappingNode = null;
- // internals
- this._pixelRatio = 1;
- this._width = this.domElement.width;
- this._height = this.domElement.height;
- this._viewport = new Vector4( 0, 0, this._width, this._height );
- this._scissor = new Vector4( 0, 0, this._width, this._height );
- this._scissorTest = false;
- this._attributes = null;
- this._geometries = null;
- this._nodes = null;
- this._animation = null;
- this._bindings = null;
- this._objects = null;
- this._pipelines = null;
- this._bundles = null;
- this._renderLists = null;
- this._renderContexts = null;
- this._textures = null;
- this._background = null;
- this._currentRenderContext = null;
- this._opaqueSort = null;
- this._transparentSort = null;
- this._frameBufferTarget = null;
- const alphaClear = this.alpha === true ? 0 : 1;
- this._clearColor = new Color4( 0, 0, 0, alphaClear );
- this._clearDepth = 1;
- this._clearStencil = 0;
- this._renderTarget = null;
- this._activeCubeFace = 0;
- this._activeMipmapLevel = 0;
- this._renderObjectFunction = null;
- this._currentRenderObjectFunction = null;
- this._currentRenderBundle = null;
- this._handleObjectFunction = this._renderObjectDirect;
- this._initialized = false;
- this._initPromise = null;
- this._compilationPromises = null;
- // backwards compatibility
- this.shadowMap = {
- enabled: false,
- type: null
- };
- this.xr = {
- enabled: false
- };
- this.debug = {
- checkShaderErrors: true,
- onShaderError: null
- };
- }
- async init() {
- if ( this._initialized ) {
- throw new Error( 'Renderer: Backend has already been initialized.' );
- }
- if ( this._initPromise !== null ) {
- return this._initPromise;
- }
- this._initPromise = new Promise( async ( resolve, reject ) => {
- const backend = this.backend;
- try {
- await backend.init( this );
- } catch ( error ) {
- reject( error );
- return;
- }
- this._nodes = new Nodes( this, backend );
- this._animation = new Animation( this._nodes, this.info );
- this._attributes = new Attributes( backend );
- this._background = new Background( this, this._nodes );
- this._geometries = new Geometries( this._attributes, this.info );
- this._textures = new Textures( this, backend, this.info );
- this._pipelines = new Pipelines( backend, this._nodes );
- this._bindings = new Bindings( backend, this._nodes, this._textures, this._attributes, this._pipelines, this.info );
- this._objects = new RenderObjects( this, this._nodes, this._geometries, this._pipelines, this._bindings, this.info );
- this._renderLists = new RenderLists();
- this._bundles = new RenderBundles();
- this._renderContexts = new RenderContexts();
- //
- this._initialized = true;
- resolve();
- } );
- return this._initPromise;
- }
- get coordinateSystem() {
- return this.backend.coordinateSystem;
- }
- async compileAsync( scene, camera, targetScene = null ) {
- if ( this._initialized === false ) await this.init();
- // preserve render tree
- const nodeFrame = this._nodes.nodeFrame;
- const previousRenderId = nodeFrame.renderId;
- const previousRenderContext = this._currentRenderContext;
- const previousRenderObjectFunction = this._currentRenderObjectFunction;
- const previousCompilationPromises = this._compilationPromises;
- //
- const sceneRef = ( scene.isScene === true ) ? scene : _scene;
- if ( targetScene === null ) targetScene = scene;
- const renderTarget = this._renderTarget;
- const renderContext = this._renderContexts.get( targetScene, camera, renderTarget );
- const activeMipmapLevel = this._activeMipmapLevel;
- const compilationPromises = [];
- this._currentRenderContext = renderContext;
- this._currentRenderObjectFunction = this.renderObject;
- this._handleObjectFunction = this._createObjectPipeline;
- this._compilationPromises = compilationPromises;
- nodeFrame.renderId ++;
- //
- nodeFrame.update();
- //
- renderContext.depth = this.depth;
- renderContext.stencil = this.stencil;
- if ( ! renderContext.clippingContext ) renderContext.clippingContext = new ClippingContext();
- renderContext.clippingContext.updateGlobal( this, camera );
- //
- sceneRef.onBeforeRender( this, scene, camera, renderTarget );
- //
- const renderList = this._renderLists.get( scene, camera );
- renderList.begin();
- this._projectObject( scene, camera, 0, renderList );
- // include lights from target scene
- if ( targetScene !== scene ) {
- targetScene.traverseVisible( function ( object ) {
- if ( object.isLight && object.layers.test( camera.layers ) ) {
- renderList.pushLight( object );
- }
- } );
- }
- renderList.finish();
- //
- if ( renderTarget !== null ) {
- this._textures.updateRenderTarget( renderTarget, activeMipmapLevel );
- const renderTargetData = this._textures.get( renderTarget );
- renderContext.textures = renderTargetData.textures;
- renderContext.depthTexture = renderTargetData.depthTexture;
- } else {
- renderContext.textures = null;
- renderContext.depthTexture = null;
- }
- //
- this._nodes.updateScene( sceneRef );
- //
- this._background.update( sceneRef, renderList, renderContext );
- // process render lists
- const opaqueObjects = renderList.opaque;
- const transparentObjects = renderList.transparent;
- const lightsNode = renderList.lightsNode;
- if ( opaqueObjects.length > 0 ) this._renderObjects( opaqueObjects, camera, sceneRef, lightsNode );
- if ( transparentObjects.length > 0 ) this._renderObjects( transparentObjects, camera, sceneRef, lightsNode );
- // restore render tree
- nodeFrame.renderId = previousRenderId;
- this._currentRenderContext = previousRenderContext;
- this._currentRenderObjectFunction = previousRenderObjectFunction;
- this._compilationPromises = previousCompilationPromises;
- this._handleObjectFunction = this._renderObjectDirect;
- // wait for all promises setup by backends awaiting compilation/linking/pipeline creation to complete
- await Promise.all( compilationPromises );
- }
- async renderAsync( scene, camera ) {
- if ( this._initialized === false ) await this.init();
- const renderContext = this._renderScene( scene, camera );
- await this.backend.resolveTimestampAsync( renderContext, 'render' );
- }
- _renderBundle( bundle, sceneRef, lightsNode ) {
- const { object, camera, renderList } = bundle;
- const renderContext = this._currentRenderContext;
- const renderContextData = this.backend.get( renderContext );
- //
- const renderBundle = this._bundles.get( object, camera );
- const renderBundleData = this.backend.get( renderBundle );
- if ( renderBundleData.renderContexts === undefined ) renderBundleData.renderContexts = new Set();
- //
- const renderBundleNeedsUpdate = renderBundleData.renderContexts.has( renderContext ) === false || object.needsUpdate === true;
- renderBundleData.renderContexts.add( renderContext );
- if ( renderBundleNeedsUpdate ) {
- if ( renderContextData.renderObjects === undefined || object.needsUpdate === true ) {
- const nodeFrame = this._nodes.nodeFrame;
- renderContextData.renderObjects = [];
- renderContextData.renderBundles = [];
- renderContextData.scene = sceneRef;
- renderContextData.camera = camera;
- renderContextData.renderId = nodeFrame.renderId;
- renderContextData.registerBundlesPhase = true;
- }
- this._currentRenderBundle = renderBundle;
- const opaqueObjects = renderList.opaque;
- if ( opaqueObjects.length > 0 ) this._renderObjects( opaqueObjects, camera, sceneRef, lightsNode );
- this._currentRenderBundle = null;
- //
- object.needsUpdate = false;
- } else {
- const renderContext = this._currentRenderContext;
- const renderContextData = this.backend.get( renderContext );
- for ( let i = 0, l = renderContextData.renderObjects.length; i < l; i ++ ) {
- const renderObject = renderContextData.renderObjects[ i ];
- this._nodes.updateBefore( renderObject );
- //
- renderObject.object.modelViewMatrix.multiplyMatrices( camera.matrixWorldInverse, renderObject.object.matrixWorld );
- renderObject.object.normalMatrix.getNormalMatrix( renderObject.object.modelViewMatrix );
- this._nodes.updateForRender( renderObject );
- this._bindings.updateForRender( renderObject );
- this.backend.draw( renderObject, this.info );
- this._nodes.updateAfter( renderObject );
- }
- }
- }
- render( scene, camera ) {
- if ( this._initialized === false ) {
- console.warn( 'THREE.Renderer: .render() called before the backend is initialized. Try using .renderAsync() instead.' );
- return this.renderAsync( scene, camera );
- }
- this._renderScene( scene, camera );
- }
- _getFrameBufferTarget() {
- const { currentColorSpace } = this;
- const useToneMapping = this._renderTarget === null && ( this.toneMapping !== NoToneMapping || this.toneMappingNode !== null );
- const useColorSpace = currentColorSpace !== LinearSRGBColorSpace && currentColorSpace !== NoColorSpace;
- if ( useToneMapping === false && useColorSpace === false ) return null;
- const { width, height } = this.getDrawingBufferSize( _drawingBufferSize );
- const { depth, stencil } = this;
- let frameBufferTarget = this._frameBufferTarget;
- if ( frameBufferTarget === null ) {
- frameBufferTarget = new RenderTarget( width, height, {
- depthBuffer: depth,
- stencilBuffer: stencil,
- type: HalfFloatType, // FloatType
- format: RGBAFormat,
- colorSpace: LinearSRGBColorSpace,
- generateMipmaps: false,
- minFilter: LinearFilter,
- magFilter: LinearFilter,
- samples: this.backend.parameters.antialias ? 4 : 0
- } );
- frameBufferTarget.isPostProcessingRenderTarget = true;
- this._frameBufferTarget = frameBufferTarget;
- }
- frameBufferTarget.depthBuffer = depth;
- frameBufferTarget.stencilBuffer = stencil;
- frameBufferTarget.setSize( width, height );
- frameBufferTarget.viewport.copy( this._viewport );
- frameBufferTarget.scissor.copy( this._scissor );
- frameBufferTarget.viewport.multiplyScalar( this._pixelRatio );
- frameBufferTarget.scissor.multiplyScalar( this._pixelRatio );
- frameBufferTarget.scissorTest = this._scissorTest;
- return frameBufferTarget;
- }
- _renderScene( scene, camera, useFrameBufferTarget = true ) {
- const frameBufferTarget = useFrameBufferTarget ? this._getFrameBufferTarget() : null;
- // preserve render tree
- const nodeFrame = this._nodes.nodeFrame;
- const previousRenderId = nodeFrame.renderId;
- const previousRenderContext = this._currentRenderContext;
- const previousRenderObjectFunction = this._currentRenderObjectFunction;
- //
- const sceneRef = ( scene.isScene === true ) ? scene : _scene;
- const outputRenderTarget = this._renderTarget;
- const activeCubeFace = this._activeCubeFace;
- const activeMipmapLevel = this._activeMipmapLevel;
- //
- let renderTarget;
- if ( frameBufferTarget !== null ) {
- renderTarget = frameBufferTarget;
- this.setRenderTarget( renderTarget );
- } else {
- renderTarget = outputRenderTarget;
- }
- //
- const renderContext = this._renderContexts.get( scene, camera, renderTarget );
- this._currentRenderContext = renderContext;
- this._currentRenderObjectFunction = this._renderObjectFunction || this.renderObject;
- //
- this.info.calls ++;
- this.info.render.calls ++;
- this.info.render.frameCalls ++;
- nodeFrame.renderId = this.info.calls;
- //
- const coordinateSystem = this.coordinateSystem;
- if ( camera.coordinateSystem !== coordinateSystem ) {
- camera.coordinateSystem = coordinateSystem;
- camera.updateProjectionMatrix();
- }
- //
- if ( scene.matrixWorldAutoUpdate === true ) scene.updateMatrixWorld();
- if ( camera.parent === null && camera.matrixWorldAutoUpdate === true ) camera.updateMatrixWorld();
- //
- let viewport = this._viewport;
- let scissor = this._scissor;
- let pixelRatio = this._pixelRatio;
- if ( renderTarget !== null ) {
- viewport = renderTarget.viewport;
- scissor = renderTarget.scissor;
- pixelRatio = 1;
- }
- this.getDrawingBufferSize( _drawingBufferSize );
- _screen.set( 0, 0, _drawingBufferSize.width, _drawingBufferSize.height );
- const minDepth = ( viewport.minDepth === undefined ) ? 0 : viewport.minDepth;
- const maxDepth = ( viewport.maxDepth === undefined ) ? 1 : viewport.maxDepth;
- renderContext.viewportValue.copy( viewport ).multiplyScalar( pixelRatio ).floor();
- renderContext.viewportValue.width >>= activeMipmapLevel;
- renderContext.viewportValue.height >>= activeMipmapLevel;
- renderContext.viewportValue.minDepth = minDepth;
- renderContext.viewportValue.maxDepth = maxDepth;
- renderContext.viewport = renderContext.viewportValue.equals( _screen ) === false;
- renderContext.scissorValue.copy( scissor ).multiplyScalar( pixelRatio ).floor();
- renderContext.scissor = this._scissorTest && renderContext.scissorValue.equals( _screen ) === false;
- renderContext.scissorValue.width >>= activeMipmapLevel;
- renderContext.scissorValue.height >>= activeMipmapLevel;
- if ( ! renderContext.clippingContext ) renderContext.clippingContext = new ClippingContext();
- renderContext.clippingContext.updateGlobal( this, camera );
- //
- sceneRef.onBeforeRender( this, scene, camera, renderTarget );
- //
- _projScreenMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse );
- _frustum.setFromProjectionMatrix( _projScreenMatrix, coordinateSystem );
- const renderList = this._renderLists.get( scene, camera );
- renderList.begin();
- this._projectObject( scene, camera, 0, renderList );
- renderList.finish();
- if ( this.sortObjects === true ) {
- renderList.sort( this._opaqueSort, this._transparentSort );
- }
- //
- if ( renderTarget !== null ) {
- this._textures.updateRenderTarget( renderTarget, activeMipmapLevel );
- const renderTargetData = this._textures.get( renderTarget );
- renderContext.textures = renderTargetData.textures;
- renderContext.depthTexture = renderTargetData.depthTexture;
- renderContext.width = renderTargetData.width;
- renderContext.height = renderTargetData.height;
- renderContext.renderTarget = renderTarget;
- renderContext.depth = renderTarget.depthBuffer;
- renderContext.stencil = renderTarget.stencilBuffer;
- } else {
- renderContext.textures = null;
- renderContext.depthTexture = null;
- renderContext.width = this.domElement.width;
- renderContext.height = this.domElement.height;
- renderContext.depth = this.depth;
- renderContext.stencil = this.stencil;
- }
- renderContext.width >>= activeMipmapLevel;
- renderContext.height >>= activeMipmapLevel;
- renderContext.activeCubeFace = activeCubeFace;
- renderContext.activeMipmapLevel = activeMipmapLevel;
- renderContext.occlusionQueryCount = renderList.occlusionQueryCount;
- //
- this._nodes.updateScene( sceneRef );
- //
- this._background.update( sceneRef, renderList, renderContext );
- //
- this.backend.beginRender( renderContext );
- // process render lists
- const opaqueObjects = renderList.opaque;
- const transparentObjects = renderList.transparent;
- const bundles = renderList.bundles;
- const lightsNode = renderList.lightsNode;
- if ( bundles.length > 0 ) this._renderBundles( bundles, sceneRef, lightsNode );
- if ( opaqueObjects.length > 0 ) this._renderObjects( opaqueObjects, camera, sceneRef, lightsNode );
- if ( transparentObjects.length > 0 ) this._renderObjects( transparentObjects, camera, sceneRef, lightsNode );
- // finish render pass
- this.backend.finishRender( renderContext );
- // restore render tree
- nodeFrame.renderId = previousRenderId;
- this._currentRenderContext = previousRenderContext;
- this._currentRenderObjectFunction = previousRenderObjectFunction;
- //
- if ( frameBufferTarget !== null ) {
- this.setRenderTarget( outputRenderTarget, activeCubeFace, activeMipmapLevel );
- _quad.material.fragmentNode = this._nodes.getOutputNode( renderTarget.texture );
- this._renderScene( _quad, _quad.camera, false );
- }
- //
- sceneRef.onAfterRender( this, scene, camera, renderTarget );
- //
- return renderContext;
- }
- getMaxAnisotropy() {
- return this.backend.getMaxAnisotropy();
- }
- getActiveCubeFace() {
- return this._activeCubeFace;
- }
- getActiveMipmapLevel() {
- return this._activeMipmapLevel;
- }
- async setAnimationLoop( callback ) {
- if ( this._initialized === false ) await this.init();
- this._animation.setAnimationLoop( callback );
- }
- async getArrayBufferAsync( attribute ) {
- return await this.backend.getArrayBufferAsync( attribute );
- }
- getContext() {
- return this.backend.getContext();
- }
- getPixelRatio() {
- return this._pixelRatio;
- }
- getDrawingBufferSize( target ) {
- return target.set( this._width * this._pixelRatio, this._height * this._pixelRatio ).floor();
- }
- getSize( target ) {
- return target.set( this._width, this._height );
- }
- setPixelRatio( value = 1 ) {
- this._pixelRatio = value;
- this.setSize( this._width, this._height, false );
- }
- setDrawingBufferSize( width, height, pixelRatio ) {
- this._width = width;
- this._height = height;
- this._pixelRatio = pixelRatio;
- this.domElement.width = Math.floor( width * pixelRatio );
- this.domElement.height = Math.floor( height * pixelRatio );
- this.setViewport( 0, 0, width, height );
- if ( this._initialized ) this.backend.updateSize();
- }
- setSize( width, height, updateStyle = true ) {
- this._width = width;
- this._height = height;
- this.domElement.width = Math.floor( width * this._pixelRatio );
- this.domElement.height = Math.floor( height * this._pixelRatio );
- if ( updateStyle === true ) {
- this.domElement.style.width = width + 'px';
- this.domElement.style.height = height + 'px';
- }
- this.setViewport( 0, 0, width, height );
- if ( this._initialized ) this.backend.updateSize();
- }
- setOpaqueSort( method ) {
- this._opaqueSort = method;
- }
- setTransparentSort( method ) {
- this._transparentSort = method;
- }
- getScissor( target ) {
- const scissor = this._scissor;
- target.x = scissor.x;
- target.y = scissor.y;
- target.width = scissor.width;
- target.height = scissor.height;
- return target;
- }
- setScissor( x, y, width, height ) {
- const scissor = this._scissor;
- if ( x.isVector4 ) {
- scissor.copy( x );
- } else {
- scissor.set( x, y, width, height );
- }
- }
- getScissorTest() {
- return this._scissorTest;
- }
- setScissorTest( boolean ) {
- this._scissorTest = boolean;
- this.backend.setScissorTest( boolean );
- }
- getViewport( target ) {
- return target.copy( this._viewport );
- }
- setViewport( x, y, width, height, minDepth = 0, maxDepth = 1 ) {
- const viewport = this._viewport;
- if ( x.isVector4 ) {
- viewport.copy( x );
- } else {
- viewport.set( x, y, width, height );
- }
- viewport.minDepth = minDepth;
- viewport.maxDepth = maxDepth;
- }
- getClearColor( target ) {
- return target.copy( this._clearColor );
- }
- setClearColor( color, alpha = 1 ) {
- this._clearColor.set( color );
- this._clearColor.a = alpha;
- }
- getClearAlpha() {
- return this._clearColor.a;
- }
- setClearAlpha( alpha ) {
- this._clearColor.a = alpha;
- }
- getClearDepth() {
- return this._clearDepth;
- }
- setClearDepth( depth ) {
- this._clearDepth = depth;
- }
- getClearStencil() {
- return this._clearStencil;
- }
- setClearStencil( stencil ) {
- this._clearStencil = stencil;
- }
- isOccluded( object ) {
- const renderContext = this._currentRenderContext;
- return renderContext && this.backend.isOccluded( renderContext, object );
- }
- clear( color = true, depth = true, stencil = true ) {
- if ( this._initialized === false ) {
- console.warn( 'THREE.Renderer: .clear() called before the backend is initialized. Try using .clearAsync() instead.' );
- return this.clearAsync( color, depth, stencil );
- }
- const renderTarget = this._renderTarget || this._getFrameBufferTarget();
- let renderTargetData = null;
- if ( renderTarget !== null ) {
- this._textures.updateRenderTarget( renderTarget );
- renderTargetData = this._textures.get( renderTarget );
- }
- this.backend.clear( color, depth, stencil, renderTargetData );
- if ( renderTarget !== null && this._renderTarget === null ) {
- // If a color space transform or tone mapping is required,
- // the clear operation clears the intermediate renderTarget texture, but does not update the screen canvas.
- _quad.material.fragmentNode = this._nodes.getOutputNode( renderTarget.texture );
- this._renderScene( _quad, _quad.camera, false );
- }
- }
- clearColor() {
- return this.clear( true, false, false );
- }
- clearDepth() {
- return this.clear( false, true, false );
- }
- clearStencil() {
- return this.clear( false, false, true );
- }
- async clearAsync( color = true, depth = true, stencil = true ) {
- if ( this._initialized === false ) await this.init();
- this.clear( color, depth, stencil );
- }
- clearColorAsync() {
- return this.clearAsync( true, false, false );
- }
- clearDepthAsync() {
- return this.clearAsync( false, true, false );
- }
- clearStencilAsync() {
- return this.clearAsync( false, false, true );
- }
- get currentColorSpace() {
- const renderTarget = this._renderTarget;
- if ( renderTarget !== null ) {
- const texture = renderTarget.texture;
- return ( Array.isArray( texture ) ? texture[ 0 ] : texture ).colorSpace;
- }
- return this.outputColorSpace;
- }
- dispose() {
- this.info.dispose();
- this._animation.dispose();
- this._objects.dispose();
- this._pipelines.dispose();
- this._nodes.dispose();
- this._bindings.dispose();
- this._renderLists.dispose();
- this._renderContexts.dispose();
- this._textures.dispose();
- this.setRenderTarget( null );
- this.setAnimationLoop( null );
- }
- setRenderTarget( renderTarget, activeCubeFace = 0, activeMipmapLevel = 0 ) {
- this._renderTarget = renderTarget;
- this._activeCubeFace = activeCubeFace;
- this._activeMipmapLevel = activeMipmapLevel;
- }
- getRenderTarget() {
- return this._renderTarget;
- }
- setRenderObjectFunction( renderObjectFunction ) {
- this._renderObjectFunction = renderObjectFunction;
- }
- getRenderObjectFunction() {
- return this._renderObjectFunction;
- }
- async computeAsync( computeNodes ) {
- if ( this._initialized === false ) await this.init();
- const nodeFrame = this._nodes.nodeFrame;
- const previousRenderId = nodeFrame.renderId;
- //
- this.info.calls ++;
- this.info.compute.calls ++;
- this.info.compute.frameCalls ++;
- nodeFrame.renderId = this.info.calls;
- //
- const backend = this.backend;
- const pipelines = this._pipelines;
- const bindings = this._bindings;
- const nodes = this._nodes;
- const computeList = Array.isArray( computeNodes ) ? computeNodes : [ computeNodes ];
- if ( computeList[ 0 ] === undefined || computeList[ 0 ].isComputeNode !== true ) {
- throw new Error( 'THREE.Renderer: .compute() expects a ComputeNode.' );
- }
- backend.beginCompute( computeNodes );
- for ( const computeNode of computeList ) {
- // onInit
- if ( pipelines.has( computeNode ) === false ) {
- const dispose = () => {
- computeNode.removeEventListener( 'dispose', dispose );
- pipelines.delete( computeNode );
- bindings.delete( computeNode );
- nodes.delete( computeNode );
- };
- computeNode.addEventListener( 'dispose', dispose );
- //
- computeNode.onInit( { renderer: this } );
- }
- nodes.updateForCompute( computeNode );
- bindings.updateForCompute( computeNode );
- const computeBindings = bindings.getForCompute( computeNode );
- const computePipeline = pipelines.getForCompute( computeNode, computeBindings );
- backend.compute( computeNodes, computeNode, computeBindings, computePipeline );
- }
- backend.finishCompute( computeNodes );
- await this.backend.resolveTimestampAsync( computeNodes, 'compute' );
- //
- nodeFrame.renderId = previousRenderId;
- }
- async hasFeatureAsync( name ) {
- if ( this._initialized === false ) await this.init();
- return this.backend.hasFeature( name );
- }
- hasFeature( name ) {
- if ( this._initialized === false ) {
- console.warn( 'THREE.Renderer: .hasFeature() called before the backend is initialized. Try using .hasFeatureAsync() instead.' );
- return false;
- }
- return this.backend.hasFeature( name );
- }
- copyFramebufferToTexture( framebufferTexture ) {
- const renderContext = this._currentRenderContext;
- this._textures.updateTexture( framebufferTexture );
- this.backend.copyFramebufferToTexture( framebufferTexture, renderContext );
- }
- copyTextureToTexture( srcTexture, dstTexture, srcRegion = null, dstPosition = null, level = 0 ) {
- this._textures.updateTexture( srcTexture );
- this._textures.updateTexture( dstTexture );
- this.backend.copyTextureToTexture( srcTexture, dstTexture, srcRegion, dstPosition, level );
- }
- readRenderTargetPixelsAsync( renderTarget, x, y, width, height, index = 0 ) {
- return this.backend.copyTextureToBuffer( renderTarget.textures[ index ], x, y, width, height );
- }
- _projectObject( object, camera, groupOrder, renderList ) {
- if ( object.visible === false ) return;
- const visible = object.layers.test( camera.layers );
- if ( visible ) {
- if ( object.isGroup ) {
- groupOrder = object.renderOrder;
- } else if ( object.isLOD ) {
- if ( object.autoUpdate === true ) object.update( camera );
- } else if ( object.isLight ) {
- renderList.pushLight( object );
- } else if ( object.isSprite ) {
- if ( ! object.frustumCulled || _frustum.intersectsSprite( object ) ) {
- if ( this.sortObjects === true ) {
- _vector3.setFromMatrixPosition( object.matrixWorld ).applyMatrix4( _projScreenMatrix );
- }
- const geometry = object.geometry;
- const material = object.material;
- if ( material.visible ) {
- renderList.push( object, geometry, material, groupOrder, _vector3.z, null );
- }
- }
- } else if ( object.isLineLoop ) {
- console.error( 'THREE.Renderer: Objects of type THREE.LineLoop are not supported. Please use THREE.Line or THREE.LineSegments.' );
- } else if ( object.isMesh || object.isLine || object.isPoints ) {
- if ( ! object.frustumCulled || _frustum.intersectsObject( object ) ) {
- const geometry = object.geometry;
- const material = object.material;
- if ( this.sortObjects === true ) {
- if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
- _vector3
- .copy( geometry.boundingSphere.center )
- .applyMatrix4( object.matrixWorld )
- .applyMatrix4( _projScreenMatrix );
- }
- if ( Array.isArray( material ) ) {
- const groups = geometry.groups;
- for ( let i = 0, l = groups.length; i < l; i ++ ) {
- const group = groups[ i ];
- const groupMaterial = material[ group.materialIndex ];
- if ( groupMaterial && groupMaterial.visible ) {
- renderList.push( object, geometry, groupMaterial, groupOrder, _vector3.z, group );
- }
- }
- } else if ( material.visible ) {
- renderList.push( object, geometry, material, groupOrder, _vector3.z, null );
- }
- }
- }
- }
- if ( object.static === true ) {
- const baseRenderList = renderList;
- // replace render list
- renderList = this._renderLists.get( object, camera );
- renderList.begin();
- baseRenderList.pushBundle( {
- object,
- camera,
- renderList,
- } );
- renderList.finish();
- }
- const children = object.children;
- for ( let i = 0, l = children.length; i < l; i ++ ) {
- this._projectObject( children[ i ], camera, groupOrder, renderList );
- }
- }
- _renderBundles( bundles, sceneRef, lightsNode ) {
- for ( const bundle of bundles ) {
- this._renderBundle( bundle, sceneRef, lightsNode );
- }
- }
- _renderObjects( renderList, camera, scene, lightsNode ) {
- // process renderable objects
- for ( let i = 0, il = renderList.length; i < il; i ++ ) {
- const renderItem = renderList[ i ];
- // @TODO: Add support for multiple materials per object. This will require to extract
- // the material from the renderItem object and pass it with its group data to renderObject().
- const { object, geometry, material, group } = renderItem;
- if ( camera.isArrayCamera ) {
- const cameras = camera.cameras;
- for ( let j = 0, jl = cameras.length; j < jl; j ++ ) {
- const camera2 = cameras[ j ];
- if ( object.layers.test( camera2.layers ) ) {
- const vp = camera2.viewport;
- const minDepth = ( vp.minDepth === undefined ) ? 0 : vp.minDepth;
- const maxDepth = ( vp.maxDepth === undefined ) ? 1 : vp.maxDepth;
- const viewportValue = this._currentRenderContext.viewportValue;
- viewportValue.copy( vp ).multiplyScalar( this._pixelRatio ).floor();
- viewportValue.minDepth = minDepth;
- viewportValue.maxDepth = maxDepth;
- this.backend.updateViewport( this._currentRenderContext );
- this._currentRenderObjectFunction( object, scene, camera2, geometry, material, group, lightsNode );
- }
- }
- } else {
- this._currentRenderObjectFunction( object, scene, camera, geometry, material, group, lightsNode );
- }
- }
- }
- renderObject( object, scene, camera, geometry, material, group, lightsNode ) {
- let overridePositionNode;
- let overrideFragmentNode;
- let overrideDepthNode;
- //
- object.onBeforeRender( this, scene, camera, geometry, material, group );
- //
- if ( scene.overrideMaterial !== null ) {
- const overrideMaterial = scene.overrideMaterial;
- if ( material.positionNode && material.positionNode.isNode ) {
- overridePositionNode = overrideMaterial.positionNode;
- overrideMaterial.positionNode = material.positionNode;
- }
- if ( overrideMaterial.isShadowNodeMaterial ) {
- overrideMaterial.side = material.shadowSide === null ? material.side : material.shadowSide;
- if ( material.depthNode && material.depthNode.isNode ) {
- overrideDepthNode = overrideMaterial.depthNode;
- overrideMaterial.depthNode = material.depthNode;
- }
- if ( material.shadowNode && material.shadowNode.isNode ) {
- overrideFragmentNode = overrideMaterial.fragmentNode;
- overrideMaterial.fragmentNode = material.shadowNode;
- }
- if ( this.localClippingEnabled ) {
- if ( material.clipShadows ) {
- if ( overrideMaterial.clippingPlanes !== material.clippingPlanes ) {
- overrideMaterial.clippingPlanes = material.clippingPlanes;
- overrideMaterial.needsUpdate = true;
- }
- if ( overrideMaterial.clipIntersection !== material.clipIntersection ) {
- overrideMaterial.clipIntersection = material.clipIntersection;
- }
- } else if ( Array.isArray( overrideMaterial.clippingPlanes ) ) {
- overrideMaterial.clippingPlanes = null;
- overrideMaterial.needsUpdate = true;
- }
- }
- }
- material = overrideMaterial;
- }
- //
- if ( material.transparent === true && material.side === DoubleSide && material.forceSinglePass === false ) {
- material.side = BackSide;
- this._handleObjectFunction( object, material, scene, camera, lightsNode, group, 'backSide' ); // create backSide pass id
- material.side = FrontSide;
- this._handleObjectFunction( object, material, scene, camera, lightsNode, group ); // use default pass id
- material.side = DoubleSide;
- } else {
- this._handleObjectFunction( object, material, scene, camera, lightsNode, group );
- }
- //
- if ( overridePositionNode !== undefined ) {
- scene.overrideMaterial.positionNode = overridePositionNode;
- }
- if ( overrideDepthNode !== undefined ) {
- scene.overrideMaterial.depthNode = overrideDepthNode;
- }
- if ( overrideFragmentNode !== undefined ) {
- scene.overrideMaterial.fragmentNode = overrideFragmentNode;
- }
- //
- object.onAfterRender( this, scene, camera, geometry, material, group );
- }
- _renderObjectDirect( object, material, scene, camera, lightsNode, group, passId ) {
- const renderObject = this._objects.get( object, material, scene, camera, lightsNode, this._currentRenderContext, passId );
- renderObject.drawRange = group || object.geometry.drawRange;
- //
- this._nodes.updateBefore( renderObject );
- //
- object.modelViewMatrix.multiplyMatrices( camera.matrixWorldInverse, object.matrixWorld );
- object.normalMatrix.getNormalMatrix( object.modelViewMatrix );
- //
- this._nodes.updateForRender( renderObject );
- this._geometries.updateForRender( renderObject );
- this._bindings.updateForRender( renderObject );
- this._pipelines.updateForRender( renderObject );
- //
- if ( this._currentRenderBundle !== null && this._currentRenderBundle.needsUpdate === true ) {
- const renderObjectData = this.backend.get( renderObject );
- renderObjectData.bundleEncoder = undefined;
- renderObjectData.lastPipelineGPU = undefined;
- }
- this.backend.draw( renderObject, this.info );
- if ( this._currentRenderBundle !== null ) {
- const renderContextData = this.backend.get( this._currentRenderContext );
- renderContextData.renderObjects.push( renderObject );
- }
- this._nodes.updateAfter( renderObject );
- }
- _createObjectPipeline( object, material, scene, camera, lightsNode, passId ) {
- const renderObject = this._objects.get( object, material, scene, camera, lightsNode, this._currentRenderContext, passId );
- //
- this._nodes.updateBefore( renderObject );
- //
- this._nodes.updateForRender( renderObject );
- this._geometries.updateForRender( renderObject );
- this._bindings.updateForRender( renderObject );
- this._pipelines.getForRender( renderObject, this._compilationPromises );
- this._nodes.updateAfter( renderObject );
- }
- get compute() {
- return this.computeAsync;
- }
- get compile() {
- return this.compileAsync;
- }
- }
- export default Renderer;
|