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

📄 modelloader.java

📁 java 3d game jme 工程开发源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                } else {
                    JOptionPane.showMessageDialog(null, "Selected file's extension is unknown model type: " + file.getName());
                }
            }
        } catch(Throwable t) {
            StringWriter writer = new StringWriter();
            PrintWriter stream = new PrintWriter(writer);
            t.printStackTrace(stream);
            JFrame frame = new JFrame();
            frame.setTitle("ModelLoader - StackTrace");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JTextPane panel = new JTextPane();
            panel.setPreferredSize(new Dimension(400, 400));
            panel.setContentType("text/plain");
            panel.setText(writer.getBuffer().toString());
            frame.setContentPane(new JScrollPane(panel));
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    }
    
    private static File createJMEFile(File f) {
        String filename = f.getName();
        if (filename.indexOf('.') != -1) {
            filename = filename.substring(0, filename.lastIndexOf('.'));
        }
        filename = filename + ".jme";
        return new File(f.getParentFile(), filename);
    }
    
    private static void outputElapsed(long startTime) {
        float elapsed = (System.currentTimeMillis() - startTime) / 1000.0f;
        logger.info("Took " + elapsed + " seconds to load the model.");
    }

    public static Node loadModel( final File file ) throws Exception {
        return loadModel( file.getName() );
    }
    public static Node loadModel( final String file ) throws Exception {
        
        String extension = extensionOf( file );

        ModelLoaderCallable callable = loaders.get( extension );
        if ( callable == null ) {
            throw new UnsupportedOperationException( "Unknown file type: " + file );
        }
        callable.setFile( file );
        Future<Node> future = GameTaskQueueManager.getManager().update( callable );
        return future.get();
    }

    private static String extensionOf( String file ) {
        String fileName = file.toUpperCase();
        int lastDot = fileName.lastIndexOf( '.' );
        String extension;
        if ( lastDot >= 0 ) {
            extension = fileName.substring( lastDot + 1 );
        } else {
            extension = "";
        }
        return extension;
    }

    private static Node scale( Node model ) {
        if ( model != null ) {
            // scale model to maximum extent of 5.0
            model.updateGeometricState( 0, true );
            BoundingVolume worldBound = model.getWorldBound();
            if ( worldBound == null ) {
                model.setModelBound( new BoundingBox() );
                model.updateModelBound();
                model.updateGeometricState( 0, true );
                worldBound = model.getWorldBound();
            }
            if ( worldBound != null ) // check not still null (no geoms)
            {
                Vector3f center = worldBound.getCenter();
                BoundingBox boundingBox = new BoundingBox( center, 0, 0, 0 );
                boundingBox.mergeLocal( worldBound );
                Vector3f extent = boundingBox.getExtent( null );
                float maxExtent = Math.max( Math.max( extent.x, extent.y ), extent.z );
                if ( maxExtent != 0 ) {
                    Node scaledModel = new Node( "scaled model" );
                    scaledModel.attachChild( model );
                    scaledModel.setLocalScale( 5.0f / maxExtent );
                    model = scaledModel;
                }
            }
        }
        return model;
    }

    public static boolean isValidModelFile( File file ) {
        return isValidModelFile( file.getName() );
    }
    public static boolean isValidModelFile( String file ) {
        return loaders.containsKey( extensionOf( file ) );
    }

    private static Map<String, ModelLoaderCallable> loaders = new HashMap<String, ModelLoaderCallable>();

    static {
        loaders.put( "DAE", new DAECallable() );
        loaders.put( "JME", new JMECallable() );
//              Note that .OBJ Ambient colors are multiplied. I would strongly suggest making them black.
        loaders.put( "OBJ", new ModelLoaderCallable(
                new ObjToJme(), new String[]{ "mtllib", "texdir" } )
        );
//              Note that some .3DS Animations from Blender may not work. I'd suggest using a version of 3DSMax.
        loaders.put( "3DS", new ModelLoaderCallable(
                new MaxToJme(), new String[]{ "texurl", "texdir" } )
        );
        loaders.put( "ASE", new ModelLoaderCallable( new AseToJme() ) );
        loaders.put( "MD2", new ModelLoaderCallable( new Md2ToJme() ) );
        loaders.put( "MD3", new ModelLoaderCallable( new Md3ToJme() ) );
        loaders.put( "MS3D", new ModelLoaderCallable( new MilkToJme() ) );
        try {
            loaders.put( "X3D", new ModelLoaderCallable( new X3dToJme() ) );
        } catch( InstantiationException ex ) { logger.logp( Level.SEVERE, ModelLoader.class.toString(),
                "<static>", "InstantiationException", ex ); }
    }

    static class ModelLoaderCallable implements Callable<Node> {
        protected String file, props[];
        protected FormatConverter converter;
        public ModelLoaderCallable( FormatConverter conv ) { this( conv, null ); }
        public ModelLoaderCallable( FormatConverter conv, String[] properties )
        {
            converter = conv;
            setProperties( properties );
        }
        public void setProperties( String[] p )
        {
            props = ( p == null ? new String[0] : p );
        }
        public void setFile( String file ) { this.file = file; }
        protected URL getURL() { return ResourceLocatorTool.locateResource( ResourceLocatorTool.TYPE_MODEL, file ); }
        public Node call() throws Exception
        {
            if( converter == null )
                return null;
            URL url = getURL();
            for( int i = 0; i < props.length; i++ )
                converter.setProperty( props[i], url );
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            converter.convert( url.openStream(), bos );
            Savable savable = BinaryImporter.getInstance().load( new ByteArrayInputStream( bos.toByteArray() ) );
            if ( savable instanceof Node ) {
                return (Node) savable;
            } else {
                Node model = new Node( "Imported Model " + file );
                model.attachChild( (Spatial) savable );
                return model;
            }
        }
    }

    private static class JMECallable extends ModelLoaderCallable {
        public JMECallable() { super( null, null ); }
        public Node call() throws Exception {
            return (Node) BinaryImporter.getInstance().load( getURL() );
        }
    }

    private static class DAECallable extends ModelLoaderCallable {
        public DAECallable() { super( null, null ); }
        public Node call() throws Exception {
            ColladaImporter.load( getURL().openStream(), "Model" );
            Node model = ColladaImporter.getModel();
            return model;
        }
    }
}

⌨️ 快捷键说明

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