Пример #1
0
    static void Report(VoxelizedMesh vm, float voxelResolution)
    {
        int voxelResolutionX = (int)voxelResolution / 8;

        for (int x = 0; x < voxelResolutionX; x++)
        {
            for (int y = 0; y < voxelResolution; y++)
            {
                for (int z = 0; z < voxelResolution; z++)
                {
                    Debug.Log(vm.volume[x, y, z]);
                }
            }
        }
    }
Пример #2
0
    void OnSceneGUI()
    {
        VoxelizedMesh voxelizedMesh = target as VoxelizedMesh;

        Handles.color = Color.green;
        float size = voxelizedMesh.HalfSize * 2f;

        foreach (Vector3Int gridPoint in voxelizedMesh.GridPoints)
        {
            Vector3 worldPos = voxelizedMesh.PointToPosition(gridPoint);
            Handles.DrawWireCube(worldPos, new Vector3(size, size, size));
        }

        Handles.color = Color.red;
        if (voxelizedMesh.TryGetComponent(out MeshCollider meshCollider))
        {
            Bounds bounds = meshCollider.bounds;
            Handles.DrawWireCube(bounds.center, bounds.extents * 2);
        }
    }
Пример #3
0
    VoxelizedMesh VoxelizeMesh(Transform t, float voxelResolution, int voxelizeLayer)
    {
        // TODO ray cast from right / left and top/ down
        Physics.queriesHitBackfaces = false;

        MeshRenderer mr = t.GetComponent <MeshRenderer>();

        if (mr == null)
        {
            return(null);
        }

        MeshFilter mf = t.GetComponent <MeshFilter>();

        if (mf == null)
        {
            return(null);
        }

        Mesh mesh = mf.sharedMesh;

        if (mesh == null)
        {
            return(null);
        }

        VoxelizedMesh vm = new VoxelizedMesh();

        voxelizedLookup[mesh] = vm;

        Transform  oldParent = t.parent;
        Vector3    oldPos    = t.position;
        Quaternion oldRot    = t.rotation;
        Vector3    oldScale  = t.localScale;

        t.parent     = null;
        t.position   = Vector3.zero;
        t.rotation   = Quaternion.identity;
        t.localScale = Vector3.one;
        int oldLayer = t.gameObject.layer;

        t.gameObject.layer = voxelizeLayer;

        LayerMask voxelizeLayerMask = 1 << voxelizeLayer;

        Bounds bounds = mr.bounds;

        Vector3 size   = bounds.size;
        Int3    voxels = new Int3(Mathf.CeilToInt(size.x / voxelResolution), Mathf.CeilToInt(size.y / voxelResolution), Mathf.CeilToInt(size.z / voxelResolution));

        voxels += new Int3(2, 2, 2);
        int voxelsX = Mathf.CeilToInt(voxels.x / 8f);

        vm.voxels = voxels;

        size        = new Vector3(voxels.x * voxelResolution, voxels.y * voxelResolution, voxels.z * voxelResolution);
        bounds.size = size;
        vm.bounds   = bounds;

        byte[,,] volume = new byte[voxelsX, voxels.y, voxels.z];

        Ray ray  = new Ray();
        Ray ray2 = new Ray();

        ray.direction  = Vector3.forward;
        ray2.direction = Vector3.back;
        Vector3 pos  = bounds.min;
        Vector3 pos2 = pos;

        pos2.z = bounds.max.z;

        Debug.Log(PrintVector3(mr.bounds.size) + " new size " + PrintVector3(size) + " voxels " + voxels.ToString());
        int voxelCount = 0;

        Vector3 halfVoxel      = Vector3.one * voxelResolution * 0.5f;
        Vector3 minBoundsVoxel = pos + halfVoxel;

        // voxels.y = 2;

        try
        {
            for (int x = 0; x < voxels.x; x++)
            {
                int  xGrid = x / 8;
                byte bit   = bits[x - (xGrid * 8)];

                for (int y = 0; y < voxels.y; y++)
                {
                    Vector3 origin = pos + new Vector3((x + 0.5f) * voxelResolution, (y + 0.5f) * voxelResolution, 0);
                    ray.origin  = origin;
                    origin.z    = pos2.z;
                    ray2.origin = origin;

                    intersectList.Clear();

                    MultiCast(ray, intersectList, 0.001f, size.z, voxelizeLayerMask);
                    MultiCast(ray2, intersectList, -0.001f, size.z, voxelizeLayerMask);

                    intersectList.Sort();

                    float half = (float)intersectList.Count / 2;
                    if (half != (int)half)
                    {
                        continue;
                    }

                    // Debug.Log(hitInfos.Length + " " + hitInfos2.Length + " " + list.Count);

                    for (int i = 0; i < intersectList.Count; i += 2)
                    {
                        int z1 = Mathf.RoundToInt((intersectList[i] - pos.z) / voxelResolution);
                        int z2 = Mathf.RoundToInt((intersectList[i + 1] - pos.z) / voxelResolution);

                        for (int z = z1; z < z2; z++)
                        {
                            Vector3 voxelPos = new Vector3(x * voxelResolution, y * voxelResolution, z * voxelResolution) + minBoundsVoxel;
                            voxelPos = t.TransformPoint(voxelPos);

                            volume[xGrid, y, z] |= bit;
                            ++voxelCount;
                            //if (!Physics.CheckBox(voxelPos, halfVoxel, Quaternion.identity, voxelizeLayerMask))
                            //{
                            //    volume[xGrid, y, z] |= bit;
                            //    ++voxelCount;
                            //}
                        }
                    }
                }
            }
        }
        catch (Exception e)
        {
            Debug.LogError(e.ToString());
        }

        Debug.Log(t.name + " voxels " + voxelCount);

        vm.volume = volume;

        t.gameObject.layer = oldLayer;
        t.parent           = oldParent;
        t.position         = oldPos;
        t.rotation         = oldRot;
        t.localScale       = oldScale;

        return(vm);
    }