Skip to content

Commit

Permalink
Procedural pattern computed with Vertex Shader
Browse files Browse the repository at this point in the history
  • Loading branch information
przemyslawzaworski authored Dec 2, 2019
1 parent 02ce652 commit bb4aab3
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
15 changes: 15 additions & 0 deletions VertexShaderImage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using UnityEngine;
public class VertexShaderImage : MonoBehaviour
{
public Shader shader;
protected Material material;
void Awake()
{
material = new Material(shader);
}
void OnRenderObject()
{
material.SetPass(0);
Graphics.DrawProcedural(MeshTopology.Triangles, 6 * 1024 * 1024, 1);
}
}
54 changes: 54 additions & 0 deletions VertexShaderImage.shader
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
Shader "Vertex Shader Image"
{
Subshader
{
Pass
{
Cull Off
CGPROGRAM
#pragma vertex VSMain
#pragma fragment PSMain
#pragma target 5.0

float2 PolarToCartesian (float2 p)
{
return p.x * float2(cos(p.y),sin(p.y));
}

float2 CartesianToPolar (float2 p)
{
return float2(length(p), atan2(p.y, p.x));
}

float3 Pattern (float2 uv)
{
uv = CartesianToPolar (abs(uv));
uv = (12.0*uv);
uv = PolarToCartesian(uv);
return float3(4.0/abs(uv),uv.x/uv.y);
}

float4 VSMain (uint id:SV_VertexID, out float3 color:TEXCOORD0) : SV_POSITION
{
float q = floor(float(id) / 6.0);
float px = fmod(float(id), 2.0);
float py = (float(id)-6.0 * floor(float(id)/6.0)) + 6.0;
py = sign(126.0-py*floor(126.0/py));
float3 center = float3(fmod(q,1024.0), 0.0, floor(q/1024.0));
float error = 0.0000001;
float2 uv = float2(center.x / 1024.0, center.z / 1024.0) + error;
uv = float2(2.0*uv-1.0);
color = Pattern(uv);
float scale = 0.1;
return UnityObjectToClipPos(float4(float3(sign(px)-0.5, 0.0, sign(py)-0.5) * scale + center * scale, 1.0));
}

float4 PSMain (float4 vertex:SV_POSITION, float3 color:TEXCOORD0) : SV_Target
{
return float4(color, 1.0);
}

ENDCG
}
}
}

0 comments on commit bb4aab3

Please sign in to comment.