Unity Shader - zzragida/UnityDocs GitHub Wiki

Reference

Tools

What is Shader?

  • ์ปดํ“จํ„ฐ์—๊ฒŒ ์–ด๋–ป๊ฒŒ ํŠน๋ณ„ํ•˜๊ฑฐ๋‚˜ ์œ ๋‹ˆํฌํ•˜๊ฒŒ ์‚ฌ๋ฌผ(something)์„ ๊ทธ๋ฆด์ง€ ์•Œ๋ ค(tell)์ฃผ๋Š” ํ”„๋กœ๊ทธ๋žจ
  • A definition in layman's terms might be given as "a program that tells a computer how to draw something in a specific and unique way". from Wikipedia

์œ ๋‹ˆํ‹ฐ ์‰์ด๋” ๊ตฌ์กฐ(Unity Shader Structure)

์œ ๋‹ˆํ‹ฐ ๋ฉ€ํ‹ฐํ”Œ๋žซํผ ์•„ํ‚คํ…์ณ(Unity Multi Platform Architecture)

  • ์œ ๋‹ˆํ‹ฐ๋Š” ๋ฉ€ํ‹ฐํ”Œ๋žซํผ ์—”์ง„์ด๊ธฐ ๋•Œ๋ฌธ์— CPU, GPU ๋ชจ๋‘ ๋ฉ€ํ‹ฐ ํ”Œ๋žซํผ์—์„œ ๋™์ž‘ํ•˜๊ธฐ ์œ„ํ•œ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Œ
  • ์‰์ด๋”์˜ ๊ฒฝ์šฐ ShaderLab์ด๋ผ๋Š” ์ถ”์ƒ ์Šคํฌ๋ฆฝํŠธ ์–ธ์–ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฉ€ํ‹ฐ ํ”Œ๋žซํผ์—์„œ ๋™์ž‘ํ•˜๋Š” ์‰์ด๋” ์ฝ”๋“œ๋ฅผ ์ž‘์—…ํ•  ์ˆ˜ ์žˆ์Œ

์‰์ด๋” ์Šคํฌ๋ฆฝํŠธ Shader Script(ShaderLab)

  • Abstract shader script layer for multiplatform system

  • Multi-Device

    • Supports old mobile hardwares (DX 7/8)
    • Supports modern mobile hardwares (DX9, Shader Model 2)
    • Supports PC and Console hardwares (Shader Model 3)
    • Supports Extreme PC hardwares (DX11, Shader Model 5)
  • Multi-Library

    • OpenGL
    • OpenGL E/S
    • DirectX
  • Multi-Language

    • GLSL
    • Cg
    • HLSL
  • ๊ธฐ๋ณธ ๊ตฌ์กฐ

Shader "Custom/Simple" {
    Properties
    {
        _MainTex ("Base (RGB)", 2D) = "white" {}
    }
 
    SubShader
    {
        Tags { "Queue" = "Geometry + 1" }
        Pass
        {
            SetTexture [_MainTex] { combine texture }
        }
    }
 
    FallBack "Diffuse"
}
  • Shader Lab Syntax - Properties

  • ์œ ๋‹ˆํ‹ฐ ์ธ์ŠคํŽ™ํ„ฐ์—์„œ ์‰์ด๋”์— ์ „๋‹ฌํ•  ๋‹ค์–‘ํ•œ ๊ฐ’์„ ์‚ฌ์šฉ์ž๊ฐ€ ํŽธ๋ฆฌํ•˜๊ฒŒ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋งŒ๋“  ์ธํ„ฐํŽ˜์ด์Šค

    • Float textbox
    • Float Range Slides
    • Vector4 textboxes
    • Color Picker
    • 2D Texture (POT)
    • 2D Texture (NPOT)
    • Cube Texture
  • Shader Lab Syntax - SubShader

  • ์œ ๋‹ˆํ‹ฐ๋Š” ํ•˜๋‚˜์˜ ์‰์ด๋”์—์„œ ๋‹ค์–‘ํ•œ ํ•˜๋“œ์›จ์–ด๋ฅผ ์ง€์›ํ•˜๊ธฐ ์œ„ํ•ด ์„œ๋ธŒ์‰์ด๋”๋ผ๋Š” ๊ตฌ๋ฌธ์„ ์ œ๊ณตํ•จ

  • ๊ฐ€์žฅ ์œ„์— ์ •์˜๋œ ์‰์ด๋”๋ถ€ํ„ฐ ์‹คํ–‰๋˜๋ฉฐ, ํ•˜๋“œ์›จ์–ด์™€ ํ˜ธํ™˜์ด ์•ˆ๋˜๋ฉด ๋‹ค์Œ ์„œ๋ธŒ์‰์ด๋”๊ฐ€ ์‹คํ–‰๋จ

  • ๋ชจ๋“  ์‰์ด๋” ๊ตฌ๋™์— ์‹คํŒจํ•˜๋ฉด ํด๋ฐฑ(FallBack)์— ์ •์˜๋œ ์‰์ด๋”๊ฐ€ ๋Œ€์‹  ์‹คํ–‰๋จ

SubShader
{
    // First Priority Shader Code
    ( Designed for high-level PC )
}
 
SubShader
{
    // Second Priority Shader Code
    ( Designed for mid-level PC )
}
 
SubShader
{
    // Third Priority Shader Code
    ( Designed for low-level PC )
}
 
// Alternative Shader Code
FallBack "VertextLit"
  • SubShader Format - Tag

  • ์„œ๋ธŒ์‰์ด๋”์˜ ๊ตฌ์กฐ๋ฅผ ํฌ๊ฒŒ ๋ Œ๋”๋ง์˜ ์‹คํ–‰ ์ˆœ์„œ์™€ ์„ค์ •์„ ์ง€์ •ํ•˜๋Š” ํƒœ๊ทธ(Tag) ๊ตฌ๋ฌธ๊ณผ ๋ Œ๋”๋ง์„ ํ‘œํ˜„ํ•˜๋Š” ํŒจ์Šค(Pass)๋กœ ๋‚˜๋ˆ„์–ด ์ง

  • ์œ ๋‹ˆํ‹ฐ๋Š” 0๋ถ€ํ„ฐ 5000๋ฒˆ๊นŒ์ง€ ๋ Œ๋”๋ง ์ˆœ์„œ๋ฅผ ์ง€์ •ํ•˜๊ฒŒ ๋˜๋Š”๋ฐ ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ์ด๋ฆ„์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์ด ์ˆœ์„œ๋ฅผ ์ง€์ •ํ•˜๊ฒŒ ๋จ

    • Background = 1000
    • Geometry = 2000
    • AlphaTest = 2450
    • Transparent = 3000
    • Overlay = 4000
    • ์˜ˆ๋ฅผ๋“ค์–ด Tags { "Queue" = "Geometry+1" }์€ Geometry(2000)์— 1์„ ๋”ํ•œ 2001์„ ์˜๋ฏธํ•จ
  • SubShader Format - Pass

  • ์ง€์˜ค๋ฉ”ํŠธ๋ฆฌ(Geometry)์˜ ๋ Œ๋”๋ง ๋ฐฉ๋ฒ•์„ ์ง€์ •ํ•˜๋Š” ๊ตฌ๋ฌธ

  • ํŒจ์Šค ๊ตฌ๋ฌธ์€ ์—ฌ๋Ÿฌ๊ฐœ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด์ „ ํŒจ์Šค์—์„œ ๋‚˜์˜จ ๊ฒฐ๊ณผ๋ฌผ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ณ„์† ์ถ”๊ฐ€์ ์ธ ํ‘œํ˜„์„ ํ•  ์ˆ˜ ์žˆ์Œ

  • ํŒจ์Šค ๊ตฌ๋ฌธ์„ ํ†ตํ•ด ๋ฌผ์ฒด์˜ ๋ Œ๋”๋ง์ด ์ˆ˜ํ–‰๋˜๋Š”๋ฐ, ์ด ๋ Œ๋”๋ง์€ ์—ฌ๋Ÿฌ๋ถ„๋“ค์ด ์นด๋ฉ”๋ผ์—์„œ ์ง€์ •ํ•œ ๋ Œ๋”๋ง ๋ฐฉ๋ฒ•์„ ํ†ตํ•ด์„œ ์ˆ˜ํ–‰๋จ

  • ์ด๋Ÿฌํ•œ ๋ Œ๋”๋ง ๋ฐฉ๋ฒ•์„ ์œ ๋‹ˆํ‹ฐ์—์„œ๋Š” ์ด๋ฅผ ๋ Œ๋”๋ง ํŒจ์Šค(Rendering Path)๋ผ๊ณ  ํ•จ

    • Pass Tag : ๋ Œ๋”๋ง ์„ค์ •์„ ์ง€์ •
    • Render State : GPU์˜ ๋ Œ๋” ์Šคํ…Œ์ดํŠธ๋ฅผ ์„ค์ •
    • ์‹ค์ œ ์‰์ด๋” ์ฝ”๋“œ ์กฐ๊ฐ

์œ ๋‹ˆํ‹ฐ ๋ Œ๋”๋ง ํŒŒ์ดํ”„๋ผ์ธ(Unity Rendering Pipeline)

๋ฒ„ํ…์Šค ๋ฆฟ(Vertex Lit)

  • ์˜ค๋ž˜๋œ ํ•˜๋“œ์›จ์–ด์—์„œ ๋™์ž‘ํ•˜๋„๋ก ์„ค๊ณ„๋จ(Designed for old hardware)
  • Direct X 7/8 compatible
  • Final pixel is calculated by interpolation between vertices
  • ์žฅ์ 
    • Simple and Fast
    • No overhead in CPU side
    • Useful to lightmapping in mobile
  • ๋‹จ์ 
    • Cookie, Lightprobe, Shadow are not available
    • Number of light has limit
    • Can't express pre-pixel expression. ex) Normal mapping
  • ๋ฒ„ํ…์Šค ๋ฆฟ์€ ํ•˜๋“œ์›จ์–ด ํ˜น์€ ์—๋””ํ„ฐ ์„ค์ •์— ๋”ฐ๋ผ 3๊ฐ€์ง€ ์ค‘ ํ•˜๋‚˜์˜ ๊ณผ์ •์ด ์ง„ํ–‰๋จ
  • ์–ด๋–ค ๊ณผ์ •์ด ์‚ฌ์šฉ๋ ์ง€๋Š” ์œ ๋‹ˆํ‹ฐ๊ฐ€ ์ž๋™์œผ๋กœ ์•Œ์•„์„œ ์„ค์ •ํ•ด ์คŒ
    • Vertex Pass
    • Just simple vertex lit mode
    • No lightmap support
    • VertexLM Pass
    • For old smartphone
    • Encodes lightmap by Double LDR
    • VertexLMRGBM Pass
    • For modern smartphone
    • Encodes lightmap by RGBM

ํฌ์›Œ๋“œ(Forward)

  • ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์ธ ๋ Œ๋”๋ง ๋ฐฉ๋ฒ•(Common rendering pipeline)
  • DirectX 9
  • Per-pixel calculation
  • ์žฅ์ 
    • Lightprobe, cookie, shadow are available
    • Can render object by independent lighting model
  • ๋‹จ์ 
    • Lighting computation increases linearly by number of lights
    • Only directional light support shadow
    • Unity supports additional shader keyword (fullforwardshadows)
  • ํฌ์›Œ๋“œ ๋ฐฉ์‹์˜ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ์ ์€ ์ค‘์š”ํ•œ ๋น›๊ณผ ์ค‘์š”ํ•˜์ง€ ์•Š์€ ๋น›์„ ๊ตฌ๋ถ„ํ•˜๋Š” ์ผ์ž„
  • ์ด๋Š” ๋ผ์ดํŠธ ์ปดํฌ๋„ŒํŠธ ์„ค์ •์—์„œ ์ง์ ‘ ์ง€์ •์„ ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ค‘์š”ํ•œ ๋น›์€ ๊ฐ€์žฅ ์ธํ…์‹œํ‹ฐ(Intensity)๊ฐ€ ๋†’์€ ๋””๋ ‰์…”๋„ ๋ผ์ดํŠธ(Directional Light) ์ž„
  • ๊ทธ๋กœ๊ธฐ ๋ผ์ดํŠธ ์ปดํฌ๋„ŒํŠธ์—์„œ Render Mode๊ฐ€ Important๋กœ ์„ค์ •๋œ ๋น›์ž„
  • ์ค‘์š”ํ•œ ๋น›์€ ํ’ˆ์งˆ์„ค์ •์—์„œ Pixel Light Count์— ์„ค์ •๋œ ์ˆ˜๋งŒํผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋‚˜๋จธ์ง€๋Š” ์•ˆ ์ค‘์š”ํ•œ ๋น›์ด ๋จ
  • ํฌ์›Œ๋“œ ๋ฐฉ์‹์€ ๋น›์˜ ๊ฐœ์ˆ˜์— ๋”ฐ๋ผ ๊ณ„์‚ฐ๋Ÿ‰์ด ์ฆ๊ฐ€ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์ด๋ฅผ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์€ ์ตœ์ ํ™”์— ํฐ ์˜ํ–ฅ์„ ์คŒ
  • ํ’ˆ์งˆ ์„ค์ •์˜ ํ”ฝ์…€ ๋ผ์ดํŠธ ์นด์šดํŠธ(Pixel Light Count) ์„ค์ •์ด ์ค‘์š”ํ•œ ๋น›์˜ ์ˆ˜๋ฅผ ์ง€์ •ํ•˜๋Š” ์ผ์ธ๋ฐ, ์œ ๋‹ˆํ‹ฐ๋Š” ์ค‘์š”ํ•œ ๋น›์€ ๊ณต๋“ค์—ฌ์„œ ๋ Œ๋”๋งํ•˜๊ณ , ์•ˆ ์ค‘์š”ํ•œ ๋น›์€ ์ •๋ง ๋Œ€์ถฉ ๋ Œ๋”๋ง ํ•จ
    • Light Setting in Forward Pipeline
    • Important Light
    • The largest intensity directional light
    • Uses in per-pixel calculation
    • Pixel Light Count
    • Not Important Light
    • Next 4 lights use for per-vertex calculation
    • Rest lights are approximated by SH(Spherical Harmonics)

๋””ํผ๋“œ ๋ Œ๋”๋ง(Deferred Lighting)

  • ์ตœ์‹  ๋ Œ๋”๋ง ๋ฐฉ๋ฒ•(Modern rendering pipeline)
  • ์žฅ์ 
    • No number of light limit
    • Useful to complex scene rendering at once
  • ๋‹จ์ 
    • Mobile doesn't support this currently
    • Supports only Blinn-Phong lighting model
    • Doesn't support transparent object
    • Only forward supports transparent objects

์œ ๋‹ˆํ‹ฐ ์‰์ด๋” ํ”„๋กœ๊ทธ๋ž˜๋ฐ(Unity Shader Programming)

๊ณ ์ • ํ•จ์ˆ˜ ํ”„๋กœ๊ทธ๋žจ(Fixed Function Program)

  • ๋ฒ„ํ…์Šค ๋ฆฟ ๋ Œ๋”๋ง ๋ฐฉ์‹์— ์ ํ•ฉํ•œ ํ”„๋กœ๊ทธ๋žจ ๋ฐฉ๋ฒ•(Mataches vertex lit rendering pipeline)
    • Shader model 1.x
    • Provides special keywords for rendering operation
  • ๊ณ ๊ธ‰ ์–ธ์–ด๋ฅผ ํ†ตํ•ด์„œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ด ๋ถˆ๊ฐ€๋Šฅ ํ•จ
  • ์‹ฌํ”Œํ•˜์ง€๋งŒ ๋น ๋ฅด์ง€ ๋•Œ๋ฌธ์—, ๋ชจ๋ฐ”์ผ์—์„œ๋Š” ํ€„๋ฆฌํ‹ฐ์—๋งŒ ๋ฌธ์ œ๊ฐ€ ์—†๋‹ค๋ฉด ์„ฑ๋Šฅ์„ ์œ„ํ•ด ์‚ฌ์šฉ์„ ๊ณ ๋ คํ•ด๋ณด๋Š” ๊ฒƒ๋„ ์ข‹์Œ

๋ฒ„ํ…์Šค/ํ”„๋ž˜๊ทธ๋จผํŠธ ํ”„๋กœ๊ทธ๋žจ(Vertex / Fragment Program)

  • ์ผ๋ฐ˜์ ์œผ๋กœ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ์‰์ด๋” ํ”„๋กœ๊ทธ๋ž˜๋ฐ(Command shader programming)
    • Unity provides keywords for coding snippet area
    • CGPROGRAM, GLSLPROGRAM
  • ์œ ๋‹ˆํ‹ฐ๋Š” ๋‘ ๊ฐ€์ง€ ์–ธ์–ด๋ฅผ ๋ชจ๋‘ ์ง€์›ํ•˜์ง€๋งŒ, ๋ฉ€ํ‹ฐํ”Œ๋žซํผ ์‰์ด๋”๋ฅผ ๋งŒ๋“ค๋ ค๋„ ํ•œ๋‹ค๋ฉด Cg๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•จ
  • ์‰์ด๋”๋žฉ ๋ฌธ๋ฒ•์œผ๋กœ ์‰์ด๋” ์ฝ”๋“œ๋ฅผ Cg๋กœ ๋งŒ๋“ค์–ด์ฃผ๋ฉด, ์ตœ์ข… HLSL ์‰์ด๋” ์ฝ”๋“œ๊ฐ€ ๋งŒ๋“ค์–ด์ด์ œ ๋˜๊ณ , ๋‚ด๋ถ€ ๋ณ€ํ™˜๊ธฐ์— ์˜ํ•ด์„œ HLSL์ฝ”๋“œ๋ฅผ GLSL๋กœ ์ž๋™์œผ๋กœ ๋ณ€ํ™˜๋จ
  • ์ด๋ ‡๊ฒŒ ๋ณ€ํ™˜๋œ GLSL ์ฝ”๋“œ๋Š” ์œ ๋‹ˆํ‹ฐ GLSL ์˜ตํ‹ฐ๋งˆ์ด์ €๋ฅผ ํ†ตํ•ด ์ตœ์ ํ™”๋˜์–ด ์ตœ์ข… ์‰์ด๋” ์ฝ”๋“œ๊ฐ€ ์™„์„ฑ๋จ
  • ๋”ฐ๋ผ์„œ Cg๋กœ ์ง  ์‰์ด๋” ์ฝ”๋“œ๊ฐ€ OpenGL์—์„œ ๋™์ž‘ํ•˜๋Š”๊ฒŒ ๊ฐ€๋Šฅํ•ด ์ง

์„œํ”ผ์Šค ์‰์ด๋” ํ”„๋กœ๊ทธ๋žจ(Surface Shader Program)

  • ๋ณต์žกํ•œ ์‰์ด๋” ์ฝ”๋“œ๋ฅผ ์‹ฌํ”Œํ•˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•œ ํ”„๋กœ๊ทธ๋žจ
    • Simplified version of vertex/fragment program by unit way.
    • Automatically converts vertex/fragment code for various situations.
  • ์„œํ”ผ์Šค ์‰์ด๋”๊ฐ€ ๋‹จ์ˆœํ•œ ์ด์œ ๋Š” ์—”์ง„์—์„œ ์ž๋™์œผ๋กœ ๋ฒ„ํ…์Šค/ํ”„๋ž˜๊ทธ๋จผํŠธ ์‰์ด๋” ์ฝ”๋“œ๋กœ ๋ณ€ํ™˜ํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์ž„
  • Cg๋‚˜ HLSL ์‰์ด๋” ์ฝ”๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋ฉด ๋จ
    • Surface Shader Program => Generate by Unity Engine => Vertex/Fragment Program
  • ๊ธฐ๋ณธ๊ตฌ์กฐ
    • Surface Shader Program = Surface Code + Light Code

์„œํ”ผ์Šค ์‰์ด๋” ์ž‘์„ฑ(Writing Surface Shaders)

์ž‘์—…์ˆœ์„œ(Work Flow)

  1. Create > Shader > Standard Surface Shader / Unlit Shader / Image Effect Shader / Compute Shader
  2. Write code in surf function
  3. Attach material

์„œํ”ผ์Šค ์ถœ๋ ฅ ๊ตฌ์กฐ(Surface Output Structure)

SurfaceOutput (<= Unity4)
SurfaceOutputStandard or SurfaceOutputStandardSpecular (>= Unity5)
Surface Output
// SurfaceOutput (>= Unity4)
struct SurfaceOutput
{
    fixed3 Albedo;  // diffuse color
    fixed3 Normal;  // tangent space normal, if written
    fixed3 Emission;
    half Specular;  // specular power in 0..1 range
    fixed Gloss;    // specular intensity
    fixed Alpha;    // alpha for transparencies
};
 
// SurfaceOutputStandard (>= Unity5)
struct SurfaceOutputStandard
{
    fixed3 Albedo;      // base (diffuse or specular) color
    fixed3 Normal;      // tangent space normal, if written
    half3 Emission;
    half Metallic;      // 0=non-metal, 1=metal
    half Smoothness;    // 0=rough, 1=smooth
    half Occlusion;     // occlusion (default 1)
    fixed Alpha;        // alpha for transparencies
};
 
// SurfaceOutputStandardSpecular (>= Unity5)
struct SurfaceOutputStandardSpecular
{
    fixed3 Albedo;      // diffuse color
    fixed3 Specular;    // specular color
    fixed3 Normal;      // tangent space normal, if written
    half3 Emission;
    half Smoothness;    // 0=rough, 1=smooth
    half Occlusion;     // occlusion (default 1)
    fixed Alpha;        // alpha for transparencies
};

์„œํ”ผ์Šค ์‰์ด๋” ์ปดํŒŒ์ผ ๋ฐฉ๋ฒ•์ง€์ •(Surface Shader compile directives)

  • Surface shader is placed inside CGPROGRAM ~~ ENDCG block
  • It must be placed inside SubShader block, not inside Pass. Surface shader will compile into multiple passes itself
  • It uses #pragma surface ... directive to indicate it's a surface shader
  • #progma surface surfaceFunction lightModel [optional|params]

ํ•„์ˆ˜ ํŒŒ๋ผ๋ฏธํ„ฐ(Required parameters)

  • surfaceFunction
  • lightModel

์˜ต์…˜ ํŒŒ๋ผ๋ฏธํ„ฐ(Optional parameters)

  • Transparency and alpha testing

    • alpha or alpha:auto
    • alpha:fade
    • alpha:premul
    • alphatest:VariableName
    • keepalpha
    • decal:add
    • decal:blend
  • Custom modifier functions

    • vertex:VertexFunction
    • finalcolor:ColorFunction
  • Shadows and Tessellation

    • addshadow
    • fullforwardshadows
    • tessellation:TessFunction
  • Code generation options

    • exclude_path:deferred, exclude_path:forward, exclude_path:prepass
    • noshadow
    • noambient
    • novertexlights
    • nolightmap
    • nodynlightmap
    • nodirlightmap
    • nofog
    • nometa
    • noforwardadd
  • Miscellaneous options

    • softvegetation
    • interpolateview
    • halfsview
    • approxview
    • dualforward

์„œํ”ผ์Šค ์‰์ด๋” ์ž…๋ ฅ ๊ตฌ์กฐ(Surface Shader input structure)

  • float3 viewDir
  • float4 with COLOR semantic
  • float4 screenPos
  • float3 worldPos
  • float3 worldRefl
  • float3 worldNormal
  • float3 worldRefl; INTERNAL_DATA
  • float3 worldNormal; INTERNAL_DATA

Surface Shader Examples

๋จธํ…Œ๋ฆฌ์–ผ๊ณผ ์‰์ด๋”(Material and Shader)

Blue Car : Car Texture + Custom/Bodywork Shader + Blue car Material Red Car : Car Texture + Custom/Bodywork Shader + Red car Material Wheel : Car Texture + Standard Shader + Wheel Material

์Šคํƒ ๋‹ค๋“œ ์‰์ด๋”(Standard Shader)

  • ์œ ๋‹ˆํ‹ฐ5์—์„œ๋Š” ์Šคํƒ ๋‹ค๋“œ ์‰์ด๋”๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š” ์ƒˆ๋กœ์šด ํƒ€์ž…์˜ ๋‚ด์žฅ ์‰์ด๋”๋ฅผ ๋„์ž…ํ•จ(Unity 5 introduces a new type of built-in shader called the Standard Shader.)
  • ์Šคํƒ ๋‹ค๋“œ ์‰์ด๋”์—๋Š” ๋ฌผ๋ฆฌ์  ๊ธฐ๋ฐ˜ ์‰์ด๋”๋ผ๋Š” ํ›จ์”ฌ ๋” ๊ณ ๊ธ‰์Šค๋Ÿฌ์šด ์กฐ๋ช… ๋ชจ๋ธ์„ ํ†ตํ•ฉํ•จ(The Standard Shader also incorporates a much more advanced lighting model called PBS(Physically Based Shading).)
  • ๋ฌผ๋ฆฌ๊ธฐ๋ฐ˜ ์‰์ด๋”ฉ(Physically Based Shading via PBS)
    • Energy conservation
    • HDR(High Dynamic Range)

์ปจํ…์ŠคํŠธ์™€ ์ปจํ…ํŠธ(The Context and the Content)

  • When thinking about lighting in Unity 5, it is handy to divide concepts into what we call the Context, which is information on the lighting that exists in the scene (coming from the skybox, lights and shadows, GI, etc) and the Content, which is usually authored directly by you and describes what is in the environment (materials, geometries, animation, etc).
  • Most times the distinction will be obvious, but not always.
  • For instance, a skybox is graphical Content, that can control some of the lighting Context of your scene.

์ปจํ…์ŠคํŠธ(The Context)

The Default Lighting Context Skyboxes Global Illumination ์ปจํ…ํŠธ(The Content) The Material Editor

๋ฉ”ํƒˆ๋ฆญ vs ์ŠคํŽ™ํ˜๋Ÿฌ ์›Œํฌํ”Œ๋กœ์šฐ(Metalic vs Specular Workflow)

  • ์Šคํƒ ๋‹ค๋“œ ์‰์ด๋”๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฉ”ํ„ฐ๋ฆฌ์–ผ์„ ์ƒ์„ฑํ•  ๋•Œ Standard์™€ Standard (Specular Setup) ๋‘๊ฐ€์ง€์ค‘์—์„œ ์ ์ ˆํ•œ๊ฒƒ์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋‹ค(When creating a material using the Standard shader you will have the choice of using one of two flavours, โ€œStandardโ€ and โ€œStandard (Specular setup)โ€.)

  • Standard: The shader exposes a โ€œmetallicโ€ value that states whether the material is metallic or not. In the case of a metallic material, the Albedo color will control the color of your specular reflection and most light will be reflected as specular reflections. Non metallic materials will have specular reflections that are the same color as the incoming light and will barely reflect when looking at the surface face-on.

  • Standard (Specular setup): Choose this shader for the classic approach. A Specular color is used to control the color and strength of specular reflections in the material. This makes it possible to have a specular reflection of a different color than the diffuse reflection for instance.

๋จธํ…Œ๋ฆฌ์–ผ ํŒŒ๋ผ๋ฏธํ„ฐ(Material Parameters)

๋จธํ…Œ๋ฆฌ์–ผ ์ฐจํŠธ(Material Charts)

๋ฉ”ํƒˆ๋ฆญ ์„ค์ •์„ ์œ„ํ•œ ์ฐจํŠธ(Reference Chart for Metallic settings)

  • Standard ์‰์ด๋”์˜ Metallic

์ŠคํŽ™ํ˜๋Ÿฌ ์„ค์ •์„ ์œ„ํ•œ ์ฐจํŠธ(Reference Chart for Specular settings)

  • Standard (Specular setup) ์‰์ด๋”์˜ Specular

์Šคํฌ๋ฆฝํŠธ๋ฅผ ํ†ตํ•ด์„œ ๋จธํ…Œ๋ฆฌ์–ผ ์†์„ฑ์ •๋ณด๋ฅผ ์ ‘๊ทผํ•˜๊ฑฐ๋‚˜ ์ˆ˜์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•(Accessing and Modifying Material parameters vis script)

๋นŒํŠธ์ธ ์‰์ด๋”(Built-in Shaders)

  • http://egloos.zum.com/chulin28ho/v/5526644

  • Mobile / Background ์‰์ด๋”

    • ๋ผ์ดํŒ… ์˜ํ–ฅ๋„ ๋ฐ›์ง€ ์•Š๊ณ  ๋‹จ์ง€ ํ…์Šค์ณ๋งŒ ์ถœ๋ ฅํ•˜๋Š” ์ดˆ ๊ฐ„๋‹จํ•œ ์‰์ด๋”์ž…๋‹ˆ๋‹ค. ZWrite๋„ ํ•˜์ง€ ์•Š์Œ
    • ์ฆ‰ ์ด๊ฒƒ์€ ๋ง๊ทธ๋Œ€๋กœ ์›๊ฑฐ๋ฆฌ ๋ฐฐ๊ฒฝ์šฉ ์‰์ด๋”.
    • ์Šค์นด์ด๋ฐ•์Šค๋Š” ์•„๋‹ˆ๊ณ  ๋ฉ€๋ฆฌ ๋ณด์ด๋Š” ์›๊ฒฝ ํ•œ ์žฅ์งœ๋ฆฌ ์ด๋ฏธ์ง€ ๊ฐ™์€ ๊ฒฝ์šฐ์— ์“ธ ์ˆ˜ ์žˆ๋Š” ์‰์ด๋”์ž…๋‹ˆ๋‹ค. 0.5g
  • Mobile / Bumped Specular

    • ๋ณด์ด๋Š” ๊ทธ๋Œ€๋กœ, ๋ชจ๋ฐ”์ผ์šฉ์œผ๋กœ ๋งŒ๋“  ์ŠคํŽ˜ํ˜๋Ÿฌ๊ฐ€ ํฌํ•จ๋œ ๋…ธ๋ง๋งต ์‚ฌ์šฉ ์‰์ด๋”์ž…๋‹ˆ๋‹ค.
    • ์ข€ ๋” ๊ฐ„๋žตํ™” ๋œ ํŠน์ง•์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”๋ฐ,
      • ๋ฉ”์ธ ์นผ๋ผ๋‚˜ ์ŠคํŽ˜ํ˜๋Ÿฌ ์นผ๋ผ๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
      • ์ŠคํŽ˜ํ˜๋Ÿฌ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ผ์ดํŠธ ๋ฐฉํ–ฅ์€ ๋ฒ„ํ…์Šค์—์„œ ๋Œ€์ถฉ ๋ฐ›์•„์„œ ์˜ต๋‹ˆ๋‹ค.
      • ๋…ธ๋ง๋งต์˜ ํƒ€์ผ๋ง์ด๋‚˜ ์˜ต์…‹์€ ๋ฒ ์ด์Šค ํ…์Šค์ณ์˜ ์„ค์ •๊ฐ’์„ ๋”ฐ๋ฆ…๋‹ˆ๋‹ค.
      • ๋””ํผ๋“œ ๋ผ์ดํŒ…์„ ์ง€์›ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋ผ์ดํŠธ๋งต๋„ ์ง€์›ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
      • ๋””๋ ‰์…”๋„ ๋ผ์ดํŠธ 1๊ฐœ๋งŒ ์ง€์›๋˜๊ณ , ๋‚˜๋จธ์ง€ ๋ผ์ดํŠธ๋“ค์€ ๋ฒ„ํ…์Šค ๋ผ์ดํŠธ๋‚˜ ๊ตฌ๋ฉด์กฐํ™” ๋ผ์ดํŠธ๋กœ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค.
    • ์ผ๋ฐ˜์ ์ธ ๋ฒ”ํ”„/์ŠคํŽ˜ํ˜๋Ÿฌ๋ณด๋‹ค๋Š” ์ข€ ๊ฐ€๋ณ๊ฒŒ ๋˜์–ด ์žˆ๊ธด ํ•ฉ๋‹ˆ๋‹ค๋งŒ, ๊ทธ๋ž˜๋„ ๋…ธ๋ง๋งต ์—ฐ์‚ฐ์ด ๋˜๋ฏ€๋กœ ๋ชจ๋ฐ”์ผ์—์„œ ํ•จ๋ถ€๋กœ ์‚ฌ์šฉํ•ด์„œ๋Š” ๊ณค๋ž€ํ•ฉ๋‹ˆ๋‹ค. 3g
  • Mobile / Bumped Specular (1 Directional Light) ์œ„์˜ ๋ฒ”ํ”„ ์ŠคํŽ˜ํ˜๋Ÿฌ ์‰์ด๋”์™€ ๋™์ผํ•˜์ง€๋งŒ, ๋‹จ ํ•œ๊ฐ€์ง€๋งŒ ๋‹ค๋ฆ…๋‹ˆ๋‹ค.

  • ๋””๋ ‰์…”๋„ ๋ผ์ดํŠธ 1๊ฐœ๋งŒ ์ง€์›๋˜๊ณ , ๋‚˜๋จธ์ง€ ๋ผ์ดํŠธ๋Š” ๋ฌด์‹œ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰ ์œ„์˜ ์‰์ด๋”๋ณด๋‹ค ์•„์ฃผ ์•ฝ๊ฐ„ ๊ฐ€๋ณ๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 2.7g
  • Mobile / Particles / Additive ์ด์ „์— ์‚ฌ์šฉํ•˜๋˜ ์ผ๋ฐ˜์ ์ธ Add ์‰์ด๋”์™€ ๊ฑฐ์˜ ๊ฐ™์œผ๋ฉฐ, ๋‹ค๋ฅธ์ ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.
  • Tint Color๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.
  • ์Šค๋ฌด์Šค ํŒŒํ‹ฐํด ์˜ต์…˜์ด ์—†์Šต๋‹ˆ๋‹ค.
  • ์•ŒํŒŒํ…Œ์ŠคํŠธ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค (์ „์—๋Š” ์žˆ์—ˆ๋‚˜?)
  • ์นผ๋ผ๋งˆ์Šคํฌ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ์‚ฌ์‹ค์ƒ ๊ทธ ์™ธ๋Š” ๋™์ผํ•˜๊ฒŒ ์ œ์ž‘๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ข€ ๋” ๊ฐ€๋ฒผ์›Œ์ง„ ๋ฒ„์ „์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 1.5g
  • Mobile / Particles / Alpha Blended ์ด์ „์— ์‚ฌ์šฉํ•˜๋˜ ์•ŒํŒŒ ๋ธ”๋ Œ๋”ฉ ์‰์ด๋”์™€ ๋™์ผํ•˜๋ฉฐ, ๋‹ค๋ฅธ์ ์€ ์œ„์˜ Add ์‰์ด๋”์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • Mobile / Particles / Multiply ์ด์ „์— ์‚ฌ์šฉํ•˜๋˜ ๋ฉ€ํ‹ฐ ์‰์ด๋”์™€ ๋™์ผํ•˜๋ฉฐ, ๋‹ค๋ฅธ์ ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • ์Šค๋ฌด์Šค ํŒŒํ‹ฐํด ์˜ต์…˜์ด ์—†์Šต๋‹ˆ๋‹ค.
  • ์•ŒํŒŒํ…Œ์ŠคํŠธ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค
  • ์นผ๋ผ๋งˆ์Šคํฌ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ์—ญ์‹œ ์ด์ „์— ์‚ฌ์šฉํ•˜๋˜ ์‰์ด๋”์™€ ๋™์ผํ•˜๋˜ ์•ฝ๊ฐ„์”ฉ ๊ฐ€๋ณ๊ฒŒ ์ œ์ž‘๋œ ๊ฒƒ ์™ธ์— ์ฐจ์ด๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค .
  • Mobile / Particles / VertexLit Blended ์ด์ „์— ์‚ฌ์šฉํ•˜๋˜ ๋ฒ„ํ…์Šค๋ผ์ดํŠธ ๋ธ”๋ Œ๋””๋“œ ์‰์ด๋”์™€ ๋™์ผํ•˜๋ฉฐ, ๋‹ค๋ฅธ์ ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.
  • ์•ŒํŒŒํ…Œ์ŠคํŠธ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.
  • ์นผ๋ผ๋งˆ์Šคํฌ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ์ดํ•˜ ์ƒ๋™
  • Mobile / Skybox ์ด์ „์— ์‚ฌ์šฉํ•˜๋˜ ์Šค์นด์ด๋ฐ•์Šค ์‰์ด๋”์ฒ˜๋Ÿผ 6๋ฉด์˜ ํ…์Šค์ณ๋ฅผ ์ง‘์–ด ๋„ฃ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€๋งŒ ๋‹จ์ง€ Tint Color ๋งŒ์ด ์ œ๊ฑฐ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์‹ค์ƒ ๊ฑฐ์˜ ๊ฐ™์ง€๋งŒ ์•„์ฃผ ์‚ด์ง ๊ฐ€๋ฒผ์šด ์ •๋„์ž…๋‹ˆ๋‹ค. ๋Œ€๋ถ€๋ถ„, ๊ธฐ๋Šฅ๋“ค์„ ๊ผญ ์ œ๊ฑฐํ• ๋งŒํผ ํšจ๊ณผ๊ฐ€ ์ปค ๋ณด์ด์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

  • Mobile / VertexLit ๊ธฐ๋ณธ์ ์ธ ๋ฒ„ํ…์Šค ์นผ๋ผ ์‰์ด๋”์—์„œ ์ผ๋ถ€ ๊ธฐ๋Šฅ์ด ์‚ญ์ œ๋œ ์‰์ด๋”์ž…๋‹ˆ๋‹ค.

  • per material ์นผ๋ผ๊ฐ€ ์‚ญ์ œ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ŠคํŽ˜ํ˜๋Ÿฌ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.
  • emission์ด ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ ์™ธ๋Š” ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์‹ค์ƒ ๋ชจ๋ฐ”์ผ ์‰์ด๋”๋ผ๊ณ  ํ•ด์„œ ํŠน๋ณ„ํ•˜๊ฒŒ ๋งŒ๋“ค์–ด ์ง„ ๊ฒƒ์€ ์—†์œผ๋ฉฐ, ๋‹จ์ง€ ๊ธฐ๋Šฅ์ด ๋ช‡ ๊ฐœ์”ฉ ์‚ญ์ œ๋œ ์ •๋„์ž…๋‹ˆ๋‹ค. ์ฆ‰ ์ผ๋ฐ˜ ๊ฒŒ์ž„์šฉ ์‰์ด๋”๋„ '๊ฐ€๋ฒผ์šด ์‰์ด๋” ๋ผ๋ฉด' ๋ชจ๋ฐ”์ผ์—์„œ๋„ ๋Œ€๋ถ€๋ถ„ ๋˜‘๊ฐ™์ด ๋Œ์•„๊ฐ€๋ฉฐ, ์‚ฌ์šฉ์— ์ œ์•ฝ์ด ์—†์Šต๋‹ˆ๋‹ค. ์ดํŽ™ํŠธ ์‰์ด๋”๋“ค๋„ ๋ชจ๋ฐ”์ผ์šฉ์œผ๋กœ ์ปจ๋ฒ„ํŒ…ํ•ด ๋†“๋„๋ก ํ•ด์•ผ๊ฒ ๋„ค์š”...

ํ•˜๋“œ์›จ์–ด ๊ทธ๋ž˜ํ”ฝ ํ˜ธํ™˜์„ฑ๊ณผ ์—๋ฎฌ๋ ˆ์ด์…˜(Graphics Hardware Capabilities and Emulation)

์ถ”๊ฐ€์ ์œผ๋กœ ํ™•์ธํ•  ๊ฒƒ๋“ค