下面是我在顶点着色器中为wave编写的HLSL代码:
//为了正确计算矩阵,将位置向量更改为4个单位。input.position.w = 1.0f;
// Offset position based on sine wave
input.position.y = height * sin(input.position.x + time) * sin(input.position.y + time);
df_dx = height*cos(input.position.x + time)*sin(input.position.y + time); //tangent along x axis
df_dy = height*sin(input.position.x + time)*cos(input.position.y + time); //tangent along y axis
/*
k = height*(sin(input.position.x + time)*cos(input.position.y + time) - cos(input.position.x + time)*sin(input.position.y + time));
n = (-input.position.y*k, input.position.x*k, 0.0f);
*/
n = cross(df_dx, df_dy);
input.normal.x = n.x;
input.normal.y = n.y;
// Calculate the normal vector against the world matrix only.
output.normal = mul(input.normal, (float3x3)worldMatrix);
// Normalize the normal vector.
output.normal = normalize(output.normal);对于我的像素着色器的代码,我从顶点着色器和法线中传入了viewDirection:
float4 textureColour;
float3 lightDir;
float lightIntensity;
float4 colour;
float4 specular;
// Sample the pixel color from the texture using the sampler at this texture coordinate location.
textureColour = shaderTexture.Sample(SampleType, input.tex);
// Invert the light direction for calculations.
lightDir = -lightDirection;
// Set the default light value for all pixels
colour = (0.5f, 0.5f, 0.1f, 0.5f);
specular = (0.0f, 0.0f, 0.0f, 0.0f);
// Calculate the amount of light on this pixel.
lightIntensity = saturate(dot(input.normal, lightDir));
// Determine the final amount of diffuse color based on the diffuse color combined with the light intensity.
if (lightIntensity > 0.0f)
{
colour += (diffuseColour * lightIntensity);
// Saturate the ambient and diffuse Colour
colour = saturate(colour);
// Calculate the reflection vector based on the light intensity, normal vector and light direction.
reflection = normalize(2 * lightIntensity * input.normal - lightDir);
//reflection = reflect(-lightDir, input.normal);
// Determine the amount of specular light based on the reflection vector, viewing direction, and specular power
specular = pow(saturate(dot(reflection, input.viewDirection)), SpecularPower);
// Sum up Specular light
//finalSpec = SpecularColor * specular;
}
// Multiply the texture pixel and the final diffuse color to get the final pixel color result.
colour = colour * textureColour;
// Add the specular component last to the output colour
colour = saturate(colour + specular / 2);
return colour;发布于 2017-03-01 05:01:52
这条线
n = cross(df_dx, df_dy);看起来很奇怪-我相信cross期望3d向量作为参数,但df_dx和df_dy似乎是标量。
也许这里需要的向量是(1.f,0.f,df_dx)和(0.f,1.f,df_dy)
https://stackoverflow.com/questions/42518136
复制相似问题