Renderer.js 26 KB

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