Renderer.js 22 KB

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