2016-09-07 2 views
0

높이 맵을 받아 입력을 기반으로 한 단일 등각 투영을 만드는 함수를 만들려고합니다. 큐브의 꼭대기를 쉽게 표현할 수있는 삼각형을 만들도록 만들었지 만, 큐브의 측면을 내려가는 것은 내가 문제가있는 것처럼 보입니다. 이 기능은 완료되지 않은 상태로 끝나기 전에 문제가 발생하여 두 면만 만듭니다.유니티 메쉬 삼각형이 뒤집혀졌습니다.

기본적으로 어떤 일이 벌어지고있는 것은 삼각형이 뒤집혀 있고 다른 것들은 그렇지 않다는 것입니다. 이유는 모르겠습니다. 두 삼각형이 같은 방향을 향하게하는면이 있고 다른면은 사각형의 두 번째면이 보이기 만하면 삼각형 중 하나가 보일 수 있습니다.

메쉬의 한면에서 다른면으로 이어지는 사각형을 만드는 경우도 있습니다.

저는 메시와 3D가 일반적으로 새롭습니다. 나는 또한 내가 UV를 매핑하는 방식이 완전히 잘못되었다는 것을 확신하지만, 그것은 나중에 나올 무언가이다.

public static MeshData generateIsoMesh(float [,] heightMap, int heightMax, AnimationCurve heightCurve) 
{ 
    int height = heightMap.GetLength(0); 
    int width = heightMap.GetLength(1); 

    int triangleNum = 0; 

    int blockHeight = 0 ; 
    int nextHeight = 0; 
    int heightDif = 0; 

    List<Vector3> vecList = new List<Vector3>(); 
    List<int> triangleList = new List<int>(); 
    List<Vector2> uvList = new List<Vector2>(); 



    for (int y = 0; y < height; y ++) 
    { 
     for (int x = 0; x < width; x ++) 
     { 

      blockHeight = ((int)(heightMap[x, y]*10)) * heightMax; 

      vecList.Add(new Vector3(x, blockHeight , y)); 
      vecList.Add(new Vector3(x, blockHeight, y+ 1)); 
      vecList.Add(new Vector3(x+1, blockHeight, y+1)); 
      vecList.Add(new Vector3(x + 1, blockHeight, y)); 

      uvList.Add(new Vector2(x/(float)width, y/(float)height)); 
      uvList.Add(new Vector2(x/(float)width, y/(float)height)); 
      uvList.Add(new Vector2(x/(float)width, y/(float)height)); 
      uvList.Add(new Vector2(x/(float)width, y/(float)height)); 

      if ((x < width-1) && (y < height-1)) 
      { 
       triangleList.Add(triangleNum); 
       triangleList.Add(triangleNum + 1); 
       triangleList.Add(triangleNum + 2); 

       triangleList.Add(triangleNum); 
       triangleList.Add(triangleNum + 2); 
       triangleList.Add(triangleNum + 3); 
       triangleNum += 4; 
      } 
      if (x < width-1 && x > 3 && x > 0) 
      { 

       nextHeight = ((int)(heightMap[x+1,y]*10)) * heightMax; 
       heightDif = (nextHeight - blockHeight)/heightMax; 
       if (heightDif > 0) 
       { 
        for (int z = 0; z < heightDif; z ++) 
        { 
         uvList.Add(new Vector2(x/(float)width, y/(float)height)); 
         uvList.Add(new Vector2(x/(float)width, y/(float)height)); 
         vecList.Add(new Vector3(x + 1, blockHeight - (1 + z), y)); 
         vecList.Add(new Vector3(x + 1, blockHeight - (1 + z), y + 1)); 

         triangleList.Add(triangleNum - 1); 
         triangleList.Add(triangleNum - 2); 
         triangleList.Add(triangleNum + 1); 

         triangleList.Add(triangleNum - 1); 
         triangleList.Add(triangleNum + 1); 
         triangleList.Add(triangleNum ); 
         triangleNum += 4; 

        } 
       } 
       else if (heightDif < 0) 
       { 
        ; 
       } 
      } 
     } 
    } 

출력 메시 : 및 http://imageshack.com/a/img924/9072/HdrZFm.png

모든 당신의 도움을 주셔서 감사 코드

을 단계를 수행하는 루프없이 동일한 메쉬. https://imageshack.us/i/poHdrZFmp

답변

0

문제는 렌더링 중이고 원인은 정점 인덱스의 방향 일 수 있습니다.

삼각형이 보이는면은 정점 인덱스의 방향에 의해 결정됩니다. 기본적으로 시계 방향으로 배치 된 경우 삼각형은 앞으로 향하고 보이는 것으로 간주됩니다. 반 시계 방향 삼각형은 버려 지므로 일반적으로 어쨌든 볼 수는 없지만 객체의 내부 렌더링에 시간을 낭비 할 필요가 없습니다.

그래서 항상 꼭지점 인덱스 방향이 시계 방향인지 확인해야합니다.

다른 긴 줄의 문제는 삼각형에 할당하기 전에 인덱스 순서를 변경해보십시오. 012 ~ 120처럼 오리엔테이션을해야합니다.