Renderer.js 31 KB

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