これを元に色々と変更。
変更点
メモ
surface shader の方が normal の計算などシンプルで、ライティングは簡単。
vert の中でテクスチャの値を使用するのに tex2Dlod を使用した。(windowsでの動作確認を行う必要あり) How to use vertex texture fetch? - Unity Forum
テクスチャの裏面を表示し陰を正常にするために、VFACE を使用。
struct Input { float2 uv_MainTex; float IsFacing:VFACE; }; ... void surf (Input IN, inout SurfaceOutputStandard o) { o.Normal *= (IN.IsFacing < 0) ? -1 : 1;//表裏で陰影が正しくなるように if(IN.IsFacing < 0){ IN.uv_MainTex.x = 1-IN.uv_MainTex.x;//表裏で表示が反転しないように } ... }
のように、surf側で法線反転・テクスチャuvの左右反転を行なった。
VFACE 使えば色々出来そうだな。
— dizgid (@inok) 2021年1月15日
fixed4 frag (fixed facing : VFACE) : SV_Target
{
// VFACE 入力は正面向きでは負の値、
// 裏向きでは負の値です。その値によって
// 2 色のうちの 1 つを出力します。
return facing > 0 ? _ColorFront : _ColorBack;
}https://t.co/kUZT2VtbeU
surface shader ではこんな感じと。
— dizgid (@inok) 2021年1月15日
struct Input { float IsFacing:VFACE; };
void SurfaceShader( Input i , inout SurfaceOutputStandard o )
{
float4 color = (i.IsFacing>0) ? float4(1,0,0,1) : float4(0,0,1,1);
o.Emission = color.rgb;
o.Alpha = color.a;
}https://t.co/2wApWcLKLK
GPU instancing 対応は、 使用するプロパティを
UNITY_INSTANCING_BUFFER_START(Props)
UNITY_DEFINE_INSTANCED_PROP(fixed4, _Color)
UNITY_DEFINE_INSTANCED_PROP(float, _WaveAmplitude1)
UNITY_INSTANCING_BUFFER_END(Props)
のようにUNITY_INSTANCING_BUFFER_START ... _END の中に指定して使う時には
UNITY_ACCESS_INSTANCED_PROP(Props, _Color)
として使う。 textureを個別に変更したい時には特殊で、texture2DArrayを使用する必要がある。 monobehaviour側で複数のtexureをまとめてtexture2DArrayにしてshaderには使用したいindexを渡す。
UnityのshaderでGPU instancingでオブジェクトごとにtextue変えたいときは Textue2DArrayでindex指定しろってこと。https://t.co/vuckHFgexU
— dizgid (@inok) 2021年1月15日
影の処理で、fullforwardshadows だと影は動かない。 addshadow だと影も揺れるアニメーションとなる。
#pragma surface surf Standard addshadow vertex:vert
Unity スプライトに影をつける ~その2~ - おねむゲーマーの備忘録
アルファの対応は、
clip(c.a - _Cutoff);
で行うのが速そう。
参考
How to use vertex texture fetch? - Unity Forum
Unity シェーダーチュートリアル アルファとアルファテスト – Tsumiki Tech Times|積木製作
【Unityシェーダ入門】ポリゴンの表面と裏面に別テクスチャを貼る - おもちゃラボ