首页 > 解决方案 > 仅在特定高度显示着色器

问题描述

我按照草教程https://www.youtube.com/watch?v=cHe7N42zE1k进行操作,最后我将着色器拖放到网格上并显示草。我希望草只在某个高度开始显示。

地形图

在上图中,您可以看到通过这种方法将草置于水下。那么你能根据高度决定从哪里开始着色器吗?

标签: c#unity3dshader

解决方案


在几何着色器(Grass.shader)中,它一直在发射刀片:

    for (int i = 0; i < BLADE_SEGMENTS; i++)
    {
        float t = i / (float)BLADE_SEGMENTS;

        float segmentHeight = height * t;
        float segmentWidth = width * (1 - t);
        float segmentForward = pow(t, _BladeCurve) * forward;

        // Select the facing-only transformation matrix for the root of the blade.
        float3x3 transformMatrix = i == 0 ? transformationMatrixFacing : transformationMatrix;

        triStream.Append(GenerateGrassVertex(pos, segmentWidth, segmentHeight, segmentForward, float2(0, t), transformMatrix));
        triStream.Append(GenerateGrassVertex(pos, -segmentWidth, segmentHeight, segmentForward, float2(1, t), transformMatrix));
    }

    // Add the final vertex as the tip.
    triStream.Append(GenerateGrassVertex(pos, 0, height, forward, float2(0.5, 1), transformationMatrix));

您可以在此处添加一个条件来告诉您的 GS 在位置低于某个阈值时不要发出。

if (pos.y > minHeightThreshold)
{
    for (int i = 0; i < BLADE_SEGMENTS; i++)
    {
        float t = i / (float)BLADE_SEGMENTS;

        float segmentHeight = height * t;
        float segmentWidth = width * (1 - t);
        float segmentForward = pow(t, _BladeCurve) * forward;

        // Select the facing-only transformation matrix for the root of the blade.
        float3x3 transformMatrix = i == 0 ? transformationMatrixFacing : transformationMatrix;

        triStream.Append(GenerateGrassVertex(pos, segmentWidth, segmentHeight, segmentForward, float2(0, t), transformMatrix));
        triStream.Append(GenerateGrassVertex(pos, -segmentWidth, segmentHeight, segmentForward, float2(1, t), transformMatrix));
    }

    // Add the final vertex as the tip.
    triStream.Append(GenerateGrassVertex(pos, 0, height, forward, float2(0.5, 1), transformationMatrix));
}

推荐阅读