- Home /
change pivot after Mesh.CombineMeshes?
How can I change pivot and delete instances after Mesh.CombineMeshes? I want the pivot to be set by a specific position from another object, which will get the mesh assigned to it in its mesh filter.
If I understand correctly, to change pivot you'd need to move all vertices of resulting mesh by difference of current pivot and pivot you want. You might want to call Combine$$anonymous$$eshes from mesh that already has pivot point you would need for performance.
By delete instances you mean delete $$anonymous$$eshes that were combined? Then you just call Destroy(...) on them. $$anonymous$$ake sure they are not used anywhere before doing it.
When I combine the meshes the pivot is way wrong. Both the assigned mesh and the meshes that are instantiated have no parent. Im not sure how I should be setting up this part of the documentation in this case, note that this isnt like my code but it is what I am following:
using UnityEngine;
using System.Collections;
[RequireComponent(typeof($$anonymous$$eshFilter))]
[RequireComponent(typeof($$anonymous$$eshRenderer))]
public class ExampleClass : $$anonymous$$onoBehaviour {
void Start() {
$$anonymous$$eshFilter[] meshFilters = GetComponentsInChildren<$$anonymous$$eshFilter>();
CombineInstance[] combine = new CombineInstance[meshFilters.Length];
int i = 0;
while (i < meshFilters.Length) {
combine[i].mesh = meshFilters[i].shared$$anonymous$$esh;//should I use shared mesh or just mesh if I am selectively combining instances?
combine[i].transform = meshFilters[i].transform.localToWorld$$anonymous$$atrix;//would you still use this in cases where there is no parent object? And is there a way here to change the pivot to a specific Vector3?
meshFilters[i].gameObject.active = false;
i++;
}
transform.GetComponent<$$anonymous$$eshFilter>().mesh = new $$anonymous$$esh();
transform.GetComponent<$$anonymous$$eshFilter>().mesh.Combine$$anonymous$$eshes(combine);
transform.gameObject.active = true;
}
}
I am using priciples behind this, to try to offset and move the instance so that it ends up where it was to begin with.
void Start() {
mf = GetComponent<$$anonymous$$eshFilter>();
origVerts = mf.mesh.vertices;
newVerts = new Vector3[origVerts.Length];
}
void Update() {
Quaternion rotation = Quaternion.Euler(eulerAngles.x, eulerAngles.y, eulerAngles.z);
$$anonymous$$atrix4x4 m = $$anonymous$$atrix4x4.TRS(translation, rotation, scale);
int i = 0;
while (i < origVerts.Length) {
newVerts[i] = m.$$anonymous$$ultiplyPoint3x4(origVerts[i]);
//but ins$$anonymous$$d just doing like newVerts[i] += offset
i++;
}
mf.mesh.vertices = newVerts;
}
But I'm not quite sure how I should be calculating the offset to get it back where it needs to be.
$$anonymous$$esh.Combine$$anonymous$$eshes applies supplied matrix to coresponding mesh's vertex(it takes only those it needs from sub$$anonymous$$esh you've specified) and writes them into $$anonymous$$esh that called that method.
So, if object that renders resulting mesh is in point (1,0,0) and you add mesh into $$anonymous$$esh.Combine$$anonymous$$eshes using Transform.localToWorld$$anonymous$$atrix of Transform with position (0,0,1) then your resulting vertex positions will be offset by (1,0,1) from (0,0,0) in world space or (0,0,1) from basis of mesh you write to. In that case if initially vertice is (0,1,0) then its ending position will be (1,1,1) in world space or (0,1,1) in local(mesh) space.
If you use that method on objects with different initial position, rotation and scale and need to keep vertex positions in world space (like static batching), you should supply combiningInto.transform.worldToLocal$$anonymous$$atrix*combiningThis.transform.localToWorld$$anonymous$$atrix;
as matrix where combiningInto is the GameObject that you will assign mesh to, combiningThis is GameObject which mesh you take into combine.
And as for how much to offset, that depends on what exactly you want. As you're applying rotation and scale too, it's most likely not as simple as "+=offset", you would need to use matrices to move point properly.
Your answer
Follow this Question
Related Questions
skinned mesh renderer, combine meshes can't see the result, what am I doing wrong/ missing? 0 Answers
CombineMeshes doesnt combine properly 0 Answers
Need Help with Dynamically Building a Mesh 0 Answers
Does using the same Mesh variable for multiple Mesh Filters take up more memory? 1 Answer
CombineMeshes Error on Run-time Meshes 2 Answers