2014-01-29 18 views
0

我已將鏡面反射鏡元件添加到我的基本光着色器中,並且出現鏡面光焦度問題。如果我將其值設置爲0,而不是沒有鏡面高光的預期遮罩對象,則會出現奇怪的效果,其中只有直接點亮的表面可見,而忽略環境和漫反射組件。零點像素着色器中的鏡面光焦度移除環境和漫反射

下面的圖片說明了這個問題:

Globe screenshots

我仍然可以與設置specularIntensity 0擺脫了鏡面元素,但我認爲設置specularPower爲0將有同樣的效果。下面是我的像素着色器的代碼,我想不出有什麼地方錯了:

float4 LightPixelShader(PixelInputType input) : SV_Target 
{ 
    float4 specular; 

    // Sample the pixel color from the texture using the sampler at this texture coordinate location. 
    float4 textureColor = shaderTexture.Sample(SampleType, input.tex); 

    // Set default output color to ambient light value for all pixels 
    float4 color = ambientColor; 

    // Invert the light direction for calculations. 
    float3 lightDir = -lightDirection; 

    // Calculate the amount of light on this pixel. 
    float lightIntensity = saturate(dot(input.normal, lightDir)); 

    if (lightIntensity > 0.0f) 
    { 
     // Determine final diffuse color 
     color += (diffuseColor * lightIntensity); 

     // Saturate ambient and diffuse color 
     color = saturate(color); 

     // Calculate the reflection vector based on the light intensity, normal vector and light direction 
     float3 reflection = normalize(2 * dot(lightDir, input.normal) * input.normal  - lightDir); 

     // Determine the amount of specular light based on the reflection vector, viewing direction and specular power 
     specular = specularIntensity * specularColor * pow(saturate(dot(reflection, normalize(input.viewDirection))), specularPower); 
    } 

    // Saturate the final color in case it's greater then 1 
    color = saturate(color); 

    // Multiply the texture pixel and the final diffuse color to get the final pixel color result. 
    color = color * textureColor; 

    // Add the specular component 
    color = saturate(color + specular); 

    return color; 
} 
+0

將'specular'初始化爲'float4(0,0,0,0)',所以如果像素不亮則不改變顏色。這可能就是爲什麼即使環境組件不見了。 –

+0

它似乎沒有太大的改變。 – jaho

+0

「不多」是什麼意思?環境組件是否重新出現? –

回答

3

罪魁禍首是這個無辜的前瞻性線位置:

specular = specularIntensity * specularColor * pow(saturate(dot(reflection, normalize(input.viewDirection))), specularPower); 

specularPower爲零,specular總是風因爲任何提高到零功率的變量都是1.因此,您所看到的最後一個地球是具有鏡面反射的所有像素的鏡面反射視圖。

但是您的問題需要幫助查找代碼中的問題。我沒有提供一個,因爲嚴格地說,沒有一個 - 因爲沒有問題。這只是一種怪異的反應。

+0

但是,通過適當的實現,0的鏡面反射能力(也稱爲光澤)不應導致完美的無光澤物體?如果公式是正確的,是否添加'if(specularPower <= 0)specular = 0;'語句有意義? – jaho

+1

@Marian不,它不會。上面的着色器中的「光亮」不太關於啞光和光澤。相反,點光源可以被散射並反射回相機的程度受到干擾。非常高的閃亮度值意味着表面非常光滑,因此只有少數集中像素將光線反射回相機。如果光澤度(平滑度)爲零,則表面完全粗糙,可以模擬從表面反射的光在所有方向(即右下圖像)均勻反射的材質。 – MooseBoys

+1

跑出人物...的確,如果您將亮度值從1連續變化到0,則會看到圖像從左下角圖像緩慢變化到右下角圖像。值0不是不連續性。 – MooseBoys

相關問題