/// <summary>
    /// Initializes all the stuff related to the mesh.
    /// </summary>
    private void Init()
    {
        DisposeNativeDatastructures();
        AllocateNativeDatastructures();

        ResetMesh();

        new SetupIndicesJob
        {
            indices    = indices,
            resolution = resolution,
        }
        .ScheduleParallel(GetNumQuads(), 64, default(JobHandle))
        .Complete();

        VertexAttributeDescriptor vertexDesc = new VertexAttributeDescriptor(VertexAttribute.Position, VertexAttributeFormat.Float32, 3, 0);

        mesh.SetVertexBufferParams(vertices.Length, vertexDesc);
        mesh.SetIndexBufferParams(indices.Length, IndexFormat.UInt16);

        mesh.SetVertexBufferData(vertices, 0, 0, vertices.Length, 0, MeshUpdateFlags.Default);
        mesh.SetIndexBufferData(indices, 0, 0, indices.Length, MeshUpdateFlags.Default);

        SubMeshDescriptor submeshDesc = new SubMeshDescriptor(0, indices.Length, MeshTopology.Triangles);

        mesh.SetSubMesh(0, submeshDesc, MeshUpdateFlags.Default);

        mesh.bounds = new Bounds(Vector3.zero, Vector3.one * 1000.0f);
    }
Example #2
0
    void InitMeshFilter()
    {
        ReleaseMeshFilter();

        int vertexCount = 100;
        int indexCount  = 100;

        meshGrass = new Mesh();

        // 宣告 vertex buffer 結構
        VertexAttributeDescriptor[] layouts = new VertexAttributeDescriptor[]
        {
            new VertexAttributeDescriptor(VertexAttribute.Position, VertexAttributeFormat.Float32, 3)
        };

        meshGrass.SetVertexBufferParams(vertexCount, layouts);

        // 宣告 index buffer 結構
        meshGrass.SetIndexBufferParams(indexCount, IndexFormat.UInt16);

        MeshUpdateFlags flag = MeshUpdateFlags.DontResetBoneBounds | MeshUpdateFlags.DontRecalculateBounds;

        meshGrass.SetVertexBufferData(getRandomPosition(vertexCount, 10f), 0, 0, vertexCount, 0, flag);
        meshGrass.SetIndexBufferData(getSequenceIndices(indexCount), 0, 0, indexCount, flag);

        // 設定 Mesh Topologiy
        SubMeshDescriptor desc = new SubMeshDescriptor(0, indexCount, MeshTopology.Points);

        meshGrass.SetSubMesh(0, desc, flag);


        meshGrass.bounds = new Bounds(Vector3.zero, new Vector3(1000f, 5f, 1000f));

        this.GetComponent <MeshFilter>().sharedMesh = meshGrass;
    }
Example #3
0
        static string GetAttributeString(VertexAttributeDescriptor attr)
        {
            var format    = attr.format;
            var dimension = attr.dimension;

            return($"{format} x {dimension} ({ConvertFormatToSize(format) * dimension} bytes)");
        }
Example #4
0
        public static NativeArray <VertexAttributeDescriptor> Descriptors(Allocator allocator)
        {
            var descriptors = new NativeArray <VertexAttributeDescriptor>(3, allocator);

            descriptors[0] = new VertexAttributeDescriptor(VertexAttribute.Position, VertexAttributeFormat.Float32, 3);
            descriptors[1] = new VertexAttributeDescriptor(VertexAttribute.Normal, VertexAttributeFormat.Float32, 3);
            descriptors[2] = new VertexAttributeDescriptor(VertexAttribute.Color, VertexAttributeFormat.Float32, 4);

            return(descriptors);
        }
        static string GetAttributeString(VertexAttributeDescriptor attr)
        {
            var format    = attr.format;
            var dimension = attr.dimension;
            var str       = $"{format} x {dimension} ({ConvertFormatToSize(format) * dimension} bytes)";

            if (attr.stream != 0)
            {
                str += $", stream {attr.stream}";
            }
            return(str);
        }
Example #6
0
        public override void AddDescriptors(VertexAttributeDescriptor[] dst, int offset, int stream)
        {
            VertexAttribute vatt = VertexAttribute.TexCoord0;

            for (int i = 0; i < uvSetCount; i++)
            {
                if (i == 1)
                {
                    vatt = VertexAttribute.TexCoord1;
                }
                dst[offset + i] = new VertexAttributeDescriptor(vatt, VertexAttributeFormat.Float32, 2, stream);
            }
        }
Example #7
0
        public override void InitModule()
        {
            dims     = new int[3];
            mesh     = new Mesh();
            triCount = 0;

            var isosurfaceTables = new IsosurfaceV5Tables();

            packedTables = isosurfaceTables.PackingTables();

            if (!useGPU)
            {
                vertices         = new List <Vector3>();
                normals          = new List <Vector3>();
                colors           = new List <Color>();
                mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
            }
            else
            {
                tablesBuffer       = new ComputeBuffer(packedTables.Length, sizeof(int));
                counterBuffer      = new ComputeBuffer(1, sizeof(uint), ComputeBufferType.Counter);
                counterCheckBuffer = new ComputeBuffer(1, sizeof(uint), ComputeBufferType.IndirectArguments);
                counterBuffer.SetCounterValue(0);
                shader.SetInt("maximumVertexNum", maximumVertexNum);

                mesh.vertexBufferTarget |= GraphicsBuffer.Target.Raw;

                var vp = new VertexAttributeDescriptor
                             (VertexAttribute.Position, VertexAttributeFormat.Float32, 3);

                var vn = new VertexAttributeDescriptor
                             (VertexAttribute.Normal, VertexAttributeFormat.Float32, 3);

                var vc = new VertexAttributeDescriptor
                             (VertexAttribute.Color, VertexAttributeFormat.Float32, 4);

                mesh.SetVertexBufferParams(maximumVertexNum, vp, vn, vc);
                mesh.SetIndexBufferParams(maximumVertexNum, IndexFormat.UInt32);

                mesh.SetSubMesh(0, new SubMeshDescriptor(0, maximumVertexNum),
                                MeshUpdateFlags.DontRecalculateBounds);

                vertexBuffer = mesh.GetVertexBuffer(0);
                indices      = new int[maximumVertexNum];
                for (int i = 0; i < maximumVertexNum; i++)
                {
                    indices[i] = i;
                }
                mesh.SetIndices(indices, MeshTopology.Triangles, 0);
            }
        }
Example #8
0
        public static NativeArray <VertexAttributeDescriptor> AllocateVertexDescriptor(Allocator allocator = Allocator.TempJob)
        {
            var array = new NativeArray <VertexAttributeDescriptor>(7, allocator);

            array[0] = new VertexAttributeDescriptor(VertexAttribute.Position, VertexAttributeFormat.Float32, 3);
            array[1] = new VertexAttributeDescriptor(VertexAttribute.Normal, VertexAttributeFormat.Float32, 3);
            array[2] = new VertexAttributeDescriptor(VertexAttribute.Color, VertexAttributeFormat.UNorm8, 4);
            array[3] = new VertexAttributeDescriptor(VertexAttribute.TexCoord0, VertexAttributeFormat.Float32, 3);
            array[4] = new VertexAttributeDescriptor(VertexAttribute.TexCoord1, VertexAttributeFormat.Float32, 3);
            array[5] = new VertexAttributeDescriptor(VertexAttribute.TexCoord2, VertexAttributeFormat.Float32, 3);
            array[6] = new VertexAttributeDescriptor(VertexAttribute.TexCoord3, VertexAttributeFormat.UNorm8, 4);

            return(array);
        }
Example #9
0
    void Awake()
    {
        // Create vertices.
        _vertices = new Vertex[] {
            new Vertex()
            {
                position = Quaternion.AngleAxis(0 / 3f * 360, Vector3.forward) * Vector3.up * 0.5f, normal = Vector3.back
            },
            new Vertex()
            {
                position = Quaternion.AngleAxis(2 / 3f * 360, Vector3.forward) * Vector3.up * 0.5f, normal = Vector3.back
            },
            new Vertex()
            {
                position = Quaternion.AngleAxis(1 / 3f * 360, Vector3.forward) * Vector3.up * 0.5f, normal = Vector3.back
            },
        };

        // Create mesh and markt it dynamic, to tell Unity that we will update the verticies continously.
        _mesh = new Mesh();
        _mesh.MarkDynamic();

        // Set the index parameters. UInt16 will allow 65535 indices. If you need more, use UInt32.
        _mesh.SetIndexBufferParams(3, IndexFormat.UInt16);

        // Set the vertex parameters (the data layout for a one vertex). For the yellow triangle, we just need positions.
        VertexAttributeDescriptor[] vertexDataLayout = new VertexAttributeDescriptor[] {
            new VertexAttributeDescriptor(VertexAttribute.Position, VertexAttributeFormat.Float32, 3),
            new VertexAttributeDescriptor(VertexAttribute.Normal, VertexAttributeFormat.Float32, 3)
        };
        _mesh.SetVertexBufferParams(3, vertexDataLayout);

        // Set the sub mesh descriptor.
        SubMeshDescriptor meshDescriptor = new SubMeshDescriptor(0, 3, MeshTopology.Triangles);

        _mesh.SetSubMesh(0, meshDescriptor, meshFlags);

        // Set bounds. If you vertices move a lot, you may want to change this in Update.
        _mesh.bounds = new Bounds(Vector3.zero, Vector3.one);

        // Set initial mesh data.
        _mesh.SetVertexBufferData(_vertices, 0, 0, _vertices.Length, 0, meshFlags);
        _mesh.SetIndexBufferData(new ushort[] { 0, 1, 2 }, 0, 0, 3, meshFlags);
    }
        /// <summary>
        /// Extract the <see cref="VertexAttributeDescriptor"/> of a <see cref="Mesh"/> by optionally storing
        /// every attribute that is NOT position in a separate vertex stream.
        /// </summary>
        public static VertexAttributeDescriptor[] CopyVertexFormat(this Mesh mesh, int positionStream = 0, int otherStream = 1)
        {
            var sourceVertexFormat = mesh.GetVertexAttributes();
            var destVertexFormat   = new VertexAttributeDescriptor[sourceVertexFormat.Length];

            for (var i = 0; i < sourceVertexFormat.Length; i++)
            {
                // CREATE THE NEW VERTEX FORMAT
                // Assign stream 0 only to VertexPosition, and stream 1 to everything else
                var stream = sourceVertexFormat[i].attribute == VertexAttribute.Position ?
                             positionStream : otherStream;

                destVertexFormat[i] = new VertexAttributeDescriptor(sourceVertexFormat[i].attribute,
                                                                    sourceVertexFormat[i].format,
                                                                    sourceVertexFormat[i].dimension, stream);
            }

            return(destVertexFormat);
        }
    void Awake()
    {
        // Create vertices.
        _vertices = new Vector3[] {
            Quaternion.AngleAxis(0 / 3f * 360, Vector3.forward) * Vector3.up * 0.5f,
            Quaternion.AngleAxis(2 / 3f * 360, Vector3.forward) * Vector3.up * 0.5f,
            Quaternion.AngleAxis(1 / 3f * 360, Vector3.forward) * Vector3.up * 0.5f,
        };

        // Create mesh and markt it dynamic, to tell Unity that we will update the verticies continously.
        _mesh = new Mesh();
        _mesh.MarkDynamic();

        // Set the index parameters. UInt16 will allow 65535 indices. If you need more, use UInt32.
        _mesh.SetIndexBufferParams(3, IndexFormat.UInt16);

        // Set the vertex parameters (the data layout for a one vertex). For the yellow triangle, we just need positions.
        VertexAttributeDescriptor[] vertexDataLayout = new VertexAttributeDescriptor[] {
            new VertexAttributeDescriptor(VertexAttribute.Position, VertexAttributeFormat.Float32, 3)
        };
        _mesh.SetVertexBufferParams(3, vertexDataLayout);

        // Set the sub mesh descriptor.
        SubMeshDescriptor meshDescriptor = new SubMeshDescriptor(0, 3, MeshTopology.Triangles);

        _mesh.SetSubMesh(0, meshDescriptor, meshFlags);

        // Set bounds. If you vertices move a lot, you may want to change this in Update.
        _mesh.bounds = new Bounds(Vector3.zero, Vector3.one);

        // Set indices.
        _mesh.SetIndexBufferData(new ushort[] { 0, 1, 2 }, 0, 0, 3, meshFlags);

        // Create material (from shader located in Resources folder).
        Material material = new Material(Shader.Find("Hidden/" + nameof(MeshDemo)));

        // Create necessary components and set references.
        MeshFilter   filter = gameObject.AddComponent <MeshFilter>();
        MeshRenderer render = gameObject.AddComponent <MeshRenderer>();

        filter.sharedMesh = _mesh;
        render.material   = material;
    }
Example #12
0
        public SimpleMeshBackend(int width, int height, Allocator allocator)
        {
            _allocator = allocator;

            int cellCount = width * height;

            _vertData = new NativeArray <VertTileData>(cellCount * 4, allocator);

            _indices = new NativeArray <ushort>(cellCount * 6, allocator);
            _verts   = new NativeArray <float3>(cellCount * 4, allocator);

            _sizeChanged = true;

            _vertexDescriptors = new NativeArray <VertexAttributeDescriptor>(4, allocator);

            // Stream 0 - Positions
            _vertexDescriptors[0] = new VertexAttributeDescriptor(
                VertexAttribute.Position,
                VertexAttributeFormat.Float32,
                3, 0);

            // Stream 1 - Vertex Tile Data
            // UV
            _vertexDescriptors[1] = new VertexAttributeDescriptor(
                VertexAttribute.TexCoord0,
                VertexAttributeFormat.Float32,
                2, 1);
            // FG Colors
            _vertexDescriptors[2] = new VertexAttributeDescriptor(
                VertexAttribute.TexCoord1,
                VertexAttributeFormat.Float32,
                4, 1);
            // BG Colors
            _vertexDescriptors[3] = new VertexAttributeDescriptor(
                VertexAttribute.TexCoord2,
                VertexAttributeFormat.Float32,
                4, 1);

            Size = new int2(width, height);

            //Debug.Log($"Initializing backend with size {Size}");
        }
Example #13
0
    static Mesh CreateQuadMesh()
    {
        Mesh mesh = new Mesh();

        mesh.hideFlags = HideFlags.HideAndDontSave;

        // Verticies.
        VertexAttributeDescriptor[] vertexDataLayout = new VertexAttributeDescriptor[] {
            new VertexAttributeDescriptor(VertexAttribute.Position, VertexAttributeFormat.Float32, 2)
        };
        mesh.SetVertexBufferParams(4, vertexDataLayout);
        mesh.SetVertexBufferData(
            new Vector2[] {
            new Vector2(-1, -1),
            new Vector2(-1, 1),
            new Vector2(1, 1),
            new Vector2(1, -1),
        },
            0, 0, 4, 0,
            meshFlags
            );

        // Indices.
        mesh.SetIndexBufferParams(4, IndexFormat.UInt16);
        mesh.SetIndexBufferData(new ushort[] { 0, 1, 2, 3 }, 0, 0, 4, meshFlags);

        // Sub mesh.
        SubMeshDescriptor meshDescriptor = new SubMeshDescriptor(0, 4, MeshTopology.Quads);

        meshDescriptor.bounds      = new Bounds(Vector3.zero, Vector3.one * 2);
        meshDescriptor.vertexCount = 4;
        mesh.subMeshCount          = 1;
        mesh.SetSubMesh(0, meshDescriptor, meshFlags);

        // Bounds.
        mesh.bounds = meshDescriptor.bounds;

        mesh.UploadMeshData(true);

        return(mesh);
    }
Example #14
0
    void UpdateMesh(NativeSlice <float> source)
    {
        if (_mesh == null)
        {
            _mesh        = new Mesh();
            _mesh.bounds = new Bounds(Vector3.zero, Vector3.one * 10);

            // Initial vertices
            using (var vertices = CreateVertexArray(source))
            {
                var pos = new VertexAttributeDescriptor
                              (VertexAttribute.Position,
                              VertexAttributeFormat.Float32, 3);

                _mesh.SetVertexBufferParams(vertices.Length, pos);
                _mesh.SetVertexBufferData(vertices, 0, 0, vertices.Length);
            }

            // Initial indices
            using (var indices = CreateIndexArray())
            {
                _mesh.SetIndexBufferParams(indices.Length, IndexFormat.UInt32);
                _mesh.SetIndexBufferData(indices, 0, 0, indices.Length);

                var lines = new SubMeshDescriptor
                                (0, indices.Length, MeshTopology.LineStrip);

                _mesh.SetSubMesh(0, lines);
            }
        }
        else
        {
            // Vertex update
            using (var vertices = CreateVertexArray(source))
                _mesh.SetVertexBufferData(vertices, 0, 0, vertices.Length);
        }
    }
Example #15
0
    static private bool createMesh(ref LodVertex[] vertices, ref ushort[] indices, out Mesh mesh)
    {
        mesh = null;

        if (vertices.Length == 0 || indices.Length == 0)
        {
            return(false);
        }

        mesh = new Mesh();

        // 宣告 vertex buffer 結構
        VertexAttributeDescriptor[] layouts = new VertexAttributeDescriptor[]
        {
            new VertexAttributeDescriptor(VertexAttribute.Position, VertexAttributeFormat.Float32, 3),
            new VertexAttributeDescriptor(VertexAttribute.Color, VertexAttributeFormat.UNorm8, 4),
            new VertexAttributeDescriptor(VertexAttribute.TexCoord0, VertexAttributeFormat.UInt32, 1)
        };

        mesh.SetVertexBufferParams(vertices.Length, layouts);

        // 宣告 index buffer 結構
        mesh.SetIndexBufferParams(indices.Length, IndexFormat.UInt16);

        MeshUpdateFlags flag = MeshUpdateFlags.Default;

        mesh.SetVertexBufferData(vertices, 0, 0, vertices.Length, 0, flag);
        mesh.SetIndexBufferData(indices, 0, 0, indices.Length, flag);

        // 設定 Mesh Topologiy
        SubMeshDescriptor desc = new SubMeshDescriptor(0, indices.Length, MeshTopology.Triangles);

        mesh.SetSubMesh(0, desc, flag);

        return(true);
    }
Example #16
0
    public static bool Generate(float size, out Mesh _mesh)
    {
        const int VERTICES_COUNT = 8;

        float halfSize = size * 0.5f;

        // vertex
        CubeVertex[] vertices = new CubeVertex[VERTICES_COUNT];
        vertices[0].pos = new Vector3(-halfSize, -halfSize, -halfSize);
        vertices[1].pos = new Vector3(-halfSize, halfSize, -halfSize);
        vertices[2].pos = new Vector3(halfSize, halfSize, -halfSize);
        vertices[3].pos = new Vector3(halfSize, -halfSize, -halfSize);
        vertices[4].pos = new Vector3(-halfSize, -halfSize, halfSize);
        vertices[5].pos = new Vector3(-halfSize, halfSize, halfSize);
        vertices[6].pos = new Vector3(halfSize, halfSize, halfSize);
        vertices[7].pos = new Vector3(halfSize, -halfSize, halfSize);

        for (int i = 0; i < VERTICES_COUNT; ++i)
        {
            vertices[i].normal = Vector3.Normalize(vertices[i].pos);
        }

        // index
        ushort[] indices = new ushort[] {
            0, 1, 2,
            2, 3, 0,
            0, 4, 5,
            5, 1, 0,
            2, 6, 7,
            7, 3, 2,
            7, 6, 5,
            5, 4, 7,
            1, 5, 6,
            6, 2, 1,
            4, 0, 3,
            3, 7, 4
        };

        MeshUpdateFlags flag = MeshUpdateFlags.Default;

        VertexAttributeDescriptor[] layouts = new VertexAttributeDescriptor[]
        {
            new VertexAttributeDescriptor(VertexAttribute.Position, VertexAttributeFormat.Float32, 3),
            new VertexAttributeDescriptor(VertexAttribute.Normal, VertexAttributeFormat.Float32, 3),
        };

        _mesh = new Mesh();

        // 宣告 index buffer 結構
        _mesh.SetIndexBufferParams(indices.Length, IndexFormat.UInt16);

        // 宣告 vertex buffer 結構
        _mesh.SetVertexBufferParams(VERTICES_COUNT, layouts);

        _mesh.SetVertexBufferData(vertices, 0, 0, VERTICES_COUNT, 0, flag);
        _mesh.SetIndexBufferData(indices, 0, 0, indices.Length, flag);

        // 設定 Mesh Topologiy
        SubMeshDescriptor desc = new SubMeshDescriptor(0, indices.Length, MeshTopology.Triangles);

        _mesh.SetSubMesh(0, desc, flag);

        return(true);
    }
Example #17
0
        protected void CreateDescriptors()
        {
            int vadLen = 1;

            if (hasNormals)
            {
                vadLen++;
            }
            if (hasTangents)
            {
                vadLen++;
            }
            if (texCoords != null)
            {
                vadLen += texCoords.uvSetCount;
            }
            if (colors != null)
            {
                vadLen++;
            }
            if (bones != null)
            {
                vadLen += 2;
            }
            vad = new VertexAttributeDescriptor[vadLen];
            var vadCount = 0;
            int stream   = 0;

            vad[vadCount] = new VertexAttributeDescriptor(VertexAttribute.Position, VertexAttributeFormat.Float32, 3, stream);
            vadCount++;
            if (hasNormals)
            {
                vad[vadCount] = new VertexAttributeDescriptor(VertexAttribute.Normal, VertexAttributeFormat.Float32, 3, stream);
                vadCount++;
            }
            if (hasTangents)
            {
                vad[vadCount] = new VertexAttributeDescriptor(VertexAttribute.Tangent, VertexAttributeFormat.Float32, 4, stream);
                vadCount++;
            }
            stream++;

            if (texCoords != null)
            {
                texCoords.AddDescriptors(vad, vadCount, stream);
                vadCount++;
                stream++;
            }

            if (colors != null)
            {
                colors.AddDescriptors(vad, vadCount, stream);
                vadCount++;
                stream++;
            }

            if (bones != null)
            {
                bones.AddDescriptors(vad, vadCount, stream);
                vadCount += 2;
                stream++;
            }
        }
Example #18
0
 public override void AddDescriptors(VertexAttributeDescriptor[] dst, int offset, int stream)
 {
     dst[offset]     = new VertexAttributeDescriptor(VertexAttribute.BlendWeight, VertexAttributeFormat.Float32, 4, stream);
     dst[offset + 1] = new VertexAttributeDescriptor(VertexAttribute.BlendIndices, VertexAttributeFormat.UInt32, 4, stream);
 }
Example #19
0
 public override void AddDescriptors(VertexAttributeDescriptor[] dst, int offset, int stream)
 {
     dst[offset] = new VertexAttributeDescriptor(VertexAttribute.Color, VertexAttributeFormat.Float32, 4, stream);
 }
Example #20
0
    public void Build(ChunkDataManager chunkDataManager)
    {
//#if UNITY_EDITOR
        UnityEngine.Profiling.Profiler.BeginSample("BUILDING CHUNK");
//#endif
        Vector2Int renderPosition = 16 * position;

        transform.position = new Vector3(renderPosition.x, 0, renderPosition.y);
        mesh.Clear();

        UnityEngine.Profiling.Profiler.BeginSample("GRABBING BLOCK DATA");

        ChunkData chunkData  = chunkDataManager.data[position];
        ChunkData front      = chunkDataManager.data[position + nFront];
        ChunkData back       = chunkDataManager.data[position + nBack];
        ChunkData left       = chunkDataManager.data[position + nLeft];
        ChunkData right      = chunkDataManager.data[position + nRight];
        ChunkData frontLeft  = chunkDataManager.data[position + nFront + nLeft];
        ChunkData frontRight = chunkDataManager.data[position + nFront + nRight];
        ChunkData backLeft   = chunkDataManager.data[position + nBack + nLeft];
        ChunkData backRight  = chunkDataManager.data[position + nBack + nRight];

        byte[,,] lightMap = new byte[48, 256, 48];

        chunkMap[0, 0] = backLeft;
        chunkMap[1, 0] = back;
        chunkMap[2, 0] = backRight;
        chunkMap[0, 1] = left;
        chunkMap[1, 1] = chunkData;
        chunkMap[2, 1] = right;
        chunkMap[0, 2] = frontLeft;
        chunkMap[1, 2] = front;
        chunkMap[2, 2] = frontRight;

        UnityEngine.Profiling.Profiler.EndSample();

        UnityEngine.Profiling.Profiler.BeginSample("SIMULATING LIGHT");

        UnityEngine.Profiling.Profiler.BeginSample("PREPARING LIGHT SIMULATION");


        Queue <Vector3Int> simulateQueue = new Queue <Vector3Int>();

        //sunray tracing needs to start above the highest non-air block to increase performance
        //all blocks above that block need to be set to 15
        for (int z = 0; z < 48; ++z)
        {
            for (int x = 0; x < 48; ++x)
            {
                if ((x % 47) * (z % 47) == 0)                 //filters outer edges
                {
                    //Debug.Log($"these should at least 0 or 47  ->  {x} {z}");
                    for (int yy = 0; yy < 256; ++yy)                     //dont do outer edges
                    {
                        lightMap[x, yy, z] = 15;                         //set all edges to 15 to stop tracing at edges
                    }
                    continue;
                }
                int y = GetHighestNonAir(chunkMap, x, z);
                for (int sunlight = y; sunlight < 256; ++sunlight)
                {
                    lightMap[x, sunlight, z] = 15;
                }


                if (x < 46)
                {
                    y = Mathf.Max(y, GetHighestNonAir(chunkMap, x + 1, z));
                }
                if (x > 1)
                {
                    y = Mathf.Max(y, GetHighestNonAir(chunkMap, x - 1, z));
                }
                if (z < 46)
                {
                    y = Mathf.Max(y, GetHighestNonAir(chunkMap, x, z + 1));
                }
                if (z > 1)
                {
                    y = Mathf.Max(y, GetHighestNonAir(chunkMap, x, z - 1));
                }
                y = Mathf.Min(y + 1, 255);
                if (y < 2)
                {
                    continue;
                }

                simulateQueue.Enqueue(new Vector3Int(x, y, z));
            }
        }

        for (int y = 0; y < 3; ++y)
        {
            for (int x = 0; x < 3; ++x)
            {
                foreach (KeyValuePair <Vector3Int, byte> kv in chunkMap[x, y].lightSources)
                {
                    Vector3Int position = kv.Key;
                    int        lX       = (16 * x) + position.x;
                    int        lY       = position.y;
                    int        lZ       = (16 * y) + position.z;
                    lightMap[lX, lY, lZ] = kv.Value;
                    simulateQueue.Enqueue(new Vector3Int(lX, lY, lZ));
                }
            }
        }

        UnityEngine.Profiling.Profiler.EndSample();

        UnityEngine.Profiling.Profiler.BeginSample("RUNNING LIGHT SIMULATION");


        int simulateCount = 0;

        while (simulateQueue.Count > 0)
        {
            Vector3Int position = simulateQueue.Dequeue();
            int        y        = position.y;
            int        x        = position.x;
            int        z        = position.z;


            byte light = lightMap[x, y, z];

            if (x < 47)
            {
                byte lightR = lightMap[x + 1, y, z];
                if (lightR < light - 1)
                {
                    byte bR = GetBlockFromMap(chunkMap, x + 1, y, z);
                    if (bR == BlockTypes.AIR)
                    {
                        lightMap[x + 1, y, z] = (byte)(light - 1);
                        simulateQueue.Enqueue(new Vector3Int(x + 1, y, z));
                    }
                }
            }
            if (x > 0)
            {
                byte lightL = lightMap[x - 1, y, z];
                if (lightL < light - 1)
                {
                    byte bL = GetBlockFromMap(chunkMap, x - 1, y, z);
                    if (bL == BlockTypes.AIR)
                    {
                        lightMap[x - 1, y, z] = (byte)(light - 1);
                        simulateQueue.Enqueue(new Vector3Int(x - 1, y, z));
                    }
                }
            }
            if (y > 0)
            {
                byte bD = GetBlockFromMap(chunkMap, x, y - 1, z);
                if (bD == BlockTypes.AIR)
                {
                    if (light == 15)
                    {
                        lightMap[x, y - 1, z] = light;
                        simulateQueue.Enqueue(new Vector3Int(x, y - 1, z));
                    }
                    else
                    {
                        byte lightD = lightMap[x, y - 1, z];
                        if (lightD < light - 1)
                        {
                            lightMap[x, y - 1, z] = (byte)(light - 1);
                            simulateQueue.Enqueue(new Vector3Int(x, y - 1, z));
                        }
                    }
                }
            }
            if (y < 255)
            {
                byte lightU = lightMap[x, y + 1, z];
                if (lightU < light - 1)
                {
                    byte bU = GetBlockFromMap(chunkMap, x, y + 1, z);
                    if (bU == BlockTypes.AIR)
                    {
                        lightMap[x, y + 1, z] = (byte)(light - 1);
                        simulateQueue.Enqueue(new Vector3Int(x, y + 1, z));
                    }
                }
            }
            if (z < 47)
            {
                byte lightF = lightMap[x, y, z + 1];
                if (lightF < light - 1)
                {
                    byte bF = GetBlockFromMap(chunkMap, x, y, z + 1);
                    if (bF == BlockTypes.AIR)
                    {
                        lightMap[x, y, z + 1] = (byte)(light - 1);
                        simulateQueue.Enqueue(new Vector3Int(x, y, z + 1));
                    }
                }
            }
            if (z > 0)
            {
                byte lightB = lightMap[x, y, z - 1];
                if (lightB < light - 1)
                {
                    byte bB = GetBlockFromMap(chunkMap, x, y, z - 1);
                    if (bB == BlockTypes.AIR)
                    {
                        lightMap[x, y, z - 1] = (byte)(light - 1);
                        simulateQueue.Enqueue(new Vector3Int(x, y, z - 1));
                    }
                }
            }
            simulateCount++;
        }
        UnityEngine.Profiling.Profiler.EndSample();

        UnityEngine.Profiling.Profiler.EndSample();


        UnityEngine.Profiling.Profiler.BeginSample("CREATING FACES");


        //low precision meshes possible with this? : https://docs.unity3d.com/ScriptReference/Rendering.VertexAttributeDescriptor.html
        //vBuffer = new NativeArray<VertexData>(786432, Allocator.Temp);
        vBufferLength = 0;
        TextureMapper textureMapper = GameManager.instance.textureMapper;

        for (int z = 0; z < 16; ++z)
        {
            for (int y = 0; y < 256; ++y)
            {
                for (int x = 0; x < 16; ++x)
                {
                    byte c = chunkData.GetBlocks()[x, y, z];
                    if (c != BlockTypes.AIR)
                    {
                        int lx = x + 16;
                        int ly = y;
                        int lz = z + 16;

                        byte bR = (x == 15 ? right.GetBlocks()[0, y, z] : chunkData.GetBlocks()[x + 1, y, z]);
                        byte bL = (x == 0 ? left.GetBlocks()[15, y, z] : chunkData.GetBlocks()[x - 1, y, z]);
                        byte bF = (z == 15 ? front.GetBlocks()[x, y, 0] : chunkData.GetBlocks()[x, y, z + 1]);
                        byte bB = (z == 0 ? back.GetBlocks()[x, y, 15] : chunkData.GetBlocks()[x, y, z - 1]);
                        byte bU = (y == 255 ? BlockTypes.AIR : chunkData.GetBlocks()[x, y + 1, z]);
                        byte bD = (y == 0 ? BlockTypes.AIR : chunkData.GetBlocks()[x, y - 1, z]);

                        byte lightR = lightMap[lx + 1, ly, lz];
                        byte lightL = lightMap[lx - 1, ly, lz];
                        byte lightF = lightMap[lx, ly, lz + 1];
                        byte lightB = lightMap[lx, ly, lz - 1];
                        byte lightU = (y == 255 ? (byte)15 : lightMap[lx, ly + 1, lz]);
                        byte lightD = (y == 0 ? (byte)15 : lightMap[lx, ly - 1, lz]);

                        TextureMapper.TextureMap textureMap = textureMapper.map[c];

                        if (bR > 127)
                        {
                            //AddFace(
                            //	new Vector3(x + 1, y, z),
                            //	new Vector3(x + 1, y + 1, z),
                            //	new Vector3(x + 1, y + 1, z + 1),
                            //	new Vector3(x + 1, y, z + 1),
                            //	Vector3.right
                            //);
                            //AddTextureFace(textureMap.right);
                            int  b  = (y == 0 ? 0 : 1);
                            int  t  = (y == 255 ? 0 : 1);
                            byte bl = (byte)((lightMap[lx + 1, ly, lz] + lightMap[lx + 1, ly, lz - 1] + lightMap[lx + 1, ly - b, lz] + lightMap[lx + 1, ly - b, lz - 1]) / 4);
                            byte tl = (byte)((lightMap[lx + 1, ly, lz] + lightMap[lx + 1, ly, lz - 1] + lightMap[lx + 1, ly + t, lz] + lightMap[lx + 1, ly + t, lz - 1]) / 4);
                            byte tr = (byte)((lightMap[lx + 1, ly, lz] + lightMap[lx + 1, ly, lz + 1] + lightMap[lx + 1, ly + t, lz] + lightMap[lx + 1, ly + t, lz + 1]) / 4);
                            byte br = (byte)((lightMap[lx + 1, ly, lz] + lightMap[lx + 1, ly, lz + 1] + lightMap[lx + 1, ly - b, lz] + lightMap[lx + 1, ly - b, lz + 1]) / 4);
                            //AddColors(textureMap,bl, tl, tr, br);
                            AddVertexData(
                                new Vector3(x + 1, y, z),
                                new Vector3(x + 1, y + 1, z),
                                new Vector3(x + 1, y + 1, z + 1),
                                new Vector3(x + 1, y, z + 1),
                                NORMAL_RIGHT,
                                textureMap.right,
                                bl, tl, tr, br);
                        }
                        if (bL > 127)
                        {
                            //AddFace(
                            //	new Vector3(x, y, z + 1),
                            //	new Vector3(x, y + 1, z + 1),
                            //	new Vector3(x, y + 1, z),
                            //	new Vector3(x, y, z),
                            //	-Vector3.right
                            //);
                            //AddTextureFace(textureMap.left);
                            int  b  = (y == 0 ? 0 : 1);
                            int  t  = (y == 255 ? 0 : 1);
                            byte br = (byte)((lightMap[lx - 1, ly, lz] + lightMap[lx - 1, ly, lz - 1] + lightMap[lx - 1, ly - b, lz] + lightMap[lx - 1, ly - b, lz - 1]) / 4);
                            byte tr = (byte)((lightMap[lx - 1, ly, lz] + lightMap[lx - 1, ly, lz - 1] + lightMap[lx - 1, ly + t, lz] + lightMap[lx - 1, ly + t, lz - 1]) / 4);
                            byte tl = (byte)((lightMap[lx - 1, ly, lz] + lightMap[lx - 1, ly, lz + 1] + lightMap[lx - 1, ly + t, lz] + lightMap[lx - 1, ly + t, lz + 1]) / 4);
                            byte bl = (byte)((lightMap[lx - 1, ly, lz] + lightMap[lx - 1, ly, lz + 1] + lightMap[lx - 1, ly - b, lz] + lightMap[lx - 1, ly - b, lz + 1]) / 4);
                            //AddColors(textureMap, bl, tl, tr, br);
                            AddVertexData(
                                new Vector3(x, y, z + 1),
                                new Vector3(x, y + 1, z + 1),
                                new Vector3(x, y + 1, z),
                                new Vector3(x, y, z),
                                NORMAL_LEFT,
                                textureMap.left,
                                bl, tl, tr, br);
                        }

                        if (bU > 127)
                        {
                            //AddFace(
                            //	new Vector3(x, y + 1, z),
                            //	new Vector3(x, y + 1, z + 1),
                            //	new Vector3(x + 1, y + 1, z + 1),
                            //	new Vector3(x + 1, y + 1, z),
                            //	Vector3.up
                            //);
                            //AddTextureFace(textureMap.top);
                            int  b  = (y == 0 ? 0 : 1);
                            int  t  = (y == 255 ? 0 : 1);
                            byte bl = (byte)((lightMap[lx, ly + t, lz] + lightMap[lx - 1, ly + t, lz] + lightMap[lx, ly + t, lz - 1] + lightMap[lx - 1, ly + t, lz - 1]) / 4);
                            byte tl = (byte)((lightMap[lx, ly + t, lz] + lightMap[lx - 1, ly + t, lz] + lightMap[lx, ly + t, lz + 1] + lightMap[lx - 1, ly + t, lz + 1]) / 4);
                            byte tr = (byte)((lightMap[lx, ly + t, lz] + lightMap[lx + 1, ly + t, lz] + lightMap[lx, ly + t, lz + 1] + lightMap[lx + 1, ly + t, lz + 1]) / 4);
                            byte br = (byte)((lightMap[lx, ly + t, lz] + lightMap[lx + 1, ly + t, lz] + lightMap[lx, ly + t, lz - 1] + lightMap[lx + 1, ly + t, lz - 1]) / 4);
                            //AddColors(textureMap, bl, tl, tr, br);
                            AddVertexData(
                                new Vector3(x, y + 1, z),
                                new Vector3(x, y + 1, z + 1),
                                new Vector3(x + 1, y + 1, z + 1),
                                new Vector3(x + 1, y + 1, z),
                                NORMAL_TOP,
                                textureMap.top,
                                bl, tl, tr, br);
                        }
                        if (bD > 127)
                        {
                            //AddFace(
                            //	new Vector3(x, y, z + 1),
                            //	new Vector3(x, y, z),
                            //	new Vector3(x+1, y, z),
                            //	new Vector3(x+1, y, z+1),

                            //	-Vector3.up
                            //);
                            //AddTextureFace(textureMap.bottom);
                            int  b  = (y == 0 ? 0 : 1);
                            int  t  = (y == 255 ? 0 : 1);
                            byte tl = (byte)((lightMap[lx, ly - b, lz] + lightMap[lx - 1, ly - b, lz] + lightMap[lx, ly - b, lz - 1] + lightMap[lx - 1, ly - b, lz - 1]) / 4);
                            byte bl = (byte)((lightMap[lx, ly - b, lz] + lightMap[lx - 1, ly - b, lz] + lightMap[lx, ly - b, lz + 1] + lightMap[lx - 1, ly - b, lz + 1]) / 4);
                            byte br = (byte)((lightMap[lx, ly - b, lz] + lightMap[lx + 1, ly - b, lz] + lightMap[lx, ly - b, lz + 1] + lightMap[lx + 1, ly - b, lz + 1]) / 4);
                            byte tr = (byte)((lightMap[lx, ly - b, lz] + lightMap[lx + 1, ly - b, lz] + lightMap[lx, ly - b, lz - 1] + lightMap[lx + 1, ly - b, lz - 1]) / 4);
                            //AddColors(textureMap, bl, tl, tr, br);
                            AddVertexData(
                                new Vector3(x, y, z + 1),
                                new Vector3(x, y, z),
                                new Vector3(x + 1, y, z),
                                new Vector3(x + 1, y, z + 1),
                                NORMAL_BOTTOM,
                                textureMap.bottom,
                                bl, tl, tr, br);
                        }

                        if (bF > 127)
                        {
                            //AddFace(
                            //	new Vector3(x + 1, y, z + 1),
                            //	new Vector3(x + 1, y + 1, z + 1),
                            //	new Vector3(x, y + 1, z + 1),
                            //	new Vector3(x, y, z + 1),
                            //	Vector3.forward
                            //);
                            //AddTextureFace(textureMap.front);
                            int  b  = (y == 0 ? 0 : 1);
                            int  t  = (y == 255 ? 0 : 1);
                            byte br = (byte)((lightMap[lx, ly, lz + 1] + lightMap[lx - 1, ly, lz + 1] + lightMap[lx, ly - b, lz + 1] + lightMap[lx - 1, ly - b, lz + 1]) / 4);
                            byte tr = (byte)((lightMap[lx, ly, lz + 1] + lightMap[lx - 1, ly, lz + 1] + lightMap[lx, ly + t, lz + 1] + lightMap[lx - 1, ly + t, lz + 1]) / 4);
                            byte tl = (byte)((lightMap[lx, ly, lz + 1] + lightMap[lx + 1, ly, lz + 1] + lightMap[lx, ly + t, lz + 1] + lightMap[lx + 1, ly + t, lz + 1]) / 4);
                            byte bl = (byte)((lightMap[lx, ly, lz + 1] + lightMap[lx + 1, ly, lz + 1] + lightMap[lx, ly - b, lz + 1] + lightMap[lx + 1, ly - b, lz + 1]) / 4);
                            //AddColors(textureMap,bl, tl, tr, br);
                            AddVertexData(
                                new Vector3(x + 1, y, z + 1),
                                new Vector3(x + 1, y + 1, z + 1),
                                new Vector3(x, y + 1, z + 1),
                                new Vector3(x, y, z + 1),
                                NORMAL_FRONT,
                                textureMap.front,
                                bl, tl, tr, br);
                        }
                        if (bB > 127)
                        {
                            //AddFace(
                            //	new Vector3(x, y, z),
                            //	new Vector3(x, y + 1, z),
                            //	new Vector3(x + 1, y + 1, z),
                            //	new Vector3(x + 1, y, z),
                            //	-Vector3.forward
                            //);
                            //AddTextureFace(textureMap.back);
                            int  b  = (y == 0 ? 0 : 1);
                            int  t  = (y == 255 ? 0 : 1);
                            byte bl = (byte)((lightMap[lx, ly, lz - 1] + lightMap[lx - 1, ly, lz - 1] + lightMap[lx, ly - b, lz - 1] + lightMap[lx - 1, ly - b, lz - 1]) / 4);
                            byte tl = (byte)((lightMap[lx, ly, lz - 1] + lightMap[lx - 1, ly, lz - 1] + lightMap[lx, ly + t, lz - 1] + lightMap[lx - 1, ly + t, lz - 1]) / 4);
                            byte tr = (byte)((lightMap[lx, ly, lz - 1] + lightMap[lx + 1, ly, lz - 1] + lightMap[lx, ly + t, lz - 1] + lightMap[lx + 1, ly + t, lz - 1]) / 4);
                            byte br = (byte)((lightMap[lx, ly, lz - 1] + lightMap[lx + 1, ly, lz - 1] + lightMap[lx, ly - b, lz - 1] + lightMap[lx + 1, ly - b, lz - 1]) / 4);
                            //AddColors(textureMap,bl, tl, tr, br);
                            AddVertexData(
                                new Vector3(x, y, z),
                                new Vector3(x, y + 1, z),
                                new Vector3(x + 1, y + 1, z),
                                new Vector3(x + 1, y, z),
                                NORMAL_BACK,
                                textureMap.back,
                                bl, tl, tr, br);
                        }
                    }
                }
            }
        }
        UnityEngine.Profiling.Profiler.EndSample();

        UnityEngine.Profiling.Profiler.BeginSample("APPLYING MESH DATA");
        //mesh.SetVertices(vertices);
        //mesh.SetUVs(0, uvs);
        //mesh.SetNormals(normals);
        //mesh.SetColors(colors);

        VertexAttributeDescriptor[] layout = new VertexAttributeDescriptor[]
        {
            new VertexAttributeDescriptor(VertexAttribute.Position, VertexAttributeFormat.Float32, 3),
            new VertexAttributeDescriptor(VertexAttribute.Color, VertexAttributeFormat.UInt8, 4),
            //new VertexAttributeDescriptor(VertexAttribute.TexCoord0, VertexAttributeFormat.Float32, 2),
        };
        mesh.SetVertexBufferParams(vBufferLength, layout);
        //VertexData[] verts = new VertexData[vertices.Count];
        ////normals 0 front; 1 back; 2 top; 3 bottom; 4 left; 5 right
        //for (int i = 0; i < verts.Length; ++i)
        //{
        //	VertexData v = new VertexData();
        //	v.x = vertices[i].x;
        //	v.y = vertices[i].y;
        //	v.z = vertices[i].z;
        //	v.a = colors[i].a;
        //	Vector3 n = normals[i];
        //	if (n == Vector3.forward) v.b = 0;
        //	if (n == Vector3.back) v.b = 1;
        //	if (n == Vector3.up) v.b = 2;
        //	if (n == Vector3.down) v.b = 3;
        //	if (n == Vector3.left) v.b = 4;
        //	if (n == Vector3.right) v.b = 5;
        //	v.r = (byte)(int)uvs[i].x;
        //	v.g = (byte)(int)uvs[i].y;

        //	//v.uv = uvs[i];
        //	//v.color = colors[i];
        //	//v.normal = normals[i];
        //	verts[i] = v;
        //}
        mesh.SetVertexBufferData(vBuffer, 0, 0, vBufferLength);
        mesh.SetTriangles(triangles, 0);
        mesh.UploadMeshData(false);


        meshRenderer.enabled = (triangles.Count != 0);
        gameObject.SetActive(true);
        //vertices.Clear();
        triangles.Clear();
        //colors.Clear();
        //uvs.Clear();
        //normals.Clear();


        meshCollider.sharedMesh = mesh;
        UnityEngine.Profiling.Profiler.EndSample();

        //#if UNITY_EDITOR
        UnityEngine.Profiling.Profiler.EndSample();
//#endif
    }
 public static int GetVertexAttributeByteSize(VertexAttributeDescriptor desc)
 {
     return(GetByteSize(desc.format, desc.dimension));
 }