Spaces:
Running
Running
File size: 47,967 Bytes
2b7aae2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 | import {
LinearFilter,
LinearMipmapLinearFilter,
LinearMipmapNearestFilter,
NearestFilter,
NearestMipmapLinearFilter,
NearestMipmapNearestFilter,
RGBAFormat,
DepthFormat,
DepthStencilFormat,
UnsignedShortType,
UnsignedIntType,
UnsignedInt248Type,
FloatType,
HalfFloatType,
MirroredRepeatWrapping,
ClampToEdgeWrapping,
RepeatWrapping,
sRGBEncoding,
LinearEncoding,
UnsignedByteType,
_SRGBAFormat,
} from '../../constants.js';
import * as MathUtils from '../../math/MathUtils.js';
import { ImageUtils } from '../../extras/ImageUtils.js';
import { createElementNS } from '../../utils.js';
import { Image } from 'skia-canvas';
function WebGLTextures(_gl, extensions, state, properties, capabilities, utils, info) {
const isWebGL2 = capabilities.isWebGL2;
const maxTextures = capabilities.maxTextures;
const maxCubemapSize = capabilities.maxCubemapSize;
const maxTextureSize = capabilities.maxTextureSize;
const maxSamples = capabilities.maxSamples;
const hasMultisampledRenderToTexture = extensions.has('WEBGL_multisampled_render_to_texture');
const MultisampledRenderToTextureExtension = hasMultisampledRenderToTexture ? extensions.get('WEBGL_multisampled_render_to_texture') : undefined;
const _videoTextures = new WeakMap();
let _canvas;
// cordova iOS (as of 5.0) still uses UIWebView, which provides OffscreenCanvas,
// also OffscreenCanvas.getContext("webgl"), but not OffscreenCanvas.getContext("2d")!
// Some implementations may only implement OffscreenCanvas partially (e.g. lacking 2d).
let useOffscreenCanvas = false;
try {
useOffscreenCanvas = typeof OffscreenCanvas !== 'undefined' && new OffscreenCanvas(1, 1).getContext('2d') !== null;
} catch (err) {
// Ignore any errors
}
function createCanvas(width, height) {
// Use OffscreenCanvas when available. Specially needed in web workers
return useOffscreenCanvas ? new OffscreenCanvas(width, height) : createElementNS('canvas');
}
function resizeImage(image, needsPowerOfTwo, needsNewCanvas, maxSize) {
let scale = 1;
// handle case if texture exceeds max size
if (image.width > maxSize || image.height > maxSize) {
scale = maxSize / Math.max(image.width, image.height);
}
// only perform resize if necessary
if (scale < 1 || needsPowerOfTwo === true) {
// only perform resize for certain image types
if (
(typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement) ||
(typeof HTMLCanvasElement !== 'undefined' && image instanceof HTMLCanvasElement) ||
(typeof ImageBitmap !== 'undefined' && image instanceof ImageBitmap) ||
(typeof Image !== 'undefined' && image instanceof Image)
) {
const floor = needsPowerOfTwo ? MathUtils.floorPowerOfTwo : Math.floor;
const width = floor(scale * image.width);
const height = floor(scale * image.height);
if (_canvas === undefined) _canvas = createCanvas(width, height);
// cube textures can't reuse the same canvas
const canvas = needsNewCanvas ? createCanvas(width, height) : _canvas;
canvas.width = width;
canvas.height = height;
const context = canvas.getContext('2d');
context.drawImage(image, 0, 0, width, height);
console.warn(
'THREE.WebGLRenderer: Texture has been resized from (' + image.width + 'x' + image.height + ') to (' + width + 'x' + height + ').'
);
return canvas;
} else {
if ('data' in image) {
console.warn('THREE.WebGLRenderer: Image in DataTexture is too big (' + image.width + 'x' + image.height + ').');
}
return image;
}
}
return image;
}
function isPowerOfTwo(image) {
return MathUtils.isPowerOfTwo(image.width) && MathUtils.isPowerOfTwo(image.height);
}
function textureNeedsPowerOfTwo(texture) {
if (isWebGL2) return false;
return (
texture.wrapS !== ClampToEdgeWrapping ||
texture.wrapT !== ClampToEdgeWrapping ||
(texture.minFilter !== NearestFilter && texture.minFilter !== LinearFilter)
);
}
function textureNeedsGenerateMipmaps(texture, supportsMips) {
return texture.generateMipmaps && supportsMips && texture.minFilter !== NearestFilter && texture.minFilter !== LinearFilter;
}
function generateMipmap(target) {
_gl.generateMipmap(target);
}
function getInternalFormat(internalFormatName, glFormat, glType, encoding, isVideoTexture = false) {
if (isWebGL2 === false) return glFormat;
if (internalFormatName !== null) {
if (_gl[internalFormatName] !== undefined) return _gl[internalFormatName];
console.warn("THREE.WebGLRenderer: Attempt to use non-existing WebGL internal format '" + internalFormatName + "'");
}
let internalFormat = glFormat;
if (glFormat === _gl.RED) {
if (glType === _gl.FLOAT) internalFormat = _gl.R32F;
if (glType === _gl.HALF_FLOAT) internalFormat = _gl.R16F;
if (glType === _gl.UNSIGNED_BYTE) internalFormat = _gl.R8;
}
if (glFormat === _gl.RGB) {
if (glType === _gl.FLOAT) internalFormat = _gl.RGB32F;
if (glType === _gl.HALF_FLOAT) internalFormat = _gl.RGB16F;
if (glType === _gl.UNSIGNED_BYTE) internalFormat = _gl.RGB8;
}
if (glFormat === _gl.RGBA) {
if (glType === _gl.FLOAT) internalFormat = _gl.RGBA32F;
if (glType === _gl.HALF_FLOAT) internalFormat = _gl.RGBA16F;
if (glType === _gl.UNSIGNED_BYTE) internalFormat = encoding === sRGBEncoding && isVideoTexture === false ? _gl.SRGB8_ALPHA8 : _gl.RGBA8;
}
if (internalFormat === _gl.R16F || internalFormat === _gl.R32F || internalFormat === _gl.RGBA16F || internalFormat === _gl.RGBA32F) {
extensions.get('EXT_color_buffer_float');
}
return internalFormat;
}
function getMipLevels(texture, image, supportsMips) {
if (
textureNeedsGenerateMipmaps(texture, supportsMips) === true ||
(texture.isFramebufferTexture && texture.minFilter !== NearestFilter && texture.minFilter !== LinearFilter)
) {
return Math.log2(Math.max(image.width, image.height)) + 1;
} else if (texture.mipmaps !== undefined && texture.mipmaps.length > 0) {
// user-defined mipmaps
return texture.mipmaps.length;
} else if (texture.isCompressedTexture && Array.isArray(texture.image)) {
return image.mipmaps.length;
} else {
// texture without mipmaps (only base level)
return 1;
}
}
// Fallback filters for non-power-of-2 textures
function filterFallback(f) {
if (f === NearestFilter || f === NearestMipmapNearestFilter || f === NearestMipmapLinearFilter) {
return _gl.NEAREST;
}
return _gl.LINEAR;
}
//
function onTextureDispose(event) {
const texture = event.target;
texture.removeEventListener('dispose', onTextureDispose);
deallocateTexture(texture);
if (texture.isVideoTexture) {
_videoTextures.delete(texture);
}
info.memory.textures--;
}
function onRenderTargetDispose(event) {
const renderTarget = event.target;
renderTarget.removeEventListener('dispose', onRenderTargetDispose);
deallocateRenderTarget(renderTarget);
}
//
function deallocateTexture(texture) {
const textureProperties = properties.get(texture);
if (textureProperties.__webglInit === undefined) return;
_gl.deleteTexture(textureProperties.__webglTexture);
properties.remove(texture);
}
function deallocateRenderTarget(renderTarget) {
const texture = renderTarget.texture;
const renderTargetProperties = properties.get(renderTarget);
const textureProperties = properties.get(texture);
if (!renderTarget) return;
if (textureProperties.__webglTexture !== undefined) {
_gl.deleteTexture(textureProperties.__webglTexture);
info.memory.textures--;
}
if (renderTarget.depthTexture) {
renderTarget.depthTexture.dispose();
}
if (renderTarget.isWebGLCubeRenderTarget) {
for (let i = 0; i < 6; i++) {
_gl.deleteFramebuffer(renderTargetProperties.__webglFramebuffer[i]);
if (renderTargetProperties.__webglDepthbuffer) _gl.deleteRenderbuffer(renderTargetProperties.__webglDepthbuffer[i]);
}
} else {
_gl.deleteFramebuffer(renderTargetProperties.__webglFramebuffer);
if (renderTargetProperties.__webglDepthbuffer) _gl.deleteRenderbuffer(renderTargetProperties.__webglDepthbuffer);
if (renderTargetProperties.__webglMultisampledFramebuffer) _gl.deleteFramebuffer(renderTargetProperties.__webglMultisampledFramebuffer);
if (renderTargetProperties.__webglColorRenderbuffer) _gl.deleteRenderbuffer(renderTargetProperties.__webglColorRenderbuffer);
if (renderTargetProperties.__webglDepthRenderbuffer) _gl.deleteRenderbuffer(renderTargetProperties.__webglDepthRenderbuffer);
}
if (renderTarget.isWebGLMultipleRenderTargets) {
for (let i = 0, il = texture.length; i < il; i++) {
const attachmentProperties = properties.get(texture[i]);
if (attachmentProperties.__webglTexture) {
_gl.deleteTexture(attachmentProperties.__webglTexture);
info.memory.textures--;
}
properties.remove(texture[i]);
}
}
properties.remove(texture);
properties.remove(renderTarget);
}
//
let textureUnits = 0;
function resetTextureUnits() {
textureUnits = 0;
}
function allocateTextureUnit() {
const textureUnit = textureUnits;
if (textureUnit >= maxTextures) {
console.warn('THREE.WebGLTextures: Trying to use ' + textureUnit + ' texture units while this GPU supports only ' + maxTextures);
}
textureUnits += 1;
return textureUnit;
}
//
function setTexture2D(texture, slot) {
const textureProperties = properties.get(texture);
if (texture.isVideoTexture) updateVideoTexture(texture);
if (texture.version > 0 && textureProperties.__version !== texture.version) {
const image = texture.image;
if (image === undefined) {
console.warn('THREE.WebGLRenderer: Texture marked for update but image is undefined');
} else if (image.complete === false) {
console.warn('THREE.WebGLRenderer: Texture marked for update but image is incomplete');
} else {
uploadTexture(textureProperties, texture, slot);
return;
}
}
state.activeTexture(_gl.TEXTURE0 + slot);
state.bindTexture(_gl.TEXTURE_2D, textureProperties.__webglTexture);
}
function setTexture2DArray(texture, slot) {
const textureProperties = properties.get(texture);
if (texture.version > 0 && textureProperties.__version !== texture.version) {
uploadTexture(textureProperties, texture, slot);
return;
}
state.activeTexture(_gl.TEXTURE0 + slot);
state.bindTexture(_gl.TEXTURE_2D_ARRAY, textureProperties.__webglTexture);
}
function setTexture3D(texture, slot) {
const textureProperties = properties.get(texture);
if (texture.version > 0 && textureProperties.__version !== texture.version) {
uploadTexture(textureProperties, texture, slot);
return;
}
state.activeTexture(_gl.TEXTURE0 + slot);
state.bindTexture(_gl.TEXTURE_3D, textureProperties.__webglTexture);
}
function setTextureCube(texture, slot) {
const textureProperties = properties.get(texture);
if (texture.version > 0 && textureProperties.__version !== texture.version) {
uploadCubeTexture(textureProperties, texture, slot);
return;
}
state.activeTexture(_gl.TEXTURE0 + slot);
state.bindTexture(_gl.TEXTURE_CUBE_MAP, textureProperties.__webglTexture);
}
const wrappingToGL = {
[RepeatWrapping]: _gl.REPEAT,
[ClampToEdgeWrapping]: _gl.CLAMP_TO_EDGE,
[MirroredRepeatWrapping]: _gl.MIRRORED_REPEAT,
};
const filterToGL = {
[NearestFilter]: _gl.NEAREST,
[NearestMipmapNearestFilter]: _gl.NEAREST_MIPMAP_NEAREST,
[NearestMipmapLinearFilter]: _gl.NEAREST_MIPMAP_LINEAR,
[LinearFilter]: _gl.LINEAR,
[LinearMipmapNearestFilter]: _gl.LINEAR_MIPMAP_NEAREST,
[LinearMipmapLinearFilter]: _gl.LINEAR_MIPMAP_LINEAR,
};
function setTextureParameters(textureType, texture, supportsMips) {
if (supportsMips) {
_gl.texParameteri(textureType, _gl.TEXTURE_WRAP_S, wrappingToGL[texture.wrapS]);
_gl.texParameteri(textureType, _gl.TEXTURE_WRAP_T, wrappingToGL[texture.wrapT]);
if (textureType === _gl.TEXTURE_3D || textureType === _gl.TEXTURE_2D_ARRAY) {
_gl.texParameteri(textureType, _gl.TEXTURE_WRAP_R, wrappingToGL[texture.wrapR]);
}
_gl.texParameteri(textureType, _gl.TEXTURE_MAG_FILTER, filterToGL[texture.magFilter]);
_gl.texParameteri(textureType, _gl.TEXTURE_MIN_FILTER, filterToGL[texture.minFilter]);
} else {
_gl.texParameteri(textureType, _gl.TEXTURE_WRAP_S, _gl.CLAMP_TO_EDGE);
_gl.texParameteri(textureType, _gl.TEXTURE_WRAP_T, _gl.CLAMP_TO_EDGE);
if (textureType === _gl.TEXTURE_3D || textureType === _gl.TEXTURE_2D_ARRAY) {
_gl.texParameteri(textureType, _gl.TEXTURE_WRAP_R, _gl.CLAMP_TO_EDGE);
}
if (texture.wrapS !== ClampToEdgeWrapping || texture.wrapT !== ClampToEdgeWrapping) {
console.warn('THREE.WebGLRenderer: Texture is not power of two. Texture.wrapS and Texture.wrapT should be set to THREE.ClampToEdgeWrapping.');
}
_gl.texParameteri(textureType, _gl.TEXTURE_MAG_FILTER, filterFallback(texture.magFilter));
_gl.texParameteri(textureType, _gl.TEXTURE_MIN_FILTER, filterFallback(texture.minFilter));
if (texture.minFilter !== NearestFilter && texture.minFilter !== LinearFilter) {
console.warn('THREE.WebGLRenderer: Texture is not power of two. Texture.minFilter should be set to THREE.NearestFilter or THREE.LinearFilter.');
}
}
if (extensions.has('EXT_texture_filter_anisotropic') === true) {
const extension = extensions.get('EXT_texture_filter_anisotropic');
if (texture.type === FloatType && extensions.has('OES_texture_float_linear') === false) return; // verify extension for WebGL 1 and WebGL 2
if (isWebGL2 === false && texture.type === HalfFloatType && extensions.has('OES_texture_half_float_linear') === false) return; // verify extension for WebGL 1 only
if (texture.anisotropy > 1 || properties.get(texture).__currentAnisotropy) {
_gl.texParameterf(textureType, extension.TEXTURE_MAX_ANISOTROPY_EXT, Math.min(texture.anisotropy, capabilities.getMaxAnisotropy()));
properties.get(texture).__currentAnisotropy = texture.anisotropy;
}
}
}
function initTexture(textureProperties, texture) {
if (textureProperties.__webglInit === undefined) {
textureProperties.__webglInit = true;
texture.addEventListener('dispose', onTextureDispose);
textureProperties.__webglTexture = _gl.createTexture();
info.memory.textures++;
}
}
function uploadTexture(textureProperties, texture, slot) {
let textureType = _gl.TEXTURE_2D;
if (texture.isDataTexture2DArray) textureType = _gl.TEXTURE_2D_ARRAY;
if (texture.isDataTexture3D) textureType = _gl.TEXTURE_3D;
initTexture(textureProperties, texture);
state.activeTexture(_gl.TEXTURE0 + slot);
state.bindTexture(textureType, textureProperties.__webglTexture);
_gl.pixelStorei(_gl.UNPACK_FLIP_Y_WEBGL, texture.flipY);
_gl.pixelStorei(_gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, texture.premultiplyAlpha);
_gl.pixelStorei(_gl.UNPACK_ALIGNMENT, texture.unpackAlignment);
_gl.pixelStorei(_gl.UNPACK_COLORSPACE_CONVERSION_WEBGL, _gl.NONE);
const needsPowerOfTwo = textureNeedsPowerOfTwo(texture) && isPowerOfTwo(texture.image) === false;
let image = resizeImage(texture.image, needsPowerOfTwo, false, maxTextureSize);
image = verifyColorSpace(texture, image);
const supportsMips = isPowerOfTwo(image) || isWebGL2,
glFormat = utils.convert(texture.format, texture.encoding);
let glType = utils.convert(texture.type),
glInternalFormat = getInternalFormat(texture.internalFormat, glFormat, glType, texture.encoding, texture.isVideoTexture);
setTextureParameters(textureType, texture, supportsMips);
let mipmap;
const mipmaps = texture.mipmaps;
const useTexStorage = isWebGL2 && texture.isVideoTexture !== true;
const allocateMemory = textureProperties.__version === undefined;
const levels = getMipLevels(texture, image, supportsMips);
if (texture.isDepthTexture) {
// populate depth texture with dummy data
glInternalFormat = _gl.DEPTH_COMPONENT;
if (isWebGL2) {
if (texture.type === FloatType) {
glInternalFormat = _gl.DEPTH_COMPONENT32F;
} else if (texture.type === UnsignedIntType) {
glInternalFormat = _gl.DEPTH_COMPONENT24;
} else if (texture.type === UnsignedInt248Type) {
glInternalFormat = _gl.DEPTH24_STENCIL8;
} else {
glInternalFormat = _gl.DEPTH_COMPONENT16; // WebGL2 requires sized internalformat for glTexImage2D
}
} else {
if (texture.type === FloatType) {
console.error('WebGLRenderer: Floating point depth texture requires WebGL2.');
}
}
// validation checks for WebGL 1
if (texture.format === DepthFormat && glInternalFormat === _gl.DEPTH_COMPONENT) {
// The error INVALID_OPERATION is generated by texImage2D if format and internalformat are
// DEPTH_COMPONENT and type is not UNSIGNED_SHORT or UNSIGNED_INT
// (https://www.khronos.org/registry/webgl/extensions/WEBGL_depth_texture/)
if (texture.type !== UnsignedShortType && texture.type !== UnsignedIntType) {
console.warn('THREE.WebGLRenderer: Use UnsignedShortType or UnsignedIntType for DepthFormat DepthTexture.');
texture.type = UnsignedShortType;
glType = utils.convert(texture.type);
}
}
if (texture.format === DepthStencilFormat && glInternalFormat === _gl.DEPTH_COMPONENT) {
// Depth stencil textures need the DEPTH_STENCIL internal format
// (https://www.khronos.org/registry/webgl/extensions/WEBGL_depth_texture/)
glInternalFormat = _gl.DEPTH_STENCIL;
// The error INVALID_OPERATION is generated by texImage2D if format and internalformat are
// DEPTH_STENCIL and type is not UNSIGNED_INT_24_8_WEBGL.
// (https://www.khronos.org/registry/webgl/extensions/WEBGL_depth_texture/)
if (texture.type !== UnsignedInt248Type) {
console.warn('THREE.WebGLRenderer: Use UnsignedInt248Type for DepthStencilFormat DepthTexture.');
texture.type = UnsignedInt248Type;
glType = utils.convert(texture.type);
}
}
//
if (useTexStorage && allocateMemory) {
state.texStorage2D(_gl.TEXTURE_2D, 1, glInternalFormat, image.width, image.height);
} else {
state.texImage2D(_gl.TEXTURE_2D, 0, glInternalFormat, image.width, image.height, 0, glFormat, glType, null);
}
} else if (texture.isDataTexture) {
// use manually created mipmaps if available
// if there are no manual mipmaps
// set 0 level mipmap and then use GL to generate other mipmap levels
if (mipmaps.length > 0 && supportsMips) {
if (useTexStorage && allocateMemory) {
state.texStorage2D(_gl.TEXTURE_2D, levels, glInternalFormat, mipmaps[0].width, mipmaps[0].height);
}
for (let i = 0, il = mipmaps.length; i < il; i++) {
mipmap = mipmaps[i];
if (useTexStorage) {
state.texSubImage2D(_gl.TEXTURE_2D, 0, 0, 0, mipmap.width, mipmap.height, glFormat, glType, mipmap.data);
} else {
state.texImage2D(_gl.TEXTURE_2D, i, glInternalFormat, mipmap.width, mipmap.height, 0, glFormat, glType, mipmap.data);
}
}
texture.generateMipmaps = false;
} else {
if (useTexStorage) {
if (allocateMemory) {
state.texStorage2D(_gl.TEXTURE_2D, levels, glInternalFormat, image.width, image.height);
}
state.texSubImage2D(_gl.TEXTURE_2D, 0, 0, 0, image.width, image.height, glFormat, glType, image.data);
} else {
state.texImage2D(_gl.TEXTURE_2D, 0, glInternalFormat, image.width, image.height, 0, glFormat, glType, image.data);
}
}
} else if (texture.isCompressedTexture) {
if (useTexStorage && allocateMemory) {
state.texStorage2D(_gl.TEXTURE_2D, levels, glInternalFormat, mipmaps[0].width, mipmaps[0].height);
}
for (let i = 0, il = mipmaps.length; i < il; i++) {
mipmap = mipmaps[i];
if (texture.format !== RGBAFormat) {
if (glFormat !== null) {
if (useTexStorage) {
state.compressedTexSubImage2D(_gl.TEXTURE_2D, i, 0, 0, mipmap.width, mipmap.height, glFormat, mipmap.data);
} else {
state.compressedTexImage2D(_gl.TEXTURE_2D, i, glInternalFormat, mipmap.width, mipmap.height, 0, mipmap.data);
}
} else {
console.warn('THREE.WebGLRenderer: Attempt to load unsupported compressed texture format in .uploadTexture()');
}
} else {
if (useTexStorage) {
state.texSubImage2D(_gl.TEXTURE_2D, i, 0, 0, mipmap.width, mipmap.height, glFormat, glType, mipmap.data);
} else {
state.texImage2D(_gl.TEXTURE_2D, i, glInternalFormat, mipmap.width, mipmap.height, 0, glFormat, glType, mipmap.data);
}
}
}
} else if (texture.isDataTexture2DArray) {
if (useTexStorage) {
if (allocateMemory) {
state.texStorage3D(_gl.TEXTURE_2D_ARRAY, levels, glInternalFormat, image.width, image.height, image.depth);
}
state.texSubImage3D(_gl.TEXTURE_2D_ARRAY, 0, 0, 0, 0, image.width, image.height, image.depth, glFormat, glType, image.data);
} else {
state.texImage3D(_gl.TEXTURE_2D_ARRAY, 0, glInternalFormat, image.width, image.height, image.depth, 0, glFormat, glType, image.data);
}
} else if (texture.isDataTexture3D) {
if (useTexStorage) {
if (allocateMemory) {
state.texStorage3D(_gl.TEXTURE_3D, levels, glInternalFormat, image.width, image.height, image.depth);
}
state.texSubImage3D(_gl.TEXTURE_3D, 0, 0, 0, 0, image.width, image.height, image.depth, glFormat, glType, image.data);
} else {
state.texImage3D(_gl.TEXTURE_3D, 0, glInternalFormat, image.width, image.height, image.depth, 0, glFormat, glType, image.data);
}
} else if (texture.isFramebufferTexture) {
if (useTexStorage && allocateMemory) {
state.texStorage2D(_gl.TEXTURE_2D, levels, glInternalFormat, image.width, image.height);
} else {
state.texImage2D(_gl.TEXTURE_2D, 0, glInternalFormat, image.width, image.height, 0, glFormat, glType, null);
}
} else {
// regular Texture (image, video, canvas)
// use manually created mipmaps if available
// if there are no manual mipmaps
// set 0 level mipmap and then use GL to generate other mipmap levels
if (mipmaps.length > 0 && supportsMips) {
if (useTexStorage && allocateMemory) {
state.texStorage2D(_gl.TEXTURE_2D, levels, glInternalFormat, mipmaps[0].width, mipmaps[0].height);
}
for (let i = 0, il = mipmaps.length; i < il; i++) {
mipmap = mipmaps[i];
if (useTexStorage) {
state.texSubImage2D(_gl.TEXTURE_2D, i, 0, 0, glFormat, glType, mipmap);
} else {
state.texImage2D(_gl.TEXTURE_2D, i, glInternalFormat, glFormat, glType, mipmap);
}
}
texture.generateMipmaps = false;
} else {
if (useTexStorage) {
if (allocateMemory) {
state.texStorage2D(_gl.TEXTURE_2D, levels, glInternalFormat, image.width, image.height);
}
state.texSubImage2D(_gl.TEXTURE_2D, 0, 0, 0, glFormat, glType, image);
} else {
state.texImage2D(_gl.TEXTURE_2D, 0, glInternalFormat, glFormat, glType, image);
}
}
}
if (textureNeedsGenerateMipmaps(texture, supportsMips)) {
generateMipmap(textureType);
}
textureProperties.__version = texture.version;
if (texture.onUpdate) texture.onUpdate(texture);
}
function uploadCubeTexture(textureProperties, texture, slot) {
if (texture.image.length !== 6) return;
initTexture(textureProperties, texture);
state.activeTexture(_gl.TEXTURE0 + slot);
state.bindTexture(_gl.TEXTURE_CUBE_MAP, textureProperties.__webglTexture);
_gl.pixelStorei(_gl.UNPACK_FLIP_Y_WEBGL, texture.flipY);
_gl.pixelStorei(_gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, texture.premultiplyAlpha);
_gl.pixelStorei(_gl.UNPACK_ALIGNMENT, texture.unpackAlignment);
_gl.pixelStorei(_gl.UNPACK_COLORSPACE_CONVERSION_WEBGL, _gl.NONE);
const isCompressed = texture && (texture.isCompressedTexture || texture.image[0].isCompressedTexture);
const isDataTexture = texture.image[0] && texture.image[0].isDataTexture;
const cubeImage = [];
for (let i = 0; i < 6; i++) {
if (!isCompressed && !isDataTexture) {
cubeImage[i] = resizeImage(texture.image[i], false, true, maxCubemapSize);
} else {
cubeImage[i] = isDataTexture ? texture.image[i].image : texture.image[i];
}
cubeImage[i] = verifyColorSpace(texture, cubeImage[i]);
}
const image = cubeImage[0],
supportsMips = isPowerOfTwo(image) || isWebGL2,
glFormat = utils.convert(texture.format, texture.encoding),
glType = utils.convert(texture.type),
glInternalFormat = getInternalFormat(texture.internalFormat, glFormat, glType, texture.encoding);
const useTexStorage = isWebGL2 && texture.isVideoTexture !== true;
const allocateMemory = textureProperties.__version === undefined;
let levels = getMipLevels(texture, image, supportsMips);
setTextureParameters(_gl.TEXTURE_CUBE_MAP, texture, supportsMips);
let mipmaps;
if (isCompressed) {
if (useTexStorage && allocateMemory) {
state.texStorage2D(_gl.TEXTURE_CUBE_MAP, levels, glInternalFormat, image.width, image.height);
}
for (let i = 0; i < 6; i++) {
mipmaps = cubeImage[i].mipmaps;
for (let j = 0; j < mipmaps.length; j++) {
const mipmap = mipmaps[j];
if (texture.format !== RGBAFormat) {
if (glFormat !== null) {
if (useTexStorage) {
state.compressedTexSubImage2D(_gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, j, 0, 0, mipmap.width, mipmap.height, glFormat, mipmap.data);
} else {
state.compressedTexImage2D(
_gl.TEXTURE_CUBE_MAP_POSITIVE_X + i,
j,
glInternalFormat,
mipmap.width,
mipmap.height,
0,
mipmap.data
);
}
} else {
console.warn('THREE.WebGLRenderer: Attempt to load unsupported compressed texture format in .setTextureCube()');
}
} else {
if (useTexStorage) {
state.texSubImage2D(_gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, j, 0, 0, mipmap.width, mipmap.height, glFormat, glType, mipmap.data);
} else {
state.texImage2D(
_gl.TEXTURE_CUBE_MAP_POSITIVE_X + i,
j,
glInternalFormat,
mipmap.width,
mipmap.height,
0,
glFormat,
glType,
mipmap.data
);
}
}
}
}
} else {
mipmaps = texture.mipmaps;
if (useTexStorage && allocateMemory) {
// TODO: Uniformly handle mipmap definitions
// Normal textures and compressed cube textures define base level + mips with their mipmap array
// Uncompressed cube textures use their mipmap array only for mips (no base level)
if (mipmaps.length > 0) levels++;
state.texStorage2D(_gl.TEXTURE_CUBE_MAP, levels, glInternalFormat, cubeImage[0].width, cubeImage[0].height);
}
for (let i = 0; i < 6; i++) {
if (isDataTexture) {
if (useTexStorage) {
state.texSubImage2D(
_gl.TEXTURE_CUBE_MAP_POSITIVE_X + i,
0,
0,
0,
cubeImage[i].width,
cubeImage[i].height,
glFormat,
glType,
cubeImage[i].data
);
} else {
state.texImage2D(
_gl.TEXTURE_CUBE_MAP_POSITIVE_X + i,
0,
glInternalFormat,
cubeImage[i].width,
cubeImage[i].height,
0,
glFormat,
glType,
cubeImage[i].data
);
}
for (let j = 0; j < mipmaps.length; j++) {
const mipmap = mipmaps[j];
const mipmapImage = mipmap.image[i].image;
if (useTexStorage) {
state.texSubImage2D(
_gl.TEXTURE_CUBE_MAP_POSITIVE_X + i,
j + 1,
0,
0,
mipmapImage.width,
mipmapImage.height,
glFormat,
glType,
mipmapImage.data
);
} else {
state.texImage2D(
_gl.TEXTURE_CUBE_MAP_POSITIVE_X + i,
j + 1,
glInternalFormat,
mipmapImage.width,
mipmapImage.height,
0,
glFormat,
glType,
mipmapImage.data
);
}
}
} else {
if (useTexStorage) {
state.texSubImage2D(_gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, 0, 0, glFormat, glType, cubeImage[i]);
} else {
state.texImage2D(_gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, glInternalFormat, glFormat, glType, cubeImage[i]);
}
for (let j = 0; j < mipmaps.length; j++) {
const mipmap = mipmaps[j];
if (useTexStorage) {
state.texSubImage2D(_gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, j + 1, 0, 0, glFormat, glType, mipmap.image[i]);
} else {
state.texImage2D(_gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, j + 1, glInternalFormat, glFormat, glType, mipmap.image[i]);
}
}
}
}
}
if (textureNeedsGenerateMipmaps(texture, supportsMips)) {
// We assume images for cube map have the same size.
generateMipmap(_gl.TEXTURE_CUBE_MAP);
}
textureProperties.__version = texture.version;
if (texture.onUpdate) texture.onUpdate(texture);
}
// Render targets
// Setup storage for target texture and bind it to correct framebuffer
function setupFrameBufferTexture(framebuffer, renderTarget, texture, attachment, textureTarget) {
const glFormat = utils.convert(texture.format, texture.encoding);
const glType = utils.convert(texture.type);
const glInternalFormat = getInternalFormat(texture.internalFormat, glFormat, glType, texture.encoding);
const renderTargetProperties = properties.get(renderTarget);
if (!renderTargetProperties.__hasExternalTextures) {
if (textureTarget === _gl.TEXTURE_3D || textureTarget === _gl.TEXTURE_2D_ARRAY) {
state.texImage3D(textureTarget, 0, glInternalFormat, renderTarget.width, renderTarget.height, renderTarget.depth, 0, glFormat, glType, null);
} else {
state.texImage2D(textureTarget, 0, glInternalFormat, renderTarget.width, renderTarget.height, 0, glFormat, glType, null);
}
}
state.bindFramebuffer(_gl.FRAMEBUFFER, framebuffer);
if (renderTarget.useRenderToTexture) {
MultisampledRenderToTextureExtension.framebufferTexture2DMultisampleEXT(
_gl.FRAMEBUFFER,
attachment,
textureTarget,
properties.get(texture).__webglTexture,
0,
getRenderTargetSamples(renderTarget)
);
} else {
_gl.framebufferTexture2D(_gl.FRAMEBUFFER, attachment, textureTarget, properties.get(texture).__webglTexture, 0);
}
state.bindFramebuffer(_gl.FRAMEBUFFER, null);
}
// Setup storage for internal depth/stencil buffers and bind to correct framebuffer
function setupRenderBufferStorage(renderbuffer, renderTarget, isMultisample) {
_gl.bindRenderbuffer(_gl.RENDERBUFFER, renderbuffer);
if (renderTarget.depthBuffer && !renderTarget.stencilBuffer) {
let glInternalFormat = _gl.DEPTH_COMPONENT16;
if (isMultisample || renderTarget.useRenderToTexture) {
const depthTexture = renderTarget.depthTexture;
if (depthTexture && depthTexture.isDepthTexture) {
if (depthTexture.type === FloatType) {
glInternalFormat = _gl.DEPTH_COMPONENT32F;
} else if (depthTexture.type === UnsignedIntType) {
glInternalFormat = _gl.DEPTH_COMPONENT24;
}
}
const samples = getRenderTargetSamples(renderTarget);
if (renderTarget.useRenderToTexture) {
MultisampledRenderToTextureExtension.renderbufferStorageMultisampleEXT(
_gl.RENDERBUFFER,
samples,
glInternalFormat,
renderTarget.width,
renderTarget.height
);
} else {
_gl.renderbufferStorageMultisample(_gl.RENDERBUFFER, samples, glInternalFormat, renderTarget.width, renderTarget.height);
}
} else {
_gl.renderbufferStorage(_gl.RENDERBUFFER, glInternalFormat, renderTarget.width, renderTarget.height);
}
_gl.framebufferRenderbuffer(_gl.FRAMEBUFFER, _gl.DEPTH_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer);
} else if (renderTarget.depthBuffer && renderTarget.stencilBuffer) {
const samples = getRenderTargetSamples(renderTarget);
if (isMultisample && renderTarget.useRenderbuffer) {
_gl.renderbufferStorageMultisample(_gl.RENDERBUFFER, samples, _gl.DEPTH24_STENCIL8, renderTarget.width, renderTarget.height);
} else if (renderTarget.useRenderToTexture) {
MultisampledRenderToTextureExtension.renderbufferStorageMultisampleEXT(
_gl.RENDERBUFFER,
samples,
_gl.DEPTH24_STENCIL8,
renderTarget.width,
renderTarget.height
);
} else {
_gl.renderbufferStorage(_gl.RENDERBUFFER, _gl.DEPTH_STENCIL, renderTarget.width, renderTarget.height);
}
_gl.framebufferRenderbuffer(_gl.FRAMEBUFFER, _gl.DEPTH_STENCIL_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer);
} else {
// Use the first texture for MRT so far
const texture = renderTarget.isWebGLMultipleRenderTargets === true ? renderTarget.texture[0] : renderTarget.texture;
const glFormat = utils.convert(texture.format, texture.encoding);
const glType = utils.convert(texture.type);
const glInternalFormat = getInternalFormat(texture.internalFormat, glFormat, glType, texture.encoding);
const samples = getRenderTargetSamples(renderTarget);
if (isMultisample && renderTarget.useRenderbuffer) {
_gl.renderbufferStorageMultisample(_gl.RENDERBUFFER, samples, glInternalFormat, renderTarget.width, renderTarget.height);
} else if (renderTarget.useRenderToTexture) {
MultisampledRenderToTextureExtension.renderbufferStorageMultisampleEXT(
_gl.RENDERBUFFER,
samples,
glInternalFormat,
renderTarget.width,
renderTarget.height
);
} else {
_gl.renderbufferStorage(_gl.RENDERBUFFER, glInternalFormat, renderTarget.width, renderTarget.height);
}
}
_gl.bindRenderbuffer(_gl.RENDERBUFFER, null);
}
// Setup resources for a Depth Texture for a FBO (needs an extension)
function setupDepthTexture(framebuffer, renderTarget) {
const isCube = renderTarget && renderTarget.isWebGLCubeRenderTarget;
if (isCube) throw new Error('Depth Texture with cube render targets is not supported');
state.bindFramebuffer(_gl.FRAMEBUFFER, framebuffer);
if (!(renderTarget.depthTexture && renderTarget.depthTexture.isDepthTexture)) {
throw new Error('renderTarget.depthTexture must be an instance of THREE.DepthTexture');
}
// upload an empty depth texture with framebuffer size
if (
!properties.get(renderTarget.depthTexture).__webglTexture ||
renderTarget.depthTexture.image.width !== renderTarget.width ||
renderTarget.depthTexture.image.height !== renderTarget.height
) {
renderTarget.depthTexture.image.width = renderTarget.width;
renderTarget.depthTexture.image.height = renderTarget.height;
renderTarget.depthTexture.needsUpdate = true;
}
setTexture2D(renderTarget.depthTexture, 0);
const webglDepthTexture = properties.get(renderTarget.depthTexture).__webglTexture;
const samples = getRenderTargetSamples(renderTarget);
if (renderTarget.depthTexture.format === DepthFormat) {
if (renderTarget.useRenderToTexture) {
MultisampledRenderToTextureExtension.framebufferTexture2DMultisampleEXT(
_gl.FRAMEBUFFER,
_gl.DEPTH_ATTACHMENT,
_gl.TEXTURE_2D,
webglDepthTexture,
0,
samples
);
} else {
_gl.framebufferTexture2D(_gl.FRAMEBUFFER, _gl.DEPTH_ATTACHMENT, _gl.TEXTURE_2D, webglDepthTexture, 0);
}
} else if (renderTarget.depthTexture.format === DepthStencilFormat) {
if (renderTarget.useRenderToTexture) {
MultisampledRenderToTextureExtension.framebufferTexture2DMultisampleEXT(
_gl.FRAMEBUFFER,
_gl.DEPTH_STENCIL_ATTACHMENT,
_gl.TEXTURE_2D,
webglDepthTexture,
0,
samples
);
} else {
_gl.framebufferTexture2D(_gl.FRAMEBUFFER, _gl.DEPTH_STENCIL_ATTACHMENT, _gl.TEXTURE_2D, webglDepthTexture, 0);
}
} else {
throw new Error('Unknown depthTexture format');
}
}
// Setup GL resources for a non-texture depth buffer
function setupDepthRenderbuffer(renderTarget) {
const renderTargetProperties = properties.get(renderTarget);
const isCube = renderTarget.isWebGLCubeRenderTarget === true;
if (renderTarget.depthTexture && !renderTargetProperties.__autoAllocateDepthBuffer) {
if (isCube) throw new Error('target.depthTexture not supported in Cube render targets');
setupDepthTexture(renderTargetProperties.__webglFramebuffer, renderTarget);
} else {
if (isCube) {
renderTargetProperties.__webglDepthbuffer = [];
for (let i = 0; i < 6; i++) {
state.bindFramebuffer(_gl.FRAMEBUFFER, renderTargetProperties.__webglFramebuffer[i]);
renderTargetProperties.__webglDepthbuffer[i] = _gl.createRenderbuffer();
setupRenderBufferStorage(renderTargetProperties.__webglDepthbuffer[i], renderTarget, false);
}
} else {
state.bindFramebuffer(_gl.FRAMEBUFFER, renderTargetProperties.__webglFramebuffer);
renderTargetProperties.__webglDepthbuffer = _gl.createRenderbuffer();
setupRenderBufferStorage(renderTargetProperties.__webglDepthbuffer, renderTarget, false);
}
}
state.bindFramebuffer(_gl.FRAMEBUFFER, null);
}
// rebind framebuffer with external textures
function rebindTextures(renderTarget, colorTexture, depthTexture) {
const renderTargetProperties = properties.get(renderTarget);
if (colorTexture !== undefined) {
setupFrameBufferTexture(renderTargetProperties.__webglFramebuffer, renderTarget, renderTarget.texture, _gl.COLOR_ATTACHMENT0, _gl.TEXTURE_2D);
}
if (depthTexture !== undefined) {
setupDepthRenderbuffer(renderTarget);
}
}
// Set up GL resources for the render target
function setupRenderTarget(renderTarget) {
const texture = renderTarget.texture;
const renderTargetProperties = properties.get(renderTarget);
const textureProperties = properties.get(texture);
renderTarget.addEventListener('dispose', onRenderTargetDispose);
if (renderTarget.isWebGLMultipleRenderTargets !== true) {
if (textureProperties.__webglTexture === undefined) {
textureProperties.__webglTexture = _gl.createTexture();
}
textureProperties.__version = texture.version;
info.memory.textures++;
}
const isCube = renderTarget.isWebGLCubeRenderTarget === true;
const isMultipleRenderTargets = renderTarget.isWebGLMultipleRenderTargets === true;
const isRenderTarget3D = texture.isDataTexture3D || texture.isDataTexture2DArray;
const supportsMips = isPowerOfTwo(renderTarget) || isWebGL2;
// Setup framebuffer
if (isCube) {
renderTargetProperties.__webglFramebuffer = [];
for (let i = 0; i < 6; i++) {
renderTargetProperties.__webglFramebuffer[i] = _gl.createFramebuffer();
}
} else {
renderTargetProperties.__webglFramebuffer = _gl.createFramebuffer();
if (isMultipleRenderTargets) {
if (capabilities.drawBuffers) {
const textures = renderTarget.texture;
for (let i = 0, il = textures.length; i < il; i++) {
const attachmentProperties = properties.get(textures[i]);
if (attachmentProperties.__webglTexture === undefined) {
attachmentProperties.__webglTexture = _gl.createTexture();
info.memory.textures++;
}
}
} else {
console.warn('THREE.WebGLRenderer: WebGLMultipleRenderTargets can only be used with WebGL2 or WEBGL_draw_buffers extension.');
}
} else if (renderTarget.useRenderbuffer) {
if (isWebGL2) {
renderTargetProperties.__webglMultisampledFramebuffer = _gl.createFramebuffer();
renderTargetProperties.__webglColorRenderbuffer = _gl.createRenderbuffer();
_gl.bindRenderbuffer(_gl.RENDERBUFFER, renderTargetProperties.__webglColorRenderbuffer);
const glFormat = utils.convert(texture.format, texture.encoding);
const glType = utils.convert(texture.type);
const glInternalFormat = getInternalFormat(texture.internalFormat, glFormat, glType, texture.encoding);
const samples = getRenderTargetSamples(renderTarget);
_gl.renderbufferStorageMultisample(_gl.RENDERBUFFER, samples, glInternalFormat, renderTarget.width, renderTarget.height);
state.bindFramebuffer(_gl.FRAMEBUFFER, renderTargetProperties.__webglMultisampledFramebuffer);
_gl.framebufferRenderbuffer(_gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, _gl.RENDERBUFFER, renderTargetProperties.__webglColorRenderbuffer);
_gl.bindRenderbuffer(_gl.RENDERBUFFER, null);
if (renderTarget.depthBuffer) {
renderTargetProperties.__webglDepthRenderbuffer = _gl.createRenderbuffer();
setupRenderBufferStorage(renderTargetProperties.__webglDepthRenderbuffer, renderTarget, true);
}
state.bindFramebuffer(_gl.FRAMEBUFFER, null);
} else {
console.warn('THREE.WebGLRenderer: WebGLMultisampleRenderTarget can only be used with WebGL2.');
}
}
}
// Setup color buffer
if (isCube) {
state.bindTexture(_gl.TEXTURE_CUBE_MAP, textureProperties.__webglTexture);
setTextureParameters(_gl.TEXTURE_CUBE_MAP, texture, supportsMips);
for (let i = 0; i < 6; i++) {
setupFrameBufferTexture(
renderTargetProperties.__webglFramebuffer[i],
renderTarget,
texture,
_gl.COLOR_ATTACHMENT0,
_gl.TEXTURE_CUBE_MAP_POSITIVE_X + i
);
}
if (textureNeedsGenerateMipmaps(texture, supportsMips)) {
generateMipmap(_gl.TEXTURE_CUBE_MAP);
}
state.unbindTexture();
} else if (isMultipleRenderTargets) {
const textures = renderTarget.texture;
for (let i = 0, il = textures.length; i < il; i++) {
const attachment = textures[i];
const attachmentProperties = properties.get(attachment);
state.bindTexture(_gl.TEXTURE_2D, attachmentProperties.__webglTexture);
setTextureParameters(_gl.TEXTURE_2D, attachment, supportsMips);
setupFrameBufferTexture(renderTargetProperties.__webglFramebuffer, renderTarget, attachment, _gl.COLOR_ATTACHMENT0 + i, _gl.TEXTURE_2D);
if (textureNeedsGenerateMipmaps(attachment, supportsMips)) {
generateMipmap(_gl.TEXTURE_2D);
}
}
state.unbindTexture();
} else {
let glTextureType = _gl.TEXTURE_2D;
if (isRenderTarget3D) {
// Render targets containing layers, i.e: Texture 3D and 2d arrays
if (isWebGL2) {
const isTexture3D = texture.isDataTexture3D;
glTextureType = isTexture3D ? _gl.TEXTURE_3D : _gl.TEXTURE_2D_ARRAY;
} else {
console.warn('THREE.DataTexture3D and THREE.DataTexture2DArray only supported with WebGL2.');
}
}
state.bindTexture(glTextureType, textureProperties.__webglTexture);
setTextureParameters(glTextureType, texture, supportsMips);
setupFrameBufferTexture(renderTargetProperties.__webglFramebuffer, renderTarget, texture, _gl.COLOR_ATTACHMENT0, glTextureType);
if (textureNeedsGenerateMipmaps(texture, supportsMips)) {
generateMipmap(glTextureType);
}
state.unbindTexture();
}
// Setup depth and stencil buffers
if (renderTarget.depthBuffer) {
setupDepthRenderbuffer(renderTarget);
}
}
function updateRenderTargetMipmap(renderTarget) {
const supportsMips = isPowerOfTwo(renderTarget) || isWebGL2;
const textures = renderTarget.isWebGLMultipleRenderTargets === true ? renderTarget.texture : [renderTarget.texture];
for (let i = 0, il = textures.length; i < il; i++) {
const texture = textures[i];
if (textureNeedsGenerateMipmaps(texture, supportsMips)) {
const target = renderTarget.isWebGLCubeRenderTarget ? _gl.TEXTURE_CUBE_MAP : _gl.TEXTURE_2D;
const webglTexture = properties.get(texture).__webglTexture;
state.bindTexture(target, webglTexture);
generateMipmap(target);
state.unbindTexture();
}
}
}
function updateMultisampleRenderTarget(renderTarget) {
if (renderTarget.useRenderbuffer) {
if (isWebGL2) {
const width = renderTarget.width;
const height = renderTarget.height;
let mask = _gl.COLOR_BUFFER_BIT;
const invalidationArray = [_gl.COLOR_ATTACHMENT0];
const depthStyle = renderTarget.stencilBuffer ? _gl.DEPTH_STENCIL_ATTACHMENT : _gl.DEPTH_ATTACHMENT;
if (renderTarget.depthBuffer) {
invalidationArray.push(depthStyle);
}
if (!renderTarget.ignoreDepthForMultisampleCopy) {
if (renderTarget.depthBuffer) mask |= _gl.DEPTH_BUFFER_BIT;
if (renderTarget.stencilBuffer) mask |= _gl.STENCIL_BUFFER_BIT;
}
const renderTargetProperties = properties.get(renderTarget);
state.bindFramebuffer(_gl.READ_FRAMEBUFFER, renderTargetProperties.__webglMultisampledFramebuffer);
state.bindFramebuffer(_gl.DRAW_FRAMEBUFFER, renderTargetProperties.__webglFramebuffer);
if (renderTarget.ignoreDepthForMultisampleCopy) {
_gl.invalidateFramebuffer(_gl.READ_FRAMEBUFFER, [depthStyle]);
_gl.invalidateFramebuffer(_gl.DRAW_FRAMEBUFFER, [depthStyle]);
}
_gl.blitFramebuffer(0, 0, width, height, 0, 0, width, height, mask, _gl.NEAREST);
_gl.invalidateFramebuffer(_gl.READ_FRAMEBUFFER, invalidationArray);
state.bindFramebuffer(_gl.READ_FRAMEBUFFER, null);
state.bindFramebuffer(_gl.DRAW_FRAMEBUFFER, renderTargetProperties.__webglMultisampledFramebuffer);
} else {
console.warn('THREE.WebGLRenderer: WebGLMultisampleRenderTarget can only be used with WebGL2.');
}
}
}
function getRenderTargetSamples(renderTarget) {
return isWebGL2 && (renderTarget.useRenderbuffer || renderTarget.useRenderToTexture) ? Math.min(maxSamples, renderTarget.samples) : 0;
}
function updateVideoTexture(texture) {
const frame = info.render.frame;
// Check the last frame we updated the VideoTexture
if (_videoTextures.get(texture) !== frame) {
_videoTextures.set(texture, frame);
texture.update();
}
}
function verifyColorSpace(texture, image) {
const encoding = texture.encoding;
const format = texture.format;
const type = texture.type;
if (texture.isCompressedTexture === true || texture.format === _SRGBAFormat) return image;
if (encoding !== LinearEncoding) {
// sRGB
if (encoding === sRGBEncoding && texture.isVideoTexture !== true) {
if (isWebGL2 === false) {
// in WebGL 1, try to use EXT_sRGB extension and unsized formats
if (extensions.has('EXT_sRGB') === true && format === RGBAFormat) {
texture.format = _SRGBAFormat;
// it's not possible to generate mips in WebGL 1 with this extension
texture.minFilter = LinearFilter;
texture.generateMipmaps = false;
} else {
// slow fallback (CPU decode)
image = ImageUtils.sRGBToLinear(image);
}
} else {
// in WebGL 2 uncompressed textures can only be sRGB encoded if they have the RGBA8 format
if (format !== RGBAFormat || type !== UnsignedByteType) {
console.warn('THREE.WebGLTextures: sRGB encoded textures have to use RGBAFormat and UnsignedByteType.');
}
}
} else {
console.error('THREE.WebGLTextures: Unsupported texture encoding:', encoding);
}
}
return image;
}
// backwards compatibility
let warnedTexture2D = false;
let warnedTextureCube = false;
function safeSetTexture2D(texture, slot) {
if (texture && texture.isWebGLRenderTarget) {
if (warnedTexture2D === false) {
console.warn("THREE.WebGLTextures.safeSetTexture2D: don't use render targets as textures. Use their .texture property instead.");
warnedTexture2D = true;
}
texture = texture.texture;
}
setTexture2D(texture, slot);
}
function safeSetTextureCube(texture, slot) {
if (texture && texture.isWebGLCubeRenderTarget) {
if (warnedTextureCube === false) {
console.warn("THREE.WebGLTextures.safeSetTextureCube: don't use cube render targets as textures. Use their .texture property instead.");
warnedTextureCube = true;
}
texture = texture.texture;
}
setTextureCube(texture, slot);
}
//
this.allocateTextureUnit = allocateTextureUnit;
this.resetTextureUnits = resetTextureUnits;
this.setTexture2D = setTexture2D;
this.setTexture2DArray = setTexture2DArray;
this.setTexture3D = setTexture3D;
this.setTextureCube = setTextureCube;
this.rebindTextures = rebindTextures;
this.setupRenderTarget = setupRenderTarget;
this.updateRenderTargetMipmap = updateRenderTargetMipmap;
this.updateMultisampleRenderTarget = updateMultisampleRenderTarget;
this.setupDepthRenderbuffer = setupDepthRenderbuffer;
this.setupFrameBufferTexture = setupFrameBufferTexture;
this.safeSetTexture2D = safeSetTexture2D;
this.safeSetTextureCube = safeSetTextureCube;
}
export { WebGLTextures };
|