private MeshVoxelizer m_voxelizer; // calling the Meshvoxelizer script which is in the scripts folder /Assets

        void Start()
        {
            RenderEvent.AddRenderEvent(Camera.main, DrawOutline);  // notify camera of render event; needed in the main camera to render the voxels

            MeshFilter   filter   = GetComponent <MeshFilter>();   // takes the mesh filter of mesh this script is attached to
            MeshRenderer renderer = GetComponent <MeshRenderer>(); // takes the mesh renderer of mesh this script is attached to

            if (filter == null || renderer == null)                // if the mesh is not present in the parent then check in the children for meshes
            {
                filter   = GetComponentInChildren <MeshFilter>();  // same as above
                renderer = GetComponentInChildren <MeshRenderer>();
            }

            if (filter == null || renderer == null)
            {
                return;
            }

            renderer.enabled = false;                                                                      //set renderer to false?

            Mesh     mesh = filter.mesh;                                                                   // derives only the mesh from the mesh filter
            Material mat  = renderer.material;                                                             // derives material from renderer

            Box3 bounds = new Box3(mesh.bounds.min, mesh.bounds.max);                                      // check for bounding box's bounds; Box3 is a function defined in the current namespace; mesh.bounds: This is the axis-aligned bounding box of the mesh in its local space (that is, not affected by the transform)

            m_voxelizer = new MeshVoxelizer(size, size, size);                                             // voxelize mesh with given size resolution
            m_voxelizer.Voxelize(mesh.vertices, mesh.triangles, bounds);                                   // access the .Voxelize public void function in MeshVoxelizer.cs; takes in the mesh's vertices triangles and bounds

            Vector3 scale = new Vector3(bounds.Size.x / size, bounds.Size.y / size, bounds.Size.z / size); // scale of each individual voxel in local space
            Vector3 m     = new Vector3(bounds.Min.x, bounds.Min.y, bounds.Min.z);

            mesh = CreateMesh(m_voxelizer.Voxels, scale, m);//come back to this


            //assign local positionns, scale and rotations to the voxelized mesh
            GameObject go = new GameObject("Voxelized");

            go.transform.parent        = transform;// transform of parent applied to voxelized child gameobject "voxelized" and is instantiated at the position of parent
            go.transform.localPosition = Vector3.zero;
            go.transform.localScale    = Vector3.one;
            go.transform.localRotation = Quaternion.identity;

            filter   = go.AddComponent <MeshFilter>();
            renderer = go.AddComponent <MeshRenderer>();

            filter.mesh       = mesh; // assign the new mesh created after voxelization to the mesh filter
            renderer.material = mat;  // do the same for material as well
        }
Esempio n. 2
0
        void Start()
        {
            RenderEvent.AddRenderEvent(Camera.main, DrawOutline);

            MeshFilter   filter   = GetComponent <MeshFilter>();
            MeshRenderer renderer = GetComponent <MeshRenderer>();

            if (filter == null || renderer == null)
            {
                filter   = GetComponentInChildren <MeshFilter>();
                renderer = GetComponentInChildren <MeshRenderer>();
            }

            if (filter == null || renderer == null)
            {
                return;
            }

            renderer.enabled = false;

            Mesh     mesh = filter.mesh;
            Material mat  = renderer.material;

            Box3 bounds = new Box3(mesh.bounds.min, mesh.bounds.max);

            m_voxelizer = new MeshVoxelizer(size, size, size);
            m_voxelizer.Voxelize(mesh.vertices, mesh.triangles, bounds);

            Vector3 scale = new Vector3(bounds.Size.x / size, bounds.Size.y / size, bounds.Size.z / size);
            Vector3 m     = new Vector3(bounds.Min.x, bounds.Min.y, bounds.Min.z);

            mesh = CreateMesh(m_voxelizer.Voxels, scale, m);

            GameObject go = new GameObject("Voxelized");

            go.transform.parent        = transform;
            go.transform.localPosition = Vector3.zero;
            go.transform.localScale    = Vector3.one;
            go.transform.localRotation = Quaternion.identity;

            filter   = go.AddComponent <MeshFilter>();
            renderer = go.AddComponent <MeshRenderer>();

            filter.mesh       = mesh;
            renderer.material = mat;
        }