Renderer.js 17 KB

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