Home | History | Annotate | Download | only in matext
      1 /*
      2  * Copyright (c) 2009-2010 jMonkeyEngine
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are
      7  * met:
      8  *
      9  * * Redistributions of source code must retain the above copyright
     10  *   notice, this list of conditions and the following disclaimer.
     11  *
     12  * * Redistributions in binary form must reproduce the above copyright
     13  *   notice, this list of conditions and the following disclaimer in the
     14  *   documentation and/or other materials provided with the distribution.
     15  *
     16  * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
     17  *   may be used to endorse or promote products derived from this software
     18  *   without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 package com.jme3.scene.plugins.ogre.matext;
     34 
     35 import com.jme3.asset.AssetKey;
     36 import com.jme3.asset.AssetManager;
     37 import com.jme3.asset.AssetNotFoundException;
     38 import com.jme3.asset.TextureKey;
     39 import com.jme3.material.Material;
     40 import com.jme3.material.MaterialList;
     41 import com.jme3.scene.plugins.ogre.MaterialLoader;
     42 import com.jme3.texture.Texture;
     43 import com.jme3.texture.Texture.WrapMode;
     44 import com.jme3.texture.Texture2D;
     45 import com.jme3.util.PlaceholderAssets;
     46 import com.jme3.util.blockparser.Statement;
     47 import java.io.IOException;
     48 import java.util.List;
     49 import java.util.logging.Level;
     50 import java.util.logging.Logger;
     51 
     52 /**
     53  * Used internally by {@link MaterialLoader}
     54  */
     55 public class MaterialExtensionLoader {
     56 
     57     private static final Logger logger = Logger.getLogger(MaterialExtensionLoader.class.getName());
     58 
     59     private AssetKey key;
     60     private AssetManager assetManager;
     61     private MaterialList list;
     62     private MaterialExtensionSet matExts;
     63     private MaterialExtension matExt;
     64     private String matName;
     65     private Material material;
     66 
     67 
     68     private void readExtendingMaterialStatement(Statement statement) throws IOException {
     69         if (statement.getLine().startsWith("set_texture_alias")){
     70             String[] split = statement.getLine().split(" ", 3);
     71             String aliasName = split[1];
     72             String texturePath = split[2];
     73 
     74             String jmeParamName = matExt.getTextureMapping(aliasName);
     75 
     76             TextureKey texKey = new TextureKey(texturePath, false);
     77             texKey.setGenerateMips(true);
     78             texKey.setAsCube(false);
     79             Texture tex;
     80 
     81             try {
     82                 tex = assetManager.loadTexture(texKey);
     83                 tex.setWrap(WrapMode.Repeat);
     84             } catch (AssetNotFoundException ex){
     85                 logger.log(Level.WARNING, "Cannot locate {0} for material {1}", new Object[]{texKey, key});
     86                 tex = new Texture2D( PlaceholderAssets.getPlaceholderImage() );
     87             }
     88 
     89             material.setTexture(jmeParamName, tex);
     90         }
     91     }
     92 
     93     private Material readExtendingMaterial(Statement statement) throws IOException{
     94         String[] split = statement.getLine().split(" ", 2);
     95         String[] subsplit = split[1].split(":");
     96         matName = subsplit[0].trim();
     97         String extendedMat = subsplit[1].trim();
     98 
     99         matExt = matExts.getMaterialExtension(extendedMat);
    100         if (matExt == null){
    101             logger.log(Level.WARNING, "Cannot find MaterialExtension for: {0}. Ignoring material.", extendedMat);
    102             matExt = null;
    103             return null;
    104         }
    105 
    106         material = new Material(assetManager, matExt.getJmeMatDefName());
    107         for (Statement extMatStat : statement.getContents()){
    108             readExtendingMaterialStatement(extMatStat);
    109         }
    110         return material;
    111     }
    112 
    113     public MaterialList load(AssetManager assetManager, AssetKey key, MaterialExtensionSet matExts,
    114             List<Statement> statements) throws IOException{
    115         this.assetManager = assetManager;
    116         this.matExts = matExts;
    117         this.key = key;
    118 
    119         list = new MaterialList();
    120 
    121         for (Statement statement : statements){
    122             if (statement.getLine().startsWith("import")){
    123                 // ignore
    124                 continue;
    125             }else if (statement.getLine().startsWith("material")){
    126                 Material material = readExtendingMaterial(statement);
    127                 list.put(matName, material);
    128                 List<String> matAliases = matExts.getNameMappings(matName);
    129                 if(matAliases != null){
    130                     for (String string : matAliases) {
    131                         list.put(string, material);
    132                     }
    133                 }
    134             }
    135         }
    136         return list;
    137     }
    138 }
    139