1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Unity Shader Hightmap from Alpha channel

Posted at

影を書き込んだテクスチャ+アルファチャンネルでハイトマップのような事をしたかったのでやってみた。
スケールにより、_Pow /= scale^2 する必要がある。
Heightmap.jpg

Shader "Unlit/HeightAlpha"
{
    Properties
    {
        _MainTex("Texture", 2D) = "white" {}
        _Pow ("Height", Range(0,0.1)) = 0.05
    }
    SubShader
    {
	    Tags { "RenderType"="Opaque" }
	    LOD 100

	    Pass
	    {
		    CGPROGRAM
		    #pragma vertex vert
		    #pragma fragment frag
		    #pragma multi_compile_fog
		
		    #include "UnityCG.cginc"

		    struct appdata
		    {
			    float4 pos : POSITION;
                float2 uv : TEXCOORD0;
                float3 normal : NORMAL;
                float4 tan : TANGENT;
		    };

		    struct v2f
		    {
			    float2 uv : TEXCOORD0;
                float2 hightVec : TEXCOORD1;
			    UNITY_FOG_COORDS(1)
			    float4 vertex : SV_POSITION;
		    };

		    sampler2D _MainTex;
		    float4 _MainTex_ST;
		    float _Pow;

		    v2f vert (appdata v)
		    {
			    v2f o;
			    o.vertex = UnityObjectToClipPos(v.pos);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                float2 a = (UnityObjectToClipPos(v.pos + v.normal).xy - o.vertex.xy);
                float2 b = (UnityObjectToClipPos(v.pos + v.tan.xyz).xy - o.vertex.xy);
                o.hightVec = float2((a.y * -b.y - a.x * b.x), a.y * b.x - a.x * b.y) * _Pow;
			    UNITY_TRANSFER_FOG(o,o.vertex);
			    return o;
		    }
		
		    fixed4 frag (v2f i) : SV_Target
		    {
                fixed height = tex2D(_MainTex, i.uv).a;
			    fixed4 col = tex2D(_MainTex, i.uv + i.hightVec * height);
			    UNITY_APPLY_FOG(i.fogCoord, col);
			    return col;
		    }
		    ENDCG
	    }
    }
}
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?