Greetings,
I’m trying to recreate what was presented in the talk of @JoachimHolmer about ‘spline-based procedual geometry’ from Unite 2015.
The Talk:
The Slides (heavy images!): A coder's guide to spline-based procedural geometry - Google Slides
problem is, when I move or rotate the gameobject and then update the mesh, the mesh doubles everything I do:
Code (I will provide more if needed):
public struct OrientedPoint {
public Vector3 position;
public Quaternion rotation;
public OrientedPoint(Vector3 pos, Quaternion rot) {
position = pos;
rotation = rot;
}
public Vector3 LocalToWorld(Vector3 point) {
return position + rotation * point;
}
public Vector3 WorldToLocal(Vector3 point) {
return Quaternion.Inverse(rotation) * (point - position);
}
public Vector3 LocalToWorldDirection(Vector3 dir) {
return rotation * dir;
}
}
(curve = OrientedPoint[ ])
private void SetMesh() {
if (curveOK) {
int vertsInShape = shape.vertices.Length;
int edgeLoops = curve.Length;
int vertCount = vertsInShape * edgeLoops;
int triCount = shape.lines.Length * segments;
int[] triIndices = new int[triCount * 3];
Vector3[] vertices = new Vector3[vertCount];
Vector3[] normals = new Vector3[vertCount];
Vector2[] uvs = new Vector2[vertCount];
for (int i = 0; i < curve.Length; i++) {
int offset = i * vertsInShape;
for (int n = 0; n < vertsInShape; n++) {
int id = offset + n;
vertices[id] = curve[i].LocalToWorld(shape.vertices[n]);
normals[id] = curve[i].LocalToWorldDirection(shape.normals[n]);
uvs[id] = new Vector2(shape.us[n], i / (float)edgeLoops);
}
}
int ti = 0;
for (int i = 0; i < segments; i++) {
int offset = i * vertsInShape;
for (int n = 0; n < shape.lines.Length; n += 2) {
int a = offset + shape.lines[n] + vertsInShape;
int b = offset + shape.lines[n];
int c = offset + shape.lines[n + 1];
int d = offset + shape.lines[n + 1] + vertsInShape;
triIndices[ti] = c; ti++;
triIndices[ti] = a; ti++;
triIndices[ti] = b; ti++;
triIndices[ti] = a; ti++;
triIndices[ti] = c; ti++;
triIndices[ti] = d; ti++;
}
}
Mesh mesh = new Mesh();
mesh.name = "CurvedMesh";
mesh.vertices = vertices;
mesh.triangles = triIndices;
mesh.normals = normals;
mesh.uv = uvs;
mesh.RecalculateNormals();
filter.sharedMesh.Clear();
filter.sharedMesh = mesh;
}
}
Tryed several things, and I think it has a problem with me also moving the curve corners (the grey and blue circles in the video. they are also gameobjects.). If I leave them unparented and don’t move them the mesh wont displace (when moving. rotating makes crazy things with the mesh that way). Any ideas how to fix this?