Skip to content

Instantly share code, notes, and snippets.

@kamend
Created December 25, 2023 18:42
Show Gist options
  • Save kamend/1576ea8e5c2badf76742bebf782e3f75 to your computer and use it in GitHub Desktop.
Save kamend/1576ea8e5c2badf76742bebf782e3f75 to your computer and use it in GitHub Desktop.
Port of Three Js Fat Lines with Unity instancing shader
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Random = System.Random;
public class InstancedLines : MonoBehaviour
{
struct LineInstanceData
{
public Matrix4x4 objectToWorld;
}
public Color lineColor;
public Material mat;
public float lineWidth;
private Vector4[] _colors;
private Vector4[] _start;
private Vector4[] _end;
private float _aspect = (float)Screen.width / (float)Screen.height;
private float _resolutionY;
private Mesh lineSegmentMesh;
private int _numInstances;
private LineInstanceData[] _instanceData;
void Start()
{
lineSegmentMesh = CreateLineSegmentMesh();
(Vector3, Vector3)[] lines = new (Vector3, Vector3)[]
{
(new Vector3(0.0f, 0,0), new Vector3(2.0f, 0.0f, 0.0f)),
(new Vector3(2.0f, 0.0f, 0.0f), new Vector3(2.0f, 2.0f, 0.0f)),
(new Vector3(2.0f, 2.0f, 0.0f), new Vector3(0.0f, 2.0f, 0.0f)),
(new Vector3(0.0f, 2.0f, 0.0f), new Vector3(0.0f, 2.0f, 3.0f))
};
_numInstances = lines.Length;
_start = new Vector4[_numInstances];
_end = new Vector4[_numInstances];
_colors = new Vector4[_numInstances];
for (var i = 0; i < lines.Length; ++i)
{
_start[i] = lines[i].Item1;
_end[i] = lines[i].Item2;
_colors[i] = lineColor;
}
_instanceData = new LineInstanceData[_numInstances];
for (int i = 0; i < _numInstances; ++i)
{
_instanceData[i].objectToWorld = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, Vector3.one);
}
}
Mesh CreateLineSegmentMesh()
{
Vector3[] verts = new[]
{
new Vector3(-1.0f, 2.0f, 0.0f),
new Vector3(1.0f, 2.0f, 0.0f),
new Vector3(-1.0f, 1.0f, 0.0f),
new Vector3(1.0f, 1.0f, 0.0f),
new Vector3(-1.0f, 0.0f, 0.0f),
new Vector3(1.0f, 0.0f, 0.0f),
new Vector3(-1.0f, -1.0f, 0.0f),
new Vector3(1.0f, -1.0f, 0.0f)
};
Vector2[] uvs = new[]
{
new Vector2(-1.0f, 2.0f),
new Vector2(1.0f, 2.0f),
new Vector2(-1.0f, 1.0f),
new Vector2(1.0f, 1.0f),
new Vector2(-1.0f, -1.0f),
new Vector2(1.0f, -1.0f),
new Vector2(-1.0f, -2.0f),
new Vector2(1, -2.0f)
};
int[] idx = new[] { 0,2,1,2,3,1,2,4,3,4,5,3,4,6,5,6,7,5};
Mesh m = new Mesh();
m.vertices = verts;
m.triangles = idx;
m.uv = uvs;
return m;
}
// Update is called once per frame
void Update()
{
_aspect = (float)Screen.width / (float)Screen.height;
_resolutionY = Screen.height;
RenderParams rp = new RenderParams(mat);
rp.matProps = new MaterialPropertyBlock();
rp.matProps.SetVectorArray("_Colors", _colors);
rp.matProps.SetVectorArray("_Start", _start);
rp.matProps.SetVectorArray("_End", _end);
rp.matProps.SetFloat("_Aspect", _aspect);
rp.matProps.SetFloat("_ResolutionY", _resolutionY);
rp.matProps.SetFloat("_LineWidth", lineWidth);
rp.matProps.SetMatrix("_LocalToWorld", transform.localToWorldMatrix);
Graphics.RenderMeshInstanced(rp, lineSegmentMesh,0, _instanceData);
}
}
Shader "InstancedLines"
{
SubShader
{
Cull Off
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_instancing
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 uv : TEXCOORD0;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
float2 uv : TEXCOORD0;
UNITY_VERTEX_INPUT_INSTANCE_ID // use this to access instanced properties in the fragment shader.
};
uniform float4 _Colors[1023];
uniform float4 _Start[1023];
uniform float4 _End[1023];
uniform float4x4 _LocalToWorld;
uniform float _Aspect;
uniform float _ResolutionY;
uniform float _LineWidth;
v2f vert(appdata v, uint instanceID: SV_InstanceID)
{
v2f o;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_TRANSFER_INSTANCE_ID(v, o);
float4 worldStart = mul(_LocalToWorld, float4(_Start[instanceID].xyz,1.0));
float4 worldEnd = mul(_LocalToWorld, float4(_End[instanceID].xyz, 1.0));
// clip space
float4 clipStart = UnityObjectToClipPos(worldStart);
float4 clipEnd = UnityObjectToClipPos(worldEnd);
// ndc space
float3 ndcStart = clipStart.xyz / clipStart.w;
float3 ndcEnd = clipEnd.xyz / clipEnd.w;
// direction
float2 dir = ndcEnd.xy - ndcStart.xy;
// account for clip-space aspect ratio
dir.x *= _Aspect;
dir = normalize( dir );
float2 offset = float2( dir.y, - dir.x );
// undo aspect ratio adjustment
dir.x /= _Aspect;
offset.x /= _Aspect;
// sign flip
if ( v.vertex.x < 0.0 ) offset *= - 1.0;
// endcaps
if ( v.vertex.y < 0.0 ) {
offset += - dir;
} else if ( v.vertex.y > 1.0 ) {
offset += dir;
}
offset *= _LineWidth;
// adjust for clip-space to screen-space conversion // maybe resolution should be based on viewport ...
offset /= _ResolutionY;
// select end
float4 clip = ( v.vertex.y < 0.5 ) ? clipStart : clipEnd;
offset *= clip.w;
clip.xy += offset;
o.vertex = clip;
o.color = _Colors[instanceID];
o.uv = v.uv;
return o;
}
fixed4 frag(v2f i) : SV_Target
{
UNITY_SETUP_INSTANCE_ID(i);
if ( abs( i.uv.y ) > 1.0 ) {
float a = i.uv.x;
float b = ( i.uv.y > 0.0 ) ? i.uv.y - 1.0 : i.uv.y + 1.0;
float len2 = a * a + b * b;
if ( len2 > 1.0 ) discard;
}
float4 c = i.color;
return c;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment