From edd42909c60fab9455d33f2590d016b83ccfad3f Mon Sep 17 00:00:00 2001 From: Bronson Zgeb Date: Sun, 28 Feb 2021 12:08:59 -0500 Subject: [PATCH] Added some more tweakable metaball options. Setup paint particle system. --- .../Materials/SphereTraceMetaball.mat | 3 +- .../Metaballs/Shaders/Includes/Metaball.hlsl | 71 +++-------- Assets/Metaballs/Shaders/Metaball.shadergraph | 119 +++++++++++++++++- .../SphereTraceMetaballs.shadersubgraph | 115 ++++++++++++++++- Assets/Scenes/MixAndJam.unity | 14 +-- .../Settings/CustomForwardRendererData.asset | 2 +- ProjectSettings/GraphicsSettings.asset | 9 +- 7 files changed, 257 insertions(+), 76 deletions(-) diff --git a/Assets/Metaballs/Materials/SphereTraceMetaball.mat b/Assets/Metaballs/Materials/SphereTraceMetaball.mat index 1614191..468101b 100644 --- a/Assets/Metaballs/Materials/SphereTraceMetaball.mat +++ b/Assets/Metaballs/Materials/SphereTraceMetaball.mat @@ -99,6 +99,7 @@ Material: - _ClearCoatSmoothness: 0 - _Cull: 2 - _Cutoff: 0.5 + - _DensityThreshold: 0.3 - _DetailAlbedoMapScale: 1 - _DetailNormalMapScale: 1 - _DstBlend: 0 @@ -120,7 +121,7 @@ Material: - _ZWrite: 1 m_Colors: - Vector3_ff841d114cec48548b0a41259e363307: {r: 0, g: 0, b: 0, a: 0} - - _Albedo: {r: 0, g: 0.30317068, b: 1, a: 0} + - _Albedo: {r: 1, g: 0, b: 0, a: 0} - _BaseColor: {r: 1, g: 1, b: 1, a: 1} - _Color: {r: 1, g: 1, b: 1, a: 1} - _Emission: {r: 0, g: 0, b: 0, a: 0} diff --git a/Assets/Metaballs/Shaders/Includes/Metaball.hlsl b/Assets/Metaballs/Shaders/Includes/Metaball.hlsl index dee5624..8f7568a 100644 --- a/Assets/Metaballs/Shaders/Includes/Metaball.hlsl +++ b/Assets/Metaballs/Shaders/Includes/Metaball.hlsl @@ -10,12 +10,7 @@ float4 _ParticlesPos[MAX_PARTICLES]; float _ParticlesSize[MAX_PARTICLES]; float _NumParticles; -float GetDistanceSphere(float3 from, float3 center, float radius) -{ - return length(from - center) - radius; -} - -float GetDistanceMetaball(float3 from) +float GetDistanceMetaball(float3 from, float densityThreshold) { float sumDensity = 0; float sumRi = 0; @@ -23,7 +18,7 @@ float GetDistanceMetaball(float3 from) for (int i = 0; i < _NumParticles; ++i) { float4 center = _ParticlesPos[i]; - float radius = 0.3 * _ParticlesSize[i]; + float radius = 0.5 * _ParticlesSize[i]; float r = length(center - from); if (r <= radius) { @@ -33,60 +28,22 @@ float GetDistanceMetaball(float3 from) sumRi += radius; } - return max(minDistance, (0.2 - sumDensity) / (3 / 2.0 * sumRi)); + return max(minDistance, (densityThreshold - sumDensity) / (3 / 2.0 * sumRi)); } -float3 CalculateNormalMetaball(float3 from) +float3 CalculateNormalMetaball(float3 from, float densityThreshold) { float delta = 10e-5; float3 normal = float3( - GetDistanceMetaball(from + float3(delta, 0, 0)) - GetDistanceMetaball(from + float3(-delta, 0, 0)), - GetDistanceMetaball(from + float3(0, delta, 0)) - GetDistanceMetaball(from + float3(-0, -delta, 0)), - GetDistanceMetaball(from + float3(0, 0, delta)) - GetDistanceMetaball(from + float3(0, 0, -delta)) + GetDistanceMetaball(from + float3(delta, 0, 0), densityThreshold) - GetDistanceMetaball(from + float3(-delta, 0, 0), densityThreshold), + GetDistanceMetaball(from + float3(0, delta, 0), densityThreshold) - GetDistanceMetaball(from + float3(-0, -delta, 0), densityThreshold), + GetDistanceMetaball(from + float3(0, 0, delta), densityThreshold) - GetDistanceMetaball(from + float3(0, 0, -delta), densityThreshold) ); return normalize(normal); } -void SphereTraceSphereP1_float(float3 WorldPosition, float3 ObjectPosition, out float Alpha) -{ - #if defined(SHADERGRAPH_PREVIEW) - Alpha = 1; - #else - float maxDistance = 100; - float threshold = 0.00001; - float t = 0; - int numSteps = 0; - - float outAlpha = 0; - - float3 viewPosition = GetCurrentViewPosition(); - half3 viewDir = SafeNormalize(WorldPosition - viewPosition); - while (t < maxDistance) - { - float minDistance = 1000000; - float3 from = viewPosition + t * viewDir; - float d = GetDistanceSphere(from, float3(0, 0, 0), 0.2); - if (d < minDistance) - { - minDistance = d; - } - - if (minDistance <= threshold * t) - { - outAlpha = 1; - break; - } - - t += minDistance; - ++numSteps; - } - - Alpha = outAlpha; - #endif -} - -void SphereTraceMetaballs_float(float3 WorldPosition, out float3 PositionWS, out float3 NormalWS, out float Alpha, +void SphereTraceMetaballs_float(float3 WorldPosition, float DensityThreshold, out float3 PositionWS, out float3 NormalWS, out float Alpha, out float3 ViewDirection) { #if defined(SHADERGRAPH_PREVIEW) @@ -108,7 +65,7 @@ void SphereTraceMetaballs_float(float3 WorldPosition, out float3 PositionWS, out { float minDistance = 1000000; float3 from = viewPosition + t * viewDir; - float d = GetDistanceMetaball(from); + float d = GetDistanceMetaball(from, DensityThreshold); if (d < minDistance) { minDistance = d; @@ -117,7 +74,7 @@ void SphereTraceMetaballs_float(float3 WorldPosition, out float3 PositionWS, out if (minDistance <= threshold * t) { PositionWS = from; - NormalWS = CalculateNormalMetaball(from); + NormalWS = CalculateNormalMetaball(from, DensityThreshold); ViewDirection = viewDir; outAlpha = 1; break; @@ -131,7 +88,7 @@ void SphereTraceMetaballs_float(float3 WorldPosition, out float3 PositionWS, out #endif } -void SphereTraceMetaballs_half(half3 WorldPosition, half3 CameraPosition, out half3 PositionWS, out half3 NormalWS, +void SphereTraceMetaballs_half(half3 WorldPosition, float DensityThreshold, out half3 PositionWS, out half3 NormalWS, out half Alpha, out half3 ViewDirection) { #if defined(SHADERGRAPH_PREVIEW) @@ -152,8 +109,8 @@ void SphereTraceMetaballs_half(half3 WorldPosition, half3 CameraPosition, out ha while (t < maxDistance) { half minDistance = 1000000; - half3 from = CameraPosition + t * viewDir; - half d = GetDistanceMetaball(from); + half3 from = viewPosition + t * viewDir; + half d = GetDistanceMetaball(from, DensityThreshold); if (d < minDistance) { minDistance = d; @@ -162,7 +119,7 @@ void SphereTraceMetaballs_half(half3 WorldPosition, half3 CameraPosition, out ha if (minDistance <= threshold * t) { PositionWS = from; - NormalWS = CalculateNormalMetaball(from); + NormalWS = CalculateNormalMetaball(from, DensityThreshold); ViewDirection = viewDir; outAlpha = 1; break; diff --git a/Assets/Metaballs/Shaders/Metaball.shadergraph b/Assets/Metaballs/Shaders/Metaball.shadergraph index 8084cdf..2a817fb 100644 --- a/Assets/Metaballs/Shaders/Metaball.shadergraph +++ b/Assets/Metaballs/Shaders/Metaball.shadergraph @@ -17,6 +17,9 @@ }, { "m_Id": "f55f0a415b7a44638c62b4a77a887b94" + }, + { + "m_Id": "9fb51cfc0c584818973239dd3724f37d" } ], "m_Keywords": [], @@ -71,6 +74,9 @@ }, { "m_Id": "f651c3b081be4a998819d357a5589ca5" + }, + { + "m_Id": "3e95375fec354c8783409765844bd353" } ], "m_GroupDatas": [], @@ -104,6 +110,20 @@ "m_SlotId": -1785826532 } }, + { + "m_OutputSlot": { + "m_Node": { + "m_Id": "3e95375fec354c8783409765844bd353" + }, + "m_SlotId": 0 + }, + "m_InputSlot": { + "m_Node": { + "m_Id": "ee3f9d5447564ffaa9fd90f55788f623" + }, + "m_SlotId": 608757049 + } + }, { "m_OutputSlot": { "m_Node": { @@ -614,6 +634,41 @@ "m_ColorMode": 0 } +{ + "m_SGVersion": 0, + "m_Type": "UnityEditor.ShaderGraph.PropertyNode", + "m_ObjectId": "3e95375fec354c8783409765844bd353", + "m_Group": { + "m_Id": "" + }, + "m_Name": "Property", + "m_DrawState": { + "m_Expanded": true, + "m_Position": { + "serializedVersion": "2", + "x": -1298.0001220703125, + "y": 112.99999237060547, + "width": 169.0, + "height": 34.0 + } + }, + "m_Slots": [ + { + "m_Id": "4f171f14b5004b57aaaefdbc63ec9383" + } + ], + "synonyms": [], + "m_Precision": 0, + "m_PreviewExpanded": true, + "m_PreviewMode": 0, + "m_CustomColors": { + "m_SerializableColors": [] + }, + "m_Property": { + "m_Id": "9fb51cfc0c584818973239dd3724f37d" + } +} + { "m_SGVersion": 0, "m_Type": "UnityEditor.ShaderGraph.Vector1MaterialSlot", @@ -775,6 +830,21 @@ "m_Labels": [] } +{ + "m_SGVersion": 0, + "m_Type": "UnityEditor.ShaderGraph.Vector1MaterialSlot", + "m_ObjectId": "4f171f14b5004b57aaaefdbc63ec9383", + "m_Id": 0, + "m_DisplayName": "DensityThreshold", + "m_SlotType": 1, + "m_Hidden": false, + "m_ShaderOutputName": "Out", + "m_StageCapability": 3, + "m_Value": 0.0, + "m_DefaultValue": 0.0, + "m_Labels": [] +} + { "m_SGVersion": 0, "m_Type": "UnityEditor.ShaderGraph.BlockNode", @@ -851,7 +921,7 @@ "m_Hidden": false, "m_ShaderOutputName": "AlphaClipThreshold", "m_StageCapability": 2, - "m_Value": 0.5, + "m_Value": 0.8999999761581421, "m_DefaultValue": 0.5, "m_Labels": [] } @@ -1006,6 +1076,29 @@ "m_SerializedDescriptor": "VertexDescription.Position" } +{ + "m_SGVersion": 1, + "m_Type": "UnityEditor.ShaderGraph.Internal.Vector1ShaderProperty", + "m_ObjectId": "9fb51cfc0c584818973239dd3724f37d", + "m_Guid": { + "m_GuidSerialized": "a1afd403-89fe-4ce4-9fe9-444df16da722" + }, + "m_Name": "DensityThreshold", + "m_DefaultReferenceName": "Vector1_9fb51cfc0c584818973239dd3724f37d", + "m_OverrideReferenceName": "_DensityThreshold", + "m_GeneratePropertyBlock": true, + "m_Precision": 0, + "overrideHLSLDeclaration": false, + "hlslDeclarationOverride": 0, + "m_Hidden": false, + "m_Value": 0.20000000298023225, + "m_FloatType": 0, + "m_RangeValues": { + "x": 0.0, + "y": 1.0 + } +} + { "m_SGVersion": 0, "m_Type": "UnityEditor.ShaderGraph.PropertyNode", @@ -1228,6 +1321,21 @@ "m_Space": 0 } +{ + "m_SGVersion": 0, + "m_Type": "UnityEditor.ShaderGraph.Vector1MaterialSlot", + "m_ObjectId": "cef6d797bf3c4bdd86fd054caff9bb73", + "m_Id": 608757049, + "m_DisplayName": "DensityThreshold", + "m_SlotType": 0, + "m_Hidden": false, + "m_ShaderOutputName": "Vector1_5df250a7bfb846319a1aa2183de92d5c", + "m_StageCapability": 3, + "m_Value": 0.20000000298023225, + "m_DefaultValue": 0.0, + "m_Labels": [] +} + { "m_SGVersion": 0, "m_Type": "UnityEditor.ShaderGraph.Vector1MaterialSlot", @@ -1372,6 +1480,9 @@ { "m_Id": "4d92a22a48e349308b311d370c5adcfd" }, + { + "m_Id": "cef6d797bf3c4bdd86fd054caff9bb73" + }, { "m_Id": "26a57ddb9982465f808531bf70a92a50" }, @@ -1392,14 +1503,16 @@ "1bec8c28-f6f7-4e25-bd03-1ebe66a30182", "f8b0de1e-7659-4ab0-a3d1-a5c361785663", "348487e7-bd23-4c00-a91a-35438fa361eb", - "c0fbe363-0b1f-4b74-8a78-201b67fca458" + "c0fbe363-0b1f-4b74-8a78-201b67fca458", + "0b57aaf6-131f-4fc7-b476-3c62a35ab308" ], "m_PropertyIds": [ -1785826532, -216284529, 736728269, -1129854196, - -337336241 + -337336241, + 608757049 ] } diff --git a/Assets/Metaballs/Shaders/SphereTraceMetaballs.shadersubgraph b/Assets/Metaballs/Shaders/SphereTraceMetaballs.shadersubgraph index 5a429fa..4c0c603 100644 --- a/Assets/Metaballs/Shaders/SphereTraceMetaballs.shadersubgraph +++ b/Assets/Metaballs/Shaders/SphereTraceMetaballs.shadersubgraph @@ -17,6 +17,9 @@ }, { "m_Id": "af02328cc2d34dfbbd13d9f5ce1b281e" + }, + { + "m_Id": "5df250a7bfb846319a1aa2183de92d5c" } ], "m_Keywords": [], @@ -56,6 +59,9 @@ }, { "m_Id": "0f16808f4ea444ce9244676832e2374d" + }, + { + "m_Id": "390d3c9b2b6141a681ffbee64a6cac52" } ], "m_GroupDatas": [], @@ -187,6 +193,20 @@ "m_SlotId": 2 } }, + { + "m_OutputSlot": { + "m_Node": { + "m_Id": "390d3c9b2b6141a681ffbee64a6cac52" + }, + "m_SlotId": 0 + }, + "m_InputSlot": { + "m_Node": { + "m_Id": "357f6d1d9b0d43f1be902b0b0e7fec4a" + }, + "m_SlotId": 6 + } + }, { "m_OutputSlot": { "m_Node": { @@ -443,6 +463,21 @@ "m_Channel": 2 } +{ + "m_SGVersion": 0, + "m_Type": "UnityEditor.ShaderGraph.Vector1MaterialSlot", + "m_ObjectId": "17ff36c67950481bb163649210f5c4c8", + "m_Id": 6, + "m_DisplayName": "DensityThreshold", + "m_SlotType": 0, + "m_Hidden": false, + "m_ShaderOutputName": "DensityThreshold", + "m_StageCapability": 3, + "m_Value": 0.0, + "m_DefaultValue": 0.0, + "m_Labels": [] +} + { "m_SGVersion": 0, "m_Type": "UnityEditor.ShaderGraph.Vector3MaterialSlot", @@ -547,8 +582,8 @@ "m_Expanded": true, "m_Position": { "serializedVersion": "2", - "x": -507.0, - "y": 52.0, + "x": -614.0000610351563, + "y": -13.999975204467774, "width": 206.0, "height": 132.0 } @@ -590,6 +625,9 @@ { "m_Id": "a44ad65d3b0545749c85e40e11b9e923" }, + { + "m_Id": "17ff36c67950481bb163649210f5c4c8" + }, { "m_Id": "e730d6912d554fc69bf1b4d190c457f5" }, @@ -616,6 +654,41 @@ "m_FunctionBody": "Enter function body here..." } +{ + "m_SGVersion": 0, + "m_Type": "UnityEditor.ShaderGraph.PropertyNode", + "m_ObjectId": "390d3c9b2b6141a681ffbee64a6cac52", + "m_Group": { + "m_Id": "" + }, + "m_Name": "Property", + "m_DrawState": { + "m_Expanded": true, + "m_Position": { + "serializedVersion": "2", + "x": -577.0000610351563, + "y": 136.0000457763672, + "width": 169.0, + "height": 34.0 + } + }, + "m_Slots": [ + { + "m_Id": "55f17bba17fe45de8d3402c090ea2b6d" + } + ], + "synonyms": [], + "m_Precision": 0, + "m_PreviewExpanded": true, + "m_PreviewMode": 0, + "m_CustomColors": { + "m_SerializableColors": [] + }, + "m_Property": { + "m_Id": "5df250a7bfb846319a1aa2183de92d5c" + } +} + { "m_SGVersion": 1, "m_Type": "UnityEditor.ShaderGraph.Internal.Vector3ShaderProperty", @@ -823,6 +896,21 @@ "m_ApplyScaling": true } +{ + "m_SGVersion": 0, + "m_Type": "UnityEditor.ShaderGraph.Vector1MaterialSlot", + "m_ObjectId": "55f17bba17fe45de8d3402c090ea2b6d", + "m_Id": 0, + "m_DisplayName": "DensityThreshold", + "m_SlotType": 1, + "m_Hidden": false, + "m_ShaderOutputName": "Out", + "m_StageCapability": 3, + "m_Value": 0.0, + "m_DefaultValue": 0.0, + "m_Labels": [] +} + { "m_SGVersion": 0, "m_Type": "UnityEditor.ShaderGraph.Vector1MaterialSlot", @@ -838,6 +926,29 @@ "m_Labels": [] } +{ + "m_SGVersion": 1, + "m_Type": "UnityEditor.ShaderGraph.Internal.Vector1ShaderProperty", + "m_ObjectId": "5df250a7bfb846319a1aa2183de92d5c", + "m_Guid": { + "m_GuidSerialized": "0b57aaf6-131f-4fc7-b476-3c62a35ab308" + }, + "m_Name": "DensityThreshold", + "m_DefaultReferenceName": "Vector1_5df250a7bfb846319a1aa2183de92d5c", + "m_OverrideReferenceName": "", + "m_GeneratePropertyBlock": true, + "m_Precision": 0, + "overrideHLSLDeclaration": false, + "hlslDeclarationOverride": 0, + "m_Hidden": false, + "m_Value": 0.20000000298023225, + "m_FloatType": 0, + "m_RangeValues": { + "x": 0.0, + "y": 1.0 + } +} + { "m_SGVersion": 1, "m_Type": "UnityEditor.ShaderGraph.CustomFunctionNode", diff --git a/Assets/Scenes/MixAndJam.unity b/Assets/Scenes/MixAndJam.unity index 18e1d6d..2ebf34a 100644 --- a/Assets/Scenes/MixAndJam.unity +++ b/Assets/Scenes/MixAndJam.unity @@ -11403,7 +11403,7 @@ Transform: m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 869074592} - m_LocalRotation: {x: 0.09112655, y: 0.032211166, z: -0.0029491126, w: 0.9953139} + m_LocalRotation: {x: 0.091126256, y: 0.032310873, z: -0.0029582412, w: 0.99531066} m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} m_Children: @@ -11508,7 +11508,7 @@ Transform: m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 906262002} - m_LocalRotation: {x: 0.054911688, y: 0.032297026, z: -0.0017770969, w: 0.9979672} + m_LocalRotation: {x: 0.054911505, y: 0.03239701, z: -0.0017825981, w: 0.9979639} m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} m_Children: @@ -11734,7 +11734,7 @@ ParticleSystemRenderer: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 949289638} m_Enabled: 1 - m_CastShadows: 1 + m_CastShadows: 0 m_ReceiveShadows: 0 m_DynamicOccludee: 1 m_MotionVectors: 1 @@ -12043,8 +12043,8 @@ ParticleSystem: startSize: serializedVersion: 2 minMaxState: 3 - scalar: 1.2 - minScalar: 1 + scalar: 1 + minScalar: 0.7 maxCurve: serializedVersion: 2 m_Curve: @@ -17174,7 +17174,7 @@ Transform: m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 1687258581} - m_LocalRotation: {x: 0.09112655, y: 0.032211162, z: -0.002949112, w: 0.9953139} + m_LocalRotation: {x: 0.09112623, y: 0.03231088, z: -0.0029582412, w: 0.99531066} m_LocalPosition: {x: 0, y: 2.5, z: -7.04} m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] @@ -17583,7 +17583,7 @@ Transform: m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 2101118512} - m_LocalRotation: {x: 0.09112655, y: 0.032211166, z: -0.0029491126, w: 0.9953139} + m_LocalRotation: {x: 0.091126285, y: 0.032310866, z: -0.0029582419, w: 0.99531066} m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} m_Children: diff --git a/Assets/Settings/CustomForwardRendererData.asset b/Assets/Settings/CustomForwardRendererData.asset index f18ff5a..0674e07 100644 --- a/Assets/Settings/CustomForwardRendererData.asset +++ b/Assets/Settings/CustomForwardRendererData.asset @@ -79,7 +79,7 @@ MonoBehaviour: restoreCamera: 1 offset: {x: 0, y: 0, z: 0, w: 0} cameraFieldOfView: 60 - downsamplingAmount: 4 + downsamplingAmount: 8 --- !u!114 &11400000 MonoBehaviour: m_ObjectHideFlags: 0 diff --git a/ProjectSettings/GraphicsSettings.asset b/ProjectSettings/GraphicsSettings.asset index db10dd6..150f2f6 100644 --- a/ProjectSettings/GraphicsSettings.asset +++ b/ProjectSettings/GraphicsSettings.asset @@ -28,6 +28,7 @@ GraphicsSettings: m_LensFlare: m_Mode: 1 m_Shader: {fileID: 102, guid: 0000000000000000f000000000000000, type: 0} + m_VideoShadersIncludeMode: 2 m_AlwaysIncludedShaders: - {fileID: 7, guid: 0000000000000000f000000000000000, type: 0} - {fileID: 15104, guid: 0000000000000000f000000000000000, type: 0} @@ -40,10 +41,8 @@ GraphicsSettings: - {fileID: 16001, guid: 0000000000000000f000000000000000, type: 0} - {fileID: 17000, guid: 0000000000000000f000000000000000, type: 0} m_PreloadedShaders: [] - m_SpritesDefaultMaterial: {fileID: 10754, guid: 0000000000000000f000000000000000, - type: 0} - m_CustomRenderPipeline: {fileID: 11400000, guid: 19ba41d7c0026c3459d37c2fe90c55a0, - type: 2} + m_SpritesDefaultMaterial: {fileID: 10754, guid: 0000000000000000f000000000000000, type: 0} + m_CustomRenderPipeline: {fileID: 11400000, guid: 19ba41d7c0026c3459d37c2fe90c55a0, type: 2} m_TransparencySortMode: 0 m_TransparencySortAxis: {x: 0, y: 0, z: 1} m_DefaultRenderingPath: 1 @@ -64,5 +63,5 @@ GraphicsSettings: m_AlbedoSwatchInfos: [] m_LightsUseLinearIntensity: 1 m_LightsUseColorTemperature: 0 + m_DefaultRenderingLayerMask: 1 m_LogWhenShaderIsCompiled: 0 - m_AllowEnlightenSupportForUpgradedProject: 1