- Home /
Need help with combining meshes
I'm working on a game in which you build by planting bricks (like Legos), and am trying to make the building system as efficient as possible by grouping bricks together into large meshes (other techniques will be added later). So far, I have it set up to where you position a pre-fab brick object (modeled in Blender), and then the mesh data is referenced and combined with an existing mesh when planted.
The problem I'm having is that when the brick meshes are combined, only the bottom faces show up; the brick object does have a mesh filter set up, so it should be working. The brick also has 3 different materials applied, but I don't think that would cause a problem.
I tried using a cube primitive instead of the brick object, and it seems to work fine:
I'm not sure why I'm having this issue when merging brick meshes. Here is the script used (written in JavaScript):
public var Brick : GameObject;
public var superShift : boolean = false;
public var offset_init : float = 0.0;
private var BricksMesh : Mesh;
private var BricksMeshGO : GameObject;
function Start()
{
BricksMesh = Mesh();
BricksMesh.name = "BricksMesh";
BricksMesh.MarkDynamic();
BricksMeshGO = new GameObject("BricksMeshGO", MeshRenderer, MeshFilter, MeshCollider);
BricksMeshGO.GetComponent(MeshFilter).mesh = BricksMesh;
BricksMeshGO.GetComponent(MeshCollider).sharedMesh = BricksMesh;
BricksMeshGO.GetComponent(Renderer).material.color = Color.white;
}
function Update()
{
var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast (ray, hit) && hit.collider != null)
Brick.transform.position = Vector3(Mathf.Round(hit.point.x), Mathf.Round(hit.point.y + offset_init), Mathf.Round(hit.point.z));
if(Input.GetKeyDown(KeyCode.KeypadEnter))
PlantBrick();
}
function PlantBrick()
{
var combine : CombineInstance[] = new CombineInstance[2];
combine[0].mesh = Brick.GetComponent.<MeshFilter>().mesh;
combine[0].transform = Brick.GetComponent.<MeshFilter>().transform.localToWorldMatrix;
combine[1].mesh = BricksMeshGO.GetComponent.<MeshFilter>().mesh;
combine[1].transform = BricksMeshGO.GetComponent.<MeshFilter>().transform.localToWorldMatrix;
BricksMeshGO.GetComponent.<MeshFilter>().mesh = new Mesh();
BricksMeshGO.GetComponent.<MeshFilter>().mesh.CombineMeshes(combine);
BricksMeshGO.GetComponent(MeshCollider).sharedMesh = BricksMeshGO.GetComponent.<MeshFilter>().sharedMesh;
}
Answer by ASFghost · Jul 08, 2017 at 11:05 PM
I figured it out - both meshes need to have the exact same material applied; it's working great now.
Your answer
Follow this Question
Related Questions
BCE0019 "enabled" is not a member of "Object"(On JavaScript Script) 2 Answers
I need help with a CharacterMotor script error in Unity5. This is formatted in Javascript. 0 Answers
How do perform the operations as per the commands at client side in socket unity? 0 Answers
Mini Game Timer - Unity 2017 0 Answers
Why am I only allowed to trigger my dash script once? 2 Answers