Unity/그래픽스프로그래밍

210715_ 그래픽스 프로그래밍(복습 및 시험)

minquu 2021. 7. 15. 11:18
반응형

먼저 필요한 어셋들을 불러와준다.

 

 

두개 와  스카이 박스도

https://assetstore.unity.com/packages/2d/textures-materials/sky/free-hdr-sky-61217

 

Free HDR Sky | 2D 하늘 | Unity Asset Store

Elevate your workflow with the Free HDR Sky asset from ProAssets. Browse more 2D 텍스처 및 소재 on the Unity Asset Store.

assetstore.unity.com

 

 

디퓨즈 Warping 할 이미지도 유니티에 넣어준다.

 

 

이거 사용할것

 

----

 

먼저 불 부터 만들 것이다.

 

유니티에 메터리얼, 세이더 폴더 하나 씩 만들어주기

 

파이어 매터리얼, 파이어 세이더(Standard Surface Shader) 만들기 

 

매터리얼에 적용시켜주기

 

쿼드 하나만들어서 

Fire 매터리얼 적용시켜주기

 

쉐이더 열어서 텍스처 한 장받는 걸로 만들기 

 

 

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

        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Standard 

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        sampler2D _MainTex;

        struct Input
        {
            float2 uv_MainTex;
        };

 

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

 

albedo  필요 없으니

이미션을 넣어줄것

 

 

----

 

 

 

매터리얼에 이미지 넣기

 

 

렌더타입과 큐를 트랜스퍼런트로 바꿔주고

 

prgma 에 alpha:fade를 추가해준다.

 

 

UV 움직임을 사용할 이미지를 하나 더 받아준다.

 

넣어준다.

 

이제 나중에 넣은 이미지의 UV를 움직이게 해준다.

 

 

            fixed4 d = tex2D (_MainTex2, float2(IN.uv_MainTex2.x, IN.uv_MainTex2.y - _Time.y));

 

불기동 이미지가 uv가 움직이게 해준다.

 

 

이건 테스트를 해본것,

 

이제 

 

노이즈 맵을 저 불기둥처럼 움직이게 해볼 것이다.

 

c의 불 모양의 uv 를 d의 r값 (움직임 채널값)을 넣어줘서 움직이게한다.

 

Shader "Custom/Fire"
{
    Properties
    {
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _MainTex2 ("Albedo (RGB)", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Transparent" "Queue" = "Transparent"}
        LOD 200

        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Standard alpha:fade

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        sampler2D _MainTex;
        sampler2D _MainTex2;

        struct Input
        {
            float2 uv_MainTex;
            float2 uv_MainTex2;
        };

 

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
            fixed4 d = tex2D (_MainTex2, float2(IN.uv_MainTex2.x, IN.uv_MainTex2.y - _Time.y));
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex + d.r);
            o.Emission = c.rgb;
            o.Alpha = c.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

 

 

 

 

위에 살짝 걸쳐지는데

 

c의 uv를 조금 올려준다.

 

Shader "Custom/Fire"
{
    Properties
    {
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _MainTex2 ("Albedo (RGB)", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Transparent" "Queue" = "Transparent"}
        LOD 200

        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Standard alpha:fade

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        sampler2D _MainTex;
        sampler2D _MainTex2;

        struct Input
        {
            float2 uv_MainTex;
            float2 uv_MainTex2;
        };

 

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
            fixed4 d = tex2D (_MainTex2, float2(IN.uv_MainTex2.x, IN.uv_MainTex2.y - _Time.y));
            fixed4 c = tex2D (_MainTex, float2(IN.uv_MainTex.x, IN.uv_MainTex.y - 0.09) + d.r);
            o.Emission = c.rgb;
            o.Alpha = c.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

 

 

 

-----

상담 받는 것 떄문에

2pass 는 영상 참고 또는 내 블로그 참고하기

 

 

 

-----

 

스카이 박스 설정

 

유니티 어셋 스토어 또는 파일질라에서

좀비 가져와서 머리만 남겨두고 

노멀맵이랑 스카이 박스 있는지 확인해준다.

 

zombie 메터리얼과 쉐이더를 만들어준다.

 

 

텍스처 한장을 받고 램버트로 해준다.

 

큐브맵을 받는 코드를 넣어준다

 

er "Custom/zombie"
{
    Properties
    {
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _Cube("Cubemap", Cube) = "" {}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }

        CGPROGRAM
        #pragma surface surf Lambert noambient

        sampler2D _MainTex;
        samplerCUBE _Cube;
        struct Input
        {
            float2 uv_MainTex;
            float3 worldRefl;
        };

        void surf (Input IN, inout SurfaceOutput o)
        {
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
            float4 re = texCUBE (_Cube, IN.worldRefl);


            o.Albedo = 0;
            o.Emission = re.rgb;
            o.Alpha = c.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

 

스카이박스를 넣어준다.

 

일단 리플렉션이 생긴다.

 

그다음은 노멀맵을 넣어준다.

 

 

ader "Custom/zombie"
{
    Properties
    {
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _Cube("Cubemap", Cube) = "" {}
        _Normal ("Normal (RGB)", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }

        CGPROGRAM
        #pragma surface surf Lambert noambient

        sampler2D _MainTex;
        samplerCUBE _Cube;
        sampler2D _Normal;

        struct Input
        {
            float2 uv_MainTex;
            float3 worldRefl;
            float2 uv_Normal;
            INTERNAL_DATA
        };

        void surf (Input IN, inout SurfaceOutput o)
        {
            o.Normal = UnpackNormal(tex2D(_Normal, IN.uv_Normal));
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
            float4 re = texCUBE (_Cube, WorldReflectionVector(IN, o.Normal));


            o.Albedo = 0;
            o.Emission = re.rgb;
            o.Alpha = c.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

 

 

노멀맵을 넣어준다.

 

 

----

diffuse warping

 

 

 

다시 헤드만 가져오기

 

램버트로 텍스처 한장을 받는 좀비 램프 세이더 만들기

 

만들고 램프 텍스쳐까지 받는 걸 만든다.

 

 

Shader "Custom/zobie_ramp"
{
    Properties
    {

        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _RampTex ("RampTex (RGB)", 2D) = "white" {}

    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM

        #pragma surface surf _Warp noambient


        sampler2D _MainTex;
        sampler2D _RampTex;

        struct Input
        {
            float2 uv_MainTex;
            float2 uv__RampTex;
        };

        void surf (Input IN, inout SurfaceOutput o)
        {
  
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
            o.Albedo = c.rgb;
            o.Alpha = c.a;
        }
        float4 Lighting_Warp(SurfaceOutput s,float3 lightDir, float atten){
            float ndotl = dot(s.Normal, lightDir) * 0.5+ 0.5;
            float4 ramp = tex2D(_RampTex, 1 - float2(ndotl, 0.5));

            return ramp;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

 

ㅋㅋ 나중에 이미지 돌려야할듯...

 

 

이제 노멀맵을 넣어보자

 

 

Shader "Custom/zobie_ramp"
{
    Properties
    {

        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _RampTex ("RampTex (RGB)", 2D) = "white" {}
        _Normal ("Normal (RGB)", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM

        #pragma surface surf _Warp noambient


        sampler2D _MainTex;
        sampler2D _RampTex;
        sampler2D _Normal;

        struct Input
        {
            float2 uv_MainTex;
            float2 uv_RampTex;
            float2 uv_Normal;
        };

        void surf (Input IN, inout SurfaceOutput o)
        {
  
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
            o.Normal = UnpackNormal(tex2D(_Normal, IN.uv_Normal));
            o.Albedo = c.rgb;
            o.Alpha = c.a;
        }
        float4 Lighting_Warp(SurfaceOutput s,float3 lightDir, float atten){
            float ndotl = dot(s.Normal, lightDir) * 0.5+ 0.5;
            float4 ramp = tex2D(_RampTex, 1 - float2(ndotl, 0.5));
            
            return ramp;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

 

노말맵 추가하기

 

알베도맵도 추가한다.

 

 

 

final 하나 뚫어서 위에서 받은 알베도와 곱해준다.

 

Shader "Custom/zobie_ramp"
{
    Properties
    {

        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _RampTex ("RampTex (RGB)", 2D) = "white" {}
        _Normal ("Normal (RGB)", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM

        #pragma surface surf _Warp noambient


        sampler2D _MainTex;
        sampler2D _RampTex;
        sampler2D _Normal;

        struct Input
        {
            float2 uv_MainTex;
            float2 uv_RampTex;
            float2 uv_Normal;
        };

        void surf (Input IN, inout SurfaceOutput o)
        {
  
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
            o.Normal = UnpackNormal(tex2D(_Normal, IN.uv_Normal));
            o.Albedo = c.rgb;
            o.Alpha = c.a;
        }
        float4 Lighting_Warp(SurfaceOutput s,float3 lightDir, float atten){
            float ndotl = dot(s.Normal, lightDir) * 0.5+ 0.5;
            float4 ramp = tex2D(_RampTex, 1 - float2(ndotl, 0.5));
            float4 final;
            final.rgb = s.Albedo * ramp.rgb;
            final.a = s.Alpha;

            return final;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

 

완료

 

반응형