2
0

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