Renderer.js 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616
  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 QuadMesh from './QuadMesh.js';
  16. import RenderBundles from './RenderBundles.js';
  17. import { NodeMaterial } from '../../nodes/Nodes.js';
  18. import { Scene } from '../../scenes/Scene.js';
  19. import { Frustum } from '../../math/Frustum.js';
  20. import { Matrix4 } from '../../math/Matrix4.js';
  21. import { Vector2 } from '../../math/Vector2.js';
  22. import { Vector3 } from '../../math/Vector3.js';
  23. import { Vector4 } from '../../math/Vector4.js';
  24. import { RenderTarget } from '../../core/RenderTarget.js';
  25. import { DoubleSide, BackSide, FrontSide, SRGBColorSpace, NoColorSpace, NoToneMapping, LinearFilter, LinearSRGBColorSpace, HalfFloatType, RGBAFormat } from '../../constants.js';
  26. const _scene = /*@__PURE__*/ new Scene();
  27. const _drawingBufferSize = /*@__PURE__*/ new Vector2();
  28. const _screen = /*@__PURE__*/ new Vector4();
  29. const _frustum = /*@__PURE__*/ new Frustum();
  30. const _projScreenMatrix = /*@__PURE__*/ new Matrix4();
  31. const _vector3 = /*@__PURE__*/ new Vector3();
  32. class Renderer {
  33. constructor( backend, parameters = {} ) {
  34. this.isRenderer = true;
  35. //
  36. const {
  37. logarithmicDepthBuffer = false,
  38. alpha = true,
  39. antialias = false,
  40. samples = 0
  41. } = parameters;
  42. // public
  43. this.domElement = backend.getDomElement();
  44. this.backend = backend;
  45. this.samples = samples || ( antialias === true ) ? 4 : 0;
  46. this.autoClear = true;
  47. this.autoClearColor = true;
  48. this.autoClearDepth = true;
  49. this.autoClearStencil = true;
  50. this.alpha = alpha;
  51. this.logarithmicDepthBuffer = logarithmicDepthBuffer;
  52. this.outputColorSpace = SRGBColorSpace;
  53. this.toneMapping = NoToneMapping;
  54. this.toneMappingExposure = 1.0;
  55. this.sortObjects = true;
  56. this.depth = true;
  57. this.stencil = false;
  58. this.clippingPlanes = [];
  59. this.info = new Info();
  60. // internals
  61. this._pixelRatio = 1;
  62. this._width = this.domElement.width;
  63. this._height = this.domElement.height;
  64. this._viewport = new Vector4( 0, 0, this._width, this._height );
  65. this._scissor = new Vector4( 0, 0, this._width, this._height );
  66. this._scissorTest = false;
  67. this._attributes = null;
  68. this._geometries = null;
  69. this._nodes = null;
  70. this._animation = null;
  71. this._bindings = null;
  72. this._objects = null;
  73. this._pipelines = null;
  74. this._bundles = null;
  75. this._renderLists = null;
  76. this._renderContexts = null;
  77. this._textures = null;
  78. this._background = null;
  79. this._quad = new QuadMesh( new NodeMaterial() );
  80. this._currentRenderContext = null;
  81. this._opaqueSort = null;
  82. this._transparentSort = null;
  83. this._frameBufferTarget = null;
  84. const alphaClear = this.alpha === true ? 0 : 1;
  85. this._clearColor = new Color4( 0, 0, 0, alphaClear );
  86. this._clearDepth = 1;
  87. this._clearStencil = 0;
  88. this._renderTarget = null;
  89. this._activeCubeFace = 0;
  90. this._activeMipmapLevel = 0;
  91. this._mrt = null;
  92. this._renderObjectFunction = null;
  93. this._currentRenderObjectFunction = null;
  94. this._currentRenderBundle = null;
  95. this._handleObjectFunction = this._renderObjectDirect;
  96. this._initialized = false;
  97. this._initPromise = null;
  98. this._compilationPromises = null;
  99. this.transparent = true;
  100. this.opaque = true;
  101. this.shadowMap = {
  102. enabled: false,
  103. type: null
  104. };
  105. this.xr = {
  106. enabled: false
  107. };
  108. this.debug = {
  109. checkShaderErrors: true,
  110. onShaderError: null
  111. };
  112. }
  113. async init() {
  114. if ( this._initialized ) {
  115. throw new Error( 'Renderer: Backend has already been initialized.' );
  116. }
  117. if ( this._initPromise !== null ) {
  118. return this._initPromise;
  119. }
  120. this._initPromise = new Promise( async ( resolve, reject ) => {
  121. const backend = this.backend;
  122. try {
  123. await backend.init( this );
  124. } catch ( error ) {
  125. reject( error );
  126. return;
  127. }
  128. this._nodes = new Nodes( this, backend );
  129. this._animation = new Animation( this._nodes, this.info );
  130. this._attributes = new Attributes( backend );
  131. this._background = new Background( this, this._nodes );
  132. this._geometries = new Geometries( this._attributes, this.info );
  133. this._textures = new Textures( this, backend, this.info );
  134. this._pipelines = new Pipelines( backend, this._nodes );
  135. this._bindings = new Bindings( backend, this._nodes, this._textures, this._attributes, this._pipelines, this.info );
  136. this._objects = new RenderObjects( this, this._nodes, this._geometries, this._pipelines, this._bindings, this.info );
  137. this._renderLists = new RenderLists();
  138. this._bundles = new RenderBundles();
  139. this._renderContexts = new RenderContexts();
  140. //
  141. this._initialized = true;
  142. resolve();
  143. } );
  144. return this._initPromise;
  145. }
  146. get coordinateSystem() {
  147. return this.backend.coordinateSystem;
  148. }
  149. async compileAsync( scene, camera, targetScene = null ) {
  150. if ( this._initialized === false ) await this.init();
  151. // preserve render tree
  152. const nodeFrame = this._nodes.nodeFrame;
  153. const previousRenderId = nodeFrame.renderId;
  154. const previousRenderContext = this._currentRenderContext;
  155. const previousRenderObjectFunction = this._currentRenderObjectFunction;
  156. const previousCompilationPromises = this._compilationPromises;
  157. //
  158. const sceneRef = ( scene.isScene === true ) ? scene : _scene;
  159. if ( targetScene === null ) targetScene = scene;
  160. const renderTarget = this._renderTarget;
  161. const renderContext = this._renderContexts.get( targetScene, camera, renderTarget );
  162. const activeMipmapLevel = this._activeMipmapLevel;
  163. const compilationPromises = [];
  164. this._currentRenderContext = renderContext;
  165. this._currentRenderObjectFunction = this.renderObject;
  166. this._handleObjectFunction = this._createObjectPipeline;
  167. this._compilationPromises = compilationPromises;
  168. nodeFrame.renderId ++;
  169. //
  170. nodeFrame.update();
  171. //
  172. renderContext.depth = this.depth;
  173. renderContext.stencil = this.stencil;
  174. if ( ! renderContext.clippingContext ) renderContext.clippingContext = new ClippingContext();
  175. renderContext.clippingContext.updateGlobal( this, camera );
  176. //
  177. sceneRef.onBeforeRender( this, scene, camera, renderTarget );
  178. //
  179. const renderList = this._renderLists.get( scene, camera );
  180. renderList.begin();
  181. this._projectObject( scene, camera, 0, renderList );
  182. // include lights from target scene
  183. if ( targetScene !== scene ) {
  184. targetScene.traverseVisible( function ( object ) {
  185. if ( object.isLight && object.layers.test( camera.layers ) ) {
  186. renderList.pushLight( object );
  187. }
  188. } );
  189. }
  190. renderList.finish();
  191. //
  192. if ( renderTarget !== null ) {
  193. this._textures.updateRenderTarget( renderTarget, activeMipmapLevel );
  194. const renderTargetData = this._textures.get( renderTarget );
  195. renderContext.textures = renderTargetData.textures;
  196. renderContext.depthTexture = renderTargetData.depthTexture;
  197. } else {
  198. renderContext.textures = null;
  199. renderContext.depthTexture = null;
  200. }
  201. //
  202. this._nodes.updateScene( sceneRef );
  203. //
  204. this._background.update( sceneRef, renderList, renderContext );
  205. // process render lists
  206. const opaqueObjects = renderList.opaque;
  207. const transparentObjects = renderList.transparent;
  208. const lightsNode = renderList.lightsNode;
  209. if ( this.opaque === true && opaqueObjects.length > 0 ) this._renderObjects( opaqueObjects, camera, sceneRef, lightsNode );
  210. if ( this.transparent === true && transparentObjects.length > 0 ) this._renderObjects( transparentObjects, camera, sceneRef, lightsNode );
  211. // restore render tree
  212. nodeFrame.renderId = previousRenderId;
  213. this._currentRenderContext = previousRenderContext;
  214. this._currentRenderObjectFunction = previousRenderObjectFunction;
  215. this._compilationPromises = previousCompilationPromises;
  216. this._handleObjectFunction = this._renderObjectDirect;
  217. // wait for all promises setup by backends awaiting compilation/linking/pipeline creation to complete
  218. await Promise.all( compilationPromises );
  219. }
  220. async renderAsync( scene, camera ) {
  221. if ( this._initialized === false ) await this.init();
  222. const renderContext = this._renderScene( scene, camera );
  223. await this.backend.resolveTimestampAsync( renderContext, 'render' );
  224. }
  225. setMRT( mrt ) {
  226. this._mrt = mrt;
  227. return this;
  228. }
  229. getMRT() {
  230. return this._mrt;
  231. }
  232. _renderBundle( bundle, sceneRef, lightsNode ) {
  233. const { object, camera, renderList } = bundle;
  234. const renderContext = this._currentRenderContext;
  235. const renderContextData = this.backend.get( renderContext );
  236. //
  237. const renderBundle = this._bundles.get( object, camera );
  238. const renderBundleData = this.backend.get( renderBundle );
  239. if ( renderBundleData.renderContexts === undefined ) renderBundleData.renderContexts = new Set();
  240. //
  241. const renderBundleNeedsUpdate = renderBundleData.renderContexts.has( renderContext ) === false || object.needsUpdate === true;
  242. renderBundleData.renderContexts.add( renderContext );
  243. if ( renderBundleNeedsUpdate ) {
  244. if ( renderContextData.renderObjects === undefined || object.needsUpdate === true ) {
  245. const nodeFrame = this._nodes.nodeFrame;
  246. renderContextData.renderObjects = [];
  247. renderContextData.renderBundles = [];
  248. renderContextData.scene = sceneRef;
  249. renderContextData.camera = camera;
  250. renderContextData.renderId = nodeFrame.renderId;
  251. renderContextData.registerBundlesPhase = true;
  252. }
  253. this._currentRenderBundle = renderBundle;
  254. const opaqueObjects = renderList.opaque;
  255. if ( opaqueObjects.length > 0 ) this._renderObjects( opaqueObjects, camera, sceneRef, lightsNode );
  256. this._currentRenderBundle = null;
  257. //
  258. object.needsUpdate = false;
  259. } else {
  260. const renderContext = this._currentRenderContext;
  261. const renderContextData = this.backend.get( renderContext );
  262. for ( let i = 0, l = renderContextData.renderObjects.length; i < l; i ++ ) {
  263. const renderObject = renderContextData.renderObjects[ i ];
  264. this._nodes.updateBefore( renderObject );
  265. //
  266. renderObject.object.modelViewMatrix.multiplyMatrices( camera.matrixWorldInverse, renderObject.object.matrixWorld );
  267. renderObject.object.normalMatrix.getNormalMatrix( renderObject.object.modelViewMatrix );
  268. this._nodes.updateForRender( renderObject );
  269. this._bindings.updateForRender( renderObject );
  270. this.backend.draw( renderObject, this.info );
  271. this._nodes.updateAfter( renderObject );
  272. }
  273. }
  274. }
  275. render( scene, camera ) {
  276. if ( this._initialized === false ) {
  277. console.warn( 'THREE.Renderer: .render() called before the backend is initialized. Try using .renderAsync() instead.' );
  278. return this.renderAsync( scene, camera );
  279. }
  280. this._renderScene( scene, camera );
  281. }
  282. _getFrameBufferTarget() {
  283. const { currentColorSpace } = this;
  284. const useToneMapping = this._renderTarget === null && ( this.toneMapping !== NoToneMapping );
  285. const useColorSpace = currentColorSpace !== LinearSRGBColorSpace && currentColorSpace !== NoColorSpace;
  286. if ( useToneMapping === false && useColorSpace === false ) return null;
  287. const { width, height } = this.getDrawingBufferSize( _drawingBufferSize );
  288. const { depth, stencil } = this;
  289. let frameBufferTarget = this._frameBufferTarget;
  290. if ( frameBufferTarget === null ) {
  291. frameBufferTarget = new RenderTarget( width, height, {
  292. depthBuffer: depth,
  293. stencilBuffer: stencil,
  294. type: HalfFloatType, // FloatType
  295. format: RGBAFormat,
  296. colorSpace: LinearSRGBColorSpace,
  297. generateMipmaps: false,
  298. minFilter: LinearFilter,
  299. magFilter: LinearFilter,
  300. samples: this.samples
  301. } );
  302. frameBufferTarget.isPostProcessingRenderTarget = true;
  303. this._frameBufferTarget = frameBufferTarget;
  304. }
  305. frameBufferTarget.depthBuffer = depth;
  306. frameBufferTarget.stencilBuffer = stencil;
  307. frameBufferTarget.setSize( width, height );
  308. frameBufferTarget.viewport.copy( this._viewport );
  309. frameBufferTarget.scissor.copy( this._scissor );
  310. frameBufferTarget.viewport.multiplyScalar( this._pixelRatio );
  311. frameBufferTarget.scissor.multiplyScalar( this._pixelRatio );
  312. frameBufferTarget.scissorTest = this._scissorTest;
  313. return frameBufferTarget;
  314. }
  315. _renderScene( scene, camera, useFrameBufferTarget = true ) {
  316. const frameBufferTarget = useFrameBufferTarget ? this._getFrameBufferTarget() : null;
  317. // preserve render tree
  318. const nodeFrame = this._nodes.nodeFrame;
  319. const previousRenderId = nodeFrame.renderId;
  320. const previousRenderContext = this._currentRenderContext;
  321. const previousRenderObjectFunction = this._currentRenderObjectFunction;
  322. //
  323. const sceneRef = ( scene.isScene === true ) ? scene : _scene;
  324. const outputRenderTarget = this._renderTarget;
  325. const activeCubeFace = this._activeCubeFace;
  326. const activeMipmapLevel = this._activeMipmapLevel;
  327. //
  328. let renderTarget;
  329. if ( frameBufferTarget !== null ) {
  330. renderTarget = frameBufferTarget;
  331. this.setRenderTarget( renderTarget );
  332. } else {
  333. renderTarget = outputRenderTarget;
  334. }
  335. //
  336. const renderContext = this._renderContexts.get( scene, camera, renderTarget );
  337. this._currentRenderContext = renderContext;
  338. this._currentRenderObjectFunction = this._renderObjectFunction || this.renderObject;
  339. //
  340. this.info.calls ++;
  341. this.info.render.calls ++;
  342. this.info.render.frameCalls ++;
  343. nodeFrame.renderId = this.info.calls;
  344. //
  345. const coordinateSystem = this.coordinateSystem;
  346. if ( camera.coordinateSystem !== coordinateSystem ) {
  347. camera.coordinateSystem = coordinateSystem;
  348. camera.updateProjectionMatrix();
  349. }
  350. //
  351. if ( scene.matrixWorldAutoUpdate === true ) scene.updateMatrixWorld();
  352. if ( camera.parent === null && camera.matrixWorldAutoUpdate === true ) camera.updateMatrixWorld();
  353. //
  354. let viewport = this._viewport;
  355. let scissor = this._scissor;
  356. let pixelRatio = this._pixelRatio;
  357. if ( renderTarget !== null ) {
  358. viewport = renderTarget.viewport;
  359. scissor = renderTarget.scissor;
  360. pixelRatio = 1;
  361. }
  362. this.getDrawingBufferSize( _drawingBufferSize );
  363. _screen.set( 0, 0, _drawingBufferSize.width, _drawingBufferSize.height );
  364. const minDepth = ( viewport.minDepth === undefined ) ? 0 : viewport.minDepth;
  365. const maxDepth = ( viewport.maxDepth === undefined ) ? 1 : viewport.maxDepth;
  366. renderContext.viewportValue.copy( viewport ).multiplyScalar( pixelRatio ).floor();
  367. renderContext.viewportValue.width >>= activeMipmapLevel;
  368. renderContext.viewportValue.height >>= activeMipmapLevel;
  369. renderContext.viewportValue.minDepth = minDepth;
  370. renderContext.viewportValue.maxDepth = maxDepth;
  371. renderContext.viewport = renderContext.viewportValue.equals( _screen ) === false;
  372. renderContext.scissorValue.copy( scissor ).multiplyScalar( pixelRatio ).floor();
  373. renderContext.scissor = this._scissorTest && renderContext.scissorValue.equals( _screen ) === false;
  374. renderContext.scissorValue.width >>= activeMipmapLevel;
  375. renderContext.scissorValue.height >>= activeMipmapLevel;
  376. if ( ! renderContext.clippingContext ) renderContext.clippingContext = new ClippingContext();
  377. renderContext.clippingContext.updateGlobal( this, camera );
  378. //
  379. sceneRef.onBeforeRender( this, scene, camera, renderTarget );
  380. //
  381. _projScreenMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse );
  382. _frustum.setFromProjectionMatrix( _projScreenMatrix, coordinateSystem );
  383. const renderList = this._renderLists.get( scene, camera );
  384. renderList.begin();
  385. this._projectObject( scene, camera, 0, renderList );
  386. renderList.finish();
  387. if ( this.sortObjects === true ) {
  388. renderList.sort( this._opaqueSort, this._transparentSort );
  389. }
  390. //
  391. if ( renderTarget !== null ) {
  392. this._textures.updateRenderTarget( renderTarget, activeMipmapLevel );
  393. const renderTargetData = this._textures.get( renderTarget );
  394. renderContext.textures = renderTargetData.textures;
  395. renderContext.depthTexture = renderTargetData.depthTexture;
  396. renderContext.width = renderTargetData.width;
  397. renderContext.height = renderTargetData.height;
  398. renderContext.renderTarget = renderTarget;
  399. renderContext.depth = renderTarget.depthBuffer;
  400. renderContext.stencil = renderTarget.stencilBuffer;
  401. } else {
  402. renderContext.textures = null;
  403. renderContext.depthTexture = null;
  404. renderContext.width = this.domElement.width;
  405. renderContext.height = this.domElement.height;
  406. renderContext.depth = this.depth;
  407. renderContext.stencil = this.stencil;
  408. }
  409. renderContext.width >>= activeMipmapLevel;
  410. renderContext.height >>= activeMipmapLevel;
  411. renderContext.activeCubeFace = activeCubeFace;
  412. renderContext.activeMipmapLevel = activeMipmapLevel;
  413. renderContext.occlusionQueryCount = renderList.occlusionQueryCount;
  414. //
  415. this._nodes.updateScene( sceneRef );
  416. //
  417. this._background.update( sceneRef, renderList, renderContext );
  418. //
  419. this.backend.beginRender( renderContext );
  420. // process render lists
  421. const opaqueObjects = renderList.opaque;
  422. const transparentObjects = renderList.transparent;
  423. const bundles = renderList.bundles;
  424. const lightsNode = renderList.lightsNode;
  425. if ( bundles.length > 0 ) this._renderBundles( bundles, sceneRef, lightsNode );
  426. if ( this.opaque === true && opaqueObjects.length > 0 ) this._renderObjects( opaqueObjects, camera, sceneRef, lightsNode );
  427. if ( this.transparent === true && transparentObjects.length > 0 ) this._renderObjects( transparentObjects, camera, sceneRef, lightsNode );
  428. // finish render pass
  429. this.backend.finishRender( renderContext );
  430. // restore render tree
  431. nodeFrame.renderId = previousRenderId;
  432. this._currentRenderContext = previousRenderContext;
  433. this._currentRenderObjectFunction = previousRenderObjectFunction;
  434. //
  435. if ( frameBufferTarget !== null ) {
  436. this.setRenderTarget( outputRenderTarget, activeCubeFace, activeMipmapLevel );
  437. const quad = this._quad;
  438. if ( this._nodes.hasOutputChange( renderTarget.texture ) ) {
  439. quad.material.fragmentNode = this._nodes.getOutputNode( renderTarget.texture );
  440. quad.material.needsUpdate = true;
  441. }
  442. this._renderScene( quad, quad.camera, false );
  443. }
  444. //
  445. sceneRef.onAfterRender( this, scene, camera, renderTarget );
  446. //
  447. return renderContext;
  448. }
  449. getMaxAnisotropy() {
  450. return this.backend.getMaxAnisotropy();
  451. }
  452. getActiveCubeFace() {
  453. return this._activeCubeFace;
  454. }
  455. getActiveMipmapLevel() {
  456. return this._activeMipmapLevel;
  457. }
  458. async setAnimationLoop( callback ) {
  459. if ( this._initialized === false ) await this.init();
  460. this._animation.setAnimationLoop( callback );
  461. }
  462. async getArrayBufferAsync( attribute ) {
  463. return await this.backend.getArrayBufferAsync( attribute );
  464. }
  465. getContext() {
  466. return this.backend.getContext();
  467. }
  468. getPixelRatio() {
  469. return this._pixelRatio;
  470. }
  471. getDrawingBufferSize( target ) {
  472. return target.set( this._width * this._pixelRatio, this._height * this._pixelRatio ).floor();
  473. }
  474. getSize( target ) {
  475. return target.set( this._width, this._height );
  476. }
  477. setPixelRatio( value = 1 ) {
  478. this._pixelRatio = value;
  479. this.setSize( this._width, this._height, false );
  480. }
  481. setDrawingBufferSize( width, height, pixelRatio ) {
  482. this._width = width;
  483. this._height = height;
  484. this._pixelRatio = pixelRatio;
  485. this.domElement.width = Math.floor( width * pixelRatio );
  486. this.domElement.height = Math.floor( height * pixelRatio );
  487. this.setViewport( 0, 0, width, height );
  488. if ( this._initialized ) this.backend.updateSize();
  489. }
  490. setSize( width, height, updateStyle = true ) {
  491. this._width = width;
  492. this._height = height;
  493. this.domElement.width = Math.floor( width * this._pixelRatio );
  494. this.domElement.height = Math.floor( height * this._pixelRatio );
  495. if ( updateStyle === true ) {
  496. this.domElement.style.width = width + 'px';
  497. this.domElement.style.height = height + 'px';
  498. }
  499. this.setViewport( 0, 0, width, height );
  500. if ( this._initialized ) this.backend.updateSize();
  501. }
  502. setOpaqueSort( method ) {
  503. this._opaqueSort = method;
  504. }
  505. setTransparentSort( method ) {
  506. this._transparentSort = method;
  507. }
  508. getScissor( target ) {
  509. const scissor = this._scissor;
  510. target.x = scissor.x;
  511. target.y = scissor.y;
  512. target.width = scissor.width;
  513. target.height = scissor.height;
  514. return target;
  515. }
  516. setScissor( x, y, width, height ) {
  517. const scissor = this._scissor;
  518. if ( x.isVector4 ) {
  519. scissor.copy( x );
  520. } else {
  521. scissor.set( x, y, width, height );
  522. }
  523. }
  524. getScissorTest() {
  525. return this._scissorTest;
  526. }
  527. setScissorTest( boolean ) {
  528. this._scissorTest = boolean;
  529. this.backend.setScissorTest( boolean );
  530. }
  531. getViewport( target ) {
  532. return target.copy( this._viewport );
  533. }
  534. setViewport( x, y, width, height, minDepth = 0, maxDepth = 1 ) {
  535. const viewport = this._viewport;
  536. if ( x.isVector4 ) {
  537. viewport.copy( x );
  538. } else {
  539. viewport.set( x, y, width, height );
  540. }
  541. viewport.minDepth = minDepth;
  542. viewport.maxDepth = maxDepth;
  543. }
  544. getClearColor( target ) {
  545. return target.copy( this._clearColor );
  546. }
  547. setClearColor( color, alpha = 1 ) {
  548. this._clearColor.set( color );
  549. this._clearColor.a = alpha;
  550. }
  551. getClearAlpha() {
  552. return this._clearColor.a;
  553. }
  554. setClearAlpha( alpha ) {
  555. this._clearColor.a = alpha;
  556. }
  557. getClearDepth() {
  558. return this._clearDepth;
  559. }
  560. setClearDepth( depth ) {
  561. this._clearDepth = depth;
  562. }
  563. getClearStencil() {
  564. return this._clearStencil;
  565. }
  566. setClearStencil( stencil ) {
  567. this._clearStencil = stencil;
  568. }
  569. isOccluded( object ) {
  570. const renderContext = this._currentRenderContext;
  571. return renderContext && this.backend.isOccluded( renderContext, object );
  572. }
  573. clear( color = true, depth = true, stencil = true ) {
  574. if ( this._initialized === false ) {
  575. console.warn( 'THREE.Renderer: .clear() called before the backend is initialized. Try using .clearAsync() instead.' );
  576. return this.clearAsync( color, depth, stencil );
  577. }
  578. const renderTarget = this._renderTarget || this._getFrameBufferTarget();
  579. let renderTargetData = null;
  580. if ( renderTarget !== null ) {
  581. this._textures.updateRenderTarget( renderTarget );
  582. renderTargetData = this._textures.get( renderTarget );
  583. }
  584. this.backend.clear( color, depth, stencil, renderTargetData );
  585. if ( renderTarget !== null && this._renderTarget === null ) {
  586. // If a color space transform or tone mapping is required,
  587. // the clear operation clears the intermediate renderTarget texture, but does not update the screen canvas.
  588. const quad = this._quad;
  589. if ( this._nodes.hasOutputChange( renderTarget.texture ) ) {
  590. quad.material.fragmentNode = this._nodes.getOutputNode( renderTarget.texture );
  591. quad.material.needsUpdate = true;
  592. }
  593. this._renderScene( quad, quad.camera, false );
  594. }
  595. }
  596. clearColor() {
  597. return this.clear( true, false, false );
  598. }
  599. clearDepth() {
  600. return this.clear( false, true, false );
  601. }
  602. clearStencil() {
  603. return this.clear( false, false, true );
  604. }
  605. async clearAsync( color = true, depth = true, stencil = true ) {
  606. if ( this._initialized === false ) await this.init();
  607. this.clear( color, depth, stencil );
  608. }
  609. clearColorAsync() {
  610. return this.clearAsync( true, false, false );
  611. }
  612. clearDepthAsync() {
  613. return this.clearAsync( false, true, false );
  614. }
  615. clearStencilAsync() {
  616. return this.clearAsync( false, false, true );
  617. }
  618. get currentColorSpace() {
  619. const renderTarget = this._renderTarget;
  620. if ( renderTarget !== null ) {
  621. const texture = renderTarget.texture;
  622. return ( Array.isArray( texture ) ? texture[ 0 ] : texture ).colorSpace;
  623. }
  624. return this.outputColorSpace;
  625. }
  626. dispose() {
  627. this.info.dispose();
  628. this._animation.dispose();
  629. this._objects.dispose();
  630. this._pipelines.dispose();
  631. this._nodes.dispose();
  632. this._bindings.dispose();
  633. this._renderLists.dispose();
  634. this._renderContexts.dispose();
  635. this._textures.dispose();
  636. this.setRenderTarget( null );
  637. this.setAnimationLoop( null );
  638. }
  639. setRenderTarget( renderTarget, activeCubeFace = 0, activeMipmapLevel = 0 ) {
  640. this._renderTarget = renderTarget;
  641. this._activeCubeFace = activeCubeFace;
  642. this._activeMipmapLevel = activeMipmapLevel;
  643. }
  644. getRenderTarget() {
  645. return this._renderTarget;
  646. }
  647. setRenderObjectFunction( renderObjectFunction ) {
  648. this._renderObjectFunction = renderObjectFunction;
  649. }
  650. getRenderObjectFunction() {
  651. return this._renderObjectFunction;
  652. }
  653. async computeAsync( computeNodes ) {
  654. if ( this._initialized === false ) await this.init();
  655. const nodeFrame = this._nodes.nodeFrame;
  656. const previousRenderId = nodeFrame.renderId;
  657. //
  658. this.info.calls ++;
  659. this.info.compute.calls ++;
  660. this.info.compute.frameCalls ++;
  661. nodeFrame.renderId = this.info.calls;
  662. //
  663. const backend = this.backend;
  664. const pipelines = this._pipelines;
  665. const bindings = this._bindings;
  666. const nodes = this._nodes;
  667. const computeList = Array.isArray( computeNodes ) ? computeNodes : [ computeNodes ];
  668. if ( computeList[ 0 ] === undefined || computeList[ 0 ].isComputeNode !== true ) {
  669. throw new Error( 'THREE.Renderer: .compute() expects a ComputeNode.' );
  670. }
  671. backend.beginCompute( computeNodes );
  672. for ( const computeNode of computeList ) {
  673. // onInit
  674. if ( pipelines.has( computeNode ) === false ) {
  675. const dispose = () => {
  676. computeNode.removeEventListener( 'dispose', dispose );
  677. pipelines.delete( computeNode );
  678. bindings.delete( computeNode );
  679. nodes.delete( computeNode );
  680. };
  681. computeNode.addEventListener( 'dispose', dispose );
  682. //
  683. computeNode.onInit( { renderer: this } );
  684. }
  685. nodes.updateForCompute( computeNode );
  686. bindings.updateForCompute( computeNode );
  687. const computeBindings = bindings.getForCompute( computeNode );
  688. const computePipeline = pipelines.getForCompute( computeNode, computeBindings );
  689. backend.compute( computeNodes, computeNode, computeBindings, computePipeline );
  690. }
  691. backend.finishCompute( computeNodes );
  692. await this.backend.resolveTimestampAsync( computeNodes, 'compute' );
  693. //
  694. nodeFrame.renderId = previousRenderId;
  695. }
  696. async hasFeatureAsync( name ) {
  697. if ( this._initialized === false ) await this.init();
  698. return this.backend.hasFeature( name );
  699. }
  700. hasFeature( name ) {
  701. if ( this._initialized === false ) {
  702. console.warn( 'THREE.Renderer: .hasFeature() called before the backend is initialized. Try using .hasFeatureAsync() instead.' );
  703. return false;
  704. }
  705. return this.backend.hasFeature( name );
  706. }
  707. copyFramebufferToTexture( framebufferTexture ) {
  708. const renderContext = this._currentRenderContext;
  709. this._textures.updateTexture( framebufferTexture );
  710. this.backend.copyFramebufferToTexture( framebufferTexture, renderContext );
  711. }
  712. copyTextureToTexture( srcTexture, dstTexture, srcRegion = null, dstPosition = null, level = 0 ) {
  713. this._textures.updateTexture( srcTexture );
  714. this._textures.updateTexture( dstTexture );
  715. this.backend.copyTextureToTexture( srcTexture, dstTexture, srcRegion, dstPosition, level );
  716. }
  717. readRenderTargetPixelsAsync( renderTarget, x, y, width, height, index = 0 ) {
  718. return this.backend.copyTextureToBuffer( renderTarget.textures[ index ], x, y, width, height );
  719. }
  720. _projectObject( object, camera, groupOrder, renderList ) {
  721. if ( object.visible === false ) return;
  722. const visible = object.layers.test( camera.layers );
  723. if ( visible ) {
  724. if ( object.isGroup ) {
  725. groupOrder = object.renderOrder;
  726. } else if ( object.isLOD ) {
  727. if ( object.autoUpdate === true ) object.update( camera );
  728. } else if ( object.isLight ) {
  729. renderList.pushLight( object );
  730. } else if ( object.isSprite ) {
  731. if ( ! object.frustumCulled || _frustum.intersectsSprite( object ) ) {
  732. if ( this.sortObjects === true ) {
  733. _vector3.setFromMatrixPosition( object.matrixWorld ).applyMatrix4( _projScreenMatrix );
  734. }
  735. const geometry = object.geometry;
  736. const material = object.material;
  737. if ( material.visible ) {
  738. renderList.push( object, geometry, material, groupOrder, _vector3.z, null );
  739. }
  740. }
  741. } else if ( object.isLineLoop ) {
  742. console.error( 'THREE.Renderer: Objects of type THREE.LineLoop are not supported. Please use THREE.Line or THREE.LineSegments.' );
  743. } else if ( object.isMesh || object.isLine || object.isPoints ) {
  744. if ( ! object.frustumCulled || _frustum.intersectsObject( object ) ) {
  745. const geometry = object.geometry;
  746. const material = object.material;
  747. if ( this.sortObjects === true ) {
  748. if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
  749. _vector3
  750. .copy( geometry.boundingSphere.center )
  751. .applyMatrix4( object.matrixWorld )
  752. .applyMatrix4( _projScreenMatrix );
  753. }
  754. if ( Array.isArray( material ) ) {
  755. const groups = geometry.groups;
  756. for ( let i = 0, l = groups.length; i < l; i ++ ) {
  757. const group = groups[ i ];
  758. const groupMaterial = material[ group.materialIndex ];
  759. if ( groupMaterial && groupMaterial.visible ) {
  760. renderList.push( object, geometry, groupMaterial, groupOrder, _vector3.z, group );
  761. }
  762. }
  763. } else if ( material.visible ) {
  764. renderList.push( object, geometry, material, groupOrder, _vector3.z, null );
  765. }
  766. }
  767. }
  768. }
  769. if ( object.static === true ) {
  770. const baseRenderList = renderList;
  771. // replace render list
  772. renderList = this._renderLists.get( object, camera );
  773. renderList.begin();
  774. baseRenderList.pushBundle( {
  775. object,
  776. camera,
  777. renderList,
  778. } );
  779. renderList.finish();
  780. }
  781. const children = object.children;
  782. for ( let i = 0, l = children.length; i < l; i ++ ) {
  783. this._projectObject( children[ i ], camera, groupOrder, renderList );
  784. }
  785. }
  786. _renderBundles( bundles, sceneRef, lightsNode ) {
  787. for ( const bundle of bundles ) {
  788. this._renderBundle( bundle, sceneRef, lightsNode );
  789. }
  790. }
  791. _renderObjects( renderList, camera, scene, lightsNode ) {
  792. // process renderable objects
  793. for ( let i = 0, il = renderList.length; i < il; i ++ ) {
  794. const renderItem = renderList[ i ];
  795. // @TODO: Add support for multiple materials per object. This will require to extract
  796. // the material from the renderItem object and pass it with its group data to renderObject().
  797. const { object, geometry, material, group } = renderItem;
  798. if ( camera.isArrayCamera ) {
  799. const cameras = camera.cameras;
  800. for ( let j = 0, jl = cameras.length; j < jl; j ++ ) {
  801. const camera2 = cameras[ j ];
  802. if ( object.layers.test( camera2.layers ) ) {
  803. const vp = camera2.viewport;
  804. const minDepth = ( vp.minDepth === undefined ) ? 0 : vp.minDepth;
  805. const maxDepth = ( vp.maxDepth === undefined ) ? 1 : vp.maxDepth;
  806. const viewportValue = this._currentRenderContext.viewportValue;
  807. viewportValue.copy( vp ).multiplyScalar( this._pixelRatio ).floor();
  808. viewportValue.minDepth = minDepth;
  809. viewportValue.maxDepth = maxDepth;
  810. this.backend.updateViewport( this._currentRenderContext );
  811. this._currentRenderObjectFunction( object, scene, camera2, geometry, material, group, lightsNode );
  812. }
  813. }
  814. } else {
  815. this._currentRenderObjectFunction( object, scene, camera, geometry, material, group, lightsNode );
  816. }
  817. }
  818. }
  819. renderObject( object, scene, camera, geometry, material, group, lightsNode ) {
  820. let overridePositionNode;
  821. let overrideFragmentNode;
  822. let overrideDepthNode;
  823. //
  824. object.onBeforeRender( this, scene, camera, geometry, material, group );
  825. //
  826. if ( scene.overrideMaterial !== null ) {
  827. const overrideMaterial = scene.overrideMaterial;
  828. if ( material.positionNode && material.positionNode.isNode ) {
  829. overridePositionNode = overrideMaterial.positionNode;
  830. overrideMaterial.positionNode = material.positionNode;
  831. }
  832. if ( overrideMaterial.isShadowNodeMaterial ) {
  833. overrideMaterial.side = material.shadowSide === null ? material.side : material.shadowSide;
  834. if ( material.depthNode && material.depthNode.isNode ) {
  835. overrideDepthNode = overrideMaterial.depthNode;
  836. overrideMaterial.depthNode = material.depthNode;
  837. }
  838. if ( material.shadowNode && material.shadowNode.isNode ) {
  839. overrideFragmentNode = overrideMaterial.fragmentNode;
  840. overrideMaterial.fragmentNode = material.shadowNode;
  841. }
  842. if ( this.localClippingEnabled ) {
  843. if ( material.clipShadows ) {
  844. if ( overrideMaterial.clippingPlanes !== material.clippingPlanes ) {
  845. overrideMaterial.clippingPlanes = material.clippingPlanes;
  846. overrideMaterial.needsUpdate = true;
  847. }
  848. if ( overrideMaterial.clipIntersection !== material.clipIntersection ) {
  849. overrideMaterial.clipIntersection = material.clipIntersection;
  850. }
  851. } else if ( Array.isArray( overrideMaterial.clippingPlanes ) ) {
  852. overrideMaterial.clippingPlanes = null;
  853. overrideMaterial.needsUpdate = true;
  854. }
  855. }
  856. }
  857. material = overrideMaterial;
  858. }
  859. //
  860. if ( material.transparent === true && material.side === DoubleSide && material.forceSinglePass === false ) {
  861. material.side = BackSide;
  862. this._handleObjectFunction( object, material, scene, camera, lightsNode, group, 'backSide' ); // create backSide pass id
  863. material.side = FrontSide;
  864. this._handleObjectFunction( object, material, scene, camera, lightsNode, group ); // use default pass id
  865. material.side = DoubleSide;
  866. } else {
  867. this._handleObjectFunction( object, material, scene, camera, lightsNode, group );
  868. }
  869. //
  870. if ( overridePositionNode !== undefined ) {
  871. scene.overrideMaterial.positionNode = overridePositionNode;
  872. }
  873. if ( overrideDepthNode !== undefined ) {
  874. scene.overrideMaterial.depthNode = overrideDepthNode;
  875. }
  876. if ( overrideFragmentNode !== undefined ) {
  877. scene.overrideMaterial.fragmentNode = overrideFragmentNode;
  878. }
  879. //
  880. object.onAfterRender( this, scene, camera, geometry, material, group );
  881. }
  882. _renderObjectDirect( object, material, scene, camera, lightsNode, group, passId ) {
  883. const renderObject = this._objects.get( object, material, scene, camera, lightsNode, this._currentRenderContext, passId );
  884. renderObject.drawRange = group || object.geometry.drawRange;
  885. //
  886. this._nodes.updateBefore( renderObject );
  887. //
  888. object.modelViewMatrix.multiplyMatrices( camera.matrixWorldInverse, object.matrixWorld );
  889. object.normalMatrix.getNormalMatrix( object.modelViewMatrix );
  890. //
  891. this._nodes.updateForRender( renderObject );
  892. this._geometries.updateForRender( renderObject );
  893. this._bindings.updateForRender( renderObject );
  894. this._pipelines.updateForRender( renderObject );
  895. //
  896. if ( this._currentRenderBundle !== null && this._currentRenderBundle.needsUpdate === true ) {
  897. const renderObjectData = this.backend.get( renderObject );
  898. renderObjectData.bundleEncoder = undefined;
  899. renderObjectData.lastPipelineGPU = undefined;
  900. }
  901. this.backend.draw( renderObject, this.info );
  902. if ( this._currentRenderBundle !== null ) {
  903. const renderContextData = this.backend.get( this._currentRenderContext );
  904. renderContextData.renderObjects.push( renderObject );
  905. }
  906. this._nodes.updateAfter( renderObject );
  907. }
  908. _createObjectPipeline( object, material, scene, camera, lightsNode, passId ) {
  909. const renderObject = this._objects.get( object, material, scene, camera, lightsNode, this._currentRenderContext, passId );
  910. //
  911. this._nodes.updateBefore( renderObject );
  912. //
  913. this._nodes.updateForRender( renderObject );
  914. this._geometries.updateForRender( renderObject );
  915. this._bindings.updateForRender( renderObject );
  916. this._pipelines.getForRender( renderObject, this._compilationPromises );
  917. this._nodes.updateAfter( renderObject );
  918. }
  919. get compute() {
  920. return this.computeAsync;
  921. }
  922. get compile() {
  923. return this.compileAsync;
  924. }
  925. }
  926. export default Renderer;