1package com.jme3.material;
2
3import com.jme3.export.InputCapsule;
4import com.jme3.export.JmeExporter;
5import com.jme3.export.JmeImporter;
6import com.jme3.export.OutputCapsule;
7import com.jme3.renderer.Renderer;
8import com.jme3.shader.VarType;
9import com.jme3.texture.Texture;
10import java.io.IOException;
11
12public 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}