Renderer.js 34 KB

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