Skip to content

Commit bb4aab3

Browse files
Procedural pattern computed with Vertex Shader
1 parent 02ce652 commit bb4aab3

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

VertexShaderImage.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using UnityEngine;
2+
public class VertexShaderImage : MonoBehaviour
3+
{
4+
public Shader shader;
5+
protected Material material;
6+
void Awake()
7+
{
8+
material = new Material(shader);
9+
}
10+
void OnRenderObject()
11+
{
12+
material.SetPass(0);
13+
Graphics.DrawProcedural(MeshTopology.Triangles, 6 * 1024 * 1024, 1);
14+
}
15+
}

VertexShaderImage.shader

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
Shader "Vertex Shader Image"
2+
{
3+
Subshader
4+
{
5+
Pass
6+
{
7+
Cull Off
8+
CGPROGRAM
9+
#pragma vertex VSMain
10+
#pragma fragment PSMain
11+
#pragma target 5.0
12+
13+
float2 PolarToCartesian (float2 p)
14+
{
15+
return p.x * float2(cos(p.y),sin(p.y));
16+
}
17+
18+
float2 CartesianToPolar (float2 p)
19+
{
20+
return float2(length(p), atan2(p.y, p.x));
21+
}
22+
23+
float3 Pattern (float2 uv)
24+
{
25+
uv = CartesianToPolar (abs(uv));
26+
uv = (12.0*uv);
27+
uv = PolarToCartesian(uv);
28+
return float3(4.0/abs(uv),uv.x/uv.y);
29+
}
30+
31+
float4 VSMain (uint id:SV_VertexID, out float3 color:TEXCOORD0) : SV_POSITION
32+
{
33+
float q = floor(float(id) / 6.0);
34+
float px = fmod(float(id), 2.0);
35+
float py = (float(id)-6.0 * floor(float(id)/6.0)) + 6.0;
36+
py = sign(126.0-py*floor(126.0/py));
37+
float3 center = float3(fmod(q,1024.0), 0.0, floor(q/1024.0));
38+
float error = 0.0000001;
39+
float2 uv = float2(center.x / 1024.0, center.z / 1024.0) + error;
40+
uv = float2(2.0*uv-1.0);
41+
color = Pattern(uv);
42+
float scale = 0.1;
43+
return UnityObjectToClipPos(float4(float3(sign(px)-0.5, 0.0, sign(py)-0.5) * scale + center * scale, 1.0));
44+
}
45+
46+
float4 PSMain (float4 vertex:SV_POSITION, float3 color:TEXCOORD0) : SV_Target
47+
{
48+
return float4(color, 1.0);
49+
}
50+
51+
ENDCG
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)