BarycentricWireframeUv1
From Unify Community Wiki
(Difference between revisions)
Joseph05408 (Talk | contribs) (Removing all content from page) |
(language edit, I know glsl is not shaderlab, but still...) |
||
(6 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
+ | The following shader assumes that you have mesh.uv set to barycentric uvs. You can set the line width, line color, and grid color. | ||
+ | ==C#== | ||
+ | <syntaxhighlight lang="csharp"> | ||
+ | public Vector2[] GetBarycentricFixed() | ||
+ | { | ||
+ | Vector2[] uvs = | ||
+ | new Vector2[4]; | ||
+ | |||
+ | const int topLeft = 0; | ||
+ | const int topRight = 1; | ||
+ | const int bottomLeft = 2; | ||
+ | const int bottomRight = 3; | ||
+ | |||
+ | uvs[topLeft] = new Vector2(0,0); | ||
+ | uvs[topRight] = new Vector2(1,0); | ||
+ | uvs[bottomLeft] = new Vector2(0,1); | ||
+ | uvs[bottomRight] = new Vector2(1,1); | ||
+ | |||
+ | return uvs; | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | ==ShaderLab - BarycentricWireframeUv1.shader== | ||
+ | <syntaxhighlight lang="glsl"> | ||
+ | Shader "BarycentricWireframeUv1" { | ||
+ | Properties { | ||
+ | _LineColor ("Line Color", Color) = (1,1,1,1) | ||
+ | _GridColor ("Grid Color", Color) = (0,0,0,0) | ||
+ | _LineWidth ("Line Width", float) = 0.1 | ||
+ | } | ||
+ | SubShader { | ||
+ | Pass { | ||
+ | |||
+ | CGPROGRAM | ||
+ | |||
+ | #pragma vertex vert | ||
+ | #pragma fragment frag | ||
+ | #include "UnityCG.cginc" | ||
+ | |||
+ | uniform float4 _LineColor; | ||
+ | uniform float4 _GridColor; | ||
+ | uniform float _LineWidth; | ||
+ | |||
+ | // vertex input: position, uv1, uv2 | ||
+ | struct appdata { | ||
+ | float4 vertex : POSITION; | ||
+ | float4 texcoord1 : TEXCOORD1; | ||
+ | float4 color : COLOR; | ||
+ | }; | ||
+ | |||
+ | struct v2f { | ||
+ | float4 pos : POSITION; | ||
+ | float4 texcoord1 : TEXCOORD1; | ||
+ | float4 color : COLOR; | ||
+ | }; | ||
+ | |||
+ | v2f vert (appdata v) { | ||
+ | v2f o; | ||
+ | o.pos = mul( glstate.matrix.mvp, v.vertex); | ||
+ | o.texcoord1 = v.texcoord1; | ||
+ | o.color = v.color; | ||
+ | return o; | ||
+ | } | ||
+ | |||
+ | float4 frag(v2f i ) : COLOR | ||
+ | { | ||
+ | if (i.texcoord1.x < _LineWidth || | ||
+ | i.texcoord1.y < _LineWidth) | ||
+ | { | ||
+ | return _LineColor; | ||
+ | } | ||
+ | |||
+ | if ((i.texcoord1.x - i.texcoord1.y) < _LineWidth && | ||
+ | (i.texcoord1.y - i.texcoord1.x) < _LineWidth) | ||
+ | { | ||
+ | return _LineColor; | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | return _GridColor; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | ENDCG | ||
+ | |||
+ | } | ||
+ | } | ||
+ | Fallback "Vertex Colored", 1 | ||
+ | } | ||
+ | </syntaxhighlight> |
Latest revision as of 17:51, 20 March 2015
The following shader assumes that you have mesh.uv set to barycentric uvs. You can set the line width, line color, and grid color.
[edit] C#
public Vector2[] GetBarycentricFixed() { Vector2[] uvs = new Vector2[4]; const int topLeft = 0; const int topRight = 1; const int bottomLeft = 2; const int bottomRight = 3; uvs[topLeft] = new Vector2(0,0); uvs[topRight] = new Vector2(1,0); uvs[bottomLeft] = new Vector2(0,1); uvs[bottomRight] = new Vector2(1,1); return uvs; }
[edit] ShaderLab - BarycentricWireframeUv1.shader
Shader "BarycentricWireframeUv1" { Properties { _LineColor ("Line Color", Color) = (1,1,1,1) _GridColor ("Grid Color", Color) = (0,0,0,0) _LineWidth ("Line Width", float) = 0.1 } SubShader { Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" uniform float4 _LineColor; uniform float4 _GridColor; uniform float _LineWidth; // vertex input: position, uv1, uv2 struct appdata { float4 vertex : POSITION; float4 texcoord1 : TEXCOORD1; float4 color : COLOR; }; struct v2f { float4 pos : POSITION; float4 texcoord1 : TEXCOORD1; float4 color : COLOR; }; v2f vert (appdata v) { v2f o; o.pos = mul( glstate.matrix.mvp, v.vertex); o.texcoord1 = v.texcoord1; o.color = v.color; return o; } float4 frag(v2f i ) : COLOR { if (i.texcoord1.x < _LineWidth || i.texcoord1.y < _LineWidth) { return _LineColor; } if ((i.texcoord1.x - i.texcoord1.y) < _LineWidth && (i.texcoord1.y - i.texcoord1.x) < _LineWidth) { return _LineColor; } else { return _GridColor; } } ENDCG } } Fallback "Vertex Colored", 1 }