06. エフェクト - hannsi-to/LFJG-LiteFrameJavaGui GitHub Wiki

まずここではオブジェクトにエフェクトを適応する方法をお伝えします。

準備

準備としてグローバル変数として EffectCache を作成してください。init関数内でインスタンを作成し createCache 関数で DrawObject エフェクトを追加してください。エフェクトを適用したいオブジェクトから setEffectCache でエフェクトを適応してください。最後にstopFrame関数内でクリーンアップしてください。実際のコードです。

public class TestGuiFrame implements LFJGFrame {
    GLObjectCache glObjectCache;
    EffectCache glRect1EffectCache;

    GLRect glRect1;

    private Frame frame;

    public static void main(String[] args) {
        new TestGuiFrame().setFrame();
    }

    public void setFrame() {
        frame = new Frame(this, "TestGuiFrame");
    }

    @Override
    public void init() {
        LFJGContext.projection = new Projection(ProjectionType.OrthographicProjection, frame.getWindowWidth(), frame.getWindowHeight());
        LFJGContext.resolution = new Vector2f(frame.getWindowWidth() / frame.getContentScaleX(), frame.getWindowHeight() / frame.getContentScaleY());

        glRect1 = new GLRect("Rect1");
        glRect1.rect(500, 500, 1000, 1000, Color.RED);

        glRect1EffectCache = new EffectCache();
        glRect1EffectCache.createCache("DrawObject1", new DrawObject());

        glRect1EffectCache.create(glRect1);
        glRect1.setEffectCache(glRect1EffectCache);

        glObjectCache = new GLObjectCache();
        glObjectCache.createCache(glRect1);
    }

    @Override
    public void drawFrame() {
        glObjectCache.draw();
    }

    @Override
    public void stopFrame() {
        glRect1EffectCache.cleanup();
        glObjectCache.cleanup();
    }

    @Override
    public void setFrameSetting() {
    }
}

まずcreateCache関数ですが引数を2渡します。 第一引数は、他のエフェクトと重複しない名前を設定してください。 第二引数は、エフェクトのインスタンしを代入してください。 setEffectCache関数でオブジェクトにエフェクトを適応します。

エフェクト追加

テストとして回転エフェクトを適応してみます。init関数内に

glRect1EffectCache.createCache("Rotate1", new Rotate(0, 0, MathHelper.toRadians(45), 500, 500));

を追加してください。これでオブジェクトが左に45度傾きます。実際の結果です。 ウィンドウサイズのせいで上が切れていますが適切に回転エフェクトが適応されています。全体のコードです。

public class TestGuiFrame implements LFJGFrame {
    GLObjectCache glObjectCache;
    EffectCache glRect1EffectCache;

    GLRect glRect1;

    private Frame frame;

    public static void main(String[] args) {
        new TestGuiFrame().setFrame();
    }

    public void setFrame() {
        frame = new Frame(this, "TestGuiFrame");
    }

    @Override
    public void init() {
        LFJGContext.projection = new Projection(ProjectionType.OrthographicProjection, frame.getWindowWidth(), frame.getWindowHeight());
        LFJGContext.resolution = new Vector2f(frame.getWindowWidth() / frame.getContentScaleX(), frame.getWindowHeight() / frame.getContentScaleY());

        glRect1 = new GLRect("Rect1");
        glRect1.rect(500, 500, 1000, 1000, Color.RED);

        glRect1EffectCache = new EffectCache();
        glRect1EffectCache.createCache("DrawObject1", new DrawObject());
        glRect1EffectCache.createCache("Rotate1", new Rotate(0, 0, MathHelper.toRadians(45), 500, 500));

        glRect1EffectCache.create(glRect1);
        glRect1.setEffectCache(glRect1EffectCache);

        glObjectCache = new GLObjectCache();
        glObjectCache.createCache(glRect1);
    }

    @Override
    public void drawFrame() {
        glObjectCache.draw();
    }

    @Override
    public void stopFrame() {
        glRect1EffectCache.cleanup();
        glObjectCache.cleanup();
    }

    @Override
    public void setFrameSetting() {
    }
}

エフェクトはいくらでも重ねがけできいます。

[!CAUTION] DrawObject、DrawFrameBufferは特別なエフェクトです。適切な場所で適切なタイミングで使用してください。

エフェクトアニメーション

drawFrame関数内でエフェクトの値を常に変えることができます。関数内に以下のコードを追加してください。

Rotate rotate1 = (Rotate) glRect1EffectCache.getEffectBase("Rotate1");
rotate1.setZ(rotate1.getZ() + MathHelper.toRadians(10));

このコードは毎フレーム10度ずつずらすコードです。実行結果はです。