Skip to content

Instantly share code, notes, and snippets.

@EsProgram
Last active December 19, 2016 02:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EsProgram/8830dfd6a074e43e4d1a to your computer and use it in GitHub Desktop.
Save EsProgram/8830dfd6a074e43e4d1a to your computer and use it in GitHub Desktop.
UnityのShader Variantについて調べてみた ref: http://qiita.com/Es_Program/items/79edf9f8fca786b365aa
#pragma multi_compile _ Lighting_ON Lighting_OFF
#ifdef Lighting_ON
//ライティングONの時の処理
#elif Lighting_OFF
//ライティングOFFの時の処理
#endif
Shader "Unlit/Test1"
{
SubShader
{
Pass
{
CGPROGRAM
#pragma vertex vert_img
#pragma fragment frag
#include "UnityCG.cginc"
#pragma multi_compile RED GREEN BLUE
#pragma debug
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = float4(0, 0, 0, 1);
#ifdef RED
col = float4(1, 0, 0, 1);
#elif GREEN
col = float4(0, 1, 0, 1);
#elif BLUE
col = float4(0, 0, 1, 1);
#endif
return col;
}
ENDCG
}
}
}
#pragma multi_compile RED GREEN BLUE
#ifdef RED
col = float4(1, 0, 0, 1);
#elif GREEN
col = float4(0, 1, 0, 1);
#elif BLUE
col = float4(0, 0, 1, 1);
#endif
// Compiled shader for custom platforms, uncompressed size: 6.4KB
Shader "Unlit/Test1" {
SubShader {
// Stats for Vertex shader:
// d3d11 : 4 math
Pass {
GpuProgramID 59568
Program "vp" {
SubProgram "d3d11 " {
// Stats: 4 math
Keywords { "RED" }
Bind "vertex" Vertex
Bind "texcoord" TexCoord0
ConstBuffer "UnityPerDraw" 352
Matrix 0 [glstate_matrix_mvp]
BindCB "UnityPerDraw" 0
"
~省略~
"
}
SubProgram "d3d11 " {
// Stats: 4 math
Keywords { "GREEN" }
Bind "vertex" Vertex
Bind "texcoord" TexCoord0
ConstBuffer "UnityPerDraw" 352
Matrix 0 [glstate_matrix_mvp]
BindCB "UnityPerDraw" 0
"
~省略~
"
}
SubProgram "d3d11 " {
// Stats: 4 math
Keywords { "BLUE" }
Bind "vertex" Vertex
Bind "texcoord" TexCoord0
ConstBuffer "UnityPerDraw" 352
Matrix 0 [glstate_matrix_mvp]
BindCB "UnityPerDraw" 0
"
~省略~
"
}
}
Program "fp" {
SubProgram "d3d11 " {
Keywords { "RED" }
"
~省略~
"
}
SubProgram "d3d11 " {
Keywords { "GREEN" }
"
~省略~
"
}
SubProgram "d3d11 " {
Keywords { "BLUE" }
"
~省略~
"
}
}
}
}
}
using System.Collections;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
public void OnGUI()
{
KeywordButtonGUI("RED");
KeywordButtonGUI("GREEN");
KeywordButtonGUI("BLUE");
}
private void KeywordButtonGUI(string keyword)
{
if(GUILayout.Button(keyword + " : ON"))
Shader.EnableKeyword(keyword);
if(GUILayout.Button(keyword + " : OFF"))
Shader.DisableKeyword(keyword);
GUILayout.Label(keyword + " Enabled : " + Shader.IsKeywordEnabled(keyword).ToString());
}
}
#pragma shader_feature FANCY_STUFF
#pragma multi_compile A B C
#pragma multi_compile D E
Shader "Unlit/Test1"
{
Properties{
[KeywordEnum(RED,GREEN,BLUE)]
_COLOR("COLOR KEYWORD", Float) = 0
}
SubShader
{
Pass
{
CGPROGRAM
#pragma vertex vert_img
#pragma fragment frag
#include "UnityCG.cginc"
#pragma multi_compile _COLOR_RED _COLOR_GREEN _COLOR_BLUE
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = float4(0, 0, 0, 1);
#ifdef _COLOR_RED
col = float4(1, 0, 0, 1);
#elif _COLOR_GREEN
col = float4(0, 1, 0, 1);
#elif _COLOR_BLUE
col = float4(0, 0, 1, 1);
#endif
return col;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment