Reputation: 1
enter image description here I have merged two meshes into one, as shown in the image. However, I want the created mesh to have a single origin point. How can I create the combined mesh with vertices centered around one origin point? In the image, there are two points.
I have this script for combining meshes:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Splines;
public class MeshCalculations : SingletonMonoBehaviour<MeshCalculations>
{
public GameObject Zone01;
public GameObject Zone02;
public Mesh createdMesh;
private void Update()
{
if (Input.GetKeyDown(KeyCode.V))
{
MergeMeshes();
}
}
public void MergeMeshes()
{
// Get MeshFilters from the two GameObjects
MeshFilter meshFilter1 = Zone01.GetComponent<MeshFilter>();
MeshFilter meshFilter2 = Zone02.GetComponent<MeshFilter>();
if (meshFilter1 == null || meshFilter2 == null)
{
Debug.LogError("One of the objects does not have a MeshFilter!");
return;
}
// CombineInstance array to hold the meshes
CombineInstance[] combine = new CombineInstance[2];
// Add the first mesh
combine[0].mesh = meshFilter1.sharedMesh;
combine[0].transform = meshFilter1.transform.localToWorldMatrix;
// Add the second mesh
combine[1].mesh = meshFilter2.sharedMesh;
combine[1].transform = meshFilter2.transform.localToWorldMatrix;
// Create a new mesh
Mesh combinedMesh = new Mesh();
combinedMesh.CombineMeshes(combine);
// Create a new GameObject to hold the combined mesh
GameObject combinedObject = new GameObject("CombinedMesh");
combinedObject.transform.position = Vector3.zero;
combinedObject.transform.rotation = Quaternion.identity;
// Add a MeshFilter and MeshRenderer to the new GameObject
MeshFilter combinedMeshFilter = combinedObject.AddComponent<MeshFilter>();
combinedMeshFilter.mesh = combinedMesh;
MeshRenderer combinedMeshRenderer = combinedObject.AddComponent<MeshRenderer>();
combinedMeshRenderer.sharedMaterial = Zone01.GetComponent<MeshRenderer>().sharedMaterial;
createdMesh = combinedMesh;
}
}
Upvotes: 0
Views: 25