-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Fix glTF CUBICSPLINE animation sampling #2818
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
Open
ittaigolde
wants to merge
2
commits into
jMonkeyEngine:master
Choose a base branch
from
ittaigolde:fix-gltf-cubic-spline-animation-sampling
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -61,6 +61,7 @@ | |
| import java.nio.ByteBuffer; | ||
| import java.nio.FloatBuffer; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.Base64; | ||
| import java.util.HashMap; | ||
| import java.util.LinkedList; | ||
|
|
@@ -1135,6 +1136,7 @@ public void readAnimation(int animationIndex) throws IOException { | |
| assertNotNull(dataIndex, "No output accessor Provided for animation sampler"); | ||
|
|
||
| String interpolation = getAsString(sampler, "interpolation"); | ||
| boolean cubicSpline = "CUBICSPLINE".equals(interpolation); | ||
| if (interpolation == null || !interpolation.equals("LINEAR")) { | ||
| // JME anim system only supports Linear interpolation (will be possible with monkanim though) | ||
| // TODO rework this once monkanim is core, | ||
|
|
@@ -1153,18 +1155,30 @@ public void readAnimation(int animationIndex) throws IOException { | |
| if (targetPath.equals("translation")) { | ||
| trackData.timeArrays.add(new TrackData.TimeData(times, TrackData.Type.Translation)); | ||
| Vector3f[] translations = readAccessorData(dataIndex, vector3fArrayPopulator); | ||
| if (cubicSpline) { | ||
| translations = getCubicSplineValues(translations); | ||
| } | ||
| trackData.translations = translations; | ||
| } else if (targetPath.equals("scale")) { | ||
| trackData.timeArrays.add(new TrackData.TimeData(times, TrackData.Type.Scale)); | ||
| Vector3f[] scales = readAccessorData(dataIndex, vector3fArrayPopulator); | ||
| if (cubicSpline) { | ||
| scales = getCubicSplineValues(scales); | ||
| } | ||
| trackData.scales = scales; | ||
| } else if (targetPath.equals("rotation")) { | ||
| trackData.timeArrays.add(new TrackData.TimeData(times, TrackData.Type.Rotation)); | ||
| Quaternion[] rotations = readAccessorData(dataIndex, quaternionArrayPopulator); | ||
| if (cubicSpline) { | ||
| rotations = getCubicSplineValues(rotations); | ||
| } | ||
| trackData.rotations = rotations; | ||
| } else { | ||
| trackData.timeArrays.add(new TrackData.TimeData(times, TrackData.Type.Morph)); | ||
| float[] weights = readAccessorData(dataIndex, floatArrayPopulator); | ||
| if (cubicSpline) { | ||
| weights = getCubicSplineValues(weights, times.length); | ||
| } | ||
| trackData.weights = weights; | ||
| hasMorphTrack = true; | ||
| } | ||
|
|
@@ -1495,6 +1509,35 @@ private MorphTrack toMorphTrack(TrackData data, Spatial spatial) { | |
| return new MorphTrack(g, data.times, data.weights, nbMorph); | ||
| } | ||
|
|
||
| private <T> T[] getCubicSplineValues(T[] data) { | ||
| if (data == null) { | ||
| throw new AssetLoadException("No output data defined for cubic spline animation sampler"); | ||
| } | ||
| if (data.length % 3 != 0) { | ||
| throw new AssetLoadException("Cubic spline animation sampler output does not contain tangent/value triplets"); | ||
| } | ||
| T[] values = Arrays.copyOf(data, data.length / 3); | ||
| for (int i = 0; i < values.length; i++) { | ||
| values[i] = data[i * 3 + 1]; | ||
| } | ||
| return values; | ||
| } | ||
|
|
||
| private float[] getCubicSplineValues(float[] data, int timesLength) { | ||
| if (data == null) { | ||
| throw new AssetLoadException("No output data defined for cubic spline animation sampler"); | ||
| } | ||
| if (timesLength <= 0 || data.length % timesLength != 0 || (data.length / timesLength) % 3 != 0) { | ||
| throw new AssetLoadException("Cubic spline animation sampler output does not match input times"); | ||
| } | ||
| int valuesPerTime = data.length / timesLength / 3; | ||
| float[] values = new float[timesLength * valuesPerTime]; | ||
| for (int i = 0; i < timesLength; i++) { | ||
| System.arraycopy(data, (i * 3 + 1) * valuesPerTime, values, i * valuesPerTime, valuesPerTime); | ||
| } | ||
| return values; | ||
| } | ||
|
Comment on lines
+1526
to
+1539
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To prevent potential private float[] getCubicSplineValues(float[] data, int timesLength) {
if (data == null) {
return null;
}
if (timesLength <= 0) {
return new float[0];
}
int valuesPerTime = data.length / timesLength / 3;
float[] values = new float[timesLength * valuesPerTime];
for (int i = 0; i < timesLength; i++) {
System.arraycopy(data, (i * 3 + 1) * valuesPerTime, values, i * valuesPerTime, valuesPerTime);
}
return values;
} |
||
|
|
||
| public <T> T fetchFromCache(String name, int index, Class<T> type) { | ||
| Object[] data = dataCache.get(name); | ||
| if (data == null) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
jme3-plugins/src/test/resources/gltf/cubicSplineScale.gltf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| { | ||
| "asset": { | ||
| "version": "2.0" | ||
| }, | ||
| "scene": 0, | ||
| "scenes": [ | ||
| { | ||
| "nodes": [ | ||
| 0 | ||
| ] | ||
| } | ||
| ], | ||
| "nodes": [ | ||
| { | ||
| "name": "AnimatedNode" | ||
| } | ||
| ], | ||
| "animations": [ | ||
| { | ||
| "name": "CubicSplineScale", | ||
| "channels": [ | ||
| { | ||
| "sampler": 0, | ||
| "target": { | ||
| "node": 0, | ||
| "path": "scale" | ||
| } | ||
| } | ||
| ], | ||
| "samplers": [ | ||
| { | ||
| "input": 0, | ||
| "interpolation": "CUBICSPLINE", | ||
| "output": 1 | ||
| } | ||
| ] | ||
| } | ||
| ], | ||
| "buffers": [ | ||
| { | ||
| "uri": "data:application/octet-stream;base64,AAAAAAAAgD8AAABAAABAQAAAgEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAPwAAgD8AAAAAAACAPwAAgD8AAAAAAACAPwAAgD8AAAAAAAAAQAAAAEAAAAAAAAAAQAAAAEAAAAAAAAAAQAAAAEAAAAAAAABAQAAAQEAAAAAAAABAQAAAQEAAAAAAAABAQAAAQEAAAAAAAACAQAAAgEAAAAAAAACAQAAAgEAAAAAAAACAQAAAgEA=", | ||
| "byteLength": 200 | ||
| } | ||
| ], | ||
| "bufferViews": [ | ||
| { | ||
| "buffer": 0, | ||
| "byteOffset": 0, | ||
| "byteLength": 20 | ||
| }, | ||
| { | ||
| "buffer": 0, | ||
| "byteOffset": 20, | ||
| "byteLength": 180 | ||
| } | ||
| ], | ||
| "accessors": [ | ||
| { | ||
| "bufferView": 0, | ||
| "componentType": 5126, | ||
| "count": 5, | ||
| "type": "SCALAR", | ||
| "min": [ | ||
| 0 | ||
| ], | ||
| "max": [ | ||
| 4 | ||
| ] | ||
| }, | ||
| { | ||
| "bufferView": 1, | ||
| "componentType": 5126, | ||
| "count": 15, | ||
| "type": "VEC3" | ||
| } | ||
| ] | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To prevent potential
NullPointerExceptions, we should add a defensive null check for thedataarray before accessing its length or copying it.