Example #1
0
    public ChunkData(ChunkData data)
    {
        position = data.position;
        chunk    = data.chunk;
        map      = data.map;

        int        x, y, z, w;
        VoxelState voxel;
        Vector4Int pos;

        for (w = 0; w < cDepth; w++)
        {
            for (x = 0; x < cWidth; x++)
            {
                for (y = 0; y < cHeight; y++)
                {
                    for (z = 0; z < cLength; z++)
                    {
                        pos = new Vector4Int(x, y, z, w);

                        voxel = setVoxel(pos, new VoxelState(getVoxel(pos), this));

                        for (int p = 0; p < 8; p++)
                        {
                            neighbor           = pos + VoxelData.faceChecks4[p];
                            voxel.neighbors[p] = getVoxel(neighbor) ??
                                                 World.Instance.worldData.GetVoxel(global_pos + neighbor);
                        }
                    }
                }
            }
        }

        Post_Populate();
    }
Example #2
0
    void UpdateSurroundingVoxels(Vector4Int voxel)
    {
        Vector4Int current;
        Chunk      chunk;

        for (int p = 0; p < 8; p++)
        {
            current = voxel + VoxelData.faceChecks4[p];

            if (!World.Instance.IsVoxelInWorld(current) ||
                chunkData.IsVoxelInChunk(current))
            {
                continue;
            }

            chunk = World.Instance.GetChunkFromVector4(
                current + Helper.floorVector4I(position)
                );
            if (chunk != null)
            {
                World.Instance.AddChunkToUpdate(chunk, true);
            }
            else
            {
                log("UpdateSurroundingVoxels: chunk null!\n" + current.ToString());
            }
        }
    }
Example #3
0
 public static void SetVector4Int(this SerializedProperty property, Vector4Int vector4Int)
 {
     property.FindPropertyRelative("m_X").intValue = vector4Int.x;
     property.FindPropertyRelative("m_Y").intValue = vector4Int.y;
     property.FindPropertyRelative("m_Z").intValue = vector4Int.z;
     property.FindPropertyRelative("m_W").intValue = vector4Int.w;
 }
Example #4
0
    public VoxelState GetVoxelFromGlobalVector4(Vector4Int pos)
    {
        // World.Instance.log("" + pos.x + "\t" + pos.y + "\t" + pos.z);
        pos -= Helper.floorVector4I(position);

        return(getVoxel(pos));
    }
Example #5
0
    public void ModifyVoxel(Vector4Int pos, byte id)
    {
        VoxelState voxel = getVoxel(pos);

        if (voxel.blockID == id)
        {
            return;
        }

        BlockType new_voxel = World.Instance.blockTypes[id];

        // Cache old opacity
        byte oldOpacity = voxel.properties.opacity;

        voxel.blockID = id;

        if (voxel.properties.opacity != oldOpacity &&
            (pos.y == ChunkData.cHeight - 1 || getVoxel(pos + Vector4Int.up).light == 15))
        {
            Lighting.CastNaturalLight(this, pos);
        }

        World.Instance.worldData.AddToModifieds(this);

        if (chunk != null)
        {
            World.Instance.AddChunkToUpdate(chunk);
        }
    }
Example #6
0
 public static void Write(this BinaryWriter writer, Vector4Int vec)
 {
     writer.Write(vec.x);
     writer.Write(vec.y);
     writer.Write(vec.z);
     writer.Write(vec.w);
 }
Example #7
0
    /* public byte lightLevel {
     *      get { return (byte) Mathf.Round(globalLightPercent * (float) VoxelData.lightLevels); }
     * } */

    public VoxelState(byte id, ChunkData chunk, Vector4Int pos)
    {
        blockID   = id;
        position  = pos;
        neighbors = new VoxelNeighbors(this);
        chunkData = chunk;
        light     = 0;
    }
Example #8
0
 public VoxelState(VoxelState voxel, ChunkData chunk)
 {
     blockID   = voxel.blockID;
     position  = voxel.position;
     neighbors = new VoxelNeighbors(this);
     chunkData = chunk;
     light     = 0;
 }
Example #9
0
	bool IsVoxelInWorld(Vector4Int pos){
		return (
			pos.x >= 0 && pos.x < wsiv_x &&
			pos.y >= 0 && pos.y < cHeight &&
			pos.z >= 0 && pos.z < wsiv_z &&
			pos.w >= 0 && pos.w < wsiv_w
		);
	}
        public WindowProcedure(Vector4Int borderSize, int captionHeight)
        {
            BorderSize    = borderSize;
            CaptionHeight = captionHeight;

            HandleWindow();
            InitializeWindowProcedure();
        }
Example #11
0
 public bool IsVoxelInChunk(Vector4Int pos)
 {
     return(!(
                pos.x < 0 || pos.x >= cWidth ||
                pos.y < 0 || pos.y >= cHeight ||
                pos.z < 0 || pos.z >= cLength ||
                pos.w < 0 || pos.w >= cDepth
                ));
 }
Example #12
0
    public static Vector4Int operator *(Vector4Int v1, int v2)
    {
        Vector4Int result = Vector4Int.zero;

        result.x = v1.x * v2;
        result.y = v1.y * v2;
        result.z = v1.z * v2;
        result.w = v1.w * v2;
        return(result);
    }
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            EditorGUI.BeginChangeCheck();
            Vector4Int value = EditorExtensions.Vector4IntField(position, label, property.GetVector4Int());

            if (EditorGUI.EndChangeCheck())
            {
                property.SetVector4Int(value);
            }
        }
Example #14
0
    public static IEnumerable <Tile> GetCellsInCross(this Tile source, Vector4Int directions)
    {
        var list = new List <Tile>();

        list.AddRange(source.GetCellsInLine(directions.left, Vector2Int.left));
        list.AddRange(source.GetCellsInLine(directions.right, Vector2Int.right));
        list.AddRange(source.GetCellsInLine(directions.up, Vector2Int.up));
        list.AddRange(source.GetCellsInLine(directions.down, Vector2Int.down));

        return(list);
    }
Example #15
0
        public static Vector4Int Vector4IntField(
            Rect position,
            GUIContent label,
            Vector4Int value)
        {
            int controlId = GUIUtility.GetControlID(s_FoldoutHash, FocusType.Keyboard, position);

            position        = MultiFieldPrefixLabel(position, controlId, label, 4);
            position.height = EditorGUIUtility.singleLineHeight;
            return(Vector4IntField(position, value));
        }
Example #16
0
    public void EditVoxel(Vector3Int pos, byte newID)
    {
        // Coordenada do bloco
        Vector4Int pos1 = Helper.floorVector4I(pos);

        // Coordenada do bloco na chunk
        pos1 -= Helper.floorVector4I(position);

        chunkData.ModifyVoxel(pos1, newID);

        UpdateSurroundingVoxels(pos1);
    }
Example #17
0
    public Chunk GetChunkFromVector4(Vector4Int pos)
    {
        if (pos.y < 0 || pos.y >= cHeight)
        {
            return(null);
        }

        int x = Helper.floor(pos.x / cWidth),
            z = Helper.floor(pos.z / cLength),
            w = Helper.floor(pos.w / cDepth);

        return(getChunk(x, z, w));
    }
Example #18
0
    public VoxelState setVoxel(Vector4Int pos, byte id)
    {
        if (pos.x < 0 || pos.x >= cWidth ||
            pos.y < 0 || pos.y >= cHeight ||
            pos.z < 0 || pos.z >= cLength ||
            pos.w < 0 || pos.w >= cDepth)
        {
            return(null);
        }
        VoxelState voxel = map[pos.x, pos.y, pos.z, pos.w] ?? new VoxelState(id, this, pos);

        voxel.blockID = id;
        return(map[pos.x, pos.y, pos.z, pos.w] = voxel);
    }
Example #19
0
	public void SetVoxel(Vector4 pos, byte value){
		if(!IsVoxelInWorld(pos))
			return;
		
		int x = Helper.floor(pos.x / cWidth) * cWidth,
			 z = Helper.floor(pos.z / cLength) * cLength,
			 w = Helper.floor(pos.w / cDepth) * cDepth;
		
		ChunkData chunk = RequestChunk(new Vector3Int(x, z, w), true);
		
		Vector4Int voxel_pos = new Vector4Int(pos.x - x, pos.y, pos.z - z, pos.w - w);
		
		chunk.ModifyVoxel(voxel_pos, value);
	}
Example #20
0
    ChunkCoord GetChunkCoordFromVector4(Vector4Int pos)
    {
        int x = pos.x / cWidth,
            z = pos.z / cLength,
            w = pos.w / cDepth;

        if (x < 0 || x >= wsic_x ||
            z < 0 || z >= wsic_z ||
            w < 0 || w >= wsic_w)
        {
            log("Vector " + pos.ToString() + " tried to create ChunkCoord at " + x + "; " + z + "; " + w);
        }
        return(new ChunkCoord(x, z, w));
    }
Example #21
0
 public static void BeginGroup(string name, Vector4Int minMax, bool beginHorizontal)
 {
     if (beginHorizontal)
     {
         GUILayout.BeginHorizontal();
     }
     EditorGUILayout.BeginVertical(GUI.skin.box,
                                   GUILayout.MinWidth(minMax.x),
                                   GUILayout.MinHeight(minMax.y),
                                   GUILayout.MaxWidth(minMax.z),
                                   GUILayout.MaxHeight(minMax.w));
     if (!string.IsNullOrEmpty(name))
     {
         EditorGUILayout.LabelField(name, EditorStyles.boldLabel);
     }
 }
Example #22
0
    void Awake()
    {
        objBuffer = new ComputeBuffer(characterPoints.Length, sizeof(float));
        Matrix4x4[] bindPosesArray = targetMesh.bindposes;
        bindPoseCount = bindPosesArray.Length;
        bonesBuffer   = new ComputeBuffer(bindPoseCount * characterPoints.Length, sizeof(Matrix3x4));
        bindBuffer    = new ComputeBuffer(bindPoseCount, sizeof(Matrix3x4));
        NativeArray <Matrix3x4> bindNative = new NativeArray <Matrix3x4>(bindPoseCount, Allocator.Temp, NativeArrayOptions.UninitializedMemory);
        Matrix3x4 *bindPtr = bindNative.Ptr();

        for (int i = 0; i < bindPoseCount; ++i)
        {
            *bindPtr = new Matrix3x4(ref bindPosesArray[i]);
            bindPtr++;
        }
        bindBuffer.SetData(bindNative);
        bindNative.Dispose();
        int[]     triangles = targetMesh.triangles;
        Vector3[] vertices  = targetMesh.vertices;
        //Vector3[] normals = targetMesh.normals;
        //Vector4[] tangents = targetMesh.tangents;
        BoneWeight[] weights = targetMesh.boneWeights;
        Vector2[]    uv      = targetMesh.uv;

        // Debug.LogError("triangles   " + triangles.Length);
        // Debug.LogError("vertices   " + vertices.Length);
        //Debug.LogError("normals   " + normals.Length);
        //Debug.LogError("tangents   " + tangents.Length);
        //  Debug.LogError("weights   " + weights.Length);
        //  Debug.LogError("uv   " + uv.Length);

        NativeArray <SkinPoint> allSkinPoints = new NativeArray <SkinPoint>(vertices.Length, Allocator.Temp, NativeArrayOptions.UninitializedMemory);
        SkinPoint *pointsPtr = allSkinPoints.Ptr();

        for (int i = 0; i < vertices.Length; ++i)
        {
            SkinPoint *currentPtr = pointsPtr + i;
            int        index      = i;
            currentPtr->position = vertices[index];
            Vector3 a = currentPtr->position;
            //currentPtr->tangent = tangents[index];
            //currentPtr->normal = normals[index];
            ref BoneWeight bone = ref weights[index];
            currentPtr->boneWeight = new Vector4(bone.weight0, bone.weight1, bone.weight2, bone.weight3);
            currentPtr->boneIndex  = new Vector4Int(bone.boneIndex0, bone.boneIndex1, bone.boneIndex2, bone.boneIndex3);
            currentPtr->uv         = uv[index];
        }
Example #23
0
	public VoxelState GetVoxel(Vector4 pos){
		if(!IsVoxelInWorld(pos))
			return null;
		
		int x = Helper.floor(pos.x / cWidth) * cWidth,
			 z = Helper.floor(pos.z / cLength) * cLength,
			 w = Helper.floor(pos.w / cDepth) * cDepth;
		
		ChunkData chunk = RequestChunk(new Vector3Int(x, z, w), false);
		
		if(chunk == null) return null;
		
		Vector4Int voxel = new Vector4Int(pos.x - x, pos.y, pos.z - z, pos.w - w);
		
		return chunk.getVoxel(voxel);
		// return chunk?.getVoxel(voxel);
	}
Example #24
0
        private static Vector4Int Vector4IntField(Rect position, Vector4Int value)
        {
            s_Vector4Ints[0] = value.x;
            s_Vector4Ints[1] = value.y;
            s_Vector4Ints[2] = value.z;
            s_Vector4Ints[3] = value.w;
            position.height  = EditorGUIUtility.singleLineHeight;
            EditorGUI.BeginChangeCheck();
            EditorGUI.MultiIntField(position, s_XYZWLabels, s_Vector4Ints);
            if (EditorGUI.EndChangeCheck())
            {
                value.x = s_Vector4Ints[0];
                value.y = s_Vector4Ints[1];
                value.z = s_Vector4Ints[2];
                value.w = s_Vector4Ints[3];
            }

            return(value);
        }
Example #25
0
    public void Populate()
    {
        int x, y, z, w;
        int cWidth  = ChunkData.cWidth,
            cHeight = ChunkData.cHeight,
            cLength = ChunkData.cLength,
            cDepth  = ChunkData.cDepth;
        Vector4Int pos,
                   neighbor,
                   global_pos = globalPosition;
        VoxelState voxel;

        for (w = 0; w < cDepth; w++)
        {
            for (x = 0; x < cWidth; x++)
            {
                for (y = 0; y < cHeight; y++)
                {
                    for (z = 0; z < cLength; z++)
                    {
                        pos = new Vector4Int(x, y, z, w);

                        voxel = setVoxel(pos, World.Instance.GetVoxel(pos + global_pos));

                        for (int p = 0; p < 8; p++)
                        {
                            neighbor = pos + VoxelData.faceChecks4[p];

                            /* voxel.neighbors[p] = IsVoxelInChunk(neighbor) ? getVoxel(neighbor) :
                             *      World.Instance.worldData.GetVoxel(neighbor); */
                            voxel.neighbors[p] = getVoxel(neighbor) ??
                                                 World.Instance.worldData.GetVoxel(global_pos + neighbor) /*  ?? null */;
                        }
                    }
                }
            }
        }

        Post_Populate();
    }
Example #26
0
 public void SetValue(Vector4Int value)
 {
     OpenGL.glUniform4i(location, value.x, value.y, value.z, value.w);
 }
Example #27
0
 public static void CastNaturalLight(ChunkData chunk, Vector4Int pos)
 {
     CastNaturalLight(chunk, pos.x, pos.z, pos.w, pos.y);
 }
Example #28
0
    public void DoChunkUpdate()
    {
        System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
        sw.Start();

        Vector3 pos        = transform.position;
        float   updateTime = Time.realtimeSinceStartup;

        int numChunksAdded = 0;
        int minCoord       = -radius + 1;
        int maxCoord       = radius + 1;

        Vector3Int prevMin        = Vector3Int.zero;
        Vector3Int prevMax        = Vector3Int.zero;
        int        chunkArraySize = 4 * radius;

        for (int lod = 0; lod < lods; lod++)
        {
            int scale     = (int)Mathf.Pow(2, lod);
            int chunkSize = minimumChunkSize * scale;
            int snapSize  = lod == (lods - 1) ? chunkSize : chunkSize * 2;

            Vector3Int snapped = new Vector3Int(
                (int)Mathf.Floor((pos.x + chunkSize / 2) / snapSize) * snapSize,
                (int)Mathf.Floor((pos.y + chunkSize / 2) / snapSize) * snapSize,
                (int)Mathf.Floor((pos.z + chunkSize / 2) / snapSize) * snapSize);

            if (lod == 0)
            {
                if (initialized && snapped == previousPosition)
                {
                    return;                     // snapped position didn't change, so no new chunks will be added
                }
                previousPosition = snapped;
                initialized      = true;
            }

            Vector3Int min = Vector3Int.one * ((minCoord - 1) * chunkSize) + snapped;
            Vector3Int max = Vector3Int.one * ((maxCoord - 2) * chunkSize) + snapped;

            Vector3Int chunkPos = Vector3Int.zero;
            for (int x = minCoord; x < maxCoord; x++)
            {
                chunkPos.x = ((x - 1) * chunkSize) + snapped.x;
                bool xInBounds = chunkPos.x >= prevMin.x && chunkPos.x <= prevMax.x;

                for (int y = minCoord; y < maxCoord; y++)
                {
                    chunkPos.y = ((y - 1) * chunkSize) + snapped.y;
                    bool yInBounds = chunkPos.y >= prevMin.y && chunkPos.y <= prevMax.y;

                    for (int z = minCoord; z < maxCoord; z++)
                    {
                        chunkPos.z = ((z - 1) * chunkSize) + snapped.z;
                        bool zInBounds = chunkPos.z >= prevMin.z && chunkPos.z <= prevMax.z;

                        if (lod != 0 && (xInBounds && yInBounds && zInBounds))
                        {
                            continue;
                        }

                        int arrx = ((chunkPos.x / chunkSize) % chunkArraySize + chunkArraySize) % chunkArraySize;
                        int arry = ((chunkPos.y / chunkSize) % chunkArraySize + chunkArraySize) % chunkArraySize;
                        int arrz = ((chunkPos.z / chunkSize) % chunkArraySize + chunkArraySize) % chunkArraySize;

                        Vector4Int key = new Vector4Int(arrx, arry, arrz, lod);

                        if (chunks[lod][arrx, arry, arrz] == null)
                        {
                            Chunk chunk = new Chunk();
                            chunk.position     = chunkPos;
                            chunk.key          = key;
                            chunk.lod          = lod;
                            chunk.creationTime = updateTime;
                            chunk.index        = arrayMax++;
                            chunk.size         = chunkSize;

                            numChunksAdded++;
                            chunks[lod][arrx, arry, arrz] = chunk;
                            chunkArray[chunk.index]       = chunk;
                            octree.AddChunk(chunk);
                        }
                        else
                        {
                            chunks[lod][arrx, arry, arrz].creationTime = updateTime;
                        }
                    }
                }
            }

            prevMax = max;
            prevMin = min;
        }

        double ElapsedMilliseconds1 = sw.Elapsed.TotalMilliseconds;

        sw.Restart();

        RemoveOldChunks(updateTime);
        UpdateArrayMin();

        double ElapsedMilliseconds2 = sw.Elapsed.TotalMilliseconds;

        Debug.LogFormat("Chunk Update: S1 {0} ms, S2 {1} ms, Total {2}ms ({3} chunks added)",
                        ElapsedMilliseconds1, ElapsedMilliseconds2, (ElapsedMilliseconds1 + ElapsedMilliseconds2), numChunksAdded);
        sw.Stop();
    }
Example #29
0
 public VoxelMod(Vector3Int pos, byte _id)
 {
     position = new Vector4Int(pos);
     id       = _id;
 }
Example #30
0
 public VoxelMod(Vector4Int pos, byte _id)
 {
     position = pos;
     id       = _id;
 }
Example #31
0
 public VoxelMod()
 {
     position = new Vector4Int();
     id       = 0;
 }