Skip to content

Commit

Permalink
More graph stuff.
Browse files Browse the repository at this point in the history
  • Loading branch information
jedjoud10 committed Apr 16, 2024
1 parent d1c2781 commit 6494a67
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 51 deletions.
24 changes: 24 additions & 0 deletions Runtime/Generation/Compiler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Collections;
using System.Collections.Generic;
using System.IO;
using Unity.Mathematics;
#if UNITY_EDITOR
using UnityEditor;
using UnityEditor.AssetImporters;
#endif
using UnityEngine;

// The monobehaviour that will actually compile the transpiled code into a shader in the editor and automatically apply it
[ExecuteInEditMode]
public class VoxelGraphCompiler: MonoBehaviour {
// Takes in the voxel graph, transpiles it, and compiles it to a proper compute shader
public void Compile(VoxelGraph graph) {
#if UNITY_EDITOR
string source = graph.Transpile();
//AssetImportContext ctx = new AssetImportContext();
//ShaderUtil.CreateComputeShaderAsset(, source);
#else
Debug.LogError("Cannot transpile code at runtime");
#endif
}
}

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

12 changes: 12 additions & 0 deletions Runtime/Generation/Importer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#if UNITY_EDITOR
using UnityEngine;
using UnityEditor.AssetImporters;
using System.IO;

[ScriptedImporter(1, "customtahini")]
public class Importer : ScriptedImporter {
public override void OnImportAsset(AssetImportContext ctx) {

}
}
#endif
11 changes: 11 additions & 0 deletions Runtime/Generation/Importer.cs.meta

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

5 changes: 5 additions & 0 deletions Runtime/Generation/Noise.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ public Noise(Var<float> intensity, Var<float> scale) {
this.scale = scale;
}

public Noise() {
this.intensity = 1.0f;
this.scale = 1.0f;
}

public Type type;
public Var<float> intensity;
public Var<float> scale;
Expand Down
11 changes: 0 additions & 11 deletions Runtime/Generation/Test.cs

This file was deleted.

8 changes: 0 additions & 8 deletions Runtime/Generation/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,4 @@ public static Var<float> y(this Var<float3> vec3) {
public static Var<float> z(this Var<float3> vec3) {
return Var<float>.Identity;
}

// Evaluate an snoise type noise at a specific position. Short-hand for creating snoise and evaluating it
public static Var<float> Snoise<T>(this Noise, Var<T> position, Var<float> scale) {
Noise n = new Noise(1.0f, scale);
n.type = Noise.Type.Simplex;
return n.Evaluate(position);
}

}
15 changes: 7 additions & 8 deletions Runtime/Generation/Var.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Codice.Client.BaseCommands.Merge;
using System;
using System.Collections;
using System.Collections.Generic;
Expand All @@ -12,9 +11,6 @@ public class Var<T> {
// is the current value defined in the code as a constant (or an editor variable)
public bool IsStatic { get; internal set; }

// variable name that will be substituted
public string CodeName { get; internal set; }

// Dimensionality of a variable
public enum Dimensions {
One,
Expand Down Expand Up @@ -44,7 +40,6 @@ public static Var<T> Identity {
return new Var<T> {
Name = "identity_" + typeof(T).FullName,
IsStatic = false,
CodeName = "identity_" + typeof(T).FullName,
};
}
}
Expand All @@ -53,9 +48,8 @@ public static Var<T> Identity {
public static implicit operator Var<T>(T value) => new Var<T> {
Name = "_st_",
IsStatic = true,
CodeName = "_st_"
};

// Implict conversion from common types to this type
public static implicit operator Var<T>(Var<double> d) => Var<T>.Identity;
public static implicit operator Var<T>(Var<float> d) => Var<T>.Identity;
Expand All @@ -64,7 +58,12 @@ public static Var<T> Identity {

// Inject a custom variable that will update its value dynamically based on the given callback
// Mainly used to pass inputs from fields from the unity editor to the graph
public static Var<T> Inject(Func<T> callback) { return null; }
public static Var<T> Inject(Func<T> callback) {
// hook on before on graph execution (runtime)
// update "internal" value
// send value to shader using uniform (make sure name matches up)
return Var<T>.Identity;
}

// Common operators
public static Var<T> operator +(Var<T> a, Var<T> b) => a;
Expand Down
16 changes: 13 additions & 3 deletions Runtime/Generation/Graph.cs → Runtime/Generation/VoxelGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,20 @@
using UnityEngine;

// A voxel graph is the base class to inherit from to be able to write custom voxel stuff
public abstract class VoxelGraph {

[ExecuteInEditMode]
public class VoxelGraph: MonoBehaviour {
// Execute the voxel graph at a specific position and fetch the density and material values
public abstract void Execute(Var<float3> position, out Var<float> density, out Var<uint> material);
public virtual void Execute(Var<float3> position, out Var<float> density, out Var<uint> material) {
density = position.y();



material = 0;
}

// This transpile the voxel graph into HLSL code that can be executed on the GPU
// This can be done outside the editor, but shader compilation MUST be done in editor
public string Transpile() {
return "";
}
}
File renamed without changes.
34 changes: 22 additions & 12 deletions Runtime/Scenes/Demo.unity
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ RenderSettings:
m_ReflectionIntensity: 1
m_CustomReflection: {fileID: 0}
m_Sun: {fileID: 0}
m_IndirectSpecularColor: {r: 22118.799, g: 16557.268, b: 17818.814, a: 1}
m_IndirectSpecularColor: {r: 22120.986, g: 16555.002, b: 17805.19, a: 1}
m_UseRadianceAmbientProbe: 0
--- !u!157 &3
LightmapSettings:
Expand Down Expand Up @@ -1123,6 +1123,11 @@ PrefabInstance:
propertyPath: meshJobsPerFrame
value: 2
objectReference: {fileID: 0}
- target: {fileID: 305494748139102449, guid: 0e6936411dbff8245ae15ba99cdb64b9,
type: 3}
propertyPath: ambientOcclusionOffset
value: 0
objectReference: {fileID: 0}
- target: {fileID: 305494748139102449, guid: 0e6936411dbff8245ae15ba99cdb64b9,
type: 3}
propertyPath: voxelMaterials.Array.size
Expand Down Expand Up @@ -1221,7 +1226,7 @@ PrefabInstance:
- target: {fileID: 7158265105977027013, guid: 0e6936411dbff8245ae15ba99cdb64b9,
type: 3}
propertyPath: seed
value: 137
value: 0
objectReference: {fileID: 0}
- target: {fileID: 7158265105977027013, guid: 0e6936411dbff8245ae15ba99cdb64b9,
type: 3}
Expand All @@ -1231,17 +1236,17 @@ PrefabInstance:
- target: {fileID: 7158265105977027013, guid: 0e6936411dbff8245ae15ba99cdb64b9,
type: 3}
propertyPath: moduloSeed.x
value: 599
value: 116
objectReference: {fileID: 0}
- target: {fileID: 7158265105977027013, guid: 0e6936411dbff8245ae15ba99cdb64b9,
type: 3}
propertyPath: moduloSeed.y
value: -881
value: -588
objectReference: {fileID: 0}
- target: {fileID: 7158265105977027013, guid: 0e6936411dbff8245ae15ba99cdb64b9,
type: 3}
propertyPath: moduloSeed.z
value: -435
value: 117
objectReference: {fileID: 0}
- target: {fileID: 7158265105977027013, guid: 0e6936411dbff8245ae15ba99cdb64b9,
type: 3}
Expand All @@ -1258,6 +1263,11 @@ PrefabInstance:
propertyPath: worldScale.z
value: 1
objectReference: {fileID: 0}
- target: {fileID: 7158265105977027013, guid: 0e6936411dbff8245ae15ba99cdb64b9,
type: 3}
propertyPath: densityOffset
value: 0
objectReference: {fileID: 0}
- target: {fileID: 7158265105977027013, guid: 0e6936411dbff8245ae15ba99cdb64b9,
type: 3}
propertyPath: worldOffset.y
Expand All @@ -1271,7 +1281,7 @@ PrefabInstance:
- target: {fileID: 7158265105977027013, guid: 0e6936411dbff8245ae15ba99cdb64b9,
type: 3}
propertyPath: previewOpacity
value: 0.436
value: 0.638
objectReference: {fileID: 0}
- target: {fileID: 7158265105977027013, guid: 0e6936411dbff8245ae15ba99cdb64b9,
type: 3}
Expand All @@ -1281,17 +1291,17 @@ PrefabInstance:
- target: {fileID: 7158265105977027013, guid: 0e6936411dbff8245ae15ba99cdb64b9,
type: 3}
propertyPath: permutationSeed.x
value: -403
value: 452
objectReference: {fileID: 0}
- target: {fileID: 7158265105977027013, guid: 0e6936411dbff8245ae15ba99cdb64b9,
type: 3}
propertyPath: permutationSeed.y
value: -969
value: 634
objectReference: {fileID: 0}
- target: {fileID: 7158265105977027013, guid: 0e6936411dbff8245ae15ba99cdb64b9,
type: 3}
propertyPath: permutationSeed.z
value: 58
value: 536
objectReference: {fileID: 0}
- target: {fileID: 7158265105977027013, guid: 0e6936411dbff8245ae15ba99cdb64b9,
type: 3}
Expand All @@ -1311,7 +1321,7 @@ PrefabInstance:
- target: {fileID: 7158265105977027013, guid: 0e6936411dbff8245ae15ba99cdb64b9,
type: 3}
propertyPath: previewDensityFactor
value: 4
value: 5
objectReference: {fileID: 0}
- target: {fileID: 7158265105977027013, guid: 0e6936411dbff8245ae15ba99cdb64b9,
type: 3}
Expand All @@ -1321,12 +1331,12 @@ PrefabInstance:
- target: {fileID: 7158265105977027013, guid: 0e6936411dbff8245ae15ba99cdb64b9,
type: 3}
propertyPath: previewWorldOffset.x
value: -116.7
value: -1010.8
objectReference: {fileID: 0}
- target: {fileID: 7158265105977027013, guid: 0e6936411dbff8245ae15ba99cdb64b9,
type: 3}
propertyPath: previewWorldOffset.y
value: -82.36
value: -32.6
objectReference: {fileID: 0}
- target: {fileID: 7158265105977027013, guid: 0e6936411dbff8245ae15ba99cdb64b9,
type: 3}
Expand Down
13 changes: 5 additions & 8 deletions Runtime/Voxelizer/VoxelGeneratorTemplate.compute
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,23 @@

// Called for each voxel in the chunk to check the density and material at a specific position
void VoxelAt(float3 position, out float density, out uint material) {
density = position.y + snoise(position * 0.001) * 20;
material = 0;
return;


/*
float height = cellular(position.xz * 0.01).x;
height = exp(-4 * height) * sin(15 * 3.1415 * height);
height = exp(-4 * height) * sin(0.5 + 2 * 3.1415 * height);
density = position.y + height * 20;
material = 0;
return;
*/

/*

float2 slope2 = float2(position.y * snoise(position.xz * 0.001), position.y * snoise(position.xz * 0.001 + 123)) * 0.04;
float h = voronoise(position.xz * 0.03 + slope2 + snoise(position * 0.04) * 0.1, 1.0, 0.3);
h = max(h, 0.5) * 80;
density = position.y - h;
material = 1;
density *= 0.4;
return;
*/


density = -fbmCellular(position.xz * 0.006, 4, 0.5, 2.0).y * 30 + position.y;
float test2 = clamp(fbmCellular(position * 0.01 * float3(1, 2.8, 1), 3, 0.66, 1.3).y - 0.2, 0, 1);
Expand Down

0 comments on commit 6494a67

Please sign in to comment.