Last active
March 1, 2018 08:08
-
-
Save hiroakioishi/454773a4f182c55673f271124ff50a32 to your computer and use it in GitHub Desktop.
[Unity]デバッグ用に法線方向に矢印を描画するShader
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
| /* | |
| * 2D Vector Field Flow Debug Arrow Render | |
| * | |
| * This code is a porting of | |
| * https://www.shadertoy.com/view/4s23DG | |
| */ | |
| Shader "Debug/VectorFieldFlowArrowRender" | |
| { | |
| Properties | |
| { | |
| _MainTex("Texture", 2D) = "white" {} | |
| _ArrowTileSize("Arrow Tile Size", Vector) = (0.1, 0.1, 1.0, 1.0) | |
| _ArrowShaftThickness("Arrow Shaft Thickness", Float) = 0.0025 | |
| _ArrowHeadLength("Arrow Head Length", Float) = 0.0125 | |
| _ArrowHeadAngle("Arrow Head Angle", Float) = 0.75 | |
| _ArrowColor("Arrow Color", Color) = (1.0, 1.0, 1.0, 1.0) | |
| } | |
| CGINCLUDE | |
| #include "UnityCG.cginc" | |
| //#define RANGE_ZERO_TO_ONE | |
| sampler2D _MainTex; | |
| float4 _MainTex_ST; | |
| float2 _ArrowTileSize; | |
| float _ArrowShaftThickness; | |
| float _ArrowHeadLength; | |
| float _ArrowHeadAngle; | |
| float4 _ArrowColor; // r, g, b, a | |
| float2 arrowTileCenterCoord(float2 uv) | |
| { | |
| return (floor(uv / _ArrowTileSize) + 0.5) * _ArrowTileSize.xy; | |
| } | |
| float arrow(float2 p, float2 v) | |
| { | |
| p -= arrowTileCenterCoord(p); | |
| float magV = length(v); | |
| float magP = length(p); | |
| if (magV > 0.0) | |
| { | |
| float2 dirP = p / magP; | |
| float2 dirV = v / magV; | |
| magV = clamp(magV, 0.001, _ArrowTileSize * 0.5); | |
| v = dirV * magV; | |
| float dist = 0.0; | |
| dist = max | |
| ( | |
| // Shaft | |
| _ArrowShaftThickness * 0.25 - | |
| max(abs(dot(p, float2(dirV.y, -dirV.x))), // Width | |
| abs(dot(p, dirV)) - magV + _ArrowHeadLength * 0.5), // Length | |
| // Arrow head | |
| min(0.0, dot(v - p, dirV) - cos(_ArrowHeadAngle * 0.5) * length(v - p)) * 2.0 + // Front sides | |
| min(0.0, dot(p, dirV) + _ArrowHeadLength - magV) //Back | |
| ); | |
| return clamp(1.0 + dist * 512.0, 0.0, 1.0); | |
| } | |
| else | |
| { | |
| return max(0.0, 1.2 - magP); | |
| } | |
| } | |
| fixed4 frag(v2f_img i) : SV_Target | |
| { | |
| float4 mainColor = tex2D(_MainTex, i.uv.xy); | |
| #ifdef RANGE_ZERO_TO_ONE | |
| float4 offset = -(float4)0.5; | |
| #else | |
| float4 offset = 0.0; | |
| #endif | |
| float arrowStrength = arrow(i.uv.xy, 2.0 * (tex2D(_MainTex, arrowTileCenterCoord(i.uv.xy)) + offset) * _ArrowTileSize.xy * 0.5); | |
| return lerp(mainColor, _ArrowColor, arrowStrength * _ArrowColor.a); | |
| } | |
| ENDCG | |
| SubShader | |
| { | |
| Tags{ "RenderType" = "Opaque" } | |
| LOD 100 | |
| Pass | |
| { | |
| CGPROGRAM | |
| #pragma vertex vert_img | |
| #pragma fragment frag | |
| ENDCG | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment