Last active
October 18, 2016 02:57
-
-
Save pacoelayudante/64b6dd38b1647ae40b4f434e99879a55 to your computer and use it in GitHub Desktop.
Genera un prisma en Unity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class GenMallaPrisma : MonoBehaviour | |
{ | |
[System.Serializable] | |
public class Segmento | |
{ | |
public float largo = 1, radio = .5f; | |
} | |
class LadoGen | |
{ | |
public LadoGen(Vector3 arrizq, Vector3 arrder, Vector3 abaizq, Vector3 abader) | |
{ | |
verts = new Vector3[] { arrizq, arrder, abaizq, abader }; | |
} | |
public Vector3[] verts; | |
public int[] tris = new int[] { 0, 2, 1, 1, 2, 3 }; | |
} | |
public MeshFilter mFilter; | |
public int cantidadLados = 3; | |
public bool taponSuperior, taponInferior; | |
public float offsetTaponSuperior, offsetTaponInferior; | |
public Segmento[] segmentos; | |
void OnValidate() | |
{ | |
if (cantidadLados < 2) cantidadLados = 2; | |
} | |
void Reset() | |
{ | |
mFilter = GetComponent<MeshFilter>(); | |
} | |
[ContextMenu("Aplicar")] | |
public void Aplicar() | |
{ | |
if (!mFilter) mFilter = GetComponent<MeshFilter>(); | |
if (mFilter)mFilter.sharedMesh = GenerarMalla(); | |
} | |
public Mesh GenerarMalla() | |
{ | |
if (segmentos.Length > 0) | |
{ | |
int cantidadDeLadosTotal = cantidadLados * segmentos.Length; | |
LadoGen[] lados = new LadoGen[cantidadDeLadosTotal]; | |
Vector3[] rayos = new Vector3[cantidadLados + 1]; | |
for (int i = 0; i < rayos.Length; i++) | |
{ | |
rayos[i] = Quaternion.Euler(0, (i * 360f) / cantidadLados, 0) * Vector3.forward; | |
} | |
float alturaPrevia = 0; | |
for (int i = 0; i < segmentos.Length; i++) | |
{ | |
float alturaActual = alturaPrevia+segmentos[i].largo; | |
float radioBase = segmentos[i].radio; | |
float radioTope = i == segmentos.Length - 1 ? segmentos[i].radio : segmentos[i + 1].radio; | |
for (int l = 0; l < cantidadLados; l++) | |
{ | |
lados[i * cantidadLados + l] = new LadoGen(rayos[l] * radioTope + Vector3.up * alturaActual, rayos[l + 1] * radioTope + Vector3.up * alturaActual, | |
rayos[l] * radioBase + Vector3.up * alturaPrevia, rayos[l + 1] * radioBase + Vector3.up * alturaPrevia); | |
} | |
alturaPrevia = alturaActual; | |
} | |
Vector3[] puntos = new Vector3[4 * cantidadDeLadosTotal + (taponSuperior ? cantidadLados*3 : 0) + (taponInferior ? cantidadLados*3 : 0)]; | |
int[] tris = new int[cantidadDeLadosTotal * 6 + (taponSuperior ? cantidadLados*3: 0) + (taponInferior ? cantidadLados*3 : 0)]; | |
Mesh m = new Mesh(); | |
for (int i=0; i<lados.Length; i++) | |
{ | |
for (int v=0; v<lados[i].verts.Length; v++) | |
{ | |
puntos[i * lados[i].verts.Length + v] = lados[i].verts[v]; | |
} | |
for (int t = 0; t < lados[i].tris.Length; t++) | |
{ | |
tris[i * lados[i].tris.Length + t] = lados[i].tris[t]+i* lados[i].verts.Length; | |
} | |
} | |
if (taponSuperior) | |
{ | |
float radioTope = segmentos[segmentos.Length - 1].radio; | |
int offsetPuntos = cantidadDeLadosTotal * 4; | |
int offsetTris = cantidadDeLadosTotal * 6; | |
for (int t = 0; t < cantidadLados; t++) | |
{ | |
puntos[t*3 + offsetPuntos] = Vector3.up * (alturaPrevia + offsetTaponSuperior); | |
puntos[t*3+1 + offsetPuntos] = rayos[t] * radioTope + Vector3.up * alturaPrevia; | |
puntos[t*3+2 + offsetPuntos] = rayos[t+1] * radioTope + Vector3.up * alturaPrevia; | |
tris[offsetTris + t * 3] = offsetPuntos +t*3; | |
tris[offsetTris + t * 3 + 1] = offsetPuntos + t*3+1; | |
tris[offsetTris + t * 3 + 2] = offsetPuntos + t*3 + 2; | |
} | |
} | |
if (taponInferior) | |
{ | |
float radioBase = segmentos[0].radio; | |
int offsetPuntos = cantidadDeLadosTotal * 4 + (taponSuperior ? cantidadLados * 3 : 0) ; | |
int offsetTris = cantidadDeLadosTotal * 6 + (taponSuperior ? cantidadLados * 3 : 0); | |
for (int t = 0; t < cantidadLados; t++) | |
{ | |
puntos[t * 3 + offsetPuntos] = - Vector3.up * offsetTaponInferior; | |
puntos[t * 3 + 1 + offsetPuntos] = rayos[t] * radioBase; | |
puntos[t * 3 + 2 + offsetPuntos] = rayos[t + 1] * radioBase; | |
tris[offsetTris + t * 3] = offsetPuntos +t*3; | |
tris[offsetTris + t * 3 + 2] = offsetPuntos + t*3+1; | |
tris[offsetTris + t * 3 + 1] = offsetPuntos + t*3 + 2; | |
} | |
} | |
m.vertices = puntos; | |
m.triangles = tris; | |
m.RecalculateNormals(); | |
m.RecalculateBounds(); | |
return m; | |
} | |
else return null; | |
} | |
#if UNITY_EDITOR | |
void OnDrawGizmosSelected() | |
{ | |
/*if (mFilter) | |
{ | |
GUIStyle style = new GUIStyle(); | |
style.normal.textColor = Color.red; | |
if (mFilter.sharedMesh) | |
{ | |
Vector3[] vs = mFilter.sharedMesh.vertices; | |
for(int i=0; i<vs.Length; i++) | |
{ | |
UnityEditor.Handles.Label(mFilter.transform.TransformPoint(vs[i]+Vector3.up*0.0125f*i), i.ToString(), style); | |
} | |
} | |
}*/ | |
} | |
#endif | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment