Skip to content

Commit

Permalink
#3 SettingsEditor started, & finished up Spherical vertices for plane…
Browse files Browse the repository at this point in the history
…t object
  • Loading branch information
Gizmotronn committed Oct 31, 2021
1 parent 849a011 commit 38d2eea
Show file tree
Hide file tree
Showing 17 changed files with 2,253 additions and 43 deletions.
1,999 changes: 1,998 additions & 1 deletion Assets/PCG/Planet Procedural Generation/Planet Generation 1.unity

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu()]
public class ColorSettings : ScriptableObject
{
public Color planetColor;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/PCG/Planet Procedural Generation/Scripts/Editor.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(Planet))]
public class PlanetEditor : Editor
{
Planet planet;
public override void OnInspectorGUI() {
base.OnInspectorGUI();

DrawSettingsEditor(Planet.shapeSettings);
DrawSettingsEditor(Planet.colorSettings);
}

void DrawSettingsEditor(Object settings) {
Editor editor = CreateEditor(settings);
// Display it
editor.OnInspectorGUI();
}

private void OnEnable() {
planet = (Planet)target;
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

82 changes: 61 additions & 21 deletions Assets/PCG/Planet Procedural Generation/Scripts/Planet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,83 @@
using System.Collections.Generic;
using UnityEngine;

public class Planet : MonoBehaviour
{
public class Planet : MonoBehaviour {
[Range(2,256)]
public int resolution = 10;


// Planet attributes
public ShapeSettings shapeSettings;
public ColorSettings colorSettings;

ShapeGenerator shapeGenerator;


// Planet Meshes
[SerializeField, HideInInspector]
MeshFilter[] meshFilters;
TerrainFace[] terrainFaces;

private void OnValidate()
{
GeneratePlanet();
}

private void OnValidate() {
Initialize();
GenerateMesh();
}

void Initialize() {
if(meshFilters == null || meshFilters.Length == 0) {
void Initialize()
{
shapeGenerator = new ShapeGenerator(shapeSettings);
if (meshFilters == null || meshFilters.Length == 0)
{
meshFilters = new MeshFilter[6];
}
terrainFaces = new TerrainFace[6];

Vector3[] directions = {Vector3.up,Vector3.down,Vector3.left,Vector3,right,Vector3.forward,Vector3.back};
Vector3[] directions = { Vector3.up, Vector3.down, Vector3.left, Vector3.right, Vector3.forward, Vector3.back };

for(int i = 0; i < 6; i++) {
GameObject meshObj = new GameObject("mesh");
meshObj.transform.parent = transform;
for (int i = 0; i < 6; i++)
{
if (meshFilters[i] == null)
{
GameObject meshObj = new GameObject("mesh");
meshObj.transform.parent = transform;

meshObj.AddComponent<MeshRenderer>().sharedMaterial = new Material(Shader.Find("Standard"));
meshFilters[i] = meshObj.AddComponent<meshFilter>();
meshFilters[i].sharedMesh = new Mesh();
meshObj.AddComponent<MeshRenderer>().sharedMaterial = new Material(Shader.Find("Standard"));
meshFilters[i] = meshObj.AddComponent<MeshFilter>();
meshFilters[i].sharedMesh = new Mesh();
}

terrainFaces[i] = new TerrainFace(meshFilters[i].sharedMesh, resolution, directions[i]);
terrainFaces[i] = new TerrainFace(shapeGenerator, meshFilters[i].sharedMesh, resolution, directions[i]);
}
}

void GenerateMesh() {
foreach (TerrainFace face in terrainFaces) {
public void GeneratePlanet() { // Reset function - other functions will only act if one attribute has been changed - this regenerates the whole thing based on current params
Initialize();
GenerateMesh();
GenerateColors();
}

public void OnShapeSettingsUpdated() {
Initialize();
GenerateMesh(); // Calling ConstructMesh - we need this to use the attributes from our PlanetSettings
}

public void OnColorSettingsUpdated() {
Initialize();
GenerateColors();
}

void GenerateMesh()
{
foreach (TerrainFace face in terrainFaces)
{
face.ConstructMesh();
}
}
}

void GenerateColors() {
// Loop through the meshes and set the materials' colours based on the colour inside ColorSettings
foreach (MeshFilter m in meshFilters) // for each inside the array
{
m.GetComponent<MeshRenderer>().sharedMaterial.color = colorSettings.planetColor;
}
}
}
8 changes: 8 additions & 0 deletions Assets/PCG/Planet Procedural Generation/Scripts/Settings.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: dbfd93a1135b5497d9739bd17599d359, type: 3}
m_Name: Color
m_EditorClassIdentifier:
planetColor: {r: 0, g: 0.8565588, b: 1, a: 0}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: a9b63090aeeb0411bbf8ce26688c294f, type: 3}
m_Name: Shape
m_EditorClassIdentifier:
planetRadius: 2

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions Assets/PCG/Planet Procedural Generation/Scripts/ShapeGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ShapeGenerator
{
ShapeSettings settings; // Assigned in the constructor
public ShapeGenerator(ShapeSettings settings) {
this.settings = settings;
}

public Vector3 CalculatePointOnPlanet(Vector3 pointOnUnitSphere) {
return pointOnUnitSphere * settings.planetRadius;
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu()]
public class ShapeSettings : ScriptableObject {
public float planetRadius = 1;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 30 additions & 21 deletions Assets/PCG/Planet Procedural Generation/Scripts/TerrainFace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,57 @@
using UnityEngine;

public class TerrainFace {

ShapeGenerator shapeGenerator;
Mesh mesh;
int resolution;
Vector3 localUp; // What way it's facing
Vector3 localUp;
Vector3 axisA;
Vector3 axisB;

public TerrainFace(Mesh mesh, int resolution, Vector3 localup) {
public TerrainFace(ShapeGenerator shapeGenerator, Mesh mesh, int resolution, Vector3 localUp)
{
this.shapeGenerator = shapeGenerator;
this.mesh = mesh;
this.resolution = resolution;
this.localUp = localUp;

axisA = new Vector3(localUp.y, localUp.z, localUp.x);
axisB = new Vector3.Cross(localUp, axisA);
axisB = Vector3.Cross(localUp, axisA);
}

public ConstructMesh() {
Vector3[] vertices = new Vector3[resolution * resolution]; // Resolution = number of verices across a single face
int[] triangles = new int[(resolution-1)*(resolution-1)*2*3];
int triIndex = 0; // We can create triangles as long as the current vertex is not along the right or bottom edge
public void ConstructMesh()
{
Vector3[] vertices = new Vector3[resolution * resolution];
int[] triangles = new int[(resolution - 1) * (resolution - 1) * 6];
int triIndex = 0;

for(int y = 0; y < resolution; y++) {
for(int x = 0; x < resolution; x++) {
int i = x + y * resolution; // Number of iterations of the inner loop + outer loop
Vector2 percent = new Vector2(x, y) / (resolution - 1); // When x is at highest point, it will be equal to 1% (lowest point 0%) # how close to complete each loop is - where should the vertex be on each face?
Vector3 pointOnUnitCube = localUp + (percent.x-.5f)*2*axisA + (percent.y - .5f) * 2 * axisB;// ^How far along the axis we are
vertices[i] = pointOnUnitCube;
for (int y = 0; y < resolution; y++)
{
for (int x = 0; x < resolution; x++)
{
int i = x + y * resolution;
Vector2 percent = new Vector2(x, y) / (resolution - 1);
Vector3 pointOnUnitCube = localUp + (percent.x - .5f) * 2 * axisA + (percent.y - .5f) * 2 * axisB;
Vector3 pointOnUnitSphere = pointOnUnitCube.normalized; // Make the game object a sphere
vertices[i] = shapeGenerator.CalculatePointOnPlanet(pointOnUnitSphere);

// The two triangles of the square in the array
if(x != resolution - 1 && y != resolution - 1) {
if (x != resolution - 1 && y != resolution - 1)
{
triangles[triIndex] = i;
triangles[triIndex+1] = i+resolution+1;
triangles[triIndex+2] = i+resolution; // End of first triangle
triangles[triIndex + 1] = i + resolution + 1;
triangles[triIndex + 2] = i + resolution;

triangles[triIndex+3] = i;
triangles[triIndex+4] = i + 1;
triangles[triIndex+5] = i + resolution+1;
triangles[triIndex + 3] = i;
triangles[triIndex + 4] = i + 1;
triangles[triIndex + 5] = i + resolution + 1;
triIndex += 6;
}
}
}
mesh.Clear();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.RecalculateNormals();
}
}
}

0 comments on commit 38d2eea

Please sign in to comment.