Home | History | Annotate | Download | only in material
      1 package com.jme3.material;
      2 
      3 import com.jme3.export.InputCapsule;
      4 import com.jme3.export.JmeExporter;
      5 import com.jme3.export.JmeImporter;
      6 import com.jme3.export.OutputCapsule;
      7 import com.jme3.renderer.Renderer;
      8 import com.jme3.shader.VarType;
      9 import com.jme3.texture.Texture;
     10 import java.io.IOException;
     11 
     12 public class MatParamTexture extends MatParam {
     13 
     14     private Texture texture;
     15     private int unit;
     16 
     17     public MatParamTexture(VarType type, String name, Texture texture, int unit) {
     18         super(type, name, texture, null);
     19         this.texture = texture;
     20         this.unit = unit;
     21     }
     22 
     23     public MatParamTexture() {
     24     }
     25 
     26     public Texture getTextureValue() {
     27         return texture;
     28     }
     29 
     30     public void setTextureValue(Texture value) {
     31         this.value = value;
     32         this.texture = value;
     33     }
     34 
     35     public void setUnit(int unit) {
     36         this.unit = unit;
     37     }
     38 
     39     public int getUnit() {
     40         return unit;
     41     }
     42 
     43     @Override
     44     public void apply(Renderer r, Technique technique) {
     45         TechniqueDef techDef = technique.getDef();
     46         r.setTexture(getUnit(), getTextureValue());
     47         if (techDef.isUsingShaders()) {
     48             technique.updateUniformParam(getPrefixedName(), getVarType(), getUnit(), true);
     49         }
     50     }
     51 
     52     @Override
     53     public void write(JmeExporter ex) throws IOException {
     54         super.write(ex);
     55         OutputCapsule oc = ex.getCapsule(this);
     56         oc.write(unit, "texture_unit", -1);
     57         oc.write(texture, "texture", null);
     58     }
     59 
     60     @Override
     61     public void read(JmeImporter im) throws IOException {
     62         super.read(im);
     63         InputCapsule ic = im.getCapsule(this);
     64         unit = ic.readInt("texture_unit", -1);
     65         texture = (Texture) ic.readSavable("texture", null);
     66     }
     67 }