- Home /
Procedural Mesh has weird offset
Hi
Since I couldn´t find a satisfying soltion to my problem, I´ll simply ask. I´m trying to generate a mesh (since it´s my first only a triangle for now). The triangle is being displayed but not at the position I would expect it. I added my script to an empty and actually was expecting that the triangle apperas right at the empty´s position. Well it´s not.
Here´s the code:
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
[System.Serializable]
[RequireComponent(typeof(MeshRenderer))]
[RequireComponent(typeof(MeshFilter))]
public class AdaptiveMeshTile : MonoBehaviour {
public int scale = 1;
public Material material;
private Mesh tileMesh;
// Use this for initialization
void Start () {
this.tileMesh = new Mesh();
}
// Update is called once per frame
void Update () {
Vector3[] vertices = new Vector3[]
{
this.transform.InverseTransformPoint(new Vector3(this.transform.position.x, 0, this.transform.position.z)),
this.transform.InverseTransformPoint(new Vector3(this.transform.position.x + scale, 0, this.transform.position.z)),
this.transform.InverseTransformPoint(new Vector3(this.transform.position.x, 0, this.transform.position.z + scale))
};
Vector2[] uv = new Vector2[]
{
new Vector2(0, 256), new Vector2(256, 256), new Vector2(256, 0)
};
int[] tris = new int[]
{
2,1,0
};
Debug.Log(this.tileMesh);
this.tileMesh.vertices = vertices;
this.tileMesh.uv = uv;
this.tileMesh.triangles = tris;
this.GetComponent<MeshFilter>().mesh = this.tileMesh;
this.GetComponent<MeshRenderer>().material = this.material;
}
}
And here´s my result:
I´d rather expect something like this (Done by empty overlap to recreate the desired effect):
Disclaimer: I´m not good at math so eventual calculations should be explained in detail xD
Thanks in advance.
Answer by Glurth · Sep 08, 2016 at 08:09 PM
Honestly, I'm not quite sure if this applies in your situation. Hopefully it will help:
Usually, you specify the coordinates of the triangle's vertices in (no-transform) model-space. Then allow the object's transform itself, to transform each of those points in the rendering engine. This way, you do NOT need to modify the vertices every cycle, (unless the mesh changes shape, of course). You can instead, simply manipulate the object's transform during update. Also, this lets you use the one single mesh for all objects with that shape, at different scales, rotations and positions.
What this means, is instead of using, for a vertex
this.transform.InverseTransformPoint(new Vector3(this.transform.position.x, 0, this.transform.position.z)),
you can instead, simply use
(1,0,1)
(which the rendering engine will then pass through the TransformPoint() function, behind the scenes)
Regarding the offset: it looks like that depends on your starting this.transform.position
Awkwardly enough it worked as soon as I started unity again. Looks like I can't have a dynamic parameter for the vertex positions because when the mesh is created and the parameter changes it gets screwed up. But if I change the default value and reattach the script it works as expected.