| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ru/docs/Web/API/WebGL_API/Tutorial/Using_textures_in_WebGL | [Back] [Original] |
Get to know MDN better
This page was translated from English by the community. Learn more and join the MDN Web Docs community.
- .
. , , .
: , -, , , CORS . . "- " .
::
//
// .
// - .
//
function loadTexture(gl, url) {
const texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
// ,
// .
// ,
// .
// .
const level = 0;
const internalFormat = gl.RGBA;
const width = 1;
const height = 1;
const border = 0;
const srcFormat = gl.RGBA;
const srcType = gl.UNSIGNED_BYTE;
const pixel = new Uint8Array([0, 0, 255, 255]); //
gl.texImage2D(gl.TEXTURE_2D, level, internalFormat,
width, height, border, srcFormat, srcType,
pixel);
const image = new Image();
image.onload = function() {
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texImage2D(gl.TEXTURE_2D, level, internalFormat,
srcFormat, srcType, image);
// WebGL1 , 2,
// 2, ,
// 2 .
if (isPowerOf2(image.width) && isPowerOf2(image.height)) {
// 2. MIP'.
gl.generateMipmap(gl.TEXTURE_2D);
} else {
// 2.
// MIP'
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
}
};
image.src = url;
return texture;
}
function isPowerOf2(value) {
return (value & (value - 1)) == 0;
}
loadTexture() WebGL texture createTexture(). , texImage2D(). ( ) , .
, Image src , . , image.onload, . texImage2D(), . , , 2 .
WebGL1 , 2, NEAREST LINEAR , mipmap. CLAMP_TO_EDGE. , 2 , WebGL , mipmap REPEAT MIRRORED_REPEAT.
, .
UV- texParameteri(). , 2 (NPOT - non-power-of-two), , UV-, UV-, , .
// gl.NEAREST gl.LINEAR, mipmap. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); // s-. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); // t-. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
, WebGL ( ). WebGL NPOT-, rgba(0,0,0,0).
loadTexture() main(). initBuffers(gl).
// const texture = loadTexture(gl, 'cubetexture.png');
. . , initBuffers().
const textureCoordBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, textureCoordBuffer);
const textureCoordinates = [
// Front
0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
0.0, 1.0,
// Back
0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
0.0, 1.0,
// Top
0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
0.0, 1.0,
// Bottom
0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
0.0, 1.0,
// Right
0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
0.0, 1.0,
// Left
0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
0.0, 1.0,
];
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(textureCoordinates),
gl.STATIC_DRAW);
...
return {
position: positionBuffer,
textureCoord: textureCoordBuffer,
indices: indexBuffer,
};
WebGL , , , .
textureCoordinates , . , 0.0 1.0. 0.0 1.0, .
, , WebGL .
, , .
, .
const vsSource = `
attribute vec4 aVertexPosition;
attribute vec2 aTextureCoord;
uniform mat4 uModelViewMatrix;
uniform mat4 uProjectionMatrix;
varying highp vec2 vTextureCoord;
void main(void) {
gl_Position = uProjectionMatrix * uModelViewMatrix * aVertexPosition;
vTextureCoord = aTextureCoord;
}
`;
, , , , .
:
const fsSource = `
varying highp vec2 vTextureCoord;
uniform sampler2D uSampler;
void main(void) {
gl_FragColor = texture2D(uSampler, vTextureCoord);
}
`;
, ( ), vTextureCoord, ( ).
uniform-,
const programInfo = {
program: shaderProgram,
attribLocations: {
vertexPosition: gl.getAttribLocation(shaderProgram, 'aVertexPosition'),
textureCoord: gl.getAttribLocation(shaderProgram, 'aTextureCoord'),
},
uniformLocations: {
projectionMatrix: gl.getUniformLocation(shaderProgram, 'uProjectionMatrix'),
modelViewMatrix: gl.getUniformLocation(shaderProgram, 'uModelViewMatrix'),
uSampler: gl.getUniformLocation(shaderProgram, 'uSampler'),
},
};
drawScene().
-, , , :
// WebGL,
{
const num = 2; // 2
const type = gl.FLOAT; // 32 bit float
const normalize = false; //
const stride = 0; //
const offset = 0; //
gl.bindBuffer(gl.ARRAY_BUFFER, buffers.textureCoord);
gl.vertexAttribPointer(programInfo.attribLocations.textureCoord, num, type, normalize, stride, offset);
gl.enableVertexAttribArray(programInfo.attribLocations.textureCoord);
}
, , :
// WebGL, 0 gl.activeTexture(gl.TEXTURE0); // 0 gl.bindTexture(gl.TEXTURE_2D, texture); // , 0 gl.uniform1i(programInfo.uniformLocations.uSampler, 0);
WebGL 8 ; gl.TEXTURE0. , 0. bindTexture(), TEXTURE_2D 0. , uSampler 0.
, texture drawScene().
drawScene(gl, programInfo, buffers, texture, deltaTime);
...
function drawScene(gl, programInfo, buffers, texture, deltaTime) {
.
- - . , CORS . . HTTP access control.
hacks.mozilla.org , CORS WebGL .
:
CORS WebGL crossOrigin Gecko 8.0.
Tainted (--) 2D canvas WebGL. , 2D <canvas> "tainted", - .
:
CORS Canvas 2D drawImage Gecko 9.0. , CORS - 2D canvas "tained" (--), 2D canvas WebGL.
:
CORS - crossorigin HTML- <video> Gecko 12.0.
This page was last modified on 24 . 2025 . by MDN contributors.
WebGLRenderingContextWebGLBufferWebGLFramebufferWebGLRenderbufferWebGLObjectWebGLProgramWebGLShaderWebGLTextureWebGLUniformLocationWebGLActiveInfoWebGLShaderPrecisionFormatWebGLContextEventWebGLQueryWebGLSamplerWebGLSyncWebGLTransformFeedbackWebGLVertexArrayObjectWebGL2RenderingContextWEBGL_compressed_texture_s3tcWEBGL_compressed_texture_s3tc_srgbWEBGL_compressed_texture_etc1WEBGL_compressed_texture_pvrtcWEBGL_debug_renderer_infoWEBGL_debug_shadersWEBGL_depth_textureOES_element_index_uintEXT_frag_depthWEBGL_lose_contextEXT_texture_filter_anisotropicEXT_sRGBOES_standard_derivativesOES_texture_floatWEBGL_draw_buffersOES_texture_float_linearEXT_shader_texture_lodOES_texture_half_floatOES_texture_half_float_linearWEBGL_color_buffer_floatEXT_color_buffer_half_floatOES_vertex_array_objectANGLE_instanced_arraysEXT_blend_minmaxEXT_disjoint_timer_queryOES_draw_buffers_indexedYour blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |