Strange mesh texture created by code in Unity

I’m learning how to mesh by code in Unity, but the texture is weird as in the image, does anyone know why this is?
I created the UV Map as it is in the code, searched Google and Unity documentation a lot, and saw that I should use the array vertices to map the UV, but I think I did something wrong

My Code:

public Vector3[] verts = {
     new Vector3(-0,5, 0,5, 0,0), new Vector3(0,5, 0,5, 0,0),
     new Vector3(0,5, -0,5, 0,0), new Vector3(-0,5, -0,5, 0,0),
     new Vector3(-0,2, 0,3, 0,0), new Vector3(0,2, 0,3, 0,0),
     new Vector3(0,2, -0,1, 0,0), new Vector3(-0,2, -0,1, 0,0),
     new Vector3(-0,2, 0,3, 0,0), new Vector3(0,2, 0,3, 0,0),
     new Vector3(0,2, -0,1, 0,0), new Vector3(-0,2, -0,1, 0,0),
     new Vector3(-0,2, 0,3, 0,1), new Vector3(0,2, 0,3, 0,1),
     new Vector3(0,2, -0,1, 0,1), new Vector3(-0,2, -0,1, 0,1),
     new Vector3(-0,2, 0,3, 0,1), new Vector3(0,2, 0,3, 0,1),
     new Vector3(0,2, -0,1, 0,1), new Vector3(-0,2, -0,1, 0,1),
     new Vector3(-0,5, 0,5, 0,1), new Vector3(-0,5, -0,5, 0,1),
     new Vector3(-0,5, -0,5, 0,1), new Vector3(-0,5, -0,5, 0,1),
     new Vector3(-0,5, -0,5, 0,1), new Vector3(-0,5, -0,5, 0,1),
     new Vector3(-0,5, -0,5, 0,1), new Vector3(-0,5, -0,5, 0,1),
     new Vector3(-0,5, -0,5, 0,1), new Vector3(-0,5, -0,5, 0,1),
     new Vector3(-0,5, -0,5, 0,1), new Vector3(-0,5, -0,5, 0,1),
     new Vector3(-0,5, -0,5, 0,1), new Vector3(-0,5, -0,5, 0,1),
     new Vector3(-0,5, -0,5, 0,1), new Vector3(-0,5, -0,5, 0,1),
     new Vector3(-0,5, -0,5, 0,1), new Vector3(-0,5, -0,5, 0,1),
     new Vector3(-0,5, -0,5, 0,1), new Vector3(-0,5, -0,5, 0,1),
     new Vector3(-0,5, -0,5, 0,1), new Vector3(-0,5, -0,5, 0,1),
     new Vector3(-0,5, -0,5, 0,1), new Vector3(-0,5, -0,5, 0,1),
     new Vector3(-0,5, -0,5, 0,1), new Vector3(-0,5, -0,5, 0,1),
     new Vector3(0,5, 0,5, 0,1), new Vector3(0,5, -0,5, 0,1)
     };
     public int[] tris = {
         0,1,4,1,5,4,1,2,5,2,6,5,2,3,6,3,7,6,3,0,7,0,4,7,8,9,12,9,13,12,9,10,13,10,14,13,10,11,14,11,15,14,11,8,15,8,12,15,20,0,3,20,3,21,0,20,46,0,46,1,1,46,47,1,47,2,3,2,42,2,47,45,46,20,17,20,16,17,20,19,16,20,32,15,31,14,15,31,47,14,47,46,14,46,17,14
     };
 
    void Start(){
    MeshFilter mF = gameObject.GetComponent<MeshFilter> ();
    Mesh msh = new Mesh ();
    msh.vertices = verts;
    msh.triangles = tris;
    Vector2[] uvs = new Vector2[verts.Length];
    for (int i = 0; i < uvs.Length; i++){ //uv map
        uvs[i] = new Vector2(verts[i].x, verts[i].y);
     }
     msh.uv = uvs;
     msh.RecalculateNormals ();
     mF.mesh = msh;
     }

Does that code even compile? It includes code (lines 31+) alongside of public variable declarations… did you just copy/paste from two areas of code? Not only that but all Vector3 constructors are given doubles, and they are specified to only take float, AFAIK.

I edited the post, the code is inside void Start, I had just copied it from my code to the post, now it is in accordance with my actual code. I compiled yes, the result is as in the image, the texture is strange, I don’t know if I did something wrong with uv mapping via code

Someone can help me pls?

Your code worked for me, best I can tell. Though I’m not sure how you’re getting your code to compile when using the European style decimal comma…

1 Like

I just changed the shader and it worked, I was using a shader that uses global position for the textures, but it didn’t work with this shader, can you help me why?

Shader "Custom/GlobalPosition"
{
    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
        #pragma surface surf Standard fullforwardshadows vertex:vert

    void vert(inout appdata_full v) {

        // Get the worldspace normal after transformation, and ensure it's unit length.
        float3 n = normalize(mul(unity_ObjectToWorld, v.normal).xyz);

        // Pick a direction for our texture's vertical "v" axis.
        // Default for floors/ceilings:
        float3 vDirection = float3(0, 0, 1);

        // For non-horizontal planes, we'll choose
        // the closest vector in the polygon's plane to world up.
        if(abs(n.y) < 1.0f) {
             vDirection = normalize(float3(0, 1, 0) - n.y * n);
        }

        // Get the perpendicular in-plane vector to use as our "u" direction.
        float3 uDirection = normalize(cross(n, vDirection));

        // Get the position of the vertex in worldspace.
        float3 worldSpace = mul(unity_ObjectToWorld, v.vertex).xyz;

        // Project the worldspace position of the vertex into our texturing plane,
        // and use this result as the primary texture coordinate.
        v.texcoord.xy = float2(dot(worldSpace, uDirection), dot(worldSpace, vDirection));
    }
        // 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;

        struct Input
        {
            float2 uv_MainTex;
        };

        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, IN.uv_MainTex) * _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"
}