From 1c1196038b91d901051c851b3f4fc06132881b9e Mon Sep 17 00:00:00 2001 From: samuelbigos Date: Wed, 6 Apr 2022 09:20:13 +0100 Subject: [PATCH] Code cleanup, add eikonal pass, update Unity. --- .idea/.idea.unity_sdf_demo/.idea/.gitignore | 13 + .../.idea/indexLayout.xml | 8 + .idea/.idea.unity_sdf_demo/.idea/vcs.xml | 6 + Assets/Compute/BoidPhysics.compute | 48 +- Assets/Compute/SDFGen.compute | 32 +- Assets/Compute/SDFGenMutate.compute | 149 ++-- Assets/Editor/CreateTexture3D.cs | 46 -- Assets/Editor/CreateTexture3D.cs.meta | 11 - Assets/{Editor.meta => Includes.meta} | 2 +- Assets/Includes/SDF.cginc | 227 ++++++ Assets/Includes/SDF.cginc.meta | 7 + Assets/Input/Actions.inputactions | 177 +++++ ...actions.meta => Actions.inputactions.meta} | 0 Assets/Input/Protoplanets.inputactions | 679 ------------------ Assets/Materials/Shaders/SDFRenderer.shader | 106 +-- Assets/Scenes/{Game.unity => SDFDemo.unity} | 140 ++-- .../{Game.unity.meta => SDFDemo.unity.meta} | 0 Assets/Scripts/BoidController.cs | 97 --- Assets/Scripts/Boids.meta | 8 + Assets/Scripts/Boids/BoidController.cs | 92 +++ .../{ => Boids}/BoidController.cs.meta | 0 Assets/Scripts/Camera.meta | 8 + Assets/Scripts/{ => Camera}/OrbitCamera.cs | 150 ++-- .../Scripts/{ => Camera}/OrbitCamera.cs.meta | 0 Assets/Scripts/Globals.cs | 8 - Assets/Scripts/PlayerBoid.cs | 26 - Assets/Scripts/PlayerBoid.cs.meta | 11 - Assets/Scripts/SDF.meta | 8 + Assets/Scripts/SDF/SDFGen.cs | 123 ++++ Assets/Scripts/{ => SDF}/SDFGen.cs.meta | 0 Assets/Scripts/SDF/SDFRenderer.cs | 49 ++ Assets/Scripts/{ => SDF}/SDFRenderer.cs.meta | 0 Assets/Scripts/SDF/SDFSurface.cs | 19 + .../SDFSurface.cs.meta} | 2 +- Assets/Scripts/SDFGen.cs | 110 --- Assets/Scripts/SDFRenderer.cs | 51 -- Packages/manifest.json | 16 +- Packages/packages-lock.json | 39 +- ProjectSettings/MemorySettings.asset | 35 + ProjectSettings/ProjectSettings.asset | 37 +- ProjectSettings/ProjectVersion.txt | 4 +- ProjectSettings/QualitySettings.asset | 19 +- ProjectSettings/SceneTemplateSettings.json | 167 +++++ ProjectSettings/UnityConnectSettings.asset | 1 + ProjectSettings/boot.config | 0 45 files changed, 1247 insertions(+), 1484 deletions(-) create mode 100644 .idea/.idea.unity_sdf_demo/.idea/.gitignore create mode 100644 .idea/.idea.unity_sdf_demo/.idea/indexLayout.xml create mode 100644 .idea/.idea.unity_sdf_demo/.idea/vcs.xml delete mode 100644 Assets/Editor/CreateTexture3D.cs delete mode 100644 Assets/Editor/CreateTexture3D.cs.meta rename Assets/{Editor.meta => Includes.meta} (77%) create mode 100644 Assets/Includes/SDF.cginc create mode 100644 Assets/Includes/SDF.cginc.meta create mode 100644 Assets/Input/Actions.inputactions rename Assets/Input/{Protoplanets.inputactions.meta => Actions.inputactions.meta} (100%) delete mode 100644 Assets/Input/Protoplanets.inputactions rename Assets/Scenes/{Game.unity => SDFDemo.unity} (83%) rename Assets/Scenes/{Game.unity.meta => SDFDemo.unity.meta} (100%) delete mode 100644 Assets/Scripts/BoidController.cs create mode 100644 Assets/Scripts/Boids.meta create mode 100644 Assets/Scripts/Boids/BoidController.cs rename Assets/Scripts/{ => Boids}/BoidController.cs.meta (100%) create mode 100644 Assets/Scripts/Camera.meta rename Assets/Scripts/{ => Camera}/OrbitCamera.cs (96%) rename Assets/Scripts/{ => Camera}/OrbitCamera.cs.meta (100%) delete mode 100644 Assets/Scripts/Globals.cs delete mode 100644 Assets/Scripts/PlayerBoid.cs delete mode 100644 Assets/Scripts/PlayerBoid.cs.meta create mode 100644 Assets/Scripts/SDF.meta create mode 100644 Assets/Scripts/SDF/SDFGen.cs rename Assets/Scripts/{ => SDF}/SDFGen.cs.meta (100%) create mode 100644 Assets/Scripts/SDF/SDFRenderer.cs rename Assets/Scripts/{ => SDF}/SDFRenderer.cs.meta (100%) create mode 100644 Assets/Scripts/SDF/SDFSurface.cs rename Assets/Scripts/{Globals.cs.meta => SDF/SDFSurface.cs.meta} (83%) delete mode 100644 Assets/Scripts/SDFGen.cs delete mode 100644 Assets/Scripts/SDFRenderer.cs create mode 100644 ProjectSettings/MemorySettings.asset create mode 100644 ProjectSettings/SceneTemplateSettings.json create mode 100644 ProjectSettings/boot.config diff --git a/.idea/.idea.unity_sdf_demo/.idea/.gitignore b/.idea/.idea.unity_sdf_demo/.idea/.gitignore new file mode 100644 index 0000000..9466ad2 --- /dev/null +++ b/.idea/.idea.unity_sdf_demo/.idea/.gitignore @@ -0,0 +1,13 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Rider ignored files +/.idea.unity_sdf_demo.iml +/contentModel.xml +/modules.xml +/projectSettingsUpdater.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/.idea.unity_sdf_demo/.idea/indexLayout.xml b/.idea/.idea.unity_sdf_demo/.idea/indexLayout.xml new file mode 100644 index 0000000..7b08163 --- /dev/null +++ b/.idea/.idea.unity_sdf_demo/.idea/indexLayout.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/.idea.unity_sdf_demo/.idea/vcs.xml b/.idea/.idea.unity_sdf_demo/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/.idea.unity_sdf_demo/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Assets/Compute/BoidPhysics.compute b/Assets/Compute/BoidPhysics.compute index 0e34969..53c5fef 100644 --- a/Assets/Compute/BoidPhysics.compute +++ b/Assets/Compute/BoidPhysics.compute @@ -1,5 +1,7 @@ #pragma kernel Step +#include "../Includes/SDF.cginc" + struct Boid { float3 position; @@ -11,26 +13,10 @@ float3 _planetCentre; float _timeStep; float _gravity; -Texture3D _sdfTex; -SamplerState sampler_sdfTex; -float _sdfRadius; -float _sdfDistMod; - int _numBoids; RWStructuredBuffer _boidBufferIn; RWStructuredBuffer _boidBufferOut; -float sdf(float3 uv) -{ - uv /= (_sdfRadius * 2.0); - uv += float3(0.5, 0.5, 0.5); - float sdf = _sdfTex.SampleLevel(sampler_sdfTex, uv, 0); - sdf = (sdf * 2.0) - 1.0; - sdf *= (_sdfRadius * 2.0); - sdf *= _sdfDistMod; - return sdf; -} - float3 projectUonV(float3 u, float3 v) { float3 r; @@ -38,22 +24,12 @@ float3 projectUonV(float3 u, float3 v) return r; } -float3 calcNormal(float3 p) -{ - float h = 0.001; - float2 k = float2(1, -1); - return normalize(k.xyy * sdf(p + k.xyy * h) + - k.yyx * sdf(p + k.yyx * h) + - k.yxy * sdf(p + k.yxy * h) + - k.xxx * sdf(p + k.xxx * h)); -} - [numthreads(1,1,1)] -void Step (uint3 id : SV_DispatchThreadID) +void Step(uint3 id : SV_DispatchThreadID) { Boid boid = _boidBufferIn[id.x]; - // collide with other boids + // Collide with other boids for (uint i = 0; i < (uint)_numBoids; i++) { if (i == id.x) @@ -69,7 +45,7 @@ void Step (uint3 id : SV_DispatchThreadID) float dist = distance(p0, p1); float r = r0 + r1; float diff = dist - r; - if (diff <= 0.0) // hit + if (diff <= 0.0) { float3 mov = diff * 0.5 * normalize(p1 - p0); boid.position += mov; @@ -82,11 +58,12 @@ void Step (uint3 id : SV_DispatchThreadID) } } - float dist = sdf(boid.position.xyz) - boid.radius; + float dist = sdfWorld(boid.position.xyz) - boid.radius; - // collide with planet + // Collide with planet { float3 toSurface = -calcNormal(boid.position); + dist *= calcEikonalCorrection(toSurface); float3 p0 = boid.position; float3 p1 = boid.position + toSurface * (dist + boid.radius); @@ -109,16 +86,9 @@ void Step (uint3 id : SV_DispatchThreadID) nv0 -= projectUonV(v0, p0 - p1); boid.velocity = nv0 * 1.0; } - - /*float3 toSurface = -calcNormal(boid.position); - float onPlanet = 1.0 - step(0.0, dist + 0.0001); - float speed = length(boid.velocity) * 0.4; - float normComponent = dot(boid.velocity, toSurface.xyz); - boid.velocity += (normComponent * -toSurface) * onPlanet; - boid.velocity += -toSurface * speed * onPlanet;*/ } - // gravity + // Gravity float3 toPlanet = normalize(_planetCentre - boid.position); float3 gravity = toPlanet * _gravity; boid.velocity += gravity * _timeStep; diff --git a/Assets/Compute/SDFGen.compute b/Assets/Compute/SDFGen.compute index 2b789d5..a21c872 100644 --- a/Assets/Compute/SDFGen.compute +++ b/Assets/Compute/SDFGen.compute @@ -1,32 +1,16 @@ #pragma kernel SDFGen -RWTexture3D _sdfTex; -uint _sdfTexSizeX; -uint _sdfTexSizeY; -uint _sdfTexSizeZ; -float _sdfRadius; -float _sdfDistMod; -float _sphereRadius; +#include "../Includes/SDF.cginc" -float sdSphere(float3 pos, float radius) -{ - return length(pos) - radius; -} +RWTexture3D _sdfTexOut; +float _sphereRadius; [numthreads(8,8,8)] void SDFGen(uint3 id : SV_DispatchThreadID) { - float3 uv = float3(float(id.x), float(id.y), float(id.z)) + float3(0.5f, 0.5f, 0.5f); - uv /= float3(float(_sdfTexSizeX), float(_sdfTexSizeY), float(_sdfTexSizeZ)); - - float3 centre; - centre.x = 0.5f; - centre.y = 0.5f; - centre.z = 0.5f; - float sphereRadius = _sphereRadius / (_sdfRadius * 2.0f); - float dist = sdSphere(uv - centre, sphereRadius); - - dist = clamp(dist / _sdfDistMod, -1.0f, 1.0f); - dist = dist * 0.5f + 0.5f; - _sdfTex[id.xyz] = dist; + float3 uv = kernelToUV(id); + float3 pos = uvToWorld(uv); + float current = sdSphere(pos, float3(0.0f, 0.0f, 0.0f), _sphereRadius); + + _sdfTexOut[id.xyz] = pack(current); } diff --git a/Assets/Compute/SDFGenMutate.compute b/Assets/Compute/SDFGenMutate.compute index cd76117..40a5f25 100644 --- a/Assets/Compute/SDFGenMutate.compute +++ b/Assets/Compute/SDFGenMutate.compute @@ -1,13 +1,8 @@ #pragma kernel SDFMutate -Texture3D _sdfTexIn; -SamplerState sampler_sdfTexIn; +#include "../Includes/SDF.cginc" + RWTexture3D _sdfTexOut; -uint _sdfTexSizeX; -uint _sdfTexSizeY; -uint _sdfTexSizeZ; -float _sdfRadius; -float _sdfDistMod; bool _mouseSubtract; bool _mouseAdd; @@ -16,12 +11,6 @@ float4 _mouseDir; float raySphereIntersect(float3 r0, float3 rd, float3 s0, float sr) { - // - r0: ray origin - // - rd: normalized ray direction - // - s0: sphere center - // - sr: sphere radius - // - Returns distance from r0 to first intersecion with sphere, - // or -1.0 if no intersection. float a = dot(rd, rd); float3 s0_r0 = r0 - s0; float b = 2.0 * dot(rd, s0_r0); @@ -30,101 +19,75 @@ float raySphereIntersect(float3 r0, float3 rd, float3 s0, float sr) { return -1.0; } - return (-b - sqrt((b * b) - 4.0 * a * c)) / (2.0 * a); + return (-b - sqrt(b * b - 4.0 * a * c)) / (2.0 * a); } -float sdf(float3 uv) +float neighborMin(uint3 kernel, uint3 delta) { - uv /= (_sdfRadius * 2.0); - uv += float3(0.5, 0.5, 0.5); - float sdf = _sdfTexIn.SampleLevel(sampler_sdfTexIn, uv, 0); - sdf = (sdf * 2.0) - 1.0; - sdf *= (_sdfRadius * 2.0); - sdf *= _sdfDistMod; - return sdf; + float a = sdfUV(kernelToUV(kernel + delta)); + float b = sdfUV(kernelToUV(kernel - delta)); + return min(abs(a), abs(b)); } -bool outOfBounds(float3 pos) +float eikonal1d(float h, float v, float g) { - if (length(pos) < _sdfRadius) - { - return false; - } - return true; + return min(h, v) + g; } -float rayMarch(float3 origin, float3 dir, float k, out float res, out float3 hitPos) +float eikonal2d(float h, float v, float g) { - res = 1.0; - float t = 0.0001; - [loop] - for (int i = 0; i < 256; i++) + if (abs(h - v) >= g) { - float3 current = origin + dir * t; - if (outOfBounds(current)) - { - return 0.0; - } - float dist = sdf(current); - if (dist <= 0.0) - { - hitPos = current; - res = 0.0; - return 1.0; - } - dist = max(dist, 0.0001); - res = min(res, k * dist / t); - t += dist; + return eikonal1d(h, v, g); } - return 0.0; + float hv = h + v; + float d = hv * hv - 2.0 * (h * h + v * v - g * g); + return 0.5 * (hv + sqrt(d)); } -float sdSphere(float3 pos, float radius) -{ - return length(pos) - radius; -} - -float opSubtraction(float d1, float d2) -{ - return max(d1, -d2); -} -float opSmoothSubtraction(float d1, float d2, float k) +bool eikonal3d(float3 u, out float e, float g) { - float h = clamp(0.5 - 0.5 * (d2 + d1) / k, 0.0, 1.0); - return lerp(d2, -d1, h) + k * h * (1.0 - h); + float xyz = u.x + u.y + u.z; + float n = 3.0; + float d = (xyz * xyz) - n * ((u.x * u.x + u.y * u.y + u.z * u.z) - (g * g)); + if (d < 0.0) + { + return false; + } + e = (1.0 / n) * (xyz + sqrt(d)); + return true; } -float opUnion(float d1, float d2) +float eikonal(float3 u, float g) { - return min(d1, d2); -} -float opSmoothUnion(float d1, float d2, float k) -{ - float h = clamp(0.5 + 0.5 * (d2 - d1) / k, 0.0, 1.0); - return lerp(d2, d1, h) - k * h * (1.0 - h); + float e = 0.0; + if (eikonal3d(u, e, g)) + { + return e; + } + float e1 = eikonal2d(u.x, u.y, g); + float e2 = eikonal2d(u.x, u.z, g); + float e3 = eikonal2d(u.y, u.z, g); + return min(min(e1, e2), e3); } [numthreads(8,8,8)] void SDFMutate(uint3 id : SV_DispatchThreadID) { - float3 uv = float3(float(id.x), float(id.y), float(id.z)) + float3(0.5f, 0.5f, 0.5f); - uv /= float3(float(_sdfTexSizeX), float(_sdfTexSizeY), float(_sdfTexSizeZ)); - - float3 centre; - centre.x = 0.5f; - centre.y = 0.5f; - centre.z = 0.5f; - float3 worldUV = (uv - centre) * (_sdfRadius * 2.0); - float dist = sdf(worldUV); - - //sdf = opSmoothSubtraction(sdSphere(worldUV - float3(0.0, 1.0, 0.0), 0.3), sdf, 0.15); - //sdf = opSmoothUnion(sdSphere(worldUV - float3(0.0, 0.75, 0.0), 0.5), sdf, 0.05); - //sdf = opSmoothUnion(sdSphere(worldUV - float3(0.9, 0.0, 0.0), 0.3), sdf, 0.1); - //sdf = opSmoothUnion(sdSphere(worldUV - float3(-0.9, 0.0, 0.0), 0.3), sdf, 0.1); - //sdf = opSmoothUnion(sdSphere(worldUV - float3(0.0, 0.0, 0.9), 0.3), sdf, 0.1); - //sdf = opSmoothUnion(sdSphere(worldUV - float3(0.0, 0.0, -0.9), 0.3), sdf, 0.1); - //sdf = opSmoothSubtraction(sdSphere(worldUV - float3(0.0, -1.0, 0.0), 0.5), sdf, 0.15); + float3 uv = kernelToUV(id); + float current = sdfUV(uv); + float3 world = uvToWorld(uv); + // Eikonal + float3 u; + u.x = neighborMin(id, uint3(1, 0, 0)); + u.y = neighborMin(id, uint3(0, 1, 0)); + u.z = neighborMin(id, uint3(0, 0, 1)); + + float g = _sdfRadius * 2.0 / _sdfTexSizeX; + float e = eikonal(u, g); + if (current > g) current = e; + if (_mouseAdd) { float toSdf = raySphereIntersect(_mouseOrigin.xyz, _mouseDir.xyz, float3(0.0, 0.0, 0.0), 1.0); @@ -137,10 +100,10 @@ void SDFMutate(uint3 id : SV_DispatchThreadID) { float addRadius = 0.05; float3 pos = ro; - dist = opSmoothUnion(sdSphere(worldUV - pos, addRadius), dist, 0.05); + current = opSmoothUnion(sdSphere(world, pos, addRadius), current, 0.05); } } - + if (_mouseSubtract) { float toSdf = raySphereIntersect(_mouseOrigin.xyz, _mouseDir.xyz, float3(0.0, 0.0, 0.0), 1.0); @@ -151,17 +114,15 @@ void SDFMutate(uint3 id : SV_DispatchThreadID) float hit = rayMarch(ro, _mouseDir.xyz, 1.0, res, hitPos); if (hit > 0.0) { - float subtractRadius = 0.2; + float subtractRadius = 0.06; for (int i = 0; i < 5; i++) { float3 pos = ro - rd * subtractRadius * 0.1 * float(i); - dist = opSmoothSubtraction(sdSphere(worldUV - pos, subtractRadius), dist, 0.05); + current = opSmoothSubtraction(sdSphere(world, pos, subtractRadius), current, 0.1); } } } - - dist /= (_sdfRadius * 2.0f); - dist /= _sdfDistMod; - _sdfTexOut[id.xyz] = dist * 0.5 + 0.5; -} + + _sdfTexOut[id.xyz] = pack(current); +} \ No newline at end of file diff --git a/Assets/Editor/CreateTexture3D.cs b/Assets/Editor/CreateTexture3D.cs deleted file mode 100644 index 97b2082..0000000 --- a/Assets/Editor/CreateTexture3D.cs +++ /dev/null @@ -1,46 +0,0 @@ -using UnityEditor; -using UnityEngine; - -public class ExampleEditorScript : MonoBehaviour -{ - [MenuItem("CreateExamples/3DTexture")] - static void CreateTexture3D() - { - // Configure the texture - int size = 32; - TextureFormat format = TextureFormat.RGBA32; - TextureWrapMode wrapMode = TextureWrapMode.Clamp; - - // Create the texture and apply the configuration - Texture3D texture = new Texture3D(size, size, size, format, false); - texture.wrapMode = wrapMode; - - // Create a 3-dimensional array to store color data - Color[] colors = new Color[size * size * size]; - - // Populate the array so that the x, y, and z values of the texture will map to red, blue, and green colors - float inverseResolution = 1.0f / (size - 1.0f); - for (int z = 0; z < size; z++) - { - int zOffset = z * size * size; - for (int y = 0; y < size; y++) - { - int yOffset = y * size; - for (int x = 0; x < size; x++) - { - colors[x + yOffset + zOffset] = new Color(x * inverseResolution, - y * inverseResolution, z * inverseResolution, 1.0f); - } - } - } - - // Copy the color values to the texture - texture.SetPixels(colors); - - // Apply the changes to the texture and upload the updated texture to the GPU - texture.Apply(); - - // Save the texture to your Unity Project - AssetDatabase.CreateAsset(texture, "Assets/Example3DTexture.asset"); - } -} \ No newline at end of file diff --git a/Assets/Editor/CreateTexture3D.cs.meta b/Assets/Editor/CreateTexture3D.cs.meta deleted file mode 100644 index 0417f1e..0000000 --- a/Assets/Editor/CreateTexture3D.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 253e56f3bd0e84540a8364bcdc5e112c -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Editor.meta b/Assets/Includes.meta similarity index 77% rename from Assets/Editor.meta rename to Assets/Includes.meta index b51c29e..2a3eab7 100644 --- a/Assets/Editor.meta +++ b/Assets/Includes.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 0e46ea2584210024f8310a888a716f8f +guid: c75e9e974343d1d40ada10c94fdbf58c folderAsset: yes DefaultImporter: externalObjects: {} diff --git a/Assets/Includes/SDF.cginc b/Assets/Includes/SDF.cginc new file mode 100644 index 0000000..45b055c --- /dev/null +++ b/Assets/Includes/SDF.cginc @@ -0,0 +1,227 @@ +#ifndef SDF_INCLUDED +#define SDF_INCLUDED + +static const float PI = 3.141592653589793238462; +static const float HIT_EPSILON = 0.001; + +Texture3D _sdfTexIn; +SamplerState sampler_sdfTexIn; +uniform uint _sdfTexSizeX; +uniform uint _sdfTexSizeY; +uniform uint _sdfTexSizeZ; +uniform float _sdfMaxDist; +uniform float _sdfRadius; + +uniform int _aoSamples; +uniform float _aoKernelSize; +uniform float _aoThreshold; +uniform float _aoContribution; + +uniform int _shadowSamples; +uniform float _shadowKernelSize; +uniform float _shadowRayOffset; +uniform float _shadowK; +uniform float _shadowContribution; + +// SDF sampling functions +float pack(float dist) +{ + dist /= _sdfMaxDist; + dist = dist * 0.5 + 0.5; + return clamp(dist, 0.0, 1.0); +} +float unpack(float dist) +{ + dist = dist * 2.0 - 1.0; + return dist * _sdfMaxDist; +} +float3 kernelToUV(uint3 k) +{ + float3 uv = float3(float(k.x), float(k.y), float(k.z)) + float3(0.5f, 0.5f, 0.5f); + return uv / float3(float(_sdfTexSizeX), float(_sdfTexSizeY), float(_sdfTexSizeZ)); +} +float3 uvToWorld(float3 uv) +{ + uv -= float3(0.5, 0.5, 0.5); + uv *= _sdfRadius * 2.0; + return uv; +} +float3 worldToUV(float3 world) +{ + world /= _sdfRadius * 2.0; + world += float3(0.5, 0.5, 0.5); + return world; +} +float sdfUV(float3 uv) +{ + return unpack(_sdfTexIn.SampleLevel(sampler_sdfTexIn, uv, 0).x); +} +float sdfWorld(float3 pos) +{ + return sdfUV(worldToUV(pos)); +} +bool rayHit(float3 pos, out float dist) +{ + dist = sdfWorld(pos); + return dist <= HIT_EPSILON; +} + +// Helpers +bool outOfBounds(float3 pos) +{ + return length(pos) > _sdfRadius; +} +float3 calcNormal(float3 p) +{ + float h = 0.001; + float2 k = float2(1, -1); + return normalize(k.xyy * sdfWorld(p + k.xyy * h) + + k.yyx * sdfWorld(p + k.yyx * h) + + k.yxy * sdfWorld(p + k.yxy * h) + + k.xxx * sdfWorld(p + k.xxx * h)); +} +float remap(float from, float to, float value) +{ + return (value - from) / (to - from); +} + +// Eikonal correction +float calcEikonalCorrection(float3 dir) +{ + float3 nDir = normalize(dir); + float eikonalFix = min(nDir.x, min(nDir.y, nDir.z)); + float l = 0.70711f; + eikonalFix = remap(0.70711, 1.0, eikonalFix); + eikonalFix = lerp(0.99, 1.0, eikonalFix); + return eikonalFix; +} + +// SDF lighting +float shadowRay(float3 origin, float3 dir, float k) +{ + float dist; + dir = normalize(dir); + float res = 1.0; + float t = _shadowRayOffset; + float3 ray = origin + dir * t; + [loop] + for (int i = 0; i < 128; i++) + { + if (rayHit(ray, dist)) + { + return 0.0; + } + if (outOfBounds(ray)) + { + break; + } + float kDist = dist * k; + res = min(res, kDist / min(t, 1.0)); + t += max(0.01, dist); + ray = origin + dir * t; + } + return res; +} +float shadowCalc(float3 pos, float3 normal, float3 sunPos) +{ + float3 sunDir = normalize(sunPos - pos); + float3 rayOrigin = pos + normal * _shadowRayOffset; + + float3 oy = normalize(pos - ddy(pos)) * _shadowKernelSize; + float3 ox = normalize(pos - ddx(pos)) * _shadowKernelSize; + float s = 0.0; + + for (int x = 0; x < _shadowSamples; x++) + { + for (int y = 0; y < _shadowSamples; y++) + { + float fx = float(x) - (float(_shadowSamples - 1) / 2.0); + float fy = float(y) - (float(_shadowSamples - 1) / 2.0); + float3 origin = rayOrigin + oy * fy + ox * fx; + s += shadowRay(origin, sunDir, _shadowK); + } + } + s /= float(_shadowSamples * _shadowSamples); + return s * _shadowContribution + (1.0 - _shadowContribution); +} +// SDF Ambient Occlusion +float aoCalc(float3 pos, float3 normal) +{ + // fibonacci sphere + // https://stackoverflow.com/questions/9600801/evenly-distributing-n-points-on-a-sphere + float phi = PI * (3.0 - sqrt(5.0)); + float sum = 0.0; + [loop] + for (int i = 0; i < _aoSamples; i++) + { + float3 p = float3(0.0, 0.0, 0.0); + p.y = 1.0 - (i / float(_aoSamples - 1.0)) * 2.0; + float radius = sqrt(1.0 - pow(p.y, 2.0)); + float theta = phi * float(i); + p.x = cos(theta) * radius; + p.z = sin(theta) * radius; + + sum += saturate(sdfWorld(pos + p * _aoKernelSize) / _aoKernelSize); + } + sum /= float(_aoSamples); + sum = 1.0 - pow(1.0 - sum, 2.0); + sum = min(1.0, sum * _aoThreshold); + return lerp(1.0, sum, _aoContribution); +} + +// Raymarch +float rayMarch(float3 origin, float3 dir, float k, out float res, out float3 hitPos) +{ + // Running eikonal equation on the SDF causes distance values to elongate along non-cardinal directions, + // the more diagonal the more pronounced. Here, adjust the distance value by the amount of cardinality. + float eikonalFix = calcEikonalCorrection(dir); + + res = 1.0; + float t = 0.001; + [loop] + for (int i = 0; i < 256; i++) + { + float3 current = origin + dir * t; + if (outOfBounds(current)) + { + return 0.0; + } + float dist = sdfWorld(current); + if (dist <= 0.0) + { + hitPos = current; + res = 0.0; + return 1.0; + } + dist = max(dist, 0.0001); + res = min(res, k * dist / t); + t += dist * eikonalFix; + } + return 0.0; +} + +// SDF operations +float sdSphere(float3 pos, float3 spherePos, float sphereRadius) +{ + return length(pos - spherePos) - sphereRadius; +} +float opSubtraction(float d1, float d2) +{ + return max(d1, -d2); +} +float opSmoothSubtraction(float d1, float d2, float k) +{ + float h = clamp(0.5 - 0.5 * (d2 + d1) / k, 0.0, 1.0); + return lerp(d2, -d1, h) + k * h * (1.0 - h); +} +float opUnion(float d1, float d2) +{ + return min(d1, d2); +} +float opSmoothUnion(float d1, float d2, float k) +{ + float h = clamp(0.5 + 0.5 * (d2 - d1) / k, 0.0, 1.0); + return lerp(d2, d1, h) - k * h * (1.0 - h); +} + +#endif // SDF_INCLUDED \ No newline at end of file diff --git a/Assets/Includes/SDF.cginc.meta b/Assets/Includes/SDF.cginc.meta new file mode 100644 index 0000000..d41258f --- /dev/null +++ b/Assets/Includes/SDF.cginc.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: df15adcec050f7f4fbd2bcd185118043 +ShaderIncludeImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Input/Actions.inputactions b/Assets/Input/Actions.inputactions new file mode 100644 index 0000000..d5be53f --- /dev/null +++ b/Assets/Input/Actions.inputactions @@ -0,0 +1,177 @@ +{ + "name": "Actions", + "maps": [ + { + "name": "Player", + "id": "de4fa7bc-fe78-4700-92e9-d6c14460ca3f", + "actions": [ + { + "name": "Look", + "type": "Value", + "id": "17e137a0-2636-41ce-a81f-4ada2e65ded6", + "expectedControlType": "Vector2", + "processors": "", + "interactions": "", + "initialStateCheck": true + }, + { + "name": "RightMouse", + "type": "Button", + "id": "65b6699c-b2bc-4040-b994-d8ebd9e03f74", + "expectedControlType": "Button", + "processors": "", + "interactions": "", + "initialStateCheck": false + }, + { + "name": "MouseWheel", + "type": "Value", + "id": "003e8a82-3122-4e0f-a4b4-4e46828f67c1", + "expectedControlType": "Vector2", + "processors": "", + "interactions": "", + "initialStateCheck": true + }, + { + "name": "Spawn", + "type": "Button", + "id": "0be7cfbb-3a00-4765-9b33-5d951df69409", + "expectedControlType": "Button", + "processors": "", + "interactions": "", + "initialStateCheck": false + } + ], + "bindings": [ + { + "name": "", + "id": "c1f7a91b-d0fd-4a62-997e-7fb9b69bf235", + "path": "/rightStick", + "interactions": "", + "processors": "", + "groups": ";Gamepad", + "action": "Look", + "isComposite": false, + "isPartOfComposite": false + }, + { + "name": "", + "id": "8c8e490b-c610-4785-884f-f04217b23ca4", + "path": "/delta", + "interactions": "", + "processors": "", + "groups": ";Keyboard&Mouse;Touch", + "action": "Look", + "isComposite": false, + "isPartOfComposite": false + }, + { + "name": "", + "id": "3e5f5442-8668-4b27-a940-df99bad7e831", + "path": "/{Hatswitch}", + "interactions": "", + "processors": "", + "groups": "Joystick", + "action": "Look", + "isComposite": false, + "isPartOfComposite": false + }, + { + "name": "", + "id": "b9f10b39-5f8e-4d3c-b853-c4ef6f242ad9", + "path": "/rightButton", + "interactions": "", + "processors": "", + "groups": "Keyboard&Mouse", + "action": "RightMouse", + "isComposite": false, + "isPartOfComposite": false + }, + { + "name": "", + "id": "812502fb-fe5f-4b69-b7e9-712279979da4", + "path": "/scroll", + "interactions": "", + "processors": "", + "groups": "Keyboard&Mouse", + "action": "MouseWheel", + "isComposite": false, + "isPartOfComposite": false + }, + { + "name": "", + "id": "675a90e9-075b-4ef7-a9e7-65e675927155", + "path": "/space", + "interactions": "", + "processors": "", + "groups": "Keyboard&Mouse", + "action": "Spawn", + "isComposite": false, + "isPartOfComposite": false + } + ] + } + ], + "controlSchemes": [ + { + "name": "Keyboard&Mouse", + "bindingGroup": "Keyboard&Mouse", + "devices": [ + { + "devicePath": "", + "isOptional": false, + "isOR": false + }, + { + "devicePath": "", + "isOptional": false, + "isOR": false + } + ] + }, + { + "name": "Gamepad", + "bindingGroup": "Gamepad", + "devices": [ + { + "devicePath": "", + "isOptional": false, + "isOR": false + } + ] + }, + { + "name": "Touch", + "bindingGroup": "Touch", + "devices": [ + { + "devicePath": "", + "isOptional": false, + "isOR": false + } + ] + }, + { + "name": "Joystick", + "bindingGroup": "Joystick", + "devices": [ + { + "devicePath": "", + "isOptional": false, + "isOR": false + } + ] + }, + { + "name": "XR", + "bindingGroup": "XR", + "devices": [ + { + "devicePath": "", + "isOptional": false, + "isOR": false + } + ] + } + ] +} \ No newline at end of file diff --git a/Assets/Input/Protoplanets.inputactions.meta b/Assets/Input/Actions.inputactions.meta similarity index 100% rename from Assets/Input/Protoplanets.inputactions.meta rename to Assets/Input/Actions.inputactions.meta diff --git a/Assets/Input/Protoplanets.inputactions b/Assets/Input/Protoplanets.inputactions deleted file mode 100644 index 2600e58..0000000 --- a/Assets/Input/Protoplanets.inputactions +++ /dev/null @@ -1,679 +0,0 @@ -{ - "name": "Protoplanets", - "maps": [ - { - "name": "Player", - "id": "de4fa7bc-fe78-4700-92e9-d6c14460ca3f", - "actions": [ - { - "name": "Look", - "type": "Value", - "id": "17e137a0-2636-41ce-a81f-4ada2e65ded6", - "expectedControlType": "Vector2", - "processors": "", - "interactions": "" - }, - { - "name": "RightMouse", - "type": "Button", - "id": "65b6699c-b2bc-4040-b994-d8ebd9e03f74", - "expectedControlType": "Button", - "processors": "", - "interactions": "" - }, - { - "name": "MouseWheel", - "type": "Value", - "id": "003e8a82-3122-4e0f-a4b4-4e46828f67c1", - "expectedControlType": "Vector2", - "processors": "", - "interactions": "" - }, - { - "name": "Spawn", - "type": "Button", - "id": "0be7cfbb-3a00-4765-9b33-5d951df69409", - "expectedControlType": "Button", - "processors": "", - "interactions": "" - } - ], - "bindings": [ - { - "name": "", - "id": "c1f7a91b-d0fd-4a62-997e-7fb9b69bf235", - "path": "/rightStick", - "interactions": "", - "processors": "", - "groups": ";Gamepad", - "action": "Look", - "isComposite": false, - "isPartOfComposite": false - }, - { - "name": "", - "id": "8c8e490b-c610-4785-884f-f04217b23ca4", - "path": "/delta", - "interactions": "", - "processors": "", - "groups": ";Keyboard&Mouse;Touch", - "action": "Look", - "isComposite": false, - "isPartOfComposite": false - }, - { - "name": "", - "id": "3e5f5442-8668-4b27-a940-df99bad7e831", - "path": "/{Hatswitch}", - "interactions": "", - "processors": "", - "groups": "Joystick", - "action": "Look", - "isComposite": false, - "isPartOfComposite": false - }, - { - "name": "", - "id": "b9f10b39-5f8e-4d3c-b853-c4ef6f242ad9", - "path": "/rightButton", - "interactions": "", - "processors": "", - "groups": "Keyboard&Mouse", - "action": "RightMouse", - "isComposite": false, - "isPartOfComposite": false - }, - { - "name": "", - "id": "812502fb-fe5f-4b69-b7e9-712279979da4", - "path": "/scroll", - "interactions": "", - "processors": "", - "groups": "Keyboard&Mouse", - "action": "MouseWheel", - "isComposite": false, - "isPartOfComposite": false - }, - { - "name": "", - "id": "675a90e9-075b-4ef7-a9e7-65e675927155", - "path": "/space", - "interactions": "", - "processors": "", - "groups": "Keyboard&Mouse", - "action": "Spawn", - "isComposite": false, - "isPartOfComposite": false - } - ] - }, - { - "name": "UI", - "id": "2abf6fe3-34da-47a6-af76-9faf1d6b5ba3", - "actions": [ - { - "name": "Navigate", - "type": "Value", - "id": "5ee879a4-15a9-4a70-b970-5ae2981c7c31", - "expectedControlType": "Vector2", - "processors": "", - "interactions": "" - }, - { - "name": "Submit", - "type": "Button", - "id": "773cccd3-be14-4ee2-905c-a5d087eba0d1", - "expectedControlType": "Button", - "processors": "", - "interactions": "" - }, - { - "name": "Cancel", - "type": "Button", - "id": "203bf578-2e6a-4a7a-88ab-4b305a882dba", - "expectedControlType": "Button", - "processors": "", - "interactions": "" - }, - { - "name": "Point", - "type": "PassThrough", - "id": "49050b48-9d47-4d18-a81f-4ce7b13fe187", - "expectedControlType": "Vector2", - "processors": "", - "interactions": "" - }, - { - "name": "Click", - "type": "PassThrough", - "id": "be2aa3b8-60ea-44bd-be7e-2cf2177ce2ac", - "expectedControlType": "Button", - "processors": "", - "interactions": "" - }, - { - "name": "ScrollWheel", - "type": "PassThrough", - "id": "bb2569d9-28d5-4775-9ae3-1e86567efe90", - "expectedControlType": "Vector2", - "processors": "", - "interactions": "" - }, - { - "name": "MiddleClick", - "type": "PassThrough", - "id": "c3bbe28b-eef6-41da-805c-76eede210d14", - "expectedControlType": "", - "processors": "", - "interactions": "" - }, - { - "name": "RightClick", - "type": "PassThrough", - "id": "9e053d1c-ce12-412f-9858-13ab8ecc33ae", - "expectedControlType": "", - "processors": "", - "interactions": "" - }, - { - "name": "TrackedDevicePosition", - "type": "PassThrough", - "id": "178bb5fa-ccbc-42d1-a8e0-5f7009126759", - "expectedControlType": "Vector3", - "processors": "", - "interactions": "" - }, - { - "name": "TrackedDeviceOrientation", - "type": "PassThrough", - "id": "d46038a1-6111-4106-8014-4c141efdd23d", - "expectedControlType": "Quaternion", - "processors": "", - "interactions": "" - } - ], - "bindings": [ - { - "name": "Gamepad", - "id": "809f371f-c5e2-4e7a-83a1-d867598f40dd", - "path": "2DVector", - "interactions": "", - "processors": "", - "groups": "", - "action": "Navigate", - "isComposite": true, - "isPartOfComposite": false - }, - { - "name": "up", - "id": "14a5d6e8-4aaf-4119-a9ef-34b8c2c548bf", - "path": "/leftStick/up", - "interactions": "", - "processors": "", - "groups": ";Gamepad", - "action": "Navigate", - "isComposite": false, - "isPartOfComposite": true - }, - { - "name": "up", - "id": "9144cbe6-05e1-4687-a6d7-24f99d23dd81", - "path": "/rightStick/up", - "interactions": "", - "processors": "", - "groups": ";Gamepad", - "action": "Navigate", - "isComposite": false, - "isPartOfComposite": true - }, - { - "name": "down", - "id": "2db08d65-c5fb-421b-983f-c71163608d67", - "path": "/leftStick/down", - "interactions": "", - "processors": "", - "groups": ";Gamepad", - "action": "Navigate", - "isComposite": false, - "isPartOfComposite": true - }, - { - "name": "down", - "id": "58748904-2ea9-4a80-8579-b500e6a76df8", - "path": "/rightStick/down", - "interactions": "", - "processors": "", - "groups": ";Gamepad", - "action": "Navigate", - "isComposite": false, - "isPartOfComposite": true - }, - { - "name": "left", - "id": "8ba04515-75aa-45de-966d-393d9bbd1c14", - "path": "/leftStick/left", - "interactions": "", - "processors": "", - "groups": ";Gamepad", - "action": "Navigate", - "isComposite": false, - "isPartOfComposite": true - }, - { - "name": "left", - "id": "712e721c-bdfb-4b23-a86c-a0d9fcfea921", - "path": "/rightStick/left", - "interactions": "", - "processors": "", - "groups": ";Gamepad", - "action": "Navigate", - "isComposite": false, - "isPartOfComposite": true - }, - { - "name": "right", - "id": "fcd248ae-a788-4676-a12e-f4d81205600b", - "path": "/leftStick/right", - "interactions": "", - "processors": "", - "groups": ";Gamepad", - "action": "Navigate", - "isComposite": false, - "isPartOfComposite": true - }, - { - "name": "right", - "id": "1f04d9bc-c50b-41a1-bfcc-afb75475ec20", - "path": "/rightStick/right", - "interactions": "", - "processors": "", - "groups": ";Gamepad", - "action": "Navigate", - "isComposite": false, - "isPartOfComposite": true - }, - { - "name": "", - "id": "fb8277d4-c5cd-4663-9dc7-ee3f0b506d90", - "path": "/dpad", - "interactions": "", - "processors": "", - "groups": ";Gamepad", - "action": "Navigate", - "isComposite": false, - "isPartOfComposite": false - }, - { - "name": "Joystick", - "id": "e25d9774-381c-4a61-b47c-7b6b299ad9f9", - "path": "2DVector", - "interactions": "", - "processors": "", - "groups": "", - "action": "Navigate", - "isComposite": true, - "isPartOfComposite": false - }, - { - "name": "up", - "id": "3db53b26-6601-41be-9887-63ac74e79d19", - "path": "/stick/up", - "interactions": "", - "processors": "", - "groups": "Joystick", - "action": "Navigate", - "isComposite": false, - "isPartOfComposite": true - }, - { - "name": "down", - "id": "0cb3e13e-3d90-4178-8ae6-d9c5501d653f", - "path": "/stick/down", - "interactions": "", - "processors": "", - "groups": "Joystick", - "action": "Navigate", - "isComposite": false, - "isPartOfComposite": true - }, - { - "name": "left", - "id": "0392d399-f6dd-4c82-8062-c1e9c0d34835", - "path": "/stick/left", - "interactions": "", - "processors": "", - "groups": "Joystick", - "action": "Navigate", - "isComposite": false, - "isPartOfComposite": true - }, - { - "name": "right", - "id": "942a66d9-d42f-43d6-8d70-ecb4ba5363bc", - "path": "/stick/right", - "interactions": "", - "processors": "", - "groups": "Joystick", - "action": "Navigate", - "isComposite": false, - "isPartOfComposite": true - }, - { - "name": "Keyboard", - "id": "ff527021-f211-4c02-933e-5976594c46ed", - "path": "2DVector", - "interactions": "", - "processors": "", - "groups": "", - "action": "Navigate", - "isComposite": true, - "isPartOfComposite": false - }, - { - "name": "up", - "id": "563fbfdd-0f09-408d-aa75-8642c4f08ef0", - "path": "/w", - "interactions": "", - "processors": "", - "groups": "Keyboard&Mouse", - "action": "Navigate", - "isComposite": false, - "isPartOfComposite": true - }, - { - "name": "up", - "id": "eb480147-c587-4a33-85ed-eb0ab9942c43", - "path": "/upArrow", - "interactions": "", - "processors": "", - "groups": "Keyboard&Mouse", - "action": "Navigate", - "isComposite": false, - "isPartOfComposite": true - }, - { - "name": "down", - "id": "2bf42165-60bc-42ca-8072-8c13ab40239b", - "path": "/s", - "interactions": "", - "processors": "", - "groups": "Keyboard&Mouse", - "action": "Navigate", - "isComposite": false, - "isPartOfComposite": true - }, - { - "name": "down", - "id": "85d264ad-e0a0-4565-b7ff-1a37edde51ac", - "path": "/downArrow", - "interactions": "", - "processors": "", - "groups": "Keyboard&Mouse", - "action": "Navigate", - "isComposite": false, - "isPartOfComposite": true - }, - { - "name": "left", - "id": "74214943-c580-44e4-98eb-ad7eebe17902", - "path": "/a", - "interactions": "", - "processors": "", - "groups": "Keyboard&Mouse", - "action": "Navigate", - "isComposite": false, - "isPartOfComposite": true - }, - { - "name": "left", - "id": "cea9b045-a000-445b-95b8-0c171af70a3b", - "path": "/leftArrow", - "interactions": "", - "processors": "", - "groups": "Keyboard&Mouse", - "action": "Navigate", - "isComposite": false, - "isPartOfComposite": true - }, - { - "name": "right", - "id": "8607c725-d935-4808-84b1-8354e29bab63", - "path": "/d", - "interactions": "", - "processors": "", - "groups": "Keyboard&Mouse", - "action": "Navigate", - "isComposite": false, - "isPartOfComposite": true - }, - { - "name": "right", - "id": "4cda81dc-9edd-4e03-9d7c-a71a14345d0b", - "path": "/rightArrow", - "interactions": "", - "processors": "", - "groups": "Keyboard&Mouse", - "action": "Navigate", - "isComposite": false, - "isPartOfComposite": true - }, - { - "name": "", - "id": "9e92bb26-7e3b-4ec4-b06b-3c8f8e498ddc", - "path": "*/{Submit}", - "interactions": "", - "processors": "", - "groups": "", - "action": "Submit", - "isComposite": false, - "isPartOfComposite": false - }, - { - "name": "", - "id": "82627dcc-3b13-4ba9-841d-e4b746d6553e", - "path": "*/{Cancel}", - "interactions": "", - "processors": "", - "groups": "", - "action": "Cancel", - "isComposite": false, - "isPartOfComposite": false - }, - { - "name": "", - "id": "c52c8e0b-8179-41d3-b8a1-d149033bbe86", - "path": "/position", - "interactions": "", - "processors": "", - "groups": "Keyboard&Mouse", - "action": "Point", - "isComposite": false, - "isPartOfComposite": false - }, - { - "name": "", - "id": "e1394cbc-336e-44ce-9ea8-6007ed6193f7", - "path": "/position", - "interactions": "", - "processors": "", - "groups": "Keyboard&Mouse", - "action": "Point", - "isComposite": false, - "isPartOfComposite": false - }, - { - "name": "", - "id": "5693e57a-238a-46ed-b5ae-e64e6e574302", - "path": "/touch*/position", - "interactions": "", - "processors": "", - "groups": "Touch", - "action": "Point", - "isComposite": false, - "isPartOfComposite": false - }, - { - "name": "", - "id": "4faf7dc9-b979-4210-aa8c-e808e1ef89f5", - "path": "/leftButton", - "interactions": "", - "processors": "", - "groups": ";Keyboard&Mouse", - "action": "Click", - "isComposite": false, - "isPartOfComposite": false - }, - { - "name": "", - "id": "8d66d5ba-88d7-48e6-b1cd-198bbfef7ace", - "path": "/tip", - "interactions": "", - "processors": "", - "groups": ";Keyboard&Mouse", - "action": "Click", - "isComposite": false, - "isPartOfComposite": false - }, - { - "name": "", - "id": "47c2a644-3ebc-4dae-a106-589b7ca75b59", - "path": "/touch*/press", - "interactions": "", - "processors": "", - "groups": "Touch", - "action": "Click", - "isComposite": false, - "isPartOfComposite": false - }, - { - "name": "", - "id": "bb9e6b34-44bf-4381-ac63-5aa15d19f677", - "path": "/trigger", - "interactions": "", - "processors": "", - "groups": "XR", - "action": "Click", - "isComposite": false, - "isPartOfComposite": false - }, - { - "name": "", - "id": "38c99815-14ea-4617-8627-164d27641299", - "path": "/scroll", - "interactions": "", - "processors": "", - "groups": ";Keyboard&Mouse", - "action": "ScrollWheel", - "isComposite": false, - "isPartOfComposite": false - }, - { - "name": "", - "id": "24066f69-da47-44f3-a07e-0015fb02eb2e", - "path": "/middleButton", - "interactions": "", - "processors": "", - "groups": ";Keyboard&Mouse", - "action": "MiddleClick", - "isComposite": false, - "isPartOfComposite": false - }, - { - "name": "", - "id": "4c191405-5738-4d4b-a523-c6a301dbf754", - "path": "/rightButton", - "interactions": "", - "processors": "", - "groups": ";Keyboard&Mouse", - "action": "RightClick", - "isComposite": false, - "isPartOfComposite": false - }, - { - "name": "", - "id": "7236c0d9-6ca3-47cf-a6ee-a97f5b59ea77", - "path": "/devicePosition", - "interactions": "", - "processors": "", - "groups": "XR", - "action": "TrackedDevicePosition", - "isComposite": false, - "isPartOfComposite": false - }, - { - "name": "", - "id": "23e01e3a-f935-4948-8d8b-9bcac77714fb", - "path": "/deviceRotation", - "interactions": "", - "processors": "", - "groups": "XR", - "action": "TrackedDeviceOrientation", - "isComposite": false, - "isPartOfComposite": false - } - ] - } - ], - "controlSchemes": [ - { - "name": "Keyboard&Mouse", - "bindingGroup": "Keyboard&Mouse", - "devices": [ - { - "devicePath": "", - "isOptional": false, - "isOR": false - }, - { - "devicePath": "", - "isOptional": false, - "isOR": false - } - ] - }, - { - "name": "Gamepad", - "bindingGroup": "Gamepad", - "devices": [ - { - "devicePath": "", - "isOptional": false, - "isOR": false - } - ] - }, - { - "name": "Touch", - "bindingGroup": "Touch", - "devices": [ - { - "devicePath": "", - "isOptional": false, - "isOR": false - } - ] - }, - { - "name": "Joystick", - "bindingGroup": "Joystick", - "devices": [ - { - "devicePath": "", - "isOptional": false, - "isOR": false - } - ] - }, - { - "name": "XR", - "bindingGroup": "XR", - "devices": [ - { - "devicePath": "", - "isOptional": false, - "isOR": false - } - ] - } - ] -} \ No newline at end of file diff --git a/Assets/Materials/Shaders/SDFRenderer.shader b/Assets/Materials/Shaders/SDFRenderer.shader index c560146..3499f92 100644 --- a/Assets/Materials/Shaders/SDFRenderer.shader +++ b/Assets/Materials/Shaders/SDFRenderer.shader @@ -7,7 +7,6 @@ Shader "Unlit/SDFRenderer" { Tags { "Queue" = "Transparent" } Blend SrcAlpha OneMinusSrcAlpha - //Blend One OneMinusSrcAlpha Pass { @@ -16,6 +15,7 @@ Shader "Unlit/SDFRenderer" #pragma fragment frag #include "UnityCG.cginc" + #include "../../Includes/SDF.cginc" struct appdata { @@ -29,12 +29,6 @@ Shader "Unlit/SDFRenderer" float3 vectorToSurface : TEXCOORD1; }; - - float4 _MainTex_ST; - - uniform sampler3D _sdfTexture; - uniform float _sdfDistMod; - uniform float _sdfRadius; uniform float _planetRadius; uniform float _boidCount; @@ -55,41 +49,20 @@ Shader "Unlit/SDFRenderer" return o; } - float sdSphere(float3 pos, float3 spherePos, float sphereRadius) - { - return length(pos - spherePos) - sphereRadius; - } - - float sdf(float3 uv) + float3 sdfCol(float3 pos) { - uv /= (_sdfRadius * 2.0); - uv += float3(0.5, 0.5, 0.5); - float sdf = tex3D(_sdfTexture, uv); - sdf = (sdf * 2.0) - 1.0; - sdf *= (_sdfRadius * 2.0); - sdf *= _sdfDistMod; - return sdf; - } - - float3 sdfCol(float3 uv) - { - float2 colUV = float2(0.5, (length(uv) / _planetRadius)); + float2 colUV = float2(0.5, length(pos) / _planetRadius); return tex2D(_planetTex, colUV); } - bool outOfBounds(float3 pos) - { - if (length(pos) < _sdfRadius) - { - return false; - } - return true; - } - float worldSample(float3 pos, out float3 col) { - float dist = sdf(pos); - col = sdfCol(pos); + float dist = sdfWorld(pos); + col = float3(1.0, 1.0, 1.0); + if (dist < HIT_EPSILON) + { + col = sdfCol(pos); + } for (int i = 0; i < _boidCount; i++) { dist = min(dist, sdSphere(pos, _boidPositions[i], _boidRadii[i])); @@ -97,21 +70,14 @@ Shader "Unlit/SDFRenderer" return dist; } - float3 calcNormal(float3 p) + float rayMarchWorld(float3 origin, float3 dir, float k, out float res, out float3 hitPos, out float3 colAtHit) { - float h = 0.001; - float2 k = float2(1, -1); - float3 col; - return normalize(k.xyy * worldSample(p + k.xyy * h, col) + - k.yyx * worldSample(p + k.yyx * h, col) + - k.yxy * worldSample(p + k.yxy * h, col) + - k.xxx * worldSample(p + k.xxx * h, col)); - } + // Running eikonal equation on the SDF causes distance values to elongate along non-cardinal directions, + // the more diagonal the more pronounced. Here, adjust the distance value by the amount of cardinality. + float eikonalFix = calcEikonalCorrection(dir); - float rayMarch(float3 origin, float3 dir, float k, out float res, out float3 hitPos, out float3 colAtHit) - { res = 1.0; - float t = 0.0001; + float t = 0.001; [loop] for (int i = 0; i < 256; i++) { @@ -128,8 +94,9 @@ Shader "Unlit/SDFRenderer" return 1.0; } dist = max(dist, 0.0001); - res = min(res, k * dist / t); - t += dist; + res = min(res, k * dist / t); + + t += dist * eikonalFix; } return 0.0; } @@ -138,50 +105,37 @@ Shader "Unlit/SDFRenderer" { float3 rayOrigin = i.worldVertex; float3 rayDirection = normalize(i.worldVertex - _WorldSpaceCameraPos); - - // hit + float res = 0.0; float3 hitPos = float3(0.0, 0.0, 0.0); float3 colAtHit = float3(1.0, 1.0, 1.0); - float hit = rayMarch(rayOrigin, rayDirection, 16.0, res, hitPos, colAtHit); + float hit = rayMarchWorld(rayOrigin, rayDirection, 16.0, res, hitPos, colAtHit); float s = 0.0; float ao = 0.0; - float ambient = 0.0; if (hit > 0.0) { float3 normal = calcNormal(hitPos); float3 sunPos = float3(0.0, 10.0, 0.0); - // shadow + // Shadow float3 sOrigin = hitPos + normal * 0.0005; float3 sDir = normalize(sunPos - sOrigin); - float3 h; - float3 c; - ambient = dot(normal, sDir) * 0.3 + 0.7; - rayMarch(sOrigin, sDir, 4.0, res, h, c); + float3 h, c; + float ambient = dot(normal, sDir) * 0.3 + 0.7; + rayMarchWorld(sOrigin, sDir, 8.0, res, h, c); s = res; s = ambient * lerp(0.5, 1.0, s); - // ao - float ao_dist = 0.025; - for (int x = -1; x < 2; x++) - { - for (int y = -1; y < 2; y++) - { - for (int z = -1; z < 2; z++) - { - float3 offset = normalize(float3(x, y, z)) * 0.025f; - float3 c; - ao += clamp(worldSample(hitPos + normal * ao_dist + offset, c) / ao_dist, 0.0, 1.0); - } - } - } - ao /= 27.0; - ao = 1.0 - pow(1.0 - ao, 3.0); + // AO + float aoDist = 0.1; + ao = clamp(worldSample(hitPos + normal * aoDist, c) / aoDist, 0.0, 1.0); + ao = lerp(0.1, 1.0, ao); + ao = 1.0 - pow(1.0 - ao, 5.0); + } - float3 col = colAtHit;//float3(255.0/255.0, 140.0/255.0, 0.0/255.0); + float3 col = colAtHit; col *= s; col *= ao; return float4(col, hit); diff --git a/Assets/Scenes/Game.unity b/Assets/Scenes/SDFDemo.unity similarity index 83% rename from Assets/Scenes/Game.unity rename to Assets/Scenes/SDFDemo.unity index ddeec5a..1168976 100644 --- a/Assets/Scenes/Game.unity +++ b/Assets/Scenes/SDFDemo.unity @@ -38,7 +38,7 @@ RenderSettings: m_ReflectionIntensity: 1 m_CustomReflection: {fileID: 0} m_Sun: {fileID: 0} - m_IndirectSpecularColor: {r: 0, g: 0, b: 0, a: 1} + m_IndirectSpecularColor: {r: 0.37311956, g: 0.3807402, b: 0.3587274, a: 1} m_UseRadianceAmbientProbe: 0 --- !u!157 &3 LightmapSettings: @@ -149,10 +149,11 @@ Transform: m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 m_Children: - {fileID: 871546280} m_Father: {fileID: 0} - m_RootOrder: 2 + m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &211207211 GameObject: @@ -181,9 +182,10 @@ Transform: m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 0} - m_RootOrder: 3 + m_RootOrder: 1 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!114 &211207213 MonoBehaviour: @@ -226,46 +228,6 @@ MonoBehaviour: m_CallState: 2 m_ActionId: 17e137a0-2636-41ce-a81f-4ada2e65ded6 m_ActionName: Player/Look[/Mouse/delta] - - m_PersistentCalls: - m_Calls: [] - m_ActionId: 5ee879a4-15a9-4a70-b970-5ae2981c7c31 - m_ActionName: UI/Navigate[/Keyboard/w,/Keyboard/upArrow,/Keyboard/s,/Keyboard/downArrow,/Keyboard/a,/Keyboard/leftArrow,/Keyboard/d,/Keyboard/rightArrow] - - m_PersistentCalls: - m_Calls: [] - m_ActionId: 773cccd3-be14-4ee2-905c-a5d087eba0d1 - m_ActionName: UI/Submit[/Keyboard/enter] - - m_PersistentCalls: - m_Calls: [] - m_ActionId: 203bf578-2e6a-4a7a-88ab-4b305a882dba - m_ActionName: UI/Cancel[/Keyboard/escape] - - m_PersistentCalls: - m_Calls: [] - m_ActionId: 49050b48-9d47-4d18-a81f-4ce7b13fe187 - m_ActionName: UI/Point[/Mouse/position] - - m_PersistentCalls: - m_Calls: [] - m_ActionId: be2aa3b8-60ea-44bd-be7e-2cf2177ce2ac - m_ActionName: UI/Click[/Mouse/leftButton] - - m_PersistentCalls: - m_Calls: [] - m_ActionId: bb2569d9-28d5-4775-9ae3-1e86567efe90 - m_ActionName: UI/ScrollWheel[/Mouse/scroll] - - m_PersistentCalls: - m_Calls: [] - m_ActionId: c3bbe28b-eef6-41da-805c-76eede210d14 - m_ActionName: UI/MiddleClick[/Mouse/middleButton] - - m_PersistentCalls: - m_Calls: [] - m_ActionId: 9e053d1c-ce12-412f-9858-13ab8ecc33ae - m_ActionName: UI/RightClick[/Mouse/rightButton] - - m_PersistentCalls: - m_Calls: [] - m_ActionId: 178bb5fa-ccbc-42d1-a8e0-5f7009126759 - m_ActionName: UI/TrackedDevicePosition - - m_PersistentCalls: - m_Calls: [] - m_ActionId: d46038a1-6111-4106-8014-4c141efdd23d - m_ActionName: UI/TrackedDeviceOrientation - m_PersistentCalls: m_Calls: - m_Target: {fileID: 1633920887} @@ -362,6 +324,7 @@ MeshRenderer: m_CastShadows: 0 m_ReceiveShadows: 0 m_DynamicOccludee: 1 + m_StaticShadowCaster: 0 m_MotionVectors: 1 m_LightProbeUsage: 1 m_ReflectionProbeUsage: 1 @@ -407,12 +370,13 @@ Transform: m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 854249153} - m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 4, y: 4, z: 4} + m_ConstrainProportionsScale: 0 m_Children: [] - m_Father: {fileID: 0} - m_RootOrder: 1 + m_Father: {fileID: 1329529345} + m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &871546279 GameObject: @@ -440,6 +404,7 @@ Transform: m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 m_Children: - {fileID: 1633920886} m_Father: {fileID: 59523603} @@ -455,6 +420,7 @@ GameObject: m_Component: - component: {fileID: 1329529345} - component: {fileID: 1329529344} + - component: {fileID: 1329529346} m_Layer: 0 m_Name: SDFGen m_TagString: Untagged @@ -474,14 +440,14 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 9444123116137a54ba882214cb9970cb, type: 3} m_Name: m_EditorClassIdentifier: - SDFGenCompute: {fileID: 7200000, guid: fbe2a8edcd66e454a80e3e5df72babc8, type: 3} - SDFMutateCompute: {fileID: 7200000, guid: dc0ab7d85c565584395f2fc3ba2c816f, type: 3} - SDFResolution: {x: 512, y: 512, z: 512} - SDFRadius: 2 - SDFDistMod: 1 - SphereRadius: 1 - SDFRendererInstance: {fileID: 854249154} - MainCamera: {fileID: 1633920889} + _sdfGenCompute: {fileID: 7200000, guid: fbe2a8edcd66e454a80e3e5df72babc8, type: 3} + _sdfMutateCompute: {fileID: 7200000, guid: dc0ab7d85c565584395f2fc3ba2c816f, type: 3} + _sdfResolution: {x: 384, y: 384, z: 384} + _sdfRadius: 2 + _sphereRadius: 1 + _sdfMaxDist: 1 + _sdfRendererInstance: {fileID: 854249154} + _mainCamera: {fileID: 1633920889} --- !u!4 &1329529345 Transform: m_ObjectHideFlags: 0 @@ -492,53 +458,25 @@ Transform: m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} - m_Children: [] + m_ConstrainProportionsScale: 0 + m_Children: + - {fileID: 854249157} m_Father: {fileID: 0} - m_RootOrder: 5 + m_RootOrder: 3 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} ---- !u!1 &1473558808 -GameObject: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - serializedVersion: 6 - m_Component: - - component: {fileID: 1473558810} - - component: {fileID: 1473558809} - m_Layer: 0 - m_Name: Game - m_TagString: Untagged - m_Icon: {fileID: 0} - m_NavMeshLayer: 0 - m_StaticEditorFlags: 0 - m_IsActive: 1 ---- !u!114 &1473558809 +--- !u!114 &1329529346 MonoBehaviour: m_ObjectHideFlags: 0 m_CorrespondingSourceObject: {fileID: 0} m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1473558808} + m_GameObject: {fileID: 1329529343} m_Enabled: 1 m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: ca9ddb64c8724e14fb3ce28f6277e8f6, type: 3} + m_Script: {fileID: 11500000, guid: 9290065c2fd73244eb150403048c8adb, type: 3} m_Name: m_EditorClassIdentifier: ---- !u!4 &1473558810 -Transform: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1473558808} - m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 8.042488, y: -8.412394, z: 8.276003} - m_LocalScale: {x: 1, y: 1, z: 1} - m_Children: [] - m_Father: {fileID: 0} - m_RootOrder: 0 - m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} + _gravity: 2 --- !u!1 &1633920885 GameObject: m_ObjectHideFlags: 0 @@ -548,9 +486,9 @@ GameObject: serializedVersion: 6 m_Component: - component: {fileID: 1633920886} + - component: {fileID: 1633920887} - component: {fileID: 1633920889} - component: {fileID: 1633920888} - - component: {fileID: 1633920887} m_Layer: 0 m_Name: OrbitCamera m_TagString: MainCamera @@ -568,6 +506,7 @@ Transform: m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} m_LocalPosition: {x: 0, y: 0.3, z: -2.688} m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 871546280} m_RootOrder: 0 @@ -589,9 +528,9 @@ MonoBehaviour: ZoomSpeed: 0.01 ZoomDeceleration: 10 PanSpeed: 0.1 - Deceleration: 5 - MinZoom: 2.5 - MaxZoom: 10 + Deceleration: 25 + MinZoom: 2 + MaxZoom: 5 GimbalH: {fileID: 59523602} GimbalV: {fileID: 871546279} --- !u!81 &1633920888 @@ -625,7 +564,7 @@ Camera: y: 0 width: 1 height: 1 - near clip plane: 0.3 + near clip plane: 0.01 far clip plane: 1000 field of view: 60 orthographic: 0 @@ -674,10 +613,10 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 70b7786c44ba6924faadaec3c0a376a9, type: 3} m_Name: m_EditorClassIdentifier: - PlanetCentre: {x: 0, y: 0, z: 0} - SDFRendererInstance: {fileID: 854249154} - SDFGenInstance: {fileID: 1329529344} - BoidPhysicsCompute: {fileID: 7200000, guid: f9c8748993dea5a40851f6a085c49b65, type: 3} + _planetCentre: {x: 0, y: 0, z: 0} + _sdfRendererInstance: {fileID: 854249154} + _sdfGenInstance: {fileID: 1329529344} + _boidPhysicsCompute: {fileID: 7200000, guid: f9c8748993dea5a40851f6a085c49b65, type: 3} --- !u!4 &1645661787 Transform: m_ObjectHideFlags: 0 @@ -688,7 +627,8 @@ Transform: m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalPosition: {x: 7.870601, y: -3.6344104, z: -0.5532966} m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 0} - m_RootOrder: 4 + m_RootOrder: 2 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} diff --git a/Assets/Scenes/Game.unity.meta b/Assets/Scenes/SDFDemo.unity.meta similarity index 100% rename from Assets/Scenes/Game.unity.meta rename to Assets/Scenes/SDFDemo.unity.meta diff --git a/Assets/Scripts/BoidController.cs b/Assets/Scripts/BoidController.cs deleted file mode 100644 index 526153b..0000000 --- a/Assets/Scripts/BoidController.cs +++ /dev/null @@ -1,97 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; -using UnityEngine.InputSystem; - -public class BoidController : MonoBehaviour -{ - public struct Boid - { - public Vector3 position; - public Vector3 velocity; - public float radius; - } - - public Vector3 PlanetCentre = new Vector3(0.0f, 0.0f, 0.0f); - public SDFRenderer SDFRendererInstance; - public SDFGen SDFGenInstance; - public ComputeShader BoidPhysicsCompute; - - List _boids = new List(); - int _playerBoidID; - - void Start(){} - - void PhysicsCompute() - { - int kernelID = BoidPhysicsCompute.FindKernel("Step"); - uint kernelSizeX; - uint kernelSizeY; - uint kernelSizeZ; - BoidPhysicsCompute.GetKernelThreadGroupSizes(kernelID, out kernelSizeX, out kernelSizeY, out kernelSizeZ); - - BoidPhysicsCompute.SetInt("_numBoids", _boids.Count); - - ComputeBuffer boidBufferIn = new ComputeBuffer(_boids.Count, 7 * 4); - boidBufferIn.SetData(_boids); - BoidPhysicsCompute.SetBuffer(kernelID, "_boidBufferIn", boidBufferIn); - - ComputeBuffer boidBufferOut = new ComputeBuffer(_boids.Count, 7 * 4); - boidBufferOut.SetData(_boids); - BoidPhysicsCompute.SetBuffer(kernelID, "_boidBufferOut", boidBufferOut); - - BoidPhysicsCompute.SetVector("_planetCentre", PlanetCentre); - BoidPhysicsCompute.SetFloat("_timeStep", Time.deltaTime); - BoidPhysicsCompute.SetFloat("_gravity", Globals.PlanetGravity); - - BoidPhysicsCompute.SetTexture(kernelID, "_sdfTex", SDFGenInstance.GetSDFTexture()); - BoidPhysicsCompute.SetFloat("_sdfRadius", SDFGenInstance.SDFRadius); - BoidPhysicsCompute.SetFloat("_sdfDistMod", SDFGenInstance.SDFDistMod); - - BoidPhysicsCompute.Dispatch(kernelID, _boids.Count, 1, 1); - - Boid[] boidArray = new Boid[_boids.Count]; - boidBufferOut.GetData(boidArray); - - for (int i = 0; i < _boids.Count; i++) - { - _boids[i] = boidArray[i]; - } - - boidBufferIn.Dispose(); - boidBufferOut.Dispose(); - } - - void Update() - { - if (_boids.Count > 0) - { - PhysicsCompute(); - } - SDFRendererInstance.SetBoids(_boids); - } - - public void RegisterPlayer(Boid boid) - { - _boids.Add(boid); - _playerBoidID = _boids.Count - 1; - } - - public void UpdatePlayer(Boid boid) - { - _boids[_playerBoidID] = boid; - } - - public void Spawn(InputAction.CallbackContext context) - { - if (context.phase != InputActionPhase.Started) - return; - - Boid newBoid = new Boid(); - - newBoid.position = new Vector4(0.0f, 1.8f, 0.0f); - newBoid.velocity = new Vector4(Globals.PlanetGravity * 0.2f * Random.Range(-1.0f, 1.0f), 0.0f, Globals.PlanetGravity * 0.2f * Random.Range(-1.0f, 1.0f)); - newBoid.radius = 0.066f; - _boids.Add(newBoid); - } -} diff --git a/Assets/Scripts/Boids.meta b/Assets/Scripts/Boids.meta new file mode 100644 index 0000000..cb41249 --- /dev/null +++ b/Assets/Scripts/Boids.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: fedb8ed66347ac846afd91186978df06 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Boids/BoidController.cs b/Assets/Scripts/Boids/BoidController.cs new file mode 100644 index 0000000..844957b --- /dev/null +++ b/Assets/Scripts/Boids/BoidController.cs @@ -0,0 +1,92 @@ +using System; +using System.Collections.Generic; +using SDF; +using UnityEngine; +using UnityEngine.InputSystem; +using UnityEngine.Serialization; +using Random = UnityEngine.Random; + +namespace Boids +{ + public class BoidController : MonoBehaviour + { + public struct Boid + { + public Vector3 Position; + public Vector3 Velocity; + public float Radius; + } + + [SerializeField] private Vector3 _planetCentre = new Vector3(0.0f, 0.0f, 0.0f); + [SerializeField] private SDFRenderer _sdfRendererInstance; + [SerializeField] private SDFGen _sdfGenInstance; + [SerializeField] private ComputeShader _boidPhysicsCompute; + + private readonly List _boids = new List(); + private int _playerBoidID; + + private void Start() + { + _boidPhysicsCompute.SetVector("_planetCentre", _planetCentre); + _boidPhysicsCompute.SetFloat("_gravity", SDFSurface.Instance.Gravity); + _sdfGenInstance.SetComputeSDFParams(_boidPhysicsCompute); + } + + private void PhysicsCompute() + { + int kernelID = _boidPhysicsCompute.FindKernel("Step"); + _boidPhysicsCompute.GetKernelThreadGroupSizes(kernelID, out uint _, out uint _, out uint _); + + _boidPhysicsCompute.SetInt("_numBoids", _boids.Count); + + ComputeBuffer boidBufferIn = new ComputeBuffer(_boids.Count, 7 * 4); + boidBufferIn.SetData(_boids); + _boidPhysicsCompute.SetBuffer(kernelID, "_boidBufferIn", boidBufferIn); + + ComputeBuffer boidBufferOut = new ComputeBuffer(_boids.Count, 7 * 4); + boidBufferOut.SetData(_boids); + _boidPhysicsCompute.SetBuffer(kernelID, "_boidBufferOut", boidBufferOut); + + _boidPhysicsCompute.SetFloat("_timeStep", Time.deltaTime); + _boidPhysicsCompute.SetTexture(kernelID, "_sdfTexIn", _sdfGenInstance.SDFTexture); + + _boidPhysicsCompute.Dispatch(kernelID, _boids.Count, 1, 1); + + Boid[] boidArray = new Boid[_boids.Count]; + boidBufferOut.GetData(boidArray); + + for (int i = 0; i < _boids.Count; i++) + { + _boids[i] = boidArray[i]; + } + + boidBufferIn.Dispose(); + boidBufferOut.Dispose(); + } + + private void Update() + { + if (_boids.Count > 0) + { + PhysicsCompute(); + } + _sdfRendererInstance.SetBoids(_boids); + } + + public void Spawn(InputAction.CallbackContext context) + { + if (context.phase != InputActionPhase.Started) + return; + + float impulse = 0.1f; + Boid newBoid = new() + { + Position = new Vector4(0.0f, 1.5f, 0.0f), + Velocity = new Vector4(SDFSurface.Instance.Gravity * impulse * Random.Range(-1.0f, 1.0f), 0.0f, SDFSurface.Instance.Gravity * impulse * Random.Range(-1.0f, 1.0f)), + Radius = 0.066f + }; + + _boids.Add(newBoid); + } + } +} diff --git a/Assets/Scripts/BoidController.cs.meta b/Assets/Scripts/Boids/BoidController.cs.meta similarity index 100% rename from Assets/Scripts/BoidController.cs.meta rename to Assets/Scripts/Boids/BoidController.cs.meta diff --git a/Assets/Scripts/Camera.meta b/Assets/Scripts/Camera.meta new file mode 100644 index 0000000..4c4fd99 --- /dev/null +++ b/Assets/Scripts/Camera.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 2857602e21e46524b8fc23fb93bf73fc +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/OrbitCamera.cs b/Assets/Scripts/Camera/OrbitCamera.cs similarity index 96% rename from Assets/Scripts/OrbitCamera.cs rename to Assets/Scripts/Camera/OrbitCamera.cs index a635e1b..194284e 100644 --- a/Assets/Scripts/OrbitCamera.cs +++ b/Assets/Scripts/Camera/OrbitCamera.cs @@ -1,76 +1,76 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; -using UnityEngine.InputSystem; - -public class OrbitCamera : MonoBehaviour -{ - public float MinDistance = 25.0f; - public float MaxDistance = 200.0f; - public float ZoomSpeed = 1.0f; - public float ZoomDeceleration = 20.0f; - public float PanSpeed = 0.1f; - public float Deceleration = 5.0f; - public float MinZoom = 1.5f; - public float MaxZoom = 10.0f; - - public GameObject GimbalH; - public GameObject GimbalV; - - private Vector2 _velocity; - private float _zoomVelocity; - private bool _mouseDown; - - void Start() - { - - } - - void Update() - { - if (_mouseDown) - { - _velocity += Pointer.current.delta.ReadValue(); - } - - GimbalH.transform.Rotate(GimbalH.transform.up, _velocity.x * PanSpeed); - GimbalV.transform.Rotate(new Vector3(1.0f, 0.0f, 0.0f), _velocity.y * -PanSpeed); - _velocity = Vector2.Lerp(_velocity, new Vector2(0.0f, 0.0f), Time.deltaTime * Deceleration); - - Vector3 newPos = transform.localPosition; - newPos += new Vector3(0.0f, 0.0f, _zoomVelocity * Mathf.Abs(transform.localPosition.z)); - newPos.z = Mathf.Clamp(newPos.z, -MaxZoom, -MinZoom); - transform.localPosition = newPos; - _zoomVelocity = Mathf.Lerp(_zoomVelocity, 0.0f, Time.deltaTime * ZoomDeceleration); - } - - public void MouseMove(InputAction.CallbackContext context) - { - } - - public void RightMouse(InputAction.CallbackContext context) - { - switch (context.phase) - { - case InputActionPhase.Started: - _mouseDown = true; - break; - case InputActionPhase.Canceled: - _mouseDown = false; - break; - default: - break; - } - } - - public void MouseWheel(InputAction.CallbackContext context) - { - if (context.phase == InputActionPhase.Started) - { - Vector2 value = context.ReadValue(); - value = Vector2.ClampMagnitude(value, 1.0f); - _zoomVelocity += value.y * ZoomSpeed; - } - - } +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.InputSystem; + +public class OrbitCamera : MonoBehaviour +{ + public float MinDistance = 25.0f; + public float MaxDistance = 200.0f; + public float ZoomSpeed = 1.0f; + public float ZoomDeceleration = 20.0f; + public float PanSpeed = 0.1f; + public float Deceleration = 5.0f; + public float MinZoom = 1.5f; + public float MaxZoom = 10.0f; + + public GameObject GimbalH; + public GameObject GimbalV; + + private Vector2 _velocity; + private float _zoomVelocity; + private bool _mouseDown; + + void Start() + { + + } + + void Update() + { + if (_mouseDown) + { + _velocity += Pointer.current.delta.ReadValue(); + } + + GimbalH.transform.Rotate(GimbalH.transform.up, _velocity.x * PanSpeed); + GimbalV.transform.Rotate(new Vector3(1.0f, 0.0f, 0.0f), _velocity.y * -PanSpeed); + _velocity = Vector2.Lerp(_velocity, new Vector2(0.0f, 0.0f), Time.deltaTime * Deceleration); + + Vector3 newPos = transform.localPosition; + newPos += new Vector3(0.0f, 0.0f, _zoomVelocity * Mathf.Abs(transform.localPosition.z)); + newPos.z = Mathf.Clamp(newPos.z, -MaxZoom, -MinZoom); + transform.localPosition = newPos; + _zoomVelocity = Mathf.Lerp(_zoomVelocity, 0.0f, Time.deltaTime * ZoomDeceleration); + } + + public void MouseMove(InputAction.CallbackContext context) + { + } + + public void RightMouse(InputAction.CallbackContext context) + { + switch (context.phase) + { + case InputActionPhase.Started: + _mouseDown = true; + break; + case InputActionPhase.Canceled: + _mouseDown = false; + break; + default: + break; + } + } + + public void MouseWheel(InputAction.CallbackContext context) + { + if (context.phase == InputActionPhase.Started) + { + Vector2 value = context.ReadValue(); + value = Vector2.ClampMagnitude(value, 1.0f); + _zoomVelocity += value.y * ZoomSpeed; + } + + } } \ No newline at end of file diff --git a/Assets/Scripts/OrbitCamera.cs.meta b/Assets/Scripts/Camera/OrbitCamera.cs.meta similarity index 100% rename from Assets/Scripts/OrbitCamera.cs.meta rename to Assets/Scripts/Camera/OrbitCamera.cs.meta diff --git a/Assets/Scripts/Globals.cs b/Assets/Scripts/Globals.cs deleted file mode 100644 index fb20381..0000000 --- a/Assets/Scripts/Globals.cs +++ /dev/null @@ -1,8 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Globals : MonoBehaviour -{ - public static float PlanetGravity = 1.0f; -} diff --git a/Assets/Scripts/PlayerBoid.cs b/Assets/Scripts/PlayerBoid.cs deleted file mode 100644 index 2c4852c..0000000 --- a/Assets/Scripts/PlayerBoid.cs +++ /dev/null @@ -1,26 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class PlayerBoid : MonoBehaviour -{ - public BoidController BoidControllerRef; - - BoidController.Boid _playerBoid; - - void Start() - { - _playerBoid = new BoidController.Boid(); - - _playerBoid.position = new Vector4(0.0f, 1.8f, 0.0f); - _playerBoid.velocity = new Vector4(Globals.PlanetGravity * 0.2f * Random.Range(-1.0f, 1.0f), 0.0f, Globals.PlanetGravity * 0.2f * Random.Range(-1.0f, 1.0f)); - _playerBoid.radius = 0.066f; - - BoidControllerRef.RegisterPlayer(_playerBoid); - } - - void Update() - { - BoidControllerRef.UpdatePlayer(_playerBoid); - } -} diff --git a/Assets/Scripts/PlayerBoid.cs.meta b/Assets/Scripts/PlayerBoid.cs.meta deleted file mode 100644 index 0769c53..0000000 --- a/Assets/Scripts/PlayerBoid.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 57b54cd470e4a044a8a084bd922d2bd1 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/SDF.meta b/Assets/Scripts/SDF.meta new file mode 100644 index 0000000..7a32b61 --- /dev/null +++ b/Assets/Scripts/SDF.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 15fa71c6afdc13b4393d9ff660e6d74f +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/SDF/SDFGen.cs b/Assets/Scripts/SDF/SDFGen.cs new file mode 100644 index 0000000..a622833 --- /dev/null +++ b/Assets/Scripts/SDF/SDFGen.cs @@ -0,0 +1,123 @@ +using UnityEngine; +using UnityEngine.InputSystem; +using UnityEngine.Rendering; + +namespace SDF +{ + public class SDFGen : MonoBehaviour + { + [SerializeField] private ComputeShader _sdfGenCompute; + [SerializeField] private ComputeShader _sdfMutateCompute; + [SerializeField] private Vector3Int _sdfResolution; + [SerializeField] private float _sdfRadius; + [SerializeField] private float _sphereRadius; + [SerializeField] private float _sdfMaxDist; + [SerializeField] private SDFRenderer _sdfRendererInstance; + [SerializeField] private Camera _mainCamera; + + public float SDFRadius => _sdfRadius; + public RenderTexture SDFTexture => _sdfVolumeTexture[_activeSdfTex]; + + private int _activeSdfTex = 0; + private readonly RenderTexture[] _sdfVolumeTexture = new RenderTexture[2]; + private Vector3Int _numKernels; + private uint _kernelSizeX; + private uint _kernelSizeY; + private uint _kernelSizeZ; + + private void Start() + { + SetComputeSDFParams(_sdfMutateCompute); + SetComputeSDFParams(_sdfGenCompute); + SetMaterialSDFParams(_sdfRendererInstance.SDFRendererMat); + InitGenCompute(); + } + + private void Update() + { + //if (Mouse.current.middleButton.wasReleasedThisFrame) + { + Ray ray = _mainCamera.ScreenPointToRay(Mouse.current.position.ReadValue()); + _sdfMutateCompute.SetBool("_mouseAdd", Mouse.current.leftButton.isPressed); + _sdfMutateCompute.SetBool("_mouseSubtract", Keyboard.current.xKey.isPressed); + _sdfMutateCompute.SetVector("_mouseOrigin", ray.origin); + _sdfMutateCompute.SetVector("_mouseDir", ray.direction); + + int inSdfTex = _activeSdfTex; + int outSdfTex = (_activeSdfTex + 1) & 2; + + int kernelID = _sdfMutateCompute.FindKernel("SDFMutate"); + _sdfMutateCompute.SetTexture(kernelID, "_sdfTexIn", _sdfVolumeTexture[inSdfTex], 0, RenderTextureSubElement.Color); + _sdfMutateCompute.SetTexture(kernelID, "_sdfTexOut", _sdfVolumeTexture[outSdfTex], 0, RenderTextureSubElement.Color); + + _sdfMutateCompute.Dispatch(kernelID, _numKernels.x, _numKernels.y, _numKernels.z); + + _activeSdfTex = outSdfTex; + _sdfRendererInstance.SetSDFTexture(SDFTexture); + } + } + + private void InitGenCompute() + { + int kernelID = _sdfGenCompute.FindKernel("SDFGen"); + _sdfGenCompute.GetKernelThreadGroupSizes(kernelID, out _kernelSizeX, out _kernelSizeY, out _kernelSizeZ); + if (_sdfResolution.x % _kernelSizeX != 0 || + _sdfResolution.y % _kernelSizeY != 0 || + _sdfResolution.z % _kernelSizeZ != 0) + { + Debug.LogError("SDF kernel size must be a multiple of SDF resolution."); + } + + _numKernels.x = (int)(_sdfResolution.x / _kernelSizeX); + _numKernels.y = (int)(_sdfResolution.y / _kernelSizeY); + _numKernels.z = (int)(_sdfResolution.z / _kernelSizeZ); + + // Create output 3D render texture. + RenderTextureDescriptor renderTexDesc = new RenderTextureDescriptor(); + renderTexDesc.dimension = TextureDimension.Tex3D; + renderTexDesc.colorFormat = RenderTextureFormat.RFloat; + renderTexDesc.enableRandomWrite = true; + renderTexDesc.width = _sdfResolution.x; + renderTexDesc.height = _sdfResolution.y; + renderTexDesc.volumeDepth = _sdfResolution.z; + renderTexDesc.msaaSamples = 1; + renderTexDesc.mipCount = 0; + renderTexDesc.useMipMap = false; + + _sdfVolumeTexture[0] = new RenderTexture(renderTexDesc); + _sdfVolumeTexture[0].Create(); + _sdfVolumeTexture[1] = new RenderTexture(renderTexDesc); + _sdfVolumeTexture[1].Create(); + + _sdfVolumeTexture[0].filterMode = FilterMode.Bilinear; + _sdfVolumeTexture[1].filterMode = FilterMode.Bilinear; + + _sdfGenCompute.SetFloat("_sphereRadius", _sphereRadius); + _sdfGenCompute.SetTexture(kernelID, "_sdfTexOut", _sdfVolumeTexture[0], 0, RenderTextureSubElement.Color); + + _sdfGenCompute.Dispatch(kernelID, _numKernels.x, _numKernels.y, _numKernels.z); + + _sdfRendererInstance.SetSDFTexture(_sdfVolumeTexture[0]); + } + + public void SetComputeSDFParams(ComputeShader shader) + { + shader.SetInt("_sdfTexSizeX", _sdfResolution.x); + shader.SetInt("_sdfTexSizeY", _sdfResolution.y); + shader.SetInt("_sdfTexSizeZ", _sdfResolution.z); + shader.SetFloat("_sdfRadius", _sdfRadius); + shader.SetFloat("_sdfMaxDist", _sdfMaxDist); + shader.SetFloat("_planetRadius", _sphereRadius); + } + + public void SetMaterialSDFParams(Material mat) + { + mat.SetInt("_sdfTexSizeX", _sdfResolution.x); + mat.SetInt("_sdfTexSizeY", _sdfResolution.y); + mat.SetInt("_sdfTexSizeZ", _sdfResolution.z); + mat.SetFloat("_sdfRadius", _sdfRadius); + mat.SetFloat("_sdfMaxDist", _sdfMaxDist); + mat.SetFloat("_planetRadius", _sphereRadius); + } + } +} diff --git a/Assets/Scripts/SDFGen.cs.meta b/Assets/Scripts/SDF/SDFGen.cs.meta similarity index 100% rename from Assets/Scripts/SDFGen.cs.meta rename to Assets/Scripts/SDF/SDFGen.cs.meta diff --git a/Assets/Scripts/SDF/SDFRenderer.cs b/Assets/Scripts/SDF/SDFRenderer.cs new file mode 100644 index 0000000..a415f6e --- /dev/null +++ b/Assets/Scripts/SDF/SDFRenderer.cs @@ -0,0 +1,49 @@ +using System.Collections.Generic; +using Boids; +using UnityEngine; + +namespace SDF +{ + public class SDFRenderer : MonoBehaviour + { + public Material SDFRendererMat => _sdfRendererMat; + + private Material _sdfRendererMat; + [SerializeField] private Texture2D _planetTexture; + + private static readonly int _boidCount = Shader.PropertyToID("_boidCount"); + private static readonly int _boidPositions = Shader.PropertyToID("_boidPositions"); + private static readonly int _boidRadii = Shader.PropertyToID("_boidRadii"); + + private void Start() + { + _sdfRendererMat = GetComponent().material; + _sdfRendererMat.SetVectorArray("_boidPositions", new Vector4[256]); + _sdfRendererMat.SetFloatArray("_boidRadii", new float[256]); + _sdfRendererMat.SetTexture("_planetTex", _planetTexture); + _sdfRendererMat.SetFloat("_planetTexHeight", (float)_planetTexture.height); + } + + public void SetSDFTexture(RenderTexture tex) + { + _sdfRendererMat.SetTexture("_sdfTexIn", tex); + } + + public void SetBoids(List boids) + { + _sdfRendererMat.SetFloat(_boidCount, boids.Count); + if (boids.Count > 0) + { + Vector4[] posArray = new Vector4[boids.Count]; + float[] radArray = new float[boids.Count]; + for (int i = 0; i _instance; + + [SerializeField] private float _gravity = 1.0f; + public float Gravity => _gravity; + + private void Awake() + { + Debug.Assert(_instance == null, "Can only have one SDF surface."); + _instance = this; + } + } +} diff --git a/Assets/Scripts/Globals.cs.meta b/Assets/Scripts/SDF/SDFSurface.cs.meta similarity index 83% rename from Assets/Scripts/Globals.cs.meta rename to Assets/Scripts/SDF/SDFSurface.cs.meta index f9b0212..5f02c03 100644 --- a/Assets/Scripts/Globals.cs.meta +++ b/Assets/Scripts/SDF/SDFSurface.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: ca9ddb64c8724e14fb3ce28f6277e8f6 +guid: 9290065c2fd73244eb150403048c8adb MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/Scripts/SDFGen.cs b/Assets/Scripts/SDFGen.cs deleted file mode 100644 index ee4a99b..0000000 --- a/Assets/Scripts/SDFGen.cs +++ /dev/null @@ -1,110 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; -using UnityEngine.Rendering; -using UnityEngine.InputSystem; - -public class SDFGen : MonoBehaviour -{ - public ComputeShader SDFGenCompute; - public ComputeShader SDFMutateCompute; - public Vector3Int SDFResolution; - public float SDFRadius; - public float SDFDistMod; - public float SphereRadius; - public SDFRenderer SDFRendererInstance; - public Camera MainCamera; - - private int _activeSdfTex = 0; - private RenderTexture[] _sdfVolumeTexture = new RenderTexture[2]; - private Vector3Int _numKernels; - private uint _kernelSizeX; - private uint _kernelSizeY; - private uint _kernelSizeZ; - - public RenderTexture GetSDFTexture() - { - return _sdfVolumeTexture[_activeSdfTex]; - } - - void InitGenCompute() - { - int kernelID = SDFGenCompute.FindKernel("SDFGen"); - SDFGenCompute.GetKernelThreadGroupSizes(kernelID, out _kernelSizeX, out _kernelSizeY, out _kernelSizeZ); - if (SDFResolution.x % _kernelSizeX != 0 || - SDFResolution.y % _kernelSizeY != 0 || - SDFResolution.z % _kernelSizeZ != 0) - { - Debug.LogError("SDF kernel size must be a multiple of SDF resolution."); - } - - _numKernels.x = (int)(SDFResolution.x / _kernelSizeX); - _numKernels.y = (int)(SDFResolution.y / _kernelSizeY); - _numKernels.z = (int)(SDFResolution.z / _kernelSizeZ); - - // Create output 3D render texture. - RenderTextureDescriptor renderTexDesc = new RenderTextureDescriptor(); - renderTexDesc.dimension = TextureDimension.Tex3D; - renderTexDesc.colorFormat = RenderTextureFormat.RFloat; - renderTexDesc.enableRandomWrite = true; - renderTexDesc.width = SDFResolution.x; - renderTexDesc.height = SDFResolution.y; - renderTexDesc.volumeDepth = SDFResolution.z; - renderTexDesc.msaaSamples = 1; - renderTexDesc.mipCount = 0; - - _sdfVolumeTexture[0] = new RenderTexture(renderTexDesc); - _sdfVolumeTexture[0].Create(); - _sdfVolumeTexture[1] = new RenderTexture(renderTexDesc); - _sdfVolumeTexture[1].Create(); - - SDFGenCompute.SetInt("_sdfTexSizeX", SDFResolution.x); - SDFGenCompute.SetInt("_sdfTexSizeY", SDFResolution.y); - SDFGenCompute.SetInt("_sdfTexSizeZ", SDFResolution.z); - SDFGenCompute.SetFloat("_sdfRadius", SDFRadius); - SDFGenCompute.SetFloat("_sdfDistMod", SDFDistMod); - SDFGenCompute.SetFloat("_sphereRadius", SphereRadius); - SDFGenCompute.SetTexture(kernelID, "_sdfTex", _sdfVolumeTexture[0], 0, RenderTextureSubElement.Color); - - SDFRendererInstance.SetSDFParams(SDFDistMod, SDFRadius, SphereRadius); - - SDFGenCompute.Dispatch(kernelID, _numKernels.x, _numKernels.y, _numKernels.z); - SDFRendererInstance.SetSDFTexture(_sdfVolumeTexture[0]); - } - - void InitMutateCompute() - { - SDFMutateCompute.SetInt("_sdfTexSizeX", SDFResolution.x); - SDFMutateCompute.SetInt("_sdfTexSizeY", SDFResolution.y); - SDFMutateCompute.SetInt("_sdfTexSizeZ", SDFResolution.z); - SDFMutateCompute.SetFloat("_sdfRadius", SDFRadius); - SDFMutateCompute.SetFloat("_sdfDistMod", SDFDistMod); - } - - void Start() - { - InitGenCompute(); - InitMutateCompute(); - } - - void Update() - { - Ray ray = MainCamera.ScreenPointToRay(Mouse.current.position.ReadValue()); - SDFMutateCompute.SetBool("_mouseAdd", Mouse.current.leftButton.isPressed); - SDFMutateCompute.SetBool("_mouseSubtract", Keyboard.current.xKey.isPressed); - SDFMutateCompute.SetVector("_mouseOrigin", ray.origin); - SDFMutateCompute.SetVector("_mouseDir", ray.direction); - - int inSdfTex = _activeSdfTex; - int outSdfTex = (_activeSdfTex + 1) & 2; - - int kernelID = SDFMutateCompute.FindKernel("SDFMutate"); - SDFMutateCompute.SetTexture(kernelID, "_sdfTexIn", _sdfVolumeTexture[inSdfTex], 0, RenderTextureSubElement.Color); - SDFMutateCompute.SetTexture(kernelID, "_sdfTexOut", _sdfVolumeTexture[outSdfTex], 0, RenderTextureSubElement.Color); - SDFMutateCompute.Dispatch(kernelID, _numKernels.x, _numKernels.y, _numKernels.z); - - _activeSdfTex = outSdfTex; - - SDFRendererInstance.SetSDFTexture(GetSDFTexture()); - } -} diff --git a/Assets/Scripts/SDFRenderer.cs b/Assets/Scripts/SDFRenderer.cs deleted file mode 100644 index a1ee73a..0000000 --- a/Assets/Scripts/SDFRenderer.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class SDFRenderer : MonoBehaviour -{ - Material _sdfRendererMat; - public Texture2D _planetTexture; - - private void Awake() - { - } - - void Start() - { - _sdfRendererMat = GetComponent().material; - _sdfRendererMat.SetVectorArray("_boidPositions", new Vector4[256]); - _sdfRendererMat.SetFloatArray("_boidRadii", new float[256]); - _sdfRendererMat.SetTexture("_planetTex", _planetTexture); - _sdfRendererMat.SetFloat("_planetTexHeight", (float)_planetTexture.height); - } - - public void SetSDFParams(float distMod, float sdfRadius, float planetRadius) - { - _sdfRendererMat.SetFloat("_sdfDistMod", distMod); - _sdfRendererMat.SetFloat("_sdfRadius", sdfRadius); - _sdfRendererMat.SetFloat("_planetRadius", planetRadius); - } - - public void SetSDFTexture(RenderTexture tex) - { - _sdfRendererMat.SetTexture("_sdfTexture", tex); - } - - public void SetBoids(List boids) - { - _sdfRendererMat.SetFloat("_boidCount", boids.Count); - if (boids.Count > 0) - { - Vector4[] posArray = new Vector4[boids.Count]; - float[] radArray = new float[boids.Count]; - for (int i = 0; i ", + "ignore": false, + "defaultInstantiationMode": 1, + "supportsModification": true + }, + "newSceneOverride": 0 +} \ No newline at end of file diff --git a/ProjectSettings/UnityConnectSettings.asset b/ProjectSettings/UnityConnectSettings.asset index fa0b146..6125b30 100644 --- a/ProjectSettings/UnityConnectSettings.asset +++ b/ProjectSettings/UnityConnectSettings.asset @@ -9,6 +9,7 @@ UnityConnectSettings: m_EventOldUrl: https://api.uca.cloud.unity3d.com/v1/events m_EventUrl: https://cdp.cloud.unity3d.com/v1/events m_ConfigUrl: https://config.uca.cloud.unity3d.com + m_DashboardUrl: https://dashboard.unity3d.com m_TestInitMode: 0 CrashReportingSettings: m_EventUrl: https://perf-events.cloud.unity3d.com diff --git a/ProjectSettings/boot.config b/ProjectSettings/boot.config new file mode 100644 index 0000000..e69de29