Skip to content
Closed
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
110 changes: 102 additions & 8 deletions jme3-core/src/main/java/com/jme3/anim/tween/action/BlendAction.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
/*
* Copyright (c) 2009-2022 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.jme3.anim.tween.action;

import com.jme3.anim.tween.Tween;
import com.jme3.anim.tween.Tweens;
import com.jme3.anim.util.HasLocalTransform;
import com.jme3.math.Transform;

Expand All @@ -13,12 +46,35 @@ public class BlendAction extends BlendableAction {
private int secondActiveIndex;
final private BlendSpace blendSpace;
private float blendWeight;
final private double[] timeFactor;
final private Tween[] scaledActions;
final private Map<HasLocalTransform, Transform> targetMap = new HashMap<>();

/**
* Creates an action that uses the given blend space to blend to blend between
* specified action. It will stretch the actions that doesn't have the same length.
*
* @param blendSpace The blend space used for calculating blend weight
* @param actions The actions to blend
*/
public BlendAction(BlendSpace blendSpace, BlendableAction... actions) {
this(blendSpace, false, actions);
}

/**
* Creates an action that uses the given blend space to blend to blend between
* specified action. It will stretch the actions that doesn't have the same length.
* If smart stretching is enabled it will try to loop actions before stretching.
*
* @param blendSpace The blend space used for calculating blend weight
* @param smartStretch If smart stretch it is false, actions that do not have the same length will
* stretch to the max length, if it is true, they will be looped when possible
* before stretching. Gives the best result when max transition weight is set
* to something lower than 1 (e.g. 0.5).
* @param actions The actions to blend
*/
public BlendAction(BlendSpace blendSpace, boolean smartStretch, BlendableAction... actions) {
super(actions);
timeFactor = new double[actions.length];
this.scaledActions = new Tween[actions.length];
this.blendSpace = blendSpace;
blendSpace.setBlendAction(this);

Expand All @@ -39,12 +95,25 @@ public BlendAction(BlendSpace blendSpace, BlendableAction... actions) {
//Blending effect maybe unexpected when blended animation don't have the same length
//Stretching any action that doesn't have the same length.
for (int i = 0; i < this.actions.length; i++) {
this.timeFactor[i] = 1;
if (this.actions[i].getLength() != getLength()) {
double actionLength = this.actions[i].getLength();
double actionLength = this.actions[i].getLength();
if (actionLength != getLength()) {
if (actionLength > 0 && getLength() > 0) {
this.timeFactor[i] = this.actions[i].getLength() / getLength();
Tween action = this.actions[i];
if (smartStretch) {
// Check if we can loop it before stretching
int count = (int) (Math.round(getLength() / actionLength));
if (count > 1) {
action = new Loop(action, count);
}
}
this.scaledActions[i] = Tweens.stretch(getLength(), action);
} else {
// If action length is 0, don't do anything
this.scaledActions[i] = this.actions[i];
}
} else {
// No need to stretch if action length equals to max length
this.scaledActions[i] = this.actions[i];
}
}
}
Expand All @@ -60,7 +129,7 @@ public void doInterpolate(double t) {
// Only interpolate the first action if the weight is below 1.
if (blendWeight < 1f) {
firstActiveAction.setWeight(1f);
firstActiveAction.interpolate(t * timeFactor[firstActiveIndex]);
scaledActions[firstActiveIndex].interpolate(t);
if (blendWeight == 0) {
for (HasLocalTransform target : targetMap.keySet()) {
collect(target, targetMap.get(target));
Expand All @@ -70,7 +139,7 @@ public void doInterpolate(double t) {

//Second action should be interpolated
secondActiveAction.setWeight(blendWeight);
secondActiveAction.interpolate(t * timeFactor[secondActiveIndex]);
scaledActions[secondActiveIndex].interpolate(t);

firstActiveAction.setCollectTransformDelegate(null);
secondActiveAction.setCollectTransformDelegate(null);
Expand Down Expand Up @@ -127,4 +196,29 @@ private void collect(HasLocalTransform target, Transform tr) {
}
}

private static class Loop implements Tween {

private final Tween delegate;
private final double length;

public Loop(Tween delegate, int count) {
this.delegate = delegate;
this.length = count * delegate.getLength();
}

@Override
public double getLength() {
return length;
}

@Override
public boolean interpolate(double t) {
if (t < 0) {
return true;
}

delegate.interpolate(t % delegate.getLength());
return t < length;
}
}
}