Renderer.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323
  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._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. 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. if ( this._initialized === false ) {
  450. console.warn( 'THREE.Renderer: .clear() called before the backend is initialized. Try using .clearAsync() instead.' );
  451. return this.clearAsync( color, depth, stencil );
  452. }
  453. let renderTargetData = null;
  454. const renderTarget = this._renderTarget;
  455. if ( renderTarget !== null ) {
  456. this._textures.updateRenderTarget( renderTarget );
  457. renderTargetData = this._textures.get( renderTarget );
  458. }
  459. this.backend.clear( color, depth, stencil, renderTargetData );
  460. }
  461. clearColor() {
  462. return this.clear( true, false, false );
  463. }
  464. clearDepth() {
  465. return this.clear( false, true, false );
  466. }
  467. clearStencil() {
  468. return this.clear( false, false, true );
  469. }
  470. async clearAsync( color = true, depth = true, stencil = true ) {
  471. if ( this._initialized === false ) await this.init();
  472. this.clear( color, depth, stencil );
  473. }
  474. clearColorAsync() {
  475. return this.clearAsync( true, false, false );
  476. }
  477. clearDepthAsync() {
  478. return this.clearAsync( false, true, false );
  479. }
  480. clearStencilAsync() {
  481. return this.clearAsync( false, false, true );
  482. }
  483. get currentColorSpace() {
  484. const renderTarget = this._renderTarget;
  485. if ( renderTarget !== null ) {
  486. const texture = renderTarget.texture;
  487. return ( Array.isArray( texture ) ? texture[ 0 ] : texture ).colorSpace;
  488. }
  489. return this.outputColorSpace;
  490. }
  491. dispose() {
  492. this.info.dispose();
  493. this._animation.dispose();
  494. this._objects.dispose();
  495. this._pipelines.dispose();
  496. this._nodes.dispose();
  497. this._bindings.dispose();
  498. this._renderLists.dispose();
  499. this._renderContexts.dispose();
  500. this._textures.dispose();
  501. this.setRenderTarget( null );
  502. this.setAnimationLoop( null );
  503. }
  504. setRenderTarget( renderTarget, activeCubeFace = 0, activeMipmapLevel = 0 ) {
  505. this._renderTarget = renderTarget;
  506. this._activeCubeFace = activeCubeFace;
  507. this._activeMipmapLevel = activeMipmapLevel;
  508. }
  509. getRenderTarget() {
  510. return this._renderTarget;
  511. }
  512. setRenderObjectFunction( renderObjectFunction ) {
  513. this._renderObjectFunction = renderObjectFunction;
  514. }
  515. getRenderObjectFunction() {
  516. return this._renderObjectFunction;
  517. }
  518. async computeAsync( computeNodes ) {
  519. if ( this._initialized === false ) await this.init();
  520. const nodeFrame = this._nodes.nodeFrame;
  521. const previousRenderId = nodeFrame.renderId;
  522. //
  523. if ( this.info.autoReset === true ) this.info.resetCompute();
  524. this.info.calls ++;
  525. this.info.compute.calls ++;
  526. this.info.compute.computeCalls ++;
  527. nodeFrame.renderId = this.info.calls;
  528. //
  529. const backend = this.backend;
  530. const pipelines = this._pipelines;
  531. const bindings = this._bindings;
  532. const nodes = this._nodes;
  533. const computeList = Array.isArray( computeNodes ) ? computeNodes : [ computeNodes ];
  534. if ( computeList[ 0 ] === undefined || computeList[ 0 ].isComputeNode !== true ) {
  535. throw new Error( 'THREE.Renderer: .compute() expects a ComputeNode.' );
  536. }
  537. backend.beginCompute( computeNodes );
  538. for ( const computeNode of computeList ) {
  539. // onInit
  540. if ( pipelines.has( computeNode ) === false ) {
  541. const dispose = () => {
  542. computeNode.removeEventListener( 'dispose', dispose );
  543. pipelines.delete( computeNode );
  544. bindings.delete( computeNode );
  545. nodes.delete( computeNode );
  546. };
  547. computeNode.addEventListener( 'dispose', dispose );
  548. //
  549. computeNode.onInit( { renderer: this } );
  550. }
  551. nodes.updateForCompute( computeNode );
  552. bindings.updateForCompute( computeNode );
  553. const computeBindings = bindings.getForCompute( computeNode );
  554. const computePipeline = pipelines.getForCompute( computeNode, computeBindings );
  555. backend.compute( computeNodes, computeNode, computeBindings, computePipeline );
  556. }
  557. backend.finishCompute( computeNodes );
  558. await this.backend.resolveTimestampAsync( computeNodes, 'compute' );
  559. //
  560. nodeFrame.renderId = previousRenderId;
  561. }
  562. hasFeatureAsync( name ) {
  563. return this.backend.hasFeatureAsync( name );
  564. }
  565. hasFeature( name ) {
  566. return this.backend.hasFeature( name );
  567. }
  568. copyFramebufferToTexture( framebufferTexture ) {
  569. const renderContext = this._currentRenderContext;
  570. this._textures.updateTexture( framebufferTexture );
  571. this.backend.copyFramebufferToTexture( framebufferTexture, renderContext );
  572. }
  573. copyTextureToTexture( position, srcTexture, dstTexture, level = 0 ) {
  574. this._textures.updateTexture( srcTexture );
  575. this._textures.updateTexture( dstTexture );
  576. this.backend.copyTextureToTexture( position, srcTexture, dstTexture, level );
  577. }
  578. readRenderTargetPixelsAsync( renderTarget, x, y, width, height ) {
  579. return this.backend.copyTextureToBuffer( renderTarget.texture, x, y, width, height );
  580. }
  581. _projectObject( object, camera, groupOrder, renderList ) {
  582. if ( object.visible === false ) return;
  583. const visible = object.layers.test( camera.layers );
  584. if ( visible ) {
  585. if ( object.isGroup ) {
  586. groupOrder = object.renderOrder;
  587. } else if ( object.isLOD ) {
  588. if ( object.autoUpdate === true ) object.update( camera );
  589. } else if ( object.isLight ) {
  590. renderList.pushLight( object );
  591. } else if ( object.isSprite ) {
  592. if ( ! object.frustumCulled || _frustum.intersectsSprite( object ) ) {
  593. if ( this.sortObjects === true ) {
  594. _vector3.setFromMatrixPosition( object.matrixWorld ).applyMatrix4( _projScreenMatrix );
  595. }
  596. const geometry = object.geometry;
  597. const material = object.material;
  598. if ( material.visible ) {
  599. renderList.push( object, geometry, material, groupOrder, _vector3.z, null );
  600. }
  601. }
  602. } else if ( object.isLineLoop ) {
  603. console.error( 'THREE.Renderer: Objects of type THREE.LineLoop are not supported. Please use THREE.Line or THREE.LineSegments.' );
  604. } else if ( object.isMesh || object.isLine || object.isPoints ) {
  605. if ( ! object.frustumCulled || _frustum.intersectsObject( object ) ) {
  606. const geometry = object.geometry;
  607. const material = object.material;
  608. if ( this.sortObjects === true ) {
  609. if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
  610. _vector3
  611. .copy( geometry.boundingSphere.center )
  612. .applyMatrix4( object.matrixWorld )
  613. .applyMatrix4( _projScreenMatrix );
  614. }
  615. if ( Array.isArray( material ) ) {
  616. const groups = geometry.groups;
  617. for ( let i = 0, l = groups.length; i < l; i ++ ) {
  618. const group = groups[ i ];
  619. const groupMaterial = material[ group.materialIndex ];
  620. if ( groupMaterial && groupMaterial.visible ) {
  621. renderList.push( object, geometry, groupMaterial, groupOrder, _vector3.z, group );
  622. }
  623. }
  624. } else if ( material.visible ) {
  625. renderList.push( object, geometry, material, groupOrder, _vector3.z, null );
  626. }
  627. }
  628. }
  629. }
  630. const children = object.children;
  631. for ( let i = 0, l = children.length; i < l; i ++ ) {
  632. this._projectObject( children[ i ], camera, groupOrder, renderList );
  633. }
  634. }
  635. _renderObjects( renderList, camera, scene, lightsNode ) {
  636. // process renderable objects
  637. for ( let i = 0, il = renderList.length; i < il; i ++ ) {
  638. const renderItem = renderList[ i ];
  639. // @TODO: Add support for multiple materials per object. This will require to extract
  640. // the material from the renderItem object and pass it with its group data to renderObject().
  641. const { object, geometry, material, group } = renderItem;
  642. if ( camera.isArrayCamera ) {
  643. const cameras = camera.cameras;
  644. for ( let j = 0, jl = cameras.length; j < jl; j ++ ) {
  645. const camera2 = cameras[ j ];
  646. if ( object.layers.test( camera2.layers ) ) {
  647. const vp = camera2.viewport;
  648. const minDepth = ( vp.minDepth === undefined ) ? 0 : vp.minDepth;
  649. const maxDepth = ( vp.maxDepth === undefined ) ? 1 : vp.maxDepth;
  650. const viewportValue = this._currentRenderContext.viewportValue;
  651. viewportValue.copy( vp ).multiplyScalar( this._pixelRatio ).floor();
  652. viewportValue.minDepth = minDepth;
  653. viewportValue.maxDepth = maxDepth;
  654. this.backend.updateViewport( this._currentRenderContext );
  655. this._currentRenderObjectFunction( object, scene, camera2, geometry, material, group, lightsNode );
  656. }
  657. }
  658. } else {
  659. this._currentRenderObjectFunction( object, scene, camera, geometry, material, group, lightsNode );
  660. }
  661. }
  662. }
  663. renderObject( object, scene, camera, geometry, material, group, lightsNode ) {
  664. let overridePositionNode;
  665. let overrideFragmentNode;
  666. //
  667. object.onBeforeRender( this, scene, camera, geometry, material, group );
  668. material.onBeforeRender( this, scene, camera, geometry, material, group );
  669. //
  670. if ( scene.overrideMaterial !== null ) {
  671. const overrideMaterial = scene.overrideMaterial;
  672. if ( material.positionNode && material.positionNode.isNode ) {
  673. overridePositionNode = overrideMaterial.positionNode;
  674. overrideMaterial.positionNode = material.positionNode;
  675. }
  676. if ( overrideMaterial.isShadowNodeMaterial ) {
  677. overrideMaterial.side = material.shadowSide === null ? material.side : material.shadowSide;
  678. if ( material.shadowNode && material.shadowNode.isNode ) {
  679. overrideFragmentNode = overrideMaterial.fragmentNode;
  680. overrideMaterial.fragmentNode = material.shadowNode;
  681. }
  682. if ( this.localClippingEnabled ) {
  683. if ( material.clipShadows ) {
  684. if ( overrideMaterial.clippingPlanes !== material.clippingPlanes ) {
  685. overrideMaterial.clippingPlanes = material.clippingPlanes;
  686. overrideMaterial.needsUpdate = true;
  687. }
  688. if ( overrideMaterial.clipIntersection !== material.clipIntersection ) {
  689. overrideMaterial.clipIntersection = material.clipIntersection;
  690. }
  691. } else if ( Array.isArray( overrideMaterial.clippingPlanes ) ) {
  692. overrideMaterial.clippingPlanes = null;
  693. overrideMaterial.needsUpdate = true;
  694. }
  695. }
  696. }
  697. material = overrideMaterial;
  698. }
  699. //
  700. if ( material.transparent === true && material.side === DoubleSide && material.forceSinglePass === false ) {
  701. material.side = BackSide;
  702. this._handleObjectFunction( object, material, scene, camera, lightsNode, 'backSide' ); // create backSide pass id
  703. material.side = FrontSide;
  704. this._handleObjectFunction( object, material, scene, camera, lightsNode ); // use default pass id
  705. material.side = DoubleSide;
  706. } else {
  707. this._handleObjectFunction( object, material, scene, camera, lightsNode );
  708. }
  709. //
  710. if ( overridePositionNode !== undefined ) {
  711. scene.overrideMaterial.positionNode = overridePositionNode;
  712. }
  713. if ( overrideFragmentNode !== undefined ) {
  714. scene.overrideMaterial.fragmentNode = overrideFragmentNode;
  715. }
  716. //
  717. object.onAfterRender( this, scene, camera, geometry, material, group );
  718. }
  719. _renderObjectDirect( object, material, scene, camera, lightsNode, passId ) {
  720. const renderObject = this._objects.get( object, material, scene, camera, lightsNode, this._currentRenderContext, passId );
  721. //
  722. this._nodes.updateBefore( renderObject );
  723. //
  724. object.modelViewMatrix.multiplyMatrices( camera.matrixWorldInverse, object.matrixWorld );
  725. object.normalMatrix.getNormalMatrix( object.modelViewMatrix );
  726. //
  727. this._nodes.updateForRender( renderObject );
  728. this._geometries.updateForRender( renderObject );
  729. this._bindings.updateForRender( renderObject );
  730. this._pipelines.updateForRender( renderObject );
  731. //
  732. this.backend.draw( renderObject, this.info );
  733. }
  734. _createObjectPipeline( object, material, scene, camera, lightsNode, passId ) {
  735. const renderObject = this._objects.get( object, material, scene, camera, lightsNode, this._currentRenderContext, passId );
  736. //
  737. this._nodes.updateBefore( renderObject );
  738. //
  739. this._nodes.updateForRender( renderObject );
  740. this._geometries.updateForRender( renderObject );
  741. this._bindings.updateForRender( renderObject );
  742. this._pipelines.getForRender( renderObject, this._compilationPromises );
  743. }
  744. get compute() {
  745. return this.computeAsync;
  746. }
  747. get compile() {
  748. return this.compileAsync;
  749. }
  750. }
  751. export default Renderer;