Renderer.js 35 KB

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