External Publication
Visit Post

(Lemur)ShaderBackgroundComponent

jMonkeyEngine Hub April 22, 2026
Source
package GUI;

import com.jme3.material.Material;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Quad;
import com.simsilica.lemur.core.GuiControl;
import com.simsilica.lemur.component.AbstractGuiComponent;

// 1. 显式实现 Cloneable 接口
public class ShaderBackgroundComponent extends AbstractGuiComponent implements Cloneable {
    private Geometry background;
    private Material material;

    public ShaderBackgroundComponent(Material material) {
        this.material = material;
    }

    @Override
    public void calculatePreferredSize(Vector3f size) {
    }

    @Override
    public void reshape(Vector3f pos, Vector3f size) {
        if (background == null) {
            Quad q = new Quad(size.x, size.y);
            background = new Geometry("shader_background", q);
            background.setMaterial(material);
            getNode().attachChild(background);
        } else {
            Quad q = (Quad) background.getMesh();
            if (size.x != q.getWidth() || size.y != q.getHeight()) {
                q.updateGeometry(size.x, size.y);
                q.clearCollisionData();
            }
        }

        background.setLocalTranslation(pos.x, pos.y - size.y, pos.z);
    }

    @Override
    public void detach(GuiControl parent) {
        if (background != null) {
            getNode().detachChild(background);
        }
        super.detach(parent);
    }

    // ==========================================
    // 2. 新增重写 clone 方法
    // ==========================================
    @Override
    public ShaderBackgroundComponent clone() {
        ShaderBackgroundComponent result = (ShaderBackgroundComponent) super.clone();

        result.background = null;


        if (this.material != null) {
            result.material = this.material.clone();
        }

        return result;
    }
}

I use this class to apply some shader effects to the UI. I’m not sure if this is the correct way to use it.

But the result is good, it basically meets my needs.

Discussion in the ATmosphere

Loading comments...