Unity3D - 使用GL.LINES创建的行有问题

时间:2014-01-11 09:04:38

标签: 3d unity3d shader

我必须在编辑器视图中绘制线条网格。此外,我需要网格在相机附近和远处溶解。所以我用SSE创建了一个着色器并将其应用到线上。当摄像机视图中有一些几何体时,一切正常。但是当场景中没有物体或者摄像机视线中没有物体变暗时。 怎么了? 还有更多!在某些相机位置线开始以大约2Hz的周期闪烁 我使用Unity 4.3.1f1,内置Intel HD4000显卡和最新驱动程序Windows 8。 也许延迟渲染可以帮助那里,但我没有Pro测试它。 enter image description here enter image description here

SSE着色器: enter image description here

Shader "GridShader"
{
    Properties 
    {
_Color("_Color", Color) = (1,1,1,1)
_DepthDividerExponent("_DepthDividerExponent", Float) = 10
_DepthDividerSin("_DepthDividerSin", Float) = 10
    }
    SubShader 
    {
        Tags
        {
"Queue"="Transparent"
"IgnoreProjector"="False"
"RenderType"="Transparent"
        }
Cull Back
ZWrite On
ZTest LEqual
ColorMask RGBA
Fog{
}
        CGPROGRAM
#pragma surface surf BlinnPhongEditor  alpha decal:blend vertex:vert
#pragma target 2.0
float4 _Color;
float _DepthDividerExponent;
float _DepthDividerSin;
sampler2D _CameraDepthTexture;
            struct EditorSurfaceOutput {
                half3 Albedo;
                half3 Normal;
                half3 Emission;
                half3 Gloss;
                half Specular;
                half Alpha;
                half4 Custom;
            };
            inline half4 LightingBlinnPhongEditor_PrePass (EditorSurfaceOutput s, half4 light)
            {
half3 spec = light.a * s.Gloss;
half4 c;
c.rgb = (s.Albedo * light.rgb + light.rgb * spec);
c.a = s.Alpha;
return c;
            }
            inline half4 LightingBlinnPhongEditor (EditorSurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
            {
                half3 h = normalize (lightDir + viewDir);
                half diff = max (0, dot ( lightDir, s.Normal ));
                float nh = max (0, dot (s.Normal, h));
                float spec = pow (nh, s.Specular*128.0);
                half4 res;
                res.rgb = _LightColor0.rgb * diff;
                res.w = spec * Luminance (_LightColor0.rgb);
                res *= atten * 2.0;
                return LightingBlinnPhongEditor_PrePass( s, res );
            }
            struct Input {
                float4 screenPos;
            };
            void vert (inout appdata_full v, out Input o) {
float4 VertexOutputMaster0_0_NoInput = float4(0,0,0,0);
float4 VertexOutputMaster0_1_NoInput = float4(0,0,0,0);
float4 VertexOutputMaster0_2_NoInput = float4(0,0,0,0);
float4 VertexOutputMaster0_3_NoInput = float4(0,0,0,0);
            }
            void surf (Input IN, inout EditorSurfaceOutput o) {
                o.Normal = float3(0.0,0.0,1.0);
                o.Alpha = 1.0;
                o.Albedo = 0.0;
                o.Emission = 0.0;
                o.Gloss = 0.0;
                o.Specular = 0.0;
                o.Custom = 0.0;
float4 ScreenDepthDiff0= LinearEyeDepth (tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(IN.screenPos)).r) - IN.screenPos.z;
float4 Add0=ScreenDepthDiff0 + float4( -1,-1,-1,-1 );
float4 Abs0=abs(Add0);
float4 Divide0=Abs0 / _DepthDividerSin.xxxx;
float4 Sin0=sin(Divide0);
float4 Divide2=Abs0 / _DepthDividerExponent.xxxx;
float4 Log1=log(Abs0);
float4 Pow0=pow(Divide2,Log1);
float4 Divide1=Sin0 / Pow0;
float4 Master0_1_NoInput = float4(0,0,1,1);
float4 Master0_2_NoInput = float4(0,0,0,0);
float4 Master0_3_NoInput = float4(0,0,0,0);
float4 Master0_4_NoInput = float4(0,0,0,0);
float4 Master0_7_NoInput = float4(0,0,0,0);
float4 Master0_6_NoInput = float4(1,1,1,1);
o.Albedo = _Color;
o.Alpha = Divide1;
                o.Normal = normalize(o.Normal);
            }
        ENDCG
    }
    Fallback "Diffuse"
}

绘图代码:

using UnityEngine;
using System.Collections.Generic;
public class LayersController : MonoBehaviour
{
    public Vector3[] origins;
    public Vector2 size;
    public int LayerCount;
    public List<Material> Materials;
    void DrawGrid(Vector3 origin, float step, Vector2 size, Material material)
    {
        // set the current material
        material.SetPass(0);
        GL.Begin(GL.LINES);
        //X axis lines
        for (float i = 0; i <= size.x; i += step)
        {
            GL.Vertex3(origin.x, origin.y, i + origin.z);
            GL.Vertex3(origin.x + size.x, origin.y, i + origin.z);
        }
        //Z axis lines
        for (float i = 0; i <= size.y; i += step)
        {
            GL.Vertex3(origin.x + i, origin.y, origin.z);
            GL.Vertex3(origin.x + i, origin.y, origin.z + size.y);
        }
        GL.End();
    }
    void OnPostRender()
    {
        for (var i = 0; i < origins.Length; i++)
        {
            DrawGrid(origins[i], 1, size, Materials[i]);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

除非您有理由进行网格照明,否则可以通过删除照明计算并直接应用颜色来修复调光问题:

inline half4 LightingBlinnPhongEditor_PrePass (EditorSurfaceOutput s, half4 light)
{
    half3 spec = light.a * s.Gloss;
    half4 c;
    c.rgb = _Color;  //replace (s.Albedo * light.rgb + light.rgb * spec);
    c.a = s.Alpha;
    return c;
}

闪烁可能是由于使用多个纹理渲染每个网格线。仔细检查只有与网格一样多的材料。没有看到你的完整实现就很难说,我无法复制闪烁。