Hi everyone, I have a question about shading a mesh I created procedurally and I’m not sure if this is the place to post it.
I created a circular piece of mesh where the inside vertices move based on a spectrum analyzer. I apply colors to the vertices through the script and assigned a custom vertex shader so the colors show up. I also had to change my main camera’s rendering path to 'Legacy Deferred (light prepass) for the colors to show up. As of now the outside of my triangles light up the correct color but the end vertices all are void of color. I’ve tried changing the normals but that doesn’t help.
Any help would be greatly appreciated! I’m also open to suggestions on how to achieve this effect in a more efficient way. Here are the two scripts I assume would be the cause of this problem, let me know if you need to see anything else.
using UnityEngine;
using System.Collections;
public class CreatePlaneMesh : MonoBehaviour {
private int triVert = 0;
private int triCount = 0;
private float depth = 100f;
public float amp = 10f;
private AudioVars levels;
// Initialization
void Start () {
MeshFilter mf = GetComponent<MeshFilter>();
Mesh mesh = new Mesh();
mf.mesh = mesh;
levels = GameObject.Find("AudioVarsObject").GetComponent<AudioVars>();
Vector3[] vertices = new Vector3[25];
for (int i=0; i<=24; i+=2) {
vertices[i]= new Vector3(4*Mathf.Cos(i*Mathf.PI/12),4*Mathf.Sin(i*Mathf.PI/12),0f+depth);
}
for (int i=1; i<24; i+=2) {
vertices[i]= new Vector3(1.5f*Mathf.Cos(i*Mathf.PI/12),1.5f*Mathf.Sin(i*Mathf.PI/12),6f+depth);
}
//Triangles
int[] tri = new int[36];
for (int i=0; i<36; i++)
{
tri[i] = triVert;
triVert++;
triCount++;
if (triCount>2) {
triVert--;
triCount=0;
}
}
Color32[] colors32 = new Color32[25];
for (int i=0; i<=24; i++) {
colors32[i] = Color32.Lerp(Color.red, Color.green, vertices[i].z);
i++;
}
//Assign Arrays
mesh.vertices = vertices;
mesh.colors32 = colors32;
mesh.triangles = tri;
}
// Update is called once per frame
void Update () {
Mesh mesh = GetComponent<MeshFilter>().mesh;
Vector3[] vertices = new Vector3[25];
for (int i=0; i<=24; i+=2) {
vertices[i]= new Vector3(4*Mathf.Cos(i*Mathf.PI/12),4*Mathf.Sin(i*Mathf.PI/12),depth);
}
vertices[1]= new Vector3(levels.peak1*amp*Mathf.Cos(1*Mathf.PI/12),levels.peak1*amp*Mathf.Sin(1*Mathf.PI/12),6f+depth);
vertices[3]= new Vector3(levels.peak2*amp*Mathf.Cos(3*Mathf.PI/12),levels.peak2*amp*Mathf.Sin(3*Mathf.PI/12),6f+depth);
vertices[5]= new Vector3(levels.peak3*amp*Mathf.Cos(5*Mathf.PI/12),levels.peak3*amp*Mathf.Sin(5*Mathf.PI/12),6f+depth);
vertices[7]= new Vector3(levels.peak4*amp*Mathf.Cos(7*Mathf.PI/12),levels.peak4*amp*Mathf.Sin(7*Mathf.PI/12),6f+depth);
vertices[9]= new Vector3(levels.peak5*amp*Mathf.Cos(9*Mathf.PI/12),levels.peak5*amp*Mathf.Sin(9*Mathf.PI/12),6f+depth);
vertices[11]= new Vector3(levels.peak6*amp*Mathf.Cos(11*Mathf.PI/12),levels.peak6*amp*Mathf.Sin(11*Mathf.PI/12),6f+depth);
vertices[13]= new Vector3(levels.peak7*amp*Mathf.Cos(13*Mathf.PI/12),levels.peak7*amp*Mathf.Sin(13*Mathf.PI/12),6f+depth);
vertices[15]= new Vector3(levels.peak8*amp*Mathf.Cos(15*Mathf.PI/12),levels.peak8*amp*Mathf.Sin(15*Mathf.PI/12),6f+depth);
vertices[17]= new Vector3(levels.peak9*amp*Mathf.Cos(17*Mathf.PI/12),levels.peak9*amp*Mathf.Sin(17*Mathf.PI/12),6f+depth);
vertices[19]= new Vector3(levels.peak10*amp*Mathf.Cos(19*Mathf.PI/12),levels.peak10*amp*Mathf.Sin(19*Mathf.PI/12),6f+depth);
vertices[21]= new Vector3(levels.peak11*amp*Mathf.Cos(21*Mathf.PI/12),levels.peak11*amp*Mathf.Sin(21*Mathf.PI/12),6f+depth);
vertices[23]= new Vector3(levels.peak12*amp*Mathf.Cos(23*Mathf.PI/12),levels.peak12*amp*Mathf.Sin(23*Mathf.PI/12),6f+depth);
Color32[] colors32 = new Color32[25];
for (int i=0; i<=24; i++) {
colors32[i] = Color32.Lerp(Color.red, Color.green, vertices[i].z-20f);
i++;
}
//Assign Arrays
mesh.vertices = vertices;
mesh.colors32 = colors32;
depth -= .4f;
if (depth < -18f) {
depth = 100f;
}
}
}
Shader "Custom/Vertex Colors" {
SubShader {
BindChannels {
Bind "Color", color
Bind "Vertex", vertex
Bind "TexCoord", texcoord
}
Pass {
}
}
}


