Daily update on 20250316-095544 by Nitload Youmu. Additional Message : Linear math inmprovement

Signed-off-by: Nitload <YoumuLiu_NSI@whv0t.onmicrosoft.com>
This commit is contained in:
Nitload
2025-03-16 09:55:44 +08:00
parent 753cc167bc
commit a5aef8506d
58 changed files with 3162 additions and 506 deletions
+22 -20
View File
@@ -111,12 +111,13 @@ namespace StgSharp.Graphics
/// </param>
/// <exception cref="ArgumentException"></exception>
public unsafe void GainAllUniforms(
ShaderProgram source,
params string[] uniformName )
ShaderProgram source,
params string[] uniformName )
{
if( uniformName.Length != 1 ) {
if( uniformName is null || uniformName.Length != 1 ) {
throw new ArgumentException(
"Camera needs only one uniform.", nameof( uniformName ) );
"Camera needs exactly one uniform.",
nameof( uniformName ) );
}
convertedUniform = source.GetUniform<Matrix44>( uniformName[ 0 ] );
}
@@ -143,13 +144,14 @@ namespace StgSharp.Graphics
{
//Console.WriteLine(Projection * ViewBase);
OpenGL.OpenGLFunction.CurrentGL
.SetUniformValue( convertedUniform, Projection * View );
.SetUniformValue( convertedUniform, Projection * View );
}
public void SetViewDirection( Vec3 position, Vec3 target, Vec3 up )
{
Vec3 direction = position - target;
if( ( position.vec == cameraAtt.colum3.vec ) && ( this.up == up ) && ( cameraAtt.colum2.vec == direction.vec ) ) {
if( ( position.reg == cameraAtt.colum3.reg ) && ( this.up == up ) && ( cameraAtt.colum2.reg ==
direction.reg ) ) {
//no difference with precious value
return;
}
@@ -169,10 +171,10 @@ namespace StgSharp.Graphics
Vec3 right = Linear.Orthogonalize( Linear.Cross( up, direction ) );
cameraAtt.colum0.vec = right.vec;
cameraAtt.colum1.vec = up.vec;
cameraAtt.colum2.vec = direction.vec;
cameraAtt.colum3.vec = position.vec;
cameraAtt.colum0.reg = right. reg;
cameraAtt.colum1.reg = up.reg;
cameraAtt.colum2.reg = direction.reg;
cameraAtt.colum3.reg = position. reg;
InternalPitch();
InternalRow();
@@ -180,10 +182,10 @@ namespace StgSharp.Graphics
}
public void SetViewRange(
Radius fovRadius,
Vec2 size,
Vec2 offset,
(float front, float back) dephRange )
Radius fovRadius,
Vec2 size,
Vec2 offset,
(float front, float back) dephRange )
{
float
distance = _target.GetLength(),
@@ -264,8 +266,8 @@ namespace StgSharp.Graphics
Scaler.Cos( angle ) );
partialCoord *= rotation;
rotationAtt.colum1.vec = partialCoord.colum0.vec;
rotationAtt.colum2.vec = partialCoord.colum1.vec;
rotationAtt.colum1.reg = partialCoord.colum0.reg;
rotationAtt.colum2.reg = partialCoord.colum1.reg;
rotationAtt.isTransposed = false;
}
@@ -282,8 +284,8 @@ namespace StgSharp.Graphics
Scaler.Cos( angle ) );
partialCoord *= rotation;
rotationAtt.colum0.vec = partialCoord.colum0.vec;
rotationAtt.colum1.vec = partialCoord.colum1.vec;
rotationAtt.colum0.reg = partialCoord.colum0.reg;
rotationAtt.colum1.reg = partialCoord.colum1.reg;
rotationAtt.isTransposed = false;
}
@@ -299,8 +301,8 @@ namespace StgSharp.Graphics
Scaler.Cos( angle ) );
partialCoord *= rotation;
rotationAtt.colum0.vec = partialCoord.colum0.vec;
rotationAtt.colum2.vec = partialCoord.colum1.vec;
rotationAtt.colum0.reg = partialCoord.colum0.reg;
rotationAtt.colum2.reg = partialCoord.colum1.reg;
rotationAtt.isTransposed = false;
}
+40
View File
@@ -0,0 +1,40 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="IMatrix.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.Graphics;
using System;
using System.Collections.Generic;
using System.Text;
namespace StgSharp.Math
{
public interface IMatrix<TSelf> where TSelf: IMatrix<TSelf> { }
}
+21 -5
View File
@@ -37,14 +37,30 @@ using System.Threading.Tasks;
namespace StgSharp.Math
{
public interface IVector
public interface IVector<TSelf> where TSelf: struct, IVector<TSelf>
{
/*
public static TItem Create<TItem>(Vector4 source) where TItem : IVector
public static abstract TSelf Zero
{
return TItem
get;
}
public static abstract TSelf One
{
get;
}
public Vec2 XY
{
get;
set;
}
public Vec3 XYZ
{
get;
set;
}
/**/
}
}
+1 -2
View File
@@ -40,7 +40,7 @@ namespace StgSharp.Math
LayoutKind.Explicit,
Size = ( 2 * 2 * 4 * sizeof( float ) ) + sizeof( bool ),
Pack = 16 )]
public struct Matrix22 : IMat
public struct Matrix22 : IMatrix<Matrix22>
{
[FieldOffset( 2 * 2 * 4 * sizeof( float ) )]
@@ -101,7 +101,6 @@ namespace StgSharp.Math
float cache = _transpose.m10;
_transpose.m10 = _transpose.m01;
_transpose.m01 = cache;
isTransposed = true;
}
}
+1 -1
View File
@@ -41,7 +41,7 @@ namespace StgSharp.Math
LayoutKind.Explicit,
Size = ( ( 3 + 2 ) * 4 * sizeof( float ) ) + sizeof( bool ),
Pack = 16 )]
public struct Matrix23 : IMat
public struct Matrix23 : IMatrix<Matrix23>
{
[FieldOffset( 5 * 4 * sizeof( float ) )] internal bool isTransposed = false;
+1 -1
View File
@@ -41,7 +41,7 @@ namespace StgSharp.Math
LayoutKind.Explicit,
Size = ( ( 4 + 2 ) * 4 * sizeof( float ) ) + sizeof( bool ),
Pack = 16 )]
public struct Matrix24 : IMat
public struct Matrix24 : IMatrix<Matrix24>
{
[FieldOffset( 6 * 4 * sizeof( float ) )] internal bool isTransposed;
+1 -1
View File
@@ -41,7 +41,7 @@ namespace StgSharp.Math
LayoutKind.Explicit,
Size = ( 5 * 4 * sizeof( float ) ) + sizeof( bool ),
Pack = 16 )]
public struct Matrix32 : IMat
public struct Matrix32 : IMatrix<Matrix32>
{
[FieldOffset( 5 * 4 * sizeof( float ) )] internal bool isTransposed;
+4 -4
View File
@@ -41,7 +41,7 @@ namespace StgSharp.Math
LayoutKind.Explicit,
Size = ( 6 * 4 * sizeof( float ) ) + sizeof( bool ),
Pack = 16 )]
public struct Matrix33 : IMat
public struct Matrix33 : IMatrix<Matrix33>
{
[FieldOffset( 6 * 4 * sizeof( float ) )] internal bool isTransposed;
@@ -162,9 +162,9 @@ namespace StgSharp.Math
{
left.InternalTranspose();
return new Vec3(
Vector4.Dot( left.transpose.colum0, right.vec ),
Vector4.Dot( left.transpose.colum1, right.vec ),
Vector4.Dot( left.transpose.colum2, right.vec ) );
Vector4.Dot( left.transpose.colum0, right.reg.AsVector4() ),
Vector4.Dot( left.transpose.colum1, right.reg.AsVector4() ),
Vector4.Dot( left.transpose.colum2, right.reg.AsVector4() ) );
}
[MethodImpl( MethodImplOptions.AggressiveInlining )]
+1 -1
View File
@@ -42,7 +42,7 @@ namespace StgSharp.Math
LayoutKind.Explicit,
Size = ( 7 * 4 * sizeof( float ) ) + sizeof( bool ),
Pack = 16 )]
public struct Matrix34 : IMat
public struct Matrix34 : IMatrix<Matrix34>
{
[FieldOffset( 7 * 4 * sizeof( float ) )] internal bool isTransposed;
+1 -1
View File
@@ -41,7 +41,7 @@ namespace StgSharp.Math
LayoutKind.Explicit,
Size = ( ( 16 + 8 ) * sizeof( float ) ) + sizeof( bool ),
Pack = 16 )]
public struct Matrix42 : IMat
public struct Matrix42 : IMatrix<Matrix42>
{
[FieldOffset( ( 16 + 8 ) * sizeof( float ) )] internal bool isTransposed;
+34 -30
View File
@@ -40,10 +40,10 @@ using System.Security.AccessControl;
namespace StgSharp.Math
{
[StructLayout(
layoutKind: LayoutKind.Explicit,
Size = ( 7 * 4 * sizeof( float ) ) + sizeof( bool ),
Pack = 16 )]
public struct Matrix43 : IEquatable<Matrix43>, IMat
layoutKind: LayoutKind.Explicit,
Size = ( 7 * 4 * sizeof( float ) ) + sizeof( bool ),
Pack = 16 )]
public struct Matrix43 : IEquatable<Matrix43>, IMatrix<Matrix43>
{
[FieldOffset( 7 * 4 * sizeof( float ) )]
@@ -54,36 +54,36 @@ namespace StgSharp.Math
[FieldOffset( 3 * 4 * sizeof( float ) )]
internal ColumnSet4 transpose;
public Matrix43()
{
Unsafe.SkipInit(out this);
isTransposed = false;
}
internal Matrix43( Vector4 c0, Vector4 c1, Vector4 c2 )
{
Unsafe.SkipInit(out this);
Unsafe.SkipInit( out this );
isTransposed = false;
mat.colum0 = c0;
mat.colum1 = c1;
mat.colum2 = c2;
}
public Matrix43(
float a00,
float a01,
float a02,
float a10,
float a11,
float a12,
float a20,
float a21,
float a22,
float a30,
float a31,
float a32 )
public Matrix43()
{
Unsafe.SkipInit(out this);
Unsafe.SkipInit( out this );
isTransposed = false;
}
public Matrix43(
float a00,
float a01,
float a02,
float a10,
float a11,
float a12,
float a20,
float a21,
float a22,
float a30,
float a31,
float a32 )
{
Unsafe.SkipInit( out this );
isTransposed = false;
mat.colum0 = new Vector4( a00, a10, a20, a30 );
mat.colum1 = new Vector4( a01, a11, a21, a31 );
@@ -145,10 +145,12 @@ namespace StgSharp.Math
internal unsafe void InternalTranspose()
{
if( !isTransposed ) {
fixed( ColumnSet3* source = &this.mat )
fixed( ColumnSet4* target = &this.transpose ) {
InternalIO.Intrinsic.transpose34( source, target );
fixed( ColumnSet3* source = &this.mat ) {
fixed( ColumnSet4* target = &this.transpose ) {
InternalIO.Intrinsic.transpose34( source, target );
}
}
isTransposed = true;
}
}
@@ -250,10 +252,12 @@ namespace StgSharp.Math
}
[MethodImpl( MethodImplOptions.AggressiveInlining )]
public static unsafe Matrix43 operator +( Matrix43 left, Matrix43 right )
public static unsafe Matrix43 operator +(
Matrix43 left,
Matrix43 right )
{
Matrix43 ret = new Matrix43();
InternalIO.Intrinsic.add_mat_3(&left.mat, &right.mat, &ret.mat);
InternalIO.Intrinsic.add_mat_3( &left.mat, &right.mat, &ret.mat );
return ret;
}
+79 -72
View File
@@ -40,10 +40,10 @@ using System.Runtime.InteropServices;
namespace StgSharp.Math
{
[StructLayout(
LayoutKind.Explicit,
Size = ( 2 * 16 * sizeof( float ) ) + sizeof( bool ),
Pack = 16 )]
public unsafe struct Matrix44 : IEquatable<Matrix44>, IMat
LayoutKind.Explicit,
Size = ( 2 * 16 * sizeof( float ) ) + sizeof( bool ),
Pack = 16 )]
public unsafe struct Matrix44 : IEquatable<Matrix44>, IMatrix<Matrix44>
{
[FieldOffset( 2 * 64 * sizeof( float ) )] internal bool isTransposed;
@@ -55,26 +55,20 @@ namespace StgSharp.Math
[FieldOffset( 8 * sizeof( float ) )] internal Vec4 colum2;
[FieldOffset( 12 * sizeof( float ) )] internal Vec4 colum3;
public Matrix44()
{
Unsafe.SkipInit(out this);
isTransposed = false;
}
internal Matrix44( ColumnSet4 mat )
{
Unsafe.SkipInit(out this);
Unsafe.SkipInit( out this );
isTransposed = false;
this.mat = mat;
}
internal Matrix44(
Vector4 vec0,
Vector4 vec1,
Vector4 vec2,
Vector4 vec3 )
Vector4 vec0,
Vector4 vec1,
Vector4 vec2,
Vector4 vec3 )
{
Unsafe.SkipInit(out this);
Unsafe.SkipInit( out this );
isTransposed = false;
mat.colum0 = vec0;
mat.colum1 = vec1;
@@ -82,25 +76,31 @@ namespace StgSharp.Math
mat.colum3 = vec3;
}
public unsafe Matrix44(
float a00,
float a01,
float a02,
float a03,
float a10,
float a11,
float a12,
float a13,
float a20,
float a21,
float a22,
float a23,
float a30,
float a31,
float a32,
float a33 )
public Matrix44()
{
Unsafe.SkipInit(out this);
Unsafe.SkipInit( out this );
isTransposed = false;
}
public unsafe Matrix44(
float a00,
float a01,
float a02,
float a03,
float a10,
float a11,
float a12,
float a13,
float a20,
float a21,
float a22,
float a23,
float a30,
float a31,
float a32,
float a33 )
{
Unsafe.SkipInit( out this );
isTransposed = false;
mat.colum0 = new Vector4( a00, a10, a20, a30 );
mat.colum1 = new Vector4( a01, a11, a21, a31 );
@@ -195,6 +195,35 @@ namespace StgSharp.Math
mat.colum2.GetHashCode(), mat.colum3.GetHashCode() );
}
[MethodImpl( MethodImplOptions.AggressiveInlining )]
public unsafe Matrix42 Multiple( in Matrix42 right )
{
InternalTranspose();
Matrix42 ans = new Matrix42();
fixed( ColumnSet4* lptr = &transpose ) {
fixed( Vector4* rptr = &right.mat.colum0 ) {
InternalIO.Intrinsic.dot_42( lptr, rptr, &ans.mat.colum0 );
}
}
return ans;
}
public unsafe Matrix43 Multiple( in Matrix43 right )
{
InternalTranspose();
Matrix43 ans = new Matrix43();
fixed( Vector4* rptr = &right.mat.colum0 ) {
fixed( ColumnSet4* lptr = &transpose ) {
InternalIO.Intrinsic.dot_43( lptr, rptr, &ans.mat.colum0 );
}
}
return ans;
}
public override string ToString()
{
InternalTranspose();
@@ -214,8 +243,8 @@ namespace StgSharp.Math
[MethodImpl( MethodImplOptions.AggressiveInlining )]
public static unsafe Matrix44 operator -(
Matrix44 left,
Matrix44 right )
Matrix44 left,
Matrix44 right )
{
return new Matrix44(
right.mat.colum0 - left.mat.colum0,
@@ -237,50 +266,26 @@ namespace StgSharp.Math
mat.mat.colum2 * value, mat.mat.colum3 * value );
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe Matrix42 Multiple(in Matrix42 right)
{
InternalTranspose();
Matrix42 ans = new Matrix42();
fixed (ColumnSet4* lptr = &transpose)
fixed (Vector4* rptr = &right.mat.colum0)
{
InternalIO.Intrinsic.dot_42(lptr,rptr, &ans.mat.colum0);
}
return ans;
}
[Obsolete("The operand may cause performance loss, use Matrix.Multiple() method instead.",false)]
[Obsolete(
"The operand may cause performance loss, use Matrix.Multiple() method instead.",
false )]
[MethodImpl( MethodImplOptions.AggressiveInlining )]
public static unsafe Matrix42 operator *(
Matrix44 left,
Matrix42 right )
Matrix44 left,
Matrix42 right )
{
return left.Multiple(right);
return left.Multiple( right );
}
public unsafe Matrix43 Multiple(in Matrix43 right)
{
InternalTranspose();
Matrix43 ans = new Matrix43();
fixed(Vector4* rptr = &right.mat.colum0)
fixed (ColumnSet4* lptr = &transpose)
{
InternalIO.Intrinsic.dot_43(lptr,rptr,&ans.mat.colum0);
}
return ans;
}
[Obsolete("The operand may cause performance loss, use Matrix.Multiple() method instead.", false)]
[Obsolete(
"The operand may cause performance loss, use Matrix.Multiple() method instead.",
false )]
[MethodImpl( MethodImplOptions.AggressiveInlining )]
public static Matrix43 operator *( Matrix44 left, Matrix43 right )
{
return left.Multiple(in right);
return left.Multiple( in right );
}
[MethodImpl( MethodImplOptions.AggressiveInlining )]
public static Matrix44 operator *( Matrix44 left, Matrix44 right )
{
@@ -308,10 +313,12 @@ namespace StgSharp.Math
}
[MethodImpl( MethodImplOptions.AggressiveInlining )]
public static unsafe Matrix44 operator +(Matrix44 left, Matrix44 right )
public static unsafe Matrix44 operator +(
Matrix44 left,
Matrix44 right )
{
Matrix44 ret = new Matrix44();
InternalIO.Intrinsic.add_mat_4(&left.mat, &right.mat, &ret.mat);
InternalIO.Intrinsic.add_mat_4( &left.mat, &right.mat, &ret.mat );
return ret;
}
+37 -6
View File
@@ -28,6 +28,9 @@
//
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
using StgSharp.HighPerformance;
using System.Net.Http.Headers;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
@@ -35,20 +38,20 @@ using System.Runtime.InteropServices;
namespace StgSharp.Math
{
/// <summary>
/// A two dimension vector defined by two elements. Vec2Ds in StgSharp are default used as colum
/// A two dimension vector defined by two elements. Vec2 in StgSharp are default used as colum
/// vector.
/// </summary>
[StructLayout( LayoutKind.Explicit, Size = 16, Pack = 16 )]
public struct Vec2 : IVector
public struct Vec2 : IVector<Vec2>
{
[FieldOffset( 0 )] internal unsafe fixed float num[ 2 ];
[FieldOffset( 0 )]
internal Vector2 v;
internal M128 reg;
[FieldOffset( 0 )]
internal Vector4 vec;
internal Vector2 v;
[FieldOffset( 0 )]
public float X;
@@ -57,12 +60,19 @@ namespace StgSharp.Math
internal Vec2( Vector2 vec )
{
Unsafe.SkipInit( out reg );
Unsafe.SkipInit( out X );
Unsafe.SkipInit( out Y );
v = vec;
}
internal Vec2( Vector4 vec )
internal Vec2( M128 vec )
{
this.vec = vec;
Unsafe.SkipInit( out reg );
Unsafe.SkipInit( out X );
Unsafe.SkipInit( out Y );
reg = vec;
reg.Write<ulong>( 1, 0 );
}
public Vec2( float x, float y )
@@ -74,6 +84,27 @@ namespace StgSharp.Math
public static Vec2 Zero => new Vec2( 0, 0 );
public static Vec2 One
{
get => new Vec2( 1, 1 );
}
public Vec2 XY
{
get => this;
set => this = value;
}
public Vec3 XYZ
{
get => new Vec3( reg );
set
{
reg = value.reg;
reg.Write<ulong>( 1, 0 );
}
}
[MethodImpl( MethodImplOptions.AggressiveInlining )]
public float Cross( Vec2 right )
{
+26 -21
View File
@@ -38,26 +38,26 @@ using System.Runtime.InteropServices;
namespace StgSharp.Math
{
[StructLayout( LayoutKind.Explicit, Size = 16, Pack = 16 )]
public struct Vec3 : IEquatable<Vec3>, IVector
public struct Vec3 : IEquatable<Vec3>, IVector<Vec3>
{
[FieldOffset( 0 )] internal unsafe fixed float num[ 3 ];
[FieldOffset( 0 )]
internal M128 reg;
[FieldOffset( 0 )] internal M128 reg;
[FieldOffset( 0 )]
internal Vector3 v;
[FieldOffset( 0 )]
internal Vector4 vec;
[FieldOffset( 0 )] internal Vector3 v;
[FieldOffset( 0 )] public float X;
[FieldOffset( 4 )] public float Y;
[FieldOffset( 8 )] public float Z;
internal Vec3( Vector4 vector )
internal Vec3( M128 vector )
{
vec = vector;
Unsafe.SkipInit( out X );
Unsafe.SkipInit( out Y );
Unsafe.SkipInit( out Z );
Unsafe.SkipInit( out v );
reg = vector;
reg.Write<uint>( 3, 0 );
}
internal Vec3( Vector3 vector )
@@ -67,21 +67,26 @@ namespace StgSharp.Math
public Vec3( Vec2 xy, float z )
{
vec = xy.vec;
Unsafe.SkipInit( out X );
Unsafe.SkipInit( out Y );
Unsafe.SkipInit( out Z );
Unsafe.SkipInit( out v );
reg = xy.reg;
Z = z;
}
public Vec3( float x, float y, float z )
{
#if NET5_0_OR_GREATER
Unsafe.SkipInit( out vec );
#endif
Unsafe.SkipInit( out reg );
Unsafe.SkipInit( out X );
Unsafe.SkipInit( out Y );
Unsafe.SkipInit( out Z );
v = new Vector3( x, y, z );
}
public unsafe Vec2 XY
{
get => new Vec2( this.vec );
get => new Vec2( this.reg );
set
{
fixed( Vector3* vptr = &v ) {
@@ -93,6 +98,7 @@ namespace StgSharp.Math
public Vec3 XYZ
{
get { return new Vec3( v ); }
set { this = value; }
}
public static Vec3 Zero
@@ -122,7 +128,7 @@ namespace StgSharp.Math
public bool Equals( Vec3 other )
{
return this.vec == other.vec;
return this.reg == other.reg;
}
public override bool Equals( object obj )
@@ -168,7 +174,7 @@ namespace StgSharp.Math
[MethodImpl( MethodImplOptions.AggressiveInlining )]
public static Vec3 operator -( Vec3 vec )
{
return new Vec3( -vec.vec );
return new Vec3( -vec.v );
}
[MethodImpl( MethodImplOptions.AggressiveInlining )]
@@ -204,7 +210,7 @@ namespace StgSharp.Math
[MethodImpl( MethodImplOptions.AggressiveInlining )]
public static Vec3 operator +( Vec3 left, Vec3 right )
{
return new Vec3( left.vec + right.vec );
return new Vec3( left.v + right.v );
}
[MethodImpl( MethodImplOptions.AggressiveInlining )]
@@ -217,9 +223,8 @@ namespace StgSharp.Math
public static unsafe implicit operator Vec3(
(float, float, float) tuple )
{
Vec3 v = *( Vec3* )&tuple;
v.vec.W = 0.0f;
return v;
Vec3 vec = new Vec3( *( Vector3* )&tuple );
return vec;
}
}
+29 -8
View File
@@ -41,7 +41,7 @@ using System.Runtime.InteropServices;
namespace StgSharp.Math
{
[StructLayout( LayoutKind.Explicit, Size = 16, Pack = 16 )]
public struct Vec4 : IEquatable<Vec4>, IVector
public struct Vec4 : IEquatable<Vec4>, IVector<Vec4>
{
[FieldOffset( 0 )] internal unsafe fixed float num[ 4 ];
@@ -57,12 +57,33 @@ namespace StgSharp.Math
internal Vec4( Vector4 v )
{
Unsafe.SkipInit( out X );
Unsafe.SkipInit( out Y );
Unsafe.SkipInit( out Z );
Unsafe.SkipInit( out W );
Unsafe.SkipInit( out reg );
vec = v;
}
public Vec4( Vec3 v, float w )
internal Vec4( M128 v )
{
vec = v.vec;
Unsafe.SkipInit( out X );
Unsafe.SkipInit( out Y );
Unsafe.SkipInit( out Z );
Unsafe.SkipInit( out W );
Unsafe.SkipInit( out vec );
reg = v;
}
public Vec4( Vec3 v3, float w )
{
Unsafe.SkipInit( out X );
Unsafe.SkipInit( out Y );
Unsafe.SkipInit( out Z );
Unsafe.SkipInit( out W );
reg = v3.reg;
W = w;
}
@@ -73,7 +94,7 @@ namespace StgSharp.Math
public unsafe Vec2 XY
{
get => new Vec2( vec );
get => new Vec2( reg );
set
{
X = value.X;
@@ -83,23 +104,23 @@ namespace StgSharp.Math
public unsafe Vec3 XYZ
{
get => new Vec3( vec );
get => new Vec3( reg );
set
{
float w = W;
vec = value.vec;
reg = value.reg;
W = w;
}
}
public static Vec4 One
{
get => new Vec4( Vector4.One );
get => new Vec4( 1, 1, 1, 1 );
}
public static Vec4 Zero
{
get => new Vec4( Vector4.Zero );
get => new Vec4( 0, 0, 0, 0 );
}
[MethodImpl( MethodImplOptions.AggressiveInlining )]
@@ -51,7 +51,7 @@ namespace StgSharp.Math
public static Vec3 Normalize( Vec3 vec )
{
return new Vec3 { vec = Vector4.Normalize( vec.vec ) };
return new Vec3 { v = Vector3.Normalize( vec.v ) };
}
public static unsafe void Normalize( ref Vec4 source, ref Vec4 target )
@@ -55,8 +55,8 @@ namespace StgSharp.Math
}
public static unsafe void Orthogonalize(
ref Vec4 source,
ref Vec4 target )
ref Vec4 source,
ref Vec4 target )
{
Vector4 s = source.vec;
Vector4 t = target.vec;
@@ -79,8 +79,8 @@ namespace StgSharp.Math
/// <param _name="source"></param>
/// <param _name="target"></param>
public static unsafe void Orthogonalize(
ref Vec3 source,
ref Vec3 target )
ref Vec3 source,
ref Vec3 target )
{
Vector3 s = source.v;
Vector3 t = target.v;
+5 -5
View File
@@ -60,7 +60,7 @@ namespace StgSharp.Math
throw new ArgumentException();
}
float r, s, u, v = 0.0f;
float r, s, u, vec = 0.0f;
bool transform = (y >= 0.5f);
if (transform)
@@ -93,16 +93,16 @@ namespace StgSharp.Math
c = (r - s1 * s1) / (s + s1);
p = 2.0F * s * u - (piby2_tail - 2.0F * c);
q = hpiby2_head - 2.0F * s1;
v = hpiby2_head - (p - q);
vec = hpiby2_head - (p - q);
}
else
{
v = y + y * u;
vec = y + y * u;
}
v = sign * v;
vec = sign * vec;
return piby2 - v;
return piby2 - vec;
*/
}
+4 -4
View File
@@ -68,7 +68,7 @@ namespace StgSharp.Math
throw new ArgumentException();
}
float r, s, u, v = 0.0f;
float r, s, u, vec = 0.0f;
bool transform = (y >= 0.5f);
if (transform)
@@ -101,14 +101,14 @@ namespace StgSharp.Math
c = (r - s1 * s1) / (s + s1);
p = 2.0F * s * u - (piby2_tail - 2.0F * c);
q = hpiby2_head - 2.0F * s1;
v = hpiby2_head - (p - q);
vec = hpiby2_head - (p - q);
}
else
{
v = y + y * u;
vec = y + y * u;
}/*
return sign * v;
return sign * vec;
*/
}
+10 -10
View File
@@ -63,11 +63,11 @@ namespace StgSharp.Math
angle = 0.7853981634f;
}
float v = 0.7853981634f * x + x * (1 - x) * (0.2447f + 0.0663f * x);
v += angle;
v *= sign;
float vec = 0.7853981634f * x + x * (1 - x) * (0.2447f + 0.0663f * x);
vec += angle;
vec *= sign;
return v;
return vec;
}
@@ -105,13 +105,13 @@ namespace StgSharp.Math
angle2 = 0.7853981634f;
}
float v = 0.7853981634f * z + z * (1 - z) * (0.2447f + 0.0663f * z);
v *= 57.29577951f;
v += angle2;
v += angle;
v *= sign;
float vec = 0.7853981634f * z + z * (1 - z) * (0.2447f + 0.0663f * z);
vec *= 57.29577951f;
vec += angle2;
vec += angle;
vec *= sign;
return v;
return vec;
*/
}