Renderer.js 35 KB

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