Renderer.js 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304
  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. this.info.calls ++;
  520. this.info.compute.calls ++;
  521. this.info.compute.computeCalls ++;
  522. nodeFrame.renderId = this.info.calls;
  523. //
  524. if ( this.info.autoReset === true ) this.info.resetCompute();
  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. readRenderTargetPixelsAsync( renderTarget, x, y, width, height ) {
  570. return this.backend.copyTextureToBuffer( renderTarget.texture, x, y, width, height );
  571. }
  572. _projectObject( object, camera, groupOrder, renderList ) {
  573. if ( object.visible === false ) return;
  574. const visible = object.layers.test( camera.layers );
  575. if ( visible ) {
  576. if ( object.isGroup ) {
  577. groupOrder = object.renderOrder;
  578. } else if ( object.isLOD ) {
  579. if ( object.autoUpdate === true ) object.update( camera );
  580. } else if ( object.isLight ) {
  581. renderList.pushLight( object );
  582. } else if ( object.isSprite ) {
  583. if ( ! object.frustumCulled || _frustum.intersectsSprite( object ) ) {
  584. if ( this.sortObjects === true ) {
  585. _vector3.setFromMatrixPosition( object.matrixWorld ).applyMatrix4( _projScreenMatrix );
  586. }
  587. const geometry = object.geometry;
  588. const material = object.material;
  589. if ( material.visible ) {
  590. renderList.push( object, geometry, material, groupOrder, _vector3.z, null );
  591. }
  592. }
  593. } else if ( object.isLineLoop ) {
  594. console.error( 'THREE.Renderer: Objects of type THREE.LineLoop are not supported. Please use THREE.Line or THREE.LineSegments.' );
  595. } else if ( object.isMesh || object.isLine || object.isPoints ) {
  596. if ( ! object.frustumCulled || _frustum.intersectsObject( object ) ) {
  597. const geometry = object.geometry;
  598. const material = object.material;
  599. if ( this.sortObjects === true ) {
  600. if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
  601. _vector3
  602. .copy( geometry.boundingSphere.center )
  603. .applyMatrix4( object.matrixWorld )
  604. .applyMatrix4( _projScreenMatrix );
  605. }
  606. if ( Array.isArray( material ) ) {
  607. const groups = geometry.groups;
  608. for ( let i = 0, l = groups.length; i < l; i ++ ) {
  609. const group = groups[ i ];
  610. const groupMaterial = material[ group.materialIndex ];
  611. if ( groupMaterial && groupMaterial.visible ) {
  612. renderList.push( object, geometry, groupMaterial, groupOrder, _vector3.z, group );
  613. }
  614. }
  615. } else if ( material.visible ) {
  616. renderList.push( object, geometry, material, groupOrder, _vector3.z, null );
  617. }
  618. }
  619. }
  620. }
  621. const children = object.children;
  622. for ( let i = 0, l = children.length; i < l; i ++ ) {
  623. this._projectObject( children[ i ], camera, groupOrder, renderList );
  624. }
  625. }
  626. _renderObjects( renderList, camera, scene, lightsNode ) {
  627. // process renderable objects
  628. for ( let i = 0, il = renderList.length; i < il; i ++ ) {
  629. const renderItem = renderList[ i ];
  630. // @TODO: Add support for multiple materials per object. This will require to extract
  631. // the material from the renderItem object and pass it with its group data to renderObject().
  632. const { object, geometry, material, group } = renderItem;
  633. if ( camera.isArrayCamera ) {
  634. const cameras = camera.cameras;
  635. for ( let j = 0, jl = cameras.length; j < jl; j ++ ) {
  636. const camera2 = cameras[ j ];
  637. if ( object.layers.test( camera2.layers ) ) {
  638. const vp = camera2.viewport;
  639. const minDepth = ( vp.minDepth === undefined ) ? 0 : vp.minDepth;
  640. const maxDepth = ( vp.maxDepth === undefined ) ? 1 : vp.maxDepth;
  641. const viewportValue = this._currentRenderContext.viewportValue;
  642. viewportValue.copy( vp ).multiplyScalar( this._pixelRatio ).floor();
  643. viewportValue.minDepth = minDepth;
  644. viewportValue.maxDepth = maxDepth;
  645. this.backend.updateViewport( this._currentRenderContext );
  646. this._currentRenderObjectFunction( object, scene, camera2, geometry, material, group, lightsNode );
  647. }
  648. }
  649. } else {
  650. this._currentRenderObjectFunction( object, scene, camera, geometry, material, group, lightsNode );
  651. }
  652. }
  653. }
  654. renderObject( object, scene, camera, geometry, material, group, lightsNode ) {
  655. let overridePositionNode;
  656. let overrideFragmentNode;
  657. //
  658. object.onBeforeRender( this, scene, camera, geometry, material, group );
  659. material.onBeforeRender( this, scene, camera, geometry, material, group );
  660. //
  661. if ( scene.overrideMaterial !== null ) {
  662. const overrideMaterial = scene.overrideMaterial;
  663. if ( material.positionNode && material.positionNode.isNode ) {
  664. overridePositionNode = overrideMaterial.positionNode;
  665. overrideMaterial.positionNode = material.positionNode;
  666. }
  667. if ( overrideMaterial.isShadowNodeMaterial ) {
  668. overrideMaterial.side = material.shadowSide === null ? material.side : material.shadowSide;
  669. if ( material.shadowNode && material.shadowNode.isNode ) {
  670. overrideFragmentNode = overrideMaterial.fragmentNode;
  671. overrideMaterial.fragmentNode = material.shadowNode;
  672. }
  673. if ( this.localClippingEnabled ) {
  674. if ( material.clipShadows ) {
  675. if ( overrideMaterial.clippingPlanes !== material.clippingPlanes ) {
  676. overrideMaterial.clippingPlanes = material.clippingPlanes;
  677. overrideMaterial.needsUpdate = true;
  678. }
  679. if ( overrideMaterial.clipIntersection !== material.clipIntersection ) {
  680. overrideMaterial.clipIntersection = material.clipIntersection;
  681. }
  682. } else if ( Array.isArray( overrideMaterial.clippingPlanes ) ) {
  683. overrideMaterial.clippingPlanes = null;
  684. overrideMaterial.needsUpdate = true;
  685. }
  686. }
  687. }
  688. material = overrideMaterial;
  689. }
  690. //
  691. if ( material.transparent === true && material.side === DoubleSide && material.forceSinglePass === false ) {
  692. material.side = BackSide;
  693. this._handleObjectFunction( object, material, scene, camera, lightsNode, 'backSide' ); // create backSide pass id
  694. material.side = FrontSide;
  695. this._handleObjectFunction( object, material, scene, camera, lightsNode ); // use default pass id
  696. material.side = DoubleSide;
  697. } else {
  698. this._handleObjectFunction( object, material, scene, camera, lightsNode );
  699. }
  700. //
  701. if ( overridePositionNode !== undefined ) {
  702. scene.overrideMaterial.positionNode = overridePositionNode;
  703. }
  704. if ( overrideFragmentNode !== undefined ) {
  705. scene.overrideMaterial.fragmentNode = overrideFragmentNode;
  706. }
  707. //
  708. object.onAfterRender( this, scene, camera, geometry, material, group );
  709. }
  710. _renderObjectDirect( object, material, scene, camera, lightsNode, passId ) {
  711. const renderObject = this._objects.get( object, material, scene, camera, lightsNode, this._currentRenderContext, passId );
  712. //
  713. this._nodes.updateBefore( renderObject );
  714. //
  715. object.modelViewMatrix.multiplyMatrices( camera.matrixWorldInverse, object.matrixWorld );
  716. object.normalMatrix.getNormalMatrix( object.modelViewMatrix );
  717. //
  718. this._nodes.updateForRender( renderObject );
  719. this._geometries.updateForRender( renderObject );
  720. this._bindings.updateForRender( renderObject );
  721. this._pipelines.updateForRender( renderObject );
  722. //
  723. this.backend.draw( renderObject, this.info );
  724. }
  725. _createObjectPipeline( object, material, scene, camera, lightsNode, passId ) {
  726. const renderObject = this._objects.get( object, material, scene, camera, lightsNode, this._currentRenderContext, passId );
  727. //
  728. this._nodes.updateBefore( renderObject );
  729. //
  730. this._nodes.updateForRender( renderObject );
  731. this._geometries.updateForRender( renderObject );
  732. this._bindings.updateForRender( renderObject );
  733. this._pipelines.getForRender( renderObject, this._compilationPromises );
  734. }
  735. get compute() {
  736. return this.computeAsync;
  737. }
  738. get compile() {
  739. return this.compileAsync;
  740. }
  741. }
  742. export default Renderer;