Renderer.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  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._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. async getArrayBuffer( attribute ) {
  208. return await this.backend.getArrayBuffer( attribute );
  209. }
  210. getContext() {
  211. return this._context;
  212. }
  213. getPixelRatio() {
  214. return this._pixelRatio;
  215. }
  216. getDrawingBufferSize( target ) {
  217. return target.set( this._width * this._pixelRatio, this._height * this._pixelRatio ).floor();
  218. }
  219. getSize( target ) {
  220. return target.set( this._width, this._height );
  221. }
  222. setPixelRatio( value = 1 ) {
  223. this._pixelRatio = value;
  224. this.setSize( this._width, this._height, false );
  225. }
  226. setDrawingBufferSize( width, height, pixelRatio ) {
  227. this._width = width;
  228. this._height = height;
  229. this._pixelRatio = pixelRatio;
  230. this.domElement.width = Math.floor( width * pixelRatio );
  231. this.domElement.height = Math.floor( height * pixelRatio );
  232. this.setViewport( 0, 0, width, height );
  233. if ( this._initialized ) this.backend.updateSize();
  234. }
  235. setSize( width, height, updateStyle = true ) {
  236. this._width = width;
  237. this._height = height;
  238. this.domElement.width = Math.floor( width * this._pixelRatio );
  239. this.domElement.height = Math.floor( height * this._pixelRatio );
  240. if ( updateStyle === true ) {
  241. this.domElement.style.width = width + 'px';
  242. this.domElement.style.height = height + 'px';
  243. }
  244. this.setViewport( 0, 0, width, height );
  245. if ( this._initialized ) this.backend.updateSize();
  246. }
  247. setOpaqueSort( method ) {
  248. this._opaqueSort = method;
  249. }
  250. setTransparentSort( method ) {
  251. this._transparentSort = method;
  252. }
  253. getScissor( target ) {
  254. const scissor = this._scissor;
  255. target.x = scissor.x;
  256. target.y = scissor.y;
  257. target.width = scissor.width;
  258. target.height = scissor.height;
  259. return target;
  260. }
  261. setScissor( x, y, width, height ) {
  262. const scissor = this._scissor;
  263. if ( x.isVector4 ) {
  264. scissor.copy( x );
  265. } else {
  266. scissor.set( x, y, width, height );
  267. }
  268. }
  269. getScissorTest() {
  270. return this._scissorTest;
  271. }
  272. setScissorTest( boolean ) {
  273. this._scissorTest = boolean;
  274. }
  275. getViewport( target ) {
  276. return target.copy( this._viewport );
  277. }
  278. setViewport( x, y, width, height, minDepth = 0, maxDepth = 1 ) {
  279. const viewport = this._viewport;
  280. if ( x.isVector4 ) {
  281. viewport.copy( x );
  282. } else {
  283. viewport.set( x, y, width, height );
  284. }
  285. viewport.minDepth = minDepth;
  286. viewport.maxDepth = maxDepth;
  287. }
  288. getClearColor( target ) {
  289. return target.copy( this._clearColor );
  290. }
  291. setClearColor( color, alpha = 1 ) {
  292. this._clearColor.set( color );
  293. this._clearAlpha = alpha;
  294. }
  295. getClearAlpha() {
  296. return this._clearAlpha;
  297. }
  298. setClearAlpha( alpha ) {
  299. this._clearAlpha = alpha;
  300. }
  301. getClearDepth() {
  302. return this._clearDepth;
  303. }
  304. setClearDepth( depth ) {
  305. this._clearDepth = depth;
  306. }
  307. getClearStencil() {
  308. return this._clearStencil;
  309. }
  310. setClearStencil( stencil ) {
  311. this._clearStencil = stencil;
  312. }
  313. clear( color = true, depth = true, stencil = true ) {
  314. const renderContext = this._currentRenderContext || this._lastRenderContext;
  315. if ( renderContext ) this.backend.clear( renderContext, color, depth, stencil );
  316. }
  317. clearColor() {
  318. this.clear( true, false, false );
  319. }
  320. clearDepth() {
  321. this.clear( false, true, false );
  322. }
  323. clearStencil() {
  324. this.clear( false, false, true );
  325. }
  326. dispose() {
  327. this._objects.dispose();
  328. this._properties.dispose();
  329. this._pipelines.dispose();
  330. this._nodes.dispose();
  331. this._bindings.dispose();
  332. this._info.dispose();
  333. this._renderLists.dispose();
  334. this._renderContexts.dispose();
  335. this._textures.dispose();
  336. this.setRenderTarget( null );
  337. this.setAnimationLoop( null );
  338. }
  339. setRenderTarget( renderTarget, activeCubeFace = 0 ) {
  340. this._renderTarget = renderTarget;
  341. this._activeCubeFace = activeCubeFace;
  342. }
  343. async compute( computeNodes ) {
  344. if ( this._initialized === false ) await this.init();
  345. const backend = this.backend;
  346. const pipelines = this._pipelines;
  347. const computeGroup = Array.isArray( computeNodes ) ? computeNodes : [ computeNodes ];
  348. backend.beginCompute( computeGroup );
  349. for ( const computeNode of computeGroup ) {
  350. // onInit
  351. if ( pipelines.has( computeNode ) === false ) {
  352. computeNode.onInit( { renderer: this } );
  353. }
  354. this._nodes.updateForCompute( computeNode );
  355. this._bindings.updateForCompute( computeNode );
  356. const computePipeline = pipelines.getForCompute( computeNode );
  357. const computeBindings = this._bindings.getForCompute( computeNode );
  358. backend.compute( computeGroup, computeNode, computeBindings, computePipeline );
  359. }
  360. backend.finishCompute( computeGroup );
  361. }
  362. getRenderTarget() {
  363. return this._renderTarget;
  364. }
  365. hasFeature( name ) {
  366. return this.backend.hasFeature( name );
  367. }
  368. copyFramebufferToTexture( framebufferTexture ) {
  369. const renderContext = this._currentRenderContext || this._lastRenderContext;
  370. this._textures.updateTexture( framebufferTexture );
  371. this.backend.copyFramebufferToTexture( framebufferTexture, renderContext );
  372. }
  373. readRenderTargetPixelsAsync( renderTarget, x, y, width, height ) {
  374. return this.backend.copyTextureToBuffer( renderTarget.texture, x, y, width, height );
  375. }
  376. _projectObject( object, camera, groupOrder, renderList ) {
  377. if ( object.visible === false ) return;
  378. const visible = object.layers.test( camera.layers );
  379. if ( visible ) {
  380. if ( object.isGroup ) {
  381. groupOrder = object.renderOrder;
  382. } else if ( object.isLOD ) {
  383. if ( object.autoUpdate === true ) object.update( camera );
  384. } else if ( object.isLight ) {
  385. renderList.pushLight( object );
  386. } else if ( object.isSprite ) {
  387. if ( ! object.frustumCulled || _frustum.intersectsSprite( object ) ) {
  388. if ( this.sortObjects === true ) {
  389. _vector3.setFromMatrixPosition( object.matrixWorld ).applyMatrix4( _projScreenMatrix );
  390. }
  391. const geometry = object.geometry;
  392. const material = object.material;
  393. if ( material.visible ) {
  394. renderList.push( object, geometry, material, groupOrder, _vector3.z, null );
  395. }
  396. }
  397. } else if ( object.isLineLoop ) {
  398. console.error( 'THREE.Renderer: Objects of type THREE.LineLoop are not supported. Please use THREE.Line or THREE.LineSegments.' );
  399. } else if ( object.isMesh || object.isLine || object.isPoints ) {
  400. if ( ! object.frustumCulled || _frustum.intersectsObject( object ) ) {
  401. const geometry = object.geometry;
  402. const material = object.material;
  403. if ( this.sortObjects === true ) {
  404. if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
  405. _vector3
  406. .copy( geometry.boundingSphere.center )
  407. .applyMatrix4( object.matrixWorld )
  408. .applyMatrix4( _projScreenMatrix );
  409. }
  410. if ( Array.isArray( material ) ) {
  411. const groups = geometry.groups;
  412. for ( let i = 0, l = groups.length; i < l; i ++ ) {
  413. const group = groups[ i ];
  414. const groupMaterial = material[ group.materialIndex ];
  415. if ( groupMaterial && groupMaterial.visible ) {
  416. renderList.push( object, geometry, groupMaterial, groupOrder, _vector3.z, group );
  417. }
  418. }
  419. } else if ( material.visible ) {
  420. renderList.push( object, geometry, material, groupOrder, _vector3.z, null );
  421. }
  422. }
  423. }
  424. }
  425. const children = object.children;
  426. for ( let i = 0, l = children.length; i < l; i ++ ) {
  427. this._projectObject( children[ i ], camera, groupOrder, renderList );
  428. }
  429. }
  430. _renderObjects( renderList, camera, scene, lightsNode ) {
  431. // process renderable objects
  432. for ( let i = 0, il = renderList.length; i < il; i ++ ) {
  433. const renderItem = renderList[ i ];
  434. // @TODO: Add support for multiple materials per object. This will require to extract
  435. // the material from the renderItem object and pass it with its group data to _renderObject().
  436. const { object, geometry, material, group } = renderItem;
  437. if ( camera.isArrayCamera ) {
  438. const cameras = camera.cameras;
  439. for ( let j = 0, jl = cameras.length; j < jl; j ++ ) {
  440. const camera2 = cameras[ j ];
  441. if ( object.layers.test( camera2.layers ) ) {
  442. const vp = camera2.viewport;
  443. const minDepth = ( vp.minDepth === undefined ) ? 0 : vp.minDepth;
  444. const maxDepth = ( vp.maxDepth === undefined ) ? 1 : vp.maxDepth;
  445. const viewportValue = this._currentRenderContext.viewportValue;
  446. viewportValue.copy( vp ).multiplyScalar( this._pixelRatio ).floor();
  447. viewportValue.minDepth = minDepth;
  448. viewportValue.maxDepth = maxDepth;
  449. this.backend.updateViewport( this._currentRenderContext );
  450. this._renderObject( object, scene, camera2, geometry, material, group, lightsNode );
  451. }
  452. }
  453. } else {
  454. this._renderObject( object, scene, camera, geometry, material, group, lightsNode );
  455. }
  456. }
  457. }
  458. _renderObject( object, scene, camera, geometry, material, group, lightsNode ) {
  459. material = scene.overrideMaterial !== null ? scene.overrideMaterial : material;
  460. //
  461. object.onBeforeRender( this, scene, camera, geometry, material, group );
  462. //
  463. const renderObject = this._objects.get( object, material, scene, camera, lightsNode, this._currentRenderContext );
  464. this._nodes.updateBefore( renderObject );
  465. //
  466. object.modelViewMatrix.multiplyMatrices( camera.matrixWorldInverse, object.matrixWorld );
  467. object.normalMatrix.getNormalMatrix( object.modelViewMatrix );
  468. //
  469. material.onBeforeRender( this, scene, camera, geometry, material, group );
  470. //
  471. if ( material.transparent === true && material.side === DoubleSide && material.forceSinglePass === false ) {
  472. material.side = BackSide;
  473. this._renderObjectDirect( object, scene, camera, geometry, material, group, lightsNode, 'backSide' ); // create backSide pass id
  474. material.side = FrontSide;
  475. this._renderObjectDirect( object, scene, camera, geometry, material, group, lightsNode ); // use default pass id
  476. material.side = DoubleSide;
  477. } else {
  478. this._renderObjectDirect( object, scene, camera, geometry, material, group, lightsNode );
  479. }
  480. //
  481. object.onAfterRender( this, scene, camera, geometry, material, group );
  482. }
  483. _renderObjectDirect( object, scene, camera, geometry, material, group, lightsNode, passId ) {
  484. //
  485. const renderObject = this._objects.get( object, material, scene, camera, lightsNode, this._currentRenderContext, passId );
  486. //
  487. this._nodes.updateForRender( renderObject );
  488. this._geometries.updateForRender( renderObject );
  489. this._bindings.updateForRender( renderObject );
  490. //
  491. this.backend.draw( renderObject, this._info );
  492. }
  493. }
  494. export default Renderer;