コード例 #1
0
    private void GenerateMesh()
    {
        myVertices = new Vector3[(myXLength + 1) * (myZLength + 1)];
        NativeArray <Vector3> vertices = new NativeArray <Vector3>((myXLength + 1) * (myZLength + 1), Allocator.Persistent);

        myTriangles = new int[myXLength * myZLength * 6];
        NativeArray <int> triangles = new NativeArray <int>(myXLength * myZLength * 6, Allocator.Persistent);

        var job = new MeshGeneratorJob()
        {
            myVertices  = vertices,
            myTriangles = triangles,
            myXLength   = myXLength,
            myZLength   = myZLength
        };

        JobHandle jobHandle = job.Schedule();

        jobHandle.Complete();

        job.myVertices.CopyTo(myVertices);
        job.myTriangles.CopyTo(myTriangles);

        vertices.Dispose();
        triangles.Dispose();

        SetMesh();
    }
コード例 #2
0
    protected override JobHandle OnUpdate(JobHandle inputDeps)
    {
        // Create a location to recieve data back from the thread
        NativeArray <Entity> entity = new NativeArray <Entity>(1, Allocator.TempJob);

        // Create the job
        // Passing in the Vertex and Triangle buffers from the entity we are working on
        // As well as the NativeArray we made to recieve the entity
        MeshGeneratorJob meshGenJob = new MeshGeneratorJob()
        {
            vertexData   = GetBufferFromEntity <MeshGeneratorVertexData>(false),
            triangleData = GetBufferFromEntity <MeshGeneratorTriangleData>(false),
            thisEntity   = entity
        };

        JobHandle handle = meshGenJob.Schedule(this, inputDeps);

        handle.Complete();

        if (entity[0] == Entity.Null)
        {
            entity.Dispose();
            return(handle);
        }

        // Once job is done we have the entity that we worked on
        // So now that the job is over with we can remove the tag
        // So it won't update anymore till we re-add the tag to
        // Prompt a re-build
        EntityManager.RemoveComponent(entity[0], typeof(MeshGeneratorTag));

        var renderMesh = EntityManager.GetSharedComponentData <RenderMesh>(entity[0]);

        NativeArray <Vector3> nativeVertexArray   = GetBufferFromEntity <MeshGeneratorVertexData>(true)[entity[0]].Reinterpret <Vector3>().ToNativeArray(Allocator.TempJob);
        NativeArray <int>     nativeTriangleArray = GetBufferFromEntity <MeshGeneratorTriangleData>(true)[entity[0]].Reinterpret <int>().ToNativeArray(Allocator.TempJob);

        mesh.vertices  = nativeVertexArray.ToArray();
        mesh.triangles = nativeTriangleArray.ToArray();

        mesh.RecalculateNormals();

        nativeVertexArray.Dispose();
        nativeTriangleArray.Dispose();

        EntityManager.SetSharedComponentData(entity[0], new RenderMesh
        {
            mesh = mesh
        });

        Debug.Log(EntityManager.GetSharedComponentData <RenderMesh>(entity[0]).mesh.vertexCount);

        entity.Dispose();

        return(handle);
    }