Exemplo n.º 1
0
    public void DisplayArea()
    {
        if (area != null) {
            DestroyImmediate (area);
        }

        area = new GameObject ("Area");
        area.hideFlags = HideFlags.HideInHierarchy;
        area.transform.parent = transform;
        area.transform.localPosition = Vector3.zero;
        area.transform.localRotation = Quaternion.identity;

        int numVoxels = queryMask.Length;
        if (numVoxels > 1820) {
            Debug.LogError ("number of voxels cannot be > 1820");
            enabled = false;
            return;
        }

        ProceduralMesh mesh = new ProceduralMesh (numVoxels * 24, numVoxels * 36);
        float yOffset = (-queryHeight * 0.5f) * VoxelEngine.instance.voxelSize + VoxelEngine.instance.halfVoxelSize;
        float zOffset = (-queryDepth * 0.5f) * VoxelEngine.instance.voxelSize + VoxelEngine.instance.halfVoxelSize;
        float xOffset = (-queryWidth * 0.5f) * VoxelEngine.instance.voxelSize + VoxelEngine.instance.halfVoxelSize;
        Vector3 yStart = new Vector3 (0.0f, yOffset, 0.0f);
        int queryDepth_x_queryWidth = queryDepth * queryWidth;
        for (int y = 0; y < queryHeight; y++) {
            int y1 = y * queryDepth_x_queryWidth;
            Vector3 zStart = new Vector3 (0.0f, 0.0f, zOffset);
            for (int z = 0; z < queryDepth; z++) {
                int z1 = z * queryWidth;
                Vector3 xStart = new Vector3 (xOffset, 0.0f, 0.0f);
                for (int x = 0; x < queryWidth; x++) {
                    Vector3 center = yStart + zStart + xStart;
                    if (queryMask [y1 + z1 + x]) {
                        ProceduralMeshes.CreateCube (mesh, VoxelEngine.instance.voxelSize, VoxelEngine.instance.voxelSize, VoxelEngine.instance.voxelSize, center);
                    }
                    xStart += VoxelEngine.instance.right;
                }
                zStart += VoxelEngine.instance.forward;
            }
            yStart += VoxelEngine.instance.up;
        }
        Mesh areaMesh = mesh.Build ();

        MeshFilter meshFilter;
        if ((meshFilter = area.GetComponent<MeshFilter> ()) == null) {
            meshFilter = area.AddComponent<MeshFilter> ();
        }
        meshFilter.sharedMesh = areaMesh;

        MeshRenderer meshRenderer;
        if ((meshRenderer = area.GetComponent<MeshRenderer> ()) == null) {
            meshRenderer = area.AddComponent<MeshRenderer> ();
        }
        meshRenderer.sharedMaterial = GetVoxelRemoverMaterial ();
    }
Exemplo n.º 2
0
    public void Build()
    {
        if (width < 1 || height < 1 || depth < 1) {
            // Debug.LogError ("width or height or depth < 1");
            enabled = false;
            return;
        }

        if (data == null) {
            // Debug.LogError ("data == null");
            enabled = false;
            return;
        }

        int numVoxels = data.Length;
        if (numVoxels > 1820) {
            // Debug.LogError ("number of voxels cannot be > 1820");
            enabled = false;
            return;
        }

        depth_x_width = depth * width;

        float halfWidth = width * 0.5f;
        float halfHeight = height * 0.5f;
        float halfDepth = depth * 0.5f;

        offset = new Vector3 (halfWidth * VoxelEngine.instance.voxelSize, halfHeight * VoxelEngine.instance.voxelSize, halfDepth * VoxelEngine.instance.voxelSize);

        ProceduralMesh mesh = new ProceduralMesh (numVoxels * 24, numVoxels * 36);
        float yOffset = -halfHeight * VoxelEngine.instance.voxelSize + VoxelEngine.instance.halfVoxelSize;
        float zOffset = -halfDepth * VoxelEngine.instance.voxelSize + VoxelEngine.instance.halfVoxelSize;
        float xOffset = -halfWidth * VoxelEngine.instance.voxelSize + VoxelEngine.instance.halfVoxelSize;
        Vector3 yStart = new Vector3 (0.0f, yOffset, 0.0f);
        Rect[] uvRects = new Rect[6];
        for (int y = 0; y < height; y++) {
            int y1 = y * depth_x_width;
            Vector3 zStart = new Vector3 (0.0f, 0.0f, zOffset);
            for (int z = 0; z < depth; z++) {
                int z1 = z * width;
                Vector3 xStart = new Vector3 (xOffset, 0.0f, 0.0f);
                for (int x = 0; x < width; x++) {
                    Vector3 center = yStart + zStart + xStart;
                    byte voxelId = data [y1 + z1 + x].id;
                    if (voxelId > 0) {
                        int excludeFaces = 0;

                        if (HasFrontNeighbor (x, y1, z)) {
                            excludeFaces |= (int)FaceDirection.FRONT;
                        }

                        bool hasTopNeighbor;
                        if ((hasTopNeighbor = HasTopNeighbor (x, y, z1))) {
                            excludeFaces |= (int)FaceDirection.TOP;
                        }

                        if (HasRightNeighbor (x, y1, z1)) {
                            excludeFaces |= (int)FaceDirection.RIGHT;
                        }

                        if (HasBottomNeighbor (x, y, z1)) {
                            excludeFaces |= (int)FaceDirection.BOTTOM;
                        }

                        if (HasBackNeighbor (x, y1, z)) {
                            excludeFaces |= (int)FaceDirection.BACK;
                        }

                        if (HasLeftNeighbor (x, y1, z1)) {
                            excludeFaces |= (int)FaceDirection.LEFT;
                        }

                        int frontFaceTileId, topFaceTileId, rightFaceTileId, backFaceTileId, bottomFaceTileId, leftFaceTileId;
                        VoxelEngine.instance.GetVoxelFaceMapping (voxelId, out frontFaceTileId, out topFaceTileId, out rightFaceTileId, out backFaceTileId, out bottomFaceTileId, out leftFaceTileId, hasTopNeighbor);

                        // front
                        uvRects [0] = VoxelEngine.instance.GetTileUv (frontFaceTileId);
                        // top
                        uvRects [1] = VoxelEngine.instance.GetTileUv (topFaceTileId);
                        // right
                        uvRects [2] = VoxelEngine.instance.GetTileUv (rightFaceTileId);
                        // back
                        uvRects [3] = VoxelEngine.instance.GetTileUv (backFaceTileId);
                        // bottom
                        uvRects [4] = VoxelEngine.instance.GetTileUv (bottomFaceTileId);
                        // left
                        uvRects [5] = VoxelEngine.instance.GetTileUv (leftFaceTileId);

                        ProceduralMeshes.CreateCube (mesh, VoxelEngine.instance.voxelSize, VoxelEngine.instance.voxelSize, VoxelEngine.instance.voxelSize, center, uvRects, excludeFaces);
                    } else {
                        ProceduralMeshes.CreateCube (mesh, VoxelEngine.instance.voxelSize, VoxelEngine.instance.voxelSize, VoxelEngine.instance.voxelSize, center, EMPTY_UV_RECTS, EXCLUDE_ALL_FACES);
                    }
                    xStart += VoxelEngine.instance.right;
                }
                zStart += VoxelEngine.instance.forward;
            }
            yStart += VoxelEngine.instance.up;
        }
        chunkMesh = mesh.Build ();
        chunkMesh.MarkDynamic ();

        if ((meshFilter = gameObject.GetComponent<MeshFilter> ()) == null) {
            meshFilter = gameObject.AddComponent<MeshFilter> ();
        }
        meshFilter.sharedMesh = chunkMesh;

        if ((meshCollider = gameObject.GetComponent<MeshCollider> ()) == null) {
            meshCollider = gameObject.AddComponent<MeshCollider> ();
        }
        meshCollider.sharedMesh = chunkMesh;

        MeshRenderer meshRenderer;
        if ((meshRenderer = gameObject.GetComponent<MeshRenderer> ()) == null) {
            meshRenderer = gameObject.AddComponent<MeshRenderer> ();
        }
        meshRenderer.sharedMaterial = VoxelEngine.instance.atlas;

        built = true;
    }