Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 FaffyWaffles · Mar 17 at 07:19 PM · uvprocedural meshuv mappinguvsuv coordinates

Procedural Mesh UV problem

alt text

I'm learning the basics of mesh generation, and am having some trouble with mapping the correct UV to the new generated mesh.

This is the code I'm using to clone the mesh:

 using System.Collections.Generic;
 using UnityEngine;
 using Sirenix.OdinInspector;
 
 public class MeshDupe : MonoBehaviour
 {
     public GameObject target;
 
     public List<Vector3> targetVertices = new List<Vector3>();
     public int[] targetTriangles;
     public List<Vector3> targetNormals = new List<Vector3>();
     public List<Vector4> targetTangents = new List<Vector4>();
     public Vector2[] targetUVs;
 
     public List<Vector3> newVertices = new List<Vector3>();
     public List<int> newTriangles = new List<int>();
     public List<Vector3> newNormals = new List<Vector3>();
     public List<Vector4> newTangents = new List<Vector4>();
     public List<Vector2> newUVs = new List<Vector2>();
 
     private Material targetMaterial;
     private Shader targetShader;
 
     [Button]
     void DupeMesh()
     {
         Mesh mesh = target.GetComponent<MeshFilter>().mesh;
     
         mesh.GetVertices(targetVertices);
         targetTriangles = mesh.triangles;
         mesh.GetNormals(targetNormals);
         mesh.GetTangents(targetTangents);
         targetUVs = mesh.uv;
 
         targetMaterial = target.GetComponent<Renderer>().material;
         targetShader = targetMaterial.shader;
 
         for (int i = 0; i < targetTriangles.Length; i += 3)
         {
             Vector3 v0 = targetVertices[targetTriangles[i + 0]];
             Vector3 v1 = targetVertices[targetTriangles[i + 1]];
             Vector3 v2 = targetVertices[targetTriangles[i + 2]];
 
             Vector2 uv0 = targetUVs[targetTriangles[i + 0]];
             Vector2 uv1 = targetUVs[targetTriangles[i + 1]];
             Vector2 uv2 = targetUVs[targetTriangles[i + 2]];
 
             AddData(v0, uv0, i);
             AddData(v1, uv1, i + 1);
             AddData(v2, uv2, i + 2);
         }
         CreateNew();
     }
     void AddData(Vector3 v, Vector2 uv, int i)
     {
         if (!newVertices.Contains(v))
         {
             newVertices.Add(v);
             newNormals.Add(targetNormals[targetTriangles[i]]);
             newTangents.Add(targetTangents[targetTriangles[i]]);
             newUVs.Add(uv);
         }
         newTriangles.Add(newVertices.IndexOf(v));     
     }
     void CreateNew()
     {
         GameObject dupe = new GameObject("dupe");
         MeshRenderer meshRenderer = dupe.AddComponent<MeshRenderer>();
         meshRenderer.sharedMaterial = new Material(Shader.Find("Standard"));
         MeshFilter meshFilter = dupe.AddComponent<MeshFilter>();
         Mesh newMesh = new Mesh();
 
         MeshCollider posCollider = dupe.AddComponent<MeshCollider>();
         posCollider.sharedMesh = newMesh;
         newMesh.SetVertices(newVertices);
         newMesh.SetTriangles(newTriangles, 0);
         newMesh.SetNormals(newNormals);
         newMesh.SetTangents(newTangents);
         newMesh.SetUVs(0, newUVs);
 
         meshFilter.mesh = newMesh;
         meshRenderer.material = targetMaterial;
         meshRenderer.material.shader = targetShader;
 
         target.SetActive(false);
     }
 }

--note, I'm creating this as an analog to another project, that is the reason I'm not referencing the mesh directly. In my project, I need to get the references from previously assigned lists.

uvhelp.jpg (340.6 kB)
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

134 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

Related Questions

Procedural Mesh UV Problem (UVs 'mirrored' instead of 'repeating') 0 Answers

C# PackTextures & UV Mapping 2 Answers

Is it possible to UV map a cylinder without duplicating a column of vertices? 1 Answer

UV Tiling/repeat 1 Answer

Update a texture at runtime using UV 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