簡體   English   中英

從畫布獲取webgl紋理

[英]Get a webgl texture from a canvas

我正在嘗試在另一個webGL畫布上繪制一個畫布。 這是我的代碼。

function createCanvas(width, height) {
    var canvas = document.createElement('canvas');
    canvas.width = width;
    canvas.height = height;
    return canvas
}

var webGLCanvas = createCanvas(1, 1);
const gl = webGLCanvas.getContext("webgl");

function getColoredTexture(image, color, clip, scale, notWebgl) {
    if(!color || color === 'None') {
        if (notWebgl) {
            var charCanvas = createCanvas(clip.w * scale, clip.h * scale)
            var charContext = charCanvas.getContext("2d");
            charContext.drawImage(image,
                clip.x, clip.y, clip.w, clip.h,
                0, 0, clip.w * scale, clip.h * scale);
            return charCanvas;
        }
        else {
            return image
        }
    }
    // Create new canvas
    var charCanvas = createCanvas(clip.w * scale, clip.h * scale)
    var charContext = charCanvas.getContext("2d");
    // Draw letter on it
    charContext.fillStyle = 'black';
    charContext.fillRect(0, 0, charCanvas.width, charCanvas.height);
    charContext.drawImage(image,
        clip.x, clip.y, clip.w, clip.h,
        0, 0, clip.w * scale, clip.h * scale);
    // Apply color
    charContext.globalCompositeOperation = 'multiply';
    charContext.fillStyle = color;
    charContext.fillRect(0, 0, clip.w * scale, clip.h * scale);
    // Restore the transparency
    charContext.globalCompositeOperation = 'destination-atop';
    charContext.drawImage(image,
        clip.x, clip.y, clip.w, clip.h,
        0, 0, clip.w * scale, clip.h * scale);
    // Restore composite operation
    charContext.globalCompositeOperation = 'source-over';

    if (notWebgl) {
        return charCanvas
    }
    else {

        return gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, charCanvas);
    }
}

我正在為具有2個渲染模式的Construct 2進行編碼:webgl和canvas。 該代碼非常適合畫布渲染模式。 我正在嘗試將圖像着色為給定的顏色並將其繪制。 為此,我將圖像繪制在小畫布上,乘以我的顏色,然后再增加透明度。 在webgl中,我想做同樣的事情,而不是返回畫布,而是要返回一個webGL Texture。 但是,當我這樣做時,出現以下錯誤:

WebGL:INVALID_VALUE:texImage2D:無畫布

為什么會發生這種情況,我該怎么辦?

創建畫布時使高度為0時出錯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM