55using System . Text ;
66using System . Threading . Tasks ;
77using Unity . MeshSync ;
8+ using UnityEditor ;
89using UnityEngine ;
910
1011#if AT_USE_SPLINES
@@ -417,66 +418,90 @@ private void UpdateProperties(SceneData scene) {
417418
418419 private EntityRecord UpdateCurveEntity ( CurvesData data , MeshSyncPlayerConfig config ) {
419420#if AT_USE_SPLINES
420- lock ( splineLock )
421- {
422- TransformData dtrans = data . transform ;
423- EntityRecord rec = UpdateTransformEntity ( dtrans , config ) ;
421+ lock ( splineLock ) {
422+ TransformData dtrans = data . transform ;
423+ EntityRecord rec = UpdateTransformEntity ( dtrans , config ) ;
424+
425+ GameObject go = rec . go ;
426+ Transform trans = go . transform ;
424427
425- GameObject go = rec . go ;
426- Transform trans = go . transform ;
428+ SplineContainer splineContainer = rec . splineContainer ;
427429
428- SplineContainer splineContainer = rec . splineContainer ;
429-
430- if ( splineContainer == null )
431- {
432- splineContainer = rec . splineContainer = Misc . GetOrAddComponent < SplineContainer > ( trans . gameObject ) ;
433- }
430+ if ( splineContainer == null ) {
431+ splineContainer = rec . splineContainer = Misc . GetOrAddComponent < SplineContainer > ( trans . gameObject ) ;
432+ }
434433
435- float3 [ ] cos = null ;
436- float3 [ ] handles_left = null ;
437- float3 [ ] handles_right = null ;
434+ float3 [ ] cos = null ;
435+ float3 [ ] handles_left = null ;
436+ float3 [ ] handles_right = null ;
438437
439- var numSplines = data . numSplines ;
438+ var numSplines = data . numSplines ;
440439
441- Spline . Changed -= SplineChanged ;
440+ Spline . Changed -= SplineChanged ;
442441
443- var newSplines = new List < Spline > ( ) ;
442+ // Try to reuse the same splines if the number of splines and knots has not changed:
443+ bool useNewSplines = splineContainer . Splines . Count != numSplines ;
444+ if ( ! useNewSplines ) {
445+ for ( int index = 0 ; index < numSplines ; index ++ ) {
446+ if ( splineContainer . Splines [ index ] . Count != data . GetNumSplinePoints ( index ) ) {
447+ useNewSplines = true ;
448+ break ;
449+ }
450+ }
451+ }
452+
453+ var newSplines = new List < Spline > ( ) ;
444454
445- for ( int index = 0 ; index < numSplines ; index ++ )
446- {
447- var spline = new Spline ( ) ;
455+ for ( int index = 0 ; index < numSplines ; index ++ ) {
456+ Spline spline ;
457+ if ( useNewSplines ) {
458+ spline = new Spline ( ) ;
448459 newSplines . Add ( spline ) ;
460+ }
461+ else {
462+ spline = splineContainer . Splines [ index ] ;
463+ }
449464
450- spline . Closed = data . IsSplineClosed ( index ) ;
465+ // Need to be in this mode to be consistent with blender:
466+ spline . SetTangentMode ( TangentMode . Broken ) ;
451467
452- var numPoints = data . GetNumSplinePoints ( index ) ;
453- m_tmpFloat3 . Resize ( numPoints ) ;
468+ spline . Closed = data . IsSplineClosed ( index ) ;
454469
455- data . ReadSplineCos ( index , m_tmpFloat3 ) ;
456- m_tmpFloat3 . CopyTo ( ref cos ) ;
470+ var numPoints = data . GetNumSplinePoints ( index ) ;
471+ m_tmpFloat3 . Resize ( numPoints ) ;
457472
458- data . ReadSplineHandlesLeft ( index , m_tmpFloat3 ) ;
459- m_tmpFloat3 . CopyTo ( ref handles_left ) ;
473+ data . ReadSplineCos ( index , m_tmpFloat3 ) ;
474+ m_tmpFloat3 . CopyTo ( ref cos ) ;
460475
461- data . ReadSplineHandlesRight ( index , m_tmpFloat3 ) ;
462- m_tmpFloat3 . CopyTo ( ref handles_right ) ;
476+ data . ReadSplineHandlesLeft ( index , m_tmpFloat3 ) ;
477+ m_tmpFloat3 . CopyTo ( ref handles_left ) ;
463478
464- for ( int pointIndex = 0 ; pointIndex < cos . Length ; pointIndex ++ )
465- {
466- var co = cos [ pointIndex ] ;
479+ data . ReadSplineHandlesRight ( index , m_tmpFloat3 ) ;
480+ m_tmpFloat3 . CopyTo ( ref handles_right ) ;
467481
468- var knot = new BezierKnot ( co , handles_left [ pointIndex ] - co , handles_right [ pointIndex ] - co , Quaternion . identity ) ;
482+ for ( int pointIndex = 0 ; pointIndex < cos . Length ; pointIndex ++ ) {
483+ var co = cos [ pointIndex ] ;
469484
485+ var knot = new BezierKnot ( co , handles_left [ pointIndex ] - co , handles_right [ pointIndex ] - co ,
486+ Quaternion . identity ) ;
487+
488+ if ( useNewSplines ) {
470489 spline . Add ( knot ) ;
471490 }
491+ else {
492+ spline . SetKnot ( pointIndex , knot ) ;
493+ }
472494 }
495+ }
473496
497+ if ( useNewSplines ) {
474498 splineContainer . Splines = newSplines ;
475-
476- Spline . Changed += SplineChanged ;
477-
478- return rec ;
479499 }
500+
501+ Spline . Changed += SplineChanged ;
502+
503+ return rec ;
504+ }
480505#else
481506 // If the curve was exported as a mesh before, delete this now, as it's handled as a curve:
482507 TransformData dtrans = data . transform ;
@@ -491,6 +516,7 @@ private EntityRecord UpdateCurveEntity(CurvesData data, MeshSyncPlayerConfig con
491516#if AT_USE_SPLINES
492517 protected virtual void SplineChanged ( Spline spline , int arg2 , SplineModification arg3 )
493518 {
519+ // Overriden in Server.
494520 }
495521#endif
496522}
0 commit comments