Renderer.js 17 KB

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