Renderer.js 28 KB

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