Skip to content

Commit

Permalink
Added E02
Browse files Browse the repository at this point in the history
  • Loading branch information
SebLague committed Aug 23, 2018
1 parent d6c9d4f commit ce482fe
Show file tree
Hide file tree
Showing 38 changed files with 4,050 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Procedural Planet E02/Assets/ColourSettings.cs
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 ColourSettings : ScriptableObject {

public Color planetColour;
}
11 changes: 11 additions & 0 deletions Procedural Planet E02/Assets/ColourSettings.cs.meta

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

8 changes: 8 additions & 0 deletions Procedural Planet E02/Assets/Editor.meta

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

61 changes: 61 additions & 0 deletions Procedural Planet E02/Assets/Editor/PlanetEditor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(Planet))]
public class PlanetEditor : Editor {

Planet planet;
Editor shapeEditor;
Editor colourEditor;

public override void OnInspectorGUI()
{
using (var check = new EditorGUI.ChangeCheckScope())
{
base.OnInspectorGUI();
if (check.changed)
{
planet.GeneratePlanet();
}
}

if (GUILayout.Button("Generate Planet"))
{
planet.GeneratePlanet();
}

DrawSettingsEditor(planet.shapeSettings, planet.OnShapeSettingsUpdated, ref planet.shapeSettingsFoldout, ref shapeEditor);
DrawSettingsEditor(planet.colourSettings, planet.OnColourSettingsUpdated, ref planet.colourSettingsFoldout, ref colourEditor);
}

void DrawSettingsEditor(Object settings, System.Action onSettingsUpdated, ref bool foldout, ref Editor editor)
{
if (settings != null)
{
foldout = EditorGUILayout.InspectorTitlebar(foldout, settings);
using (var check = new EditorGUI.ChangeCheckScope())
{
if (foldout)
{
CreateCachedEditor(settings, null, ref editor);
editor.OnInspectorGUI();

if (check.changed)
{
if (onSettingsUpdated != null)
{
onSettingsUpdated();
}
}
}
}
}
}

private void OnEnable()
{
planet = (Planet)target;
}
}
11 changes: 11 additions & 0 deletions Procedural Planet E02/Assets/Editor/PlanetEditor.cs.meta

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

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

public class Planet : MonoBehaviour {

[Range(2,256)]
public int resolution = 10;
public bool autoUpdate = true;

public ShapeSettings shapeSettings;
public ColourSettings colourSettings;

[HideInInspector]
public bool shapeSettingsFoldout;
[HideInInspector]
public bool colourSettingsFoldout;

ShapeGenerator shapeGenerator;

[SerializeField, HideInInspector]
MeshFilter[] meshFilters;
TerrainFace[] terrainFaces;


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 };

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();
}

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

public void GeneratePlanet()
{
Initialize();
GenerateMesh();
GenerateColours();
}

public void OnShapeSettingsUpdated()
{
if (autoUpdate)
{
Initialize();
GenerateMesh();
}
}

public void OnColourSettingsUpdated()
{
if (autoUpdate)
{
Initialize();
GenerateColours();
}
}

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

void GenerateColours()
{
foreach (MeshFilter m in meshFilters)
{
m.GetComponent<MeshRenderer>().sharedMaterial.color = colourSettings.planetColour;
}
}
}
11 changes: 11 additions & 0 deletions Procedural Planet E02/Assets/Planet.cs.meta

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

8 changes: 8 additions & 0 deletions Procedural Planet E02/Assets/Scenes.meta

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

Loading

0 comments on commit ce482fe

Please sign in to comment.