Skip to content

Commit

Permalink
Merge branch 'voronoi'
Browse files Browse the repository at this point in the history
  • Loading branch information
kareemalkoul committed Jun 13, 2022
2 parents 5e4617f + 587f07b commit 8ff168f
Show file tree
Hide file tree
Showing 10 changed files with 1,101 additions and 14 deletions.
8 changes: 5 additions & 3 deletions Assets/Materials/Particle3D.mat
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
%TAG !u! tag:unity3d.com,2011:
--- !u!21 &2100000
Material:
serializedVersion: 6
serializedVersion: 8
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Particle3D
m_Shader: {fileID: 4800000, guid: 773f4e8ce8ec9c74cae8ffe7f76df354, type: 3}
m_ShaderKeywords:
m_ValidKeywords: []
m_InvalidKeywords: []
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 1
Expand Down Expand Up @@ -55,6 +56,7 @@ Material:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Ints: []
m_Floats:
- AbsorptionScale: 1.5
- _BumpScale: 1
Expand All @@ -78,5 +80,5 @@ Material:
- AbsorptionCoff: {r: 0.45, g: 0.029, b: 0.018, a: 1}
- _Color: {r: 1, g: 1, b: 1, a: 1}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
- _WaterColor: {r: 0.25882354, g: 0.57254905, b: 0.97647065, a: 1}
- _WaterColor: {r: 0.8627451, g: 0.9556026, b: 1, a: 0.18039216}
m_BuildTextureStacks: []
2 changes: 1 addition & 1 deletion Assets/Prefabs/Fluid3D.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,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: []
m_Father: {fileID: 0}
m_RootOrder: 0
Expand Down Expand Up @@ -84,6 +85,5 @@ MonoBehaviour:
Lines: []
solver: {fileID: 3940404416909209042}
RenderParticleMat: {fileID: 2100000, guid: 79b8e4e87d0027242b76475f51be3f57, type: 2}
WaterColor: {r: 0.25882354, g: 0.57254905, b: 0.97647065, a: 1}
IsRenderInShader: 1
IsBoundsDrawed: 1
4 changes: 4 additions & 0 deletions Assets/Scripts/Fluid3D/FluidBase3D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ protected virtual void Awake()

protected virtual void Start()
{
var res = Screen.currentResolution;
print(res);
// float ratio = (float)res.width/res.height;
// print(ratio);
InitBuffers();
InitProfilling();
rangeCompare = range + moveForce * Vector3.one;
Expand Down
10 changes: 6 additions & 4 deletions Assets/Scripts/Fluid3D/FluidRenderer3D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using UnityEngine.Profiling;
using System.Collections.Generic;


namespace Kareem.Fluid.SPH
{
[RequireComponent(typeof(Fluid3D))]
Expand All @@ -14,7 +15,7 @@ public class FluidRenderer3D : MonoBehaviour

public Fluid3D solver;
public Material RenderParticleMat;
public Color WaterColor;
// public Color WaterColor;
public bool IsRenderInShader = true;
public bool IsBoundsDrawed = true;

Expand Down Expand Up @@ -43,8 +44,9 @@ void OnRenderObject()
void DrawParticle()
{
float radius = 0.1f;
Color WaterColor = Color.blue;
RenderParticleMat.SetPass(0);
RenderParticleMat.SetColor("_WaterColor", WaterColor);
// RenderParticleMat.SetColor("_WaterColor", WaterColor);
RenderParticleMat.SetBuffer("_ParticlesBuffer", solver.ParticlesBuffer);
// RenderParticleMat.SetFloat("_ParticleRadius", solver.BallRadius);

Expand All @@ -55,11 +57,11 @@ void DrawParticle()
}
else
{
DrawByExtension(radius);
DrawByExtension(radius, WaterColor);
}
}

void DrawByExtension(float radius)
void DrawByExtension(float radius, Color WaterColor)
{
GetDate.Begin();
FluidParticle3D[] particles = new FluidParticle3D[solver.NumParticles];
Expand Down
81 changes: 77 additions & 4 deletions Assets/Shaders/Particle3D.shader
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Shader "Custom/SPH3D" {


Shader "Custom/SPH3D" {
Properties {
_MainTex("Texture", 2D) = "black" {}
_ParticleRadius("Particle Radius", Float) = 0.05
Expand All @@ -7,10 +9,13 @@

CGINCLUDE
#include "UnityCG.cginc"


sampler2D _MainTex;
float4 _MainTex_ST;
fixed4 _WaterColor;
float4 _WaterColor;
uniform float iTime;
uniform float3 iResolution;

float _ParticleRadius;
float4x4 _InvViewMatrix;
Expand All @@ -21,7 +26,7 @@
};

struct g2f {
float4 pos : POSITION;
float4 pos : SV_POSITION;
float2 tex : TEXCOORD0;
float4 color : COLOR;
};
Expand All @@ -33,6 +38,60 @@

StructuredBuffer<FluidParticle> _ParticlesBuffer;

float2 fract(float2 num){
return num - floor(num);
}

float2 random(float2 block_index){
float2 p = float2(block_index);
float2 ret = fract(sin(float2(dot(p,float2(123.1,311.7)),dot(p,float2(269.5,183.3))))*4358.5453);

return ret;
//return vec2(0., 0.);
}

float4 mix(float4 v1, float4 v2, float a) { return v1 * (1 - a) + v2 * a;}

float2 hash2(float2 p ) {
return fract(sin(float2(dot(p, float2(123.4, 748.6)), dot(p, float2(547.3, 659.3))))*5232.85324);
}
float hash(float2 p) {
return fract(sin(dot(p, float2(43.232, 75.876)))*4526.3257);
}

float voronoi(float2 p) {
float2 n = floor(p);
float2 f = fract(p);
float md = 5.0;

float2 m = float2(0.0, 0.0);

for (int i = -1;i<=1;i++) {
for (int j = -1;j<=1;j++) {
float2 g = float2(i, j);
float o = hash(n+g);
o = 0.5+0.5*sin(unity_DeltaTime+5.038*o);
float2 r = g + o - f;
float d = dot(r, r);
if (d<md) {
md = d;
m = n+g+o;
}
}
}
return md;
}

float ov(float2 p) {
float v = 0.0;
float a = 0.4;
for (int i = 0;i<10;i++) {
v+= voronoi(p)*a;
p*=2.0;
a*=0.5;
}
return v;
}
// --------------------------------------------------------------------
// Vertex Shader
// --------------------------------------------------------------------
Expand Down Expand Up @@ -67,8 +126,15 @@

pIn.pos = mul(UNITY_MATRIX_VP, pIn.pos);

float4 a = float4(0.2, 0.4, 1.0, 1.0);
float4 b = float4(0.85, 0.9, 1.0, 1.0);

pIn.color = float4(mix(a, b, smoothstep(0.0, 0.5, ov(uv*5.0))));
// _WaterColor = float4(mix(a, b, smoothstep(0.0, 0.5, ov(uv*5.0))));

pIn.color = IN[0].color;
pIn.tex = uv;


triStream.Append(pIn);
}
Expand All @@ -81,13 +147,20 @@
// Fragment Shader
// --------------------------------------------------------------------
fixed4 frag(g2f input) : SV_Target {

float2 uv = float2(input.pos.x, input.pos.y)/float2(2560,1600);
// log(input.pos.x);
float4 a = float4(0.2, 0.4, 1.0, 1.0);
float4 b = float4(0.85, 0.9, 1.0, 1.0);

_WaterColor = _WaterColor* float4(mix(a, b, smoothstep(0.0, 0.5, ov(uv*5.0))));
return tex2D(_MainTex, input.tex)*_WaterColor;
}

ENDCG

SubShader {
Tags{ "RenderType" = "Transparent" "RenderType" = "Transparent" }
Tags{ "RenderType" = "Transparent" }
LOD 300

ZWrite Off
Expand Down
35 changes: 35 additions & 0 deletions ProjectSettings/MemorySettings.asset
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!387306366 &1
MemorySettings:
m_ObjectHideFlags: 0
m_EditorMemorySettings:
m_MainAllocatorBlockSize: -1
m_ThreadAllocatorBlockSize: -1
m_MainGfxBlockSize: -1
m_ThreadGfxBlockSize: -1
m_CacheBlockSize: -1
m_TypetreeBlockSize: -1
m_ProfilerBlockSize: -1
m_ProfilerEditorBlockSize: -1
m_BucketAllocatorGranularity: -1
m_BucketAllocatorBucketsCount: -1
m_BucketAllocatorBlockSize: -1
m_BucketAllocatorBlockCount: -1
m_ProfilerBucketAllocatorGranularity: -1
m_ProfilerBucketAllocatorBucketsCount: -1
m_ProfilerBucketAllocatorBlockSize: -1
m_ProfilerBucketAllocatorBlockCount: -1
m_TempAllocatorSizeMain: -1
m_JobTempAllocatorBlockSize: -1
m_BackgroundJobTempAllocatorBlockSize: -1
m_JobTempAllocatorReducedBlockSize: -1
m_TempAllocatorSizeGIBakingWorker: -1
m_TempAllocatorSizeNavMeshWorker: -1
m_TempAllocatorSizeAudioWorker: -1
m_TempAllocatorSizeCloudWorker: -1
m_TempAllocatorSizeGfx: -1
m_TempAllocatorSizeJobWorker: -1
m_TempAllocatorSizeBackgroundWorker: -1
m_TempAllocatorSizePreloadManager: -1
m_PlatformMemorySettings: {}
4 changes: 2 additions & 2 deletions ProjectSettings/ProjectVersion.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
m_EditorVersion: 2020.3.30f1
m_EditorVersionWithRevision: 2020.3.30f1 (1fb1bf06830e)
m_EditorVersion: 2021.3.0f1
m_EditorVersionWithRevision: 2021.3.0f1 (6eacc8284459)
Empty file added ProjectSettings/boot.config
Empty file.
7 changes: 7 additions & 0 deletions UserSettings/EditorUserSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ EditorUserSettings:
m_ObjectHideFlags: 0
serializedVersion: 4
m_ConfigSettings:
RecentlyUsedSceneGuid-0:
value: 5b53025e000d59035a0b092742705d4443164d797f2c73697b2d4967b0b3626a
flags: 0
RecentlyUsedScenePath-0:
value: 22424703114646680e0b0227036c6c111b07142f1f2b233e2867083debf42d
flags: 0
Expand All @@ -28,9 +31,13 @@ EditorUserSettings:
m_VCDebugCmd: 0
m_VCDebugOut: 0
m_SemanticMergeMode: 2
m_DesiredImportWorkerCount: 4
m_StandbyImportWorkerCount: 2
m_IdleImportWorkerShutdownDelay: 60000
m_VCShowFailedCheckout: 1
m_VCOverwriteFailedCheckoutAssets: 1
m_VCProjectOverlayIcons: 1
m_VCHierarchyOverlayIcons: 1
m_VCOtherOverlayIcons: 1
m_VCAllowAsyncUpdate: 1
m_ArtifactGarbageCollection: 1
Loading

0 comments on commit 8ff168f

Please sign in to comment.