Renderer.js 22 KB

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