将表面着色器转换为片段着色器

时间:2016-03-03 15:05:04

标签: unity3d shader fragment-shader vertex-shader unity5

在将表面着色器转换为片段着色器时寻求帮助。最终将纹理渲染为Unity3D Rendertexture。谢谢!

表面着色器输出噪声,基于https://scrawkblog.com/2013/05/18/gpu-gems-to-unity-improved-perlin-noise/

这是我想要转换的表面着色器:

Shader "Noise/Diffuse3D"
{
Properties
{
    _MainTex ("Base (RGB)", 2D) = "white" {}
    _Color("Color", Color) = (1,1,1,1)
}
SubShader
{
    Tags { "RenderType"="Opaque" }
    LOD 200

    CGPROGRAM
    #pragma surface surf Lambert vertex:vert
    #pragma target 3.0
    #pragma glsl
    #include "ImprovedPerlinNoise3D.cginc"

    sampler2D _MainTex;
    fixed4 _Color;

    struct Input
    {
        float2 uv_MainTex;
        float3 noiseUV;
    };

    void vert(inout appdata_full v, out Input o)
    {
        UNITY_INITIALIZE_OUTPUT(Input,o);
        //o.noiseUV = v.vertex.xyz; //use model space, not world space for noise uvs
                                  // v.vertex is the input vertex in the vertex shader
        //float3 worldPos = mul(_Object2World, v.vertex).xyz;
        o.noiseUV = mul(_Object2World, v.vertex).xyz;

    }

    void surf(Input IN, inout SurfaceOutput o)
    {
        //uncomment this for fractal noise
        //float n = fBm(IN.noiseUV, 4);

        //uncomment this for turbulent noise
        float n = turbulence(IN.noiseUV, 4);

        //uncomment this for ridged multi fractal
        //float n = ridgedmf(IN.noiseUV, 4, 1.0);

        o.Albedo = _Color.rgb * n;
        o.Alpha = _Color.a;
    }
    ENDCG
}
FallBack "Diffuse"
}

1 个答案:

答案 0 :(得分:0)

来自reddit的回答:

 Pass
{
    CGPROGRAM
    #pragma vertex vert
    #pragma fragment frag
    #pragma target 3.0
    #pragma glsl
    #include "ImprovedPerlinNoise3D.cginc"
    #pragma multi_compile_fog

    #include "UnityCG.cginc"

    struct appdata
    {
        float4 vertex : POSITION;
    };

    struct v2f
    {
        float3 noiseUV : TEXCOORD0;
        UNITY_FOG_COORDS(1)
        float4 vertex : SV_POSITION;
    };

    fixed4 _Color;
    v2f vert (appdata v)
    {
        v2f o;
        UNITY_INITIALIZE_OUTPUT(v2f,o);
        o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
        o.noiseUV = mul(_Object2World, v.vertex).xyz;
        UNITY_TRANSFER_FOG(o,o.vertex);
        return o;
    }

    fixed4 frag (v2f i) : SV_Target
    {

        //uncomment this for fractal noise
        //float n = fBm(i.noiseUV, 4);

        //uncomment this for turbulent noise
        float n = turbulence(i.noiseUV, 4);

        //uncomment this for ridged multi fractal
        //float n = ridgedmf(i.noiseUV, 4, 1.0);
        fixed4 col = _Color;
        col.rgb*=n;
        UNITY_APPLY_FOG(i.fogCoord, col);
        return col;
    }
    ENDCG
}
}
FallBack "Diffuse"