Renderer.js 21 KB

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