Unite Europe 2016
A Crash Course to Writing Custom Unity Shaders!
[원본 강의] https://www.youtube.com/watch?v=3penhrrKCYg
[NVIDIA Cg 기능] http://http.developer.nvidia.com/Cg/clip.html
[유티니 셰이더 설명] https://docs.unity3d.com/Manual/SL-Reference.html
[유니티 내장 셰이더 다운로드] https://unity3d.com/kr/get-unity/download/archive
유니티에서는 기본적으로 ShaderLab으로 작성되어 있습니다.
ShaderLab이라는 블록 공간안에 CG(구현부)로 이루어진 블록이 중간에 있는 구조로 되어 있습니다.
다음은 오브잭트에 세이더를 적용시에 랜더링 되는 순서를 말합니다.
이제 기본적인 셰이더를 작성해보겠습니다.
셰이더 파일을 생성후 내용을 모두 삭제후에 다음과 같은 ShaderLab부분만을 작성합니다.
1 2 3 4 | Shader "Unlit/TestShader" { } | cs |
Unlit/TestShader 부분은 셰이더 선택의 경로 및 이름입니다.
해당 셰이더를 적용하면 핑크색(마젠타)색으로 지정되며, 오브잭트 또한 핑크색이 됩니다.
이제 셰이더의 프로퍼티 부분을 작성할 차례입니다.
1 2 3 4 5 6 7 8 9 10 | Shader "Unlit/TestShader" { // 설정된 데이터를 메테리얼에 적용합니다. Properties { //2D Texture and Color _MainTexture("Main Color(RGB) Hello" , 2D) = "white"{} //default Value _Color("Custom Color" , Color) = (1,1,1,1) } } | cs |
이제 인스펙터 창에서 텍스쳐와 색상을 설정하는 창이 생성되었음을 확인 할 수 있습니다.
다만, 아직까지는 커스텀한 셰이더가 어떠한 랜더링도 하지 않는 상태입니다.
다음은 기본적인 셰이더 작성입니다.
하나의 텍스쳐에 기본적인 색상을 적용합니다.
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | SShader "Unlit/TestShader" { Properties { _MainTexture("Main Color(RGB) Hello" , 2D) = "white"{} //default color _Color("Custom Color" , Color) = (1,1,1,1) } SubShader { // draw call (랜더링 단위) Pass { CGPROGRAM //open cg #pragma vertex vertexFunction #pragma fragment fragmentFunction #include "UnityCG.cginc" //Vertices //Normal //Color //uv struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; }; struct v2f { float4 positon : SV_POSITION; float2 uv : TEXCOORD0; }; float4 _Color; sampler2D _MainTexture; //Build Object v2f vertexFunction(appdata IN) { v2f OUT; OUT.positon = mul(UNITY_MATRIX_MVP , IN.vertex); OUT.uv = IN.uv; return OUT; } // Color In It fixed4 fragmentFunction(v2f IN) : SV_Target { fixed4 textureColor = tex2D(_MainTexture , IN.uv); return textureColor * _Color; } ENDCG // close CG } } } | cs |
이제 다른 효과를 주어보겠습니다.
조건부로 특정 픽셀을 출력전에 지우도록 하는 셰이더 입니다. (녹아내리는 효과)
(또는 다른 텍스쳐로 텍스쳐 변환 효과)
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | Shader "Unlit/TestShader" { Properties { _MainTexture("Main Color(RGB) Hello" , 2D) = "white"{} //default color _Color("Custom Color" , Color) = (1,1,1,1) //-- 추가 _DissolveTexture("Cheese" , 2D) = "white"{} _DissolveAmount("Cheese Cut Out Amount" , Range(0,1) ) = 1 //-- } SubShader { Pass { CGPROGRAM //open cg #pragma vertex vertexFunction #pragma fragment fragmentFunction #include "UnityCG.cginc" struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; }; struct v2f { float4 positon : SV_POSITION; float2 uv : TEXCOORD0; }; float4 _Color; sampler2D _MainTexture; //-- 추가 sampler2D _DissolveTexture; float _DissolveAmount; //-- //Build Object v2f vertexFunction(appdata IN) { v2f OUT; // mul - multiply a matrix by a column vector, // row vector by a matrix, or matrix by a matrix OUT.positon = mul(UNITY_MATRIX_MVP , IN.vertex); OUT.uv = IN.uv; return OUT; } // Color In It fixed4 fragmentFunction(v2f IN) : SV_Target { // 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. fixed4 textureColor = tex2D(_MainTexture , IN.uv); float4 dissolveColor = tex2D(_DissolveTexture ,IN.uv); // clip - conditionally kill a pixel before output clip(dissolveColor.rgb - _DissolveAmount); return textureColor * _Color; } ENDCG // close CG } } } | cs |
다음은 각기 버텍스를 밖으로 밀어내는 셰이더를 작성해보겠습니다.
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | Shader "Unlit/TestShader" { Properties { _MainTexture("Main Color(RGB) Hello" , 2D) = "white"{} //default color _Color("Custom Color" , Color) = (1,1,1,1) _DissolveTexture("Cheese" , 2D) = "white"{} _DissolveAmount("Cheese Cut Out Amount" , Range(0,1) ) = 1 _ExtrudeAmount("Extrude Amount" , Range(0,1)) = 1 } SubShader { Pass { CGPROGRAM //open cg #pragma vertex vertexFunction #pragma fragment fragmentFunction #include "UnityCG.cginc" struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; float3 normal : NORMAL; }; struct v2f { float4 positon : SV_POSITION; float2 uv : TEXCOORD0; }; float4 _Color; sampler2D _MainTexture; sampler2D _DissolveTexture; float _DissolveAmount; float _ExtrudeAmount; //Build Object v2f vertexFunction(appdata IN) { v2f OUT; // Extrude. IN.vertex.xyz += IN.normal.xyz * _ExtrudeAmount; // mul - multiply a matrix by a column vector, // row vector by a matrix, or matrix by a matrix OUT.positon = mul(UNITY_MATRIX_MVP , IN.vertex); OUT.uv = IN.uv; return OUT; } // Color In It fixed4 fragmentFunction(v2f IN) : SV_Target { //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. fixed4 textureColor = tex2D(_MainTexture , IN.uv); float4 dissolveColor = tex2D(_DissolveTexture ,IN.uv); // clip - conditionally kill a pixel before output clip(dissolveColor.rgb - _DissolveAmount); return textureColor * _Color; } ENDCG // close CG } } } | cs |
동영상의 강의는 여기까지이고 코드 구현부분은 그렇다고 해도 아직 사용하는 함수들이나
정의들에 대해서 공부가 더 필요할 것 같습니다.
해당 코드가 모바일에서도 돌아가는지도 확인이 필요할것 같습니다..
'엔진 > Unity' 카테고리의 다른 글
[UNITY] 오브잭트 풀링 - OBJECT POOLING (0) | 2016.09.29 |
---|---|
[UNITY] 캐릭터 회전 - Character Rotation (0) | 2016.09.29 |
[UNITY]]이벤트 함수 호출 순서 (0) | 2016.09.28 |