Search Unity

It does not display properly mesh

Discussion in 'General Graphics' started by yaaru, Mar 3, 2017.

  1. yaaru

    yaaru

    Joined:
    Mar 3, 2017
    Posts:
    15
    Hey.
    Create a flat mesh in runtime (many small meshes), load height map with Google in it. Mesh doing standard: the vertices, triangles and uv.
    Attached three images: 1 general view that it was clear what was going on, 2 and 3 are views of the same site. On 2 shows that the short-range mesh is not visible to the camera. 3 On the camera raised up and drawn mesh. How to make that mesh was always drawn, if the camera is looking at him?
    I tried to change the shader, and various camera settings.
    Thank you.
     

    Attached Files:

    • 11.jpg
      11.jpg
      File size:
      70.9 KB
      Views:
      814
    • 22.jpg
      22.jpg
      File size:
      47.3 KB
      Views:
      667
    • 33.jpg
      33.jpg
      File size:
      64.1 KB
      Views:
      759
  2. yaaru

    yaaru

    Joined:
    Mar 3, 2017
    Posts:
    15
    Guys, where are the specialists?
    I really need help!
     
  3. tomer20072

    tomer20072

    Joined:
    Dec 8, 2015
    Posts:
    57
    can you explain what you are trying to do a little more?
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,282
    I'm not sure i understand. It sounds like you may have some float precision issues, quite common when dealing with large terrain data that is to scale. What is the position of the terrain meshes and how large are they?
     
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    I believe you're having problems with frustum culling. The "mesh" as far as the CPU knows is out of view so it isn't being rendered. If you disable the in shader's vertex displacement and can't see the mesh then you will possibly won't see it with displacement either.

    The solution is when generating your mesh override the bounds to include the possible displacement range.
     
    karl_jones likes this.
  6. yaaru

    yaaru

    Joined:
    Mar 3, 2017
    Posts:
    15
    Sorry, I was busy.
    Here is the code for creating a mesh:

    public void CreateMeshPart (GameObject PartTerrain, Texture2D tex, int xSize, int ySize, int Grid, ref TMeshOptions MeshOptions, bool MaxSize)
    {

    Mesh mesh = new Mesh();
    mesh.name = PartTerrain.name;
    MeshFilter mf = PartTerrain.AddComponent<MeshFilter> ();
    MeshRenderer mr = PartTerrain.AddComponent<MeshRenderer> ();

    if (xSize % Grid != 0) MeshOptions.TailX = true;
    if (ySize % Grid != 0) MeshOptions.TailY = true;

    xSize++;
    ySize++;

    int xxSize = xSize / Grid;
    int yySize = ySize / Grid;

    if (xSize % Grid != 0) xxSize += 1;
    if (ySize % Grid != 0) yySize += 1;

    if (MeshOptions.TailX) xxSize++;
    if (MeshOptions.TailY) yySize++;

    MeshOptions.XCount = xxSize;
    MeshOptions.YCount = yySize;

    Vector3[] vertices = new Vector3[xxSize * yySize];
    Vector2[] uv = new Vector2[vertices.Length];
    for (int y = 0, i = 0; y < yySize; y += 1)
    {
    for (int x = 0; x < xxSize; x += 1, i += 1)
    {
    float PixX = x * Grid;
    float PixY = y * Grid;

    if (x == xxSize - 1 && MeshOptions.TailX) PixX = xSize - 1;
    if (y == yySize - 1 && MeshOptions.TailY) PixY = ySize - 1;

    vertices = new Vector3(PixX, 0, PixY);
    uv = new Vector2((float)PixX / xSize - 1, (float)PixY / ySize - 1);

    }
    }

    mesh.vertices = vertices;
    mesh.uv = uv;

    int[] triangles = new int[(xxSize - 1) * (yySize - 1) * 6];
    for (int ti = 0, vi = 0, y = 0; y < yySize - 1; y++, vi++)
    {
    for (int x = 0; x < xxSize - 1; x++, ti += 6, vi++)
    {
    triangles[ti] = vi;
    triangles[ti + 3] = triangles[ti + 2] = vi + 1;
    triangles[ti + 4] = triangles[ti + 1] = vi + xxSize - 1 + 1;
    triangles[ti + 5] = vi + xxSize - 1 + 2;
    }
    }

    mesh.triangles = triangles;

    mesh.RecalculateNormals();

    mf.mesh = mesh;

    mr.material.mainTexture = tex;
    mr.material.mainTextureScale = new Vector2(mf.mesh.bounds.size.x / (250 / 15), mf.mesh.bounds.size.z / (250 / 15));

    mr.material.shader = Shader.Find("Legacy Shaders/Diffuse");
    }
     
  7. yaaru

    yaaru

    Joined:
    Mar 3, 2017
    Posts:
    15
    Can I talk more, please?
    I create a mesh with the desired step as seen in my code and can not change the step, because I overlay the map of heights with the same pitch. I have an emulator in which the real values of space.
    Is your decision related to changing the mesh's grid? Or I not so understood?
     
  8. yaaru

    yaaru

    Joined:
    Mar 3, 2017
    Posts:
    15
    Sizes are arbitrary, as I said it's an emulator. I take the map of heights from Google.
     
  9. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    Try:
    mesh.bounds.size.y = 1000.0;
     
  10. yaaru

    yaaru

    Joined:
    Mar 3, 2017
    Posts:
    15
    I get the height value from Google, then I pass these values to the mesh.
    Do you suggest that everyone should put 1000 forcibly? Do I understand correctly?
    The whole task is to properly build a height map.
    Here is the code for determining the height map for the mesh:

    Mesh mesh = _masMeshTerrain.MeshTerrain.GetComponent<MeshFilter>().mesh;
    Vector3[] vv = new Vector3[mesh.vertices.Length];
    mesh.vertices.CopyTo(vv, 0);
    for (int k = 0; k < vv.Length; k++)
    {
    vv[k].y = /..calc/ ;
    }

    mesh.vertices = vv;
    mesh.RecalculateNormals();

    MeshCollider mm = _masMeshTerrain.MeshTerrain.GetComponent<MeshCollider>();
    mm.enabled = false;
    mm.enabled = true;
     
  11. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    Setting the bounds to 1000 high is just to see if that solves the problem you're having, it's not a final solution. If you're modifying the height from script it shouldn't be necessary, you made it sound like you were modifying the height in the shader which would cause the problem.

    Try calling RecalculateBounds() when you call RecalculateNormals()
    https://docs.unity3d.com/ScriptReference/Mesh.RecalculateBounds.html
     
    GameRocker, yaaru and karl_jones like this.
  12. yaaru

    yaaru

    Joined:
    Mar 3, 2017
    Posts:
    15
    Your decision helped, Thank you very much!
     
  13. GameRocker

    GameRocker

    Joined:
    Oct 10, 2012
    Posts:
    2
    Thank you so much man, I have been trying to fix a similar problem for a very long time.