| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ja/docs/Web/API/WebGL_API/Tutorial/Using_textures_in_WebGL | [Back] [Original] |
Get to know MDN better
6
: 2 "webgl-demo.js"
//
//
//
//
function loadTexture(gl, url) {
const texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
//
//
// 1
//
//
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 = () => {
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texImage2D(
gl.TEXTURE_2D,
level,
internalFormat,
srcFormat,
srcType,
image,
);
// WebGL1 2
// 2
//
if (isPowerOf2(image.width) && isPowerOf2(image.height)) {
// 2 mips
gl.generateMipmap(gl.TEXTURE_2D);
} else {
// 2 mips
//
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 createTexture() WebGL texture texImage2D()
Image src URL image.onload texImage2D() 2
WebGL1 2 NEAREST LINEAR CLAMP_TO_EDGE 2 2 WebGL REPEAT MIRRORED_REPEAT
UV texParameteri() UV UV NPOT 2
// gl.LINEAR gl.NEAREST
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 (rgb(0 0 0 / 0%))
main() loadTexture() initBuffers(gl)
WebGL Why is my WebGL texture upside-down?
gl.UNPACK_FLIP_Y_WEBGL true pixelStorei() WebGL
:
main() initBuffers()
//
const texture = loadTexture(gl, "cubetexture.png");
// WebGL
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
: cubetexture.png JavaScript
initBuffers()
: "init-buffer.js"
function initTextureBuffer(gl) {
const textureCoordBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, textureCoordBuffer);
const textureCoordinates = [
//
0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0,
//
0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0,
//
0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0,
//
0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0,
//
0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0,
//
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 textureCoordBuffer;
}
GL
textureCoordinates 0.0 1.0 0.0 1.0
GL
initBuffers()
:
"init-buffers.js" initBuffers() initColorBuffer()
const textureCoordBuffer = initTextureBuffer(gl);
:
"init-buffers.js" initBuffers() return
return {
position: positionBuffer,
textureCoord: textureCoordBuffer,
indices: indexBuffer,
};
:
main() vsSource
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;
}
`;
:
main() fsSource
const fsSource = `
varying highp vec2 vTextureCoord;
uniform sampler2D uSampler;
void main(void) {
gl_FragColor = texture2D(uSampler, vTextureCoord);
}
`;
Texel vTextureCoord
:
main() programInfo
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()
:
"draw-scene.js" drawScene()
// webgl
function setTextureAttribute(gl, buffers, programInfo) {
const num = 2; // 2
const type = gl.FLOAT; // 32
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);
}
:
"draw-scene.js" drawScene() setColorAttribute()
setTextureAttribute(gl, buffers, programInfo);
:
drawScene() gl.uniformMatrix4fv() 2
// 0 WebGL
gl.activeTexture(gl.TEXTURE0);
// 0
gl.bindTexture(gl.TEXTURE_2D, texture);
// 0
gl.uniform1i(programInfo.uniformLocations.uSampler, 0);
WebGL 8 gl.TEXTURE0 0 WebGL bindTexture() 0 TEXTURE_2D uSampler 0
drawScene() texture
drawScene()
function drawScene(gl, programInfo, buffers, texture, cubeRotation) {
//
}
main() drawScene()
drawScene(gl, programInfo, buffers, texture, cubeRotation);
WebGL CORS CORS HTTP
CORS WebGL hacks.mozilla.org
2D WebGL 2D <canvas> canvas
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_indexed| Web Proxy Viewer | New URL | Original Page |