-
[유니티 쉐이더 스타트업] 색상을 입력받아 출력해보기 - 1프로그래밍/Unity Shader 2019. 5. 13. 02:37
https://zerodeg.tistory.com/35
[Unity Shader] 쉐이더에서 변수는 어떻게 이용할까?
https://zerodeg.tistory.com/34 [Unity Shader] 색상의 사칙연산을 해보기 data-ad-unit = "DAN-tocxr50w70wz" data-ad-width = "728" data-ad-height = "90"> data-ad-unit = "DAN-tocxr50w70wz" data-ad-width..
zerodeg.tistory.com
오늘은 저번시간에 이어서 색상을 유니티에서 입력받아 출력해보는 방법에 대해 알아보려고 합니다. 아직 쉐이더에서 변수를 어떻게 사용하는지 모르시는 분이 계시다면 위 게시물을 보시면 도움이 될거라고 생각합니다.
https://zerodeg.tistory.com/30?category=812191
[Unity Shader] Shader Properties 제작해보기
Unity Ver : 2019.1.0.f2 Personal Shader "Custom/NewSurfaceShader" { Properties { _Color ("Color", Color) = (1,1,1,1) _MainTex ("Albedo (RGB)", 2D) = "white" {} _Glossiness ("Smoothness", Range(0,1)..
zerodeg.tistory.com
먼저 Properties에서 컬러를 입력받는 인터페이스를 만들어 볼건데요. 인터페이스를 받는 부분은 위 링크에서 확인할 수 있으니 모르시는 분은 참고하시길 바라고, Color를 입력받는 TestColor 인터페이스를 아래와 같이 추가하겠습니다.
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 _TestColor("TestColor", Color) = (1, 1, 1, 1) }
_TestColor를 자세히 보면 (1, 1, 1, 1)로 되어있는 것을 볼 수 있는데요. float4 형식으로 되어 있다는 점을 알 수 있습니다. 이 점을 확인했으니 float4 자료형의 변수를 하나 선언해보겠습니다. 이 변수를 이용하여 유니티에서 색상을 입력받아 출력할 예정입니다. 변수이름은 _TestColor이고 변수의 위치는 CGPROGRAM ~ ENDCG 사이의 함수가 아닌 공백입니다. 여기서 주의하셔야할 점은 그 변수를 사용하는 위치보다 변수의 선언부가 상단에 있어야 합니다. (오류가 나지 않지만 절차지향적인 특성으로 인해 좋지 않습니다.)
그리고 surf 함수에서 Albedo를 _TestColor.rgb로 초기화 해주시면 모든 작업이 끝이납니다. 저번 포스팅을 보신분은 알겠지만 rgb는 각각 red green blue를 나타내고 개별적으로 사용이 가능하다고 말씀드렸는데요. float4로 선언을 하고도 rgb만을 사용한 것은 Albedo에서 float4의 alpha 값이 그다지 필요가 없기 때문이라고 합니다. 코드는 아래와 같습니다.
CGPROGRAM // Physically based Standard lighting model, and enable shadows on all light types #pragma surface surf Standard fullforwardshadows noambient // Use shader model 3.0 target, to get nicer looking lighting #pragma target 3.0 sampler2D _MainTex; struct Input { float2 uv_MainTex; }; half _Glossiness; half _Metallic; fixed4 _Color; float4 _TestColor; // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader. // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing. // #pragma instancing_options assumeuniformscaling UNITY_INSTANCING_BUFFER_START(Props) // put more per-instance properties here UNITY_INSTANCING_BUFFER_END(Props) void surf (Input IN, inout SurfaceOutputStandard o) { // Albedo comes from a texture tinted by color fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color; o.Albedo = _TestColor.rgb; //Metallic and smoothness come from slider variables o.Metallic = _Metallic; o.Smoothness = _Glossiness; o.Alpha = c.a; } ENDCG
유니티를 확인해보면 위 이미지와 같이 TestColor를 변경할 수 있습니다. 색상을 변경하고 x버튼을 누르고 씬뷰를 확인해보면 아래와 같이 색상이 바뀌는 것을 확인할 수가 있습니다.
'프로그래밍 > Unity Shader' 카테고리의 다른 글
[유니티 쉐이더 스타트업] 쉐이더 코드를 깔끔하게 정리하는법 (0) 2019.05.19 [유니티 쉐이더 스타트업] 색상을 입력 받아 출력해보기 - 2 (0) 2019.05.18 [유니티 쉐이더 스타트업] 쉐이더에서 변수는 어떻게 이용할까? (0) 2019.05.11 [유니티 쉐이더 스타트업] 색상의 사칙연산을 해보기 (0) 2019.05.10 [유니티 쉐이더 스타트업] 색상을 출력해보기 (0) 2019.05.03