如何解决如何在窗格上添加多个灯光效果?
我对 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);
}
}
this is how it looks at the moment
所以“光”被覆盖了。
解决方法
effect
属性是 Java 中的任何其他属性。如果您将其设置为一个值,然后立即将其设置为第二个值,它将具有第二个值。
要结合两种效果,请使用 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);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。