Question by
FuturePilot1701 · Aug 05, 2019 at 12:42 AM ·
texturemeshmaterialprocedural
How to add texture to a cube sphere?
I have been trying for a few weeks now to figure out how to generate procedural planets. I currently have a script that generates a cube sphere of any size / resolution. But I dont really understand materials/textures/UV mapping, etc. I have watched a tutorial on youtube that goes through the entire process but I want to create it myself, and the guy in the tutorial doesnt really explain what hes doing most of the time, and I dont understand some of the code. I guess I have 2 main questions.
1) what is the best way to add a material / color to a cube sphere? 2) how do I make the different faces match up when adding a material / color / height nosie?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CubeSphere : MonoBehaviour
{
public float radius;
public float resolution;
public List<Vector3> points;
public Vector3[] vertices;
public Color[] colors;
public GameObject point;
void Start()
{
int i = 0;
//bottom
for (float x = -radius; x <= radius; x += radius / resolution)
{
for (float z = -radius; z <= radius; z += radius / resolution)
{
points.Add(new Vector3(x, -radius, z));
i++;
}
}
//0XY
for (float y = -radius; y <= radius; y += radius / resolution)
{
for (float x = -radius; x <= radius; x += radius / resolution)
{
points.Add(new Vector3(x, y, -radius));
i++;
}
}
//0ZY
for (float y = -radius; y <= radius; y += radius / resolution)
{
for (float z = radius; z >= -radius; z -= radius / resolution)
{
points.Add(new Vector3(-radius, y, z));
i++;
}
}
//1XY
for (float y = -radius; y <= radius; y += radius / resolution)
{
for (float x = radius; x >= -radius; x -= radius / resolution)
{
points.Add(new Vector3(x, y, radius));
i++;
}
}
//1ZY
for (float y = -radius; y <= radius; y += radius / resolution)
{
for (float z = -radius; z <= radius; z += radius / resolution)
{
points.Add(new Vector3(radius, y, z));
i++;
}
}
//top
for (float x = -radius; x <= radius; x += radius / resolution)
{
for (float z = radius; z >= -radius; z -= radius / resolution)
{
points.Add(new Vector3(x, radius, z));
i++;
}
}
vertices = points.ToArray();
for (int n = 0; n < vertices.Length; n++)
{
Vector3 temp = vertices[n];
vertices[n] = temp.normalized * radius;
}
int[] triangles = new int[vertices.Length * 6];
//foreach (Vector3 vertex in vertices)
//{
// Instantiate(point, vertex, transform.rotation);
//}
int v = 0;
int index = 0;
int xSize = 3;
int zSize = 3;
if (resolution == 1)
{
xSize = 3;
zSize = 3;
}
if (resolution == 2)
{
xSize = 3 + 2;
zSize = 3 + 2;
}
if (resolution > 2)
{
xSize = 3 + (((int)resolution - 1) * 2);
zSize = 3 + (((int)resolution - 1) * 2);
}
for (int s = 0; s < 6; s++)
{
for (int x = 0; x < xSize - 1; x++)
{
for (int z = 0; z < zSize - 1; z++)
{
triangles[index] = v;
triangles[index + 1] = v + xSize;
triangles[index + 2] = v + 1;
triangles[index + 3] = v + xSize;
triangles[index + 4] = v + xSize + 1;
triangles[index + 5] = v + 1;
v++;
index += 6;
}
v++;
}
v += xSize;
}
//Vector2[] uv = new Vector2[vertices.Length];
//for (int u = 0; u < uv.Length; u++)
//{
// uv[u] = new Vector2(vertices[u].x, vertices[u].z);
//}
Mesh mesh = GetComponent<MeshFilter>().mesh;
mesh.Clear();
mesh.vertices = vertices;
mesh.triangles = triangles;
//mesh.uv = uv;
mesh.Optimize();
mesh.RecalculateNormals();
//GetComponent<MeshCollider>().sharedMesh = mesh;
}
}
Comment