为什么这个与一个绑定到“TEXCOORD0”语义的变量相关的着色器片段除以5

时间:2016-11-12 14:49:41

标签: shader gpu ogre

我是着色器编程的新手。只是有些人对以下代码感到困惑,其中“noiseCoord.xy”除以5,这真是一个很大的分母。据我所知,noiseCoord的范围通常为0.0到1.0。实际上,我有这样的情况,被一个大分母所划分。这是实验价值吗?希望你的伙伴能帮助我解决这个问题。先谢谢你!

       //Vertex program
        void main_vp_old(
                float4 pos          : POSITION,
                float4 normal       : NORMAL,
                float2 tex          : TEXCOORD0,

                out float4 oPos     : POSITION,
                out float fresnel   : COLOR,
                out float3 noiseCoord : TEXCOORD0,
                out float4 projectionCoord : TEXCOORD1,

                uniform float4x4 worldViewProjMatrix,
                uniform float3 eyePosition, // object space
                uniform float fresnelBias,
                uniform float fresnelScale,
                uniform float fresnelPower,
                uniform float timeVal,
                uniform float scale,  // the amount to scale the noise texture by
                uniform float scroll, // the amount by which to scroll the noise
                uniform float noise  // the noise perturb as a factor of the  time
                )
        {
            oPos = mul(worldViewProjMatrix, pos);
            ..........................................
            ..........................................
            // Noise map coords
            noiseCoord.xy = (tex + (timeVal * scroll)) * scale;
            noiseCoord.z = noise * timeVal;
            ..........................................
        }







 // Fragment program for distorting a texture using a 3D noise texture
    void main_fp(
            float3 noiseCoord           : TEXCOORD0,
            float4 projectionCoord      : TEXCOORD1,
            float3 eyeDir               : TEXCOORD2,
            float3 normal               : TEXCOORD3,

            out float4 col      : COLOR,

            uniform float4 tintColour,
            uniform float noiseScale, 
            uniform float fresnelBias,
            uniform float fresnelScale,
            uniform float fresnelPower,
            uniform sampler2D noiseMap : register(s0),
            uniform sampler2D reflectMap : register(s1),
            uniform sampler2D refractMap : register(s2)
            )
    {
        // Do the tex projection manually so we can distort _after_
        float2 final = projectionCoord.xy / projectionCoord.w;

        // just here, why was divided by such 5 instead of others?
        float3 noiseNormal = (tex2D(noiseMap, (noiseCoord.xy / 5)).rgb - 0.5).rbg * noiseScale;  


        final += noiseNormal.xz;

        // Fresnel
        //normal = normalize(normal + noiseNormal.xz);
        float fresnel = fresnelBias + fresnelScale * pow(1 + dot(eyeDir, normal), fresnelPower);

        // Reflection / refraction
        float4 reflectionColour = tex2D(reflectMap, final);
        float4 refractionColour = tex2D(refractMap, final) + tintColour;

        // Final colour
        col = lerp(refractionColour, reflectionColour, fresnel);

    }

1 个答案:

答案 0 :(得分:0)

是的,看起来没有充分的理由陪我。着色器代码已经在顶点着色器中应用了比例因子(统一称为' scale')。在片段着色器中应用另一个硬编码比例因子对我来说似乎没有道理。将统一的比例增加倍数将会高出许多倍。在CPU上0.2f。