简体   繁体   English

Webgl在线上应用1D纹理

[英]Webgl apply 1D texture on a line

I have an webgl application with a lot of segments (gl.LINES). 我有一个包含很多细分(gl.LINES)的webgl应用程序。 Each segment needs to apply a specific 1D texture, or if you prefer, each pixels of a segment needs to have a specific color. 每个细分都需要应用特定的1D纹理,或者,如果您愿意,细分的每个像素都需要具有特定的颜色。 This will be use as an alternative to an histogram on the segment. 这将用作段上直方图的替代方法。

I made a test texture of 4 pixels from red to black. 我做了一个从红色到黑色的4像素测试纹理。 I would have liked to see a gradient from red to black, but instead the output is a red only line. 我希望看到从红色到黑色的渐变,但是输出是仅红色的线条。

Here is my getTextureFromPixelArray method: 这是我的getTextureFromPixelArray方法:

Scene.getTextureFromPixelArray = function(data, width, height) {
    if (_.isArray(data) && data.length) {
        var gl = this._context;
        data = new Uint8Array(data);
        var texture = gl.createTexture();
        gl.bindTexture(gl.TEXTURE_2D, texture);
        gl.pixelStorei(gl.UNPACK_ALIGNMENT, 1);
        gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, data);
        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
        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.bindTexture(gl.TEXTURE_2D, null);
        return texture;
    } else {
        this.log("Unable to create texture");
    }
    return null;
};

Here is my code for generating a test texture 1D : 这是我用于生成测试纹理1D的代码:

var verticles = [];

verticles.push(this.vertex1.position[0]);
verticles.push(this.vertex1.position[1]);
verticles.push(this.vertex2.position[0]);
verticles.push(this.vertex2.position[1]);

var color = [];
var textureWidth = 4;
var textureHeight = 1;
var textureCoords = [];
for (var i=0;i<textureHeight;i++) {
    for (var j=0;j<textureWidth;j++) {
        color.push(Math.round(255 - j*255/(textureWidth-1))); // Red
        color.push(0); // Green
        color.push(0); // Blue
        color.push(255); // Alpha
    }
}
textureCoords.push(0);
textureCoords.push(0.5);
textureCoords.push(1);
textureCoords.push(0.5);

var vertexPositionAttributeLocation = gl.getAttribLocation(shaderProgram, "aVertexPosition"); 
var textureCoordAttributeLocation = gl.getAttribLocation(shaderProgram, "aTextureCoord");

// Set vertex buffers
gl.enableVertexAttribArray(vertexPositionAttributeLocation);
var verticlesBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, verticlesBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(verticles), gl.STATIC_DRAW);
gl.vertexAttribPointer(vertexPositionAttributeLocation, 2, gl.FLOAT, false, 0, 0);

var texture = this.getTextureFromPixelArray(textureData, textureWidth, textureHeight||1);

var vertexTextureCoordBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexTextureCoordBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(textureCoords), gl.STATIC_DRAW);
gl.vertexAttribPointer(textureCoordAttributeLocation, 2, gl.FLOAT, false, 0, 0);

gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.uniform1i(gl.getUniformLocation(shaderProgram, "uSampler"), 0);

// Draw the segment
gl.drawArrays(gl.LINES, 0, verticles.length/2);

Here is my shaders: 这是我的着色器:

Scene.fragmentShaderSrc = "\
    precision mediump float;\
    varying highp vec2 vTextureCoord;\
    uniform sampler2D uSampler;\
    void main(void) {gl_FragColor = texture2D(uSampler, vTextureCoord);}\
";

Scene.vertexShaderSrc = "\
    attribute vec2 aVertexPosition;\
    attribute vec2 aTextureCoord;\
    uniform mat4 uPMatrix;\
    uniform mat4 uModelView;\
    varying highp vec2 vTextureCoord;\
    void main(void) {\
        gl_Position = uPMatrix * uModelView * vec4(aVertexPosition,0,1);\
        vTextureCoord = aTextureCoord;\
    }\
";

Can someone see where I made an error? 有人可以看到我在哪里出错吗?

I needed to enable the texture coordinate attribute with : 我需要使用以下命令启用纹理坐标属性:

gl.enableVertexAttribArray(textureCoordAttributeLocation);

before 之前

var vertexTextureCoordBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexTextureCoordBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(textureCoords), gl.STATIC_DRAW);
gl.vertexAttribPointer(textureCoordAttributeLocation, 2, gl.FLOAT, false, 0, 0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM