commit
59c85d58c8
@ -0,0 +1,128 @@ |
|||||||
|
/* |
||||||
|
* Copyright (c) 2017 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.asset.DesktopAssetManager; |
||||||
|
import com.jme3.asset.plugins.ClasspathLocator; |
||||||
|
import com.jme3.collision.CollisionResult; |
||||||
|
import com.jme3.collision.CollisionResults; |
||||||
|
import com.jme3.material.Material; |
||||||
|
import com.jme3.material.plugins.J3MLoader; |
||||||
|
import com.jme3.math.Ray; |
||||||
|
import com.jme3.math.Vector3f; |
||||||
|
import com.jme3.scene.shape.Quad; |
||||||
|
import org.junit.Test; |
||||||
|
|
||||||
|
/** |
||||||
|
* Verify that collideWith() doesn't reports collisions with phantom triangles. |
||||||
|
* This was issue #710 at GitHub. |
||||||
|
* |
||||||
|
* @author Stephen Gold |
||||||
|
*/ |
||||||
|
public class PhantomTrianglesTest { |
||||||
|
|
||||||
|
AssetManager assetManager; |
||||||
|
|
||||||
|
/** |
||||||
|
* ray in the -Z direction, starting from (0.1, 0.2, 10) |
||||||
|
*/ |
||||||
|
final private Ray ray = new Ray(/* origin */new Vector3f(0.1f, 0.2f, 10f), |
||||||
|
/* direction */ new Vector3f(0f, 0f, -1f)); |
||||||
|
Node rootNode; |
||||||
|
|
||||||
|
/** |
||||||
|
* Cast a ray at the geometries and report all collisions. |
||||||
|
*/ |
||||||
|
void castRay() { |
||||||
|
CollisionResults results = new CollisionResults(); |
||||||
|
rootNode.collideWith(ray, results); |
||||||
|
int numResults = results.size(); |
||||||
|
for (int resultI = 0; resultI < numResults; resultI++) { |
||||||
|
CollisionResult result = results.getCollision(resultI); |
||||||
|
Geometry geometry = result.getGeometry(); |
||||||
|
String name = geometry.getName(); |
||||||
|
if (name.equals("white lines")) { |
||||||
|
assert false; // phantom triangle
|
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Attach a red square in the XY plane with its lower left corner at (0, 0, |
||||||
|
* 0). It is composed of 2 triangles. |
||||||
|
*/ |
||||||
|
void createRedSquare() { |
||||||
|
Mesh quadMesh = new Quad(1f, 1f); |
||||||
|
Geometry redSquare = new Geometry("red square", quadMesh); |
||||||
|
Material red = assetManager.loadMaterial("Common/Materials/RedColor.j3m"); |
||||||
|
redSquare.setMaterial(red); |
||||||
|
rootNode.attachChild(redSquare); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Attach a pair of parallel white lines in the z=1 plane. |
||||||
|
*/ |
||||||
|
void createWhiteLines() { |
||||||
|
Mesh lineMesh = new Mesh(); |
||||||
|
lineMesh.setMode(Mesh.Mode.Lines); |
||||||
|
float[] corners = new float[]{ |
||||||
|
-1f, -1f, 0f, |
||||||
|
-1f, 1f, 0f, |
||||||
|
1f, 1f, 0f, |
||||||
|
1f, -1f, 0f |
||||||
|
}; |
||||||
|
lineMesh.setBuffer(VertexBuffer.Type.Position, 3, corners); |
||||||
|
short[] indices = new short[]{0, 1, 2, 3}; |
||||||
|
lineMesh.setBuffer(VertexBuffer.Type.Index, 2, indices); |
||||||
|
lineMesh.updateBound(); |
||||||
|
Geometry whiteLines = new Geometry("white lines", lineMesh); |
||||||
|
Material white = assetManager.loadMaterial("Common/Materials/WhiteColor.j3m"); |
||||||
|
whiteLines.setMaterial(white); |
||||||
|
whiteLines.move(0f, 0f, 1f); |
||||||
|
rootNode.attachChild(whiteLines); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testPhantomTriangles() { |
||||||
|
assetManager = new DesktopAssetManager(); |
||||||
|
assetManager.registerLocator(null, ClasspathLocator.class); |
||||||
|
assetManager.registerLoader(J3MLoader.class, "j3m", "j3md"); |
||||||
|
rootNode = new Node(); |
||||||
|
|
||||||
|
createRedSquare(); |
||||||
|
createWhiteLines(); |
||||||
|
rootNode.updateLogicalState(0.01f); |
||||||
|
rootNode.updateGeometricState(); |
||||||
|
castRay(); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,147 @@ |
|||||||
|
/* |
||||||
|
* Copyright (c) 2017 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 jme3test.texture; |
||||||
|
|
||||||
|
import com.jme3.app.SimpleApplication; |
||||||
|
import com.jme3.input.KeyInput; |
||||||
|
import com.jme3.input.controls.ActionListener; |
||||||
|
import com.jme3.input.controls.KeyTrigger; |
||||||
|
import com.jme3.material.Material; |
||||||
|
import com.jme3.math.FastMath; |
||||||
|
import com.jme3.math.Quaternion; |
||||||
|
import com.jme3.math.Vector3f; |
||||||
|
import com.jme3.scene.Geometry; |
||||||
|
import com.jme3.scene.Mesh; |
||||||
|
import com.jme3.scene.Spatial; |
||||||
|
import com.jme3.scene.shape.Box; |
||||||
|
import com.jme3.util.SkyFactory; |
||||||
|
|
||||||
|
/** |
||||||
|
* Simple application to test sky rotation with a cube-mapped sky. |
||||||
|
* |
||||||
|
* Press "T" to rotate the sky and floor to the camera's left. Press "Y" to |
||||||
|
* rotate the sky and floor to the camera's right. Both should appear to move by |
||||||
|
* the same amount in the same direction. |
||||||
|
* |
||||||
|
* See issue #651 for further information. |
||||||
|
* |
||||||
|
* @author Stephen Gold |
||||||
|
*/ |
||||||
|
public class TestSkyRotation extends SimpleApplication implements ActionListener { |
||||||
|
|
||||||
|
/** |
||||||
|
* objects visible in the scene |
||||||
|
*/ |
||||||
|
private Spatial floor, sky; |
||||||
|
/** |
||||||
|
* Y-axis rotation angle in radians |
||||||
|
*/ |
||||||
|
private float angle = 0f; |
||||||
|
|
||||||
|
public static void main(String[] arguments) { |
||||||
|
TestSkyRotation application = new TestSkyRotation(); |
||||||
|
application.start(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void simpleInitApp() { |
||||||
|
/* |
||||||
|
* Configure the camera. |
||||||
|
*/ |
||||||
|
flyCam.setEnabled(false); |
||||||
|
Vector3f location = new Vector3f(-7f, 4f, 8f); |
||||||
|
cam.setLocation(location); |
||||||
|
Quaternion orientation; |
||||||
|
orientation = new Quaternion(0.0037f, 0.944684f, -0.01067f, 0.327789f); |
||||||
|
assert FastMath.approximateEquals(orientation.norm(), 1f); |
||||||
|
cam.setRotation(orientation); |
||||||
|
/* |
||||||
|
* Attach a cube-mapped sky to the scene graph. |
||||||
|
*/ |
||||||
|
sky = SkyFactory.createSky(assetManager, |
||||||
|
"Scenes/Beach/FullskiesSunset0068.dds", |
||||||
|
SkyFactory.EnvMapType.CubeMap); |
||||||
|
rootNode.attachChild(sky); |
||||||
|
/* |
||||||
|
* Attach a "floor" geometry to the scene graph. |
||||||
|
*/ |
||||||
|
Mesh floorMesh = new Box(10f, 0.1f, 10f); |
||||||
|
floor = new Geometry("floor", floorMesh); |
||||||
|
Material floorMaterial = new Material(assetManager, |
||||||
|
"Common/MatDefs/Misc/Unshaded.j3md"); |
||||||
|
floorMaterial.setTexture("ColorMap", |
||||||
|
assetManager.loadTexture("Interface/Logo/Monkey.jpg")); |
||||||
|
floor.setMaterial(floorMaterial); |
||||||
|
rootNode.attachChild(floor); |
||||||
|
/* |
||||||
|
* Configure mappings and listeners for keyboard input. |
||||||
|
*/ |
||||||
|
inputManager.addMapping("left", new KeyTrigger(KeyInput.KEY_T)); |
||||||
|
inputManager.addMapping("right", new KeyTrigger(KeyInput.KEY_Y)); |
||||||
|
inputManager.addListener(this, "left"); |
||||||
|
inputManager.addListener(this, "right"); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Handle an input action from the user. |
||||||
|
* |
||||||
|
* @param name the name of the action |
||||||
|
* @param ongoing true→depress key, false→release key |
||||||
|
* @param ignored |
||||||
|
*/ |
||||||
|
@Override |
||||||
|
public void onAction(String name, boolean ongoing, float ignored) { |
||||||
|
if (!ongoing) { |
||||||
|
return; |
||||||
|
} |
||||||
|
/* |
||||||
|
* Update the Y-axis rotation angle based on which key was pressed. |
||||||
|
*/ |
||||||
|
if (name.equals("left")) { |
||||||
|
angle += 0.1f; // radians
|
||||||
|
System.out.print("rotate floor and sky leftward ..."); |
||||||
|
} else if (name.equals("right")) { |
||||||
|
angle -= 0.1f; // radians
|
||||||
|
System.out.printf("rotate floor and sky spatials rightward ..."); |
||||||
|
} else { |
||||||
|
return; |
||||||
|
} |
||||||
|
/* |
||||||
|
* Update the local rotations of both objects based on the angle. |
||||||
|
*/ |
||||||
|
System.out.printf(" to %.1f radians left of start%n", angle); |
||||||
|
Quaternion rotation = new Quaternion(); |
||||||
|
rotation.fromAngleNormalAxis(angle, Vector3f.UNIT_Y); |
||||||
|
floor.setLocalRotation(rotation); |
||||||
|
sky.setLocalRotation(rotation); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,58 @@ |
|||||||
|
package com.jme3.scene.plugins.gltf; |
||||||
|
|
||||||
|
import com.jme3.asset.AssetInfo; |
||||||
|
import com.jme3.util.LittleEndien; |
||||||
|
|
||||||
|
import java.io.*; |
||||||
|
import java.util.ArrayList; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by Nehon on 12/09/2017. |
||||||
|
*/ |
||||||
|
public class GlbLoader extends GltfLoader { |
||||||
|
|
||||||
|
private static final int GLTF_MAGIC = 0x46546C67; |
||||||
|
private static final int JSON_TYPE = 0x4E4F534A; |
||||||
|
private static final int BIN_TYPE = 0x004E4942; |
||||||
|
private ArrayList<byte[]> data = new ArrayList<>(); |
||||||
|
|
||||||
|
@Override |
||||||
|
public Object load(AssetInfo assetInfo) throws IOException { |
||||||
|
LittleEndien stream = new LittleEndien(new DataInputStream(assetInfo.openStream())); |
||||||
|
int magic = stream.readInt(); |
||||||
|
int version = stream.readInt(); |
||||||
|
int length = stream.readInt(); |
||||||
|
System.err.println(magic == GLTF_MAGIC ? "gltf" : "no no no"); |
||||||
|
System.err.println(version); |
||||||
|
System.err.println(length); |
||||||
|
|
||||||
|
byte[] json = null; |
||||||
|
|
||||||
|
//length is the total size, we have to remove the header size (3 integers = 12 bytes).
|
||||||
|
length -= 12; |
||||||
|
|
||||||
|
while (length > 0) { |
||||||
|
int chunkLength = stream.readInt(); |
||||||
|
int chunkType = stream.readInt(); |
||||||
|
if (chunkType == JSON_TYPE) { |
||||||
|
json = new byte[chunkLength]; |
||||||
|
stream.read(json); |
||||||
|
System.err.println(new String(json)); |
||||||
|
} else { |
||||||
|
byte[] bin = new byte[chunkLength]; |
||||||
|
stream.read(bin); |
||||||
|
data.add(bin); |
||||||
|
} |
||||||
|
//8 is the byte size of the 2 ints chunkLength and chunkType.
|
||||||
|
length -= chunkLength + 8; |
||||||
|
} |
||||||
|
|
||||||
|
return loadFromStream(assetInfo, new ByteArrayInputStream(json)); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected byte[] getBytes(int bufferIndex, String uri, Integer bufferLength) throws IOException { |
||||||
|
return data.get(bufferIndex); |
||||||
|
} |
||||||
|
|
||||||
|
} |
Loading…
Reference in new issue