Stage3d着色器奇怪的行为添加变量

时间:2013-03-07 11:18:18

标签: flash shader vertex-shader stage3d

我正在使用此着色器:

<script id="per-fragment-lighting-vs2" type="x-shader/x-vertex">
    attribute vec3 aVertexPosition;
    attribute vec3 aVertexNormal;
    attribute vec2 aTextureCoord;

    uniform mat4 uMVMatrix;
    uniform mat4 uMV2Matrix;
    uniform mat4 uPMatrix;
    uniform mat3 uNMatrix;

    varying vec2 vTextureCoord;
    varying vec3 vTransformedNormal;
    varying vec4 vPosition;

    void main(void) {
        gl_Position = uMVMatrix * vec4(aVertexPosition, 1.0);
        vTextureCoord = aTextureCoord;
        vTransformedNormal = uNMatrix * aVertexNormal;        
    }
</script>

<script id="per-fragment-lighting-fs2" type="x-shader/x-fragment">
    precision mediump float;

    varying vec2 vTextureCoord;
    varying vec3 vTransformedNormal;
    varying vec4 vPosition;

    uniform float uMaterialShininess;

    uniform vec3 uAmbientColor;

    uniform vec3 uPointLightingLocation;
    uniform vec3 uPointLightingSpecularColor;
    uniform vec3 uPointLightingDiffuseColor;

    uniform sampler2D uSampler;

    void main(void) {
        vec4 fragmentColor = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t));
        gl_FragColor = fragmentColor;
    }
</script>

enter image description here

但是如果我在着色器上添加​​一个变量,它会完全改变结果,尽管它没有在片段着色器上使用:

<script id="per-fragment-lighting-vs2" type="x-shader/x-vertex">
    attribute vec3 aVertexPosition;
    attribute vec3 aVertexNormal;
    attribute vec2 aTextureCoord;

    uniform mat4 uMVMatrix;
    uniform mat4 uMV2Matrix;
    uniform mat4 uPMatrix;
    uniform mat3 uNMatrix;

    varying vec2 vTextureCoord;
    varying vec3 vTransformedNormal;
    varying vec4 vPosition;

    void main(void) {
        gl_Position = uMVMatrix * vec4(aVertexPosition, 1.0);
        vTextureCoord = aTextureCoord;
        vTransformedNormal = uNMatrix * aVertexNormal;
        **vPosition = vec4(1,1,1,1);**
    }
</script>

enter image description here

你能帮我解释一下这里发生了什么吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

glsl2agal的问题是在顶点和像素着色器中以不同方式映射变量。

例如:

Vertex Shader                   
v0 vPosition                    
v1 vTextureCoord                
v2 vTransformedNormal

Fragment Shader
v1 vPosition
....

这显然导致纹理输出不良但顶点定位良好。

为了解决这个问题,我必须制作补丁以重新映射所有变量并搜索并替换生成的AGAL代码。

现在它工作正常。