Browse Source

* 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
sha..rd 14 years ago
parent
commit
86a4ad3718

+ 1 - 0
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 '{'");

+ 10 - 0
engine/src/core/com/jme3/material/Material.java

@@ -84,6 +84,8 @@ public class Material implements Cloneable, Savable, Comparable<Material> {
         additiveLight.setBlendMode(RenderState.BlendMode.AlphaAdditive);
         additiveLight.setDepthWrite(false);
     }
+
+    private String assetName;
     private MaterialDef def;
     private ListMap<String, MatParam> paramValues = new ListMap<String, MatParam>();
     private Technique technique;
@@ -173,6 +175,14 @@ public class Material implements Cloneable, Savable, Comparable<Material> {
     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) {

+ 4 - 4
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(){

+ 26 - 1
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);
     }