Skip to content

Instantly share code, notes, and snippets.

@m-hayabusa
Last active September 28, 2021 11:41
Show Gist options
  • Save m-hayabusa/06a04a0c4a5721178203c0b88a91b609 to your computer and use it in GitHub Desktop.
Save m-hayabusa/06a04a0c4a5721178203c0b88a91b609 to your computer and use it in GitHub Desktop.
ディスプレイの部分だけいい感じにやってくためのシェーダー ライセンスはMITでどうぞ
Copyright 2021 m-hayabusa
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Shader "nekomimiStudio/nekomimiDisplayShader" //VERSION 1.0, 2021-09-28 14:40
{
Properties
{
_Color ("Color", Color) = (1,1,1,1)
[NoScaleOffset] _ColorMask ("ColorMask", 2D) = "white" {}
_MainTex ("Albedo (RGB)", 2D) = "white" {}
[NoScaleOffset] _BumpMap ("Bump Map", 2D) = "bump" {}
[NoScaleOffset] _EmissionMap ("Emission Map", 2D) = "black" {}
[HDR] _EmissionColor ("Emission Color", Color) = (0,0,0)
[NoScaleOffset] _MetallicGlossMap("M/S Map", 2D) = "white" {}
_LCDRange ("LCD Pos", Vector) = (0,0,0.5,0.5)
[NoScaleOffset] _LCDTex ("LCD Color", 2D) = "black" {}
[Toggle(_LCDUSEMASK)] _LCDUSEMASK ("Use LCD Mask", Int) = 0
[NoScaleOffset] _LCDMask ("LCD Mask", 2D) = "black" {}
[ToggleOff] _LCDUseOverlay ("Use LCD Overlay", Int) = 0
[NoScaleOffset] _LCDOverlay ("LCD Overlay", 2D) = "black" {}
[HDR] _LCDColor ("LCD Brightness", Color) = (1,1,1)
[Toggle(_LCDISMIRROR)] _LCDISMIRROR ("LCD Flip (isSelfie)", Int) = 0
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows
#pragma multi_compile _ _LCDUSEMASK _LCDISMIRROR
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
sampler2D _MainTex;
sampler2D _ColorMask;
sampler2D _LCDTex;
sampler2D _LCDMask;
sampler2D _LCDOverlay;
sampler2D _BumpMap;
sampler2D _EmissionMap;
sampler2D _MetallicGlossMap;
struct Input
{
float2 uv_MainTex;
};
half _Glossiness;
fixed4 _Color;
fixed4 _EmissionColor;
fixed4 _LCDEmissionColor;
fixed4 _LCDColor;
fixed4 _LCDRange;
// int _LCDUseMask;
int _LCDUseOverlay;
// int _LCDisMirror;
// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
// See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
// #pragma instancing_options assumeuniformscaling
UNITY_INSTANCING_BUFFER_START(Props)
// put more per-instance properties here
UNITY_INSTANCING_BUFFER_END(Props)
void surf (Input IN, inout SurfaceOutputStandard o)
{
// Albedo comes from a texture tinted by color
fixed4 c;
fixed4 e;
fixed4 m = tex2D (_MetallicGlossMap, IN.uv_MainTex);
float2 uv; //LCD部を0-1にしたUV
uv.x = ((IN.uv_MainTex.x - _LCDRange.x) / (_LCDRange.z - _LCDRange.x));
uv.y = ((IN.uv_MainTex.y - _LCDRange.y) / (_LCDRange.w - _LCDRange.y));
_Color.rgb = _Color.rgb + ((1 - tex2D (_ColorMask, IN.uv_MainTex).a) * (1 - _Color.rgb));
fixed4 Color_Main = tex2D (_MainTex, IN.uv_MainTex) * _Color;
fixed4 Color_Emission = tex2D (_EmissionMap, IN.uv_MainTex) * _EmissionColor;
float4 Color_Overlay = tex2D (_LCDOverlay, uv) * _LCDColor;
#ifdef _LCDISMIRROR
//LCD左右反転 オーバーレイは反転させないので、この(変な)タイミングになる
uv.x = ((IN.uv_MainTex.x - _LCDRange.z) / (_LCDRange.x - _LCDRange.z));
#endif
fixed4 Color_LCD = tex2D (_LCDTex, uv) * _LCDColor;
// オーバーレイが無効ならAlphaを0にしてしまう そうでないならオーバーレイ画像のAlphaを使って混ぜる
float OverlayAlpha = Color_Overlay.a * _LCDUseOverlay;
#ifdef _LCDUSEMASK
// if (_LCDUseMask) { // マスク使う
float Mask_LCDMask = tex2D (_LCDMask, IN.uv_MainTex).a;
c = (Color_Main * (1 - Mask_LCDMask))
+ (Color_LCD * (1 - OverlayAlpha) + Color_Overlay * (OverlayAlpha)) * Mask_LCDMask;
e = (Color_Emission * (1 - Mask_LCDMask))
+ (Color_LCD * (1 - OverlayAlpha) + Color_Overlay * (OverlayAlpha)) * Mask_LCDMask;
#else
// } else { // マスク使わない
if (( _LCDRange.x <= IN.uv_MainTex.x && IN.uv_MainTex.x <= _LCDRange.z) && (_LCDRange.y <= IN.uv_MainTex.y && IN.uv_MainTex.y <= _LCDRange.w)){
c = Color_LCD * (1 - OverlayAlpha) + Color_Overlay * (OverlayAlpha);
e = Color_LCD * (1 - OverlayAlpha) + Color_Overlay * (OverlayAlpha);
} else {
c = Color_Main;
e = Color_Emission;
}
// }
#endif
o.Albedo = c.rgb;
o.Emission = e.rgb;
o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_MainTex));
o.Smoothness = m.a;
o.Metallic = m.r;
// o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
  • Color: 本体色です
  • ColorMask: 本体色を適用しない場所を塗り潰したテクスチャを割り当てることができます
  • Albedo: メインの色を描いたテクスチャを割り当てます
  • Bump Map: ノーマルマップを割り当てることができます
  • Emmision Map: エミッションテクスチャを割り当てることができます
  • Emmision Color: エミッションテクスチャに載せる色です
  • M/S Map: メタリック・スムースネスマップを割り当てます

ここまでのテクスチャや色は、以下で記載するLCD部分には適用されません (Bump MapとM/S Mapを除く)

  • LCD Pos: UV上の画面の位置です。(X,Y,Z,W)が(X0,Y0, X1,Y1)になっています
  • LCD Color: 画面に表示するテクスチャを割り当てます
  • Use LCD Mask: LCD Maskを使用する場合はチェックを入れてください Keyword:_LCDUSEMASK
  • LCD Mask: 画面角が丸い端末、ノッチがある端末など、LCD部分の中で切り抜く必要がある場合に割り当てることができます (このテクスチャのアルファがLCD Colorの透明度として扱われる)
  • Use LCD Overlay: LCD Overlayを使用する場合はチェックを入れてください
  • LCD Overlay: LCD Colorに加算して表示するテクスチャです。LCD ColorにRenderTextureを割り当てて、その上にカメラのUIを表示したい時などに使えます
  • LCD Brightness: LCD Colorのエミッションの倍率です
  • LCD Flip: LCD Colorを鏡写しで表示したいときに使います。LCD ColorにRenderTextureを割り当てている状態で、かつそれがインカメラの時に使えます Keyword:_LCDISMIRROR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment