Search in sources :

Example 1 with ColladaImporter

use of com.ardor3d.extension.model.collada.jdom.ColladaImporter in project energy3d by concord-consortium.

the class SceneManager method createCompass.

private Node createCompass() throws IOException {
    final ResourceSource source = ResourceLocatorTool.locateResource(ResourceLocatorTool.TYPE_MODEL, "compass.dae");
    final ColladaImporter colladaImporter = new ColladaImporter();
    // Load the collada scene
    Logger.getLogger(ColladaAnimUtils.class.getName()).setLevel(Level.SEVERE);
    Logger.getLogger(ColladaMaterialUtils.class.getName()).setLevel(Level.SEVERE);
    final ColladaStorage storage = colladaImporter.load(source);
    final Node compass = storage.getScene();
    BMText txt;
    final double Z = 0.1;
    txt = new BMText("N", "N", FontManager.getInstance().getAnnotationFont(), Align.South);
    txt.setTextColor(ColorRGBA.BLACK);
    txt.setAutoRotate(false);
    txt.setTranslation(2, 0.0, Z);
    txt.setRotation(new Matrix3().fromAngles(0.0, MathUtils.HALF_PI, -MathUtils.HALF_PI));
    compass.attachChild(txt);
    txt = new BMText("S", "S", FontManager.getInstance().getAnnotationFont(), Align.South);
    txt.setTextColor(ColorRGBA.BLACK);
    txt.setAutoRotate(false);
    txt.setTranslation(-2, -0.0, Z);
    txt.setRotation(new Matrix3().fromAngles(0.0, -MathUtils.HALF_PI, MathUtils.HALF_PI));
    compass.attachChild(txt);
    txt = new BMText("W", "W", FontManager.getInstance().getAnnotationFont(), Align.South);
    txt.setAutoRotate(false);
    txt.setTranslation(-0.0, 2, Z);
    txt.setRotation(new Matrix3().fromAngles(-MathUtils.HALF_PI, 0.0, 0.0));
    compass.attachChild(txt);
    txt = new BMText("E", "E", FontManager.getInstance().getAnnotationFont(), Align.South);
    txt.setAutoRotate(false);
    txt.setTranslation(-0.0, -2, Z);
    txt.setRotation(new Matrix3().fromAngles(MathUtils.HALF_PI, MathUtils.PI, 0.0));
    compass.attachChild(txt);
    final DirectionalLight light = new DirectionalLight();
    light.setDirection(new Vector3(0, 0, -1));
    light.setEnabled(true);
    final LightState lightState = new LightState();
    lightState.attach(light);
    compass.setRenderState(lightState);
    compass.getSceneHints().setLightCombineMode(LightCombineMode.Replace);
    compass.updateWorldRenderStates(true);
    final Node compassNode = new Node();
    compassNode.setRotation(new Matrix3().fromAngles(-MathUtils.HALF_PI, 0.0, 0.0));
    compassNode.attachChild(compass);
    System.out.println("done");
    compass.addController(new SpatialController<Spatial>() {

        @Override
        public void update(final double time, final Spatial caller) {
            final Vector3 direction = getCamera().getDirection().normalize(null);
            direction.setZ(0);
            direction.normalizeLocal();
            double angle = -direction.smallestAngleBetween(Vector3.UNIT_Y);
            if (direction.dot(Vector3.UNIT_X) > 0) {
                angle = -angle;
            }
            angle -= MathUtils.HALF_PI;
            compass.setRotation(new Matrix3().fromAngles(0.0, 0.0, angle - 0.3));
        }
    });
    return compassNode;
}
Also used : LightState(com.ardor3d.renderer.state.LightState) ColladaImporter(com.ardor3d.extension.model.collada.jdom.ColladaImporter) Node(com.ardor3d.scenegraph.Node) CameraNode(com.ardor3d.scenegraph.extension.CameraNode) ReadOnlyVector3(com.ardor3d.math.type.ReadOnlyVector3) Vector3(com.ardor3d.math.Vector3) Matrix3(com.ardor3d.math.Matrix3) ResourceSource(com.ardor3d.util.resource.ResourceSource) Spatial(com.ardor3d.scenegraph.Spatial) DirectionalLight(com.ardor3d.light.DirectionalLight) ColladaStorage(com.ardor3d.extension.model.collada.jdom.data.ColladaStorage) BMText(com.ardor3d.ui.text.BMText)

Example 2 with ColladaImporter

use of com.ardor3d.extension.model.collada.jdom.ColladaImporter in project energy3d by concord-consortium.

the class SceneManager method createEarth.

private Node createEarth() throws IOException {
    final ResourceSource source = ResourceLocatorTool.locateResource(ResourceLocatorTool.TYPE_MODEL, "earth.dae");
    final ColladaImporter colladaImporter = new ColladaImporter();
    final ColladaStorage storage = colladaImporter.load(source);
    final Node earth = storage.getScene();
    final DirectionalLight light = new DirectionalLight();
    light.setDirection(new Vector3(0, 0, -1));
    light.setEnabled(true);
    final LightState lightState = new LightState();
    lightState.attach(light);
    earth.setRenderState(lightState);
    earth.getSceneHints().setLightCombineMode(LightCombineMode.Replace);
    earth.updateWorldRenderStates(true);
    final Node node = new Node();
    node.setRotation(new Matrix3().fromAngles(-MathUtils.HALF_PI, 0.0, 0.0));
    node.attachChild(earth);
    earth.addController(new SpatialController<Spatial>() {

        @Override
        public void update(final double time, final Spatial caller) {
            final Vector3 direction = getCamera().getDirection().normalize(null);
            direction.setZ(0);
            direction.normalizeLocal();
            double angle = -direction.smallestAngleBetween(Vector3.UNIT_Y);
            if (direction.dot(Vector3.UNIT_X) > 0) {
                angle = -angle;
            }
            angle -= MathUtils.HALF_PI;
            earth.setRotation(new Matrix3().fromAngles(0, 0, angle));
        }
    });
    return node;
}
Also used : LightState(com.ardor3d.renderer.state.LightState) ResourceSource(com.ardor3d.util.resource.ResourceSource) Spatial(com.ardor3d.scenegraph.Spatial) ColladaImporter(com.ardor3d.extension.model.collada.jdom.ColladaImporter) Node(com.ardor3d.scenegraph.Node) CameraNode(com.ardor3d.scenegraph.extension.CameraNode) DirectionalLight(com.ardor3d.light.DirectionalLight) ReadOnlyVector3(com.ardor3d.math.type.ReadOnlyVector3) Vector3(com.ardor3d.math.Vector3) ColladaStorage(com.ardor3d.extension.model.collada.jdom.data.ColladaStorage) Matrix3(com.ardor3d.math.Matrix3)

Example 3 with ColladaImporter

use of com.ardor3d.extension.model.collada.jdom.ColladaImporter in project energy3d by concord-consortium.

the class Foundation method importCollada.

public Node importCollada(final URL file, final Vector3 position) throws Exception {
    if (importedNodes == null) {
        importedNodes = new ArrayList<Node>();
    }
    if (importedNodeStates == null) {
        importedNodeStates = new ArrayList<NodeState>();
    }
    if (position != null) {
        // when position is null, the cursor is already set to be wait by the loading method
        SceneManager.getInstance().cursorWait(true);
    }
    File sourceFile = new File(file.toURI());
    if (!sourceFile.exists() && Scene.getURL() != null) {
        sourceFile = new File(new File(Scene.getURL().toURI()).getParentFile(), Util.getFileName(file.getPath()).replaceAll("%20", " "));
    }
    if (sourceFile.exists()) {
        final double az = Math.toRadians(getAzimuth());
        final boolean zeroAz = Util.isZero(az);
        // 0.633 is determined by fitting the length in Energy3D to the length in SketchUp
        final double scale = Scene.getInstance().getAnnotationScale() * 0.633;
        final ColladaStorage storage = new ColladaImporter().load(new URLResourceSource(sourceFile.toURI().toURL()));
        final Node originalNode = storage.getScene();
        originalNode.setScale(scale);
        if (position != null) {
            // when position is null, the node uses the position saved in the associated NodeState object
            final NodeState ns = new NodeState();
            ns.setSourceURL(file);
            ns.setAbsolutePosition(position.clone());
            importedNodeStates.add(ns);
            originalNode.setTranslation(position);
            final Vector3 relativePosition = position.subtract(getAbsCenter().multiplyLocal(1, 1, 0), null).addLocal(0, 0, height);
            // why not -getAzimuth()?
            ns.setRelativePosition(zeroAz ? relativePosition : new Matrix3().fromAngles(0, 0, az).applyPost(relativePosition, null));
        }
        // now construct a new node that is a parent of all planar meshes
        final Node newNode = new Node(originalNode.getName());
        final String nodeString = "Node #" + importedNodes.size() + ", Foundation #" + id;
        final List<Mesh> meshes = new ArrayList<Mesh>();
        Util.getMeshes(originalNode, meshes);
        String warnInfo = null;
        final int nodeIndex = importedNodes.size();
        int meshIndex = 0;
        for (final Mesh m : meshes) {
            final ReadOnlyTransform t = m.getWorldTransform();
            final MeshData md = m.getMeshData();
            switch(md.getIndexMode(0)) {
                case Triangles:
                    final List<Mesh> children = TriangleMeshLib.getPlanarMeshes(m);
                    if (!children.isEmpty()) {
                        for (final Mesh s : children) {
                            s.setTransform(t);
                            final UserData ud = new UserData(this, nodeIndex, meshIndex);
                            ud.setNormal((Vector3) s.getUserData());
                            ud.setRenderState(s.getLocalRenderState(StateType.Texture));
                            ud.setTextureBuffer(s.getMeshData().getTextureBuffer(0));
                            s.setUserData(ud);
                            s.setName("Mesh #" + meshIndex + ", " + nodeString);
                            newNode.attachChild(s);
                            meshIndex++;
                        }
                    }
                    break;
                case Lines:
                    break;
                default:
                    warnInfo = md.getIndexMode(0).name();
                    break;
            }
        }
        if (warnInfo != null) {
            JOptionPane.showMessageDialog(MainFrame.getInstance(), "Non-triangular mesh " + warnInfo + " is found.", "Warning", JOptionPane.WARNING_MESSAGE);
        }
        if (newNode.getNumberOfChildren() > 0) {
            importedNodes.add(newNode);
            newNode.setScale(scale);
            newNode.updateWorldTransform(true);
            root.attachChild(newNode);
            createMeshThickness(newNode);
            if (!zeroAz) {
                setRotatedNormalsForImportedMeshes();
            }
            return newNode;
        }
        if (position != null) {
            SceneManager.getInstance().cursorWait(false);
        }
    } else {
        if (position != null) {
            // get rid of the dead nodes no longer linked to files
            for (final Iterator<NodeState> it = importedNodeStates.iterator(); it.hasNext(); ) {
                if (file.equals(it.next().getSourceURL())) {
                    it.remove();
                }
            }
        }
    }
    return null;
}
Also used : Node(com.ardor3d.scenegraph.Node) ColladaImporter(com.ardor3d.extension.model.collada.jdom.ColladaImporter) URLResourceSource(com.ardor3d.util.resource.URLResourceSource) ArrayList(java.util.ArrayList) ReadOnlyTransform(com.ardor3d.math.type.ReadOnlyTransform) Mesh(com.ardor3d.scenegraph.Mesh) ReadOnlyVector3(com.ardor3d.math.type.ReadOnlyVector3) Vector3(com.ardor3d.math.Vector3) CullHint(com.ardor3d.scenegraph.hint.CullHint) Matrix3(com.ardor3d.math.Matrix3) MeshData(com.ardor3d.scenegraph.MeshData) ColladaStorage(com.ardor3d.extension.model.collada.jdom.data.ColladaStorage) File(java.io.File)

Aggregations

ColladaImporter (com.ardor3d.extension.model.collada.jdom.ColladaImporter)3 ColladaStorage (com.ardor3d.extension.model.collada.jdom.data.ColladaStorage)3 Matrix3 (com.ardor3d.math.Matrix3)3 Vector3 (com.ardor3d.math.Vector3)3 ReadOnlyVector3 (com.ardor3d.math.type.ReadOnlyVector3)3 Node (com.ardor3d.scenegraph.Node)3 DirectionalLight (com.ardor3d.light.DirectionalLight)2 LightState (com.ardor3d.renderer.state.LightState)2 Spatial (com.ardor3d.scenegraph.Spatial)2 CameraNode (com.ardor3d.scenegraph.extension.CameraNode)2 ResourceSource (com.ardor3d.util.resource.ResourceSource)2 ReadOnlyTransform (com.ardor3d.math.type.ReadOnlyTransform)1 Mesh (com.ardor3d.scenegraph.Mesh)1 MeshData (com.ardor3d.scenegraph.MeshData)1 CullHint (com.ardor3d.scenegraph.hint.CullHint)1 BMText (com.ardor3d.ui.text.BMText)1 URLResourceSource (com.ardor3d.util.resource.URLResourceSource)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1