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 /
  • Help Room /
avatar image
0
Question by Cinabutts0 · Aug 24, 2018 at 04:15 AM · uvprocedural meshuv mappingmesh verticesmesh manipulation

Assigning uvs to "procedural" mesh.

This mesh is not technically procedural as in it's a script that creates a new mesh but it's a low poly water script that produces waves using the given mesh's vertices. I did not create this script and it can actually be downloaded for free HERE. The only annoying concern is that it doesn't do anything at all to effect the uvs. This is my current version of the script.

PLEASE KEEP IN MIND, I AM NEW TO CODING, So try to dumb it down for me please lol also I know what uvs are and all that I just have no idea of the code I need to write to actually SET the uvs.

 using System.Collections;
 using System.Collections.Generic;
 
 using UnityEngine;
 
 namespace LowPolyWater
 {
     public class LowPolyWater : MonoBehaviour
     {
         public float waveHeight = 0.5f;
         public float waveFrequency = 0.5f;
         public float waveLength = 0.75f;
         public bool clampY = true;
         public bool _Gizmos = false;
         // private float _NewGizmoY;
         public float _GizmoY = 5f;
         public Color32 _GizmoColor;
 
         //Position where the waves originate from
         public Vector3 waveOriginPosition = new Vector3 (0.0f, 0.0f, 0.0f);
 
         private Mesh mesh;
         private MeshCollider meshCollider;
 
         private Vector3[] vertices;
         private MeshFilter meshFilter;
 
         private void Awake ()
         {
             //Get the Mesh Filter of the gameobject
             meshFilter = GetComponent<MeshFilter> ();
             meshCollider = gameObject.GetComponent<MeshCollider> ();
         }
 
         void Start ()
         {
             // _NewGizmoY = transform.position.y + _GizmoY;
             CreateMeshLowPoly (meshFilter);
             // Debug.Log (transform.position.y);
             // Debug.Log (transform.position.y + _GizmoY);
         }
 
         /// <summary>
         /// Rearranges the mesh vertices to create a 'low poly' effect
         /// </summary>
         /// <param name="mf">Mesh filter of gameobject</param>
         /// <returns></returns>
         MeshFilter CreateMeshLowPoly (MeshFilter mf)
         {
             mesh = mf.sharedMesh;
 
             //Get the original vertices of the gameobject's mesh
             Vector3[] originalVertices = mesh.vertices;
 
             //Get the list of triangle indices of the gameobject's mesh
             int[] triangles = mesh.triangles;
 
             //Create a vector array for new vertices 
             Vector3[] vertices = new Vector3[triangles.Length];
 
             //Assign vertices to create triangles out of the mesh
             for (int i = 0; i < triangles.Length; i++)
             {
                 vertices[i] = originalVertices[triangles[i]];
                 triangles[i] = i;
             }
 
             //Update the gameobject's mesh with new vertices
             mesh.vertices = vertices;
             mesh.SetTriangles (triangles, 0);
             mesh.RecalculateBounds ();
             mesh.RecalculateNormals ();
             mesh.GetType ();
             this.vertices = mesh.vertices;
             mesh.uv = new Vector2[mesh.vertexCount];
 
             meshCollider.sharedMesh = mesh;
 
             return mf;
         }
 
         void Update ()
         {
             GenerateWaves ();
         }
 
         private void OnDrawGizmos ()
         {
             if (_Gizmos)
             {
             Gizmos.color = _GizmoColor;
             Gizmos.DrawWireMesh (mesh, -1, this.transform.position + new Vector3 (this.transform.position.x,this.transform.position.x + _GizmoY, this.transform.position.z), gameObject.transform.rotation, this.transform.localScale);
             }
         }
 
         /// <summary>
         /// Based on the specified wave height and frequency, generate 
         /// wave motion originating from waveOriginPosition
         /// </summary>
 
         void GenerateWaves ()
         {
 
             //Count the vertices?
             for (int i = 0; i < vertices.Length; i++)
             {
                 Vector3 v = vertices[i];
 
                 //Initially set the wave height to 0
                 v.y = 0.0f;
 
                 //Get the distance between wave origin position and the current vertex
                 float distance = Vector3.Distance (v, waveOriginPosition);
                 distance = (distance % waveLength) / waveLength;
 
                 //Oscilate the wave height via sine to create a wave effect
                 v.y = waveHeight * Mathf.Sin (Time.time * Mathf.PI * 4.0f * waveFrequency +
                     (Mathf.PI * 2.0f * distance));
 
                 //CLAMP THE Y TO MINIMUM OF "waveOriginPosition.y" so the waves do not go below original y coordinates
                 if (clampY)
                     v.y = Mathf.Clamp (v.y, waveOriginPosition.y, waveHeight * 4.0f + waveOriginPosition.y);
 
                 //Update the vertex
                 vertices[i] = v;
             }
 
             //Update the mesh properties
             mesh.MarkDynamic ();
             mesh.vertices = vertices;
 
             mesh.RecalculateNormals ();
             meshCollider.sharedMesh = mesh;
             meshFilter.mesh = mesh;
             // OnDrawGizmos ();
             // mesh.uv.Initialize ();
 
         }
 
     }
 }



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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by eses · Aug 24, 2018 at 11:43 AM

Hi, @Cinabutts0

The question has been asked before: https://answers.unity.com/questions/64410/generating-uvs-for-a-scripted-mesh.html

Maybe it is helpful? It's pretty much just plotting the vert positions in UV space.

Comment
Add comment · 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
0

Answer by Cinabutts0 · Aug 24, 2018 at 07:24 PM

@eses Yes that does help me but I can't figure out where and how to get vertices[I].x and y..

Comment
Add comment · 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

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

152 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 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

Create Runtime Mesh (plane) Inside linked points Like in the image . 0 Answers

Assigning uv's to multiple faces 1 Answer

Help setting UV coordinates for code-generated plane 0 Answers

Texture not Drawn on all uv Coordinates 0 Answers

I want reverse(?) spherize 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