게임 그래픽 프로그래밍

쉐이더2: 인터페이스 생성해 외부 입력 받기

101won 2024. 9. 2. 23:33

> 색상은 사칙 연산이 가능하다.
o.Emission = float3(0.5,0.5,0.5) + float3(0.5,0.5,0.5); // 흰색=회색+회색

 

>  float1은 모든 자리에 대응 계산된다.

o.Emission = float3(0,0,0) + 1; 

 

float4 test = float4(1,0,0,1);
o.Albedo = test.rgb; // test의 rgb값만 가져와 float3 타입으로 변환해 사용 가능

> 외부 입력 받아 사용
Properties: 인터페이스 만들기 - struct 외부: 변수 선언 - void surf : 사용

슬라이더 인터페이스

 

Shader "Custom/NewShader" // 이름
{ 
    Properties // 엔진에서 입력할 수 있는 부위
    {
       
       // _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _Glossiness ("Smoothness", Range(0,1)) = 0.5
        _Metallic ("Metallic", Range(0,1)) = 0.0

        /////////////////// 슬라이더 인터페이스 생성 ////////////////////////////////
        _Red("Red",Range(0,1)) =0
        _Green("Green",Range(0,1)) =0
        _Blue("Blue",Range(0,1)) =0

        _BrightDark("Bright&Dark",Range(-1,1)) =0 // -1로 완전 어둠도 가능한 밝기 조정
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM
        #pragma surface surf Standard fullforwardshadows noambient
        #pragma target 3.0

        sampler2D _MainTex;

        struct Input // 엔진에서 받아오는 데이터 구조체. 무조건 1개는 있어야 함
        {
            float2 uv_MainTex;
        };

        // 전역 변수
        half _Glossiness;
        half _Metallic;
        fixed4 _Color;

        /////////// 변수 선언 //////////////////////////
        float _Red;
        float _Green;
        float _Blue;

        float _BrightDark;

        void surf (Input IN, inout SurfaceOutputStandard o) // 색상, 이미지 출력부 
        {

            float4 test = float4(1,0,0,1);

            fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
          
            //////////// 사용 //////////////////////////////////
                o.Albedo = float3(_Red, _Green, _Blue)+_BrightDark;
                
                o.Alpha = c.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}