- Home /
texturing a procedurally created mesh
for a top down 2d project i made a script that creates a cone shaped mesh to represent a weapon's cone of fire.
the cone is procedurally made using some values of the weapon such as it's range and spread angle.
now i'm attemting to throw a texture on this cone, but im having trouble understanding how to actually do this.
all i need is for a square texture to appear on the cone surface while the parts of the texture that are outside the shape bounds not show.
can i avoid UV editing somehow? if not how would i go about it considering the procedural nature of this?
here is the mesh script
using UnityEngine;
using System.Collections.Generic;
[RequireComponent(typeof(MeshFilter))]
public class WeaponConeMesh : MonoBehaviour {
Mesh mesh;
MeshRenderer meshRenderer;
Transform weapon;
float range;
float weaponAngle;
float coneAngle;
Vector3 projOrigin;
int numOfTriangles = 10;
List<Vector3> verticesList = new List<Vector3> ();
List<int> trianglesList = new List<int> ();
void Awake() {
mesh = GetComponent<MeshFilter>().mesh;
meshRenderer = GetComponent<MeshRenderer>();
weapon = transform.parent;
}
void Start() {
WeaponHandler weaponHandler = weapon.GetComponent<WeaponHandler>();
range = weaponHandler.range;
projOrigin = weaponHandler.projOrigin;
if (weaponHandler.angle <= 1) {
coneAngle = 1;
} else {
coneAngle = weaponHandler.angle;
}
transform.position = projOrigin;
MakeMeshData();
CreateMesh();
}
void MakeMeshData() {
float angleMultiplier = 1f;
float pointIncrement = 1f / (numOfTriangles / 2);
for (int i = 0; i < numOfTriangles + 2; i++) {
if (i == 0) {
verticesList.Add(new Vector3(0, 0, 0));
}
else {
verticesList.Add(Quaternion.Euler(new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, coneAngle * angleMultiplier)) * Vector3.up * range);
angleMultiplier -= pointIncrement;
}
}
for (int i = 0; i < numOfTriangles; i++) {
List<int> singleTriangle = new List<int> ();
singleTriangle.Add(0);
singleTriangle.Add(1 + i);
singleTriangle.Add(2 + i);
trianglesList.AddRange(singleTriangle);
}
}
void CreateMesh() {
mesh.Clear();
mesh.vertices = verticesList.ToArray();
mesh.triangles = trianglesList.ToArray();
}
}
Comment
Your answer
Follow this Question
Related Questions
Assigning UV Map to model at runtime 0 Answers
Procedural Uniform UVs On a Plane 2 Answers
"Baking" to 2d UV Map? 1 Answer
2 Textures on a single mesh 1 Answer