Last active
October 6, 2018 16:08
-
-
Save gtk2k/bb205ade48b970cb4646a23c84238caa to your computer and use it in GitHub Desktop.
@hammmmさんのUnity WebRTC PluginサンプルのシェーダーをカラースペースがLinearでもできるようにしたもの
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Custom/SampleShader1" { | |
Properties{ | |
_ColorSpace("Color Space", Int) = 0 // Gamma=0, Linear=1 | |
_Collect("Gamma", Float) = 2.2 | |
_MainTex("Main Tex", 2D) = "white" {} | |
} | |
SubShader{ | |
Tags { "RenderType" = "Opaque" } | |
CGPROGRAM | |
#pragma surface surf Lambert //alpha | |
struct Input { | |
float2 uv_MainTex; | |
}; | |
int _ColorSpace; | |
float _Collect; | |
sampler2D _MainTex; | |
float3 yuv2rgb(float3 yuv) { | |
// The YUV to RBA conversion, please refer to: http://en.wikipedia.org/wiki/YUV | |
// Y'UV420sp (NV21) to RGB conversion (Android) section. | |
float y_value; | |
float u_value; | |
float v_value; | |
if (_ColorSpace == 0) { | |
// Color Spaceの設定が"Gamma"の場合 | |
y_value = yuv[0]; | |
u_value = yuv[1]; | |
v_value = yuv[2]; | |
} | |
else { | |
// Color Spaceの設定が"Linear"の場合 | |
y_value = pow(yuv[0], _Collect); | |
u_value = pow(yuv[1], _Collect); | |
v_value = pow(yuv[2], _Collect); | |
} | |
float r = y_value + 1.370705 * (v_value - 0.5); | |
float g = y_value - 0.698001 * (v_value - 0.5) - (0.337633 * (u_value - 0.5)); | |
float b = y_value + 1.732446 * (u_value - 0.5); | |
return float3(r, g, b); | |
} | |
void surf(Input IN, inout SurfaceOutput o) { | |
if (_ColorSpace == 0) { | |
// Color Spaceの設定が"Gamma"の場合 | |
o.Albedo = yuv2rgb(tex2D(_MainTex, IN.uv_MainTex).rgb); | |
} | |
else { | |
// Color Spaceの設定が"Linear"の場合 | |
o.Albedo = pow(yuv2rgb(tex2D(_MainTex, IN.uv_MainTex).rgb), 1 / _Collect); | |
} | |
} | |
ENDCG | |
} | |
Fallback "Diffuse" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment