Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed May 13, 2020 at 12:24 PM by GravityL for the following reason:

Other

avatar image
0
Question by GravityL · May 11, 2020 at 08:20 AM · meshrenderingbugmeshrendererdots

ECS mesh dissapears on camera move in Game or Scene

Hi, I'm having a weird bug rendering meshes with ECS. It renders but when I move the camera in the Game or Scene view it suddendly dissapears at some points.

A similar code implementing the same methods for chunk generation and triangles creation works just fine with GameObjects.

Here is a video showing it: Youtube

And the code:

 using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  using Unity.Entities;
  using Unity.Rendering;
  using Unity.Collections;
  using Unity.Transforms;
  using Unity.Mathematics;
  
  public class Chunk : MonoBehaviour {
   
      [SerializeField] private Mesh mesh;
      [SerializeField] private Material material;
      [SerializeField] private int xSize = 16;
      [SerializeField] private int ySize = 225;
      [SerializeField] private int zSize = 16;
  
      FastNoise noise = new FastNoise();
      float[,] heightMap ;
      float[,] heightMap2;
  
      private void Start(){
          heightMap = new float[xSize,zSize];
          heightMap2 = new float[xSize,zSize];
  
          EntityManager entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
  
          EntityArchetype entityArchetype = entityManager.CreateArchetype(
              typeof(ChunkSize),
              typeof(Translation),
              typeof(RenderMesh),
              typeof(LocalToWorld),
              typeof(RenderBounds)
          );
  
          Entity entity = entityManager.CreateEntity(entityArchetype);
  
          //NativeArray<Entity> entityArray = new NativeArray<Entity>(1,Allocator.Temp);
  
          //entityManager.CreateEntity(entityArchetype, entityArray);
  
          DynamicBuffer<Block> dynamicBuffer =  entityManager.AddBuffer<Block>(entity);    
  
          List<Vector3> vertex = new List<Vector3>();
  
          for(int x=0; x< xSize ; x++)
              for(int y=0; y< ySize; y++)
                  for(int z=0; z< zSize; z++){                
  
                      float simplex1 = noise.GetSimplex(x*.8f, z*.8f)*10;
                      float simplex2 = noise.GetSimplex(x * 3f, z * 3f) * 10*(noise.GetSimplex(x*.3f, z*.3f)+.5f);
                      float heightMap = simplex1 + simplex2;
                      float dirtHeight = ySize * .5f + heightMap;
  
                      dynamicBuffer.Add(new Block{
                          xCoord = x,
                          yCoord = y,
                          zCoord = z,
                          material = y > dirtHeight ?  BlockMaterials.air : BlockMaterials.dirt
                      });
  
                  }
  
          for(int x=0; x<= xSize ; x++)
              for(int y=0; y<= ySize; y++)
                  for(int z=0; z<= zSize; z++){
                      vertex.Add(new Vector3(x,y,z));
                  }
  
          DynamicBuffer<Block> blockBuffer = dynamicBuffer.Reinterpret<Block>();
  
          Vector3 max = new Vector3(0,0,0);
  
          foreach (var item in vertex.ToArray())
          {
              if(Vector3.Distance(item, Vector3.zero) > Vector3.Distance(max, Vector3.zero)){
                  max = item;
              }
          }
  
          List<int> triangles = GenerateCubesByTriangles(xSize, ySize, zSize, blockBuffer);
  
          UpdateMesh(vertex.ToArray(),triangles.ToArray());
  
          entityManager.SetSharedComponentData(entity, new RenderMesh{
              mesh = mesh,
              material = material
          });
  
         // entityManager.SetComponentData(entity, new Translation{Value = new float3(0f,0f,0f)});
      }
  
      void UpdateMesh(Vector3[] vertices, int[] triangles){
          mesh.Clear();
          mesh.vertices = vertices;
          mesh.triangles = triangles;
          mesh.RecalculateNormals();
      }
  
      List<int> GenerateCubesByTriangles(int _xSize, int _ySize, int _zSize, DynamicBuffer<Block> blockBuffer){
          List<int> _triangles = new List<int>();
  
          int vert = 0;
       
          for(int x=0; x< _xSize ; x++){
  
              for(int y=0; y< _ySize; y++){
               
                  for(int z=0; z< _zSize; z++){    
                   
                      if(blockBuffer[x*ySize+y*zSize+z].material == BlockMaterials.air) {vert++; continue;};
  
                      int[] formulas = new int[]{
                          vert + 0,
                          vert + 1 ,
                          vert + (_zSize + 1),
                          vert + (_zSize + 1) + 1,
                          vert + (_zSize + 1) * (_ySize + 1),
                          vert + (_zSize + 1) * (_ySize + 1) + 1,
                          vert + (_zSize + 1) * (_ySize + 1) + (_zSize + 1)  ,
                          vert + (_zSize + 1) * (_ySize + 1) + (_zSize + 1) + 1
                      };
  
                      /* FRENTE */
                      if((x==0 &&blockBuffer[x*ySize+y*zSize+z].material != BlockMaterials.air) || (x>0 && blockBuffer[(x-1)*ySize+y*zSize+z].material == BlockMaterials.air)){
                       
                          _triangles.Add(formulas[2]);
                          _triangles.Add(formulas[0]);
                          _triangles.Add(formulas[1]);
  
                          _triangles.Add(formulas[3]);
                          _triangles.Add(formulas[2]);
                          _triangles.Add(formulas[1]);
                       
                      }
  
                      /* FONDO */
                      if((x==xSize-1 &&blockBuffer[x*ySize+y*zSize+z].material != BlockMaterials.air) || (x < xSize-1 && blockBuffer[(x+1)*ySize+y*zSize+z].material == BlockMaterials.air)){
                          _triangles.Add(formulas[4]);
                          _triangles.Add(formulas[6]);
                          _triangles.Add(formulas[5]);
  
                          _triangles.Add(formulas[5]);
                          _triangles.Add(formulas[6]);
                          _triangles.Add(formulas[7]);
  
                      }
  
   
                      /* PISO */
                      if((y==0 &&blockBuffer[x*ySize+y*zSize+z].material != BlockMaterials.air)|| (y>0 && blockBuffer[x*ySize+(y-1)*zSize+z].material == BlockMaterials.air)){
  
                          _triangles.Add(formulas[0]);
                          _triangles.Add(formulas[4]);
                          _triangles.Add(formulas[5]);
  
                          _triangles.Add(formulas[0]);
                          _triangles.Add(formulas[5]);
                          _triangles.Add(formulas[1]);
  
                      }
  
  
                      /* TECHO */
                      if(y<ySize-1 && blockBuffer[x*ySize+(y+1)*zSize+z].material == BlockMaterials.air){
                          _triangles.Add(formulas[7]);
                          _triangles.Add(formulas[6]);
                          _triangles.Add(formulas[2]);
  
                          _triangles.Add(formulas[7]);
                          _triangles.Add(formulas[2]);
                          _triangles.Add(formulas[3]);
                      }
  
                   
                      /* LATERAL */
                      if((z==0 &&blockBuffer[x*ySize+y*zSize+z].material != BlockMaterials.air) || (z>0 && blockBuffer[x*ySize+y*zSize+z-1].material == BlockMaterials.air)){
                          _triangles.Add(formulas[6]);
                          _triangles.Add(formulas[4]);
                          _triangles.Add(formulas[0]);
  
                          _triangles.Add(formulas[2]);
                          _triangles.Add(formulas[6]);
                          _triangles.Add(formulas[0]);
                      }
  
  
                      /* LATERAL2 */
                      if((z==zSize-1 &&blockBuffer[x*ySize+y*zSize+z].material != BlockMaterials.air) || (z<zSize-1 && blockBuffer[x*ySize+y*zSize+z+1].material == BlockMaterials.air)){
  
                          _triangles.Add(formulas[7]);
                          _triangles.Add(formulas[1]);
                          _triangles.Add(formulas[5]);
  
                          _triangles.Add(formulas[7]);
                          _triangles.Add(formulas[3]);
                          _triangles.Add(formulas[1]);  
                      }
   
                      vert++;
  
                  }
  
                  vert++;
  
              }
  
              vert += _zSize + 1;
  
          }
  
          return _triangles;
      }
  
  }

Block component:

  using UnityEngine;
  using Unity.Entities;
  using Unity.Rendering;
  
  public struct Block : IBufferElementData {
      public BlockMaterials material;
      public int xCoord;
      public int yCoord;
      public int zCoord;
  }
  
  public enum BlockMaterials
  {
      air,
      water,
      dirt,
      stone,
      sand,
      sandstone
  }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

176 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Render VBO on GPU through MeshRenderer 1 Answer

Unity Renders objects that are not in build - Bug? 1 Answer

Mesh Renderer glitch when switching its parent 0 Answers

Better model? Use Mesh clipping or add more vertices? 0 Answers

Unity5 Procedural meshing slower than in Unity4? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges