Ejemplo n.º 1
0
    public void VoxelizeMesh()
    {
        if (mesh[0] == null)
        {
            return;
        }
        if (!objectHasChanged)
        {
            return;
        }

        Transform[] ts = gameObject.GetComponentsInChildren <Transform>();
        for (int i = 1; i < ts.Length; ++i)
        {
            Destroy(ts[i].gameObject);
        }
        STLLoader.Vec3[] direction = new STLLoader.Vec3[3];
        direction[0] = new STLLoader.Vec3(1, 0, 0);
        direction[1] = new STLLoader.Vec3(0, 1, 0);
        direction[2] = new STLLoader.Vec3(0, 0, 1);


        voxelizer = new STLLoader.Voxelizer(xResolution, yResolution, zResolution);
        voxelizer.MultiyDirectionsVoxelize(mesh[0], direction);
        List <STLLoader.Vec3> localCorners = voxelizer.CalculateLocalCorners();

        STLLoader.Quickhull quickhull   = new STLLoader.Quickhull();
        List <int>          convexFaces = new List <int>();

        quickhull.CalculateQuickhull(localCorners, ref convexFaces);
        convexHullOutline.Clear();
        quickhull.CalculatePolygonEdges(ref convexHullOutline);
        STLLoader.Vec3 centerOfMass = voxelizer.GetCenterOfMass();

        //voxelizer.SafeLayer(fileName, 0);
        voxelMesh = new STLLoader.Mesh[1];

        voxelMesh[0] = voxelizer.GenerateMesh(0, resolution);

        float voxelSize = voxelizer.GetVoxelSize();

        STLLoader.Vec3 offset = voxelizer.GetMinOffset();

        convexHull.ConfigureConvexHull(ref localCorners, ref convexFaces, voxelSize, new Vector3(offset.x, offset.y, offset.z), ref convexHullOutline);


        CreateIndexedMesh(ref voxelMesh, (int)resolution);

        transform.localScale = new Vector3(voxelSize, voxelSize, voxelSize);
        transform.position   = new Vector3(offset.x, offset.y, offset.z);


        objectHasChanged    = false;
        voxelMeshHasChanged = false;
    }
Ejemplo n.º 2
0
    void Update()
    {
        if (!showPlane)
        {
            changeUpLayer   = 0;
            changeDownLayer = 0;
            if (Input.GetKeyDown("w"))
            {
                changeUpLayer       = maxLayer <= resolution ? ++changeUpLayer : changeUpLayer;
                voxelMeshHasChanged = true;
            }

            if (Input.GetKeyDown("escape"))
            {
                Application.Quit();
            }

            if (Input.GetKeyDown("e"))
            {
                changeDownLayer     = minLayer < maxLayer ? ++changeDownLayer : changeDownLayer;
                voxelMeshHasChanged = true;
            }
            if (Input.GetKeyDown("s"))
            {
                changeUpLayer       = maxLayer > minLayer ? --changeUpLayer : changeUpLayer;
                voxelMeshHasChanged = true;
            }
            if (Input.GetKeyDown("d"))
            {
                changeDownLayer     = minLayer > 0 ? --changeDownLayer : changeDownLayer;
                voxelMeshHasChanged = true;
            }

            if (voxelMeshHasChanged)
            {
                RecreateMesh();
                voxelMeshHasChanged = false;
            }
            if (Input.GetKeyDown(KeyCode.LeftControl))
            {
                chooseConvexHullObject = true;
                convexHull.gameObject.SetActive(true);
                convexHullEdges.isActive = true;
            }
            if (Input.GetKeyUp(KeyCode.LeftControl))
            {
                chooseConvexHullObject = false;
                convexHull.gameObject.SetActive(false);
                convexHullEdges.isActive = false;
            }


            if (Input.GetMouseButtonDown(0))
            {
                if (chooseConvexHullObject)
                {
                    Vector3 p1 = new Vector3();
                    Vector3 p2 = new Vector3();
                    Vector3 p3 = new Vector3();
                    if (convexHull.FindFaceOnConvexHull(ref p1, ref p2, ref p3))
                    {
                        float          scale  = voxelizer.GetSizeVoxel();
                        STLLoader.Vec3 offset = voxelizer.GetMinOffset();


                        STLLoader.Vec3 voxelP1 = new STLLoader.Vec3(p1.x, p1.y, p1.z);
                        STLLoader.Vec3 voxelP2 = new STLLoader.Vec3(p2.x, p2.y, p2.z);
                        STLLoader.Vec3 voxelP3 = new STLLoader.Vec3(p3.x, p3.y, p3.z);

                        List <STLLoader.Vec3> voxelPlane = voxelizer.GetAllVoxelOnPlane(voxelP1, voxelP2, voxelP3, false);
                        for (int i = 0; i < voxelPlane.Count; ++i)
                        {
                            GameObject     ins1       = Instantiate <GameObject>(testCube);
                            STLLoader.Vec3 minOffset1 = voxelizer.GetMinOffset();
                            float          sizeVoxel1 = voxelizer.GetSizeVoxel();
                            ins1.transform.position   = new Vector3((voxelPlane[i].x + 0.5f) * sizeVoxel1 + minOffset1.x, (voxelPlane[i].y + 0.5f) * sizeVoxel1 + minOffset1.y, (voxelPlane[i].z + 0.5f) * sizeVoxel1 + minOffset1.z);
                            ins1.transform.localScale = new Vector3(sizeVoxel1 + 0.2f * sizeVoxel1, sizeVoxel1 + 0.1f * sizeVoxel1, sizeVoxel1 + 0.1f * sizeVoxel1);
                            ins1.transform.parent     = transform;
                        }
                        startPlane = voxelPlane;
                    }
                }
                else if (Input.GetKey(KeyCode.LeftAlt))
                {
                    PickVoxel();
                }
            }
            if (Input.GetKey(KeyCode.LeftAlt) && Input.GetMouseButtonDown(1))
            {
                RemoveUnderPlane();
            }
        }
    }