서피스셰이더프로그램 셰이더개발을쉽게!
유니티렌더링시스템소개 렌더링패스 셰이더랩 서피스셰이더 데모 2
유니티렌더링시스템 3
Deferred Lighting Rendering Path Dual Lightmapping Post Effect Processing Realtime Shadow LightProbe Directional Lightmapping HDR Gamma Space DirectX11 support with Nvidia ( 개발중 ) 4
3.0 렌더링기능 디퍼드라이팅렌더링패스 라이트매핑솔루션탑재 실시간그림자 듀얼라이트매핑 5
6
7
8
3.5 렌더링기능 9
10
11
디렉셔널 라이트맵 스페큘러 하이라이트 노멀맵 12
13
렌더링패스 14
(Rendering Path) Vertex Lit Forward Rendering Deferred Lighting 15
Vertex Lit Rendering Path 정점에서만빛의계산을수행 빠르다. 하드웨어호환성이높다 VertexLit Forward Rendering Per-Pixel 에관련된기능은사용불가능 그림자 노멀맵 쿠키 높은디테일의스페큘러하이라이트 VertexLit Forward Rendering 16
Forward Rendering Path Important Per Pixel Rendering Not Important Vertex Lit Spherical Harmonics ForwardBase Pass ForwardAdd Pass Directional Light Vertex Lit Spherical Harmonics Per Pixel Light 17
Forward Pass Renderer All Important All Auto Per Pixel Count : 4 All Not Important Per Pixel Count : 1 18
Deferred Lighting Rendering Path Forward Rendering All Important Deferred Lighting All Important Deferred Lighting All Not Important 19
Deferred Lighting Process PrePassBase PrePassLighting PrePassFinal ARGB32 렌더텍스쳐 Z Buffer ARGB32 렌더텍스쳐 라이팅모델 사용자함수최종결과 20
Deferred Rendering Path Scene View 21
Deferred Rendering Path - Step1 PrePassBase G Buffer ViewSpace Normal Depth Buffer 22
Deferred Rendering Path - Step2 PrePassLighting LightBuffer +SpotLight +PointLight 23
Deferred Rendering Path - Final PrePassFinal Screenshots from Interstellar Marines http://interstellarmarines.com 24
셰이더랩 25
HLSL DirectX GLSL OpenGL GLSL OpenGL/ES HLSL DirectX Cg OpenGL Cg ShaderLab 26
셰이더파일 Shader "MyShader" { Properties { _MyTexture ("My Texture", 2D) = "white" { } } SubShader { } } 유니티에디터 유니티플레이어 Compile Platform Build Platform Shader 27
프로퍼티서브셰이더태그렌더큐설정등.. 패스 렌더스테이트지정 Culling Depth Alpha Blending 셰이더함수 Vertex Function Fragment Function 28
속성 (Properties ) 에디터와셰이더의연결 Properties { _Color ("Main Color", Color) = (1,1,1,1) _Shininess ("Shininess", Range (0.03, 1)) = 0.078125 _MainTex ("Base (RGB) Gloss (A)", 2D) = "white" {} _ShadeRange("Shading Range",Float) = 0.02... } 다양한컨트롤 29
(SubShader) SubShader { // } SubShader { // } FallBack "VertexLit" SubShader ( Main ) SubShader ( Sub ) FallBack 30
(Tag) Overlay Transparent 4000 Geometry 3000 Background 2000 1000 Tags {"Queue" = "Transparent+1" } or material.renderqueue = 3001; 31
(Pass) 설정 Name Tag 렌더스테이트 Culling Depth Alpha Blending 셰이더코드 Fixed Function Program Vertex and Fragment Program Surface Shader Program 32
Fixed Function Program 33
Fixed Function Program 예시 Shader "Fixed Function Shader" { Properties { _Color ("Main Color", Color) = (1,1,1,0) _SpecColor ("Spec Color", Color) = (1,1,1,1) _Emission ("Emmisive Color", Color) = (0,0,0,0) _Shininess ("Shininess", Range (0.01, 1)) = 0.7 _MainTex ("Base (RGB)", 2D) = "white" {} _BlendTex ("Alpha Blended (RGBA) ", 2D) = "white" {} } SubShader { Pass { Material { Diffuse [_Color] Ambient [_Color] Shininess [_Shininess] Specular [_SpecColor] Emission [_Emission] } Lighting On SeparateSpecular On SetTexture [_MainTex] { combine previous * texture } } } } SetTexture [_BlendTex] { combine previous lerp (texture) texture } 34
Vertex / Fragment Program 35
Vertex/Fragment Shader 예시 GLSL 프로그램 CG 프로그램 Shader "GLSL shader" { SubShader { Pass { GLSLPROGRAM #ifdef VERTEX void main() { gl_position = gl_modelviewprojectionmatrix * gl_vertex; } #endif #ifdef FRAGMENT void main() { gl_fragcolor = vec4(1.0, 0.0, 0.0, 1.0); } #endif ENDGLSL } } } Shader "Cg shader" { SubShader { Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag float4 vert(float4 invert : POSITION) : SV_POSITION { float4 outvert = mul (UNITY_MATRIX_MVP, invert); return outvert; } } } } half4 frag(float4 i) : COLOR { return half4(1.0, 0.0, 0.0, 1.0); } ENDCG 36
서피스셰이더프로그램 37
Platform D3D9 OpenGL OpenGL ES Flash Keyword VERTEXLIGHT SHADOW LIGHTING HDR COOKIE LIGHTMAP Diffuse Shader #ifdef LIGHTMAP_OFF c = LightingLambert (o, IN.lightDir, atten); #endif // LIGHTMAP_OFF #ifdef LIGHTMAP_OFF c.rgb += o.albedo * IN.vlight; #endif // LIGHTMAP_OFF #ifndef LIGHTMAP_OFF 38
Fragment Logic Editor Setting ex) Lightmap Surface Lighting Auto Generation Fragment Function 39
정점셰이더 프래그먼트 ( 픽셀 ) 셰이더 Vertex Function Surface Function Lighting Function Lightmap Composition FinalColor Function 최종결과 Position Normal Color Tangent Diffuse Normal Emission Gloss Specular Alpha Clip Lighting Color 40
서피스셰이더프로그램 버텍스프래그먼트프로그램 CGPROGRAM #pragma surface surf Lambert sampler2d _MainTex; struct Input { float2 uv_maintex; }; void surf (Input IN, inout SurfaceOutput o) { half4 c = tex2d (_MainTex, IN.uv_MainTex); o.albedo = c.rgb; o.alpha = c.a; } ENDCG v2f_surf vert_surf (appdata_full v) { v2f_surf o; o.pos = mul (UNITY_MATRIX_MVP, v.vertex); o.pack0.xy = TRANSFORM_TEX(v.texcoord, _MainTex); #ifndef LIGHTMAP_OFF o.lmap.xy = v.texcoord1.xy * unity_lightmapst.xy + unity_lightmapst.zw; #endif float3 worldn = mul((float3x3)_object2world, SCALED_NORMAL); #ifdef LIGHTMAP_OFF o.normal = worldn; #endif #ifdef LIGHTMAP_OFF float3 shlight = ShadeSH9 (float4(worldn,1.0)); o.vlight = shlight; #ifdef VERTEXLIGHT_ON... 15 Lines 132 Lines 41
Chicken Shader Bundle 42
Fixed Function Program Vertex Fragment Program Surface Shader Program Vertex Lit Rendering Path O Forward Rendering Path O O Deferred Lighting Rendering Path O 43
44
45
데모 46
개발자들을위한팁 #pragma debug 47
Q&A 48