Renderer.js 34 KB

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