首页 > 解决方案 > 如何在窗格上添加多个灯光效果?

问题描述

我对 JavaFX 灯光效果有疑问。我有一个游戏需要在同一个窗格上使用多个点灯,但如果可能的话,我还没有设法做到这一点。现在我有一个窗格和它上面的所有元素。

这似乎是一种不好的方法,所以如果有人知道为 2D 游戏添加光源的更好方法,我将非常感谢您的帮助!

似乎只能将一个灯光效果附加到窗格,因为每当我尝试设置新的灯光效果时,另一个都会被删除。对于这个项目来说,一盏灯是不够的。如果有更好的添加灯光的方法,请告诉我!也许将灯连接到块上,然后以某种方式使其在窗格上发光?这是代码:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.effect.Light;
import javafx.scene.effect.Lighting;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class game extends Application{

@Override
public void start(Stage alku) throws Exception {
    Pane test=new Pane();
    Rectangle box = new Rectangle(200,200);
    box.setFill(Color.WHITE);
    box.setTranslateX(50);
    box.setTranslateY(50);
    test.getChildren().add(box);
    
    Rectangle box2 = new Rectangle(200,200);
    box2.setFill(Color.WHITE);
    box2.setTranslateX(50);
    box2.setTranslateY(300);
    test.getChildren().add(box2);
    
    Scene scene = new Scene(test,400,400);
    
Lighting light = new Lighting();
Light.Point l = new Light.Point();
l.xProperty().set(70);
l.yProperty().set(200);
l.setZ(50);
l.setColor(Color.GREEN);
light.setLight(l);
test.setEffect(light);


  Lighting light2 = new Lighting();
Light.Point l2 = new Light.Point();
l2.xProperty().set(20);
l2.yProperty().set(200);
l2.setZ(50);
l2.setColor(Color.RED);
light2.setLight(l2);
test.setEffect(light2);



    alku.setTitle("light test");
    alku.setScene(scene);
    alku.show();
}


public static void main(String[] args) {
    
     launch(args); 
    }


}


    

这就是现在的样子

所以“光”被覆盖了。

标签: javajavafxlight

解决方案


effect属性与 Java 中的任何其他属性一样。如果将其设置为一个值,然后立即将其设置为第二个值,它将具有第二个值。

要组合两种效果,请使用 a Blend

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.effect.Blend;
import javafx.scene.effect.BlendMode;
import javafx.scene.effect.Light;
import javafx.scene.effect.Lighting;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class Game extends Application {

    @Override
    public void start(Stage alku) throws Exception {
        Pane test = new Pane();
        Rectangle box = new Rectangle(200, 200);
        box.setFill(Color.WHITE);
        box.setTranslateX(50);
        box.setTranslateY(50);
        test.getChildren().add(box);

        Rectangle box2 = new Rectangle(200, 200);
        box2.setFill(Color.WHITE);
        box2.setTranslateX(50);
        box2.setTranslateY(300);
        test.getChildren().add(box2);

        Scene scene = new Scene(test, 400, 400);

        Lighting light = new Lighting();
        Light.Point l = new Light.Point();
        l.xProperty().set(70);
        l.yProperty().set(200);
        l.setZ(50);
        l.setColor(Color.GREEN);
        light.setLight(l);
        //test.setEffect(light);

        Lighting light2 = new Lighting();
        Light.Point l2 = new Light.Point();
        l2.xProperty().set(20);
        l2.yProperty().set(200);
        l2.setZ(50);
        l2.setColor(Color.RED);
        light2.setLight(l2);
        //test.setEffect(light2);

        Blend blend = new Blend(BlendMode.ADD);
        blend.setTopInput(light);
        blend.setBottomInput(light2);
        
        test.setEffect(blend);

        alku.setTitle("light test");
        alku.setScene(scene);
        alku.show();
    }

    public static void main(String[] args) {

        launch(args);
    }

}

你基本上可以对任意数量的灯做同样的事情:

Lighting[] lotsOfLights = ... ;

Effect allLights = lotsOfLights[0] ;

for (int i = 1 ; i < lotsOfLights.length ; i++) 
    allLights = new Blend(BlendMode.ADD, allLights, lotsOfLights[i]);

someNode.setEffect(allLights);

推荐阅读