WebGLTextures.js 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486
  1. import { LinearFilter, LinearMipmapLinearFilter, LinearMipmapNearestFilter, NearestFilter, NearestMipmapLinearFilter, NearestMipmapNearestFilter, RGBFormat, RGBAFormat, DepthFormat, DepthStencilFormat, UnsignedShortType, UnsignedIntType, UnsignedInt248Type, FloatType, HalfFloatType, MirroredRepeatWrapping, ClampToEdgeWrapping, RepeatWrapping } from '../../constants.js';
  2. import * as MathUtils from '../../math/MathUtils.js';
  3. import { createElementNS } from '../../utils.js';
  4. function WebGLTextures( _gl, extensions, state, properties, capabilities, utils, info ) {
  5. const isWebGL2 = capabilities.isWebGL2;
  6. const maxTextures = capabilities.maxTextures;
  7. const maxCubemapSize = capabilities.maxCubemapSize;
  8. const maxTextureSize = capabilities.maxTextureSize;
  9. const maxSamples = capabilities.maxSamples;
  10. const msaartcSupported = extensions.has( 'EXT_multisampled_render_to_texture' );
  11. const msaaExt = msaartcSupported ? extensions.get( 'EXT_multisampled_render_to_texture' ) : undefined;
  12. const _videoTextures = new WeakMap();
  13. let _canvas;
  14. // cordova iOS (as of 5.0) still uses UIWebView, which provides OffscreenCanvas,
  15. // also OffscreenCanvas.getContext("webgl"), but not OffscreenCanvas.getContext("2d")!
  16. // Some implementations may only implement OffscreenCanvas partially (e.g. lacking 2d).
  17. let useOffscreenCanvas = false;
  18. try {
  19. useOffscreenCanvas = typeof OffscreenCanvas !== 'undefined'
  20. && ( new OffscreenCanvas( 1, 1 ).getContext( '2d' ) ) !== null;
  21. } catch ( err ) {
  22. // Ignore any errors
  23. }
  24. function createCanvas( width, height ) {
  25. // Use OffscreenCanvas when available. Specially needed in web workers
  26. return useOffscreenCanvas ?
  27. new OffscreenCanvas( width, height ) : createElementNS( 'canvas' );
  28. }
  29. function resizeImage( image, needsPowerOfTwo, needsNewCanvas, maxSize ) {
  30. let scale = 1;
  31. // handle case if texture exceeds max size
  32. if ( image.width > maxSize || image.height > maxSize ) {
  33. scale = maxSize / Math.max( image.width, image.height );
  34. }
  35. // only perform resize if necessary
  36. if ( scale < 1 || needsPowerOfTwo === true ) {
  37. // only perform resize for certain image types
  38. if ( ( typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement ) ||
  39. ( typeof HTMLCanvasElement !== 'undefined' && image instanceof HTMLCanvasElement ) ||
  40. ( typeof ImageBitmap !== 'undefined' && image instanceof ImageBitmap ) ) {
  41. const floor = needsPowerOfTwo ? MathUtils.floorPowerOfTwo : Math.floor;
  42. const width = floor( scale * image.width );
  43. const height = floor( scale * image.height );
  44. if ( _canvas === undefined ) _canvas = createCanvas( width, height );
  45. // cube textures can't reuse the same canvas
  46. const canvas = needsNewCanvas ? createCanvas( width, height ) : _canvas;
  47. canvas.width = width;
  48. canvas.height = height;
  49. const context = canvas.getContext( '2d' );
  50. context.drawImage( image, 0, 0, width, height );
  51. console.warn( 'THREE.WebGLRenderer: Texture has been resized from (' + image.width + 'x' + image.height + ') to (' + width + 'x' + height + ').' );
  52. return canvas;
  53. } else {
  54. if ( 'data' in image ) {
  55. console.warn( 'THREE.WebGLRenderer: Image in DataTexture is too big (' + image.width + 'x' + image.height + ').' );
  56. }
  57. return image;
  58. }
  59. }
  60. return image;
  61. }
  62. function isPowerOfTwo( image ) {
  63. return MathUtils.isPowerOfTwo( image.width ) && MathUtils.isPowerOfTwo( image.height );
  64. }
  65. function textureNeedsPowerOfTwo( texture ) {
  66. if ( isWebGL2 ) return false;
  67. return ( texture.wrapS !== ClampToEdgeWrapping || texture.wrapT !== ClampToEdgeWrapping ) ||
  68. ( texture.minFilter !== NearestFilter && texture.minFilter !== LinearFilter );
  69. }
  70. function textureNeedsGenerateMipmaps( texture, supportsMips ) {
  71. return texture.generateMipmaps && supportsMips &&
  72. texture.minFilter !== NearestFilter && texture.minFilter !== LinearFilter;
  73. }
  74. function generateMipmap( target, texture, width, height, depth = 1 ) {
  75. _gl.generateMipmap( target );
  76. const textureProperties = properties.get( texture );
  77. textureProperties.__maxMipLevel = Math.log2( Math.max( width, height, depth ) );
  78. }
  79. function getInternalFormat( internalFormatName, glFormat, glType ) {
  80. if ( isWebGL2 === false ) return glFormat;
  81. if ( internalFormatName !== null ) {
  82. if ( _gl[ internalFormatName ] !== undefined ) return _gl[ internalFormatName ];
  83. console.warn( 'THREE.WebGLRenderer: Attempt to use non-existing WebGL internal format \'' + internalFormatName + '\'' );
  84. }
  85. let internalFormat = glFormat;
  86. if ( glFormat === _gl.RED ) {
  87. if ( glType === _gl.FLOAT ) internalFormat = _gl.R32F;
  88. if ( glType === _gl.HALF_FLOAT ) internalFormat = _gl.R16F;
  89. if ( glType === _gl.UNSIGNED_BYTE ) internalFormat = _gl.R8;
  90. }
  91. if ( glFormat === _gl.RGB ) {
  92. if ( glType === _gl.FLOAT ) internalFormat = _gl.RGB32F;
  93. if ( glType === _gl.HALF_FLOAT ) internalFormat = _gl.RGB16F;
  94. if ( glType === _gl.UNSIGNED_BYTE ) internalFormat = _gl.RGB8;
  95. }
  96. if ( glFormat === _gl.RGBA ) {
  97. if ( glType === _gl.FLOAT ) internalFormat = _gl.RGBA32F;
  98. if ( glType === _gl.HALF_FLOAT ) internalFormat = _gl.RGBA16F;
  99. if ( glType === _gl.UNSIGNED_BYTE ) internalFormat = _gl.RGBA8;
  100. }
  101. if ( internalFormat === _gl.R16F || internalFormat === _gl.R32F ||
  102. internalFormat === _gl.RGBA16F || internalFormat === _gl.RGBA32F ) {
  103. extensions.get( 'EXT_color_buffer_float' );
  104. }
  105. return internalFormat;
  106. }
  107. // Fallback filters for non-power-of-2 textures
  108. function filterFallback( f ) {
  109. if ( f === NearestFilter || f === NearestMipmapNearestFilter || f === NearestMipmapLinearFilter ) {
  110. return _gl.NEAREST;
  111. }
  112. return _gl.LINEAR;
  113. }
  114. //
  115. function onTextureDispose( event ) {
  116. const texture = event.target;
  117. texture.removeEventListener( 'dispose', onTextureDispose );
  118. deallocateTexture( texture );
  119. if ( texture.isVideoTexture ) {
  120. _videoTextures.delete( texture );
  121. }
  122. info.memory.textures --;
  123. }
  124. function onRenderTargetDispose( event ) {
  125. const renderTarget = event.target;
  126. renderTarget.removeEventListener( 'dispose', onRenderTargetDispose );
  127. deallocateRenderTarget( renderTarget );
  128. }
  129. //
  130. function deallocateTexture( texture ) {
  131. const textureProperties = properties.get( texture );
  132. if ( textureProperties.__webglInit === undefined ) return;
  133. _gl.deleteTexture( textureProperties.__webglTexture );
  134. properties.remove( texture );
  135. }
  136. function deallocateRenderTarget( renderTarget ) {
  137. const texture = renderTarget.texture;
  138. const renderTargetProperties = properties.get( renderTarget );
  139. const textureProperties = properties.get( texture );
  140. if ( ! renderTarget ) return;
  141. if ( textureProperties.__webglTexture !== undefined ) {
  142. _gl.deleteTexture( textureProperties.__webglTexture );
  143. info.memory.textures --;
  144. }
  145. if ( renderTarget.depthTexture ) {
  146. renderTarget.depthTexture.dispose();
  147. }
  148. if ( renderTarget.isWebGLCubeRenderTarget ) {
  149. for ( let i = 0; i < 6; i ++ ) {
  150. _gl.deleteFramebuffer( renderTargetProperties.__webglFramebuffer[ i ] );
  151. if ( renderTargetProperties.__webglDepthbuffer ) _gl.deleteRenderbuffer( renderTargetProperties.__webglDepthbuffer[ i ] );
  152. }
  153. } else {
  154. _gl.deleteFramebuffer( renderTargetProperties.__webglFramebuffer );
  155. if ( renderTargetProperties.__webglDepthbuffer ) _gl.deleteRenderbuffer( renderTargetProperties.__webglDepthbuffer );
  156. if ( renderTargetProperties.__webglMultisampledFramebuffer ) _gl.deleteFramebuffer( renderTargetProperties.__webglMultisampledFramebuffer );
  157. if ( renderTargetProperties.__webglColorRenderbuffer ) _gl.deleteRenderbuffer( renderTargetProperties.__webglColorRenderbuffer );
  158. if ( renderTargetProperties.__webglDepthRenderbuffer ) _gl.deleteRenderbuffer( renderTargetProperties.__webglDepthRenderbuffer );
  159. }
  160. if ( renderTarget.isWebGLMultipleRenderTargets ) {
  161. for ( let i = 0, il = texture.length; i < il; i ++ ) {
  162. const attachmentProperties = properties.get( texture[ i ] );
  163. if ( attachmentProperties.__webglTexture ) {
  164. _gl.deleteTexture( attachmentProperties.__webglTexture );
  165. info.memory.textures --;
  166. }
  167. properties.remove( texture[ i ] );
  168. }
  169. }
  170. properties.remove( texture );
  171. properties.remove( renderTarget );
  172. }
  173. //
  174. let textureUnits = 0;
  175. function resetTextureUnits() {
  176. textureUnits = 0;
  177. }
  178. function allocateTextureUnit() {
  179. const textureUnit = textureUnits;
  180. if ( textureUnit >= maxTextures ) {
  181. console.warn( 'THREE.WebGLTextures: Trying to use ' + textureUnit + ' texture units while this GPU supports only ' + maxTextures );
  182. }
  183. textureUnits += 1;
  184. return textureUnit;
  185. }
  186. //
  187. function setTexture2D( texture, slot ) {
  188. const textureProperties = properties.get( texture );
  189. if ( texture.isVideoTexture ) updateVideoTexture( texture );
  190. if ( texture.version > 0 && textureProperties.__version !== texture.version ) {
  191. const image = texture.image;
  192. if ( image === undefined ) {
  193. console.warn( 'THREE.WebGLRenderer: Texture marked for update but image is undefined' );
  194. } else if ( image.complete === false ) {
  195. console.warn( 'THREE.WebGLRenderer: Texture marked for update but image is incomplete' );
  196. } else {
  197. uploadTexture( textureProperties, texture, slot );
  198. return;
  199. }
  200. }
  201. state.activeTexture( _gl.TEXTURE0 + slot );
  202. state.bindTexture( _gl.TEXTURE_2D, textureProperties.__webglTexture );
  203. }
  204. function setTexture2DArray( texture, slot ) {
  205. const textureProperties = properties.get( texture );
  206. if ( texture.version > 0 && textureProperties.__version !== texture.version ) {
  207. uploadTexture( textureProperties, texture, slot );
  208. return;
  209. }
  210. state.activeTexture( _gl.TEXTURE0 + slot );
  211. state.bindTexture( _gl.TEXTURE_2D_ARRAY, textureProperties.__webglTexture );
  212. }
  213. function setTexture3D( texture, slot ) {
  214. const textureProperties = properties.get( texture );
  215. if ( texture.version > 0 && textureProperties.__version !== texture.version ) {
  216. uploadTexture( textureProperties, texture, slot );
  217. return;
  218. }
  219. state.activeTexture( _gl.TEXTURE0 + slot );
  220. state.bindTexture( _gl.TEXTURE_3D, textureProperties.__webglTexture );
  221. }
  222. function setTextureCube( texture, slot ) {
  223. const textureProperties = properties.get( texture );
  224. if ( texture.version > 0 && textureProperties.__version !== texture.version ) {
  225. uploadCubeTexture( textureProperties, texture, slot );
  226. return;
  227. }
  228. state.activeTexture( _gl.TEXTURE0 + slot );
  229. state.bindTexture( _gl.TEXTURE_CUBE_MAP, textureProperties.__webglTexture );
  230. }
  231. const wrappingToGL = {
  232. [ RepeatWrapping ]: _gl.REPEAT,
  233. [ ClampToEdgeWrapping ]: _gl.CLAMP_TO_EDGE,
  234. [ MirroredRepeatWrapping ]: _gl.MIRRORED_REPEAT
  235. };
  236. const filterToGL = {
  237. [ NearestFilter ]: _gl.NEAREST,
  238. [ NearestMipmapNearestFilter ]: _gl.NEAREST_MIPMAP_NEAREST,
  239. [ NearestMipmapLinearFilter ]: _gl.NEAREST_MIPMAP_LINEAR,
  240. [ LinearFilter ]: _gl.LINEAR,
  241. [ LinearMipmapNearestFilter ]: _gl.LINEAR_MIPMAP_NEAREST,
  242. [ LinearMipmapLinearFilter ]: _gl.LINEAR_MIPMAP_LINEAR
  243. };
  244. function setTextureParameters( textureType, texture, supportsMips ) {
  245. if ( supportsMips ) {
  246. _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_S, wrappingToGL[ texture.wrapS ] );
  247. _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_T, wrappingToGL[ texture.wrapT ] );
  248. if ( textureType === _gl.TEXTURE_3D || textureType === _gl.TEXTURE_2D_ARRAY ) {
  249. _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_R, wrappingToGL[ texture.wrapR ] );
  250. }
  251. _gl.texParameteri( textureType, _gl.TEXTURE_MAG_FILTER, filterToGL[ texture.magFilter ] );
  252. _gl.texParameteri( textureType, _gl.TEXTURE_MIN_FILTER, filterToGL[ texture.minFilter ] );
  253. } else {
  254. _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_S, _gl.CLAMP_TO_EDGE );
  255. _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_T, _gl.CLAMP_TO_EDGE );
  256. if ( textureType === _gl.TEXTURE_3D || textureType === _gl.TEXTURE_2D_ARRAY ) {
  257. _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_R, _gl.CLAMP_TO_EDGE );
  258. }
  259. if ( texture.wrapS !== ClampToEdgeWrapping || texture.wrapT !== ClampToEdgeWrapping ) {
  260. console.warn( 'THREE.WebGLRenderer: Texture is not power of two. Texture.wrapS and Texture.wrapT should be set to THREE.ClampToEdgeWrapping.' );
  261. }
  262. _gl.texParameteri( textureType, _gl.TEXTURE_MAG_FILTER, filterFallback( texture.magFilter ) );
  263. _gl.texParameteri( textureType, _gl.TEXTURE_MIN_FILTER, filterFallback( texture.minFilter ) );
  264. if ( texture.minFilter !== NearestFilter && texture.minFilter !== LinearFilter ) {
  265. console.warn( 'THREE.WebGLRenderer: Texture is not power of two. Texture.minFilter should be set to THREE.NearestFilter or THREE.LinearFilter.' );
  266. }
  267. }
  268. if ( extensions.has( 'EXT_texture_filter_anisotropic' ) === true ) {
  269. const extension = extensions.get( 'EXT_texture_filter_anisotropic' );
  270. if ( texture.type === FloatType && extensions.has( 'OES_texture_float_linear' ) === false ) return; // verify extension for WebGL 1 and WebGL 2
  271. if ( isWebGL2 === false && ( texture.type === HalfFloatType && extensions.has( 'OES_texture_half_float_linear' ) === false ) ) return; // verify extension for WebGL 1 only
  272. if ( texture.anisotropy > 1 || properties.get( texture ).__currentAnisotropy ) {
  273. _gl.texParameterf( textureType, extension.TEXTURE_MAX_ANISOTROPY_EXT, Math.min( texture.anisotropy, capabilities.getMaxAnisotropy() ) );
  274. properties.get( texture ).__currentAnisotropy = texture.anisotropy;
  275. }
  276. }
  277. }
  278. function initTexture( textureProperties, texture ) {
  279. if ( textureProperties.__webglInit === undefined ) {
  280. textureProperties.__webglInit = true;
  281. texture.addEventListener( 'dispose', onTextureDispose );
  282. textureProperties.__webglTexture = _gl.createTexture();
  283. info.memory.textures ++;
  284. }
  285. }
  286. function uploadTexture( textureProperties, texture, slot ) {
  287. let textureType = _gl.TEXTURE_2D;
  288. if ( texture.isDataTexture2DArray ) textureType = _gl.TEXTURE_2D_ARRAY;
  289. if ( texture.isDataTexture3D ) textureType = _gl.TEXTURE_3D;
  290. initTexture( textureProperties, texture );
  291. state.activeTexture( _gl.TEXTURE0 + slot );
  292. state.bindTexture( textureType, textureProperties.__webglTexture );
  293. _gl.pixelStorei( _gl.UNPACK_FLIP_Y_WEBGL, texture.flipY );
  294. _gl.pixelStorei( _gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, texture.premultiplyAlpha );
  295. _gl.pixelStorei( _gl.UNPACK_ALIGNMENT, texture.unpackAlignment );
  296. _gl.pixelStorei( _gl.UNPACK_COLORSPACE_CONVERSION_WEBGL, _gl.NONE );
  297. const needsPowerOfTwo = textureNeedsPowerOfTwo( texture ) && isPowerOfTwo( texture.image ) === false;
  298. const image = resizeImage( texture.image, needsPowerOfTwo, false, maxTextureSize );
  299. const supportsMips = isPowerOfTwo( image ) || isWebGL2,
  300. glFormat = utils.convert( texture.format );
  301. let glType = utils.convert( texture.type ),
  302. glInternalFormat = getInternalFormat( texture.internalFormat, glFormat, glType );
  303. setTextureParameters( textureType, texture, supportsMips );
  304. let mipmap;
  305. const mipmaps = texture.mipmaps;
  306. if ( texture.isDepthTexture ) {
  307. // populate depth texture with dummy data
  308. glInternalFormat = _gl.DEPTH_COMPONENT;
  309. if ( isWebGL2 ) {
  310. if ( texture.type === FloatType ) {
  311. glInternalFormat = _gl.DEPTH_COMPONENT32F;
  312. } else if ( texture.type === UnsignedIntType ) {
  313. glInternalFormat = _gl.DEPTH_COMPONENT24;
  314. } else if ( texture.type === UnsignedInt248Type ) {
  315. glInternalFormat = _gl.DEPTH24_STENCIL8;
  316. } else {
  317. glInternalFormat = _gl.DEPTH_COMPONENT16; // WebGL2 requires sized internalformat for glTexImage2D
  318. }
  319. } else {
  320. if ( texture.type === FloatType ) {
  321. console.error( 'WebGLRenderer: Floating point depth texture requires WebGL2.' );
  322. }
  323. }
  324. // validation checks for WebGL 1
  325. if ( texture.format === DepthFormat && glInternalFormat === _gl.DEPTH_COMPONENT ) {
  326. // The error INVALID_OPERATION is generated by texImage2D if format and internalformat are
  327. // DEPTH_COMPONENT and type is not UNSIGNED_SHORT or UNSIGNED_INT
  328. // (https://www.khronos.org/registry/webgl/extensions/WEBGL_depth_texture/)
  329. if ( texture.type !== UnsignedShortType && texture.type !== UnsignedIntType ) {
  330. console.warn( 'THREE.WebGLRenderer: Use UnsignedShortType or UnsignedIntType for DepthFormat DepthTexture.' );
  331. texture.type = UnsignedShortType;
  332. glType = utils.convert( texture.type );
  333. }
  334. }
  335. if ( texture.format === DepthStencilFormat && glInternalFormat === _gl.DEPTH_COMPONENT ) {
  336. // Depth stencil textures need the DEPTH_STENCIL internal format
  337. // (https://www.khronos.org/registry/webgl/extensions/WEBGL_depth_texture/)
  338. glInternalFormat = _gl.DEPTH_STENCIL;
  339. // The error INVALID_OPERATION is generated by texImage2D if format and internalformat are
  340. // DEPTH_STENCIL and type is not UNSIGNED_INT_24_8_WEBGL.
  341. // (https://www.khronos.org/registry/webgl/extensions/WEBGL_depth_texture/)
  342. if ( texture.type !== UnsignedInt248Type ) {
  343. console.warn( 'THREE.WebGLRenderer: Use UnsignedInt248Type for DepthStencilFormat DepthTexture.' );
  344. texture.type = UnsignedInt248Type;
  345. glType = utils.convert( texture.type );
  346. }
  347. }
  348. //
  349. state.texImage2D( _gl.TEXTURE_2D, 0, glInternalFormat, image.width, image.height, 0, glFormat, glType, null );
  350. } else if ( texture.isDataTexture ) {
  351. // use manually created mipmaps if available
  352. // if there are no manual mipmaps
  353. // set 0 level mipmap and then use GL to generate other mipmap levels
  354. if ( mipmaps.length > 0 && supportsMips ) {
  355. for ( let i = 0, il = mipmaps.length; i < il; i ++ ) {
  356. mipmap = mipmaps[ i ];
  357. state.texImage2D( _gl.TEXTURE_2D, i, glInternalFormat, mipmap.width, mipmap.height, 0, glFormat, glType, mipmap.data );
  358. }
  359. texture.generateMipmaps = false;
  360. textureProperties.__maxMipLevel = mipmaps.length - 1;
  361. } else {
  362. state.texImage2D( _gl.TEXTURE_2D, 0, glInternalFormat, image.width, image.height, 0, glFormat, glType, image.data );
  363. textureProperties.__maxMipLevel = 0;
  364. }
  365. } else if ( texture.isCompressedTexture ) {
  366. for ( let i = 0, il = mipmaps.length; i < il; i ++ ) {
  367. mipmap = mipmaps[ i ];
  368. if ( texture.format !== RGBAFormat && texture.format !== RGBFormat ) {
  369. if ( glFormat !== null ) {
  370. state.compressedTexImage2D( _gl.TEXTURE_2D, i, glInternalFormat, mipmap.width, mipmap.height, 0, mipmap.data );
  371. } else {
  372. console.warn( 'THREE.WebGLRenderer: Attempt to load unsupported compressed texture format in .uploadTexture()' );
  373. }
  374. } else {
  375. state.texImage2D( _gl.TEXTURE_2D, i, glInternalFormat, mipmap.width, mipmap.height, 0, glFormat, glType, mipmap.data );
  376. }
  377. }
  378. textureProperties.__maxMipLevel = mipmaps.length - 1;
  379. } else if ( texture.isDataTexture2DArray ) {
  380. state.texImage3D( _gl.TEXTURE_2D_ARRAY, 0, glInternalFormat, image.width, image.height, image.depth, 0, glFormat, glType, image.data );
  381. textureProperties.__maxMipLevel = 0;
  382. } else if ( texture.isDataTexture3D ) {
  383. state.texImage3D( _gl.TEXTURE_3D, 0, glInternalFormat, image.width, image.height, image.depth, 0, glFormat, glType, image.data );
  384. textureProperties.__maxMipLevel = 0;
  385. } else {
  386. // regular Texture (image, video, canvas)
  387. // use manually created mipmaps if available
  388. // if there are no manual mipmaps
  389. // set 0 level mipmap and then use GL to generate other mipmap levels
  390. if ( mipmaps.length > 0 && supportsMips ) {
  391. for ( let i = 0, il = mipmaps.length; i < il; i ++ ) {
  392. mipmap = mipmaps[ i ];
  393. state.texImage2D( _gl.TEXTURE_2D, i, glInternalFormat, glFormat, glType, mipmap );
  394. }
  395. texture.generateMipmaps = false;
  396. textureProperties.__maxMipLevel = mipmaps.length - 1;
  397. } else {
  398. state.texImage2D( _gl.TEXTURE_2D, 0, glInternalFormat, glFormat, glType, image );
  399. textureProperties.__maxMipLevel = 0;
  400. }
  401. }
  402. if ( textureNeedsGenerateMipmaps( texture, supportsMips ) ) {
  403. generateMipmap( textureType, texture, image.width, image.height );
  404. }
  405. textureProperties.__version = texture.version;
  406. if ( texture.onUpdate ) texture.onUpdate( texture );
  407. }
  408. function uploadCubeTexture( textureProperties, texture, slot ) {
  409. if ( texture.image.length !== 6 ) return;
  410. initTexture( textureProperties, texture );
  411. state.activeTexture( _gl.TEXTURE0 + slot );
  412. state.bindTexture( _gl.TEXTURE_CUBE_MAP, textureProperties.__webglTexture );
  413. _gl.pixelStorei( _gl.UNPACK_FLIP_Y_WEBGL, texture.flipY );
  414. _gl.pixelStorei( _gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, texture.premultiplyAlpha );
  415. _gl.pixelStorei( _gl.UNPACK_ALIGNMENT, texture.unpackAlignment );
  416. _gl.pixelStorei( _gl.UNPACK_COLORSPACE_CONVERSION_WEBGL, _gl.NONE );
  417. const isCompressed = ( texture && ( texture.isCompressedTexture || texture.image[ 0 ].isCompressedTexture ) );
  418. const isDataTexture = ( texture.image[ 0 ] && texture.image[ 0 ].isDataTexture );
  419. const cubeImage = [];
  420. for ( let i = 0; i < 6; i ++ ) {
  421. if ( ! isCompressed && ! isDataTexture ) {
  422. cubeImage[ i ] = resizeImage( texture.image[ i ], false, true, maxCubemapSize );
  423. } else {
  424. cubeImage[ i ] = isDataTexture ? texture.image[ i ].image : texture.image[ i ];
  425. }
  426. }
  427. const image = cubeImage[ 0 ],
  428. supportsMips = isPowerOfTwo( image ) || isWebGL2,
  429. glFormat = utils.convert( texture.format ),
  430. glType = utils.convert( texture.type ),
  431. glInternalFormat = getInternalFormat( texture.internalFormat, glFormat, glType );
  432. setTextureParameters( _gl.TEXTURE_CUBE_MAP, texture, supportsMips );
  433. let mipmaps;
  434. if ( isCompressed ) {
  435. for ( let i = 0; i < 6; i ++ ) {
  436. mipmaps = cubeImage[ i ].mipmaps;
  437. for ( let j = 0; j < mipmaps.length; j ++ ) {
  438. const mipmap = mipmaps[ j ];
  439. if ( texture.format !== RGBAFormat && texture.format !== RGBFormat ) {
  440. if ( glFormat !== null ) {
  441. state.compressedTexImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, j, glInternalFormat, mipmap.width, mipmap.height, 0, mipmap.data );
  442. } else {
  443. console.warn( 'THREE.WebGLRenderer: Attempt to load unsupported compressed texture format in .setTextureCube()' );
  444. }
  445. } else {
  446. state.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, j, glInternalFormat, mipmap.width, mipmap.height, 0, glFormat, glType, mipmap.data );
  447. }
  448. }
  449. }
  450. textureProperties.__maxMipLevel = mipmaps.length - 1;
  451. } else {
  452. mipmaps = texture.mipmaps;
  453. for ( let i = 0; i < 6; i ++ ) {
  454. if ( isDataTexture ) {
  455. state.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, glInternalFormat, cubeImage[ i ].width, cubeImage[ i ].height, 0, glFormat, glType, cubeImage[ i ].data );
  456. for ( let j = 0; j < mipmaps.length; j ++ ) {
  457. const mipmap = mipmaps[ j ];
  458. const mipmapImage = mipmap.image[ i ].image;
  459. state.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, j + 1, glInternalFormat, mipmapImage.width, mipmapImage.height, 0, glFormat, glType, mipmapImage.data );
  460. }
  461. } else {
  462. state.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, glInternalFormat, glFormat, glType, cubeImage[ i ] );
  463. for ( let j = 0; j < mipmaps.length; j ++ ) {
  464. const mipmap = mipmaps[ j ];
  465. state.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, j + 1, glInternalFormat, glFormat, glType, mipmap.image[ i ] );
  466. }
  467. }
  468. }
  469. textureProperties.__maxMipLevel = mipmaps.length;
  470. }
  471. if ( textureNeedsGenerateMipmaps( texture, supportsMips ) ) {
  472. // We assume images for cube map have the same size.
  473. generateMipmap( _gl.TEXTURE_CUBE_MAP, texture, image.width, image.height );
  474. }
  475. textureProperties.__version = texture.version;
  476. if ( texture.onUpdate ) texture.onUpdate( texture );
  477. }
  478. // Render targets
  479. // Setup storage for target texture and bind it to correct framebuffer
  480. function setupFrameBufferTexture( framebuffer, renderTarget, texture, attachment, textureTarget ) {
  481. const glFormat = utils.convert( texture.format );
  482. const glType = utils.convert( texture.type );
  483. const glInternalFormat = getInternalFormat( texture.internalFormat, glFormat, glType );
  484. if ( ! renderTarget.hasExternalTextures ) {
  485. if ( textureTarget === _gl.TEXTURE_3D || textureTarget === _gl.TEXTURE_2D_ARRAY ) {
  486. state.texImage3D( textureTarget, 0, glInternalFormat, renderTarget.width, renderTarget.height, renderTarget.depth, 0, glFormat, glType, null );
  487. } else {
  488. state.texImage2D( textureTarget, 0, glInternalFormat, renderTarget.width, renderTarget.height, 0, glFormat, glType, null );
  489. }
  490. }
  491. state.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
  492. if ( renderTarget.useMultisampleRenderToTexture ) {
  493. msaaExt.framebufferTexture2DMultisampleEXT( _gl.FRAMEBUFFER, attachment, textureTarget, properties.get( texture ).__webglTexture, 0, getRenderTargetSamples( renderTarget ) );
  494. } else {
  495. _gl.framebufferTexture2D( _gl.FRAMEBUFFER, attachment, textureTarget, properties.get( texture ).__webglTexture, 0 );
  496. }
  497. state.bindFramebuffer( _gl.FRAMEBUFFER, null );
  498. }
  499. // Setup storage for internal depth/stencil buffers and bind to correct framebuffer
  500. function setupRenderBufferStorage( renderbuffer, renderTarget, isMultisample ) {
  501. _gl.bindRenderbuffer( _gl.RENDERBUFFER, renderbuffer );
  502. if ( renderTarget.depthBuffer && ! renderTarget.stencilBuffer ) {
  503. let glInternalFormat = _gl.DEPTH_COMPONENT16;
  504. if ( isMultisample || renderTarget.useMultisampleRenderToTexture ) {
  505. const depthTexture = renderTarget.depthTexture;
  506. if ( depthTexture && depthTexture.isDepthTexture ) {
  507. if ( depthTexture.type === FloatType ) {
  508. glInternalFormat = _gl.DEPTH_COMPONENT32F;
  509. } else if ( depthTexture.type === UnsignedIntType ) {
  510. glInternalFormat = _gl.DEPTH_COMPONENT24;
  511. }
  512. }
  513. const samples = getRenderTargetSamples( renderTarget );
  514. if ( renderTarget.useMultisampleRenderToTexture ) {
  515. msaaExt.renderbufferStorageMultisampleEXT( _gl.RENDERBUFFER, samples, glInternalFormat, renderTarget.width, renderTarget.height );
  516. } else {
  517. _gl.renderbufferStorageMultisample( _gl.RENDERBUFFER, samples, glInternalFormat, renderTarget.width, renderTarget.height );
  518. }
  519. } else {
  520. _gl.renderbufferStorage( _gl.RENDERBUFFER, glInternalFormat, renderTarget.width, renderTarget.height );
  521. }
  522. _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer );
  523. } else if ( renderTarget.depthBuffer && renderTarget.stencilBuffer ) {
  524. const samples = getRenderTargetSamples( renderTarget );
  525. if ( isMultisample && renderTarget.useMultisampleRenderbuffer ) {
  526. _gl.renderbufferStorageMultisample( _gl.RENDERBUFFER, samples, _gl.DEPTH24_STENCIL8, renderTarget.width, renderTarget.height );
  527. } else if ( renderTarget.useMultisampleRenderToTexture ) {
  528. msaaExt.renderbufferStorageMultisampleEXT( _gl.RENDERBUFFER, samples, _gl.DEPTH24_STENCIL8, renderTarget.width, renderTarget.height );
  529. } else {
  530. _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.DEPTH_STENCIL, renderTarget.width, renderTarget.height );
  531. }
  532. _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_STENCIL_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer );
  533. } else {
  534. // Use the first texture for MRT so far
  535. const texture = renderTarget.isWebGLMultipleRenderTargets === true ? renderTarget.texture[ 0 ] : renderTarget.texture;
  536. const glFormat = utils.convert( texture.format );
  537. const glType = utils.convert( texture.type );
  538. const glInternalFormat = getInternalFormat( texture.internalFormat, glFormat, glType );
  539. const samples = getRenderTargetSamples( renderTarget );
  540. if ( isMultisample && renderTarget.useMultisampleRenderbuffer ) {
  541. _gl.renderbufferStorageMultisample( _gl.RENDERBUFFER, samples, glInternalFormat, renderTarget.width, renderTarget.height );
  542. } else if ( renderTarget.useMultisampleRenderToTexture ) {
  543. msaaExt.renderbufferStorageMultisampleEXT( _gl.RENDERBUFFER, samples, glInternalFormat, renderTarget.width, renderTarget.height );
  544. } else {
  545. _gl.renderbufferStorage( _gl.RENDERBUFFER, glInternalFormat, renderTarget.width, renderTarget.height );
  546. }
  547. }
  548. _gl.bindRenderbuffer( _gl.RENDERBUFFER, null );
  549. }
  550. // Setup resources for a Depth Texture for a FBO (needs an extension)
  551. function setupDepthTexture( framebuffer, renderTarget ) {
  552. const isCube = ( renderTarget && renderTarget.isWebGLCubeRenderTarget );
  553. if ( isCube ) throw new Error( 'Depth Texture with cube render targets is not supported' );
  554. state.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
  555. if ( ! ( renderTarget.depthTexture && renderTarget.depthTexture.isDepthTexture ) ) {
  556. throw new Error( 'renderTarget.depthTexture must be an instance of THREE.DepthTexture' );
  557. }
  558. // upload an empty depth texture with framebuffer size
  559. if ( ! properties.get( renderTarget.depthTexture ).__webglTexture ||
  560. renderTarget.depthTexture.image.width !== renderTarget.width ||
  561. renderTarget.depthTexture.image.height !== renderTarget.height ) {
  562. renderTarget.depthTexture.image.width = renderTarget.width;
  563. renderTarget.depthTexture.image.height = renderTarget.height;
  564. renderTarget.depthTexture.needsUpdate = true;
  565. }
  566. setTexture2D( renderTarget.depthTexture, 0 );
  567. const webglDepthTexture = properties.get( renderTarget.depthTexture ).__webglTexture;
  568. const samples = getRenderTargetSamples( renderTarget );
  569. if ( renderTarget.depthTexture.format === DepthFormat ) {
  570. if ( renderTarget.useMultisampleRenderToTexture ) {
  571. msaaExt.framebufferTexture2DMultisampleEXT( _gl.FRAMEBUFFER, _gl.DEPTH_ATTACHMENT, _gl.TEXTURE_2D, webglDepthTexture, 0, samples );
  572. } else {
  573. _gl.framebufferTexture2D( _gl.FRAMEBUFFER, _gl.DEPTH_ATTACHMENT, _gl.TEXTURE_2D, webglDepthTexture, 0 );
  574. }
  575. } else if ( renderTarget.depthTexture.format === DepthStencilFormat ) {
  576. if ( renderTarget.useMultisampleRenderToTexture ) {
  577. msaaExt.framebufferTexture2DMultisampleEXT( _gl.FRAMEBUFFER, _gl.DEPTH_STENCIL_ATTACHMENT, _gl.TEXTURE_2D, webglDepthTexture, 0, samples );
  578. } else {
  579. _gl.framebufferTexture2D( _gl.FRAMEBUFFER, _gl.DEPTH_STENCIL_ATTACHMENT, _gl.TEXTURE_2D, webglDepthTexture, 0 );
  580. }
  581. } else {
  582. throw new Error( 'Unknown depthTexture format' );
  583. }
  584. }
  585. // Setup GL resources for a non-texture depth buffer
  586. function setupDepthRenderbuffer( renderTarget ) {
  587. const renderTargetProperties = properties.get( renderTarget );
  588. const isCube = ( renderTarget.isWebGLCubeRenderTarget === true );
  589. if ( renderTarget.depthTexture && ! renderTarget.autoAllocateDepthBuffer ) {
  590. if ( isCube ) throw new Error( 'target.depthTexture not supported in Cube render targets' );
  591. setupDepthTexture( renderTargetProperties.__webglFramebuffer, renderTarget );
  592. } else {
  593. if ( isCube ) {
  594. renderTargetProperties.__webglDepthbuffer = [];
  595. for ( let i = 0; i < 6; i ++ ) {
  596. state.bindFramebuffer( _gl.FRAMEBUFFER, renderTargetProperties.__webglFramebuffer[ i ] );
  597. renderTargetProperties.__webglDepthbuffer[ i ] = _gl.createRenderbuffer();
  598. setupRenderBufferStorage( renderTargetProperties.__webglDepthbuffer[ i ], renderTarget, false );
  599. }
  600. } else {
  601. state.bindFramebuffer( _gl.FRAMEBUFFER, renderTargetProperties.__webglFramebuffer );
  602. renderTargetProperties.__webglDepthbuffer = _gl.createRenderbuffer();
  603. setupRenderBufferStorage( renderTargetProperties.__webglDepthbuffer, renderTarget, false );
  604. }
  605. }
  606. state.bindFramebuffer( _gl.FRAMEBUFFER, null );
  607. }
  608. // rebind framebuffer with external textures
  609. function rebindTextures( renderTarget, colorTexture, depthTexture ) {
  610. const renderTargetProperties = properties.get( renderTarget );
  611. if ( colorTexture !== undefined ) {
  612. setupFrameBufferTexture( renderTargetProperties.__webglFramebuffer, renderTarget, renderTarget.texture, _gl.COLOR_ATTACHMENT0, _gl.TEXTURE_2D );
  613. }
  614. if ( depthTexture !== undefined ) {
  615. setupDepthRenderbuffer( renderTarget );
  616. }
  617. }
  618. // Set up GL resources for the render target
  619. function setupRenderTarget( renderTarget ) {
  620. const texture = renderTarget.texture;
  621. const renderTargetProperties = properties.get( renderTarget );
  622. const textureProperties = properties.get( texture );
  623. renderTarget.addEventListener( 'dispose', onRenderTargetDispose );
  624. if ( renderTarget.isWebGLMultipleRenderTargets !== true ) {
  625. if ( textureProperties.__webglTexture === undefined ) {
  626. textureProperties.__webglTexture = _gl.createTexture();
  627. }
  628. textureProperties.__version = texture.version;
  629. info.memory.textures ++;
  630. }
  631. const isCube = ( renderTarget.isWebGLCubeRenderTarget === true );
  632. const isMultipleRenderTargets = ( renderTarget.isWebGLMultipleRenderTargets === true );
  633. const isRenderTarget3D = texture.isDataTexture3D || texture.isDataTexture2DArray;
  634. const supportsMips = isPowerOfTwo( renderTarget ) || isWebGL2;
  635. // Handles WebGL2 RGBFormat fallback - #18858
  636. if ( isWebGL2 && texture.format === RGBFormat && ( texture.type === FloatType || texture.type === HalfFloatType ) ) {
  637. texture.format = RGBAFormat;
  638. console.warn( 'THREE.WebGLRenderer: Rendering to textures with RGB format is not supported. Using RGBA format instead.' );
  639. }
  640. // Setup framebuffer
  641. if ( isCube ) {
  642. renderTargetProperties.__webglFramebuffer = [];
  643. for ( let i = 0; i < 6; i ++ ) {
  644. renderTargetProperties.__webglFramebuffer[ i ] = _gl.createFramebuffer();
  645. }
  646. } else {
  647. renderTargetProperties.__webglFramebuffer = _gl.createFramebuffer();
  648. if ( isMultipleRenderTargets ) {
  649. if ( capabilities.drawBuffers ) {
  650. const textures = renderTarget.texture;
  651. for ( let i = 0, il = textures.length; i < il; i ++ ) {
  652. const attachmentProperties = properties.get( textures[ i ] );
  653. if ( attachmentProperties.__webglTexture === undefined ) {
  654. attachmentProperties.__webglTexture = _gl.createTexture();
  655. info.memory.textures ++;
  656. }
  657. }
  658. } else {
  659. console.warn( 'THREE.WebGLRenderer: WebGLMultipleRenderTargets can only be used with WebGL2 or WEBGL_draw_buffers extension.' );
  660. }
  661. } else if ( renderTarget.useMultisampleRenderbuffer ) {
  662. if ( isWebGL2 ) {
  663. renderTargetProperties.__webglMultisampledFramebuffer = _gl.createFramebuffer();
  664. renderTargetProperties.__webglColorRenderbuffer = _gl.createRenderbuffer();
  665. _gl.bindRenderbuffer( _gl.RENDERBUFFER, renderTargetProperties.__webglColorRenderbuffer );
  666. const glFormat = utils.convert( texture.format );
  667. const glType = utils.convert( texture.type );
  668. const glInternalFormat = getInternalFormat( texture.internalFormat, glFormat, glType );
  669. const samples = getRenderTargetSamples( renderTarget );
  670. _gl.renderbufferStorageMultisample( _gl.RENDERBUFFER, samples, glInternalFormat, renderTarget.width, renderTarget.height );
  671. state.bindFramebuffer( _gl.FRAMEBUFFER, renderTargetProperties.__webglMultisampledFramebuffer );
  672. _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, _gl.RENDERBUFFER, renderTargetProperties.__webglColorRenderbuffer );
  673. _gl.bindRenderbuffer( _gl.RENDERBUFFER, null );
  674. if ( renderTarget.depthBuffer ) {
  675. renderTargetProperties.__webglDepthRenderbuffer = _gl.createRenderbuffer();
  676. setupRenderBufferStorage( renderTargetProperties.__webglDepthRenderbuffer, renderTarget, true );
  677. }
  678. state.bindFramebuffer( _gl.FRAMEBUFFER, null );
  679. } else {
  680. console.warn( 'THREE.WebGLRenderer: WebGLMultisampleRenderTarget can only be used with WebGL2.' );
  681. }
  682. }
  683. }
  684. // Setup color buffer
  685. if ( isCube ) {
  686. state.bindTexture( _gl.TEXTURE_CUBE_MAP, textureProperties.__webglTexture );
  687. setTextureParameters( _gl.TEXTURE_CUBE_MAP, texture, supportsMips );
  688. for ( let i = 0; i < 6; i ++ ) {
  689. setupFrameBufferTexture( renderTargetProperties.__webglFramebuffer[ i ], renderTarget, texture, _gl.COLOR_ATTACHMENT0, _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i );
  690. }
  691. if ( textureNeedsGenerateMipmaps( texture, supportsMips ) ) {
  692. generateMipmap( _gl.TEXTURE_CUBE_MAP, texture, renderTarget.width, renderTarget.height );
  693. }
  694. state.unbindTexture();
  695. } else if ( isMultipleRenderTargets ) {
  696. const textures = renderTarget.texture;
  697. for ( let i = 0, il = textures.length; i < il; i ++ ) {
  698. const attachment = textures[ i ];
  699. const attachmentProperties = properties.get( attachment );
  700. state.bindTexture( _gl.TEXTURE_2D, attachmentProperties.__webglTexture );
  701. setTextureParameters( _gl.TEXTURE_2D, attachment, supportsMips );
  702. setupFrameBufferTexture( renderTargetProperties.__webglFramebuffer, renderTarget, attachment, _gl.COLOR_ATTACHMENT0 + i, _gl.TEXTURE_2D );
  703. if ( textureNeedsGenerateMipmaps( attachment, supportsMips ) ) {
  704. generateMipmap( _gl.TEXTURE_2D, attachment, renderTarget.width, renderTarget.height );
  705. }
  706. }
  707. state.unbindTexture();
  708. } else {
  709. let glTextureType = _gl.TEXTURE_2D;
  710. if ( isRenderTarget3D ) {
  711. // Render targets containing layers, i.e: Texture 3D and 2d arrays
  712. if ( isWebGL2 ) {
  713. const isTexture3D = texture.isDataTexture3D;
  714. glTextureType = isTexture3D ? _gl.TEXTURE_3D : _gl.TEXTURE_2D_ARRAY;
  715. } else {
  716. console.warn( 'THREE.DataTexture3D and THREE.DataTexture2DArray only supported with WebGL2.' );
  717. }
  718. }
  719. state.bindTexture( glTextureType, textureProperties.__webglTexture );
  720. setTextureParameters( glTextureType, texture, supportsMips );
  721. setupFrameBufferTexture( renderTargetProperties.__webglFramebuffer, renderTarget, texture, _gl.COLOR_ATTACHMENT0, glTextureType );
  722. if ( textureNeedsGenerateMipmaps( texture, supportsMips ) ) {
  723. generateMipmap( glTextureType, texture, renderTarget.width, renderTarget.height, renderTarget.depth );
  724. }
  725. state.unbindTexture();
  726. }
  727. // Setup depth and stencil buffers
  728. if ( renderTarget.depthBuffer ) {
  729. setupDepthRenderbuffer( renderTarget );
  730. }
  731. }
  732. function updateRenderTargetMipmap( renderTarget ) {
  733. const supportsMips = isPowerOfTwo( renderTarget ) || isWebGL2;
  734. const textures = renderTarget.isWebGLMultipleRenderTargets === true ? renderTarget.texture : [ renderTarget.texture ];
  735. for ( let i = 0, il = textures.length; i < il; i ++ ) {
  736. const texture = textures[ i ];
  737. if ( textureNeedsGenerateMipmaps( texture, supportsMips ) ) {
  738. const target = renderTarget.isWebGLCubeRenderTarget ? _gl.TEXTURE_CUBE_MAP : _gl.TEXTURE_2D;
  739. const webglTexture = properties.get( texture ).__webglTexture;
  740. state.bindTexture( target, webglTexture );
  741. generateMipmap( target, texture, renderTarget.width, renderTarget.height );
  742. state.unbindTexture();
  743. }
  744. }
  745. }
  746. function updateMultisampleRenderTarget( renderTarget ) {
  747. if ( renderTarget.useMultisampleRenderbuffer ) {
  748. if ( isWebGL2 ) {
  749. const width = renderTarget.width;
  750. const height = renderTarget.height;
  751. let mask = _gl.COLOR_BUFFER_BIT;
  752. const invalidationArray = [ _gl.COLOR_ATTACHMENT0 ];
  753. const depthStyle = renderTarget.stencilBuffer ? _gl.DEPTH_STENCIL_ATTACHMENT : _gl.DEPTH_ATTACHMENT;
  754. if ( renderTarget.depthBuffer ) {
  755. invalidationArray.push( depthStyle );
  756. }
  757. if ( ! renderTarget.ignoreDepthForMultisampleCopy ) {
  758. if ( renderTarget.depthBuffer ) mask |= _gl.DEPTH_BUFFER_BIT;
  759. if ( renderTarget.stencilBuffer ) mask |= _gl.STENCIL_BUFFER_BIT;
  760. }
  761. const renderTargetProperties = properties.get( renderTarget );
  762. state.bindFramebuffer( _gl.READ_FRAMEBUFFER, renderTargetProperties.__webglMultisampledFramebuffer );
  763. state.bindFramebuffer( _gl.DRAW_FRAMEBUFFER, renderTargetProperties.__webglFramebuffer );
  764. if ( renderTarget.ignoreDepthForMultisampleCopy ) {
  765. _gl.invalidateFramebuffer( _gl.READ_FRAMEBUFFER, [ depthStyle ] );
  766. _gl.invalidateFramebuffer( _gl.DRAW_FRAMEBUFFER, [ depthStyle ] );
  767. }
  768. _gl.blitFramebuffer( 0, 0, width, height, 0, 0, width, height, mask, _gl.NEAREST );
  769. _gl.invalidateFramebuffer( _gl.READ_FRAMEBUFFER, invalidationArray );
  770. state.bindFramebuffer( _gl.READ_FRAMEBUFFER, null );
  771. state.bindFramebuffer( _gl.DRAW_FRAMEBUFFER, renderTargetProperties.__webglMultisampledFramebuffer );
  772. } else {
  773. console.warn( 'THREE.WebGLRenderer: WebGLMultisampleRenderTarget can only be used with WebGL2.' );
  774. }
  775. }
  776. }
  777. function getRenderTargetSamples( renderTarget ) {
  778. return ( isWebGL2 && ( renderTarget.useMultisampleRenderbuffer || renderTarget.useMultisampleRenderToTexture ) ) ?
  779. Math.min( maxSamples, renderTarget.samples ) : 0;
  780. }
  781. function updateVideoTexture( texture ) {
  782. const frame = info.render.frame;
  783. // Check the last frame we updated the VideoTexture
  784. if ( _videoTextures.get( texture ) !== frame ) {
  785. _videoTextures.set( texture, frame );
  786. texture.update();
  787. }
  788. }
  789. // backwards compatibility
  790. let warnedTexture2D = false;
  791. let warnedTextureCube = false;
  792. function safeSetTexture2D( texture, slot ) {
  793. if ( texture && texture.isWebGLRenderTarget ) {
  794. if ( warnedTexture2D === false ) {
  795. console.warn( 'THREE.WebGLTextures.safeSetTexture2D: don\'t use render targets as textures. Use their .texture property instead.' );
  796. warnedTexture2D = true;
  797. }
  798. texture = texture.texture;
  799. }
  800. setTexture2D( texture, slot );
  801. }
  802. function safeSetTextureCube( texture, slot ) {
  803. if ( texture && texture.isWebGLCubeRenderTarget ) {
  804. if ( warnedTextureCube === false ) {
  805. console.warn( 'THREE.WebGLTextures.safeSetTextureCube: don\'t use cube render targets as textures. Use their .texture property instead.' );
  806. warnedTextureCube = true;
  807. }
  808. texture = texture.texture;
  809. }
  810. setTextureCube( texture, slot );
  811. }
  812. //
  813. this.allocateTextureUnit = allocateTextureUnit;
  814. this.resetTextureUnits = resetTextureUnits;
  815. this.setTexture2D = setTexture2D;
  816. this.setTexture2DArray = setTexture2DArray;
  817. this.setTexture3D = setTexture3D;
  818. this.setTextureCube = setTextureCube;
  819. this.rebindTextures = rebindTextures;
  820. this.setupRenderTarget = setupRenderTarget;
  821. this.updateRenderTargetMipmap = updateRenderTargetMipmap;
  822. this.updateMultisampleRenderTarget = updateMultisampleRenderTarget;
  823. this.setupDepthRenderbuffer = setupDepthRenderbuffer;
  824. this.setupFrameBufferTexture = setupFrameBufferTexture;
  825. this.safeSetTexture2D = safeSetTexture2D;
  826. this.safeSetTextureCube = safeSetTextureCube;
  827. }
  828. export { WebGLTextures };