Hello Assets

这章更新的有点坎坷哈,之前在本本上运行老是报错,问题已经在前一篇描述了,呵呵,好,下面进行正式话题,这章大致讲的是资源管理器

资源管理器

JME3包含了一个很强大的资源管理器,帮助你创建属于你自己的资源。材质是一个多媒体文件,比如一个模型、材质、纹理、场景、着色器、声音、和文字。资源管理器包含了一个根类路径,所以它能从当前路径加载任何文件(最顶级的路径即你的工程路径)。此外,资源管理器也能通过配置、增加任何其他路径到它的根路径上,让资源管理器能加载自定义的路径上的资源。在一个Jme项目中,JME3在你的项目中的assets目录中查找模型等资源。以下是我们推荐的存放资源的目录结构:

assets/Interface/
assets/MatDefs/
assets/Materials/
assets/Models/
assets/Scenes/
assets/Shaders/
assets/Sounds/
assets/Textures/
build.xml
src/...
dist/...

以上只是一个最平常的例子,你能根据你自己的喜好自定义路径。

加载材质

把纹理放到assets/Textures目录里,在你设置材质之前加载这些纹理到你的材质中,以下是相关的实例代码:

        // Create a wall with a simple texture from test_data
        Box box = new Box(Vector3f.ZERO, 2.5f,2.5f,1.0f);
        Spatial wall = new Geometry("Box", box );
        Material mat_brick = new Material(
            assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        mat_brick.setTexture("ColorMap",
            assetManager.loadTexture("Textures/Terrain/BrickWall/BrickWall.jpg"));
        wall.setMaterial(mat_brick);
        wall.setLocalTranslation(2.0f,-2.5f,0.0f);
        rootNode.attachChild(wall);

加载文字和字体

这个例子显示在窗口底部边缘显示默认字体格式的“Hello Text”,你需要将这些文字绑定在guiNode上,这是一个特殊的节点,它显示的是平面上元素(可以理解成是二维的),你能够去掉所有附属在该节点上的存在文字。以下是实例代码:将

        // Display a line of text with a default font
        guiNode.detachAllChildren();
        guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt");
        BitmapText helloText = new BitmapText(guiFont, false);
        helloText.setSize(guiFont.getCharSet().getRenderedSize());
        helloText.setText("Hello World");
        helloText.setLocalTranslation(300, helloText.getLineHeight(), 0);
        guiNode.attachChild(helloText);

加载一个Orgl xml模型

以OgreXML格式导出你的模型(.mesh.xml, .scene, .material, .skeleton.xml),然后放到assets/Models自目录中。以下是实例代码

        // Load a model from test_data (OgreXML + material + texture)
        Spatial ninja = assetManager.loadModel("Models/Ninja/Ninja.mesh.xml");
        ninja.scale(0.05f, 0.05f, 0.05f);
        ninja.rotate(0.0f, -3.0f, 0.0f);
        ninja.setLocalTranslation(0.0f, -5.0f, -2.0f);
        rootNode.attachChild(ninja);
        // You must add a light to make the model visible
        DirectionalLight sun = new DirectionalLight();
        sun.setDirection(new Vector3f(-0.1f, -0.7f, -1.0f));
        rootNode.addLight(sun);

如果你用jMonkeyPlatform创建build脚本,原始的OgreXML文件不会被包含在你的游戏中。当你试图加载他们的时候你会得到一个如下的错误消息。

com.jme3.asset.DesktopAssetManager loadAsset
WARNING: Cannot locate resource: Scenes/town/main.scene
com.jme3.app.Application handleError
SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
java.lang.NullPointerException

对于release版本,你只需要.j3o文件。用jMonkeyPlatform菜单中有该功能能将OgreXML模型转化成.j3o格式。通过自定义路径加载资源如果你的游戏需要用到第三方提供的模型文件,或者某个文件不在默认目录上。那么你能注册一个自定义的位置然后加载该文件。下面是一个注册到你项目根目录的文件town.zip用法的一个例子

    assetManager.registerLocator("town.zip", ZipLocator.class.getName());
    Spatial scene = assetManager.loadModel("main.scene");
    rootNode.attachChild(scene);

下面是通过httpZipLocator方式,下载压缩模型

    assetManager.registerLocator("http://jmonkeyengine.googlecode.com/files/wildhouse.zip",
                                 HttpZipLocator.class.getName());
    Spatial scene = assetManager.loadModel("main.scene");
    rootNode.attachChild(scene);
JME3提供了ClasspathLocator, ZipLocator, FileLocator, HttpZipLocator, and UrlLocator等方法加载资源。

转载于:https://www.cnblogs.com/zpfly/archive/2011/06/26/2085564.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值