123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931 |
- 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 { Scene, Frustum, Matrix4, Vector2, Vector3, Vector4, Color, DoubleSide, BackSide, FrontSide, SRGBColorSpace, NoToneMapping } from 'three';
- const _scene = new Scene();
- const _drawingBufferSize = new Vector2();
- const _screen = new Vector4();
- const _frustum = new Frustum();
- const _projScreenMatrix = new Matrix4();
- const _vector3 = new Vector3();
- class Renderer {
- constructor( backend ) {
- this.isRenderer = true;
- // public
- this.domElement = backend.getDomElement();
- this.backend = backend;
- this.autoClear = true;
- this.autoClearColor = true;
- this.autoClearDepth = true;
- this.autoClearStencil = true;
- this.outputColorSpace = SRGBColorSpace;
- this.toneMapping = NoToneMapping;
- this.toneMappingExposure = 1.0;
- this.sortObjects = true;
- this.depth = true;
- this.stencil = true;
- this.info = new Info();
- // 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._properties = null;
- this._attributes = null;
- this._geometries = null;
- this._nodes = null;
- this._bindings = null;
- this._objects = null;
- this._pipelines = null;
- this._renderLists = null;
- this._renderContexts = null;
- this._textures = null;
- this._background = null;
- this._animation = new Animation();
- this._currentRenderContext = null;
- this._lastRenderContext = null;
- this._opaqueSort = null;
- this._transparentSort = null;
- this._clearAlpha = 1;
- this._clearColor = new Color( 0x000000 );
- this._clearDepth = 1;
- this._clearStencil = 0;
- this._renderTarget = null;
- this._activeCubeFace = 0;
- this._activeMipmapLevel = 0;
- this._initialized = false;
- this._initPromise = null;
- // backwards compatibility
- this.shadowMap = {
- enabled: false,
- type: null
- };
- this.xr = {
- enabled: false
- };
- }
- 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._attributes = new Attributes( backend );
- this._background = new Background( this, this._nodes );
- this._geometries = new Geometries( this._attributes, this.info );
- this._textures = new Textures( 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._renderContexts = new RenderContexts();
- //
- this._animation.setNodes( this._nodes );
- this._animation.start();
- this._initialized = true;
- resolve();
- } );
- return this._initPromise;
- }
- get coordinateSystem() {
- return this.backend.coordinateSystem;
- }
- async compile( /*scene, camera*/ ) {
- console.warn( 'THREE.Renderer: .compile() is not implemented yet.' );
- }
- async render( scene, camera ) {
- if ( this._initialized === false ) await this.init();
- // preserve render tree
- const nodeFrame = this._nodes.nodeFrame;
- const previousRenderId = nodeFrame.renderId;
- const previousRenderState = this._currentRenderContext;
- //
- const sceneRef = ( scene.isScene === true ) ? scene : _scene;
- const renderTarget = this._renderTarget;
- const renderContext = this._renderContexts.get( scene, camera, renderTarget );
- const activeCubeFace = this._activeCubeFace;
- const activeMipmapLevel = this._activeMipmapLevel;
- this._currentRenderContext = renderContext;
- nodeFrame.renderId ++;
- //
- const coordinateSystem = this.coordinateSystem;
- if ( camera.coordinateSystem !== coordinateSystem ) {
- camera.coordinateSystem = coordinateSystem;
- camera.updateProjectionMatrix();
- }
- //
- if ( this._animation.isAnimating === false ) nodeFrame.update();
- if ( scene.matrixWorldAutoUpdate === true ) scene.updateMatrixWorld();
- if ( camera.parent === null && camera.matrixWorldAutoUpdate === true ) camera.updateMatrixWorld();
- if ( this.info.autoReset === true ) this.info.reset();
- this.info.render.frame ++;
- //
- 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;
- renderContext.depth = this.depth;
- renderContext.stencil = this.stencil;
- //
- 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;
- } else {
- renderContext.textures = null;
- renderContext.depthTexture = null;
- renderContext.width = this.domElement.width;
- renderContext.height = this.domElement.height;
- }
- 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 lightsNode = renderList.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 = previousRenderState;
- this._lastRenderContext = renderContext;
- //
- sceneRef.onAfterRender( this, scene, camera, renderTarget );
- }
- getActiveCubeFace() {
- return this._activeCubeFace;
- }
- getActiveMipmapLevel() {
- return this._activeMipmapLevel;
- }
- setAnimationLoop( callback ) {
- if ( this._initialized === false ) this.init();
- const animation = this._animation;
- animation.setAnimationLoop( callback );
- ( callback === null ) ? animation.stop() : animation.start();
- }
- getArrayBuffer( attribute ) { // @deprecated, r155
- console.warn( 'THREE.Renderer: getArrayBuffer() is deprecated. Use getArrayBufferAsync() instead.' );
- return this.getArrayBufferAsync( attribute );
- }
- async getArrayBufferAsync( attribute ) {
- return await this.backend.getArrayBufferAsync( attribute );
- }
- getContext() {
- return this._context;
- }
- 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;
- }
- 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._clearAlpha = alpha;
- }
- getClearAlpha() {
- return this._clearAlpha;
- }
- setClearAlpha( alpha ) {
- this._clearAlpha = 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 || this._lastRenderContext;
- return renderContext && this.backend.isOccluded( renderContext, object );
- }
- clear( color = true, depth = true, stencil = true ) {
- const renderContext = this._currentRenderContext || this._lastRenderContext;
- if ( renderContext ) this.backend.clear( renderContext, color, depth, stencil );
- }
- clearColor() {
- this.clear( true, false, false );
- }
- clearDepth() {
- this.clear( false, true, false );
- }
- clearStencil() {
- this.clear( false, false, true );
- }
- dispose() {
- this.info.dispose();
- this._objects.dispose();
- this._properties.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;
- }
- async compute( computeNodes ) {
- if ( this._initialized === false ) await this.init();
- const backend = this.backend;
- const pipelines = this._pipelines;
- const bindings = this._bindings;
- const nodes = this._nodes;
- const computeList = Array.isArray( computeNodes ) ? computeNodes : [ computeNodes ];
- 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 );
- }
- hasFeature( name ) {
- return this.backend.hasFeature( name );
- }
- copyFramebufferToTexture( framebufferTexture ) {
- const renderContext = this._currentRenderContext || this._lastRenderContext;
- this._textures.updateTexture( framebufferTexture );
- this.backend.copyFramebufferToTexture( framebufferTexture, renderContext );
- }
- readRenderTargetPixelsAsync( renderTarget, x, y, width, height ) {
- return this.backend.copyTextureToBuffer( renderTarget.texture, 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 );
- }
- }
- }
- }
- const children = object.children;
- for ( let i = 0, l = children.length; i < l; i ++ ) {
- this._projectObject( children[ i ], camera, groupOrder, renderList );
- }
- }
- _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._renderObject( object, scene, camera2, geometry, material, group, lightsNode );
- }
- }
- } else {
- this._renderObject( object, scene, camera, geometry, material, group, lightsNode );
- }
- }
- }
- _renderObject( object, scene, camera, geometry, material, group, lightsNode ) {
- material = scene.overrideMaterial !== null ? scene.overrideMaterial : material;
- //
- object.onBeforeRender( this, scene, camera, geometry, material, group );
- material.onBeforeRender( this, scene, camera, geometry, material, group );
- //
- if ( material.transparent === true && material.side === DoubleSide && material.forceSinglePass === false ) {
- material.side = BackSide;
- this._renderObjectDirect( object, material, scene, camera, lightsNode, 'backSide' ); // create backSide pass id
- material.side = FrontSide;
- this._renderObjectDirect( object, material, scene, camera, lightsNode ); // use default pass id
- material.side = DoubleSide;
- } else {
- this._renderObjectDirect( object, material, scene, camera, lightsNode );
- }
- //
- object.onAfterRender( this, scene, camera, geometry, material, group );
- }
- _renderObjectDirect( object, material, scene, camera, lightsNode, passId ) {
- const renderObject = this._objects.get( object, material, scene, camera, lightsNode, this._currentRenderContext, passId );
- //
- 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 );
- //
- this.backend.draw( renderObject, this.info );
- }
- }
- export default Renderer;
|