Exemplo n.º 1
0
        public void AddChunkRenderComponent(Entity chunkRenderEntity, Entity chunkEntity, ref Chunk chunk, int materialID, int skeletonID)// byte buildState = 0)
        {
            ChunkRenderer chunkRender = new ChunkRenderer {
            };

            chunkRender.chunk      = chunkEntity;
            chunkRender.materialID = (byte)materialID;
            chunkRender.SetMetaData(voxelSpawnSystem.meta, voxelSpawnSystem.voxelIDs);
            //, ChunkSpawnSystem.maxCacheVerts, ChunkSpawnSystem.maxCacheTriangles);
            chunkRender.Value = chunk.Value;

            /*if (skeletonID != 0)
             * {
             *  chunkRender.hasWeights = 1;
             *  chunkRender.InitializeBoneWeights(ChunkSpawnSystem.maxCacheVerts,
             *      worldSpawnSystem.skeletonsMeta[chunk.worldID].data.datas);
             * }*/
            if (World.EntityManager.HasComponent <ChunkRenderer>(chunkRenderEntity))
            {
                World.EntityManager.SetComponentData(chunkRenderEntity, chunkRender);
            }
            else
            {
                World.EntityManager.AddComponentData(chunkRenderEntity, chunkRender);
            }
            ChunkSides sides = new ChunkSides();

            sides.Init(chunk.Value.voxelDimensions);
            World.EntityManager.SetComponentData(chunkRenderEntity, sides);

            ChunkMesh chunkMesh = new ChunkMesh();

            chunkMesh.Init(chunk.Value.voxelDimensions);
            World.EntityManager.SetComponentData(chunkRenderEntity, chunkMesh);
        }
Exemplo n.º 2
0
 public static void DestroyChunkRender(EntityManager EntityManager, Entity e)
 {
     if (EntityManager.Exists(e))
     {
         if (EntityManager.HasComponent <ChunkRenderer>(e))
         {
             ChunkRenderer chunkRenderer = EntityManager.GetComponentData <ChunkRenderer>(e);
             chunkRenderer.Dispose();
         }
         if (EntityManager.HasComponent <ChunkSides>(e))
         {
             ChunkSides chunkSides = EntityManager.GetComponentData <ChunkSides>(e);
             chunkSides.Dispose();
         }
         if (EntityManager.HasComponent <RenderMesh>(e))
         {
             RenderMesh renderer = EntityManager.GetSharedComponentData <RenderMesh>(e);
             GameObject.Destroy(renderer.mesh);
         }
         else if (EntityManager.HasComponent <ChunkMesh>(e))
         {
             var renderer = EntityManager.GetSharedComponentData <ChunkMeshLink>(e);
             GameObject.Destroy(renderer.mesh);
         }
         EntityManager.DestroyEntity(e);
     }
 }
Exemplo n.º 3
0
 public void AddRenderEntitiesToChunk(Entity chunkEntity, ref Chunk chunk, World world, List <Material> materials, float3 spawnPosition, Entity[] renderEntities, VoxData model)
 {
     if (ChunkSpawnSystem.isDebugLog)
     {
         Debug.LogError("Adding " + materials.Count + " chunk renders to chunk: " + chunk.Value.chunkPosition + "::" + chunk.Value.voxelDimensions);
     }
     chunk.chunkRenders = new BlitableArray <Entity>(materials.Count, Allocator.Persistent);
     for (int j = 0; j < materials.Count; j++)
     {
         //Entity renderEntity = renderEntities[i * materials.Count + j];
         Entity renderEntity = renderEntities[j];
         SetChunkRender(
             renderEntity,
             chunkEntity,
             ref chunk,
             materials[j],
             j,
             spawnPosition,
             model.id != 0,
             world.skeletonID);
         if (model.id != 0)
         {
             ChunkRenderer chunkRender = World.EntityManager.GetComponentData <ChunkRenderer>(renderEntity);
             chunkRender.isCenter = 1;
             List <Color> colors = model.GetColors(); //  new List<Color>(); //
             chunkRender.voxelColors = new BlitableArray <float3>(colors.Count, Allocator.Persistent);
             for (int a = 0; a < colors.Count; a++)
             {
                 chunkRender.voxelColors[a] = new float3(colors[a].r, colors[a].b, colors[a].g);
             }
             World.EntityManager.SetComponentData(renderEntity, chunkRender);
         }
         chunk.chunkRenders[j] = renderEntity;
     }
 }
Exemplo n.º 4
0
        // add noise to render, using original verts and making new one
        public void UpdateRender(Entity entity, ref ChunkRenderer chunkRenderer, ref ChunkMesh chunkMesh, ref ChunkMeshAnimation chunkMeshAnimation)
        {
            if (UnityEngine.Time.time - chunkMesh.timePassed >= animationSpeed)
            {
                chunkMesh.timePassed = UnityEngine.Time.time;
                var chunk = World.EntityManager.GetComponentData <Chunk>(chunkRenderer.chunk);
                if (World.EntityManager.Exists(chunk.world) == false || !World.EntityManager.HasComponent <World>(chunk.world))
                {
                    return;
                }
                var worldComponent = World.EntityManager.GetComponentData <World>(chunk.world);
                var renderer       = World.EntityManager.GetSharedComponentData <RenderMesh>(entity);
                var mesh           = renderer.mesh;
                // apply animation
                if (World.EntityManager.HasComponent <Skeleton>(chunk.world))
                {
                    var skeleton = World.EntityManager.GetComponentData <Skeleton>(chunk.world);
                    if (skeleton.bones.Length > 0)
                    {
                        var          vertsCount    = chunkMesh.buildPointer.vertIndex;
                        float3[]     bonePositions = new float3[skeleton.bones.Length];
                        quaternion[] boneRotations = new quaternion[skeleton.bones.Length];
                        for (int i = 0; i < skeleton.bones.Length; i++)
                        {
                            var boneEntity = skeleton.bones[i];
                            //originalBonePositions[i] = World.EntityManager.GetComponentData<Bone>(boneEntity).position;
                            //bonePositions[i] = World.EntityManager.GetComponentData<Translation>(boneEntity).Value;
                            boneRotations[i] = World.EntityManager.GetComponentData <Rotation>(boneEntity).Value;
                            bonePositions[i] = World.EntityManager.GetComponentData <Translation>(boneEntity).Value - World.EntityManager.GetComponentData <Bone>(boneEntity).position;
                            //Debug.LogError(i + " - Bone Position: " + bonePositions[i]);
                        }
                        chunkMeshAnimation.SetBones(bonePositions, boneRotations);

                        var layout = new[]
                        {
                            new VertexAttributeDescriptor(UnityEngine.Rendering.VertexAttribute.Position, VertexAttributeFormat.Float32, 3),
                            new VertexAttributeDescriptor(UnityEngine.Rendering.VertexAttribute.Color, VertexAttributeFormat.Float32, 3),
                            new VertexAttributeDescriptor(UnityEngine.Rendering.VertexAttribute.TexCoord0, VertexAttributeFormat.Float32, 2),
                        };
                        mesh.SetVertexBufferParams(vertsCount, layout);
                        //mesh.SetVertexBufferData(cache.vertices, 0, 0, vertsCount);
                        mesh.SetVertexBufferData(chunkMeshAnimation.GetTempArray(), 0, 0, vertsCount);
                        mesh.UploadMeshData(false);
                        //mesh.RecalculateBounds();
                        //mesh.RecalculateNormals();
                    }
                    else
                    {
                        return;
                        //newVerts = ApplyRandomNoise(caches[zoxID.id].verts);
                    }
                }
                else
                {
                    return;
                }
            }
        }
Exemplo n.º 5
0
        public void UpdateChunk(Entity chunkEntity)
        {
            Chunk chunk = World.EntityManager.GetComponentData <Chunk>(chunkEntity);

            if (ChunkSpawnSystem.isDebugLog)
            {
                UnityEngine.Debug.LogError("Telling Chunk's Renderers to start updating at: " + chunk.Value.chunkPosition + "::" + chunk.Value.voxelDimensions);
            }
            // later for all renders, do this
            for (int i = 0; i < chunk.chunkRenders.Length; i++)
            {
                Entity chunkRenderEntity = chunk.chunkRenders[i];                //chunkSpawnSystem.chunkRenders[chunk.chunkRenders[i]];
                if (World.EntityManager.HasComponent <ChunkRenderer>(chunkRenderEntity))
                {
                    ChunkRenderer chunkRenderer = World.EntityManager.GetComponentData <ChunkRenderer>(chunkRenderEntity);
                    chunkRenderer.Value = chunk.Value;
                    World.EntityManager.SetComponentData(chunkRenderEntity, chunkRenderer);
                }
                else
                {
                    chunkRenderSystem.AddChunkRenderComponent(chunkRenderEntity, chunkEntity, ref chunk, 0, 0);
                }
                if (World.EntityManager.HasComponent <ChunkRendererBuilder>(chunkRenderEntity))
                {
                    World.EntityManager.SetComponentData(chunkRenderEntity, new ChunkRendererBuilder {
                        state = 1
                    });
                }
                else
                {
                    World.EntityManager.AddComponentData(chunkRenderEntity, new ChunkRendererBuilder {
                        state = 1
                    });                                                                                                                  //.AddChunkRenderComponent(chunkRenderEntity, ref chunk, 0, 0, 1);
                }
            }
        }