Renderer.js 35 KB

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