GLSL内置dFdx功能无法识别

时间:2017-09-16 22:02:16

标签: graphics 3d three.js webgl shader

我在GLSL中编写着色器。这些着色器:(顶点着色器和片段着色器)用于平面着色,但我的问题是:

它不起作用,所以我尝试以某种方式调试它。当所有内容都被评论时,它运行良好,但是,当我取消注释第一行vec3 u = dFdx(vVertex);时,它会停止工作。

所以我的假设是它不理解dFdx()函数。在其他功能(如pow())中,情况类似。

我没有导入任何库,或者可能存在其他问题?

我使用THREE.js并将着色器插入应用程序。

<script type="text/x-glsl" id="flatVertexShader">
varying vec3 vVertex;

void main(){
    vVertex = vec3(modelMatrix * vec4(position, 1.0));
    gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4( position, 1.0 );
}   

</script>

<script type="text/x-glsl" id="flatFragmentShader">
varying vec3 vVertex;

uniform int ill_type;           //type of illumination
uniform vec3 uMaterialColor;    //color of material
uniform float uKa;              //ambient coefficient [0,1]
uniform float uKd;              //diffuse coefficient [0,1]
uniform float uKs;              //specular coefficient [0,1]
uniform float uNShiny;          //shininess coefficient
uniform vec3 uDirLightPos;      //light position
uniform float uLightIntensity;  //light intensity [0,1]

void main()
{     
    vec3 u = dFdx(vVertex);
    //vec3 v = dFdy(vVertex);
    //vec3 u = vVertex;
    //vec3 v = vVertex;
    //vec3 N = normalize(cross(u,v));

    //ambient
    float ambient = uKa;
    float diffuse = 0.0;
    float specular = 0.0;


    //vec3 L = normalize(uDirLightPos - vVertex);

    //diffuse
    //diffuse += uLightIntensity * uKd * max(dot(N, L), 0.0);


    if(ill_type == 0){  //Phong Illumination Model
        //vec3 R = normalize(reflect(L,N));
        //specular += uLightIntensity * uKs * pow(max(dot(R,normalize(vVertex)),0.0), uNShiny);
    }
    else{               //Blinn Illumination Model
        //vec3 H = L-vVector;
        //H = H / length(H);
        //specular += uKs * uLightIntensity * pow(max(dot(N,H),0.0), uNShiny);
    }


    float total = ambient + diffuse + specular;

    gl_FragColor = vec4(total * uMaterialColor.x, total*uMaterialColor.y, total * uMaterialColor.z,1.0);
}

0 个答案:

没有答案