Created
December 10, 2019 23:02
-
-
Save basisbit/2241784c753d71f1f71b9b2337538e75 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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