- Home /
 
Apply UV to procedural mesh
I have created a mesh procedurally and then I am trying to create a terrain on top of it. I am new to Unity so from what I understand I need to start by applying some texture to do so. In order to do that though I first need to apply the UV to my mesh. What I had in mind is, simply place the texture on top of the mesh but from what I understood I can't do that and I need to generate per triangle the uv. Doing the above with this code:
  Vector2[] uvs_temp = Unwrapping.GeneratePerTriangleUV(mesh);
  Vector2[] uvs= new Vector2[vertices.Length];
  for(int i=0; i<uvs_temp.Length; i++) {
         // Removing duplicates 
         if(i == 0 || uvs_temp[i] != uvs_temp[i-1]) {
             uvs[i] = new Vector2(uvs_temp[i].x, uvs_temp[i].y);
         }
   }
   mesh.uv = uvs;
 
               Results in the result above which if I understand correctly since I applied to each trianglethe uv, it re-applies the same grass per triangle, but I can't understand how to fix it.
How can I apply a texture on top of my whole mesh?
 
Answer by MerryLeo · Feb 05 at 06:30 PM
I am also quite new to Unity and I don't know if this will help, but I know that you can use a shader to apply the texture to your mesh. For example, you could write a standard surface shader that will apply a texture using world coordinates instead. Here is a sample code I wrote for this:
 Shader "Custom/TerrainShader"
 {
     Properties
     {
         _Color ("Color", Color) = (1,1,1,1)
         _MainTex ("Albedo (RGB)", 2D) = "white" {}
         _Glossiness ("Smoothness", Range(0,1)) = 0.5
         _Metallic ("Metallic", Range(0,1)) = 0.0
     }
     SubShader
     {
         Tags { "RenderType"="Opaque" }
         LOD 200
 
         CGPROGRAM
         // Physically based Standard lighting model, and enable shadows on all light types
         #pragma surface surf Standard fullforwardshadows
 
         // Use shader model 3.0 target, to get nicer looking lighting
         #pragma target 3.0
 
         sampler2D _MainTex;
         float4 _MainTex_ST;
 
         struct Input 
         {
             float3 worldPos;
         };
 
         half _Glossiness;
         half _Metallic;
         fixed4 _Color;
 
         // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
         // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
         // #pragma instancing_options assumeuniformscaling
         UNITY_INSTANCING_BUFFER_START(Props)
             // put more per-instance properties here
         UNITY_INSTANCING_BUFFER_END(Props)
 
         void surf (Input IN, inout SurfaceOutputStandard o)
         {
             // Albedo comes from a texture tinted by color
             fixed4 c =  tex2D (_MainTex, float2(_MainTex_ST.z + _MainTex_ST.x * IN.worldPos.x, _MainTex_ST.w + _MainTex_ST.y * IN.worldPos.z)) * _Color;
             o.Albedo = c.rgb;
             // Metallic and smoothness come from slider variables
             o.Metallic = _Metallic;
             o.Smoothness = _Glossiness;
             o.Alpha = c.a;
         }
         ENDCG
     }
     FallBack "Diffuse"
 }
 
               This Standard Surface Shader applies a repeating texture to a mesh with the x-axis and z-axis. The only issue you might encounter with this is if you move your terrain. If you do so, the texture will stretch. Hopefully this will help.
Your answer