Renderer.js 35 KB

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