Home Game Development unity – Producing UVs for an annular sector/ wedge-shaped procedural mesh

unity – Producing UVs for an annular sector/ wedge-shaped procedural mesh

0
unity – Producing UVs for an annular sector/ wedge-shaped procedural mesh

[ad_1]

I’m utilizing code to generate a mesh to point out the firing arcs for a tabletop-style recreation I’m engaged on. However when making use of a fabric to it, I solely get a strong colour.

Custom Mesh and Test Material

Right here you may see the customized mesh and a Quad with the identical check materials utilized.

Custom Mesh's Properities
Custom Mesh's UV Checker

Any assist could be appreciated as I’m extra of a logic coder and am solely simply dipping my toes into this Customized Mesh coding. I think it has one thing to do with the UVs however undecided find out how to appropriate it whether it is.

Right here is the code I’m utilizing to generate this mesh:

utilizing UnityEngine;
utilizing System;
utilizing System.Linq;
utilizing System.Collections;
utilizing System.Collections.Generic;

public class FiringArcMeshGenerator : MonoBehaviour
{
    /// <abstract>
    /// Credit score to Knight666 (knight666.com) & Rickz0r 
    /// https://gamedev.stackexchange.com/questions/31170/drawing-a-dynamic-indicator-for-a-field-of-view
    /// Code modified for modular use by ThomFoxx
    /// </abstract>
    [SerializeField] non-public int _quality = 6;
    non-public Mesh _arcMesh;

    [SerializeField] non-public bool _isPrimary;
    [SerializeField] non-public float _primaryAngle = 45, _auxAngle = 180;

    non-public float _angleOfFire;

    non-public float _minDist;
    non-public float _maxDist;
    [SerializeField] non-public Vector3[] _verts;
    [SerializeField] non-public int[] _tris;
    non-public Vector2[] _uv;

    public void MeshSetup(ShipInfo ship)
    {
        WeaponInfo weapon;
        _arcMesh = new Mesh();
        if (_isPrimary)
        {
            _angleOfFire = _primaryAngle / 2;
            weapon = ship.Major;
            _arcMesh.title = "Major Arc";
        }
        else
        {
            _angleOfFire = _auxAngle / 2;
            _quality *= 2;
            weapon = ship.Auxiliary;
            _arcMesh.title = "Auxiliary Arc";
        }

        _minDist = weapon.MinRange;
        _maxDist = weapon.MaxRange;

        

        _arcMesh.vertices = new Vector3[4 * _quality];
        _arcMesh.triangles = new int[3 * 2 * _quality];

        Vector3[] normals = new Vector3[4 * _quality];
        

        

        for (int i = 0; i < normals.Size; i++)
            normals[i] = new Vector3(0, 1, 0);

        
        _arcMesh.normals = normals;

        GenerateMesh();

        GetComponent<MeshFilter>().mesh = _arcMesh;
    }

    void GenerateMesh()
    {
        float angle_lookat = 0;

        float angle_start = angle_lookat - _angleOfFire;
        float angle_end = angle_lookat + _angleOfFire;
        float angle_delta = (angle_end - angle_start) / _quality;

        float angle_curr = angle_start;
        float angle_next = angle_start + angle_delta;

        Vector3 pos_curr_min = Vector3.zero;
        Vector3 pos_curr_max = Vector3.zero;

        Vector3 pos_next_min = Vector3.zero;
        Vector3 pos_next_max = Vector3.zero;

        Vector3[] vertices = new Vector3[4 * _quality];
        int[] triangles = new int[3 * 2 * _quality];

        for (int i = 0; i < _quality; i++)
        {
            Vector3 sphere_curr = new Vector3(
                Mathf.Sin(Mathf.Deg2Rad * (angle_curr)), 
                0f,
                Mathf.Cos(Mathf.Deg2Rad * (angle_curr)));

            Vector3 sphere_next = new Vector3(
                Mathf.Sin(Mathf.Deg2Rad * (angle_next)), 
                0f,
                Mathf.Cos(Mathf.Deg2Rad * (angle_next)));

            pos_curr_min = remodel.place + sphere_curr * _minDist;
            pos_curr_max = remodel.place + sphere_curr * _maxDist;

            pos_next_min = remodel.place + sphere_next * _minDist;
            pos_next_max = remodel.place + sphere_next * _maxDist;

            int a = 4 * i;
            int b = 4 * i + 1;
            int c = 4 * i + 2;
            int d = 4 * i + 3;

            vertices[a] = pos_curr_min;
            vertices[b] = pos_curr_max;
            vertices[c] = pos_next_max;
            vertices[d] = pos_next_min;            

            angle_curr += angle_delta;
            angle_next += angle_delta;

        }        
        
        _verts = vertices.Distinct().ToArray();
        _tris = new int[3 * 2 * _quality];

        CalculateTrianlges();

        _uv = new Vector2[_verts.Length];
        for (int i = 0; i < _verts.Size; i++)
        {
            _uv[i]=new Vector2(_verts[i].x, _verts[i].z);
        }
        


        _arcMesh.Clear();        
        _arcMesh.vertices = _verts;
        _arcMesh.triangles = _tris;
        _arcMesh.uv = _uv;
        _arcMesh.RecalculateNormals();
        _arcMesh.RecalculateUVDistributionMetrics();
        //_arcMesh.OptimizeReorderVertexBuffer();
    }

    non-public void CalculateTrianlges()
    {
        if (_minDist >0)
        {
            //takes the preliminary odd numbering on account of Distinct Operate
            _tris[0] = 0;
            _tris[1] = 1;
            _tris[2] = 3;

            _tris[3] = 1;
            _tris[4] = 2;
            _tris[5] = 3;

            _tris[6] = 2;
            _tris[7] = 5;
            _tris[8] = 3;

            _tris[9] = 4;
            _tris[10] = 5;
            _tris[11] = 2;

            for (int i = 4; i < _verts.Size - 3; i += 2)
            {
                _tris[i * 3] = i;
                _tris[i * 3 + 1] = i + 2;
                _tris[i * 3 + 2] = i + 3;
            }
            for (int i = 5; i < _verts.Size - 2; i += 2)
            {
                _tris[i * 3] = i;
                _tris[i * 3 + 1] = i - 1;
                _tris[i * 3 + 2] = i + 2;
            }
        }
        else
        {
            //nonetheless creating
        }
    }
}

Edit: With the repair of a lacking command, Thanks @DMGregory, It is not a single colour and reveals the feel. However how do I get it to deform to the mesh? Proper now it tiles, however I would like it to stretch the one texture over the single-sided mesh.

Tiling Texture that needs to conform to the Mesh Shape

Sorry for the unhealthy paintbrush artwork, however that is what I’m aiming for. 00 could be within the backside left nook (Purple) with 33 within the prime proper nook (Gold/Yellow).
Bad paintbrush sketch

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here