Spline Deformer Fix

This commit is contained in:
Josef 2025-01-10 11:45:38 +01:00
parent 73df307cef
commit acfed9eda6
4 changed files with 41 additions and 23 deletions

View File

@ -446,8 +446,13 @@ namespace Rokojori
node.SetGlobalQuaternion( node.GetGlobalQuaternion() * rotation ); node.SetGlobalQuaternion( node.GetGlobalQuaternion() * rotation );
} }
public static void LookTowards( this Node3D node, Vector3 forward, Vector3 up ) public static void LookTowards( this Node3D node, Vector3 forward, Vector3 up, Vector3? up2 = null )
{ {
if ( forward == up )
{
up = up2 == null ? Vector3.Back : (Vector3)up2;
}
node.LookAt( forward + node.GlobalPosition, up ); node.LookAt( forward + node.GlobalPosition, up );
} }

View File

@ -6,10 +6,6 @@ using System.Collections.Generic;
namespace Rokojori namespace Rokojori
{ {
[Tool] [Tool]
[GlobalClass, Icon("res://addons/rokojori_action_library/Icons/Spline.svg") ] [GlobalClass, Icon("res://addons/rokojori_action_library/Icons/Spline.svg") ]
public partial class Deformer : Node3D public partial class Deformer : Node3D
@ -21,7 +17,7 @@ namespace Rokojori
public class MappingData public class MappingData
{ {
public Vector3 localPosition; public Vector3 localPosition;
public float parameter; public float normalizedSplineParameter;
public float weight; public float weight;
} }
@ -30,6 +26,14 @@ namespace Rokojori
Max_Distance Max_Distance
} }
[Export]
public bool update = false;
[Export]
public bool updateAlways = false;
[ExportGroup( "Input")]
[Export] [Export]
public Spline[] sourceSplines; public Spline[] sourceSplines;
@ -39,6 +43,7 @@ namespace Rokojori
[Export] [Export]
public bool updateSourceMesh; public bool updateSourceMesh;
[ExportGroup( "Output")]
[Export] [Export]
public Spline[] deformerSplines; public Spline[] deformerSplines;
@ -48,6 +53,9 @@ namespace Rokojori
[Export( PropertyHint.Range, "0,1")] [Export( PropertyHint.Range, "0,1")]
public float targetSmoothing = 0f; public float targetSmoothing = 0f;
[ExportGroup( "Spline Settings")]
[Export] [Export]
public float splineMaxDistance = 1000f; public float splineMaxDistance = 1000f;
@ -60,11 +68,7 @@ namespace Rokojori
[Export] [Export]
public int splineMappingDepth = 3; public int splineMappingDepth = 3;
[Export]
public bool update = false;
[Export]
public bool updateAlways = false;
MappingData[] deformerMappings; MappingData[] deformerMappings;
@ -74,14 +78,15 @@ namespace Rokojori
{ {
var curve = s.GetCurve(); var curve = s.GetCurve();
var closestParameter = curve.GetClosestParameterTo( worldPosition, splineMappingResolution, splineMappingDepth ); var closestParameter = curve.GetClosestParameterTo( worldPosition, splineMappingResolution, splineMappingDepth );
var pose = curve.GetPoseByPointIndex( closestParameter ); var pointIndex = curve.NormalizedToPointIndex( closestParameter );
var pose = curve.GetPoseByPointIndex( pointIndex );
var localPosition = pose.ApplyInverse( worldPosition ); var localPosition = pose.ApplyInverse( worldPosition );
var mappingData = new MappingData(); var mappingData = new MappingData();
mappingData.localPosition = localPosition; mappingData.localPosition = localPosition;
mappingData.parameter = closestParameter; mappingData.normalizedSplineParameter = closestParameter;
mappingData.weight = 0; mappingData.weight = 0;
return mappingData; return mappingData;
@ -109,7 +114,7 @@ namespace Rokojori
var vertex = meshGeometry.vertices[ i ]; var vertex = meshGeometry.vertices[ i ];
var mapping = CreateSourceMapping( sourceSplines[ j ], vertex ); var mapping = CreateSourceMapping( sourceSplines[ j ], vertex );
var curve = sourceSplines[ j ].GetCurve(); var curve = sourceSplines[ j ].GetCurve();
var distance = curve.PositionAt( mapping.parameter ) - vertex; var distance = curve.PositionAt( mapping.normalizedSplineParameter ) - vertex;
var inverseWeight = MathX.NormalizeClamped( distance.Length(), splineMinDistance, splineMaxDistance ); var inverseWeight = MathX.NormalizeClamped( distance.Length(), splineMinDistance, splineMaxDistance );
mapping.weight = 1f - inverseWeight; mapping.weight = 1f - inverseWeight;
weights += mapping.weight; weights += mapping.weight;
@ -144,14 +149,15 @@ namespace Rokojori
if ( targetSmoothing > 0 ) if ( targetSmoothing > 0 )
{ {
var pose = curve.SmoothedPoseAt( mapping.parameter, targetSmoothing * 0.5f, 2, targetSmoothing ); var pose = curve.SmoothedPoseAt( mapping.normalizedSplineParameter, targetSmoothing * 0.5f, 2, targetSmoothing );
pose.ApplyTwist( curve.TwistAt( mapping.normalizedSplineParameter ) );
vertexPosition += pose.Apply( mapping.localPosition ) * mapping.weight; vertexPosition += pose.Apply( mapping.localPosition ) * mapping.weight;
} }
else else
{ {
var pose = curve.PoseAt( mapping.parameter ); var pose = curve.PoseAt( mapping.normalizedSplineParameter );
pose.ApplyTwist( curve.TwistAt( mapping.parameter ) ); pose.ApplyTwist( curve.TwistAt( mapping.normalizedSplineParameter ) );
vertexPosition += pose.Apply( mapping.localPosition ) * mapping.weight; vertexPosition += pose.Apply( mapping.localPosition ) * mapping.weight;
} }

View File

@ -37,7 +37,6 @@ namespace Rokojori
[Export] [Export]
public Vector3 autoOrientationUp = Vector3.Up; public Vector3 autoOrientationUp = Vector3.Up;
[Export] [Export]
public bool updateAlways = false; public bool updateAlways = false;
@ -238,25 +237,33 @@ namespace Rokojori
return; return;
} }
var up = autoOrientationUp.Normalized();
if ( SplineAutoOrientationMode.Next_Neighbor == autoOrientationMode ) if ( SplineAutoOrientationMode.Next_Neighbor == autoOrientationMode )
{ {
for ( int i = 0; i < list.Count; i++ ) for ( int i = 0; i < list.Count; i++ )
{ {
var sp = list[ i ]; var point = list[ i ];
if ( i == ( list.Count - 1 ) ) if ( i == ( list.Count - 1 ) )
{ {
if ( closed ) if ( closed )
{ {
var first = list[ 0 ]; var first = list[ 0 ];
sp.LookAt( first.GlobalPosition, autoOrientationUp ); var firstDirection = ( first.GlobalPosition - point.GlobalPosition ).Normalized();
point.LookTowards( firstDirection, up);
} }
continue; continue;
} }
var next = list[ i + 1 ]; var next = list[ i + 1 ];
sp.LookAt( next.GlobalPosition, autoOrientationUp );
var direction = ( next.GlobalPosition - point.GlobalPosition ).Normalized();
point.LookTowards( direction, up );
} }
} }
else if ( SplineAutoOrientationMode.Tangent == autoOrientationMode ) else if ( SplineAutoOrientationMode.Tangent == autoOrientationMode )
@ -264,7 +271,7 @@ namespace Rokojori
for ( int i = 0; i < list.Count; i++ ) for ( int i = 0; i < list.Count; i++ )
{ {
var sp = list[ i ]; var point = list[ i ];
var tangentForward = Vector3.Zero; var tangentForward = Vector3.Zero;
@ -282,7 +289,7 @@ namespace Rokojori
continue; continue;
} }
sp.LookAt( sp.GlobalPosition + tangentForward, autoOrientationUp ); point.LookTowards( tangentForward, up );
} }
} }