Renderer.js 27 KB

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