From 86a4ad371889cc1439c4f54d89175f379facfa9a Mon Sep 17 00:00:00 2001 From: "sha..rd" Date: Sun, 3 Apr 2011 21:04:47 +0000 Subject: [PATCH] * J3O files will now automatically load J3M files if they were loaded via assetManager.loadMaterial() git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@7175 75d07b2b-3a1a-0410-a2c5-0572b91ccdca --- .../com/jme3/material/plugins/J3MLoader.java | 1 + .../src/core/com/jme3/material/Material.java | 10 +++++++ .../core/com/jme3/material/MaterialDef.java | 8 +++--- engine/src/core/com/jme3/scene/Geometry.java | 27 ++++++++++++++++++- 4 files changed, 41 insertions(+), 5 deletions(-) diff --git a/engine/src/core-plugins/com/jme3/material/plugins/J3MLoader.java b/engine/src/core-plugins/com/jme3/material/plugins/J3MLoader.java index d8477a46e..ec3268a46 100644 --- a/engine/src/core-plugins/com/jme3/material/plugins/J3MLoader.java +++ b/engine/src/core-plugins/com/jme3/material/plugins/J3MLoader.java @@ -534,6 +534,7 @@ public class J3MLoader implements AssetLoader { throw new IOException("Extended material "+extendedMat+" cannot be found."); material = new Material(def); + material.setAssetName(fileName); }else if (scan.hasNext("\\{")){ if (extending){ throw new IOException("Expected ':', got '{'"); diff --git a/engine/src/core/com/jme3/material/Material.java b/engine/src/core/com/jme3/material/Material.java index b9aedc78e..0292d93c5 100644 --- a/engine/src/core/com/jme3/material/Material.java +++ b/engine/src/core/com/jme3/material/Material.java @@ -84,6 +84,8 @@ public class Material implements Cloneable, Savable, Comparable { additiveLight.setBlendMode(RenderState.BlendMode.AlphaAdditive); additiveLight.setDepthWrite(false); } + + private String assetName; private MaterialDef def; private ListMap paramValues = new ListMap(); private Technique technique; @@ -173,6 +175,14 @@ public class Material implements Cloneable, Savable, Comparable { public Material() { } + public String getAssetName() { + return assetName; + } + + public void setAssetName(String assetName) { + this.assetName = assetName; + } + public int getSortId() { Technique t = getActiveTechnique(); if (sortingId == -1 && t != null && t.getShader() != null) { diff --git a/engine/src/core/com/jme3/material/MaterialDef.java b/engine/src/core/com/jme3/material/MaterialDef.java index ecc617163..6f9c99afb 100644 --- a/engine/src/core/com/jme3/material/MaterialDef.java +++ b/engine/src/core/com/jme3/material/MaterialDef.java @@ -65,12 +65,12 @@ public class MaterialDef { logger.log(Level.INFO, "Loaded material definition: {0}", name); } - public void setAssetName(String assetName){ - this.assetName = assetName; + public String getAssetName() { + return assetName; } - public String getAssetName(){ - return assetName; + public void setAssetName(String assetName) { + this.assetName = assetName; } public AssetManager getAssetManager(){ diff --git a/engine/src/core/com/jme3/scene/Geometry.java b/engine/src/core/com/jme3/scene/Geometry.java index 725449d9f..afa143f9e 100644 --- a/engine/src/core/com/jme3/scene/Geometry.java +++ b/engine/src/core/com/jme3/scene/Geometry.java @@ -32,6 +32,7 @@ package com.jme3.scene; +import com.jme3.asset.AssetNotFoundException; import com.jme3.bounding.BoundingVolume; import com.jme3.collision.Collidable; import com.jme3.collision.CollisionResults; @@ -45,9 +46,13 @@ import com.jme3.scene.VertexBuffer.Type; import com.jme3.util.TempVars; import java.io.IOException; import java.util.Queue; +import java.util.logging.Level; +import java.util.logging.Logger; public class Geometry extends Spatial { + private static final Logger logger = Logger.getLogger(Geometry.class.getName()); + /** * The mesh contained herein */ @@ -327,6 +332,9 @@ public class Geometry extends Spatial { super.write(ex); OutputCapsule oc = ex.getCapsule(this); oc.write(mesh, "mesh", null); + if (material != null){ + oc.write(material.getAssetName(), "materialName", null); + } oc.write(material, "material", null); oc.write(ignoreTransform, "ignoreTransform", false); } @@ -336,7 +344,24 @@ public class Geometry extends Spatial { super.read(im); InputCapsule ic = im.getCapsule(this); mesh = (Mesh) ic.readSavable("mesh", null); - material = (Material) ic.readSavable("material", null); + + material = null; + String matName = ic.readString("materialName", null); + if (matName != null){ + // Material name is set, + // Attempt to load material via J3M + try { + material = im.getAssetManager().loadMaterial(matName); + } catch (AssetNotFoundException ex){ + // Cannot find J3M file. + logger.log(Level.FINE, "Could not load J3M file {0} for Geometry.", + matName); + } + } + // If material is NULL, try to load it from the geometry + if (material == null){ + material = (Material) ic.readSavable("material", null); + } ignoreTransform = ic.readBoolean("ignoreTransform", false); }