Skip to content

Commit

Permalink
3D Julia set for Mandelbulb
Browse files Browse the repository at this point in the history
- Mandelbulb julia set
- code cleanup & bugfixes
  • Loading branch information
srpnt3 committed May 5, 2020
1 parent e7ed967 commit 246afab
Show file tree
Hide file tree
Showing 29 changed files with 4,762 additions and 2,662 deletions.
231 changes: 119 additions & 112 deletions Fractals Project/Assets/Scenes/InfiniteSpheres.unity

Large diffs are not rendered by default.

1,001 changes: 301 additions & 700 deletions Fractals Project/Assets/Scenes/Mandelbrot.unity

Large diffs are not rendered by default.

1,278 changes: 797 additions & 481 deletions Fractals Project/Assets/Scenes/Mandelbulb.unity

Large diffs are not rendered by default.

541 changes: 190 additions & 351 deletions Fractals Project/Assets/Scenes/MengerSponge.unity

Large diffs are not rendered by default.

432 changes: 196 additions & 236 deletions Fractals Project/Assets/Scenes/RayMarching2D.unity

Large diffs are not rendered by default.

528 changes: 264 additions & 264 deletions Fractals Project/Assets/Scenes/RayMarching3D.unity

Large diffs are not rendered by default.

531 changes: 269 additions & 262 deletions Fractals Project/Assets/Scenes/Sierpinski.unity

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions Fractals Project/Assets/Scripts/Mandelbrot/Mandelbrot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ protected override void Render(RenderTexture s) {

// calculate some things
CalculateArea();
iterations = iterations / 20 * 20;
ComputeBuffer colors = new ComputeBuffer(colorGradient.Length, sizeof(float) * 3);
colors.SetData(colorGradient);

Expand Down Expand Up @@ -107,8 +106,8 @@ public Vector2 O_Coordinates {
}

public float O_Iterations {
get => iterations;
set => iterations = Mathf.RoundToInt(value);
get => iterations;
set => iterations = Mathf.RoundToInt(value / 20) * 20;
}

public bool O_Julia {
Expand Down
18 changes: 11 additions & 7 deletions Fractals Project/Assets/Scripts/Mandelbulb/Mandelbulb.compute
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ float4x4 CamInverseProjection;

// variables
float Power;
int Steps;
int Iterations;
bool Julia;
float3 C;
float Mix;

// constants
static const int steps = 300;
static const float epsilon = 0.0001;

// ray
Expand Down Expand Up @@ -50,7 +52,7 @@ float2 DE(float3 p) {
while(i < Iterations) {
r = length(z);

if (r > 2) break;
if (r > 3) break;

// convert to polar coordinates
float theta = acos(z.z / r);
Expand All @@ -64,13 +66,13 @@ float2 DE(float3 p) {

// convert back to cartesian coordinates
z = zr * float3(sin(theta) * cos(phi), sin(phi) * sin(theta), cos(theta));
z += p;
z += Julia ? C : p;

// next iteration
i++;
}
float dst = 0.5 * log(r) * r / dr;
return float2(dst, sin(i));
return float2(dst / 2, sin(i));
}

// cast a ray and return the result
Expand All @@ -79,15 +81,15 @@ float3 March(Ray ray) {
float3 eye = ray.origin;

int s = 0;
while (s < Steps) {
while (s < steps) {
d = DE(ray.origin); // calculate distance
if (length(eye - ray.origin) > 100) s = Steps; // to far away
if (length(eye - ray.origin) > 100) /*break;*/ s = steps; // to far away
if (d.x < epsilon) break; // hit
ray.origin += ray.direction * d.x; // march
s++; // next iteration
}

return float3(1 - s / float(Steps), length(eye - ray.origin), d.y);
return float3(1 - s / float(steps), length(eye - ray.origin), d.y);
}

// main function
Expand All @@ -105,4 +107,6 @@ void CSMain (uint3 id : SV_DispatchThreadID) {
float3 res = March(ray);
float a = res.x * (2 - Mix * 2) + res.z * (Mix * 2);
Texture[id.xy] = Source[id.xy] + (float4(a, a, a, 0) * float4(0.8, 0.2, 0.2, 0));
/*float4 ao = res.x * float4(0.1, 0.74, 0.61, 0);
Texture[id.xy] = Source[id.xy] + ao;*/
}
25 changes: 15 additions & 10 deletions Fractals Project/Assets/Scripts/Mandelbulb/Mandelbulb.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System;
using UnityEngine;
using UnityEngine.PlayerLoop;
using UnityEngine;

public class Mandelbulb : App {

private float power = 11;
private int steps = 100;
private int iterations = 8;
private bool julia = false;
private Vector3 c = Vector3.zero;
private float mix = 0.5f;

private void Start() {
Expand All @@ -22,8 +21,9 @@ protected override void Render(RenderTexture s) {
shader.SetMatrix("CamToWorld", cam.cameraToWorldMatrix);
shader.SetMatrix("CamInverseProjection", cam.projectionMatrix.inverse);
shader.SetFloat("Power", power);
shader.SetInt("Steps", steps);
shader.SetInt("Iterations", iterations);
shader.SetBool("Julia", julia);
shader.SetVector("C", c);
shader.SetFloat("Mix", mix);

shader.Dispatch(0, Mathf.CeilToInt(w / 8), Mathf.CeilToInt(h / 8), 1);
Expand All @@ -36,15 +36,20 @@ public float O_Power {
set => power = value;
}

public float O_Steps {
get => steps;
set => steps = Mathf.RoundToInt(value);
}

public float O_Iterations {
get => iterations;
set => iterations = Mathf.RoundToInt(value);
}

public bool O_Julia {
get => julia;
set => julia = value;
}

public Vector3 O_C {
get => c;
set => c = value;
}

public float O_Mix {
get => mix;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Collections;
using System.Collections.Generic;
using TMPro;
using TMPro;
using UnityEngine;

public class O_Single : MonoBehaviour {
Expand Down Expand Up @@ -28,7 +26,7 @@ private void Update() {
}
}

public void OnValueChanged(string val) {
public void OnEndEdit(string val) {
if (ready) {
app.SetOption(optionName, float.Parse(val));
}
Expand Down
61 changes: 61 additions & 0 deletions Fractals Project/Assets/Scripts/UI/Options/O_Slider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class O_Slider : MonoBehaviour {

public App app;
public string optionName;
public float min;
public float max;
public bool wholeNumbers;

// included in prefab
public Slider slider;
public TMP_InputField inputField;
private bool typing;
//public TextMeshProUGUI value;
private bool ready;

private void Start() {
slider.maxValue = max;
slider.minValue = min;
slider.wholeNumbers = wholeNumbers;
float value = (float) app.GetOption(optionName);
slider.value = value;
inputField.text = value.ToString();

// design stuff
transform.GetChild(0).GetComponent<TextMeshProUGUI>().text = name;
inputField.textComponent.alignment = TextAlignmentOptions.MidlineRight;
ready = true;
}

private void Update() {
float value = (float) app.GetOption(optionName);
slider.value = value;
if (!typing) {
inputField.text = value.ToString();
}
}

public void OnValueChanged(float val) {
if (ready) {
app.SetOption(optionName, val);
}
}

public void OnEndEdit(string val) {
if (ready) {
app.SetOption(optionName, Mathf.Clamp(float.Parse(val), slider.minValue, slider.maxValue));
}
}

public void OnSelect() {
typing = true;
}

public void OnDeselect() {
typing = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ public class O_Vec2 : MonoBehaviour {
private bool ready;

private void Start() {
inputFieldA.text = ((Vector2) app.GetOption(optionName)).x.ToString();
inputFieldB.text = ((Vector2) app.GetOption(optionName)).y.ToString();
Vector2 value = (Vector2) app.GetOption(optionName);
inputFieldA.text = value.x.ToString();
inputFieldB.text = value.y.ToString();

// design stuff
transform.GetChild(0).GetComponent<TextMeshProUGUI>().text = name;
Expand All @@ -25,12 +26,13 @@ private void Start() {

private void Update() {
if (!typing) {
inputFieldA.text = ((Vector2) app.GetOption(optionName)).x.ToString();
inputFieldB.text = ((Vector2) app.GetOption(optionName)).y.ToString();
Vector2 value = (Vector2) app.GetOption(optionName);
inputFieldA.text = value.x.ToString();
inputFieldB.text = value.y.ToString();
}
}

public void OnValueChanged() {
public void OnEndEdit() {
if (ready) {
app.SetOption(optionName, new Vector2(float.Parse(inputFieldA.text), float.Parse(inputFieldB.text)));
}
Expand Down
52 changes: 52 additions & 0 deletions Fractals Project/Assets/Scripts/UI/Options/O_Vec3.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using TMPro;
using UnityEngine;

public class O_Vec3 : MonoBehaviour {

public App app;
public string optionName;

// included in prefab
public TMP_InputField inputFieldA;
public TMP_InputField inputFieldB;
public TMP_InputField inputFieldC;
private bool typing;
private bool ready;

private void Start() {
Vector3 value = (Vector3) app.GetOption(optionName);
inputFieldA.text = value.x.ToString();
inputFieldB.text = value.y.ToString();
inputFieldC.text = value.z.ToString();

// design stuff
transform.GetChild(0).GetComponent<TextMeshProUGUI>().text = name;
inputFieldA.textComponent.alignment = TextAlignmentOptions.MidlineRight;
inputFieldB.textComponent.alignment = TextAlignmentOptions.MidlineRight;
inputFieldC.textComponent.alignment = TextAlignmentOptions.MidlineRight;
ready = true;
}

private void Update() {
if (!typing) {
Vector3 value = (Vector3) app.GetOption(optionName);
inputFieldA.text = value.x.ToString();
inputFieldB.text = value.y.ToString();
inputFieldC.text = value.z.ToString();
}
}

public void OnEndEdit() {
if (ready) {
app.SetOption(optionName, new Vector3(float.Parse(inputFieldA.text), float.Parse(inputFieldB.text), float.Parse(inputFieldC.text)));
}
}

public void OnSelect() {
typing = true;
}

public void OnDeselect() {
typing = false;
}
}
11 changes: 11 additions & 0 deletions Fractals Project/Assets/Scripts/UI/Options/O_Vec3.cs.meta

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

8 changes: 4 additions & 4 deletions Fractals Project/Assets/Scripts/Utils/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ private IEnumerator TakeScreenshot() {

// take
yield return new WaitForEndOfFrame();
String path = Application.persistentDataPath + "/screenshots/" + shader.name + "/";
String path = Application.persistentDataPath + "/screenshots/" + shader.name + "/" + w + "x" + h + "/";
Directory.CreateDirectory(path);
String name = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".png";
ScreenCapture.CaptureScreenshot(path + name);
Debug.Log("Screenshot saved as " + name);
Expand Down Expand Up @@ -117,8 +118,8 @@ private void Awake() {
c.Default.Look.canceled += ctx => cursor = Vector2.zero;
c.Default.Tilt.performed += ctx => tilt = ctx.ReadValue<float>();
c.Default.Tilt.canceled += ctx => tilt = 0;
c.Default.ScreenClick.performed += ctx => { StartDrag(); };
c.Default.ScreenClick.canceled += ctx => { EndDrag(); SwitchCursor(); };
c.Default.ScreenClick.performed += ctx => { StartDrag(); SwitchCursor(); };
c.Default.ScreenClick.canceled += ctx => { EndDrag(); };
c.Default.Back.canceled += ctx => { Cursor.lockState = CursorLockMode.None; Cursor.visible = true; GetComponent<SceneLoader>().Load(0); };
c.Default.ToggleOptions.canceled += ctx => { options.SetActive(!options.activeSelf); };
c.Default.Zoom.performed += ctx => deltaZoom = ctx.ReadValue<float>();
Expand All @@ -128,7 +129,6 @@ private void Awake() {

// enable controls
private void OnEnable() {
Directory.CreateDirectory(Application.persistentDataPath + "/screenshots/" + shader.name + "/");
cam = GetComponent<Camera>();
c.Enable();
}
Expand Down
43 changes: 0 additions & 43 deletions Fractals Project/Assets/Scripts/Utils/Options/O_Slider.cs

This file was deleted.

Loading

0 comments on commit 246afab

Please sign in to comment.