스토리지

[5.3] Shader, Texture 받아오기 본문

Unity/Shader

[5.3] Shader, Texture 받아오기

ljw4104 2021. 5. 3. 13:20

NVIDIA의 CG를 이용

 

tex2D

Name tex2D - performs a texture lookup in a given 2D sampler and, in some cases, a shadow comparison. May also use pre computed derivatives if those are provided. Synopsis float4 tex2D(sampler2D samp, float2 s) float4 tex2D(sampler2D samp, float2 s, int te

developer.download.nvidia.com

*Texture 한장을 가져오는 Shader 코드

Shader "Custom/Text"
{
    Properties
    {
        _MainTex ("Albedo (RGB)", 2D) = "white" {}  //흰색 Texture, 중괄호에는 옵션이 들어감.
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200

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

        //위에서 2D 형식으로 선언시, sampler2D 형식으로 받을 수 있다.
        sampler2D _MainTex;

        struct Input
        {
            float2 uv_MainTex;      //해당 Texture의 UV좌표를 가져옴.
        };

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
            // Albedo comes from a texture tinted by color
            fixed4 c = tex2D(_MainTex, IN.uv_MainTex);          //(CG CODE) UV좌표의 색상정보를 가져오고 화면에 출력
            o.Albedo = c.rgb;
            o.Alpha = c.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

Texture가 잘 삽입됨 + /3 으로 나누어서 흑백처리함.

 

두 장의 이미지를 주어진 비율에 따라 섞는 코드

Shader "Custom/Text"
{
	Properties
	{
		_MainTex ("Albedo (RGB)", 2D) = "white" {}  //흰색 Texture, 중괄호에는 옵션이 들어감.
		_SubTex ("Albedo (RGB)", 2D) = "white" {}
		_Value ("Lerp Value", Range(0,1)) = 0
	}
		SubShader
	{
		Tags { "RenderType" = "Opaque" }
		LOD 200

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

		//위에서 2D 형식으로 선언시, sampler2D 형식으로 받을 수 있다.
		sampler2D _MainTex;
		sampler _SubTex;
		float _Value;

		struct Input
		{
			float2 uv_MainTex;      //해당 Texture의 UV좌표를 가져옴.
			float2 uv_SubTex;
		};

		void surf(Input IN, inout SurfaceOutputStandard o)
		{
			// Albedo comes from a texture tinted by color
			fixed4 m = tex2D(_MainTex, IN.uv_MainTex);          //(CG CODE) UV좌표의 색상정보를 가져오고 화면에 출력
			fixed4 s = tex2D(_SubTex, IN.uv_SubTex);
			//o.Albedo = (c.r + c.g + c.b) / 3;					//흑백이 됨 (RGB가 동일한 값이 나오기 때문)
			o.Albedo = lerp(m.rgb, s.rgb, _Value);					//두 장의 Texture을 반반씩 섞음
		}
		ENDCG
	}
		FallBack "Diffuse"
}

'Unity > Shader' 카테고리의 다른 글

[5.3] UV _Time  (0) 2021.05.03
[5.3] UV  (0) 2021.05.03
[5.3] Shader 연습 1  (0) 2021.05.03
[5.3] ShaderLab  (0) 2021.05.03
[5.3] Shader  (0) 2021.05.03
Comments