Пример #1
0
    void ScheduleMoreJobs()
    {
        NativeArray <ArchetypeChunk> dataChunks = meshDataGroup.CreateArchetypeChunkArray(Allocator.TempJob);

        EntityCommandBuffer eCBuffer       = new EntityCommandBuffer(Allocator.TempJob);
        JobHandle           allHandles     = new JobHandle();
        JobHandle           previousHandle = new JobHandle();

        ArchetypeChunkEntityType             entityType = GetArchetypeChunkEntityType();
        ArchetypeChunkComponentType <Sector> sectorType = GetArchetypeChunkComponentType <Sector>(true);
        ArchetypeChunkComponentType <SectorVisFacesCount> faceCountsType = GetArchetypeChunkComponentType <SectorVisFacesCount>(true);
        ArchetypeChunkBufferType <Block>      blocksType = GetArchetypeChunkBufferType <Block>(true);
        ArchetypeChunkBufferType <BlockFaces> facesType  = GetArchetypeChunkBufferType <BlockFaces>(true);

        for (int c = 0; c < dataChunks.Length; c++)
        {
            ArchetypeChunk dataChunk = dataChunks[c];

            //	Get chunk data
            NativeArray <Entity> entities = dataChunk.GetNativeArray(entityType);
            NativeArray <Sector> sectors  = dataChunk.GetNativeArray(sectorType);
            NativeArray <SectorVisFacesCount> faceCounts    = dataChunk.GetNativeArray(faceCountsType);
            BufferAccessor <Block>            blockAccessor = dataChunk.GetBufferAccessor(blocksType);
            BufferAccessor <BlockFaces>       facesAccessor = dataChunk.GetBufferAccessor(facesType);

            for (int e = 0; e < entities.Length; e++)
            {
                MeshDataJob meshDataJob = new MeshDataJob()
                {
                    ECBuffer = eCBuffer,
                    entity   = entities[e],
                    counts   = faceCounts[e],
                    sector   = sectors[e],

                    blocks     = new NativeArray <Block>(blockAccessor[e].AsNativeArray(), Allocator.TempJob),
                    blockFaces = new NativeArray <BlockFaces>(facesAccessor[e].AsNativeArray(), Allocator.TempJob),
                };

                JobHandle thisHandle = meshDataJob.Schedule(previousHandle);
                allHandles = JobHandle.CombineDependencies(thisHandle, allHandles);

                previousHandle = thisHandle;
            }
        }

        runningCommandBuffer = eCBuffer;
        runningJobHandle     = allHandles;

        dataChunks.Dispose();
    }
    protected override JobHandle OnUpdate(JobHandle inputDeps)
    {
        NativeArray <ArchetypeChunk> dataChunks = meshDataGroup.CreateArchetypeChunkArray(Allocator.TempJob);

        if (dataChunks.Length == 0)
        {
            dataChunks.Dispose();
            return(inputDeps);
        }

        EntityCommandBuffer eCBuffer = new EntityCommandBuffer(Allocator.TempJob);

        ArchetypeChunkEntityType             entityType = GetArchetypeChunkEntityType();
        ArchetypeChunkComponentType <Sector> sectorType = GetArchetypeChunkComponentType <Sector>(true);
        ArchetypeChunkComponentType <SectorVisFacesCount> faceCountsType = GetArchetypeChunkComponentType <SectorVisFacesCount>(true);
        ArchetypeChunkBufferType <Block>      blocksType = GetArchetypeChunkBufferType <Block>(true);
        ArchetypeChunkBufferType <BlockFaces> facesType  = GetArchetypeChunkBufferType <BlockFaces>(true);

        for (int c = 0; c < dataChunks.Length; c++)
        {
            ArchetypeChunk dataChunk = dataChunks[c];

            //	Get chunk data
            NativeArray <Entity> entities = dataChunk.GetNativeArray(entityType);
            NativeArray <Sector> sectors  = dataChunk.GetNativeArray(sectorType);
            NativeArray <SectorVisFacesCount> faceCounts    = dataChunk.GetNativeArray(faceCountsType);
            BufferAccessor <Block>            blockAccessor = dataChunk.GetBufferAccessor(blocksType);
            BufferAccessor <BlockFaces>       facesAccessor = dataChunk.GetBufferAccessor(facesType);

            for (int e = 0; e < entities.Length; e++)
            {
                var meshDataJob = new MeshDataJob()
                {
                    ECBuffer = eCBuffer,
                    entity   = entities[e],
                    counts   = faceCounts[e],
                    sector   = sectors[e],

                    blocks     = new NativeArray <Block>(blockAccessor[e].AsNativeArray(), Allocator.TempJob),
                    blockFaces = new NativeArray <BlockFaces>(facesAccessor[e].AsNativeArray(), Allocator.TempJob),
                }.Schedule(inputDeps);
                meshDataJob.Complete();
            }
        }
        eCBuffer.Playback(entityManager);
        eCBuffer.Dispose();

        dataChunks.Dispose();
        return(inputDeps);
    }
    protected override void OnUpdate()
    {
        int drawmode = GetComponent <drawModeData>(mapGenerator).value;

        int mapWidth   = GetComponent <width>(mapGenerator).value;
        int mapHeight  = GetComponent <height>(mapGenerator).value;
        int mapSurface = math.mul(mapWidth, mapHeight);

        DynamicBuffer <noiseMapBuffer> heightMap = GetBuffer <noiseMapBuffer>(mapGenerator);

        heightMapNativeArray = heightMap.ToNativeArray(Allocator.TempJob).Reinterpret <float>();
        colourMapNativeArray = new NativeArray <MaterialColor>(mapSurface, Allocator.TempJob);

        #region JOB CALCULATION
        //=====================================================================================================
        // TEXTURE MAP calculation
        //=====================================================================================================
        if (drawmode == 0)
        {
            TextureMapJob textureMapJob = new TextureMapJob
            {
                mWidth      = mapWidth,
                mHeight     = mapHeight,
                noiseMapJob = heightMapNativeArray,
                colorsJob   = colourMapNativeArray.Reinterpret <Color>(),
            };
            JobHandle jobHandle = textureMapJob.Schedule();
            jobHandle.Complete();
        }
        //=====================================================================================================
        // COLOR MAP calculation
        //=====================================================================================================
        else if (drawmode == 1)
        {
            DynamicBuffer <TerrainTypeBuffer> regionsBuffer = GetBuffer <TerrainTypeBuffer>(mapGenerator);
            ColorMapJob colorMapJob = new ColorMapJob
            {
                mWidth        = mapWidth,
                mHeight       = mapHeight,
                noiseMapJob   = heightMapNativeArray,
                colorsJob     = colourMapNativeArray,
                regionsBuffer = regionsBuffer,
            };
            JobHandle jobHandle = colorMapJob.Schedule();
            jobHandle.Complete();
        }
        //=====================================================================================================
        // MESH MAP calculation
        //=====================================================================================================
        else if (drawmode == 2)
        {
            int lvlDetail = GetComponent <levelOfDetailData>(mapGenerator).value;
            int meshSimplificationIncrement = (lvlDetail == 0) ? 1 : math.mul(lvlDetail, 2);
            int verticesPerLine             = ((mapWidth - 1) / meshSimplificationIncrement) + 1;// 1 2 3 4 5 6 7 ... 241 (241-1)/2 (this imply 241 is a const because not all number are pow of 2)
            //Size of the meshes now depending on the level of detail stored (enter when generated for now)
            verticesArray  = new NativeArray <float3>(((int)math.pow(verticesPerLine, 2)), Allocator.TempJob);
            trianglesArray = new NativeArray <int>(math.mul((int)math.pow(verticesPerLine - 1, 2), 6), Allocator.TempJob);
            uvsArray       = new NativeArray <float2>(((int)math.pow(verticesPerLine, 2)), Allocator.TempJob);


            DynamicBuffer <TerrainTypeBuffer> regionsBuffer = GetBuffer <TerrainTypeBuffer>(mapGenerator);

            //Temporary Solution for animation Curve
            curveHeightArray = new NativeArray <float>(mapSurface, Allocator.TempJob);
            var AnimCurve = _em.GetComponentData <MapHeightCurve>(mapGenerator).value;
            for (int i = 0; i < mapSurface; i++)
            {
                curveHeightArray[i] = AnimCurve.Evaluate(heightMapNativeArray[i]);
            }

            ColorMapJob colorMapJob = new ColorMapJob
            {
                mWidth        = mapWidth,
                mHeight       = mapHeight,
                noiseMapJob   = heightMapNativeArray,
                colorsJob     = colourMapNativeArray,
                regionsBuffer = regionsBuffer,
            };
            JobHandle colorJobHandle = colorMapJob.Schedule();

            MeshDataJob meshDataJob = new MeshDataJob
            {
                widthJob         = mapWidth,
                heightJob        = mapHeight,
                noiseMapJob      = heightMapNativeArray,
                verticesJob      = verticesArray,
                trianglesJob     = trianglesArray,
                uvsJob           = uvsArray,
                heightMulJob     = GetComponent <mapHeightMultiplierData>(mapGenerator).value,
                curveJob         = curveHeightArray,
                levelOfDetailJob = lvlDetail,
                meshSimplificationIncrementJob = meshSimplificationIncrement,
                verticesPerLineJob             = verticesPerLine,
            };
            JobHandle meshjobHandle = meshDataJob.Schedule();
            JobHandle.CompleteAll(ref colorJobHandle, ref meshjobHandle);
            curveHeightArray.Dispose();
        }
        #endregion JOB CALCULATION

        //=====================================================================================================
        // TEXTURE2D applied to the plane (TextureJob and ColorJob)
        //=====================================================================================================
        Texture2D texture2D = new Texture2D(mapWidth, mapHeight);
        texture2D.filterMode = FilterMode.Point;
        texture2D.wrapMode   = TextureWrapMode.Clamp;
        texture2D.SetPixels(colourMapNativeArray.Reinterpret <Color>().ToArray());
        texture2D.Apply();

        var localToWorldScale = new NonUniformScale
        {
            Value = new float3(mapWidth, mapHeight, mapHeight)
        };

        //float4x4 scaleMesh = float4x4.Scale(mapWidth, mapHeight, mapHeight); //CAREFUL y need to be as big as the other vector points!
        //float4x4 totScale = math.mul(_em.GetComponentData<LocalToWorld>(mapGenerator).Value, scaleMesh);
        _em.SetComponentData(mapGenerator, localToWorldScale);
        if (drawmode == 0 || drawmode == 1)
        {
            var material = _em.GetSharedComponentData <RenderMesh>(mapGenerator).material;
            material.mainTexture = texture2D;
        }
        else if (drawmode == 2)
        {
            Mesh mesh = new Mesh();
            mesh.name      = "planePROC";
            mesh.vertices  = verticesArray.Reinterpret <Vector3>().ToArray();
            mesh.uv        = uvsArray.Reinterpret <Vector2>().ToArray();
            mesh.triangles = trianglesArray.ToArray();
            mesh.RecalculateNormals();
            //other mesh

            /*
             * MeshFilter meshFilter = _em.GetComponentData<MeshFilterData>(mapGenerator).value;
             * MeshRenderer meshRenderer = _em.GetComponentData<MeshRendererData>(mapGenerator).value;
             * Renderer renderer = _em.GetComponentData<RendererData>(mapGenerator).value;
             * //renderer.transform.localScale = new float3(texture2D.width, 1, texture2D.height);
             * meshFilter.sharedMesh = mesh;
             * meshRenderer.sharedMaterial.mainTexture = texture2D;
             */
            //other mesh
            var material = _em.GetSharedComponentData <RenderMesh>(mapGenerator).material;
            material.mainTexture = texture2D;
            _em.SetSharedComponentData(mapGenerator, new RenderMesh {
                mesh = mesh, material = _em.GetComponentData <MapMaterialData>(mapGenerator).MeshMat
            });
            //_em.SetSharedComponentData(mapGenerator, new RenderMesh { material = material, mesh = mesh });

            verticesArray.Dispose();
            trianglesArray.Dispose();
            uvsArray.Dispose();
        }
        //=====================================================================================================

        #region Event Trigger End
        colourMapNativeArray.Dispose();
        heightMapNativeArray.Dispose();
        _em.RemoveComponent <Event_MapGen_MapDisplay>(GetSingletonEntity <Event_MapGenTag>());
        #endregion Event Trigger End
        sw.Stop();
        UnityEngine.Debug.Log($"Elapsed Texture{drawmode} = {sw.Elapsed}");
    }