티스토리 뷰

#Standard (물리기반 쉐이더)

-물리기반 쉐이더 SurfaceOutputStandard 구조체 사용

 #pragma surface surf Standard
        #pragma target 3.0

        sampler2D _MainTex;
        sampler2D _BumpMap;
        sampler2D _Occlusion;
        float _Metallic;
        float _Smoothness;

        struct Input
        {
            float2 uv_MainTex;
            float2 uv_BumpMap;
        };

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
            o.Occlusion = tex2D(_Occlusion, IN.uv_MainTex);
            o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
            o.Albedo = c.rgb;
            o.Metallic = _Metallic;
            o.Smoothness = _Smoothness;
            o.Alpha = c.a;
        }

#Lambert

-SurfaceOutput 구조체 사용

Shader "Custom/Lambert"
{
    Properties
    {
        _MainTex("Albedo (RGB)", 2D) = "white" {}

    }
        SubShader
    {
        Tags { "RenderType" = "Opaque" }
        CGPROGRAM
        
        #pragma surface surf Lambert

        sampler2D _MainTex;

        struct Input
        {
            float2 uv_MainTex;
        };

        void surf(Input IN, inout SurfaceOutput o)
        {
            fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
            
            o.Albedo = c.rgb;
            o.Alpha = c.a;
        }
        ENDCG
    }
        FallBack "Diffuse"
}

#Blinn Phong

Shader "Custom/BlinnPhong"
{
    Properties
    {
        _MainTex("Albedo (RGB)", 2D) = "white" {}
        _SpecColor("spec color",Color) = (1,1,1,1) //반드시 변수명 고정, SubShader안에 변수 정의 X
        _SpecStrength("spec str",Range(0,1)) = 0
        _Gloss("gloss",Range(0,1)) = 0
    }
        SubShader
    {
        Tags { "RenderType" = "Opaque" }
        CGPROGRAM
        #pragma surface surf BlinnPhong
        sampler2D _MainTex;

        struct Input
        {
            float2 uv_MainTex;
        };

        void surf(Input IN, inout SurfaceOutput o)
        {
            fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
            o.Specular = 0.5; //스펙큘러의 크기
            o.Gloss = 1; //스펙큘러의 강도
            o.Albedo = c.rgb;
            o.Alpha = c.a;
        }
        ENDCG
    }
        FallBack "Diffuse"
}

#디지털 라이트

 *디렉셔널 라이트 -직진성을 가진 라이트

 *포인트 라이트 -사방으로 뻗어나가는 특성

 *스포트 라이트 -특정한 부분을 강조, 표현할때 사용

*커스텀 라이트만들기

        #pragma surface surf Test noambient //custom light, noambient-엠비언트 라이트 제거
float4 LightingTest(SurfaceOutput s, float3 lightDir, float atten)
        {
            return float4(1, 0, 0, 1);
        }

*Labert 라이트 연산

Shader "Custom/Test5"
{
    Properties
    {
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _BumpMap("NormalMap",2D) = "bump"{} //normal map 추가
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }

        CGPROGRAM
        #pragma surface surf Test noambient //custom light, noambient-엠비언트 라이트 제거

        sampler2D _MainTex;
        sampler2D _BumpMap;

        struct Input
        {
            float2 uv_MainTex;
            float2 uv_BumpMap;
        };


        void surf (Input IN, inout SurfaceOutput o)
        {
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
            o.Albedo = c.rgb;
            o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap)); //NormalMap
            o.Alpha = c.a;
        }
        float4 LightingTest(SurfaceOutput s, float3 lightDir, float atten)
        {
            float ndotl = saturate(dot(s.Normal, lightDir)); //saturate 0~1
            //return float4(1, 0, 0, 1);
            return ndotl;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

 

*normalmap추가, noambient

*Half-Lambert 라이트 연산 (기존 Lambert연산은 0~1로 떨어짐, 급격하게 변함으로 자연스럽지 못함)

Shader "Custom/Test5"
{
    Properties
    {
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _BumpMap("NormalMap",2D) = "bump"{} //normal map 추가
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }

        CGPROGRAM
        #pragma surface surf Test noambient //custom light, noambient-엠비언트 라이트 제거

        sampler2D _MainTex;
        sampler2D _BumpMap;

        struct Input
        {
            float2 uv_MainTex;
            float2 uv_BumpMap;
        };


        void surf (Input IN, inout SurfaceOutput o)
        {
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
            o.Albedo = c.rgb;
            o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap)); //NormalMap
            o.Alpha = c.a;
        }
        float4 LightingTest(SurfaceOutput s, float3 lightDir, float atten)
        {
            //float ndotl = saturate(dot(s.Normal, lightDir)); //saturate 0~1 , Lambert
            float ndotl = dot(s.Normal, lightDir) * 0.5 + 0.5; //half-lambert
            return ndotl;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

*Lambert 라이트 완성(조명 색상 _LightColor0.rgb, 강도, 빛 감쇠 atten, Albedo텍스쳐 추가 s.Albedo)

float ndotl = saturate(dot(s.Normal, lightDir));
            float4 final;
            final.rbg = ndotl * s.Albedo * _LightColor0.rgb * atten;
            final.a = s.Albedo;
            return final;

*Rim 라이트 - Fresnel(프레넬)공식

Shader "Custom/RimTest"
{
    Properties
    {
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }

        CGPROGRAM
        #pragma surface surf Lambert noambient

        sampler2D _MainTex;

        struct Input
        {
            float2 uv_MainTex;
        };

        void surf (Input IN, inout SurfaceOutput o)
        {
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
            o.Albedo = c.rgb;
            o.Alpha = c.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

 

*조명 영향을 받지않는 기본형(noambient)

*Rim Light 추가

Shader "Custom/RimTest"
{
    Properties
    {
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }

        CGPROGRAM
        #pragma surface surf Lambert noambient

        sampler2D _MainTex;

        struct Input
        {
            float2 uv_MainTex;
            float3 viewDir; //view vector 추가
        };

        void surf (Input IN, inout SurfaceOutput o)
        {
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
            o.Albedo = 0;

            //Rim Light
            float rim = dot(o.Normal, IN.viewDir);
            o.Emission = pow(1 - rim, 3);
            o.Alpha = c.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

*Rim Light에 색상과 넓이 조절

Shader "Custom/RimTest"
{
    Properties
    {
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _BumpMap("NormalMap",2D) = "bump"{}
        _RimColor("RimColor",Color) = (1,1,1,1)
        _RimPower("RimPower",Range(1,10))=3
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }

        CGPROGRAM
        #pragma surface surf Lambert

        sampler2D _MainTex;
        sampler2D _BumpMap;
        float4 _RimColor;
        float _RimPower;

        struct Input
        {
            float2 uv_MainTex;
            float2 uv_BumpMap;
            float3 viewDir; //view vector 추가
        };

        void surf (Input IN, inout SurfaceOutput o)
        {
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
            o.Albedo = c.rgb;
            o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));

            //color,power추가
            float rim = saturate(dot(o.Normal, IN.viewDir));
            o.Emission = pow(1 - rim, _RimPower) * _RimColor.rgb;

            //Rim Light
            //float rim = dot(o.Normal, IN.viewDir);
            //o.Emission = pow(1 - rim, 3);
            o.Alpha = c.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
글 보관함