forked from SebLague/Procedural-Planets
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
38 changed files
with
4,050 additions
and
0 deletions.
There are no files selected for viewing
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
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; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.