[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/docs/Web/API/WebGL_API/Tutorial/Using_textures_in_WebGL [Back]  [Original]

WebGL - Web API | MDN

MDN Web Docs

View in English Always switch to English

WebGL

3D

6

: CORS

: 2 "webgl-demo.js"

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

js
// 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()

js
// 
const texture = loadTexture(gl, "cubetexture.png");
//  WebGL 
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);

: cubetexture.png JavaScript

initBuffers()

: "init-buffer.js"

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()

js
const textureCoordBuffer = initTextureBuffer(gl);

: "init-buffers.js" initBuffers() return

js
return {
  position: positionBuffer,
  textureCoord: textureCoordBuffer,
  indices: indexBuffer,
};

: main() vsSource

js
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

js
const fsSource = `
    varying highp vec2 vTextureCoord;

    uniform sampler2D uSampler;

    void main(void) {
      gl_FragColor = texture2D(uSampler, vTextureCoord);
    }
  `;

Texel vTextureCoord

: main() programInfo

js
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()

js
//  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()

js
setTextureAttribute(gl, buffers, programInfo);

: drawScene() gl.uniformMatrix4fv() 2

js
//  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()

js
function drawScene(gl, programInfo, buffers, texture, cubeRotation) {
  // 
}

main() drawScene()

js
drawScene(gl, programInfo, buffers, texture, cubeRotation);

|

WebGL CORS CORS HTTP

WebGL WebGL file:/// URL

CORS WebGL hacks.mozilla.org

2D WebGL 2D <canvas> canvas


Web Proxy Viewer  |  New URL  |  Original Page