Conflicts: jme3-core/src/main/java/com/jme3/scene/Node.java jme3-core/src/main/java/com/jme3/scene/Spatial.javaexperimental^2^2
parent
280733c1ce
commit
2b35f288c2
@ -0,0 +1,174 @@ |
|||||||
|
/* |
||||||
|
* Copyright (c) 2009-2016 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.scene; |
||||||
|
|
||||||
|
import com.jme3.material.MatParamOverride; |
||||||
|
import com.jme3.math.Matrix4f; |
||||||
|
import com.jme3.renderer.Camera; |
||||||
|
import com.jme3.shader.VarType; |
||||||
|
import static com.jme3.shader.VarType.Texture2D; |
||||||
|
import com.jme3.texture.Texture2D; |
||||||
|
import java.lang.reflect.Field; |
||||||
|
import java.util.HashSet; |
||||||
|
import java.util.Set; |
||||||
|
import static org.junit.Assert.assertEquals; |
||||||
|
|
||||||
|
public class MPOTestUtils { |
||||||
|
|
||||||
|
private static final Camera DUMMY_CAM = new Camera(640, 480); |
||||||
|
|
||||||
|
private static final SceneGraphVisitor VISITOR = new SceneGraphVisitor() { |
||||||
|
@Override |
||||||
|
public void visit(Spatial spatial) { |
||||||
|
validateSubScene(spatial); |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
private static void validateSubScene(Spatial scene) { |
||||||
|
scene.checkCulling(DUMMY_CAM); |
||||||
|
|
||||||
|
Set<MatParamOverride> actualOverrides = new HashSet<MatParamOverride>(); |
||||||
|
for (MatParamOverride override : scene.getWorldOverrides()) { |
||||||
|
actualOverrides.add(override); |
||||||
|
} |
||||||
|
|
||||||
|
Set<MatParamOverride> expectedOverrides = new HashSet<MatParamOverride>(); |
||||||
|
Spatial current = scene; |
||||||
|
while (current != null) { |
||||||
|
for (MatParamOverride override : current.getLocalOverrides()) { |
||||||
|
expectedOverrides.add(override); |
||||||
|
} |
||||||
|
current = current.getParent(); |
||||||
|
} |
||||||
|
|
||||||
|
assertEquals("For " + scene, expectedOverrides, actualOverrides); |
||||||
|
} |
||||||
|
|
||||||
|
public static void validateScene(Spatial scene) { |
||||||
|
scene.updateGeometricState(); |
||||||
|
scene.depthFirstTraversal(VISITOR); |
||||||
|
} |
||||||
|
|
||||||
|
public static MatParamOverride mpoInt(String name, int value) { |
||||||
|
return new MatParamOverride(VarType.Int, name, value); |
||||||
|
} |
||||||
|
|
||||||
|
public static MatParamOverride mpoBool(String name, boolean value) { |
||||||
|
return new MatParamOverride(VarType.Boolean, name, value); |
||||||
|
} |
||||||
|
|
||||||
|
public static MatParamOverride mpoFloat(String name, float value) { |
||||||
|
return new MatParamOverride(VarType.Float, name, value); |
||||||
|
} |
||||||
|
|
||||||
|
public static MatParamOverride mpoMatrix4Array(String name, Matrix4f[] value) { |
||||||
|
return new MatParamOverride(VarType.Matrix4Array, name, value); |
||||||
|
} |
||||||
|
|
||||||
|
public static MatParamOverride mpoTexture2D(String name, Texture2D texture) { |
||||||
|
return new MatParamOverride(VarType.Texture2D, name, texture); |
||||||
|
} |
||||||
|
|
||||||
|
private static int getRefreshFlags(Spatial scene) { |
||||||
|
try { |
||||||
|
Field refreshFlagsField = Spatial.class.getDeclaredField("refreshFlags"); |
||||||
|
refreshFlagsField.setAccessible(true); |
||||||
|
return (Integer) refreshFlagsField.get(scene); |
||||||
|
} catch (NoSuchFieldException ex) { |
||||||
|
throw new AssertionError(ex); |
||||||
|
} catch (SecurityException ex) { |
||||||
|
throw new AssertionError(ex); |
||||||
|
} catch (IllegalArgumentException ex) { |
||||||
|
throw new AssertionError(ex); |
||||||
|
} catch (IllegalAccessException ex) { |
||||||
|
throw new AssertionError(ex); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private static void dumpSceneRF(Spatial scene, String indent, boolean last, int refreshFlagsMask) { |
||||||
|
StringBuilder sb = new StringBuilder(); |
||||||
|
|
||||||
|
sb.append(indent); |
||||||
|
if (last) { |
||||||
|
if (!indent.isEmpty()) { |
||||||
|
sb.append("└─"); |
||||||
|
} else { |
||||||
|
sb.append(" "); |
||||||
|
} |
||||||
|
indent += " "; |
||||||
|
} else { |
||||||
|
sb.append("├─"); |
||||||
|
indent += "│ "; |
||||||
|
} |
||||||
|
sb.append(scene.getName()); |
||||||
|
int rf = getRefreshFlags(scene) & refreshFlagsMask; |
||||||
|
if (rf != 0) { |
||||||
|
sb.append("("); |
||||||
|
if ((rf & 0x1) != 0) { |
||||||
|
sb.append("T"); |
||||||
|
} |
||||||
|
if ((rf & 0x2) != 0) { |
||||||
|
sb.append("B"); |
||||||
|
} |
||||||
|
if ((rf & 0x4) != 0) { |
||||||
|
sb.append("L"); |
||||||
|
} |
||||||
|
if ((rf & 0x8) != 0) { |
||||||
|
sb.append("l"); |
||||||
|
} |
||||||
|
if ((rf & 0x10) != 0) { |
||||||
|
sb.append("O"); |
||||||
|
} |
||||||
|
sb.append(")"); |
||||||
|
} |
||||||
|
|
||||||
|
if (!scene.getLocalOverrides().isEmpty()) { |
||||||
|
sb.append(" [MPO]"); |
||||||
|
} |
||||||
|
|
||||||
|
System.out.println(sb); |
||||||
|
|
||||||
|
if (scene instanceof Node) { |
||||||
|
Node node = (Node) scene; |
||||||
|
int childIndex = 0; |
||||||
|
for (Spatial child : node.getChildren()) { |
||||||
|
boolean childLast = childIndex == node.getQuantity() - 1; |
||||||
|
dumpSceneRF(child, indent, childLast, refreshFlagsMask); |
||||||
|
childIndex++; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public static void dumpSceneRF(Spatial scene, int refreshFlagsMask) { |
||||||
|
dumpSceneRF(scene, "", true, refreshFlagsMask); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,204 @@ |
|||||||
|
/* |
||||||
|
* Copyright (c) 2009-2016 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.scene; |
||||||
|
|
||||||
|
import com.jme3.asset.AssetManager; |
||||||
|
import com.jme3.export.binary.BinaryExporter; |
||||||
|
import com.jme3.material.MatParamOverride; |
||||||
|
import org.junit.Test; |
||||||
|
|
||||||
|
import static com.jme3.scene.MPOTestUtils.*; |
||||||
|
import static org.junit.Assert.*; |
||||||
|
|
||||||
|
import com.jme3.system.TestUtil; |
||||||
|
import java.util.ArrayList; |
||||||
|
|
||||||
|
|
||||||
|
public class SceneMatParamOverrideTest { |
||||||
|
|
||||||
|
|
||||||
|
private static Node createDummyScene() { |
||||||
|
Node scene = new Node("Scene Node"); |
||||||
|
|
||||||
|
Node a = new Node("A"); |
||||||
|
Node b = new Node("B"); |
||||||
|
|
||||||
|
Node c = new Node("C"); |
||||||
|
Node d = new Node("D"); |
||||||
|
|
||||||
|
Node e = new Node("E"); |
||||||
|
Node f = new Node("F"); |
||||||
|
|
||||||
|
Node g = new Node("G"); |
||||||
|
Node h = new Node("H"); |
||||||
|
Node j = new Node("J"); |
||||||
|
|
||||||
|
scene.attachChild(a); |
||||||
|
scene.attachChild(b); |
||||||
|
|
||||||
|
a.attachChild(c); |
||||||
|
a.attachChild(d); |
||||||
|
|
||||||
|
b.attachChild(e); |
||||||
|
b.attachChild(f); |
||||||
|
|
||||||
|
c.attachChild(g); |
||||||
|
c.attachChild(h); |
||||||
|
c.attachChild(j); |
||||||
|
|
||||||
|
return scene; |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testOverrides_AddAfterAttach() { |
||||||
|
Node scene = createDummyScene(); |
||||||
|
scene.updateGeometricState(); |
||||||
|
|
||||||
|
Node root = new Node("Root Node"); |
||||||
|
root.updateGeometricState(); |
||||||
|
|
||||||
|
root.attachChild(scene); |
||||||
|
scene.getChild("A").addMatParamOverride(mpoInt("val", 5)); |
||||||
|
|
||||||
|
validateScene(root); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testOverrides_AddBeforeAttach() { |
||||||
|
Node scene = createDummyScene(); |
||||||
|
scene.getChild("A").addMatParamOverride(mpoInt("val", 5)); |
||||||
|
scene.updateGeometricState(); |
||||||
|
|
||||||
|
Node root = new Node("Root Node"); |
||||||
|
root.updateGeometricState(); |
||||||
|
|
||||||
|
root.attachChild(scene); |
||||||
|
|
||||||
|
validateScene(root); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testOverrides_RemoveBeforeAttach() { |
||||||
|
Node scene = createDummyScene(); |
||||||
|
scene.updateGeometricState(); |
||||||
|
|
||||||
|
Node root = new Node("Root Node"); |
||||||
|
root.updateGeometricState(); |
||||||
|
|
||||||
|
scene.getChild("A").addMatParamOverride(mpoInt("val", 5)); |
||||||
|
validateScene(scene); |
||||||
|
|
||||||
|
scene.getChild("A").clearMatParamOverrides(); |
||||||
|
validateScene(scene); |
||||||
|
|
||||||
|
root.attachChild(scene); |
||||||
|
validateScene(root); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testOverrides_RemoveAfterAttach() { |
||||||
|
Node scene = createDummyScene(); |
||||||
|
scene.updateGeometricState(); |
||||||
|
|
||||||
|
Node root = new Node("Root Node"); |
||||||
|
root.updateGeometricState(); |
||||||
|
|
||||||
|
scene.getChild("A").addMatParamOverride(mpoInt("val", 5)); |
||||||
|
|
||||||
|
root.attachChild(scene); |
||||||
|
validateScene(root); |
||||||
|
|
||||||
|
scene.getChild("A").clearMatParamOverrides(); |
||||||
|
validateScene(root); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testOverrides_IdenticalNames() { |
||||||
|
Node scene = createDummyScene(); |
||||||
|
|
||||||
|
scene.getChild("A").addMatParamOverride(mpoInt("val", 5)); |
||||||
|
scene.getChild("C").addMatParamOverride(mpoInt("val", 7)); |
||||||
|
|
||||||
|
validateScene(scene); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testOverrides_CloningScene_DoesntCloneMPO() { |
||||||
|
Node originalScene = createDummyScene(); |
||||||
|
|
||||||
|
originalScene.getChild("A").addMatParamOverride(mpoInt("int", 5)); |
||||||
|
originalScene.getChild("A").addMatParamOverride(mpoBool("bool", true)); |
||||||
|
originalScene.getChild("A").addMatParamOverride(mpoFloat("float", 3.12f)); |
||||||
|
|
||||||
|
Node clonedScene = originalScene.clone(false); |
||||||
|
|
||||||
|
validateScene(clonedScene); |
||||||
|
validateScene(originalScene); |
||||||
|
|
||||||
|
ArrayList<MatParamOverride> clonedOverrides = clonedScene.getChild("A").getLocalOverrides(); |
||||||
|
ArrayList<MatParamOverride> originalOverrides = originalScene.getChild("A").getLocalOverrides(); |
||||||
|
|
||||||
|
assertNotSame(clonedOverrides, originalOverrides); |
||||||
|
assertEquals(clonedOverrides, originalOverrides); |
||||||
|
|
||||||
|
for (int i = 0; i < clonedOverrides.size(); i++) { |
||||||
|
assertSame(clonedOverrides.get(i), originalOverrides.get(i)); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testOverrides_SaveAndLoad_KeepsMPOs() { |
||||||
|
MatParamOverride override = mpoInt("val", 5); |
||||||
|
Node scene = createDummyScene(); |
||||||
|
scene.getChild("A").addMatParamOverride(override); |
||||||
|
|
||||||
|
AssetManager assetManager = TestUtil.createAssetManager(); |
||||||
|
Node loadedScene = BinaryExporter.saveAndLoad(assetManager, scene); |
||||||
|
|
||||||
|
Node root = new Node("Root Node"); |
||||||
|
root.attachChild(loadedScene); |
||||||
|
validateScene(root); |
||||||
|
validateScene(scene); |
||||||
|
|
||||||
|
assertNotSame(override, loadedScene.getChild("A").getLocalOverrides().get(0)); |
||||||
|
assertEquals(override, loadedScene.getChild("A").getLocalOverrides().get(0)); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testEquals() { |
||||||
|
assertEquals(mpoInt("val", 5), mpoInt("val", 5)); |
||||||
|
assertEquals(mpoBool("val", true), mpoBool("val", true)); |
||||||
|
assertNotEquals(mpoInt("val", 5), mpoInt("val", 6)); |
||||||
|
assertNotEquals(mpoInt("val1", 5), mpoInt("val2", 5)); |
||||||
|
assertNotEquals(mpoBool("val", true), mpoInt("val", 1)); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue