Renderer.js 16 KB

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