Renderer.js 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  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( 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 render( 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. }
  233. getMaxAnisotropy() {
  234. return this.backend.getMaxAnisotropy();
  235. }
  236. getActiveCubeFace() {
  237. return this._activeCubeFace;
  238. }
  239. getActiveMipmapLevel() {
  240. return this._activeMipmapLevel;
  241. }
  242. async setAnimationLoop( callback ) {
  243. if ( this._initialized === false ) await this.init();
  244. this._animation.setAnimationLoop( callback );
  245. }
  246. getArrayBuffer( attribute ) { // @deprecated, r155
  247. console.warn( 'THREE.Renderer: getArrayBuffer() is deprecated. Use getArrayBufferAsync() instead.' );
  248. return this.getArrayBufferAsync( attribute );
  249. }
  250. async getArrayBufferAsync( attribute ) {
  251. return await this.backend.getArrayBufferAsync( attribute );
  252. }
  253. getContext() {
  254. return this.backend.getContext();
  255. }
  256. getPixelRatio() {
  257. return this._pixelRatio;
  258. }
  259. getDrawingBufferSize( target ) {
  260. return target.set( this._width * this._pixelRatio, this._height * this._pixelRatio ).floor();
  261. }
  262. getSize( target ) {
  263. return target.set( this._width, this._height );
  264. }
  265. setPixelRatio( value = 1 ) {
  266. this._pixelRatio = value;
  267. this.setSize( this._width, this._height, false );
  268. }
  269. setDrawingBufferSize( width, height, pixelRatio ) {
  270. this._width = width;
  271. this._height = height;
  272. this._pixelRatio = pixelRatio;
  273. this.domElement.width = Math.floor( width * pixelRatio );
  274. this.domElement.height = Math.floor( height * pixelRatio );
  275. this.setViewport( 0, 0, width, height );
  276. if ( this._initialized ) this.backend.updateSize();
  277. }
  278. setSize( width, height, updateStyle = true ) {
  279. this._width = width;
  280. this._height = height;
  281. this.domElement.width = Math.floor( width * this._pixelRatio );
  282. this.domElement.height = Math.floor( height * this._pixelRatio );
  283. if ( updateStyle === true ) {
  284. this.domElement.style.width = width + 'px';
  285. this.domElement.style.height = height + 'px';
  286. }
  287. this.setViewport( 0, 0, width, height );
  288. if ( this._initialized ) this.backend.updateSize();
  289. }
  290. setOpaqueSort( method ) {
  291. this._opaqueSort = method;
  292. }
  293. setTransparentSort( method ) {
  294. this._transparentSort = method;
  295. }
  296. getScissor( target ) {
  297. const scissor = this._scissor;
  298. target.x = scissor.x;
  299. target.y = scissor.y;
  300. target.width = scissor.width;
  301. target.height = scissor.height;
  302. return target;
  303. }
  304. setScissor( x, y, width, height ) {
  305. const scissor = this._scissor;
  306. if ( x.isVector4 ) {
  307. scissor.copy( x );
  308. } else {
  309. scissor.set( x, y, width, height );
  310. }
  311. }
  312. getScissorTest() {
  313. return this._scissorTest;
  314. }
  315. setScissorTest( boolean ) {
  316. this._scissorTest = boolean;
  317. }
  318. getViewport( target ) {
  319. return target.copy( this._viewport );
  320. }
  321. setViewport( x, y, width, height, minDepth = 0, maxDepth = 1 ) {
  322. const viewport = this._viewport;
  323. if ( x.isVector4 ) {
  324. viewport.copy( x );
  325. } else {
  326. viewport.set( x, y, width, height );
  327. }
  328. viewport.minDepth = minDepth;
  329. viewport.maxDepth = maxDepth;
  330. }
  331. getClearColor( target ) {
  332. return target.copy( this._clearColor );
  333. }
  334. setClearColor( color, alpha = 1 ) {
  335. this._clearColor.set( color );
  336. this._clearColor.a = alpha;
  337. }
  338. getClearAlpha() {
  339. return this._clearColor.a;
  340. }
  341. setClearAlpha( alpha ) {
  342. this._clearColor.a = alpha;
  343. }
  344. getClearDepth() {
  345. return this._clearDepth;
  346. }
  347. setClearDepth( depth ) {
  348. this._clearDepth = depth;
  349. }
  350. getClearStencil() {
  351. return this._clearStencil;
  352. }
  353. setClearStencil( stencil ) {
  354. this._clearStencil = stencil;
  355. }
  356. isOccluded( object ) {
  357. const renderContext = this._currentRenderContext;
  358. return renderContext && this.backend.isOccluded( renderContext, object );
  359. }
  360. clear( color = true, depth = true, stencil = true ) {
  361. let renderTargetData = null;
  362. const renderTarget = this._renderTarget;
  363. if ( renderTarget !== null ) {
  364. this._textures.updateRenderTarget( renderTarget );
  365. renderTargetData = this._textures.get( renderTarget );
  366. }
  367. this.backend.clear( color, depth, stencil, renderTargetData );
  368. }
  369. clearColor() {
  370. this.clear( true, false, false );
  371. }
  372. clearDepth() {
  373. this.clear( false, true, false );
  374. }
  375. clearStencil() {
  376. this.clear( false, false, true );
  377. }
  378. get currentColorSpace() {
  379. const renderTarget = this._renderTarget;
  380. if ( renderTarget !== null ) {
  381. const texture = renderTarget.texture;
  382. return ( Array.isArray( texture ) ? texture[ 0 ] : texture ).colorSpace;
  383. }
  384. return this.outputColorSpace;
  385. }
  386. dispose() {
  387. this.info.dispose();
  388. this._animation.dispose();
  389. this._objects.dispose();
  390. this._pipelines.dispose();
  391. this._nodes.dispose();
  392. this._bindings.dispose();
  393. this._renderLists.dispose();
  394. this._renderContexts.dispose();
  395. this._textures.dispose();
  396. this.setRenderTarget( null );
  397. this.setAnimationLoop( null );
  398. }
  399. setRenderTarget( renderTarget, activeCubeFace = 0, activeMipmapLevel = 0 ) {
  400. this._renderTarget = renderTarget;
  401. this._activeCubeFace = activeCubeFace;
  402. this._activeMipmapLevel = activeMipmapLevel;
  403. }
  404. getRenderTarget() {
  405. return this._renderTarget;
  406. }
  407. setRenderObjectFunction( renderObjectFunction ) {
  408. this._renderObjectFunction = renderObjectFunction;
  409. }
  410. getRenderObjectFunction() {
  411. return this._renderObjectFunction;
  412. }
  413. async compute( computeNodes ) {
  414. if ( this._initialized === false ) await this.init();
  415. const nodeFrame = this._nodes.nodeFrame;
  416. const previousRenderId = nodeFrame.renderId;
  417. //
  418. this.info.calls ++;
  419. this.info.compute.calls ++;
  420. nodeFrame.renderId = this.info.calls;
  421. //
  422. const backend = this.backend;
  423. const pipelines = this._pipelines;
  424. const bindings = this._bindings;
  425. const nodes = this._nodes;
  426. const computeList = Array.isArray( computeNodes ) ? computeNodes : [ computeNodes ];
  427. if ( computeList[ 0 ] === undefined || computeList[ 0 ].isComputeNode !== true ) {
  428. throw new Error( 'THREE.Renderer: .compute() expects a ComputeNode.' );
  429. }
  430. backend.beginCompute( computeNodes );
  431. for ( const computeNode of computeList ) {
  432. // onInit
  433. if ( pipelines.has( computeNode ) === false ) {
  434. const dispose = () => {
  435. computeNode.removeEventListener( 'dispose', dispose );
  436. pipelines.delete( computeNode );
  437. bindings.delete( computeNode );
  438. nodes.delete( computeNode );
  439. };
  440. computeNode.addEventListener( 'dispose', dispose );
  441. //
  442. computeNode.onInit( { renderer: this } );
  443. }
  444. nodes.updateForCompute( computeNode );
  445. bindings.updateForCompute( computeNode );
  446. const computeBindings = bindings.getForCompute( computeNode );
  447. const computePipeline = pipelines.getForCompute( computeNode, computeBindings );
  448. backend.compute( computeNodes, computeNode, computeBindings, computePipeline );
  449. }
  450. backend.finishCompute( computeNodes );
  451. //
  452. nodeFrame.renderId = previousRenderId;
  453. }
  454. hasFeatureAsync( name ) {
  455. return this.backend.hasFeatureAsync( name );
  456. }
  457. hasFeature( name ) {
  458. return this.backend.hasFeature( name );
  459. }
  460. copyFramebufferToTexture( framebufferTexture ) {
  461. const renderContext = this._currentRenderContext;
  462. this._textures.updateTexture( framebufferTexture );
  463. this.backend.copyFramebufferToTexture( framebufferTexture, renderContext );
  464. }
  465. readRenderTargetPixelsAsync( renderTarget, x, y, width, height ) {
  466. return this.backend.copyTextureToBuffer( renderTarget.texture, x, y, width, height );
  467. }
  468. _projectObject( object, camera, groupOrder, renderList ) {
  469. if ( object.visible === false ) return;
  470. const visible = object.layers.test( camera.layers );
  471. if ( visible ) {
  472. if ( object.isGroup ) {
  473. groupOrder = object.renderOrder;
  474. } else if ( object.isLOD ) {
  475. if ( object.autoUpdate === true ) object.update( camera );
  476. } else if ( object.isLight ) {
  477. renderList.pushLight( object );
  478. } else if ( object.isSprite ) {
  479. if ( ! object.frustumCulled || _frustum.intersectsSprite( object ) ) {
  480. if ( this.sortObjects === true ) {
  481. _vector3.setFromMatrixPosition( object.matrixWorld ).applyMatrix4( _projScreenMatrix );
  482. }
  483. const geometry = object.geometry;
  484. const material = object.material;
  485. if ( material.visible ) {
  486. renderList.push( object, geometry, material, groupOrder, _vector3.z, null );
  487. }
  488. }
  489. } else if ( object.isLineLoop ) {
  490. console.error( 'THREE.Renderer: Objects of type THREE.LineLoop are not supported. Please use THREE.Line or THREE.LineSegments.' );
  491. } else if ( object.isMesh || object.isLine || object.isPoints ) {
  492. if ( ! object.frustumCulled || _frustum.intersectsObject( object ) ) {
  493. const geometry = object.geometry;
  494. const material = object.material;
  495. if ( this.sortObjects === true ) {
  496. if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
  497. _vector3
  498. .copy( geometry.boundingSphere.center )
  499. .applyMatrix4( object.matrixWorld )
  500. .applyMatrix4( _projScreenMatrix );
  501. }
  502. if ( Array.isArray( material ) ) {
  503. const groups = geometry.groups;
  504. for ( let i = 0, l = groups.length; i < l; i ++ ) {
  505. const group = groups[ i ];
  506. const groupMaterial = material[ group.materialIndex ];
  507. if ( groupMaterial && groupMaterial.visible ) {
  508. renderList.push( object, geometry, groupMaterial, groupOrder, _vector3.z, group );
  509. }
  510. }
  511. } else if ( material.visible ) {
  512. renderList.push( object, geometry, material, groupOrder, _vector3.z, null );
  513. }
  514. }
  515. }
  516. }
  517. const children = object.children;
  518. for ( let i = 0, l = children.length; i < l; i ++ ) {
  519. this._projectObject( children[ i ], camera, groupOrder, renderList );
  520. }
  521. }
  522. _renderObjects( renderList, camera, scene, lightsNode ) {
  523. // process renderable objects
  524. for ( let i = 0, il = renderList.length; i < il; i ++ ) {
  525. const renderItem = renderList[ i ];
  526. // @TODO: Add support for multiple materials per object. This will require to extract
  527. // the material from the renderItem object and pass it with its group data to renderObject().
  528. const { object, geometry, material, group } = renderItem;
  529. if ( camera.isArrayCamera ) {
  530. const cameras = camera.cameras;
  531. for ( let j = 0, jl = cameras.length; j < jl; j ++ ) {
  532. const camera2 = cameras[ j ];
  533. if ( object.layers.test( camera2.layers ) ) {
  534. const vp = camera2.viewport;
  535. const minDepth = ( vp.minDepth === undefined ) ? 0 : vp.minDepth;
  536. const maxDepth = ( vp.maxDepth === undefined ) ? 1 : vp.maxDepth;
  537. const viewportValue = this._currentRenderContext.viewportValue;
  538. viewportValue.copy( vp ).multiplyScalar( this._pixelRatio ).floor();
  539. viewportValue.minDepth = minDepth;
  540. viewportValue.maxDepth = maxDepth;
  541. this.backend.updateViewport( this._currentRenderContext );
  542. this._currentRenderObjectFunction( object, scene, camera2, geometry, material, group, lightsNode );
  543. }
  544. }
  545. } else {
  546. this._currentRenderObjectFunction( object, scene, camera, geometry, material, group, lightsNode );
  547. }
  548. }
  549. }
  550. renderObject( object, scene, camera, geometry, material, group, lightsNode ) {
  551. let overridePositionNode;
  552. //
  553. object.onBeforeRender( this, scene, camera, geometry, material, group );
  554. material.onBeforeRender( this, scene, camera, geometry, material, group );
  555. //
  556. if ( scene.overrideMaterial !== null ) {
  557. const overrideMaterial = scene.overrideMaterial;
  558. if ( material.positionNode && material.positionNode.isNode ) {
  559. overridePositionNode = overrideMaterial.positionNode;
  560. overrideMaterial.positionNode = material.positionNode;
  561. }
  562. material = overrideMaterial;
  563. }
  564. //
  565. if ( material.transparent === true && material.side === DoubleSide && material.forceSinglePass === false ) {
  566. material.side = BackSide;
  567. this._renderObjectDirect( object, material, scene, camera, lightsNode, 'backSide' ); // create backSide pass id
  568. material.side = FrontSide;
  569. this._renderObjectDirect( object, material, scene, camera, lightsNode ); // use default pass id
  570. material.side = DoubleSide;
  571. } else {
  572. this._renderObjectDirect( object, material, scene, camera, lightsNode );
  573. }
  574. //
  575. if ( overridePositionNode !== undefined ) {
  576. scene.overrideMaterial.positionNode = overridePositionNode;
  577. }
  578. //
  579. object.onAfterRender( this, scene, camera, geometry, material, group );
  580. }
  581. _renderObjectDirect( object, material, scene, camera, lightsNode, passId ) {
  582. const renderObject = this._objects.get( object, material, scene, camera, lightsNode, this._currentRenderContext, passId );
  583. //
  584. this._nodes.updateBefore( renderObject );
  585. //
  586. object.modelViewMatrix.multiplyMatrices( camera.matrixWorldInverse, object.matrixWorld );
  587. object.normalMatrix.getNormalMatrix( object.modelViewMatrix );
  588. //
  589. this._nodes.updateForRender( renderObject );
  590. this._geometries.updateForRender( renderObject );
  591. this._bindings.updateForRender( renderObject );
  592. this._pipelines.updateForRender( renderObject );
  593. //
  594. this.backend.draw( renderObject, this.info );
  595. }
  596. }
  597. export default Renderer;