Home | History | Annotate | Download | only in material
      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.material;
     34 
     35 import com.jme3.asset.AssetManager;
     36 import com.jme3.shader.VarType;
     37 import java.util.*;
     38 import java.util.logging.Level;
     39 import java.util.logging.Logger;
     40 
     41 /**
     42  * Describes a J3MD (Material definition).
     43  *
     44  * @author Kirill Vainer
     45  */
     46 public class MaterialDef {
     47 
     48     private static final Logger logger = Logger.getLogger(MaterialDef.class.getName());
     49 
     50     private String name;
     51     private String assetName;
     52     private AssetManager assetManager;
     53 
     54     private List<TechniqueDef> defaultTechs;
     55     private Map<String, TechniqueDef> techniques;
     56     private Map<String, MatParam> matParams;
     57 
     58     /**
     59      * Serialization only. Do not use.
     60      */
     61     public MaterialDef(){
     62     }
     63 
     64     /**
     65      * Creates a new material definition with the given name.
     66      *
     67      * @param assetManager The asset manager to use to load shaders
     68      * @param name The debug name of the material definition
     69      */
     70     public MaterialDef(AssetManager assetManager, String name){
     71         this.assetManager = assetManager;
     72         this.name = name;
     73         techniques = new HashMap<String, TechniqueDef>();
     74         matParams = new HashMap<String, MatParam>();
     75         defaultTechs = new ArrayList<TechniqueDef>();
     76         logger.log(Level.INFO, "Loaded material definition: {0}", name);
     77     }
     78 
     79     /**
     80      * Returns the asset key name of the asset from which this material
     81      * definition was loaded.
     82      *
     83      * @return Asset key name of the j3md file
     84      */
     85     public String getAssetName() {
     86         return assetName;
     87     }
     88 
     89     /**
     90      * Set the asset key name.
     91      *
     92      * @param assetName the asset key name
     93      */
     94     public void setAssetName(String assetName) {
     95         this.assetName = assetName;
     96     }
     97 
     98     /**
     99      * Returns the AssetManager passed in the constructor.
    100      *
    101      * @return the AssetManager passed in the constructor.
    102      */
    103     public AssetManager getAssetManager(){
    104         return assetManager;
    105     }
    106 
    107     /**
    108      * The debug name of the material definition.
    109      *
    110      * @return debug name of the material definition.
    111      */
    112     public String getName(){
    113         return name;
    114     }
    115 
    116     /**
    117      * Adds a new material parameter.
    118      *
    119      * @param type Type of the parameter
    120      * @param name Name of the parameter
    121      * @param value Default value of the parameter
    122      * @param ffBinding Fixed function binding for the parameter
    123      */
    124     public void addMaterialParam(VarType type, String name, Object value, FixedFuncBinding ffBinding) {
    125         matParams.put(name, new MatParam(type, name, value, ffBinding));
    126     }
    127 
    128     /**
    129      * Returns the material parameter with the given name.
    130      *
    131      * @param name The name of the parameter to retrieve
    132      *
    133      * @return The material parameter, or null if it does not exist.
    134      */
    135     public MatParam getMaterialParam(String name){
    136         return matParams.get(name);
    137     }
    138 
    139     /**
    140      * Returns a collection of all material parameters declared in this
    141      * material definition.
    142      * <p>
    143      * Modifying the material parameters or the collection will lead
    144      * to undefined results.
    145      *
    146      * @return All material parameters declared in this definition.
    147      */
    148     public Collection<MatParam> getMaterialParams(){
    149         return matParams.values();
    150     }
    151 
    152     /**
    153      * Adds a new technique definition to this material definition.
    154      * <p>
    155      * If the technique name is "Default", it will be added
    156      * to the list of {@link MaterialDef#getDefaultTechniques() default techniques}.
    157      *
    158      * @param technique The technique definition to add.
    159      */
    160     public void addTechniqueDef(TechniqueDef technique){
    161         if (technique.getName().equals("Default")){
    162             defaultTechs.add(technique);
    163         }else{
    164             techniques.put(technique.getName(), technique);
    165         }
    166     }
    167 
    168     /**
    169      * Returns a list of all default techniques.
    170      *
    171      * @return a list of all default techniques.
    172      */
    173     public List<TechniqueDef> getDefaultTechniques(){
    174         return defaultTechs;
    175     }
    176 
    177     /**
    178      * Returns a technique definition with the given name.
    179      * This does not include default techniques which can be
    180      * retrieved via {@link MaterialDef#getDefaultTechniques() }.
    181      *
    182      * @param name The name of the technique definition to find
    183      *
    184      * @return The technique definition, or null if cannot be found.
    185      */
    186     public TechniqueDef getTechniqueDef(String name) {
    187         return techniques.get(name);
    188     }
    189 
    190 }
    191