#Change mesh in runtime

1 messages · Page 1 of 1 (latest)

candid spear
#

I have tried every way I can think of, but I cannot replace a mesh in runtime.

I use the following code, code too long so I can only use pastebin:
https://pastebin.com/9iUb2wQ4

First I tried the code in the commented part to use RenderMeshUtility.AddComponents, then I tried to use UntypedWeakReferenceId but it seems I need to link UntypedWeakReferenceId to an existing object.

Can anyone help me out?

solemn girder
#

didn't read through your long code 😄 but this is what I do:

var terrainResultMesh = new Mesh();
terrainResultMesh.SetVertices(verticesArray);
terrainResultMesh.SetIndices(trianglesArray, MeshTopology.Triangles, 0);                
terrainResultMesh.RecalculateNormals();
var meshID = hybridRenderer.RegisterMesh(terrainResultMesh);
RefRW<MaterialMeshInfo> terrainMaterialMeshInfo = SystemAPI.GetComponentRW<MaterialMeshInfo>(entity, false);
terrainMaterialMeshInfo.ValueRW.MeshID = meshID;
SystemAPI.SetComponent(entity, new RenderBounds { Value = AABBExtensions.ToAABB(terrainResultMesh.bounds) });```
candid spear
#

Interesting, I should try this!

stiff lagoon
#

hey im trying to create a cubemesh for a set of positions, then combine all those meshes into a single one. Then i want to create a Mesh Collider and add this to a single Entity. I've implemented the following but when I trigger it my new Physics Collider has no Geometry.

` if (Input.GetKeyDown(KeyCode.R))
{

             CombineInstance[] combine = new CombineInstance[entityArray.Length];
             
            for (int i = 0; i < entityArray.Length; i++)
            {
                var entityPosition = transformArray[i];
                combine[i].mesh = mesh;
                combine[i].transform = entityPosition.ToMatrix();
                
         
                
            }
            
            
            
            Mesh completeMesh = new Mesh();
            completeMesh.CombineMeshes(combine);
        
            var meshID =   hybridRenderer.RegisterMesh(completeMesh);
           
            
            var colliderBlob=  MeshCollider.Create(completeMesh,CollisionFilter.Default,Material.Default);
            var collider = new PhysicsCollider() { Value = colliderBlob, };
          
            
            
            RefRW<MaterialMeshInfo> callowMaterialMeshInfo = SystemAPI.GetComponentRW<MaterialMeshInfo>(entityArray[0]);
            callowMaterialMeshInfo.ValueRW.MeshID = meshID;
            
            SystemAPI.SetComponent(entityArray[0], new RenderBounds { Value = AABBExtensions.ToAABB(mesh.bounds) });
          
           SystemAPI.SetComponent<PhysicsCollider>(entityArray[0], collider);
           var comp =  SystemAPI.GetComponent<PhysicsCollider>(entityArray[0]);
     
           
    }`
#

i'll know it works when I can see the collider from the Physics Debug Display.