Skip to content

Instantly share code, notes, and snippets.

@jquave
Created July 12, 2020 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jquave/af3910e0662f9ba2a2b296bb4d57e9c7 to your computer and use it in GitHub Desktop.
Save jquave/af3910e0662f9ba2a2b296bb4d57e9c7 to your computer and use it in GitHub Desktop.
//using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor.SceneManagement;
using UnityEngine;
public class MeshGen : MonoBehaviour
{
int treeSpokes = -1;
float rspd = 0.175f;
float branchWidth = 0.175f;
public float innerHeightOffset = 2.5f;
(Vector3[], int[], Vector3[]) genTreeVerts()
{
float jaggedNess = 1.1f;
if (treeSpokes == -1)
{
treeSpokes = Random.Range(3, 8);
}
int leafSides = 12;
int leafTriangleCount = leafSides * 3;
int numVerts = leafTriangleCount * treeSpokes;
//int numVerts = 3;
float h = 0.01f;
Vector3[] verts = new Vector3[numVerts];
int[] triIndexes = new int[numVerts];
Vector3[] normals = new Vector3[numVerts];
int row = 0;
RaycastHit hitInfo;
bool hasHit = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo);
for (var i = 0; i < numVerts; i++)
{
//float randomY = Random.Range(0.0f, 1.0f);
float randomY = 0.0f;
if (i % (leafSides * 3) == 0)
{
row++;
}
float innerRadius = 0.01f;
float outerRadius = 0.25f;
//float mOuterRadius = outerRadius * (2 - Mathf.Log(i + 1));
float mOuterRadius = outerRadius * (treeSpokes - row + 5);
if ((i - 1) % 3 == 0)
{
// Every third element should hit the middle of the "tree"
verts[i] = new Vector3(innerRadius * Mathf.Cos(i * rspd), row + innerHeightOffset, innerRadius * Mathf.Sin(i * rspd));
normals[i] = verts[i];
//normals[i] = Vector3.up;
}
else if ((i + 1) % 3 == 0)
{
verts[i] = new Vector3(mOuterRadius * Mathf.Cos(i * rspd + branchWidth), row + randomY, mOuterRadius * Mathf.Sin(i * rspd + branchWidth));
normals[i] = verts[i];
}
else
{
verts[i] = new Vector3(mOuterRadius * Mathf.Cos(i * rspd), row + randomY, mOuterRadius * Mathf.Sin(i * rspd));
normals[i] = verts[i];
}
//Vector3 distToVert = hitInfo.point - verts[i];
//verts[i] += 0.1f * distToVert;
triIndexes[i] = i;
}
return (verts, triIndexes, normals);
/*var reverseTris = new int[numVerts];
triIndexes.CopyTo(reverseTris, 0);
System.Array.Reverse(reverseTris);
var allTris = new int[numVerts * 2];
triIndexes.CopyTo(allTris, 0);
reverseTris.CopyTo(allTris, numVerts);
return (verts, allTris, normals);*/
}
MeshRenderer mr;
Mesh m;
MeshFilter mf;
void Start()
{
mr = gameObject.AddComponent<MeshRenderer>();
//mr.sharedMaterial = new Material(Shader.Find("Standard"));
mr.sharedMaterial = new Material(Shader.Find("HDRP/Lit"));
mr.sharedMaterial.SetColor("_BaseColor", Color.green);
m = new Mesh();
mf = gameObject.AddComponent<MeshFilter>();
}
// Update is called once per frame
void Update()
{
var (verts, indexes, normals) = genTreeVerts();
m.vertices = verts;
m.triangles = indexes;
m.normals = normals;
mf.mesh = m;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment