⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 colladaimporter.java

📁 java 3d game jme 工程开发源代码
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
        if (root.haslibrary_geometries()) {
            try {
                processGeometry(root.getlibrary_geometries());
            } catch (Exception e) {
                if (!squelch) {
                    logger.log(Level.WARNING,
                            "Error processing geometry library information", e);
                }
            }
        }
        // controllers will define the action of another object. For example,
        // there may be a controller with a skin tag, defining how a mesh
        // is skinning a skeleton.
        if (root.haslibrary_controllers()) {
            try {
                processControllerLibrary(root.getlibrary_controllers());
            } catch (Exception e) {
                if (!squelch) {
                    logger.log(Level.WARNING,
                            "Error processing controller library information",
                            e);
                }
            }
        }
        if (root.haslibrary_nodes()) {
            try {
                processNodes(root.getlibrary_nodes());
            } catch (Exception e) {
                if (!squelch) {
                    logger.log(Level.WARNING,
                            "Error processing nodes library information", e);
                }
            }
        }
        // process the visual scene. This scene will define how the geometries
        // are structured in the world.
        if (root.haslibrary_visual_scenes()) {
            try {
                processVisualSceneLibrary(root.getlibrary_visual_scenes());
            } catch (Exception e) {
                if (!squelch) {
                    logger
                            .log(
                                    Level.WARNING,
                                    "Error processing visual scene library information",
                                    e);
                }
            }
        }
        if (root.haslibrary_physics_scenes()) {
            try {
                library_physics_scenesType library = root
                        .getlibrary_physics_scenes();
                for (int i = 0; i < library.getphysics_sceneCount(); i++) {
                    physics_sceneType scene = library.getphysics_sceneAt(i);
                    put(scene.getid().toString(), scene);
                }
            } catch (Exception e) {
                if (!squelch) {
                    logger
                            .log(
                                    Level.WARNING,
                                    "Error processing physics scene library information",
                                    e);
                }
            }
        }
        if (root.haslibrary_physics_models()) {
            try {
                library_physics_modelsType library = root
                        .getlibrary_physics_models();
                for (int i = 0; i < library.getphysics_modelCount(); i++) {
                    physics_modelType model = library.getphysics_modelAt(i);
                    put(model.getid().toString(), model);
                }
            } catch (Exception e) {
                if (!squelch) {
                    logger
                            .log(
                                    Level.WARNING,
                                    "Error processing physics model library information",
                                    e);
                }
            }
        }
        // the scene tag actually takes instances of the visual scene defined
        // above
        // and attaches them to the model that is returned.
        if (root.hasscene()) {
            try {
                processScene(root.getscene());
            } catch (Exception e) {
                if (!squelch) {
                    logger.log(Level.WARNING,
                            "Error processing scene information", e);
                }
            }
        }
        try {
            optimizeGeometry();
        } catch (Exception e) {
            if (!squelch) {
                logger.log(Level.WARNING, "Error optimizing geometry", e);
            }
        }
    }

    /**
     * optimizeGeometry
     */
    private void optimizeGeometry() {
        for (String key : resourceLibrary.keySet()) {
            Object val = resourceLibrary.get(key);

            if (val instanceof Spatial) {
                Spatial spatial = (Spatial) val;
                int options = GeometryTool.MV_SAME_COLORS
                        | GeometryTool.MV_SAME_NORMALS
                        | GeometryTool.MV_SAME_TEXS;
                if (spatial.getParent() instanceof SkinNode) {
                    SkinNode pNode = ((SkinNode) spatial.getParent());
                    pNode.assignSkeletonBoneInfluences();

                    if (spatial instanceof Node) {
                        Node skins = (Node) spatial;
                        for (int i = 0; i < skins.getQuantity(); i++) {
                            TriMesh mesh = (TriMesh) skins.getChild(i);

                            if (OPTIMIZE_GEOMETRY) {
                                VertMap map = GeometryTool.minimizeVerts(mesh,
                                        options);
                                if (optimizeCallBack != null) {
                                    optimizeCallBack.remapInfluences(mesh, map);
                                }

                                int geomIndex = pNode.getSkins().getChildIndex(
                                        mesh);
                                pNode.remapInfluences(map, geomIndex);
                            }
                        }
                    } else if (spatial instanceof TriMesh) {
                        TriMesh mesh = (TriMesh) spatial;
                        if (OPTIMIZE_GEOMETRY) {
                            VertMap map = GeometryTool.minimizeVerts(mesh,
                                    options);
                            if (optimizeCallBack != null) {
                                optimizeCallBack.remapInfluences(mesh, map);
                            }

                            int geomIndex = pNode.getSkins()
                                    .getChildIndex(mesh);
                            pNode.remapInfluences(map, geomIndex);
                        }
                    }

                    if (OPTIMIZE_GEOMETRY) {
                        pNode.regenInfluenceOffsets();
                    }

                    pNode.revertToBind();
                } else if (spatial instanceof TriMesh) {
                    TriMesh mesh = (TriMesh) spatial;
                    if (OPTIMIZE_GEOMETRY) {
                        VertMap map = GeometryTool.minimizeVerts(mesh, options);
                        if (optimizeCallBack != null) {
                            optimizeCallBack.remapInfluences(mesh, map);
                        }
                    }
                }
            }
        }
    }

    /**
     * processLightLibrary
     * 
     * @param libraryLights
     * @throws Exception
     */
    private void processLightLibrary(library_lightsType libraryLights)
            throws Exception {
        if (libraryLights.haslight()) {
            for (int i = 0; i < libraryLights.getlightCount(); i++) {
                processLight(libraryLights.getlightAt(i));
            }
        }
    }

    /**
     * @param light
     * @throws Exception
     */
    private void processLight(lightType light) throws Exception {
        technique_commonType4 common = light.gettechnique_common();
        Light l = null;
        if (common.hasdirectional()) {
            l = new DirectionalLight();
            l.setDiffuse(getLightColor(common.getdirectional().getcolor()));
        } else if (common.haspoint()) {
            l = new PointLight();
            l.setDiffuse(getLightColor(common.getpoint().getcolor()));
            l.setAttenuate(true);
            l.setConstant(Float.parseFloat(common.getpoint()
                    .getconstant_attenuation().getValue().toString()));
            l.setLinear(Float.parseFloat(common.getpoint()
                    .getlinear_attenuation().getValue().toString()));
            l.setQuadratic(Float.parseFloat(common.getpoint()
                    .getquadratic_attenuation().getValue().toString()));
        } else if (common.hasspot()) {
            l = new SpotLight();
            l.setDiffuse(getLightColor(common.getspot().getcolor()));
            l.setAttenuate(true);
            l.setConstant(Float.parseFloat(common.getspot()
                    .getconstant_attenuation().getValue().toString()));
            l.setLinear(Float.parseFloat(common.getspot()
                    .getlinear_attenuation().getValue().toString()));
            l.setQuadratic(Float.parseFloat(common.getspot()
                    .getquadratic_attenuation().getValue().toString()));
            ((SpotLight) l).setAngle(Float.parseFloat(common.getspot()
                    .getfalloff_angle().getValue().toString()));
            ((SpotLight) l).setExponent(Float.parseFloat(common.getspot()
                    .getfalloff_exponent().getValue().toString()));
        }
        if (l != null) {
            l.getSpecular().set(0, 0, 0, 1);
            if (common.hasambient()) {
                l.setAmbient(getLightColor(common.getambient().getcolor()));
            } else {
                l.getAmbient().set(0, 0, 0, 1);
            }
            l.setEnabled(true);
            LightNode lightNode = new LightNode(light.getid().toString());
            lightNode.setLight(l);
            if (lightNodeNames == null) {
                lightNodeNames = new ArrayList<String>();
            }
            lightNodeNames.add(lightNode.getName());
            put(lightNode.getName(), lightNode);
        }
    }

    /**
     * getLightColor
     * 
     * @param color
     * @return c
     */
    private ColorRGBA getLightColor(TargetableFloat3 color) {
        StringTokenizer st = new StringTokenizer(color.getValue().toString());
        return new ColorRGBA(Float.parseFloat(st.nextToken()), Float
                .parseFloat(st.nextToken()), Float.parseFloat(st.nextToken()),
                1);
    }

    /**
     * processScene finalizes the model node to be returned as the COLLADA
     * model. This looks up visual scene instances that were placed in the
     * resource library previously.
     * 
     * @param scene
     *            the scene to process
     * @throws Exception
     *             thrown if there is an error processing the xml.
     */
    public void processScene(sceneType scene) throws Exception {
        if (scene.hasinstance_visual_scene()) {
            for (int i = 0; i < scene.getinstance_visual_sceneCount(); i++) {
                String key = scene.getinstance_visual_sceneAt(i).geturl()
                        .toString().substring(1);
                Node n = (Node) resourceLibrary.get(key);
                if (n != null) {
                    model.attachChild(n);
                }
            }
        }
        if (scene.hasinstance_physics_scene()) {
            for (int i = 0; i < scene.getinstance_physics_sceneCount(); i++) {
                String key = scene.getinstance_physics_sceneAt(i).geturl()
                        .toString().substring(1);
                physics_sceneType physScene = (physics_sceneType) resourceLibrary
                        .get(key);
                if (physScene != null) {
                    processPhysicsScene(physScene);
                }
            }
        }
    }

    private void processPhysicsScene(physics_sceneType physScene)
            throws Exception {
        if (physScene.hasinstance_physics_model()) {
            for (int i = 0; i < physScene.getinstance_physics_modelCount(); i++) {
                instance_physics_modelType instPhysModel = physScene
                        .getinstance_physics_modelAt(i);
                String key = instPhysModel.geturl().toString().substring(1);
                physics_modelType physModel = (physics_modelType) resourceLibrary
                        .get(key);
                if (physModel != null) {
                    processPhysicsModel(physModel);
                }
                if (instPhysModel.hasinstance_rigid_body()) {
                    // get the Spatial that is the collision mesh
                    String rigidBodyKey = instPhysModel
                            .getinstance_rigid_body().getbody().toString();
                    Spatial collisionMesh = (Spatial) resourceLibrary
                            .get(rigidBodyKey);
                    if (collisionMesh != null) {
                        // get the target
                        String targetKey = instPhysModel
                                .getinstance_rigid_body().gettarget()
                                .toString().substring(1);
                        Node n = (Node) resourceLibrary.get(targetKey);
                        if (n != null) {
                            n.setUserData("COLLISION", collisionMesh);
                        }
                    }
                }
            }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -