📄 colladaimporter.java
字号:
/*
* Copyright (c) 2003-2009 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.jmex.model.collada;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.jme.animation.Bone;
import com.jme.animation.BoneAnimation;
import com.jme.animation.BoneTransform;
import com.jme.animation.SkinNode;
import com.jme.animation.TextureKeyframeController;
import com.jme.bounding.BoundingBox;
import com.jme.image.Image;
import com.jme.image.Texture;
import com.jme.image.Texture.WrapAxis;
import com.jme.light.DirectionalLight;
import com.jme.light.Light;
import com.jme.light.LightNode;
import com.jme.light.PointLight;
import com.jme.light.SpotLight;
import com.jme.math.FastMath;
import com.jme.math.Matrix3f;
import com.jme.math.Matrix4f;
import com.jme.math.Quaternion;
import com.jme.math.Vector2f;
import com.jme.math.Vector3f;
import com.jme.renderer.Camera;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.scene.CameraNode;
import com.jme.scene.Controller;
import com.jme.scene.Geometry;
import com.jme.scene.Node;
import com.jme.scene.SharedMesh;
import com.jme.scene.SharedNode;
import com.jme.scene.Spatial;
import com.jme.scene.TexCoords;
import com.jme.scene.TriMesh;
import com.jme.scene.state.BlendState;
import com.jme.scene.state.ClipState;
import com.jme.scene.state.ColorMaskState;
import com.jme.scene.state.CullState;
import com.jme.scene.state.FogState;
import com.jme.scene.state.MaterialState;
import com.jme.scene.state.RenderState;
import com.jme.scene.state.ShadeState;
import com.jme.scene.state.StencilState;
import com.jme.scene.state.TextureState;
import com.jme.scene.state.ZBufferState;
import com.jme.system.DisplaySystem;
import com.jme.util.TextureManager;
import com.jme.util.export.binary.BinaryExporter;
import com.jme.util.export.binary.BinaryImporter;
import com.jme.util.geom.BufferUtils;
import com.jme.util.geom.GeometryTool;
import com.jme.util.geom.VertMap;
import com.jme.util.resource.ResourceLocatorTool;
import com.jmex.model.collada.schema.COLLADAType;
import com.jmex.model.collada.schema.IDREF_arrayType;
import com.jmex.model.collada.schema.InstanceWithExtra;
import com.jmex.model.collada.schema.Name_arrayType;
import com.jmex.model.collada.schema.TargetableFloat3;
import com.jmex.model.collada.schema.accessorType;
import com.jmex.model.collada.schema.animationType;
import com.jmex.model.collada.schema.assetType;
import com.jmex.model.collada.schema.bind_materialType;
import com.jmex.model.collada.schema.cameraType;
import com.jmex.model.collada.schema.collada_schema_1_4_1Doc;
import com.jmex.model.collada.schema.colorType;
import com.jmex.model.collada.schema.common_newparam_type;
import com.jmex.model.collada.schema.controllerType;
import com.jmex.model.collada.schema.effectType;
import com.jmex.model.collada.schema.float4x4;
import com.jmex.model.collada.schema.float_arrayType;
import com.jmex.model.collada.schema.fx_sampler2D_common;
import com.jmex.model.collada.schema.fx_surface_common;
import com.jmex.model.collada.schema.geometryType;
import com.jmex.model.collada.schema.imageType;
import com.jmex.model.collada.schema.instance_controllerType;
import com.jmex.model.collada.schema.instance_geometryType;
import com.jmex.model.collada.schema.instance_materialType;
import com.jmex.model.collada.schema.instance_physics_modelType;
import com.jmex.model.collada.schema.lambertType;
import com.jmex.model.collada.schema.library_animationsType;
import com.jmex.model.collada.schema.library_camerasType;
import com.jmex.model.collada.schema.library_controllersType;
import com.jmex.model.collada.schema.library_effectsType;
import com.jmex.model.collada.schema.library_geometriesType;
import com.jmex.model.collada.schema.library_imagesType;
import com.jmex.model.collada.schema.library_lightsType;
import com.jmex.model.collada.schema.library_materialsType;
import com.jmex.model.collada.schema.library_nodesType;
import com.jmex.model.collada.schema.library_physics_modelsType;
import com.jmex.model.collada.schema.library_physics_scenesType;
import com.jmex.model.collada.schema.library_visual_scenesType;
import com.jmex.model.collada.schema.lightType;
import com.jmex.model.collada.schema.materialType;
import com.jmex.model.collada.schema.meshType;
import com.jmex.model.collada.schema.nodeType2;
import com.jmex.model.collada.schema.opticsType;
import com.jmex.model.collada.schema.orthographicType;
import com.jmex.model.collada.schema.paramType3;
import com.jmex.model.collada.schema.passType3;
import com.jmex.model.collada.schema.perspectiveType;
import com.jmex.model.collada.schema.phongType;
import com.jmex.model.collada.schema.physics_modelType;
import com.jmex.model.collada.schema.physics_sceneType;
import com.jmex.model.collada.schema.polygonsType;
import com.jmex.model.collada.schema.rigid_bodyType;
import com.jmex.model.collada.schema.sceneType;
import com.jmex.model.collada.schema.shapeType2;
import com.jmex.model.collada.schema.skinType;
import com.jmex.model.collada.schema.sourceType;
import com.jmex.model.collada.schema.techniqueType2;
import com.jmex.model.collada.schema.techniqueType4;
import com.jmex.model.collada.schema.technique_commonType;
import com.jmex.model.collada.schema.technique_commonType2;
import com.jmex.model.collada.schema.technique_commonType4;
import com.jmex.model.collada.schema.textureType;
import com.jmex.model.collada.schema.trianglesType;
import com.jmex.model.collada.schema.vertex_weightsType;
import com.jmex.model.collada.schema.visual_sceneType;
/**
* <code>ColladaNode</code> provides a mechanism to parse and load a COLLADA
* (COLLAborative Design Activity) model. Making use of a DOM parse, the XML
* formatted COLLADA file is parsed into Java Type classes and then processed by
* jME. This processing is currently aimed at the 1.4.1 release of the COLLADA
* Specification, and will, in most likelyhood, require updating with a new
* release of COLLADA.
*
* @author Mark Powell, Rikard Herlitz, and others
*/
public class ColladaImporter {
private static final Logger logger = Logger.getLogger(ColladaImporter.class
.getName());
// asset information
private String modelAuthor;
private String tool;
private String revision;
private String unitName;
private float unitMeter;
private String upAxis;
private static ColladaImporter instance;
private String name;
private String[] boneIds;
private static boolean squelch;
// If true, models loaded by ColladaImporter will automatically have
// geometry optimization applied. default: true.
public static boolean OPTIMIZE_GEOMETRY = true;
public static OptimizeCallback optimizeCallBack = null;
private Map<String, Object> resourceLibrary;
private ArrayList<String> controllerNames;
private ArrayList<String> uvControllerNames;
private ArrayList<String> skinNodeNames;
private ArrayList<String> cameraNodeNames;
private ArrayList<String> lightNodeNames;
private ArrayList<String> geometryNames;
private ArrayList<String> skeletonNames;
private Map<String, Object> userInformation;
private Map<TriMesh, String> subMaterialLibrary;
private Node model;
/**
* Unique Serial ID for ColladaNode
*/
private static final long serialVersionUID = -4024091270314000507L;
/**
* Default constructor instantiates a ColladaImporter object. A basic Node
* structure is built and no data is loaded until the <code>load</code>
* method is called.
*
* @param name
* the name of the node.
*/
private ColladaImporter(String name) {
this.name = name;
}
public static boolean hasUserInformation(String key) {
if (instance.userInformation == null) {
return false;
} else {
return instance.userInformation.containsKey(key);
}
}
public static void addUserInformation(String key, Object value) {
if (instance.userInformation == null) {
instance.userInformation = new HashMap<String, Object>();
}
instance.userInformation.put(key, value);
}
public static Object getUserInformation(String key) {
if (instance.userInformation == null) {
return null;
} else {
return instance.userInformation.get(key);
}
}
/**
* load takes the model path as a string object and uses the
* COLLADASchemaDoc object to load it. This is then stored as a heirarchy of
* data objects. This heirarchy is passed to the processCollada method to
* build the jME data structures necessary to view the model.
*
* @param source
* the source to import.
* @param textureDirectory
* the location of the textures.
* @param name
* the name of the node.
*/
public static void load(InputStream source, String name) {
if (instance == null) {
instance = new ColladaImporter(name);
}
instance.load(source);
}
/**
* load is called by the static load method, creating an instance of the
* model to be returned.
*
* @param source
* the source to import.
* @param textureDirectory
* the location of the textures.
*/
private void load(InputStream source) {
model = new Node(name);
resourceLibrary = new HashMap<String, Object>();
subMaterialLibrary = new HashMap<TriMesh, String>();
collada_schema_1_4_1Doc doc = new collada_schema_1_4_1Doc();
try {
COLLADAType root = new COLLADAType(doc.load(source));
logger.info("Version: " + root.getversion().getValue());
processCollada(root);
} catch (Exception ex) {
logger.log(Level.WARNING, "Unable to load Collada file. ", ex);
return;
}
}
/**
* returns the names of the controllers that affect this imported model.
*
* @return the list of string values for each controller name.
*/
public static ArrayList<String> getControllerNames() {
if (instance == null) {
return null;
}
return instance.controllerNames;
}
/**
* @return
*/
public static ArrayList<String> getUVControllerNames() {
if (instance == null) {
return null;
}
return instance.uvControllerNames;
}
public static void addUVControllerName(String name) {
if (instance.uvControllerNames == null) {
instance.uvControllerNames = new ArrayList<String>();
}
instance.uvControllerNames.add(name);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -