- Home /
culling issues on dynamically generated mesh
i am using a mesh to display gun trails in my game. i am updating only the vertex positions when a new trail is created, uvs and triangles are consistent.
the problem is that it disappears at certain camera angles / positions, i think the issue is occlusion / frustrum culling.
in 2017.2 i could solve this by disabling occlusion culling in the mesh renderer component.
unfortunately, as of 2017.3 the mesh is still culled.
what is the correct way to solve this?
can i disable culling for the object or can i update the culling box via script at runtime?
script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class gunTrail : MonoBehaviour {
[RequireComponent(typeof(MeshFilter))]
public int maxtrails = 6;
public float trailWidth = .01f;
int nextTrail = 0;
bool Trailchanges = false;
Vector3[] trailstart;
Vector3[] traildirection;
Vector3[] vertices;
Vector2[] uvs;
int[] triangles;
Mesh mesh;
void Start () {
mesh = new Mesh ();
GetComponent<MeshFilter> ().mesh = mesh;
trailstart = new Vector3[maxtrails];
traildirection = new Vector3[maxtrails];
vertices = new Vector3[maxtrails*3];
triangles = new int[maxtrails*3*2];
uvs = new Vector2[vertices.Length];
for (int i = 0; i < maxtrails; i++) {
vertices [i * 3 + 0] = new Vector3 (0, -10, 0);
vertices [i * 3 + 1] = new Vector3 (0, -10, 0);
vertices [i * 3 + 2] = new Vector3 (0, -10, 0);
triangles [i * 6 + 0] = i * 3;
triangles [i * 6 + 1] = i * 3 + 1;
triangles [i * 6 + 2] = i * 3 + 2;
triangles [i * 6 + 3] = i * 3;
triangles [i * 6 + 4] = i * 3 + 2;
triangles [i * 6 + 5] = i * 3 + 1;
uvs [i * 3 + 0] = new Vector2 (0f, 0f); //wil be first start point
uvs [i * 3 + 1] = new Vector2 (1f, .5f); //will be end point
uvs [i * 3 + 2] = new Vector2 (0f, 1f); //will be 2nd start point
}
mesh.vertices = vertices;
mesh.uv = uvs;
mesh.triangles = triangles;
}
void FixedUpdate () {
if (Trailchanges) {
mesh.vertices = vertices;
//mesh.triangles = triangles;
Trailchanges = false;
}
}
public void makeTrail(Vector3 start, Vector3 direction){
trailstart [nextTrail] = start;
traildirection [nextTrail] = direction;
if (false) {
vertices [nextTrail * 3] = start;
vertices [nextTrail * 3 + 1] = start + direction;
vertices [nextTrail * 3 + 2] = start + new Vector3 (0f, trailWidth, 0f);
} else {
vertices [nextTrail * 3] = start + direction;
vertices [nextTrail * 3 + 1] = start;
vertices [nextTrail * 3 + 2] = start + direction + new Vector3 (0f, trailWidth, 0f);
}
Trailchanges = true;
nextTrail++;
nextTrail = nextTrail % maxtrails;
}
}
Answer by BloodMarked · Jan 21, 2018 at 12:41 PM
mesh.RecalculateBounds();
now i feel dumb for even asking : |
but if anyone knows a way to disable culling on a mesh i would appreciate it
No, you can't disable frustum culling. However you could assign a custom bounds that is large enought so it's always in view. However in most cases any kind of mesh as some limits so it would be a good idea to set a bounds that actually fits the largest possible bounding volume your mesh might have.
Note that occlusion culling has nothing to do with frustum culling.
Thanks! This helps me a lot, now I feel dumb for getting stuck at this too :|