Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Twinklier · Nov 21, 2019 at 02:04 PM · meshmaterialcustom

How do you assign custom materials to your custom mesh?

I have a script to create a custom mesh, a custom material using LWRP's shader graph. Decided to put them together but it doesn't seem to work. The cube and the quad next to it looks just fine tho.alt text

Here's a peak using System.Collections; using System.Collections.Generic; using UnityEngine;

 [RequireComponent(typeof(MeshFilter))]
 [RequireComponent(typeof(MeshRenderer))]
 [ExecuteInEditMode]
 public class MeshEditor : MonoBehaviour
 {
     public Material material;
 
     List<Transform> verticesTransforms; //three of these transforms so that i can change vertices (per triangle) easily
 
     Mesh mesh;
 
     List<Vector3> curr_vertex;  //modify vertices 3 at a time, then add them to the vertices list later
     Vector3Int curr_triangle;  //modify the current triangle then assign to the list later
 
     public List<Vector3> vertices;
     public List<int> triangles;
 
 //-------------------------------------------------------------------------------------------------------------
 
     void Start()
     {
         if (!Application.isPlaying)
         {
             mesh = new Mesh();
         }
     }
 //-------------------------------------------------------------------------------------------------------------
 
     void Update()
     {
         if (!Application.isPlaying) //Just need this only to help be build things
         {
             GetComponent<MeshRenderer>().material = material;
             GetComponent<MeshFilter>().mesh = mesh;
 
 //-------------------------------------------------------------------------------------------------------------
             while (verticesTransforms.Count < 3)
             {
                 verticesTransforms.Add(null);
             }
             while (verticesTransforms.Count > 3)
             {
                 verticesTransforms.RemoveAt(verticesTransforms.Count - 1);
             }
             while (curr_vertex.Count < 3)
             {
                 curr_vertex.Add(Vector3.zero);
             }
             while (curr_vertex.Count > 3)
             {
                 curr_vertex.RemoveAt(curr_vertex.Count - 1);
             }
 //-------------------------------------------------------------------------------------------------------------
 
 
             for (int i = 0; i < 3; i++)
             {
                 if (i < 3)
                 {
                     if (verticesTransforms[i] != null)
                     {
                         verticesTransforms[i].parent = transform; //nice and organized
                         curr_vertex[i] = verticesTransforms[i].localPosition; //adjust the vertex by moving the transforms in the editor
                     }
                     else
                     {
                         verticesTransforms[i] = new GameObject("Vertex Transform").transform; //in case the vertex is missing
                     }
                 }
 
                 vertices[vertices.Count - 1 - i] = curr_vertex[i]; //now add the recently-modified current vertex to the list
 //-------------------------------------------------------------------------------------------------------------
 
                 if (i == 0)
                 {
                     triangles[triangles.Count - 1] = curr_triangle.x;
                 }
                 if (i == 1)
                 {
                     triangles[triangles.Count - 2] = curr_triangle.x;
                 }
                 if (i == 2)
                 {
                     triangles[triangles.Count - 1] = curr_triangle.x;
                 }
 
                 //used vector3 for triangle cuz it's a bit more convenient (for me)
             }
 
 //-------------------------------------------------------------------------------------------------------------
 
             mesh.triangles = triangles.ToArray();
             mesh.vertices = vertices.ToArray();
             mesh.RecalculateNormals();
             mesh.name = "custom mesh";
         }
     }
 }
 
material-mesh-problem.png (134.7 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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Pangamini · Nov 21, 2019 at 02:48 PM

I don't see your code generating texture coords anywhere. That way the shader doesn't know, which part of the texture should it sample, and probably just samples the corner of the texture over the entire mesh. See UVs/texcoords of the mesh

Comment
Add comment · Show 1 · Share
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 Twinklier · Nov 22, 2019 at 01:13 PM 0
Share

Works the charm. Thanks for the help!

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

140 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Having an issue with applying a flat texture to a mesh 0 Answers

Custom UI material with Mask ? 2 Answers

Animal Crossing - Technical Question 1 Answer

Creating a mesh and assigning a material during edit time. 1 Answer

Is it possible to save runtime generated mesh as a texture ? 1 Answer


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