// Start is called before the first frame update
    void Start()
    {
        MemoryStream ms         = new MemoryStream(Data.bytes);
        int          totalCount = MTFileUtils.ReadInt(ms);

        positions = new Vector3[totalCount];
        colors    = new Color[totalCount];
        int spawned = 0;

        while (ms.Position < ms.Length && spawned < totalCount)
        {
            ushort spawnedCount = MTFileUtils.ReadUShort(ms);
            for (int i = 0; i < spawnedCount; ++i)
            {
                positions[spawned] = MTFileUtils.ReadVector3(ms);
                var scale = MTFileUtils.ReadVector3(ms);
                colors[spawned] = MTFileUtils.ReadColor(ms);
                ++spawned;
            }
        }
        ms.Close();
    }
Ejemplo n.º 2
0
    public BatchSplitor(byte[] data, Bounds bnd, Mesh mesh)
    {
        MemoryStream ms = new MemoryStream(data);

        TotalCount = MTFileUtils.ReadInt(ms);
        matrices   = new NativeArray <float4x4>(TotalCount, Allocator.Persistent);
        batchIds   = new NativeArray <int>(TotalCount, Allocator.Persistent);
        colors     = new Vector4[TotalCount];
        int spawned = 0;

        while (ms.Position < ms.Length && spawned < matrices.Length)
        {
            ushort spawnedCount = MTFileUtils.ReadUShort(ms);
            for (int i = 0; i < spawnedCount; ++i)
            {
                var pos   = MTFileUtils.ReadVector3(ms);
                var scale = MTFileUtils.ReadVector3(ms);
                var color = MTFileUtils.ReadColor(ms);
                matrices[spawned] = float4x4.TRS(pos, Quaternion.identity, scale);
                colors[spawned]   = color;
                batchIds[spawned] = 0;
                ++spawned;
            }
        }
        ms.Close();
        if (spawned != TotalCount)
        {
            Debug.LogError("terrain detail layer total count is different with spawned count");
        }
        //使用空间4叉树分batch
        if (TotalCount >= count_per_batch)
        {
            job             = new MTDetailBatchSeparateJob();
            job.batchIds    = batchIds;
            job.spawnMatrix = matrices;
            NativeArray <SeparateBound> bounds = new NativeArray <SeparateBound>(4, Allocator.TempJob);
            int offset = 0;
            SplitBounds(TotalCount, bnd.min, bnd.max, bounds, ref offset);
            job.bounds   = bounds;
            separateJobs = job.Schedule(TotalCount, 16);
            IsComplete   = false;
        }
        else
        {
            SplitedBatches = new BatchParameters[1];
            var param = new BatchParameters()
            {
                InstanceCount = TotalCount, Bnd = new Bounds(bnd.center, bnd.size)
            };
            for (int i = 0; i < matrices.Length; ++i)
            {
                param.Matrices.Add(matrices[i]);
                param.Colors.Add(colors[i]);
            }
            SplitedBatches[0] = param;
            LogResults();
            batchIds.Dispose();
            matrices.Dispose();
            IsComplete = true;
        }
    }