Example #1
0
    // voxelArray is only used if createIfMissing is true
    private static Voxel SearchDown(OctreeNode node, Vector3Int position, bool createIfMissing, VoxelArray voxelArray)
    {
        VoxelComponent useComponent = null;

        while (node.size != 1)
        {
            // initial root node is larger than COMPONENT_BLOCK_SIZE so a component node should always be found
            if (createIfMissing && useComponent == null && node.size == COMPONENT_BLOCK_SIZE)
            {
                if (node.voxelComponent == null || node.voxelComponent.isDestroyed)
                {
                    GameObject voxelObject = new GameObject();
                    voxelObject.transform.parent = voxelArray.transform;
                    voxelObject.name             = node.position.ToString();
                    node.voxelComponent          = voxelObject.AddComponent <VoxelComponent>();
                }
                useComponent = node.voxelComponent;
            }

            int        halfSize = node.size / 2;
            bool       xLarge   = position.x >= node.position.x + halfSize;
            bool       yLarge   = position.y >= node.position.y + halfSize;
            bool       zLarge   = position.z >= node.position.z + halfSize;
            int        branchI  = (xLarge ? 1 : 0) + (yLarge ? 2 : 0) + (zLarge ? 4 : 0);
            OctreeNode branch   = node.branches[branchI];
            if (branch == null)
            {
                if (!createIfMissing)
                {
                    return(null);
                }
                Vector3Int branchPos = new Vector3Int(
                    node.position.x + (xLarge ? halfSize : 0),
                    node.position.y + (yLarge ? halfSize : 0),
                    node.position.z + (zLarge ? halfSize : 0)
                    );
                branch = new OctreeNode(branchPos, halfSize);
                node.branches[branchI] = branch;
                branch.parent          = node;
            }
            node = branch;
        }

        if (!createIfMissing)
        {
            return(node.voxel);
        }
        else if (node.voxel != null)
        {
            return(node.voxel);
        }
        else
        {
            Voxel newVoxel = voxelArray.InstantiateVoxel(position, useComponent);
            node.voxel          = newVoxel;
            newVoxel.octreeNode = node;
            return(newVoxel);
        }
    }
Example #2
0
    private void ReadVoxel(JSONObject voxelObject, VoxelArray voxelArray,
                           List <Material> materials, List <Substance> substances)
    {
        if (voxelObject["at"] == null)
        {
            return;
        }
        Vector3 position = ReadVector3(voxelObject["at"].AsArray);
        Voxel   voxel    = null;

        if (!editor)
        {
            // slightly faster -- doesn't add to octree
            voxel = voxelArray.InstantiateVoxel(position);
        }
        else
        {
            voxel = voxelArray.VoxelAt(position, true);
        }

        if (voxelObject["s"] != null)
        {
            voxel.substance = substances[voxelObject["s"].AsInt];
        }

        if (voxelObject["f"] != null)
        {
            foreach (JSONNode faceNode in voxelObject["f"].AsArray)
            {
                JSONObject faceObject = faceNode.AsObject;
                if (fileWriterVersion == 0)
                {
                    // faces were oriented differently. Get voxel for each face
                    int faceI = faceObject["i"].AsInt;
                    voxel = voxelArray.VoxelAt(position + Voxel.DirectionForFaceI(faceI), true);
                }
                ReadFace(faceObject, voxel, materials);
            }
        }

        if (voxelObject["e"] != null)
        {
            foreach (JSONNode edgeNode in voxelObject["e"].AsArray)
            {
                ReadEdge(edgeNode.AsObject, voxel);
            }
        }
        voxel.UpdateVoxel();
    }
    private void ReadVoxel(MessagePackObject voxelObj, VoxelArray voxelArray,
                           List <Material> materials, List <Material> overlays, List <Substance> substances)
    {
        var voxelList = voxelObj.AsList();

        if (voxelList.Count == 0)
        {
            return;
        }

        Vector3 position = ReadVector3(voxelList[0]);
        Voxel   voxel    = null;

        if (!editor)
        {
            // slightly faster -- doesn't add to octree
            voxel = voxelArray.InstantiateVoxel(position);
        }
        else
        {
            voxel = voxelArray.VoxelAt(position, true);
        }

        if (voxelList.Count >= 2)
        {
            foreach (var faceObj in voxelList[1].AsList())
            {
                ReadFace(faceObj, voxel, materials, overlays);
            }
        }

        if (voxelList.Count >= 3 && voxelList[2].AsInt32() != -1)
        {
            voxel.substance = substances[voxelList[2].AsInt32()];
        }

        if (voxelList.Count >= 4)
        {
            foreach (var edgeObj in voxelList[3].AsList())
            {
                ReadEdge(edgeObj, voxel);
            }
        }

        voxel.UpdateVoxel();
    }