Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by samblack · Nov 24, 2011 at 09:39 PM · meshproceduralseamsedgespacktextures

Procedural Mesh with visible seams

Hello,

I've been working on proceduraly generated meshs with variable size. At the moment it's just a big square although I intend it to have holes in the future. Because I want those meshes to have multiple textures that I will get through WWW I am creating an atlas texture map (through PackTexture) but when I use the atlas, the squares' seams become noticeble. If I use a material with a single texture the seams are not visible. I've been searching the web but so far I have yet to figure out why this is happening.

The way I generate the mesh is: For each cell I create four vertices, then two triangles and finaly UV's. The UV's are based on the Rect list obtained from Texture.PackTextures.

The generating code:

 using UnityEngine;
 using UnityEditor;
 using System.Collections;
 
 public class MeshCreator : MonoBehaviour 
 {
     public Texture2D tex1;
     public Texture2D tex2;
     public Texture2D tex3;
     public Texture2D atlas;
     
     void Start () 
     {
         GetComponent<MeshRenderer>().enabled = false;
         Texture2D[] texs = new Texture2D[3];
         texs[0] = tex1;
         texs[1] = tex2;
         texs[2] = tex3;
         
         /*foreach (Texture2D texture in texs)  
         {
             string path = AssetDatabase.GetAssetPath(texture);
             //Debug.Log("path: " + path);
             TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
             textureImporter.isReadable = true;
             AssetDatabase.ImportAsset(path);
         }*/
         
         atlas = new Texture2D(0, 0);
         Rect[] texUVS = atlas.PackTextures(texs, 0, 2048);
         
         Mesh myMesh = new Mesh();
     
         int XX = 10, YY = 10;// 10x10 Mesh
         
         int CellSize = 2;
         
         int NumCells = XX * YY;
         int NumTriangles =  NumCells * 2; // Each cell has 2 tris
         
         int NumVertices = NumCells * 4; // Each cell has 4 verts
         int p0, p1, p2, p3;
                 
         Vector3[] verts = new Vector3[NumVertices];        
         int[] triangles = new int[NumTriangles * 3];        
         Vector2[] uvs = new Vector2[NumVertices];
         
         for(float c = 0, x = 0, z = 0, v = 0, t = 0; c < NumCells; c++, x+= CellSize, v+=4, t+=6)
         {
             
             if( c % XX == 0 && c > 0)
             {
                 x = 0;
                 z+= CellSize;
             }
             
             p0 = (int)v;
             p1 = (int)v + 1;
             p2 = (int)v + 2;
             p3 = (int)v + 3;
         
             verts[p0] = new Vector3(x,0,z);
             verts[p1] = new Vector3(x + CellSize,0,z);            
             verts[p2] = new Vector3(x,0,z + CellSize);
             verts[p3] = new Vector3(x + CellSize, 0, z + CellSize);
             
             // tri 1
             triangles[(int)t] = p2;
             triangles[(int)t+1] = p1;
             triangles[(int)t+2] = p0;
             
             // tri 2
             triangles[(int)t+3] = p2;
             triangles[(int)t+4] = p3;
             triangles[(int)t+5] = p1;
             
             
             //uvs 
             int r = Random.Range(0,(texUVS.Length ));
             uvs[p0] = new Vector2(texUVS[r].xMin, texUVS[r].yMin); //(0,0)
             uvs[p1] = new Vector2(texUVS[r].xMax, texUVS[r].yMin); //(1,0)
             uvs[p2] = new Vector2(texUVS[r].xMin, texUVS[r].yMax); //(0,1)
             uvs[p3] = new Vector2(texUVS[r].xMax, texUVS[r].yMax); //(1,1)
             
             
             /*
             uvs[p0] = new Vector2(0.0f, 0.0f);
             uvs[p1] = new Vector2(1.0f, 0.0f);
             uvs[p2] = new Vector2(0.0f, 1.0f);
             uvs[p3] = new Vector2(1.0f, 1.0f);*/
         }
         
         myMesh.vertices = verts;
         myMesh.triangles = triangles;
         myMesh.uv = uvs;
         
         myMesh.RecalculateNormals();
         myMesh.RecalculateBounds();
         
         myMesh.Optimize();
                 
         GetComponent<MeshFilter>().mesh = myMesh;
         GetComponent<MeshCollider>().sharedMesh = myMesh;
         GetComponent<MeshRenderer>().material.mainTexture = atlas;        
         
         GetComponent<MeshRenderer>().enabled = true;
         
         
     }
 }

And here's a picture using a black texture in the atlas:

alt text

Thank you.

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

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

[SOLVED]Procedural generated side by side cubes without 'seams'? 2 Answers

Create the visual spring in Unity? 3 Answers

Creating a Pentahedron (a 5 sided shape) Mesh procedurally 1 Answer

Create planar UVs on procedurally generated mesh 1 Answer

Generate mesh from raycast positions, independent of rotations 0 Answers


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