首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试创建一个波浪着色器,我似乎不能计算法线来应用镜面照明到我的波浪

尝试创建一个波浪着色器,我似乎不能计算法线来应用镜面照明到我的波浪
EN

Stack Overflow用户
提问于 2017-03-01 04:29:13
回答 1查看 379关注 0票数 0

下面是我在顶点着色器中为wave编写的HLSL代码:

//为了正确计算矩阵,将位置向量更改为4个单位。input.position.w = 1.0f;

代码语言:javascript
复制
// 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:

代码语言:javascript
复制
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;
EN

回答 1

Stack Overflow用户

发布于 2017-03-01 05:01:52

这条线

代码语言:javascript
复制
n = cross(df_dx, df_dy);

看起来很奇怪-我相信cross期望3d向量作为参数,但df_dxdf_dy似乎是标量。

也许这里需要的向量是(1.f,0.f,df_dx)(0.f,1.f,df_dy)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42518136

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档