Renderer.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  1. import Animation from './Animation.js';
  2. import RenderObjects from './RenderObjects.js';
  3. import Attributes from './Attributes.js';
  4. import Geometries from './Geometries.js';
  5. import Info from './Info.js';
  6. import Pipelines from './Pipelines.js';
  7. import Bindings from './Bindings.js';
  8. import RenderLists from './RenderLists.js';
  9. import RenderContexts from './RenderContexts.js';
  10. import Textures from './Textures.js';
  11. import Background from './Background.js';
  12. import Nodes from './nodes/Nodes.js';
  13. import { Frustum, Matrix4, Vector2, Vector3, Vector4, Color, DoubleSide, BackSide, FrontSide, SRGBColorSpace, NoToneMapping } from 'three';
  14. const _drawingBufferSize = new Vector2();
  15. const _screen = new Vector4();
  16. const _frustum = new Frustum();
  17. const _projScreenMatrix = new Matrix4();
  18. const _vector3 = new Vector3();
  19. class Renderer {
  20. constructor( backend ) {
  21. this.isRenderer = true;
  22. // public
  23. this.domElement = backend.getDomElement();
  24. this.backend = backend;
  25. this.autoClear = true;
  26. this.autoClearColor = true;
  27. this.autoClearDepth = true;
  28. this.autoClearStencil = true;
  29. this.outputColorSpace = SRGBColorSpace;
  30. this.toneMapping = NoToneMapping;
  31. this.toneMappingExposure = 1.0;
  32. this.sortObjects = true;
  33. this.depth = true;
  34. this.stencil = true;
  35. // internals
  36. this._pixelRatio = 1;
  37. this._width = this.domElement.width;
  38. this._height = this.domElement.height;
  39. this._viewport = new Vector4( 0, 0, this._width, this._height );
  40. this._scissor = new Vector4( 0, 0, this._width, this._height );
  41. this._scissorTest = false;
  42. this._info = null;
  43. this._properties = null;
  44. this._attributes = null;
  45. this._geometries = null;
  46. this._nodes = null;
  47. this._bindings = null;
  48. this._objects = null;
  49. this._pipelines = null;
  50. this._renderLists = null;
  51. this._renderContexts = null;
  52. this._textures = null;
  53. this._background = null;
  54. this._animation = new Animation();
  55. this._currentRenderContext = null;
  56. this._lastRenderContext = null;
  57. this._opaqueSort = null;
  58. this._transparentSort = null;
  59. this._clearAlpha = 1;
  60. this._clearColor = new Color( 0x000000 );
  61. this._clearDepth = 1;
  62. this._clearStencil = 0;
  63. this._renderTarget = null;
  64. this._currentActiveCubeFace = 0;
  65. this._initialized = false;
  66. this._initPromise = null;
  67. // backwards compatibility
  68. this.shadowMap = {
  69. enabled: false,
  70. type: null
  71. };
  72. this.xr = {
  73. enabled: false
  74. };
  75. }
  76. async init() {
  77. if ( this._initialized ) {
  78. throw new Error( 'Renderer: Backend has already been initialized.' );
  79. }
  80. if ( this._initPromise !== null ) {
  81. return this._initPromise;
  82. }
  83. this._initPromise = new Promise( async ( resolve, reject ) => {
  84. const backend = this.backend;
  85. try {
  86. await backend.init( this );
  87. } catch ( error ) {
  88. reject( error );
  89. return;
  90. }
  91. this._info = new Info();
  92. this._nodes = new Nodes( this, backend );
  93. this._attributes = new Attributes( backend );
  94. this._background = new Background( this, this._nodes );
  95. this._geometries = new Geometries( this._attributes, this._info );
  96. this._textures = new Textures( backend, this._info );
  97. this._pipelines = new Pipelines( backend, this._nodes );
  98. this._bindings = new Bindings( backend, this._nodes, this._textures, this._attributes, this._pipelines, this._info );
  99. this._objects = new RenderObjects( this, this._nodes, this._geometries, this._pipelines, this._bindings, this._info );
  100. this._renderLists = new RenderLists();
  101. this._renderContexts = new RenderContexts();
  102. //
  103. this._animation.setNodes( this._nodes );
  104. this._animation.start();
  105. this._initialized = true;
  106. resolve();
  107. } );
  108. return this._initPromise;
  109. }
  110. get coordinateSystem() {
  111. return this.backend.coordinateSystem;
  112. }
  113. async compile( /*scene, camera*/ ) {
  114. console.warn( 'THREE.Renderer: .compile() is not implemented yet.' );
  115. }
  116. async render( scene, camera ) {
  117. if ( this._initialized === false ) await this.init();
  118. // preserve render tree
  119. const nodeFrame = this._nodes.nodeFrame;
  120. const previousRenderId = nodeFrame.renderId;
  121. const previousRenderState = this._currentRenderContext;
  122. //
  123. const renderTarget = this._renderTarget;
  124. const renderContext = this._renderContexts.get( scene, camera, renderTarget );
  125. const activeCubeFace = this._activeCubeFace;
  126. this._currentRenderContext = renderContext;
  127. nodeFrame.renderId ++;
  128. //
  129. const coordinateSystem = this.coordinateSystem;
  130. if ( camera.coordinateSystem !== coordinateSystem ) {
  131. camera.coordinateSystem = coordinateSystem;
  132. camera.updateProjectionMatrix();
  133. }
  134. //
  135. if ( this._animation.isAnimating === false ) nodeFrame.update();
  136. if ( scene.matrixWorldAutoUpdate === true ) scene.updateMatrixWorld();
  137. if ( camera.parent === null && camera.matrixWorldAutoUpdate === true ) camera.updateMatrixWorld();
  138. if ( this._info.autoReset === true ) this._info.reset();
  139. this._info.render.frame ++;
  140. //
  141. let viewport = this._viewport;
  142. let scissor = this._scissor;
  143. let pixelRatio = this._pixelRatio;
  144. if ( renderTarget !== null ) {
  145. viewport = renderTarget.viewport;
  146. scissor = renderTarget.scissor;
  147. pixelRatio = 1;
  148. }
  149. this.getDrawingBufferSize( _drawingBufferSize );
  150. _screen.set( 0, 0, _drawingBufferSize.width, _drawingBufferSize.height );
  151. const minDepth = ( viewport.minDepth === undefined ) ? 0 : viewport.minDepth;
  152. const maxDepth = ( viewport.maxDepth === undefined ) ? 1 : viewport.maxDepth;
  153. renderContext.viewportValue.copy( viewport ).multiplyScalar( pixelRatio ).floor();
  154. renderContext.viewportValue.minDepth = minDepth;
  155. renderContext.viewportValue.maxDepth = maxDepth;
  156. renderContext.viewport = renderContext.viewportValue.equals( _screen ) === false;
  157. renderContext.scissorValue.copy( scissor ).multiplyScalar( pixelRatio ).floor();
  158. renderContext.scissor = this._scissorTest && renderContext.scissorValue.equals( _screen ) === false;
  159. renderContext.depth = this.depth;
  160. renderContext.stencil = this.stencil;
  161. //
  162. _projScreenMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse );
  163. _frustum.setFromProjectionMatrix( _projScreenMatrix, coordinateSystem );
  164. const renderList = this._renderLists.get( scene, camera );
  165. renderList.init();
  166. this._projectObject( scene, camera, 0, renderList );
  167. renderList.finish();
  168. if ( this.sortObjects === true ) {
  169. renderList.sort( this._opaqueSort, this._transparentSort );
  170. }
  171. //
  172. if ( renderTarget !== null ) {
  173. this._textures.updateRenderTarget( renderTarget );
  174. const renderTargetData = this._textures.get( renderTarget );
  175. renderContext.texture = renderTargetData.texture;
  176. renderContext.depthTexture = renderTargetData.depthTexture;
  177. } else {
  178. renderContext.texture = null;
  179. renderContext.depthTexture = null;
  180. }
  181. renderContext.activeCubeFace = activeCubeFace;
  182. //
  183. this._nodes.updateScene( scene );
  184. //
  185. this._background.update( scene, renderList, renderContext );
  186. //
  187. this.backend.beginRender( renderContext );
  188. // process render lists
  189. const opaqueObjects = renderList.opaque;
  190. const transparentObjects = renderList.transparent;
  191. const lightsNode = renderList.lightsNode;
  192. if ( opaqueObjects.length > 0 ) this._renderObjects( opaqueObjects, camera, scene, lightsNode );
  193. if ( transparentObjects.length > 0 ) this._renderObjects( transparentObjects, camera, scene, lightsNode );
  194. // finish render pass
  195. this.backend.finishRender( renderContext );
  196. // restore render tree
  197. nodeFrame.renderId = previousRenderId;
  198. this._currentRenderContext = previousRenderState;
  199. this._lastRenderContext = renderContext;
  200. }
  201. setAnimationLoop( callback ) {
  202. if ( this._initialized === false ) this.init();
  203. const animation = this._animation;
  204. animation.setAnimationLoop( callback );
  205. ( callback === null ) ? animation.stop() : animation.start();
  206. }
  207. getArrayBuffer( attribute ) { // @deprecated, r155
  208. console.warn( 'THREE.Renderer: getArrayBuffer() is deprecated. Use getArrayBufferAsync() instead.' );
  209. return this.getArrayBufferAsync( attribute );
  210. }
  211. async getArrayBufferAsync( attribute ) {
  212. return await this.backend.getArrayBufferAsync( attribute );
  213. }
  214. getContext() {
  215. return this._context;
  216. }
  217. getPixelRatio() {
  218. return this._pixelRatio;
  219. }
  220. getDrawingBufferSize( target ) {
  221. return target.set( this._width * this._pixelRatio, this._height * this._pixelRatio ).floor();
  222. }
  223. getSize( target ) {
  224. return target.set( this._width, this._height );
  225. }
  226. setPixelRatio( value = 1 ) {
  227. this._pixelRatio = value;
  228. this.setSize( this._width, this._height, false );
  229. }
  230. setDrawingBufferSize( width, height, pixelRatio ) {
  231. this._width = width;
  232. this._height = height;
  233. this._pixelRatio = pixelRatio;
  234. this.domElement.width = Math.floor( width * pixelRatio );
  235. this.domElement.height = Math.floor( height * pixelRatio );
  236. this.setViewport( 0, 0, width, height );
  237. if ( this._initialized ) this.backend.updateSize();
  238. }
  239. setSize( width, height, updateStyle = true ) {
  240. this._width = width;
  241. this._height = height;
  242. this.domElement.width = Math.floor( width * this._pixelRatio );
  243. this.domElement.height = Math.floor( height * this._pixelRatio );
  244. if ( updateStyle === true ) {
  245. this.domElement.style.width = width + 'px';
  246. this.domElement.style.height = height + 'px';
  247. }
  248. this.setViewport( 0, 0, width, height );
  249. if ( this._initialized ) this.backend.updateSize();
  250. }
  251. setOpaqueSort( method ) {
  252. this._opaqueSort = method;
  253. }
  254. setTransparentSort( method ) {
  255. this._transparentSort = method;
  256. }
  257. getScissor( target ) {
  258. const scissor = this._scissor;
  259. target.x = scissor.x;
  260. target.y = scissor.y;
  261. target.width = scissor.width;
  262. target.height = scissor.height;
  263. return target;
  264. }
  265. setScissor( x, y, width, height ) {
  266. const scissor = this._scissor;
  267. if ( x.isVector4 ) {
  268. scissor.copy( x );
  269. } else {
  270. scissor.set( x, y, width, height );
  271. }
  272. }
  273. getScissorTest() {
  274. return this._scissorTest;
  275. }
  276. setScissorTest( boolean ) {
  277. this._scissorTest = boolean;
  278. }
  279. getViewport( target ) {
  280. return target.copy( this._viewport );
  281. }
  282. setViewport( x, y, width, height, minDepth = 0, maxDepth = 1 ) {
  283. const viewport = this._viewport;
  284. if ( x.isVector4 ) {
  285. viewport.copy( x );
  286. } else {
  287. viewport.set( x, y, width, height );
  288. }
  289. viewport.minDepth = minDepth;
  290. viewport.maxDepth = maxDepth;
  291. }
  292. getClearColor( target ) {
  293. return target.copy( this._clearColor );
  294. }
  295. setClearColor( color, alpha = 1 ) {
  296. this._clearColor.set( color );
  297. this._clearAlpha = alpha;
  298. }
  299. getClearAlpha() {
  300. return this._clearAlpha;
  301. }
  302. setClearAlpha( alpha ) {
  303. this._clearAlpha = alpha;
  304. }
  305. getClearDepth() {
  306. return this._clearDepth;
  307. }
  308. setClearDepth( depth ) {
  309. this._clearDepth = depth;
  310. }
  311. getClearStencil() {
  312. return this._clearStencil;
  313. }
  314. setClearStencil( stencil ) {
  315. this._clearStencil = stencil;
  316. }
  317. clear( color = true, depth = true, stencil = true ) {
  318. const renderContext = this._currentRenderContext || this._lastRenderContext;
  319. if ( renderContext ) this.backend.clear( renderContext, color, depth, stencil );
  320. }
  321. clearColor() {
  322. this.clear( true, false, false );
  323. }
  324. clearDepth() {
  325. this.clear( false, true, false );
  326. }
  327. clearStencil() {
  328. this.clear( false, false, true );
  329. }
  330. dispose() {
  331. this._objects.dispose();
  332. this._properties.dispose();
  333. this._pipelines.dispose();
  334. this._nodes.dispose();
  335. this._bindings.dispose();
  336. this._info.dispose();
  337. this._renderLists.dispose();
  338. this._renderContexts.dispose();
  339. this._textures.dispose();
  340. this.setRenderTarget( null );
  341. this.setAnimationLoop( null );
  342. }
  343. setRenderTarget( renderTarget, activeCubeFace = 0 ) {
  344. this._renderTarget = renderTarget;
  345. this._activeCubeFace = activeCubeFace;
  346. }
  347. async compute( computeNodes ) {
  348. if ( this._initialized === false ) await this.init();
  349. const backend = this.backend;
  350. const pipelines = this._pipelines;
  351. const bindings = this._bindings;
  352. const nodes = this._nodes;
  353. const computeList = Array.isArray( computeNodes ) ? computeNodes : [ computeNodes ];
  354. backend.beginCompute( computeNodes );
  355. for ( const computeNode of computeList ) {
  356. // onInit
  357. if ( pipelines.has( computeNode ) === false ) {
  358. const dispose = () => {
  359. computeNode.removeEventListener( 'dispose', dispose );
  360. pipelines.delete( computeNode );
  361. bindings.delete( computeNode );
  362. nodes.delete( computeNode );
  363. };
  364. computeNode.addEventListener( 'dispose', dispose );
  365. //
  366. computeNode.onInit( { renderer: this } );
  367. }
  368. nodes.updateForCompute( computeNode );
  369. bindings.updateForCompute( computeNode );
  370. const computePipeline = pipelines.getForCompute( computeNode );
  371. const computeBindings = bindings.getForCompute( computeNode );
  372. backend.compute( computeNodes, computeNode, computeBindings, computePipeline );
  373. }
  374. backend.finishCompute( computeNodes );
  375. }
  376. getRenderTarget() {
  377. return this._renderTarget;
  378. }
  379. hasFeature( name ) {
  380. return this.backend.hasFeature( name );
  381. }
  382. copyFramebufferToTexture( framebufferTexture ) {
  383. const renderContext = this._currentRenderContext || this._lastRenderContext;
  384. this._textures.updateTexture( framebufferTexture );
  385. this.backend.copyFramebufferToTexture( framebufferTexture, renderContext );
  386. }
  387. readRenderTargetPixelsAsync( renderTarget, x, y, width, height ) {
  388. return this.backend.copyTextureToBuffer( renderTarget.texture, x, y, width, height );
  389. }
  390. _projectObject( object, camera, groupOrder, renderList ) {
  391. if ( object.visible === false ) return;
  392. const visible = object.layers.test( camera.layers );
  393. if ( visible ) {
  394. if ( object.isGroup ) {
  395. groupOrder = object.renderOrder;
  396. } else if ( object.isLOD ) {
  397. if ( object.autoUpdate === true ) object.update( camera );
  398. } else if ( object.isLight ) {
  399. renderList.pushLight( object );
  400. } else if ( object.isSprite ) {
  401. if ( ! object.frustumCulled || _frustum.intersectsSprite( object ) ) {
  402. if ( this.sortObjects === true ) {
  403. _vector3.setFromMatrixPosition( object.matrixWorld ).applyMatrix4( _projScreenMatrix );
  404. }
  405. const geometry = object.geometry;
  406. const material = object.material;
  407. if ( material.visible ) {
  408. renderList.push( object, geometry, material, groupOrder, _vector3.z, null );
  409. }
  410. }
  411. } else if ( object.isLineLoop ) {
  412. console.error( 'THREE.Renderer: Objects of type THREE.LineLoop are not supported. Please use THREE.Line or THREE.LineSegments.' );
  413. } else if ( object.isMesh || object.isLine || object.isPoints ) {
  414. if ( ! object.frustumCulled || _frustum.intersectsObject( object ) ) {
  415. const geometry = object.geometry;
  416. const material = object.material;
  417. if ( this.sortObjects === true ) {
  418. if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
  419. _vector3
  420. .copy( geometry.boundingSphere.center )
  421. .applyMatrix4( object.matrixWorld )
  422. .applyMatrix4( _projScreenMatrix );
  423. }
  424. if ( Array.isArray( material ) ) {
  425. const groups = geometry.groups;
  426. for ( let i = 0, l = groups.length; i < l; i ++ ) {
  427. const group = groups[ i ];
  428. const groupMaterial = material[ group.materialIndex ];
  429. if ( groupMaterial && groupMaterial.visible ) {
  430. renderList.push( object, geometry, groupMaterial, groupOrder, _vector3.z, group );
  431. }
  432. }
  433. } else if ( material.visible ) {
  434. renderList.push( object, geometry, material, groupOrder, _vector3.z, null );
  435. }
  436. }
  437. }
  438. }
  439. const children = object.children;
  440. for ( let i = 0, l = children.length; i < l; i ++ ) {
  441. this._projectObject( children[ i ], camera, groupOrder, renderList );
  442. }
  443. }
  444. _renderObjects( renderList, camera, scene, lightsNode ) {
  445. // process renderable objects
  446. for ( let i = 0, il = renderList.length; i < il; i ++ ) {
  447. const renderItem = renderList[ i ];
  448. // @TODO: Add support for multiple materials per object. This will require to extract
  449. // the material from the renderItem object and pass it with its group data to _renderObject().
  450. const { object, geometry, material, group } = renderItem;
  451. if ( camera.isArrayCamera ) {
  452. const cameras = camera.cameras;
  453. for ( let j = 0, jl = cameras.length; j < jl; j ++ ) {
  454. const camera2 = cameras[ j ];
  455. if ( object.layers.test( camera2.layers ) ) {
  456. const vp = camera2.viewport;
  457. const minDepth = ( vp.minDepth === undefined ) ? 0 : vp.minDepth;
  458. const maxDepth = ( vp.maxDepth === undefined ) ? 1 : vp.maxDepth;
  459. const viewportValue = this._currentRenderContext.viewportValue;
  460. viewportValue.copy( vp ).multiplyScalar( this._pixelRatio ).floor();
  461. viewportValue.minDepth = minDepth;
  462. viewportValue.maxDepth = maxDepth;
  463. this.backend.updateViewport( this._currentRenderContext );
  464. this._renderObject( object, scene, camera2, geometry, material, group, lightsNode );
  465. }
  466. }
  467. } else {
  468. this._renderObject( object, scene, camera, geometry, material, group, lightsNode );
  469. }
  470. }
  471. }
  472. _renderObject( object, scene, camera, geometry, material, group, lightsNode ) {
  473. material = scene.overrideMaterial !== null ? scene.overrideMaterial : material;
  474. //
  475. object.onBeforeRender( this, scene, camera, geometry, material, group );
  476. //
  477. const renderObject = this._objects.get( object, material, scene, camera, lightsNode, this._currentRenderContext );
  478. this._nodes.updateBefore( renderObject );
  479. //
  480. object.modelViewMatrix.multiplyMatrices( camera.matrixWorldInverse, object.matrixWorld );
  481. object.normalMatrix.getNormalMatrix( object.modelViewMatrix );
  482. //
  483. material.onBeforeRender( this, scene, camera, geometry, material, group );
  484. //
  485. if ( material.transparent === true && material.side === DoubleSide && material.forceSinglePass === false ) {
  486. material.side = BackSide;
  487. this._renderObjectDirect( object, scene, camera, geometry, material, group, lightsNode, 'backSide' ); // create backSide pass id
  488. material.side = FrontSide;
  489. this._renderObjectDirect( object, scene, camera, geometry, material, group, lightsNode ); // use default pass id
  490. material.side = DoubleSide;
  491. } else {
  492. this._renderObjectDirect( object, scene, camera, geometry, material, group, lightsNode );
  493. }
  494. //
  495. object.onAfterRender( this, scene, camera, geometry, material, group );
  496. }
  497. _renderObjectDirect( object, scene, camera, geometry, material, group, lightsNode, passId ) {
  498. //
  499. const renderObject = this._objects.get( object, material, scene, camera, lightsNode, this._currentRenderContext, passId );
  500. //
  501. this._nodes.updateForRender( renderObject );
  502. this._geometries.updateForRender( renderObject );
  503. this._bindings.updateForRender( renderObject );
  504. //
  505. this.backend.draw( renderObject, this._info );
  506. }
  507. }
  508. export default Renderer;