Skip to content

Commit

Permalink
solve issue #1806 (global FrameInterpolator violates threading model) (
Browse files Browse the repository at this point in the history
…#1943)

* solve issue #1806 (global FrameInterpolator violates threading model)

* FrameInterpolator:  deprecate the global instance
  • Loading branch information
stephengold authored and Ali-RS committed Feb 12, 2023
1 parent 524c57f commit 387aba8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
12 changes: 9 additions & 3 deletions jme3-core/src/main/java/com/jme3/anim/MorphTrack.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2009-2021 jMonkeyEngine
* Copyright (c) 2009-2023 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -52,7 +52,11 @@ public class MorphTrack implements AnimTrack<float[]> {
* Weights and times for track.
*/
private float[] weights;
private FrameInterpolator interpolator = FrameInterpolator.DEFAULT;
/**
* The interpolator to use, or null to always use the default interpolator
* of the current thread.
*/
private FrameInterpolator interpolator = null;
private float[] times;
private int nbMorphTargets;

Expand Down Expand Up @@ -219,7 +223,9 @@ public void getDataAtTime(double t, float[] store) {
/ (times[endFrame] - times[startFrame]);
}

interpolator.interpolateWeights(blend, startFrame, weights, nbMorphTargets, store);
FrameInterpolator fi = (interpolator == null)
? FrameInterpolator.getThreadDefault() : interpolator;
fi.interpolateWeights(blend, startFrame, weights, nbMorphTargets, store);
}

/**
Expand Down
13 changes: 10 additions & 3 deletions jme3-core/src/main/java/com/jme3/anim/TransformTrack.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2009-2021 jMonkeyEngine
* Copyright (c) 2009-2023 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -50,7 +50,11 @@
public class TransformTrack implements AnimTrack<Transform> {

private double length;
private FrameInterpolator interpolator = FrameInterpolator.DEFAULT;
/**
* The interpolator to use, or null to always use the default interpolator
* of the current thread.
*/
private FrameInterpolator interpolator = null;
private HasLocalTransform target;

/**
Expand Down Expand Up @@ -281,7 +285,10 @@ public void getDataAtTime(double t, Transform transform) {
/ (times[endFrame] - times[startFrame]);
}

Transform interpolated = interpolator.interpolate(blend, startFrame, translations, rotations, scales, times);
FrameInterpolator fi = (interpolator == null)
? FrameInterpolator.getThreadDefault() : interpolator;
Transform interpolated = fi.interpolate(
blend, startFrame, translations, rotations, scales, times);

if (translations != null) {
transform.setTranslation(interpolated.getTranslation());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2009-2021 jMonkeyEngine
* Copyright (c) 2009-2023 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -38,7 +38,19 @@
* Created by nehon on 15/04/17.
*/
public class FrameInterpolator {
/**
* A global default instance of this class, for compatibility with JME v3.5.
* Due to issue #1806, use of this instance is discouraged.
*
* @deprecated use {@link #getThreadDefault()}
*/
@Deprecated
public static final FrameInterpolator DEFAULT = new FrameInterpolator();
/**
* The per-thread default instances of this class.
*/
private static final ThreadLocal<FrameInterpolator> THREAD_DEFAULT
= ThreadLocal.withInitial(() -> new FrameInterpolator());

private AnimInterpolator<Float> timeInterpolator;
private AnimInterpolator<Vector3f> translationInterpolator = AnimInterpolators.LinearVec3f;
Expand All @@ -52,6 +64,16 @@ public class FrameInterpolator {

final private Transform transforms = new Transform();

/**
* Obtain the default interpolator for the current thread.
*
* @return the pre-existing instance (not null)
*/
public static FrameInterpolator getThreadDefault() {
FrameInterpolator result = THREAD_DEFAULT.get();
return result;
}

public Transform interpolate(float t, int currentIndex, CompactVector3Array translations,
CompactQuaternionArray rotations, CompactVector3Array scales, float[] times) {
timesReader.setData(times);
Expand Down

0 comments on commit 387aba8

Please sign in to comment.