Ejemplo n.º 1
0
    public LODOctreeNode GetNodeWithCPos(int cx, int cy, int cz, int lod)
    {
        int chunkNumX = LODOctreeMan._xChunkCount >> lod;
        int chunkNumY = LODOctreeMan._yChunkCount >> lod;
        int chunkNumZ = LODOctreeMan._zChunkCount >> lod;
        int cxround   = (cx >> lod) % chunkNumX;
        int cyround   = (cy >> lod) % chunkNumY;
        int czround   = (cz >> lod) % chunkNumZ;

        if (cxround < 0)
        {
            cxround += chunkNumX;
        }
        if (cyround < 0)
        {
            cyround += chunkNumY;
        }
        if (czround < 0)
        {
            czround += chunkNumZ;
        }
        LODOctreeNode curNode = _lodTreeNodes[lod][cxround, cyround, czround];

        if (curNode.CX == cx && curNode.CY == cy && curNode.CZ == cz)
        {
            return(curNode);
        }
        return(null);
    }
Ejemplo n.º 2
0
    public void DetachNodeEvents(DelegateNodeVisible visibleHandler,
                                 DelegateNodeInvisible invisibleHandler,
                                 DelegateNodeMeshCreated meshCreatedHandler,
                                 DelegateNodeMeshDestroy meshDestroyHandler,
                                 DelegateNodePhyxReady phyxReadyHandler,
                                 Vector3 pos, int nodeLod = 0)
    {
        LODOctreeNode node = GetParentNodeWithPos(pos, nodeLod);

        if (visibleHandler != null)
        {
            node.handlerVisible -= visibleHandler;
        }
        if (invisibleHandler != null)
        {
            node.handlerInvisible -= invisibleHandler;
        }
        if (meshCreatedHandler != null)
        {
            node.handlerMeshCreated -= meshCreatedHandler;
        }
        if (meshDestroyHandler != null)
        {
            node.handlerMeshDestroy -= meshDestroyHandler;
        }
        if (phyxReadyHandler != null)
        {
            node.handlerPhyxReady -= phyxReadyHandler;
        }
    }
Ejemplo n.º 3
0
    public bool IsPosValid(IntVector4 pos)
    {
        if (pos.w != 0)
        {
            return(false);
        }
        // lod 0
        LODOctreeNode node = GetNodeWithCPos(pos.x >> VoxelTerrainConstants._shift,
                                             pos.y >> VoxelTerrainConstants._shift,
                                             pos.z >> VoxelTerrainConstants._shift,
                                             pos.w);

        if (node == null)
        {
            return(false);
        }

        int n = node._data.Length;

        for (int i = 0; i < n; i++)
        {
            //TODO : code
            //if(!node._data[i]..IsReady)		return false;
        }
        return(true);
    }
Ejemplo n.º 4
0
 public void OnDestroy()
 {
     if (_root != null)
     {
         LODOctreeNode.DestroyNodeData(_root);
     }
 }
Ejemplo n.º 5
0
 public VFVoxelChunkData(LODOctreeNode node, byte[] dataArray, bool bFromPool = false)
 {
     _node      = node;
     _chunkData = dataArray;
     _bFromPool = bFromPool;
     BuildStep  = BuildStep_FinDataLoading;
 }
Ejemplo n.º 6
0
    public ILODNodeData CreateLODNodeData(LODOctreeNode node)
    {
        Block45LODNodeData data = new Block45LODNodeData(node);

        data.HelperProc = this;
        return(data);
    }
Ejemplo n.º 7
0
    public ILODNodeData CreateLODNodeData(LODOctreeNode node)
    {
        VFVoxelChunkData data = new VFVoxelChunkData(node);

        data.HelperProc = this;
        return(data);
    }
Ejemplo n.º 8
0
    private VFVoxelChunkData ForgeChunk(IntVector3 pos, int lod)
    {
        LODOctreeNode lodNode = new LODOctreeNode(null, lod,
                                                  pos.x << VoxelTerrainConstants._shift,
                                                  pos.y << VoxelTerrainConstants._shift,
                                                  pos.z << VoxelTerrainConstants._shift);
        VFVoxelChunkData cd = new VFVoxelChunkData(lodNode);

        cd.ChunkPosLod_w = new IntVector4(pos, lod);
        return(cd);
    }
Ejemplo n.º 9
0
 public void OnWriteVoxelOfTerrain(LODOctreeNode node, byte oldVol, byte newVol, int idxVol)
 {
     if (newVol < oldVol)
     {
         IntVector4 chunkPos = new IntVector4(node.CX, node.CY, node.CZ, node.Lod);
         if (!_fluidProcessor.DirtyChunkPosList.Contains(chunkPos))
         {
             _fluidProcessor.DirtyChunkPosList.Add(chunkPos);
         }
     }
 }
Ejemplo n.º 10
0
    public LODOctreeNode GetParentNodeWithPos(Vector3 pos, int nodeLod = 0)
    {
        int nX   = Mathf.FloorToInt(pos.x / _treeSideLen);
        int nY   = Mathf.FloorToInt(pos.y / _treeSideLen);
        int nZ   = Mathf.FloorToInt(pos.z / _treeSideLen);
        int idxX = nX % LODOctreeMan._xLodRootChunkCount;
        int idxY = nY % LODOctreeMan._yLodRootChunkCount;
        int idxZ = nZ % LODOctreeMan._zLodRootChunkCount;

        if (idxX < 0)
        {
            idxX += LODOctreeMan._xLodRootChunkCount;
        }
        if (idxY < 0)
        {
            idxY += LODOctreeMan._yLodRootChunkCount;
        }
        if (idxZ < 0)
        {
            idxZ += LODOctreeMan._zLodRootChunkCount;
        }
        int idx = (idxX * LODOctreeMan._yLodRootChunkCount + idxY) * LODOctreeMan._zLodRootChunkCount + idxZ;

        LODOctreeNode curNode = _lodTrees[idx]._root;

        if (nodeLod == LODOctreeMan._maxLod)
        {
            return(curNode);
        }

        IntVector3 ofsPos = new IntVector3(pos.x - nX * _treeSideLen, pos.y - nY * _treeSideLen, pos.z - nZ * _treeSideLen);

        for (int lod = LODOctreeMan._maxLod; lod > nodeLod; lod--)
        {
            int childLen = LODOctreeNode._halfLens[lod];
            int idxChild = 0;
            if (ofsPos.x >= childLen)
            {
                idxChild += 1; ofsPos.x -= childLen;
            }
            if (ofsPos.y >= childLen)
            {
                idxChild += 2; ofsPos.y -= childLen;
            }
            if (ofsPos.z >= childLen)
            {
                idxChild += 4; ofsPos.z -= childLen;
            }
            curNode = curNode._child[idxChild];
        }
        return(curNode);
    }
Ejemplo n.º 11
0
    public LODOctreeNode(LODOctreeNode parent, int lod, int posx, int posy, int posz)
    {
        _lod    = lod;
        _reqLod = 0xff;
        _posx   = posx;
        _posy   = posy;
        _posz   = posz;

        _parent = parent;
        _child  = null;
        initChildren();
        createNodeData();
    }
Ejemplo n.º 12
0
 private void TraverseFillTreeNodeArray(LODOctreeNode node, ref LODOctreeNode[][,,] lodTreeNodes, IntVector3 startIdx)
 {
     lodTreeNodes[node.Lod][startIdx.x, startIdx.y, startIdx.z] = node;
     if (node.Lod > 0)
     {
         for (int i = 0; i < 8; i++)
         {
             IntVector3 idx = new IntVector3((startIdx.x << 1) + (i & 1),
                                             (startIdx.y << 1) + ((i >> 1) & 1),
                                             (startIdx.z << 1) + ((i >> 2) & 1));
             TraverseFillTreeNodeArray(node._child[i], ref lodTreeNodes, idx);
         }
     }
 }
Ejemplo n.º 13
0
 public LODOctree(LODOctreeMan man, int maxlod, IntVector3 idxPos, IntVector3 sideLen)
 {
     _parentMan = man;
     //_maxLOD = maxlod;
     //_sideLen = sideLen;
     _idxPos = idxPos;
     _pos    = idxPos * sideLen;
     _root   = new LODOctreeNode(null, maxlod,
                                 (int)LODOctreeMan.InitPos.x,
                                 (int)LODOctreeMan.InitPos.y,
                                 (int)LODOctreeMan.InitPos.z);       // this pos can cause root to reposition.
     _rootHalfLen = LODOctreeNode._halfLens[_root.Lod];
     _rootCenter  = _root.Center;
 }
Ejemplo n.º 14
0
 private void ClearNode(LODOctreeNode node)
 {
     if (node._data [0].IsEmpty)
     {
         node._data = null;
     }
     if (node._child != null)
     {
         for (int i = 0; i < 8; i++)
         {
             ClearNode(node._child[i]);
         }
     }
 }
Ejemplo n.º 15
0
 public static void DestroyNodeData(LODOctreeNode node)
 {
     for (int i = 0; i < idxDataMax; i++)
     {
         node._data[i].OnDestroyNodeData();
     }
     node._data = null;
     if (node._child != null)
     {
         for (int i = 0; i < 8; i++)
         {
             DestroyNodeData(node._child[i]);
         }
     }
 }
Ejemplo n.º 16
0
    private void initChildren()
    {
        if (_lod == 0)
        {
            return;
        }

        _child = new LODOctreeNode[8];
        int childLen = _halfLens[_lod];

        for (int i = 0; i < 8; i++)
        {
            _child[i] = new LODOctreeNode(this, _lod - 1, _posx + childLen * (i & 1),
                                          _posy + childLen * ((i >> 1) & 1),
                                          _posz + childLen * ((i >> 2) & 1));
        }
    }
Ejemplo n.º 17
0
 // Methods
 public Block45LODNodeData(LODOctreeNode node)
 {
     _node = node;
 }
Ejemplo n.º 18
0
 // Methods
 public VFVoxelChunkData(LODOctreeNode node)
 {
     _node      = node;
     _chunkData = S_ChunkDataNull;
 }
Ejemplo n.º 19
0
 public ILODNodeData CreateLODNodeData(LODOctreeNode node)
 {
     return(null);
 }
Ejemplo n.º 20
0
    public LODOctreeMan(ILODNodeDataMan[] lodNodeDataMans, int maxlod, int refreshThreshold = 1, Transform observer = null)
    {
        self     = this;
        _maxLod  = maxlod;
        Observer = observer;

        int xChunkCount = LODOctreeMan._xChunkCount;
        int yChunkCount = LODOctreeMan._yChunkCount;
        int zChunkCount = LODOctreeMan._zChunkCount;
        int xVoxelCount = LODOctreeMan._xVoxelCount;
        int yVoxelCount = LODOctreeMan._yVoxelCount;
        int zVoxelCount = LODOctreeMan._zVoxelCount;

        _curCamPos           = InitPos;
        _lastCamPos          = InitPos;
        _sqrRefreshThreshold = refreshThreshold * refreshThreshold;
        _treeSideLen         = Lod0NodeSize << LODOctreeMan._maxLod;
        _viewBoundsSize      = new IntVector3(xVoxelCount, yVoxelCount, zVoxelCount);
        _viewBoundsLod       = new Bounds[LODOctreeMan._maxLod + 1];
        int lod = 0;

        for (lod = LODOctreeMan._maxLod; lod >= 0; lod--)
        {
            int shift = LODOctreeMan._maxLod - lod;
            _viewBoundsLod[lod] = new Bounds(InitPos,
                                             new Vector3(xVoxelCount >> shift,
                                                         yVoxelCount >> shift,
                                                         zVoxelCount >> shift));
        }

#if false
        lodNodeDataMans = new ILODNodeDataMan[] { VFVoxelTerrain.self
                                                  , VFVoxelWater.self
                                                  , Block45Man.self };
#endif
        LODOctreeNode.ClearNodeDataCreation();
        foreach (ILODNodeDataMan ndataMan in lodNodeDataMans)
        {
            LODOctreeNode.AddNodeDataCreation(ndataMan);
            ndataMan.LodMan   = this;
            _procPostInit    += ndataMan.ProcPostLodInit;
            _procPostUpdate  += ndataMan.ProcPostLodUpdate;
            _procPostRefresh += ndataMan.ProcPostLodRefresh;
        }
        LODOctreeNode.InitHalfLen();
        _lodTreeNodes = new LODOctreeNode[LODOctreeMan._maxLod + 1][, , ];
        for (lod = 0; lod <= LODOctreeMan._maxLod; lod++)
        {
            _lodTreeNodes[lod] = new LODOctreeNode[xChunkCount >> lod, yChunkCount >> lod, zChunkCount >> lod];
        }

        _lodTrees = new LODOctree[LODOctreeMan._xLodRootChunkCount *
                                  LODOctreeMan._yLodRootChunkCount *
                                  LODOctreeMan._zLodRootChunkCount];
        int i = 0;
        for (int x = 0; x < LODOctreeMan._xLodRootChunkCount; x++)
        {
            for (int y = 0; y < LODOctreeMan._yLodRootChunkCount; y++)
            {
                for (int z = 0; z < LODOctreeMan._zLodRootChunkCount; z++)
                {
                    _lodTrees[i] = new LODOctree(this, LODOctreeMan._maxLod,
                                                 new IntVector3(x, y, z),
                                                 new IntVector3(_treeSideLen, _treeSideLen, _treeSideLen));
                    _lodTrees[i].FillTreeNodeArray(ref _lodTreeNodes);
                    i++;
                }
            }
        }

        if (_procPostInit != null)
        {
            _procPostInit();
        }

#if LODREFRESH_THREADING
        StartLodThread();
#endif
    }
Ejemplo n.º 21
0
    public int Write(int x, int y, int z, VFVoxel voxel, int lod = 0, bool bUpdateLod = true)
    {
        _dirtyChunkList.Clear();

        int chunkNumX = LODOctreeMan._xChunkCount >> lod;
        int chunkNumY = LODOctreeMan._yChunkCount >> lod;
        int chunkNumZ = LODOctreeMan._zChunkCount >> lod;
        int cx        = x >> VoxelTerrainConstants._shift;
        int cy        = y >> VoxelTerrainConstants._shift;
        int cz        = z >> VoxelTerrainConstants._shift;
        int cxround   = (cx >> lod) % chunkNumX;
        int cyround   = (cy >> lod) % chunkNumY;
        int czround   = (cz >> lod) % chunkNumZ;

        if (cxround < 0)
        {
            cxround += chunkNumX;
        }
        if (cyround < 0)
        {
            cyround += chunkNumY;
        }
        if (czround < 0)
        {
            czround += chunkNumZ;
        }
        int              vx        = (x >> lod) & VoxelTerrainConstants._mask;
        int              vy        = (y >> lod) & VoxelTerrainConstants._mask;
        int              vz        = (z >> lod) & VoxelTerrainConstants._mask;
        LODOctreeNode    node      = _lodNodes[lod][cxround, cyround, czround];
        VFVoxelChunkData chunkData = (VFVoxelChunkData)node._data[_idxData];
        IntVector4       poslod    = chunkData.ChunkPosLod;

        if (poslod == null || poslod.x != cx || poslod.y != cy || poslod.z != cz)
        {
            return(0);
        }

        VFVoxelChunkData.ProcOnWriteVoxel onWriteVoxel = null;
        if (_idxData == 0 && VFVoxelWater.self != null)
        {
            onWriteVoxel = VFVoxelWater.self.OnWriteVoxelOfTerrain;
        }
        if (!chunkData.WriteVoxelAtIdx(vx, vy, vz, voxel, bUpdateLod, onWriteVoxel))
        {
            return(0x100);             //1<<8
        }
        _dirtyChunkList.Add(chunkData);
        int fx = 0, fy = 0, fz = 0;
        int dirtyMask = 0x80;           // bit 0,1,2 for xyz dirty mask;bit 4,5,6  for sign(neg->1);bit 7 for current pos(now not used)

        // Note: To write at lod boundary will cause wrong chunk writing because cxround/cyround/czround is incorrect.
        // To write edge voxel will cause neibour chunks being modified
        if (vx < VFVoxelChunkData.S_MinNoDirtyIdx)
        {
            fx = -1; dirtyMask |= 0x11;
        }
        else
        if (vx >= VFVoxelChunkData.S_MaxNoDirtyIdx)
        {
            fx = 1; dirtyMask |= 0x01;
        }
        if (vy < VFVoxelChunkData.S_MinNoDirtyIdx)
        {
            fy = -1; dirtyMask |= 0x22;
        }
        else
        if (vy >= VFVoxelChunkData.S_MaxNoDirtyIdx)
        {
            fy = 1; dirtyMask |= 0x02;
        }
        if (vz < VFVoxelChunkData.S_MinNoDirtyIdx)
        {
            fz = -1; dirtyMask |= 0x44;
        }
        else
        if (vz >= VFVoxelChunkData.S_MaxNoDirtyIdx)
        {
            fz = 1; dirtyMask |= 0x04;
        }

        if (dirtyMask != 0x80)
        {
            int cxlod = (cx >> lod);
            int cylod = (cy >> lod);
            int czlod = (cz >> lod);
            for (int i = 1; i < 8; i++)
            {
                if ((dirtyMask & i) == i)
                {
                    int dx = fx * VFVoxelChunkData.S_NearChunkOfs[i, 0], dy = fy * VFVoxelChunkData.S_NearChunkOfs[i, 1], dz = fz * VFVoxelChunkData.S_NearChunkOfs[i, 2];
                    cxround = (cxlod + dx) % chunkNumX;
                    cyround = (cylod + dy) % chunkNumY;
                    czround = (czlod + dz) % chunkNumZ;
                    if (cxround < 0)
                    {
                        cxround += chunkNumX;
                    }
                    if (cyround < 0)
                    {
                        cyround += chunkNumY;
                    }
                    if (czround < 0)
                    {
                        czround += chunkNumZ;
                    }
                    chunkData = (VFVoxelChunkData)_lodNodes[lod][cxround, cyround, czround]._data[_idxData];
                    if (!chunkData.WriteVoxelAtIdx(
                            vx - dx * VoxelTerrainConstants._numVoxelsPerAxis,
                            vy - dy * VoxelTerrainConstants._numVoxelsPerAxis,
                            vz - dz * VoxelTerrainConstants._numVoxelsPerAxis, voxel, bUpdateLod))
                    {
                        dirtyMask |= 1 << (i + 8);                      // flag for unsuccessful write
                    }
                    else
                    {
                        _dirtyChunkList.Add(chunkData);
                    }
                }
            }
        }
        return(dirtyMask);
    }
Ejemplo n.º 22
0
    public void AddOctNewNodeToAttach(Block45OctNode octNode)
    {
        LODOctreeNode node = LodMan.GetParentNodeWithPos(octNode._pos.ToVector3(), octNode._pos.w);

        octNode.AttachLODNode((Block45LODNodeData)node._data[IdxInLODNodeData]);
    }