You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity基于Kinect深度数据构建地形:世界空间渐变着色器显示异常求助

Troubleshooting Your World Space Terrain Shader Fade Issue

Hey there, I’ve run into a similar precision-related problem with world space shaders in Unity before, so let’s break down why your gradient disappears when zooming out and how to fix it.

The Root Cause: World Space Floating Point Precision

When you zoom out the Scene view camera, the world space coordinates of your terrain vertices start to have tiny relative differences (since the camera is far away, the absolute coordinate values are large, and floating-point numbers lose precision with large values). Your gradient calculation relies on these small differences (like height variations), so once the precision drops too much, the shader can’t distinguish between different heights—resulting in a flat color.

Fix 1: Switch to Object Space for Gradient Calculation

Instead of using world space positions, use object space coordinates (relative to your terrain object’s origin). These values stay small and consistent regardless of how far the camera is, so your gradient will work at any zoom level.

Here’s how to modify your shader:

  1. Add properties to define your gradient range (match these to your terrain’s actual min/max height):
    Properties {
        _Color("Basic Color", Color) = (1,1,1,1)
        _BottomColor("Bottom Gradient Color", Color) = (0,0.5,0,1)
        _TopColor("Top Gradient Color", Color) = (1,1,0,1)
        _MinHeight("Minimum Terrain Height", Float) = 0.0
        _MaxHeight("Maximum Terrain Height", Float) = 10.0
    }
    
  2. Adjust the vertex shader to pass object space positions instead of world space:
    struct appdata {
        float4 vertex : POSITION;
    };
    
    struct v2f {
        float4 pos : SV_POSITION;
        float3 objectPos : TEXCOORD0;
    };
    
    v2f vert (appdata v) {
        v2f o;
        o.pos = UnityObjectToClipPos(v.vertex);
        o.objectPos = v.vertex.xyz; // Object space position is just the raw vertex data
        return o;
    }
    
  3. Calculate the gradient using object space height in the fragment shader:
    fixed4 frag (v2f i) : SV_Target {
        // Normalize the object space height to a 0-1 range
        float heightNormalized = saturate((i.objectPos.y - _MinHeight) / (_MaxHeight - _MinHeight));
        // Lerp between your gradient colors
        fixed4 gradient = lerp(_BottomColor, _TopColor, heightNormalized);
        // Multiply with your base color
        return gradient * _Color;
    }
    

Fix 2: Check Terrain LOD Settings

If your terrain uses LOD (Level of Detail) groups, make sure all LOD levels are assigned your custom shader. Sometimes when zooming out, Unity switches to a lower LOD that uses a default flat shader, which would make your gradient disappear.

  • Select your terrain object in the Hierarchy
  • Go to the Terrain component’s Settings tab
  • Scroll down to LOD Settings and verify that all LOD tiers use your custom shader

Fix 3: Disable Scene View Fast Rendering

Unity’s Scene view has a "Fast Mode" that simplifies rendering for performance, which can sometimes strip out shader effects like gradients. Try turning it off:

  • In the Scene view’s top-right corner, click the dropdown menu (next to the "2D" toggle)
  • Uncheck Fast Mode

Final Checks

  • Double-check that _MinHeight and _MaxHeight are set to values that match your terrain’s actual height range (you can get this from the Terrain component’s Heightmap settings)
  • Test the shader in both Scene view and Game view to ensure consistency

内容的提问来源于stack exchange,提问作者Jonathon.lau

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 04:16:59