Skip to content

Instantly share code, notes, and snippets.

@basisbit
Created Dec 10, 2019
Embed
What would you like to do?
using UnityEngine;
public class Waveform : MonoBehaviour
{
private Material material;
private float[] samples;
private void Start()
{
material = GetComponent<Renderer>().material;
samples = new float[1024];
}
private void Update()
{
for (int i = 0; i < samples.Length; i++)
samples[i] = Mathf.Sin(Time.time + i * 1f) / 2 + 0.5f;
material.SetFloatArray("_samples", samples);
}
}
Shader "Unlit/Waveform"
{
Properties
{
}
SubShader
{
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
v2f vert(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
float _samples[1024];
fixed4 frag(v2f i) : SV_Target
{
float s = _samples[(int)i.uv.x * 1024];
if (abs(i.uv.y - s) < 0.01f)
return fixed4(1, 1, 1, 1);
else
return fixed4(0, 0, 0, 1);
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment