- Home /
Vertex Colours flickering blue
Hi,
I am working on a little custom trail script for my game and I've ran into an issue. Basically when there is one in the scene it works fine, the vertex colour is set, but as soon as I create a second instance it will change both meshes to blue and occasionally flicker back to the original colour.
The shader I am currently using is the Shaders/Mobile/Particles/Alpha Blended which I assume uses vertex colouring somewhere as the colours do change, however I am not sure how to approach the issue I'm running into.
I read around a little online and I believe that it is related to Dynamic batching but I am not sure if this is true, if so, is there a way to get around the issue?
[EDIT] I disabled Dynamic batching and it fixed the issue, however that means none of my game can benefit from Dynamic batching, is there a way to flag these as non-batchable?
And here is the code which is used to generate this trail effect so far :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace PlanetDefender
{
[RequireComponent(typeof(MeshRenderer), typeof(MeshFilter))]
public class Trail : MonoBehaviour
{
private Vector3[] _vertices;
private int[] _indices;
private Color[] _colours;
private Mesh _mesh;
[SerializeField]
private float _startWidth;
[SerializeField]
private float _endWidth;
[SerializeField]
private float _length;
[SerializeField]
private Color _startColour;
[SerializeField]
private Color _endColour;
public float startWidth
{
get { return _startWidth; }
set
{
_startWidth = value;
rebuild();
}
}
public float endWidth
{
get { return _endWidth; }
set
{
_endWidth = value;
rebuild();
}
}
public float length
{
get { return _length; }
set
{
_length = value;
rebuild();
}
}
public Color startColour
{
get { return _startColour; }
set
{
_startColour = value;
rebuild();
}
}
public Color endColour
{
get { return _endColour; }
set
{
_endColour = value;
rebuild();
}
}
private MeshFilter _meshFilter;
void Awake()
{
_meshFilter = GetComponent<MeshFilter>();
_mesh = new Mesh();
_meshFilter.sharedMesh = _mesh;
_indices = new int[] { 0, 2, 1, 2, 0, 3 };
_vertices = new Vector3[4];
_colours = new Color[4];
rebuild();
}
// Catch an editor value change
void OnValidate()
{
// Prevent editor running code unless its ingame
if (_vertices != null && _vertices.Length > 0)
{
// rebuild();
}
}
private void rebuild()
{
_vertices[0] = new Vector3(-_endWidth, _length, 0);
_vertices[1] = new Vector3(-_startWidth, 0, 0);
_vertices[2] = new Vector3(_startWidth, 0, 0);
_vertices[3] = new Vector3(_endWidth, _length, 0);
_colours[0] = _endColour;
_colours[1] = _startColour;
_colours[2] = _startColour;
_colours[3] = _endColour;
_mesh.vertices = _vertices;
_mesh.colors = _colours;
_mesh.SetIndices(_indices, MeshTopology.Triangles, 0);
_mesh.RecalculateNormals();
_mesh.RecalculateBounds();
_mesh.Optimize();
}
}
}