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 Grinde · Jun 21, 2015 at 11:34 AM · c#meshmaterial

UVs don't apply correctly on script-generated meshes

I'm having an issue with textures only applying to a small corner of some meshes I'm generating. The values I'm putting in scale from 0 to 1 as they should, and the meshes themselves are positioned correctly, but only one square segment is actually textured. I'm a bit lost as to what I'm doing wrong, and any help would be greatly appreciated. Full script is below (based on http://wiki.unity3d.com/index.php/CreatePlane), just attach to a GameObject. Most relevant bit starts at line 75.

alt text

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class ChunkManager : MonoBehaviour {
 
     public float chunkSize = 256;
     public int chunkSegs = 64;
     
     private GameObject map;
     private Dictionary<string, GameObject> chunks;
 
     private int vGridCount;
     private int vCount;
     private int tCount;
 
     private float scale;
     private float hSize;
     private float iSegs;
 
     // Use this for initialization
     void Start () {
         chunks = new Dictionary<string, GameObject> ();
         map = this.gameObject;
 
         vGridCount = chunkSegs + 1;
         vCount = vGridCount * vGridCount;
         tCount = chunkSegs * chunkSegs * 6;
 
         scale = chunkSize / (float)chunkSegs;
         hSize = chunkSize / 2.0f;
         iSegs = 1.0f / (float)chunkSegs;
 
         for (int x = -1; x < 2; x++) {
             for (int y = -1; y < 2; y++) {
                 CreateChunk (x, y);
             }
         }
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     // Creates a new chunk
     void CreateChunk(float x, float z) {
         CreateChunk (Mathf.FloorToInt (x / chunkSize), Mathf.FloorToInt (z / chunkSize));
     }
 
     string GetChunkId(int cx, int cz) {
         return cx.ToString() + "-" + cz.ToString();
     }
     
     void CreateChunk(int cx, int cz) {
         string chunkid = GetChunkId (cx, cz);
 
         Debug.Log ("Creating chunk " + chunkid);
 
         // Create the chunk object
 
         GameObject chunk = new GameObject ();
         chunk.name = "Chunk " + chunkid;
         chunk.transform.parent = map.transform;
         chunk.transform.position = new Vector3((float)cx * chunkSize, 0.0f, (float)cz * chunkSize);
 
         // Build the mesh
 
         Mesh mesh = new Mesh();
 
         Vector3[] vertices = new Vector3[vCount];
         Vector2[] uvs = new Vector2[vCount];
         int[] triangles = new int[tCount];
 
         // Calculate uvs and vertices
 
         int offset = 0;
         float x, y;
         for (y = 0.0f; y < vGridCount; y++) {
             for (x = 0.0f; x < vGridCount; x++) {
                 vertices[offset] = new Vector3(x * scale - hSize, 0.0f, y * scale - hSize);
                 uvs[offset++] = new Vector2(x * iSegs, y * iSegs);
                 if (chunkid == "0-0" && y == 0.0f) {
                     Debug.Log (uvs[offset-1].x);
                 }
             }
         }
 
         // Calculate triangles
 
         offset = 0;
         int ix, iy, a, b, c, d;
         for (iy = 0; iy < chunkSegs; iy++) {
             for (ix = 0; ix < chunkSegs; ix++) {
                 a = ( iy      * vGridCount) + ix;
                 b = ((iy + 1) * vGridCount) + ix;
                 c = a + 1;
                 d = b + 1;
 
                 triangles[offset  ] = a;
                 triangles[offset+1] = b;
                 triangles[offset+2] = c;
 
                 triangles[offset+3] = b;
                 triangles[offset+4] = d;
                 triangles[offset+5] = c;
             }
         }
 
         // Update mesh
 
         mesh.vertices = vertices;
         mesh.uv = uvs;
         mesh.triangles = triangles;
         mesh.RecalculateNormals ();
         mesh.RecalculateBounds ();
 
         // Attach mesh filter and mesh renderer
         
         MeshFilter meshFilter = (MeshFilter)chunk.AddComponent (typeof(MeshFilter));
         MeshRenderer meshRenderer = (MeshRenderer)chunk.AddComponent (typeof(MeshRenderer));
         chunk.AddComponent (typeof(BoxCollider));
 
         meshFilter.sharedMesh = mesh;
         meshRenderer.sharedMaterial = new Material(Shader.Find("Diffuse"));
 
         chunks.Add (chunkid, chunk);
     }
 
 }


Comment
Add comment · Show 4
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
avatar image Owen-Reynolds · Jun 21, 2015 at 03:51 PM 0
Share

Is the small textured area showing the entire texture, or just the lower-left corner ... ? Is the rest a solid smear of colors based on the texture you currently have, or ... ? That would be caused by uv values past 1 and a clamped texture (but I don't think your UV values are past 1.)

I don't much care for how it assigns the shared material (why not just assign an existing material. or keep the one you have now?) I assume a texture being assigned later?

avatar image Grinde · Jun 21, 2015 at 05:10 PM 0
Share

I'm just using a material with the default diffuse shader, no actual textures yet. At some point I'll be generating the textures to apply. The small corner is the only part that is visible - the rest is simply invisible, but unity shows the center of the object in the correct place. UV values go from 0 to 1 with even increments according to the debug log.

I've tried creating the material in Start and using the same one for each chunk, but the result is the same.

E: I've added a picture to the original question.

avatar image Owen-Reynolds · Jun 21, 2015 at 07:44 PM 1
Share

I don't understand the picture. BUT. if you can't see the faces, UVs aren't the problem. A bad UV map will apply the wrong color, but will never not show a pixel. 100% of the time, the world's worst UV map will put some color from the texture, onto the pixel. The only way to hide a pixel is if the texture has transparency, and so does the shader (diffuse shaders do not.)

UVs aren't the problem. $$anonymous$$aybe the normals are bad (or you're just on the wrong side) or tris are messed up ... .

FYI, most people put on a checkerboard or a duck or something, just for testing. The default shader uses a white texture. So it's not like you're testing it "without a texture" first.

avatar image Grinde · Jun 21, 2015 at 08:41 PM 0
Share

In the picture you can see one (of nine) of the chunks selected. The movement arrows are placed in the center of the object, but only the top right corner is visibly textured.

Found the problem though - I forgot to increment the offset when calculating my tris. Whoops.

Thanks for the help.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Change Material of quad in script c# unity 2d 1 Answer

Distribute terrain in zones 3 Answers

Splitting Procedurally Generated Mesh, Based On Height 0 Answers

How to highlight only one face of a Cube from script? 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