Shaderでメガネ

PCメガネを割と強めに床においたら壊れてしまいました…
そんなわけで、今回はメガネでShaderの練習。
モデリングはちょっと適当ですが、レンズは湾曲を持たせていて、その湾曲具合によって後方の写り込みを操作。今回は簡易的に視線ベクトルと法線ベクトルを使って、反射角を求めた値をちょっといじればできるかなと思ってやってみた。反射角自体はreflect()でそのまま出せる。GrabPassで取得したピクセルカラーを取得する IN.screenPos.xy /IN.screenPos.w の分母の値に、reflect()から取得したベクトルのxとyを使って調整してみた。一応それっぽく見えている気がする。メガネのフレーム自体がグラスの歪みに表示されてしまうのを防ぐためメガネのフレームはレンダー順をずらすためTransparentに設定。メガネの柄の部分はOpaqueだけど、ちょっと表示がおかしいのは…なんでだろ…(?)
フチの明るさは下記のサイトを参考に、視線ベクトルと法線ベクトルの内積の角度から調整して角度がキツイ場合に少し明るくEmissionを調整。
参考)○×つくろーどっとコム | その4 サーフェイスシェーダでガラスを作る
Shader "Glasses" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
}
SubShader {
Tags {
"Queue"="Transparent"
"RenderType"="Transparent"
}
LOD 200
GrabPass {}
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
sampler2D _MainTex;
sampler2D _GrabTexture;
struct Input {
float2 uv_MainTex;
float4 screenPos;
float3 viewDir;
float3 worldNormal;
};
half _Glossiness;
half _Metallic;
fixed4 _Color;
// 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_CBUFFER_START(Props)
// put more per-instance properties here
UNITY_INSTANCING_CBUFFER_END
void surf (Input IN, inout SurfaceOutputStandard o) {
float adj = 0.2;
float margin = 1.0 - dot(normalize(IN.viewDir), normalize(IN.worldNormal));
float2 reflectDir = reflect(IN.worldNormal, IN.viewDir);
float2 grabUV = float2(IN.screenPos.x + adj * reflectDir.x, IN.screenPos.y + adj * reflectDir.y) / IN.screenPos.w;
fixed3 grabC = tex2D(_GrabTexture, grabUV).rgb;
o.Emission = 0.5 * margin;
o.Albedo = grabC * _Color;
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = 1;
}
ENDCG
}
FallBack "Diffuse"
}
コメントを残す