修改水

This commit is contained in:
2026-01-01 22:00:33 +08:00
parent 040a222bd6
commit 9ceffccd39
1800 changed files with 103929 additions and 139495 deletions

View File

@@ -1,62 +0,0 @@
#if (OBI_BURST && OBI_MATHEMATICS && OBI_COLLECTIONS)
using Unity.Jobs;
using Unity.Collections;
using Unity.Mathematics;
using Unity.Burst;
using UnityEngine;
namespace Obi
{
[BurstCompile]
struct BuildParticleMeshDataJob : IJobParallelFor
{
[ReadOnly] public NativeArray<int> particleIndices;
[ReadOnly] public NativeArray<int> rendererIndices;
[ReadOnly] public NativeArray<ParticleRendererData> rendererData;
[ReadOnly] public NativeArray<float4> renderablePositions;
[ReadOnly] public NativeArray<quaternion> renderableOrientations;
[ReadOnly] public NativeArray<float4> renderableRadii;
[ReadOnly] public NativeArray<float4> colors;
[NativeDisableParallelForRestriction] public NativeArray<ParticleVertex> vertices;
[NativeDisableParallelForRestriction] public NativeArray<int> indices;
[ReadOnly] public int firstParticle;
public void Execute(int i)
{
int p = particleIndices[firstParticle + i];
int r = rendererIndices[firstParticle + i];
ParticleVertex v = new ParticleVertex();
v.pos = new float4(renderablePositions[p].xyz, 1);
v.color = colors[p] * (Vector4)rendererData[r].color;
v.b1 = new float4(math.mul(renderableOrientations[p], new float3(1, 0, 0)), renderableRadii[p][0] * renderableRadii[p][3] * rendererData[r].radiusScale);
v.b2 = new float4(math.mul(renderableOrientations[p], new float3(0, 1, 0)), renderableRadii[p][1] * renderableRadii[p][3] * rendererData[r].radiusScale);
v.b3 = new float4(math.mul(renderableOrientations[p], new float3(0, 0, 1)), renderableRadii[p][2] * renderableRadii[p][3] * rendererData[r].radiusScale);
v.offset = new float3(1, 1, 0);
vertices[i * 4] = v;
v.offset = new float3(-1, 1, 0);
vertices[i * 4 + 1] = v;
v.offset = new float3(-1, -1, 0);
vertices[i * 4 + 2] = v;
v.offset = new float3(1, -1, 0);
vertices[i * 4 + 3] = v;
indices[i * 6] = (i * 4 + 2);
indices[i * 6 + 1] = (i * 4 + 1);
indices[i * 6 + 2] = (i * 4);
indices[i * 6 + 3] = (i * 4 + 3);
indices[i * 6 + 4] = (i * 4 + 2);
indices[i * 6 + 5] = (i * 4);
}
}
}
#endif

View File

@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: cacb4a3597ee245a6b40358728c300e9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,247 +0,0 @@
#if (OBI_BURST && OBI_MATHEMATICS && OBI_COLLECTIONS)
using UnityEngine;
using UnityEngine.Rendering;
using Unity.Jobs;
using Unity.Collections;
using Unity.Mathematics;
using Unity.Burst;
using Unity.Collections.LowLevel.Unsafe;
using System.Collections.Generic;
#if (SRP_UNIVERSAL)
using UnityEngine.Rendering.Universal;
#endif
namespace Obi
{
public class BurstFoamRenderSystem : ObiFoamRenderSystem
{
protected NativeArray<float2> sortHandles;
protected struct SortHandleComparer : IComparer<float2>
{
public int Compare(float2 a, float2 b)
{
return b.y.CompareTo(a.y);
}
}
protected SortHandleComparer comparer = new SortHandleComparer();
public BurstFoamRenderSystem(ObiSolver solver) : base(solver)
{
#if (SRP_UNIVERSAL)
if (GraphicsSettings.currentRenderPipeline is UniversalRenderPipelineAsset)
renderBatch = new ProceduralRenderBatch<DiffuseParticleVertex>(0, Resources.Load<Material>("ObiMaterials/URP/Fluid/FoamParticlesURP"), new RenderBatchParams(true));
else
#endif
renderBatch = new ProceduralRenderBatch<DiffuseParticleVertex>(0, Resources.Load<Material>("ObiMaterials/Fluid/FoamParticles"), new RenderBatchParams(true));
ReallocateRenderBatch();
}
public override void Dispose()
{
base.Dispose();
if (sortHandles.IsCreated)
sortHandles.Dispose();
}
private void ReallocateRenderBatch()
{
// in case the amount of particles allocated does not match
// the amount requested by the solver, reallocate
if (!sortHandles.IsCreated || m_Solver.foamPositions.count * 4 != renderBatch.vertexCount)
{
renderBatch.Dispose();
renderBatch.vertexCount = m_Solver.foamPositions.count * 4;
renderBatch.triangleCount = m_Solver.foamPositions.count * 2;
renderBatch.Initialize(layout);
if (sortHandles.IsCreated)
sortHandles.Dispose();
sortHandles = new NativeArray<float2>(m_Solver.foamPositions.count, Allocator.Persistent);
}
}
public override void Setup()
{
}
public override void Step()
{
}
public override unsafe void Render()
{
if (!Application.isPlaying)
return;
var solver = m_Solver.implementation as BurstSolverImpl;
ReallocateRenderBatch();
foreach (Camera camera in cameras)
{
if (camera == null)
continue;
JobHandle inputDeps = new JobHandle();
var sortJob = sortHandles.Slice(0, m_Solver.foamCount[3]).SortJob(comparer);
//Clear all triangle indices to zero:
UnsafeUtility.MemClear(
NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(renderBatch.triangles),
UnsafeUtility.SizeOf<int>() * renderBatch.triangles.Length);
var projectJob = new ProjectOnSortAxisJob
{
inputPositions = solver.abstraction.foamPositions.AsNativeArray<float4>(),
sortHandles = sortHandles,
sortAxis = solver.abstraction.transform.InverseTransformDirection(camera.transform.forward)
};
inputDeps = projectJob.Schedule(m_Solver.foamCount[3], 256, inputDeps);
inputDeps = sortJob.Schedule(inputDeps);
var sortParticlesJob = new SortParticles
{
sortHandles = sortHandles,
inputPositions = solver.abstraction.foamPositions.AsNativeArray<float4>(),
inputVelocities = solver.abstraction.foamVelocities.AsNativeArray<float4>(),
inputColors = solver.abstraction.foamColors.AsNativeArray<float4>(),
inputAttributes = solver.abstraction.foamAttributes.AsNativeArray<float4>(),
outputPositions = solver.auxPositions,
outputVelocities = solver.auxVelocities,
outputColors = solver.auxColors,
outputAttributes = solver.auxAttributes
};
inputDeps = sortParticlesJob.Schedule(m_Solver.foamCount[3], 256, inputDeps);
var meshJob = new BuildFoamMeshDataJob
{
inputPositions = solver.auxPositions,
inputVelocities = solver.auxVelocities,
inputColors = solver.auxColors,
inputAttributes = solver.auxAttributes,
vertices = renderBatch.vertices,
indices = renderBatch.triangles,
};
inputDeps = meshJob.Schedule(m_Solver.foamCount[3], 128, inputDeps);
inputDeps.Complete();
renderBatch.mesh.SetVertexBufferData(renderBatch.vertices, 0, 0, renderBatch.vertexCount, 0, MeshUpdateFlags.DontValidateIndices | MeshUpdateFlags.DontRecalculateBounds | MeshUpdateFlags.DontResetBoneBounds | MeshUpdateFlags.DontNotifyMeshUsers);
renderBatch.mesh.SetIndexBufferData(renderBatch.triangles, 0, 0, renderBatch.triangleCount * 3, MeshUpdateFlags.DontRecalculateBounds | MeshUpdateFlags.DontValidateIndices);
matProps.SetFloat("_FadeDepth", 0);
matProps.SetFloat("_VelocityStretching", m_Solver.maxFoamVelocityStretch);
matProps.SetFloat("_RadiusScale", m_Solver.foamRadiusScale);
matProps.SetFloat("_FadeIn", m_Solver.foamFade.x);
matProps.SetFloat("_FadeOut", m_Solver.foamFade.y);
matProps.SetFloat("_ScatterDensity", m_Solver.foamVolumeDensity);
matProps.SetFloat("_AmbientDensity", m_Solver.foamAmbientDensity);
matProps.SetColor("_ScatterColor", m_Solver.foamScatterColor);
matProps.SetColor("_AmbientColor", m_Solver.foamAmbientColor);
var rp = renderBatch.renderParams;
rp.worldBounds = m_Solver.bounds;
rp.camera = camera;
rp.matProps = matProps;
Graphics.RenderMesh(rp, renderBatch.mesh, 0, m_Solver.transform.localToWorldMatrix, m_Solver.transform.localToWorldMatrix);
}
}
[BurstCompile]
unsafe struct ProjectOnSortAxisJob : IJobParallelFor
{
[ReadOnly] public NativeArray<float4> inputPositions;
[NativeDisableParallelForRestriction] public NativeArray<float2> sortHandles;
public float3 sortAxis;
public void Execute(int i)
{
sortHandles[i] = new float2(i, math.dot(inputPositions[i].xyz, sortAxis));
}
}
[BurstCompile]
unsafe struct SortParticles : IJobParallelFor
{
[ReadOnly] public NativeArray<float2> sortHandles;
[ReadOnly] public NativeArray<float4> inputPositions;
[ReadOnly] public NativeArray<float4> inputVelocities;
[ReadOnly] public NativeArray<float4> inputColors;
[ReadOnly] public NativeArray<float4> inputAttributes;
[NativeDisableParallelForRestriction] public NativeArray<float4> outputPositions;
[NativeDisableParallelForRestriction] public NativeArray<float4> outputVelocities;
[NativeDisableParallelForRestriction] public NativeArray<float4> outputColors;
[NativeDisableParallelForRestriction] public NativeArray<float4> outputAttributes;
public void Execute(int i)
{
int o = (int)sortHandles[i].x;
outputPositions[i] = inputPositions[o];
outputVelocities[i] = inputVelocities[o];
outputColors[i] = inputColors[o];
outputAttributes[i] = inputAttributes[o];
}
}
[BurstCompile]
struct BuildFoamMeshDataJob : IJobParallelFor
{
[ReadOnly] public NativeArray<float4> inputPositions;
[ReadOnly] public NativeArray<float4> inputVelocities;
[ReadOnly] public NativeArray<float4> inputColors;
[ReadOnly] public NativeArray<float4> inputAttributes;
[NativeDisableParallelForRestriction] public NativeArray<DiffuseParticleVertex> vertices;
[NativeDisableParallelForRestriction] public NativeArray<int> indices;
public void Execute(int i)
{
DiffuseParticleVertex v = new DiffuseParticleVertex();
v.pos = new float4(inputPositions[i].xyz, 1);
v.color = inputColors[i];
v.velocity = inputVelocities[i];
v.attributes = inputAttributes[i];
v.offset = new float3(1, 1, 0);
vertices[i * 4] = v;
v.offset = new float3(-1, 1, 0);
vertices[i * 4 + 1] = v;
v.offset = new float3(-1, -1, 0);
vertices[i * 4 + 2] = v;
v.offset = new float3(1, -1, 0);
vertices[i * 4 + 3] = v;
indices[i * 6] = (i * 4 + 2);
indices[i * 6 + 1] = (i * 4 + 1);
indices[i * 6 + 2] = (i * 4);
indices[i * 6 + 3] = (i * 4 + 3);
indices[i * 6 + 4] = (i * 4 + 2);
indices[i * 6 + 5] = (i * 4);
}
}
}
}
#endif

View File

@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 555e1cc2122984a93afc0f6f31fa173b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,100 +0,0 @@
#if (OBI_BURST && OBI_MATHEMATICS && OBI_COLLECTIONS)
using UnityEngine;
using Unity.Jobs;
using Unity.Collections;
using Unity.Mathematics;
using Unity.Burst;
namespace Obi
{
public class BurstInstancedParticleRenderSystem : ObiInstancedParticleRenderSystem
{
public BurstInstancedParticleRenderSystem(ObiSolver solver) : base(solver)
{
}
public override void Render()
{
using (m_RenderMarker.Auto())
{
var instanceTransformsJob = new InstancedParticleTransforms
{
activeParticles = activeParticles.AsNativeArray<int>(),
rendererData = rendererData.AsNativeArray<ParticleRendererData>(),
rendererIndex = rendererIndex.AsNativeArray<int>(),
instanceTransforms = instanceTransforms.AsNativeArray<float4x4>(),
instanceColors = instanceColors.AsNativeArray<float4>(),
renderablePositions = m_Solver.renderablePositions.AsNativeArray<float4>(),
renderableOrientations = m_Solver.renderableOrientations.AsNativeArray<quaternion>(),
renderableRadii = m_Solver.renderableRadii.AsNativeArray<float4>(),
colors = m_Solver.colors.AsNativeArray<float4>(),
solverToWorld = m_Solver.transform.localToWorldMatrix
};
instanceTransformsJob.Schedule(activeParticles.count, 32).Complete();
var mpb = new MaterialPropertyBlock();
//Draw instances:
for (int i = 0; i < batchList.Count; i++)
{
var batch = batchList[i];
if (batch.instanceCount > 0)
{
// workaround for RenderMeshInstanced bug
// (https://forum.unity.com/threads/gpu-instanced-custom-properties-dont-take-unity_baseinstanceid-into-account.1520602/)
// also, no NativeArray<> overload :(
mpb.SetVectorArray("_Colors", instanceColors.AsNativeArray<Vector4>().Slice(batch.firstInstance, batch.instanceCount).ToArray());
var rp = batch.renderParams;
rp.material = batch.material;
rp.worldBounds = m_Solver.bounds;
rp.matProps = mpb;
// TODO: use generic overload to pass matrix + instance color.
Graphics.RenderMeshInstanced(rp, batch.mesh, 0, instanceTransforms.AsNativeArray<Matrix4x4>(), batch.instanceCount, batch.firstInstance);
}
}
}
}
[BurstCompile]
struct InstancedParticleTransforms : IJobParallelFor
{
[ReadOnly] public NativeArray<int> activeParticles;
[ReadOnly] public NativeArray<ParticleRendererData> rendererData;
[ReadOnly] public NativeArray<int> rendererIndex;
[ReadOnly] public NativeArray<float4> renderablePositions;
[ReadOnly] public NativeArray<quaternion> renderableOrientations;
[ReadOnly] public NativeArray<float4> renderableRadii;
[ReadOnly] public NativeArray<float4> colors;
[ReadOnly] public float4x4 solverToWorld;
[NativeDisableParallelForRestriction] public NativeArray<float4x4> instanceTransforms;
[NativeDisableParallelForRestriction] public NativeArray<float4> instanceColors;
public void Execute(int i)
{
int p = activeParticles[i];
Matrix4x4 tfrm = float4x4.TRS(renderablePositions[p].xyz,
renderableOrientations[p],
renderableRadii[p].xyz * renderableRadii[p][3] * rendererData[rendererIndex[i]].radiusScale);
instanceTransforms[i] = math.mul(solverToWorld, tfrm);
instanceColors[i] = colors[p] * (Vector4)rendererData[rendererIndex[i]].color;
}
}
}
}
#endif

View File

@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: b3896a2253db543bcaa92126d83a7ba0
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,58 +0,0 @@
#if (OBI_BURST && OBI_MATHEMATICS && OBI_COLLECTIONS)
using UnityEngine;
using UnityEngine.Rendering;
using Unity.Jobs;
using Unity.Mathematics;
namespace Obi
{
public class BurstParticleRenderSystem : ObiParticleRenderSystem
{
public BurstParticleRenderSystem(ObiSolver solver) : base(solver)
{
m_Solver = solver;
}
public override void Render()
{
using (m_RenderMarker.Auto())
{
for (int i = 0; i < batchList.Count; ++i)
{
var batch = batchList[i];
var buildArraysJob = new BuildParticleMeshDataJob
{
particleIndices = activeParticles.AsNativeArray<int>(),
rendererIndices = rendererIndex.AsNativeArray<int>(),
rendererData = rendererData.AsNativeArray<ParticleRendererData>(),
renderablePositions = m_Solver.renderablePositions.AsNativeArray<float4>(),
renderableOrientations = m_Solver.renderableOrientations.AsNativeArray<quaternion>(),
renderableRadii = m_Solver.renderableRadii.AsNativeArray<float4>(),
colors = m_Solver.colors.AsNativeArray<float4>(),
vertices = batch.vertices,
indices = batch.triangles,
firstParticle = batch.firstParticle,
};
buildArraysJob.Schedule(batch.vertexCount / 4, 32).Complete();
batch.mesh.SetVertexBufferData(batch.vertices, 0, 0, batch.vertexCount, 0, MeshUpdateFlags.DontValidateIndices | MeshUpdateFlags.DontRecalculateBounds | MeshUpdateFlags.DontResetBoneBounds | MeshUpdateFlags.DontNotifyMeshUsers);
batch.mesh.SetIndexBufferData(batch.triangles, 0, 0, batch.triangleCount * 3, MeshUpdateFlags.DontRecalculateBounds | MeshUpdateFlags.DontValidateIndices);
var rp = batch.renderParams;
rp.worldBounds = m_Solver.bounds;
Graphics.RenderMesh(rp, batch.mesh, 0, m_Solver.transform.localToWorldMatrix, m_Solver.transform.localToWorldMatrix);
}
}
}
}
}
#endif

View File

@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: d1d1ddc20ab7148e3ac806e3559ba2b8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: