Renderer.js 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  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 = false;
  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. const renderContext = this._renderScene( scene, camera );
  199. await this.backend.resolveTimestampAsync( renderContext, 'render' );
  200. }
  201. render( scene, camera ) {
  202. if ( this._initialized === false ) {
  203. console.warn( 'THREE.Renderer: .render() called before the backend is initialized. Try using .renderAsync() instead.' );
  204. return this.renderAsync( scene, camera );
  205. }
  206. this._renderScene( scene, camera );
  207. }
  208. _renderScene( scene, camera ) {
  209. // preserve render tree
  210. const nodeFrame = this._nodes.nodeFrame;
  211. const previousRenderId = nodeFrame.renderId;
  212. const previousRenderContext = this._currentRenderContext;
  213. const previousRenderObjectFunction = this._currentRenderObjectFunction;
  214. //
  215. const sceneRef = ( scene.isScene === true ) ? scene : _scene;
  216. const renderTarget = this._renderTarget;
  217. const renderContext = this._renderContexts.get( scene, camera, renderTarget );
  218. const activeCubeFace = this._activeCubeFace;
  219. const activeMipmapLevel = this._activeMipmapLevel;
  220. this._currentRenderContext = renderContext;
  221. this._currentRenderObjectFunction = this._renderObjectFunction || this.renderObject;
  222. //
  223. this.info.calls ++;
  224. this.info.render.calls ++;
  225. nodeFrame.renderId = this.info.calls;
  226. //
  227. const coordinateSystem = this.coordinateSystem;
  228. if ( camera.coordinateSystem !== coordinateSystem ) {
  229. camera.coordinateSystem = coordinateSystem;
  230. camera.updateProjectionMatrix();
  231. }
  232. //
  233. if ( scene.matrixWorldAutoUpdate === true ) scene.updateMatrixWorld();
  234. if ( camera.parent === null && camera.matrixWorldAutoUpdate === true ) camera.updateMatrixWorld();
  235. //
  236. let viewport = this._viewport;
  237. let scissor = this._scissor;
  238. let pixelRatio = this._pixelRatio;
  239. if ( renderTarget !== null ) {
  240. viewport = renderTarget.viewport;
  241. scissor = renderTarget.scissor;
  242. pixelRatio = 1;
  243. }
  244. this.getDrawingBufferSize( _drawingBufferSize );
  245. _screen.set( 0, 0, _drawingBufferSize.width, _drawingBufferSize.height );
  246. const minDepth = ( viewport.minDepth === undefined ) ? 0 : viewport.minDepth;
  247. const maxDepth = ( viewport.maxDepth === undefined ) ? 1 : viewport.maxDepth;
  248. renderContext.viewportValue.copy( viewport ).multiplyScalar( pixelRatio ).floor();
  249. renderContext.viewportValue.width >>= activeMipmapLevel;
  250. renderContext.viewportValue.height >>= activeMipmapLevel;
  251. renderContext.viewportValue.minDepth = minDepth;
  252. renderContext.viewportValue.maxDepth = maxDepth;
  253. renderContext.viewport = renderContext.viewportValue.equals( _screen ) === false;
  254. renderContext.scissorValue.copy( scissor ).multiplyScalar( pixelRatio ).floor();
  255. renderContext.scissor = this._scissorTest && renderContext.scissorValue.equals( _screen ) === false;
  256. renderContext.scissorValue.width >>= activeMipmapLevel;
  257. renderContext.scissorValue.height >>= activeMipmapLevel;
  258. if ( ! renderContext.clippingContext ) renderContext.clippingContext = new ClippingContext();
  259. renderContext.clippingContext.updateGlobal( this, camera );
  260. //
  261. sceneRef.onBeforeRender( this, scene, camera, renderTarget );
  262. //
  263. _projScreenMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse );
  264. _frustum.setFromProjectionMatrix( _projScreenMatrix, coordinateSystem );
  265. const renderList = this._renderLists.get( scene, camera );
  266. renderList.begin();
  267. this._projectObject( scene, camera, 0, renderList );
  268. renderList.finish();
  269. if ( this.sortObjects === true ) {
  270. renderList.sort( this._opaqueSort, this._transparentSort );
  271. }
  272. //
  273. if ( renderTarget !== null ) {
  274. this._textures.updateRenderTarget( renderTarget, activeMipmapLevel );
  275. const renderTargetData = this._textures.get( renderTarget );
  276. renderContext.textures = renderTargetData.textures;
  277. renderContext.depthTexture = renderTargetData.depthTexture;
  278. renderContext.width = renderTargetData.width;
  279. renderContext.height = renderTargetData.height;
  280. renderContext.renderTarget = renderTarget;
  281. renderContext.depth = renderTarget.depthBuffer;
  282. renderContext.stencil = renderTarget.stencilBuffer;
  283. } else {
  284. renderContext.textures = null;
  285. renderContext.depthTexture = null;
  286. renderContext.width = this.domElement.width;
  287. renderContext.height = this.domElement.height;
  288. renderContext.depth = this.depth;
  289. renderContext.stencil = this.stencil;
  290. }
  291. renderContext.width >>= activeMipmapLevel;
  292. renderContext.height >>= activeMipmapLevel;
  293. renderContext.activeCubeFace = activeCubeFace;
  294. renderContext.activeMipmapLevel = activeMipmapLevel;
  295. renderContext.occlusionQueryCount = renderList.occlusionQueryCount;
  296. //
  297. this._nodes.updateScene( sceneRef );
  298. //
  299. this._background.update( sceneRef, renderList, renderContext );
  300. //
  301. this.backend.beginRender( renderContext );
  302. // process render lists
  303. const opaqueObjects = renderList.opaque;
  304. const transparentObjects = renderList.transparent;
  305. const lightsNode = renderList.lightsNode;
  306. if ( opaqueObjects.length > 0 ) this._renderObjects( opaqueObjects, camera, sceneRef, lightsNode );
  307. if ( transparentObjects.length > 0 ) this._renderObjects( transparentObjects, camera, sceneRef, lightsNode );
  308. // finish render pass
  309. this.backend.finishRender( renderContext );
  310. // restore render tree
  311. nodeFrame.renderId = previousRenderId;
  312. this._currentRenderContext = previousRenderContext;
  313. this._currentRenderObjectFunction = previousRenderObjectFunction;
  314. //
  315. sceneRef.onAfterRender( this, scene, camera, renderTarget );
  316. //
  317. return renderContext;
  318. }
  319. getMaxAnisotropy() {
  320. return this.backend.getMaxAnisotropy();
  321. }
  322. getActiveCubeFace() {
  323. return this._activeCubeFace;
  324. }
  325. getActiveMipmapLevel() {
  326. return this._activeMipmapLevel;
  327. }
  328. async setAnimationLoop( callback ) {
  329. if ( this._initialized === false ) await this.init();
  330. this._animation.setAnimationLoop( callback );
  331. }
  332. getArrayBuffer( attribute ) { // @deprecated, r155
  333. console.warn( 'THREE.Renderer: getArrayBuffer() is deprecated. Use getArrayBufferAsync() instead.' );
  334. return this.getArrayBufferAsync( attribute );
  335. }
  336. async getArrayBufferAsync( attribute ) {
  337. return await this.backend.getArrayBufferAsync( attribute );
  338. }
  339. getContext() {
  340. return this.backend.getContext();
  341. }
  342. getPixelRatio() {
  343. return this._pixelRatio;
  344. }
  345. getDrawingBufferSize( target ) {
  346. return target.set( this._width * this._pixelRatio, this._height * this._pixelRatio ).floor();
  347. }
  348. getSize( target ) {
  349. return target.set( this._width, this._height );
  350. }
  351. setPixelRatio( value = 1 ) {
  352. this._pixelRatio = value;
  353. this.setSize( this._width, this._height, false );
  354. }
  355. setDrawingBufferSize( width, height, pixelRatio ) {
  356. this._width = width;
  357. this._height = height;
  358. this._pixelRatio = pixelRatio;
  359. this.domElement.width = Math.floor( width * pixelRatio );
  360. this.domElement.height = Math.floor( height * pixelRatio );
  361. this.setViewport( 0, 0, width, height );
  362. if ( this._initialized ) this.backend.updateSize();
  363. }
  364. setSize( width, height, updateStyle = true ) {
  365. this._width = width;
  366. this._height = height;
  367. this.domElement.width = Math.floor( width * this._pixelRatio );
  368. this.domElement.height = Math.floor( height * this._pixelRatio );
  369. if ( updateStyle === true ) {
  370. this.domElement.style.width = width + 'px';
  371. this.domElement.style.height = height + 'px';
  372. }
  373. this.setViewport( 0, 0, width, height );
  374. if ( this._initialized ) this.backend.updateSize();
  375. }
  376. setOpaqueSort( method ) {
  377. this._opaqueSort = method;
  378. }
  379. setTransparentSort( method ) {
  380. this._transparentSort = method;
  381. }
  382. getScissor( target ) {
  383. const scissor = this._scissor;
  384. target.x = scissor.x;
  385. target.y = scissor.y;
  386. target.width = scissor.width;
  387. target.height = scissor.height;
  388. return target;
  389. }
  390. setScissor( x, y, width, height ) {
  391. const scissor = this._scissor;
  392. if ( x.isVector4 ) {
  393. scissor.copy( x );
  394. } else {
  395. scissor.set( x, y, width, height );
  396. }
  397. }
  398. getScissorTest() {
  399. return this._scissorTest;
  400. }
  401. setScissorTest( boolean ) {
  402. this._scissorTest = boolean;
  403. this.backend.setScissorTest( boolean );
  404. }
  405. getViewport( target ) {
  406. return target.copy( this._viewport );
  407. }
  408. setViewport( x, y, width, height, minDepth = 0, maxDepth = 1 ) {
  409. const viewport = this._viewport;
  410. if ( x.isVector4 ) {
  411. viewport.copy( x );
  412. } else {
  413. viewport.set( x, y, width, height );
  414. }
  415. viewport.minDepth = minDepth;
  416. viewport.maxDepth = maxDepth;
  417. }
  418. getClearColor( target ) {
  419. return target.copy( this._clearColor );
  420. }
  421. setClearColor( color, alpha = 1 ) {
  422. this._clearColor.set( color );
  423. this._clearColor.a = alpha;
  424. }
  425. getClearAlpha() {
  426. return this._clearColor.a;
  427. }
  428. setClearAlpha( alpha ) {
  429. this._clearColor.a = alpha;
  430. }
  431. getClearDepth() {
  432. return this._clearDepth;
  433. }
  434. setClearDepth( depth ) {
  435. this._clearDepth = depth;
  436. }
  437. getClearStencil() {
  438. return this._clearStencil;
  439. }
  440. setClearStencil( stencil ) {
  441. this._clearStencil = stencil;
  442. }
  443. isOccluded( object ) {
  444. const renderContext = this._currentRenderContext;
  445. return renderContext && this.backend.isOccluded( renderContext, object );
  446. }
  447. clear( color = true, depth = true, stencil = true ) {
  448. if ( this._initialized === false ) {
  449. console.warn( 'THREE.Renderer: .clear() called before the backend is initialized. Try using .clearAsync() instead.' );
  450. return this.clearAsync( color, depth, stencil );
  451. }
  452. let renderTargetData = null;
  453. const renderTarget = this._renderTarget;
  454. if ( renderTarget !== null ) {
  455. this._textures.updateRenderTarget( renderTarget );
  456. renderTargetData = this._textures.get( renderTarget );
  457. }
  458. this.backend.clear( color, depth, stencil, renderTargetData );
  459. }
  460. clearColor() {
  461. return this.clear( true, false, false );
  462. }
  463. clearDepth() {
  464. return this.clear( false, true, false );
  465. }
  466. clearStencil() {
  467. return this.clear( false, false, true );
  468. }
  469. async clearAsync( color = true, depth = true, stencil = true ) {
  470. if ( this._initialized === false ) await this.init();
  471. this.clear( color, depth, stencil );
  472. }
  473. clearColorAsync() {
  474. return this.clearAsync( true, false, false );
  475. }
  476. clearDepthAsync() {
  477. return this.clearAsync( false, true, false );
  478. }
  479. clearStencilAsync() {
  480. return this.clearAsync( false, false, true );
  481. }
  482. get currentColorSpace() {
  483. const renderTarget = this._renderTarget;
  484. if ( renderTarget !== null ) {
  485. const texture = renderTarget.texture;
  486. return ( Array.isArray( texture ) ? texture[ 0 ] : texture ).colorSpace;
  487. }
  488. return this.outputColorSpace;
  489. }
  490. dispose() {
  491. this.info.dispose();
  492. this._animation.dispose();
  493. this._objects.dispose();
  494. this._pipelines.dispose();
  495. this._nodes.dispose();
  496. this._bindings.dispose();
  497. this._renderLists.dispose();
  498. this._renderContexts.dispose();
  499. this._textures.dispose();
  500. this.setRenderTarget( null );
  501. this.setAnimationLoop( null );
  502. }
  503. setRenderTarget( renderTarget, activeCubeFace = 0, activeMipmapLevel = 0 ) {
  504. this._renderTarget = renderTarget;
  505. this._activeCubeFace = activeCubeFace;
  506. this._activeMipmapLevel = activeMipmapLevel;
  507. }
  508. getRenderTarget() {
  509. return this._renderTarget;
  510. }
  511. setRenderObjectFunction( renderObjectFunction ) {
  512. this._renderObjectFunction = renderObjectFunction;
  513. }
  514. getRenderObjectFunction() {
  515. return this._renderObjectFunction;
  516. }
  517. async computeAsync( computeNodes ) {
  518. if ( this._initialized === false ) await this.init();
  519. const nodeFrame = this._nodes.nodeFrame;
  520. const previousRenderId = nodeFrame.renderId;
  521. //
  522. this.info.calls ++;
  523. this.info.compute.calls ++;
  524. this.info.compute.computeCalls ++;
  525. nodeFrame.renderId = this.info.calls;
  526. //
  527. const backend = this.backend;
  528. const pipelines = this._pipelines;
  529. const bindings = this._bindings;
  530. const nodes = this._nodes;
  531. const computeList = Array.isArray( computeNodes ) ? computeNodes : [ computeNodes ];
  532. if ( computeList[ 0 ] === undefined || computeList[ 0 ].isComputeNode !== true ) {
  533. throw new Error( 'THREE.Renderer: .compute() expects a ComputeNode.' );
  534. }
  535. backend.beginCompute( computeNodes );
  536. for ( const computeNode of computeList ) {
  537. // onInit
  538. if ( pipelines.has( computeNode ) === false ) {
  539. const dispose = () => {
  540. computeNode.removeEventListener( 'dispose', dispose );
  541. pipelines.delete( computeNode );
  542. bindings.delete( computeNode );
  543. nodes.delete( computeNode );
  544. };
  545. computeNode.addEventListener( 'dispose', dispose );
  546. //
  547. computeNode.onInit( { renderer: this } );
  548. }
  549. nodes.updateForCompute( computeNode );
  550. bindings.updateForCompute( computeNode );
  551. const computeBindings = bindings.getForCompute( computeNode );
  552. const computePipeline = pipelines.getForCompute( computeNode, computeBindings );
  553. backend.compute( computeNodes, computeNode, computeBindings, computePipeline );
  554. }
  555. backend.finishCompute( computeNodes );
  556. await this.backend.resolveTimestampAsync( computeNodes, 'compute' );
  557. //
  558. nodeFrame.renderId = previousRenderId;
  559. }
  560. hasFeatureAsync( name ) {
  561. return this.backend.hasFeatureAsync( name );
  562. }
  563. hasFeature( name ) {
  564. return this.backend.hasFeature( name );
  565. }
  566. copyFramebufferToTexture( framebufferTexture ) {
  567. const renderContext = this._currentRenderContext;
  568. this._textures.updateTexture( framebufferTexture );
  569. this.backend.copyFramebufferToTexture( framebufferTexture, renderContext );
  570. }
  571. copyTextureToTexture( position, srcTexture, dstTexture, level = 0 ) {
  572. this._textures.updateTexture( srcTexture );
  573. this._textures.updateTexture( dstTexture );
  574. this.backend.copyTextureToTexture( position, srcTexture, dstTexture, level );
  575. }
  576. readRenderTargetPixelsAsync( renderTarget, x, y, width, height ) {
  577. return this.backend.copyTextureToBuffer( renderTarget.texture, x, y, width, height );
  578. }
  579. _projectObject( object, camera, groupOrder, renderList ) {
  580. if ( object.visible === false ) return;
  581. const visible = object.layers.test( camera.layers );
  582. if ( visible ) {
  583. if ( object.isGroup ) {
  584. groupOrder = object.renderOrder;
  585. } else if ( object.isLOD ) {
  586. if ( object.autoUpdate === true ) object.update( camera );
  587. } else if ( object.isLight ) {
  588. renderList.pushLight( object );
  589. } else if ( object.isSprite ) {
  590. if ( ! object.frustumCulled || _frustum.intersectsSprite( object ) ) {
  591. if ( this.sortObjects === true ) {
  592. _vector3.setFromMatrixPosition( object.matrixWorld ).applyMatrix4( _projScreenMatrix );
  593. }
  594. const geometry = object.geometry;
  595. const material = object.material;
  596. if ( material.visible ) {
  597. renderList.push( object, geometry, material, groupOrder, _vector3.z, null );
  598. }
  599. }
  600. } else if ( object.isLineLoop ) {
  601. console.error( 'THREE.Renderer: Objects of type THREE.LineLoop are not supported. Please use THREE.Line or THREE.LineSegments.' );
  602. } else if ( object.isMesh || object.isLine || object.isPoints ) {
  603. if ( ! object.frustumCulled || _frustum.intersectsObject( object ) ) {
  604. const geometry = object.geometry;
  605. const material = object.material;
  606. if ( this.sortObjects === true ) {
  607. if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
  608. _vector3
  609. .copy( geometry.boundingSphere.center )
  610. .applyMatrix4( object.matrixWorld )
  611. .applyMatrix4( _projScreenMatrix );
  612. }
  613. if ( Array.isArray( material ) ) {
  614. const groups = geometry.groups;
  615. for ( let i = 0, l = groups.length; i < l; i ++ ) {
  616. const group = groups[ i ];
  617. const groupMaterial = material[ group.materialIndex ];
  618. if ( groupMaterial && groupMaterial.visible ) {
  619. renderList.push( object, geometry, groupMaterial, groupOrder, _vector3.z, group );
  620. }
  621. }
  622. } else if ( material.visible ) {
  623. renderList.push( object, geometry, material, groupOrder, _vector3.z, null );
  624. }
  625. }
  626. }
  627. }
  628. const children = object.children;
  629. for ( let i = 0, l = children.length; i < l; i ++ ) {
  630. this._projectObject( children[ i ], camera, groupOrder, renderList );
  631. }
  632. }
  633. _renderObjects( renderList, camera, scene, lightsNode ) {
  634. // process renderable objects
  635. for ( let i = 0, il = renderList.length; i < il; i ++ ) {
  636. const renderItem = renderList[ i ];
  637. // @TODO: Add support for multiple materials per object. This will require to extract
  638. // the material from the renderItem object and pass it with its group data to renderObject().
  639. const { object, geometry, material, group } = renderItem;
  640. if ( camera.isArrayCamera ) {
  641. const cameras = camera.cameras;
  642. for ( let j = 0, jl = cameras.length; j < jl; j ++ ) {
  643. const camera2 = cameras[ j ];
  644. if ( object.layers.test( camera2.layers ) ) {
  645. const vp = camera2.viewport;
  646. const minDepth = ( vp.minDepth === undefined ) ? 0 : vp.minDepth;
  647. const maxDepth = ( vp.maxDepth === undefined ) ? 1 : vp.maxDepth;
  648. const viewportValue = this._currentRenderContext.viewportValue;
  649. viewportValue.copy( vp ).multiplyScalar( this._pixelRatio ).floor();
  650. viewportValue.minDepth = minDepth;
  651. viewportValue.maxDepth = maxDepth;
  652. this.backend.updateViewport( this._currentRenderContext );
  653. this._currentRenderObjectFunction( object, scene, camera2, geometry, material, group, lightsNode );
  654. }
  655. }
  656. } else {
  657. this._currentRenderObjectFunction( object, scene, camera, geometry, material, group, lightsNode );
  658. }
  659. }
  660. }
  661. renderObject( object, scene, camera, geometry, material, group, lightsNode ) {
  662. let overridePositionNode;
  663. let overrideFragmentNode;
  664. //
  665. object.onBeforeRender( this, scene, camera, geometry, material, group );
  666. material.onBeforeRender( this, scene, camera, geometry, material, group );
  667. //
  668. if ( scene.overrideMaterial !== null ) {
  669. const overrideMaterial = scene.overrideMaterial;
  670. if ( material.positionNode && material.positionNode.isNode ) {
  671. overridePositionNode = overrideMaterial.positionNode;
  672. overrideMaterial.positionNode = material.positionNode;
  673. }
  674. if ( overrideMaterial.isShadowNodeMaterial ) {
  675. overrideMaterial.side = material.shadowSide === null ? material.side : material.shadowSide;
  676. if ( material.shadowNode && material.shadowNode.isNode ) {
  677. overrideFragmentNode = overrideMaterial.fragmentNode;
  678. overrideMaterial.fragmentNode = material.shadowNode;
  679. }
  680. if ( this.localClippingEnabled ) {
  681. if ( material.clipShadows ) {
  682. if ( overrideMaterial.clippingPlanes !== material.clippingPlanes ) {
  683. overrideMaterial.clippingPlanes = material.clippingPlanes;
  684. overrideMaterial.needsUpdate = true;
  685. }
  686. if ( overrideMaterial.clipIntersection !== material.clipIntersection ) {
  687. overrideMaterial.clipIntersection = material.clipIntersection;
  688. }
  689. } else if ( Array.isArray( overrideMaterial.clippingPlanes ) ) {
  690. overrideMaterial.clippingPlanes = null;
  691. overrideMaterial.needsUpdate = true;
  692. }
  693. }
  694. }
  695. material = overrideMaterial;
  696. }
  697. //
  698. if ( material.transparent === true && material.side === DoubleSide && material.forceSinglePass === false ) {
  699. material.side = BackSide;
  700. this._handleObjectFunction( object, material, scene, camera, lightsNode, 'backSide' ); // create backSide pass id
  701. material.side = FrontSide;
  702. this._handleObjectFunction( object, material, scene, camera, lightsNode ); // use default pass id
  703. material.side = DoubleSide;
  704. } else {
  705. this._handleObjectFunction( object, material, scene, camera, lightsNode );
  706. }
  707. //
  708. if ( overridePositionNode !== undefined ) {
  709. scene.overrideMaterial.positionNode = overridePositionNode;
  710. }
  711. if ( overrideFragmentNode !== undefined ) {
  712. scene.overrideMaterial.fragmentNode = overrideFragmentNode;
  713. }
  714. //
  715. object.onAfterRender( this, scene, camera, geometry, material, group );
  716. }
  717. _renderObjectDirect( object, material, scene, camera, lightsNode, passId ) {
  718. const renderObject = this._objects.get( object, material, scene, camera, lightsNode, this._currentRenderContext, passId );
  719. //
  720. this._nodes.updateBefore( renderObject );
  721. //
  722. object.modelViewMatrix.multiplyMatrices( camera.matrixWorldInverse, object.matrixWorld );
  723. object.normalMatrix.getNormalMatrix( object.modelViewMatrix );
  724. //
  725. this._nodes.updateForRender( renderObject );
  726. this._geometries.updateForRender( renderObject );
  727. this._bindings.updateForRender( renderObject );
  728. this._pipelines.updateForRender( renderObject );
  729. //
  730. this.backend.draw( renderObject, this.info );
  731. }
  732. _createObjectPipeline( object, material, scene, camera, lightsNode, passId ) {
  733. const renderObject = this._objects.get( object, material, scene, camera, lightsNode, this._currentRenderContext, passId );
  734. //
  735. this._nodes.updateBefore( renderObject );
  736. //
  737. this._nodes.updateForRender( renderObject );
  738. this._geometries.updateForRender( renderObject );
  739. this._bindings.updateForRender( renderObject );
  740. this._pipelines.getForRender( renderObject, this._compilationPromises );
  741. }
  742. get compute() {
  743. return this.computeAsync;
  744. }
  745. get compile() {
  746. return this.compileAsync;
  747. }
  748. }
  749. export default Renderer;