daily update on 20240513 by nitload youmu, instancing render added

This commit is contained in:
YoumuLiu
2024-05-13 03:39:54 +08:00
parent 56459deec5
commit a3506c96a3
78 changed files with 2562 additions and 1098 deletions
@@ -10,39 +10,52 @@ namespace StgSharp.Geometries
{
public abstract class ICoord
{
protected Matrix44 coordMat;
protected List<vec4d> pointBuffer;
private Matrix44 coordMat;
private Matrix44 origin;
public ICoord()
{
pointBuffer = new List<vec4d>();
}
public abstract Matrix44 GlobalCoordTransformation
//A matrix can convert vertex to global coord.
public Matrix44 GlobalCoordTransformation
{
get;
get
{
return coordMat * origin;
}
}
public abstract vec3d Origin
public vec3d Origin
{
get;
get => origin.colum3.XYZ;
internal set => origin.colum3.XYZ = value;
}
public abstract vec3d X_axis
public vec3d X_axis
{
get;
get => coordMat.colum0.XYZ;
internal set => coordMat.colum0.XYZ = value;
}
public abstract vec3d Y_axis
public vec3d Y_axis
{
get;
get => coordMat.colum1.XYZ;
internal set => coordMat.colum1.XYZ = value;
}
public abstract vec3d Z_axis
public vec3d Z_axis
{
get;
get => coordMat.colum2.XYZ;
set => coordMat.colum2.XYZ = value;
}
protected ref Matrix44 CoordMat
{
get => ref coordMat;
}
//TODO 几何体构建器需要重写
public T BuildGeometry<T>(params vec4d[] defaultVertex) where T : IGeometry
{
T ret = Activator.CreateInstance<T>();
@@ -0,0 +1,67 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="PlainCoordinate.cs"
// Project: StgSharp
// AuthorGroup: Nitload Space
// Copyright (c) Nitload Space. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the “Software”), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and
// this permission notice shall be included in all copies
// or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”,
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
using StgSharp.Math;
using System;
using System.Collections.Generic;
using System.Net.Http.Headers;
using System.Text;
namespace StgSharp.Geometries
{
public class PlainCoordinate : ICoord
{
public PlainCoordinate(
vec3d origin,
vec3d xAxis,
vec3d yAxis
)
{
CoordMat = Matrix44.Unit;
Linear.Orthogonalize(ref xAxis, ref yAxis);
CoordMat.colum0 = new vec4d(xAxis, 0);
CoordMat.colum1 = new vec4d(yAxis, 0);
CoordMat.colum2 = new vec4d(Vec3d.Cross(xAxis, yAxis), 0);
Origin = origin;
}
public new vec3d Z_axis
{
get => Vec3d.Cross(X_axis, Y_axis);
set => throw new InvalidOperationException();
}
}
}
@@ -10,14 +10,5 @@ namespace StgSharp.Geometries
{
public class StreamLocalCoordinate : ICoord
{
public override Matrix44 GlobalCoordTransformation => throw new NotImplementedException();
public override vec3d Origin => Vec3d.Zero;
public override vec3d X_axis => coordMat.colum0.XYZ;
public override vec3d Y_axis => coordMat.colum1.XYZ;
public override vec3d Z_axis => coordMat.colum2.XYZ;
}
}
@@ -0,0 +1,138 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.Math
{
public static partial class GeometryScaler
{
internal const float DegToRad = 180 / Scaler.Pi;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Degree ToDegree(float value)
{
return new Degree(value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Sin(Radius r)
{
return MathF.Sin(r._radius);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Tan(Radius r)
{
return MathF.Tan(r._radius);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Cos(Radius r)
{
return MathF.Cos(r._radius);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Sin(Degree r)
{
return MathF.Sin(r._degree/DegToRad);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Tan(Degree r)
{
return MathF.Tan(r._degree / DegToRad);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Cos(Degree r)
{
return MathF.Cos(r._degree / DegToRad);
}
}
public struct Degree
{
internal float _degree;
internal Degree(float dgree)
{
_degree = dgree;
}
public static implicit operator Radius(Degree d)
{
return new Radius(d._degree / GeometryScaler.DegToRad);
}
}
public struct Radius
{
internal float _radius;
public static Radius Zero
{
get =>new Radius(0);
}
public Radius(float radius)
{
_radius = radius;
}
[Obsolete("Unsafe, you may forget this is radius.",true)]
public Radius() { }
public static implicit operator Degree(Radius r)
{
return new Degree(r._radius * 180 / Scaler.Pi);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Radius operator -(Radius r)
{
return new Radius(-r._radius);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Radius operator -(Radius left, Radius Right)
{
return new Radius(left._radius - Right._radius);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Radius operator +(Radius left, Radius Right)
{
return new Radius(left._radius + Right._radius);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Radius operator *(Radius left, float right)
{
return new Radius(left._radius * right);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Radius operator /(Radius left, float right)
{
return new Radius(left._radius / right);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(Radius left, Radius right)
{
return left._radius == right._radius;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(Radius left, Radius right)
{
return left._radius != right._radius;
}
}
}
@@ -0,0 +1,94 @@
using StgSharp.Math;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
namespace StgSharp.Geometries
{
public delegate vec3d GeometryMotionDelegate(TimeSpanProvider timeSource, out float rotation);
public class GeometryMotion
{
private bool isIncrementMode;
private bool sourceIsStatic;
private TimeSpanProvider time;
private GeometryMotionDelegate movement;
private IntPtr handle;
public static GeometryMotion DefaultMotion
{
get
{
return new GeometryMotion
(
true,
null,
null
);
}
}
private static vec3d defaultMotion(TimeSpanProvider provider, out float rotation)
{
rotation = 0;
return Vec3d.Zero;
}
public unsafe GeometryMotion(
bool isIncreament,
TimeSpanProvider timeSource,
GeometryMotionDelegate motionDelegate
)
{
isIncrementMode = isIncreament;
time = timeSource;
if (motionDelegate == null)
{
handle = (IntPtr)(delegate*<TimeSpanProvider, out float, vec3d>)&defaultMotion;
movement = defaultMotion;
}
else if (motionDelegate.Method.IsStatic)
{
sourceIsStatic = true;
handle = Marshal.GetFunctionPointerForDelegate(motionDelegate);
movement = motionDelegate;
}
else
{
movement = motionDelegate;
}
}
public bool IsIncrement
{
get => isIncrementMode;
}
public unsafe void RunMotion(ref vec3d coord, ref float rotation)
{
float rot = 0;
if (sourceIsStatic)
{
}
vec3d vec = ((delegate*<TimeSpanProvider, out float, vec3d>)handle)(time, out rot);
if (isIncrementMode)
{
coord += vec;
rotation += rot;
}
else
{
coord = vec.XYZ;
rotation = rot;
}
}
}
}
@@ -40,62 +40,100 @@ using System.Threading.Tasks;
namespace StgSharp.Geometries
{
public interface IInstancing<T> where T : IInstancing<T>
public interface IInstancing<T> where T : IInstancing<T>
{
public int BufferId
{
get;
get;
set;
}
public vec2d CenterPositionGlobal
public vec3d CenterPositionGlobal
{
get;
set;
}
public vec2d Coord
public vec3d Coord
{
get { return new(GlobalBuffer.CoordList[BufferId]); }
set { GlobalBuffer.CoordList[BufferId] = value.vec; }
get { return GlobalBuffer.CoordList[BufferId].XYZ; }
set { GlobalBuffer.CoordList[BufferId] = new vec4d( value.vec); }
}
public PlainInstancingBuffer<T> GlobalBuffer
{
public PlainInstancingBuffer<T> GlobalBuffer
{
get;
internal set;
}
public float Rotation
{
get { return GlobalBuffer.CoordList[BufferId].W; }
set
{
vec4d temp = GlobalBuffer.CoordList[BufferId];
temp.W = value;
GlobalBuffer.CoordList[BufferId] = temp;
}
}
public float Scale
{
get { return GlobalBuffer.ScalingList[BufferId]; }
set { GlobalBuffer.ScalingList[BufferId] = value; }
}
public GeometryMotion Motion
{
get; set;
}
public void Move()
{
(vec3d coord , float rotation) =(Coord, Rotation);
Motion.RunMotion(ref coord, ref rotation);
(Rotation, Coord) = (rotation, coord);
}
}
public class ParticalOnPlain : IInstancing<ParticalOnPlain>
public class ParticleOnPlain : IInstancing<ParticleOnPlain>
{
public int BufferId
{
get;
set;
}
vec2d IInstancing<ParticalOnPlain>.CenterPositionGlobal
public ParticleOnPlain(PlainInstancingBuffer<ParticleOnPlain> buffer)
{
get ;
set ;
if (buffer == null)
{
throw new ArgumentNullException(paramName: nameof(buffer));
}
GlobalBuffer = buffer;
BufferId = buffer.CreateInstanceID();
buffer.InstanceList.Add(this);
}
public PlainInstancingBuffer<ParticalOnPlain> GlobalBuffer
public int BufferId
{
get;
set;
}
public ParticalOnPlain(PlainInstancingBuffer<ParticalOnPlain> buffer)
public PlainInstancingBuffer<ParticleOnPlain> GlobalBuffer
{
GlobalBuffer = buffer;
BufferId = buffer.CreateInstanceID();
get;
set;
}
public GeometryMotion Motion
{
get;
set;
}
vec3d IInstancing<ParticleOnPlain>.CenterPositionGlobal
{
get;
set;
}
}
}
@@ -19,7 +19,7 @@ namespace StgSharp.Geometries
public class PlainEntity:IPlainEntity
{
private PlainGeometry shape;
private PlainGeometry _shape;
vec2d IPlainEntity.CenterPosition
{
@@ -27,6 +27,11 @@ namespace StgSharp.Geometries
set => throw new NotImplementedException();
}
public PlainEntity(PlainGeometry shape)
{
_shape = shape;
}
bool IPlainEntity.CollideWith(IPlainEntity entity)
{
throw new NotImplementedException();
@@ -1,26 +1,81 @@
using System;
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="PlainInstancingBuffer.cs"
// Project: StgSharp
// AuthorGroup: Nitload Space
// Copyright (c) Nitload Space. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the “Software”), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and
// this permission notice shall be included in all copies
// or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”,
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
using StgSharp.Math;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Numerics;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace StgSharp.Geometries
{
public class PlainInstancingBuffer<T> where T : IInstancing<T>
public class PlainInstancingBuffer<T> : IDisposable where T : IInstancing<T>
{
private List<Vector4> coordList;
private List<float> scalingList;
private CoordinatePlain globalCorrdinnate;
private List<vec4d> coordList;
private List<IInstancing<T>> instanceList;
private List<float> scalingList;
private Mutex mutex;
private bool disposedValue;
#region field of mem opt
vec4d[] coordArray;
float[] scaleArray;
#endregion
public PlainInstancingBuffer()
{
coordList = new List<Vector4>();
instanceList = new List<IInstancing<T>>();
scalingList = new List<float>();
coordList = new List<vec4d>(capacity: 100);
instanceList = new List<IInstancing<T>>(capacity:100);
scalingList = new List<float>(capacity: 100);
coordArray = new vec4d[0];
scaleArray = new float[0];
mutex = new Mutex();
}
internal List<Vector4> CoordList
internal List<IInstancing<T>> InstanceList
{
get => instanceList;
}
internal List<vec4d> CoordList
{
get => coordList;
}
@@ -30,34 +85,108 @@ namespace StgSharp.Geometries
get => scalingList;
}
public Span<vec4d> CoordSpan
{
get
{
if (coordArray.Length != coordList.Capacity)
{
FieldInfo fieldInfo = typeof(List<vec4d>).
GetField("_items", BindingFlags.NonPublic | BindingFlags.Instance);
if (fieldInfo == null)
{
throw new NullReferenceException();
}
coordArray = (vec4d[])fieldInfo.GetValue(coordList);
}
return new Span<vec4d>(coordArray, 0, coordList.Count);
}
}
public Span<float> ScalingSpan
{
get
{
if (scaleArray.Length != coordList.Capacity)
{
FieldInfo fieldInfo = typeof(List<float>).
GetField("_items", BindingFlags.NonPublic | BindingFlags.Instance);
if (fieldInfo == null)
{
throw new NullReferenceException();
}
scaleArray = (float[])fieldInfo.GetValue(scalingList);
}
return new Span<float>(scaleArray, 0, coordList.Count);
}
}
public int CreateInstanceID()
{
mutex.WaitOne();
int ret = instanceList.Count;
coordList.Add(new Vector4());
coordList.Add(new vec4d());
scalingList.Add(0);
mutex.ReleaseMutex();
return ret;
}
public void Remove(T instance)
{
mutex.WaitOne();
if (instance.GlobalBuffer != this)
{
throw new ArgumentException();
throw new ArgumentException(
message:"instance does not belong to current Particle Buffer",
paramName: nameof(instance));
}
int index = instance.BufferId;
IInstancing<T> temp = instanceList[instanceList.Count - 1];
instanceList[instance.BufferId] = temp;
temp.BufferId = instance.BufferId;
coordList[instance.BufferId] = coordList[coordList.Count - 1];
scalingList[instance.BufferId] = scalingList[scalingList.Count - 1];
instanceList.RemoveAt(instanceList.Count - 1);
coordList.RemoveAt(coordList.Count - 1);
scalingList.RemoveAt(scalingList.Count - 1);
mutex.ReleaseMutex();
}
public void UpdateAllParticles()
{
foreach (var particle in instanceList)
{
}
}
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
mutex.Dispose();
}
coordList.Clear();
instanceList.Clear();
scalingList.Clear();
disposedValue = true;
}
}
// ~PlainInstancingBuffer()
// {
// Dispose(disposing: false);
// }
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}
@@ -50,7 +50,10 @@ namespace StgSharp.Geometries
endPoint = new Point(end);
}
public Line() { }
public Line()
{
}
public Line(Point begin, Point end)
{
@@ -59,7 +62,7 @@ namespace StgSharp.Geometries
}
internal override int[] Indices => throw new Exception();
internal override int[] Indices => throw new NotSupportedException();
public override Line[] GetAllSides()
{
@@ -74,18 +77,18 @@ namespace StgSharp.Geometries
/// <summary>
/// Move begin side of a line.
/// It is recommended to override this function rather than
/// loading an <see cref="Func<int,vec3d>"/> with same function.
/// loading an <see cref="Func{T, TResult}"/> with same function.
/// </summary>
/// <param name="tick">Time passed since current line was created.</param>
public virtual vec3d MoveBeginPoint(int tick)
{
return movBegin.Invoke(time);
return movBegin(time);
}
/// <summary>
/// Move end side of a line.
/// It is recommended to override this function rather than
/// loading an <see cref="Func<int,vec3d>"/> with same function.
/// loading an <see cref="Func{T,TResult}"/> with same function.
/// </summary>
/// <param name="tick">Time passed since current line was created.</param>
/// <returns></returns>
@@ -52,7 +52,7 @@ namespace StgSharp.Geometries
v0x, v0y, v0z,
v1x, v1y, v1z,
v2x, v2y, v2z,
v3x, v2y, v3z
v3x, v3y, v3z
)
{
if (
@@ -46,7 +46,7 @@ namespace StgSharp.Geometries
v0x, v0y, v0z,
v1x, v1y, v1z,
v2x, v2y, v2z,
v3x, v2y, v3z
v3x, v3y, v3z
)
{
#if DEBUG