Renderer.js 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575
  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. this.info.render.frameCalls ++;
  326. nodeFrame.renderId = this.info.calls;
  327. //
  328. const coordinateSystem = this.coordinateSystem;
  329. if ( camera.coordinateSystem !== coordinateSystem ) {
  330. camera.coordinateSystem = coordinateSystem;
  331. camera.updateProjectionMatrix();
  332. }
  333. //
  334. if ( scene.matrixWorldAutoUpdate === true ) scene.updateMatrixWorld();
  335. if ( camera.parent === null && camera.matrixWorldAutoUpdate === true ) camera.updateMatrixWorld();
  336. //
  337. let viewport = this._viewport;
  338. let scissor = this._scissor;
  339. let pixelRatio = this._pixelRatio;
  340. if ( renderTarget !== null ) {
  341. viewport = renderTarget.viewport;
  342. scissor = renderTarget.scissor;
  343. pixelRatio = 1;
  344. }
  345. this.getDrawingBufferSize( _drawingBufferSize );
  346. _screen.set( 0, 0, _drawingBufferSize.width, _drawingBufferSize.height );
  347. const minDepth = ( viewport.minDepth === undefined ) ? 0 : viewport.minDepth;
  348. const maxDepth = ( viewport.maxDepth === undefined ) ? 1 : viewport.maxDepth;
  349. renderContext.viewportValue.copy( viewport ).multiplyScalar( pixelRatio ).floor();
  350. renderContext.viewportValue.width >>= activeMipmapLevel;
  351. renderContext.viewportValue.height >>= activeMipmapLevel;
  352. renderContext.viewportValue.minDepth = minDepth;
  353. renderContext.viewportValue.maxDepth = maxDepth;
  354. renderContext.viewport = renderContext.viewportValue.equals( _screen ) === false;
  355. renderContext.scissorValue.copy( scissor ).multiplyScalar( pixelRatio ).floor();
  356. renderContext.scissor = this._scissorTest && renderContext.scissorValue.equals( _screen ) === false;
  357. renderContext.scissorValue.width >>= activeMipmapLevel;
  358. renderContext.scissorValue.height >>= activeMipmapLevel;
  359. if ( ! renderContext.clippingContext ) renderContext.clippingContext = new ClippingContext();
  360. renderContext.clippingContext.updateGlobal( this, camera );
  361. //
  362. sceneRef.onBeforeRender( this, scene, camera, renderTarget );
  363. //
  364. _projScreenMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse );
  365. _frustum.setFromProjectionMatrix( _projScreenMatrix, coordinateSystem );
  366. const renderList = this._renderLists.get( scene, camera );
  367. renderList.begin();
  368. this._projectObject( scene, camera, 0, renderList );
  369. renderList.finish();
  370. if ( this.sortObjects === true ) {
  371. renderList.sort( this._opaqueSort, this._transparentSort );
  372. }
  373. //
  374. if ( renderTarget !== null ) {
  375. this._textures.updateRenderTarget( renderTarget, activeMipmapLevel );
  376. const renderTargetData = this._textures.get( renderTarget );
  377. renderContext.textures = renderTargetData.textures;
  378. renderContext.depthTexture = renderTargetData.depthTexture;
  379. renderContext.width = renderTargetData.width;
  380. renderContext.height = renderTargetData.height;
  381. renderContext.renderTarget = renderTarget;
  382. renderContext.depth = renderTarget.depthBuffer;
  383. renderContext.stencil = renderTarget.stencilBuffer;
  384. } else {
  385. renderContext.textures = null;
  386. renderContext.depthTexture = null;
  387. renderContext.width = this.domElement.width;
  388. renderContext.height = this.domElement.height;
  389. renderContext.depth = this.depth;
  390. renderContext.stencil = this.stencil;
  391. }
  392. renderContext.width >>= activeMipmapLevel;
  393. renderContext.height >>= activeMipmapLevel;
  394. renderContext.activeCubeFace = activeCubeFace;
  395. renderContext.activeMipmapLevel = activeMipmapLevel;
  396. renderContext.occlusionQueryCount = renderList.occlusionQueryCount;
  397. //
  398. this._nodes.updateScene( sceneRef );
  399. //
  400. this._background.update( sceneRef, renderList, renderContext );
  401. //
  402. this.backend.beginRender( renderContext );
  403. // process render lists
  404. const opaqueObjects = renderList.opaque;
  405. const transparentObjects = renderList.transparent;
  406. const bundles = renderList.bundles;
  407. const lightsNode = renderList.lightsNode;
  408. if ( bundles.length > 0 ) this._renderBundles( bundles, sceneRef, lightsNode );
  409. if ( opaqueObjects.length > 0 ) this._renderObjects( opaqueObjects, camera, sceneRef, lightsNode );
  410. if ( transparentObjects.length > 0 ) this._renderObjects( transparentObjects, camera, sceneRef, lightsNode );
  411. // finish render pass
  412. this.backend.finishRender( renderContext );
  413. // restore render tree
  414. nodeFrame.renderId = previousRenderId;
  415. this._currentRenderContext = previousRenderContext;
  416. this._currentRenderObjectFunction = previousRenderObjectFunction;
  417. //
  418. if ( frameBufferTarget !== null ) {
  419. this.setRenderTarget( outputRenderTarget, activeCubeFace, activeMipmapLevel );
  420. _quad.material.fragmentNode = this._nodes.getOutputNode( renderTarget.texture );
  421. this._renderScene( _quad, _quad.camera, false );
  422. }
  423. //
  424. sceneRef.onAfterRender( this, scene, camera, renderTarget );
  425. //
  426. return renderContext;
  427. }
  428. getMaxAnisotropy() {
  429. return this.backend.getMaxAnisotropy();
  430. }
  431. getActiveCubeFace() {
  432. return this._activeCubeFace;
  433. }
  434. getActiveMipmapLevel() {
  435. return this._activeMipmapLevel;
  436. }
  437. async setAnimationLoop( callback ) {
  438. if ( this._initialized === false ) await this.init();
  439. this._animation.setAnimationLoop( callback );
  440. }
  441. async getArrayBufferAsync( attribute ) {
  442. return await this.backend.getArrayBufferAsync( attribute );
  443. }
  444. getContext() {
  445. return this.backend.getContext();
  446. }
  447. getPixelRatio() {
  448. return this._pixelRatio;
  449. }
  450. getDrawingBufferSize( target ) {
  451. return target.set( this._width * this._pixelRatio, this._height * this._pixelRatio ).floor();
  452. }
  453. getSize( target ) {
  454. return target.set( this._width, this._height );
  455. }
  456. setPixelRatio( value = 1 ) {
  457. this._pixelRatio = value;
  458. this.setSize( this._width, this._height, false );
  459. }
  460. setDrawingBufferSize( width, height, pixelRatio ) {
  461. this._width = width;
  462. this._height = height;
  463. this._pixelRatio = pixelRatio;
  464. this.domElement.width = Math.floor( width * pixelRatio );
  465. this.domElement.height = Math.floor( height * pixelRatio );
  466. this.setViewport( 0, 0, width, height );
  467. if ( this._initialized ) this.backend.updateSize();
  468. }
  469. setSize( width, height, updateStyle = true ) {
  470. this._width = width;
  471. this._height = height;
  472. this.domElement.width = Math.floor( width * this._pixelRatio );
  473. this.domElement.height = Math.floor( height * this._pixelRatio );
  474. if ( updateStyle === true ) {
  475. this.domElement.style.width = width + 'px';
  476. this.domElement.style.height = height + 'px';
  477. }
  478. this.setViewport( 0, 0, width, height );
  479. if ( this._initialized ) this.backend.updateSize();
  480. }
  481. setOpaqueSort( method ) {
  482. this._opaqueSort = method;
  483. }
  484. setTransparentSort( method ) {
  485. this._transparentSort = method;
  486. }
  487. getScissor( target ) {
  488. const scissor = this._scissor;
  489. target.x = scissor.x;
  490. target.y = scissor.y;
  491. target.width = scissor.width;
  492. target.height = scissor.height;
  493. return target;
  494. }
  495. setScissor( x, y, width, height ) {
  496. const scissor = this._scissor;
  497. if ( x.isVector4 ) {
  498. scissor.copy( x );
  499. } else {
  500. scissor.set( x, y, width, height );
  501. }
  502. }
  503. getScissorTest() {
  504. return this._scissorTest;
  505. }
  506. setScissorTest( boolean ) {
  507. this._scissorTest = boolean;
  508. this.backend.setScissorTest( boolean );
  509. }
  510. getViewport( target ) {
  511. return target.copy( this._viewport );
  512. }
  513. setViewport( x, y, width, height, minDepth = 0, maxDepth = 1 ) {
  514. const viewport = this._viewport;
  515. if ( x.isVector4 ) {
  516. viewport.copy( x );
  517. } else {
  518. viewport.set( x, y, width, height );
  519. }
  520. viewport.minDepth = minDepth;
  521. viewport.maxDepth = maxDepth;
  522. }
  523. getClearColor( target ) {
  524. return target.copy( this._clearColor );
  525. }
  526. setClearColor( color, alpha = 1 ) {
  527. this._clearColor.set( color );
  528. this._clearColor.a = alpha;
  529. }
  530. getClearAlpha() {
  531. return this._clearColor.a;
  532. }
  533. setClearAlpha( alpha ) {
  534. this._clearColor.a = alpha;
  535. }
  536. getClearDepth() {
  537. return this._clearDepth;
  538. }
  539. setClearDepth( depth ) {
  540. this._clearDepth = depth;
  541. }
  542. getClearStencil() {
  543. return this._clearStencil;
  544. }
  545. setClearStencil( stencil ) {
  546. this._clearStencil = stencil;
  547. }
  548. isOccluded( object ) {
  549. const renderContext = this._currentRenderContext;
  550. return renderContext && this.backend.isOccluded( renderContext, object );
  551. }
  552. clear( color = true, depth = true, stencil = true ) {
  553. if ( this._initialized === false ) {
  554. console.warn( 'THREE.Renderer: .clear() called before the backend is initialized. Try using .clearAsync() instead.' );
  555. return this.clearAsync( color, depth, stencil );
  556. }
  557. const renderTarget = this._renderTarget || this._getFrameBufferTarget();
  558. let renderTargetData = null;
  559. if ( renderTarget !== null ) {
  560. this._textures.updateRenderTarget( renderTarget );
  561. renderTargetData = this._textures.get( renderTarget );
  562. }
  563. this.backend.clear( color, depth, stencil, renderTargetData );
  564. if ( renderTarget !== null && this._renderTarget === null ) {
  565. // If a color space transform or tone mapping is required,
  566. // the clear operation clears the intermediate renderTarget texture, but does not update the screen canvas.
  567. _quad.material.fragmentNode = this._nodes.getOutputNode( renderTarget.texture );
  568. this._renderScene( _quad, _quad.camera, false );
  569. }
  570. }
  571. clearColor() {
  572. return this.clear( true, false, false );
  573. }
  574. clearDepth() {
  575. return this.clear( false, true, false );
  576. }
  577. clearStencil() {
  578. return this.clear( false, false, true );
  579. }
  580. async clearAsync( color = true, depth = true, stencil = true ) {
  581. if ( this._initialized === false ) await this.init();
  582. this.clear( color, depth, stencil );
  583. }
  584. clearColorAsync() {
  585. return this.clearAsync( true, false, false );
  586. }
  587. clearDepthAsync() {
  588. return this.clearAsync( false, true, false );
  589. }
  590. clearStencilAsync() {
  591. return this.clearAsync( false, false, true );
  592. }
  593. get currentColorSpace() {
  594. const renderTarget = this._renderTarget;
  595. if ( renderTarget !== null ) {
  596. const texture = renderTarget.texture;
  597. return ( Array.isArray( texture ) ? texture[ 0 ] : texture ).colorSpace;
  598. }
  599. return this.outputColorSpace;
  600. }
  601. dispose() {
  602. this.info.dispose();
  603. this._animation.dispose();
  604. this._objects.dispose();
  605. this._pipelines.dispose();
  606. this._nodes.dispose();
  607. this._bindings.dispose();
  608. this._renderLists.dispose();
  609. this._renderContexts.dispose();
  610. this._textures.dispose();
  611. this.setRenderTarget( null );
  612. this.setAnimationLoop( null );
  613. }
  614. setRenderTarget( renderTarget, activeCubeFace = 0, activeMipmapLevel = 0 ) {
  615. this._renderTarget = renderTarget;
  616. this._activeCubeFace = activeCubeFace;
  617. this._activeMipmapLevel = activeMipmapLevel;
  618. }
  619. getRenderTarget() {
  620. return this._renderTarget;
  621. }
  622. setRenderObjectFunction( renderObjectFunction ) {
  623. this._renderObjectFunction = renderObjectFunction;
  624. }
  625. getRenderObjectFunction() {
  626. return this._renderObjectFunction;
  627. }
  628. async computeAsync( computeNodes ) {
  629. if ( this._initialized === false ) await this.init();
  630. const nodeFrame = this._nodes.nodeFrame;
  631. const previousRenderId = nodeFrame.renderId;
  632. //
  633. this.info.calls ++;
  634. this.info.compute.calls ++;
  635. this.info.compute.frameCalls ++;
  636. nodeFrame.renderId = this.info.calls;
  637. //
  638. const backend = this.backend;
  639. const pipelines = this._pipelines;
  640. const bindings = this._bindings;
  641. const nodes = this._nodes;
  642. const computeList = Array.isArray( computeNodes ) ? computeNodes : [ computeNodes ];
  643. if ( computeList[ 0 ] === undefined || computeList[ 0 ].isComputeNode !== true ) {
  644. throw new Error( 'THREE.Renderer: .compute() expects a ComputeNode.' );
  645. }
  646. backend.beginCompute( computeNodes );
  647. for ( const computeNode of computeList ) {
  648. // onInit
  649. if ( pipelines.has( computeNode ) === false ) {
  650. const dispose = () => {
  651. computeNode.removeEventListener( 'dispose', dispose );
  652. pipelines.delete( computeNode );
  653. bindings.delete( computeNode );
  654. nodes.delete( computeNode );
  655. };
  656. computeNode.addEventListener( 'dispose', dispose );
  657. //
  658. computeNode.onInit( { renderer: this } );
  659. }
  660. nodes.updateForCompute( computeNode );
  661. bindings.updateForCompute( computeNode );
  662. const computeBindings = bindings.getForCompute( computeNode );
  663. const computePipeline = pipelines.getForCompute( computeNode, computeBindings );
  664. backend.compute( computeNodes, computeNode, computeBindings, computePipeline );
  665. }
  666. backend.finishCompute( computeNodes );
  667. await this.backend.resolveTimestampAsync( computeNodes, 'compute' );
  668. //
  669. nodeFrame.renderId = previousRenderId;
  670. }
  671. async hasFeatureAsync( name ) {
  672. if ( this._initialized === false ) await this.init();
  673. return this.backend.hasFeature( name );
  674. }
  675. hasFeature( name ) {
  676. if ( this._initialized === false ) {
  677. console.warn( 'THREE.Renderer: .hasFeature() called before the backend is initialized. Try using .hasFeatureAsync() instead.' );
  678. return false;
  679. }
  680. return this.backend.hasFeature( name );
  681. }
  682. copyFramebufferToTexture( framebufferTexture ) {
  683. const renderContext = this._currentRenderContext;
  684. this._textures.updateTexture( framebufferTexture );
  685. this.backend.copyFramebufferToTexture( framebufferTexture, renderContext );
  686. }
  687. copyTextureToTexture( srcTexture, dstTexture, srcRegion = null, dstPosition = null, level = 0 ) {
  688. this._textures.updateTexture( srcTexture );
  689. this._textures.updateTexture( dstTexture );
  690. this.backend.copyTextureToTexture( srcTexture, dstTexture, srcRegion, dstPosition, level );
  691. }
  692. readRenderTargetPixelsAsync( renderTarget, x, y, width, height, index = 0 ) {
  693. return this.backend.copyTextureToBuffer( renderTarget.textures[ index ], x, y, width, height );
  694. }
  695. _projectObject( object, camera, groupOrder, renderList ) {
  696. if ( object.visible === false ) return;
  697. const visible = object.layers.test( camera.layers );
  698. if ( visible ) {
  699. if ( object.isGroup ) {
  700. groupOrder = object.renderOrder;
  701. } else if ( object.isLOD ) {
  702. if ( object.autoUpdate === true ) object.update( camera );
  703. } else if ( object.isLight ) {
  704. renderList.pushLight( object );
  705. } else if ( object.isSprite ) {
  706. if ( ! object.frustumCulled || _frustum.intersectsSprite( object ) ) {
  707. if ( this.sortObjects === true ) {
  708. _vector3.setFromMatrixPosition( object.matrixWorld ).applyMatrix4( _projScreenMatrix );
  709. }
  710. const geometry = object.geometry;
  711. const material = object.material;
  712. if ( material.visible ) {
  713. renderList.push( object, geometry, material, groupOrder, _vector3.z, null );
  714. }
  715. }
  716. } else if ( object.isLineLoop ) {
  717. console.error( 'THREE.Renderer: Objects of type THREE.LineLoop are not supported. Please use THREE.Line or THREE.LineSegments.' );
  718. } else if ( object.isMesh || object.isLine || object.isPoints ) {
  719. if ( ! object.frustumCulled || _frustum.intersectsObject( object ) ) {
  720. const geometry = object.geometry;
  721. const material = object.material;
  722. if ( this.sortObjects === true ) {
  723. if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
  724. _vector3
  725. .copy( geometry.boundingSphere.center )
  726. .applyMatrix4( object.matrixWorld )
  727. .applyMatrix4( _projScreenMatrix );
  728. }
  729. if ( Array.isArray( material ) ) {
  730. const groups = geometry.groups;
  731. for ( let i = 0, l = groups.length; i < l; i ++ ) {
  732. const group = groups[ i ];
  733. const groupMaterial = material[ group.materialIndex ];
  734. if ( groupMaterial && groupMaterial.visible ) {
  735. renderList.push( object, geometry, groupMaterial, groupOrder, _vector3.z, group );
  736. }
  737. }
  738. } else if ( material.visible ) {
  739. renderList.push( object, geometry, material, groupOrder, _vector3.z, null );
  740. }
  741. }
  742. }
  743. }
  744. if ( object.static === true ) {
  745. const baseRenderList = renderList;
  746. // replace render list
  747. renderList = this._renderLists.get( object, camera );
  748. renderList.begin();
  749. baseRenderList.pushBundle( {
  750. object,
  751. camera,
  752. renderList,
  753. } );
  754. renderList.finish();
  755. }
  756. const children = object.children;
  757. for ( let i = 0, l = children.length; i < l; i ++ ) {
  758. this._projectObject( children[ i ], camera, groupOrder, renderList );
  759. }
  760. }
  761. _renderBundles( bundles, sceneRef, lightsNode ) {
  762. for ( const bundle of bundles ) {
  763. this._renderBundle( bundle, sceneRef, lightsNode );
  764. }
  765. }
  766. _renderObjects( renderList, camera, scene, lightsNode ) {
  767. // process renderable objects
  768. for ( let i = 0, il = renderList.length; i < il; i ++ ) {
  769. const renderItem = renderList[ i ];
  770. // @TODO: Add support for multiple materials per object. This will require to extract
  771. // the material from the renderItem object and pass it with its group data to renderObject().
  772. const { object, geometry, material, group } = renderItem;
  773. if ( camera.isArrayCamera ) {
  774. const cameras = camera.cameras;
  775. for ( let j = 0, jl = cameras.length; j < jl; j ++ ) {
  776. const camera2 = cameras[ j ];
  777. if ( object.layers.test( camera2.layers ) ) {
  778. const vp = camera2.viewport;
  779. const minDepth = ( vp.minDepth === undefined ) ? 0 : vp.minDepth;
  780. const maxDepth = ( vp.maxDepth === undefined ) ? 1 : vp.maxDepth;
  781. const viewportValue = this._currentRenderContext.viewportValue;
  782. viewportValue.copy( vp ).multiplyScalar( this._pixelRatio ).floor();
  783. viewportValue.minDepth = minDepth;
  784. viewportValue.maxDepth = maxDepth;
  785. this.backend.updateViewport( this._currentRenderContext );
  786. this._currentRenderObjectFunction( object, scene, camera2, geometry, material, group, lightsNode );
  787. }
  788. }
  789. } else {
  790. this._currentRenderObjectFunction( object, scene, camera, geometry, material, group, lightsNode );
  791. }
  792. }
  793. }
  794. renderObject( object, scene, camera, geometry, material, group, lightsNode ) {
  795. let overridePositionNode;
  796. let overrideFragmentNode;
  797. let overrideDepthNode;
  798. //
  799. object.onBeforeRender( this, scene, camera, geometry, material, group );
  800. //
  801. if ( scene.overrideMaterial !== null ) {
  802. const overrideMaterial = scene.overrideMaterial;
  803. if ( material.positionNode && material.positionNode.isNode ) {
  804. overridePositionNode = overrideMaterial.positionNode;
  805. overrideMaterial.positionNode = material.positionNode;
  806. }
  807. if ( overrideMaterial.isShadowNodeMaterial ) {
  808. overrideMaterial.side = material.shadowSide === null ? material.side : material.shadowSide;
  809. if ( material.depthNode && material.depthNode.isNode ) {
  810. overrideDepthNode = overrideMaterial.depthNode;
  811. overrideMaterial.depthNode = material.depthNode;
  812. }
  813. if ( material.shadowNode && material.shadowNode.isNode ) {
  814. overrideFragmentNode = overrideMaterial.fragmentNode;
  815. overrideMaterial.fragmentNode = material.shadowNode;
  816. }
  817. if ( this.localClippingEnabled ) {
  818. if ( material.clipShadows ) {
  819. if ( overrideMaterial.clippingPlanes !== material.clippingPlanes ) {
  820. overrideMaterial.clippingPlanes = material.clippingPlanes;
  821. overrideMaterial.needsUpdate = true;
  822. }
  823. if ( overrideMaterial.clipIntersection !== material.clipIntersection ) {
  824. overrideMaterial.clipIntersection = material.clipIntersection;
  825. }
  826. } else if ( Array.isArray( overrideMaterial.clippingPlanes ) ) {
  827. overrideMaterial.clippingPlanes = null;
  828. overrideMaterial.needsUpdate = true;
  829. }
  830. }
  831. }
  832. material = overrideMaterial;
  833. }
  834. //
  835. if ( material.transparent === true && material.side === DoubleSide && material.forceSinglePass === false ) {
  836. material.side = BackSide;
  837. this._handleObjectFunction( object, material, scene, camera, lightsNode, group, 'backSide' ); // create backSide pass id
  838. material.side = FrontSide;
  839. this._handleObjectFunction( object, material, scene, camera, lightsNode, group ); // use default pass id
  840. material.side = DoubleSide;
  841. } else {
  842. this._handleObjectFunction( object, material, scene, camera, lightsNode, group );
  843. }
  844. //
  845. if ( overridePositionNode !== undefined ) {
  846. scene.overrideMaterial.positionNode = overridePositionNode;
  847. }
  848. if ( overrideDepthNode !== undefined ) {
  849. scene.overrideMaterial.depthNode = overrideDepthNode;
  850. }
  851. if ( overrideFragmentNode !== undefined ) {
  852. scene.overrideMaterial.fragmentNode = overrideFragmentNode;
  853. }
  854. //
  855. object.onAfterRender( this, scene, camera, geometry, material, group );
  856. }
  857. _renderObjectDirect( object, material, scene, camera, lightsNode, group, passId ) {
  858. const renderObject = this._objects.get( object, material, scene, camera, lightsNode, this._currentRenderContext, passId );
  859. renderObject.drawRange = group || object.geometry.drawRange;
  860. //
  861. this._nodes.updateBefore( renderObject );
  862. //
  863. object.modelViewMatrix.multiplyMatrices( camera.matrixWorldInverse, object.matrixWorld );
  864. object.normalMatrix.getNormalMatrix( object.modelViewMatrix );
  865. //
  866. this._nodes.updateForRender( renderObject );
  867. this._geometries.updateForRender( renderObject );
  868. this._bindings.updateForRender( renderObject );
  869. this._pipelines.updateForRender( renderObject );
  870. //
  871. if ( this._currentRenderBundle !== null && this._currentRenderBundle.needsUpdate === true ) {
  872. const renderObjectData = this.backend.get( renderObject );
  873. renderObjectData.bundleEncoder = undefined;
  874. renderObjectData.lastPipelineGPU = undefined;
  875. }
  876. this.backend.draw( renderObject, this.info );
  877. if ( this._currentRenderBundle !== null ) {
  878. const renderContextData = this.backend.get( this._currentRenderContext );
  879. renderContextData.renderObjects.push( renderObject );
  880. }
  881. this._nodes.updateAfter( renderObject );
  882. }
  883. _createObjectPipeline( object, material, scene, camera, lightsNode, passId ) {
  884. const renderObject = this._objects.get( object, material, scene, camera, lightsNode, this._currentRenderContext, passId );
  885. //
  886. this._nodes.updateBefore( renderObject );
  887. //
  888. this._nodes.updateForRender( renderObject );
  889. this._geometries.updateForRender( renderObject );
  890. this._bindings.updateForRender( renderObject );
  891. this._pipelines.getForRender( renderObject, this._compilationPromises );
  892. this._nodes.updateAfter( renderObject );
  893. }
  894. get compute() {
  895. return this.computeAsync;
  896. }
  897. get compile() {
  898. return this.compileAsync;
  899. }
  900. }
  901. export default Renderer;