Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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