在Fragment Shader中添加通用凹凸贴图

时间:2017-04-28 12:20:28

标签: opengl heightmap bump-mapping

我有一个正常的obj,我想在它上面使用表面凹凸贴图。 任何人都可以帮助我如何从obj法线和凹凸贴图计算最终法线贴图

我的片段着色器是:

fragment float4 fragmentShaderObj(VertexOutObj interpolated [[stage_in]], const device Uniforms&  uniforms    [[ buffer(1) ]], texture2d_array<float>  tex2D     [[ texture(0) ]], sampler           sampler2D [[ sampler(0) ]]) {
    float4x4 mv_Matrix = uniforms.modelMatrix;
    // Ambient
    Light light = uniforms.light;
    if(interpolated.mapB >= 0){
        float3 norm = (2.0*tex2D.sample(sampler2D, interpolated.texCoord,interpolated.mapB).rgb-float3(1,1,1));
    }
    interpolated.normal = normalize(interpolated.normal);



    float4 ambientColor = float4(light.color *interpolated.ka, 1);

    //Diffuse
    float3 normal_inv = float3(0,0,0)-interpolated.normal;

    //float diffuseFactor1 = max(0.0,dot(interpolated.normal, light.direction));

    float diffuseFactor1 = max(max(0.0,dot(interpolated.normal, light.direction)),dot(normal_inv, light.direction));
    float4 diffuseColor = float4(light.color * diffuseFactor1*interpolated.kd ,1.0);
    //Specular
        float3 eye = normalize(interpolated.fragmentPosition); 
        float3 reflection = reflect(light.direction, interpolated.normal);
        float specularFactor = max(0.0, dot(reflection, eye));
        float4 specularColor = float4(light.color * interpolated.ns * specularFactor * interpolated.ks,1.0);

    float4 color = tex2D.sample(sampler2D, interpolated.texCoord,interpolated.mapKd);


    if(color.a < 0.1)
        discard_fragment();
    color.rgb = color.rgb * (ambientColor + diffuseColor + specularColor).rgb;
    return color;
}

现在如何使用interpolated.normal添加该范数。它不能直接添加。

0 个答案:

没有答案