/* * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. */ import java.nio.ByteBuffer; import javafx.application.Application; import javafx.scene.Group; import javafx.scene.LightBase; import javafx.scene.PointLight; import javafx.scene.Scene; import javafx.scene.image.PixelFormat; import javafx.scene.image.WritableImage; import javafx.scene.paint.Color; import javafx.scene.paint.PhongMaterial; import javafx.scene.shape.Shape3D; import javafx.scene.shape.Sphere; import javafx.scene.transform.Rotate; import javafx.stage.Stage; /** * * @author Andrew Glushchenko */ public class MiltipleLightingTestApp extends Application { private static int WIDTH = 500; private static int HEIGHT = 500; private static int SCALE = 100; private Group root; private Group light1mv; private Group light2mv; private Scene scene; private PhongMaterial material; VisibleLight vl1; VisibleLight vl2; @Override public void start(Stage stage) throws Exception { initScene(); stage.setScene(scene); stage.show(); } protected void initScene() { scene = new Scene(buildGroup(), WIDTH, HEIGHT, true); root.translateXProperty().set(WIDTH / 2); root.translateYProperty().set(HEIGHT / 2); root.translateZProperty().set(150); light1mv.setRotationAxis(Rotate.X_AXIS); light2mv.setRotationAxis(Rotate.X_AXIS); light1mv.setRotate(90); light2mv.setRotate(-90); } protected Group buildGroup() { material = new PhongMaterial(); material.setSpecularColor(Color.rgb(255, 255, 255)); material.setSpecularPower(0); Shape3D sh = new Sphere(); sh.setMaterial(material); sh.setScaleX(SCALE); sh.setScaleY(SCALE); sh.setScaleZ(SCALE); vl1 = new VisibleLight(Color.BLUE); vl2 = new VisibleLight(Color.RED); light1mv = buildLightGroup(vl1); light2mv = buildLightGroup(vl2); root = new Group(light1mv, light2mv, sh); return root; } private static Group buildLightGroup(VisibleLight vl) { vl.setTranslateZ(-140); Sphere balance = new Sphere(); balance.setTranslateZ(140); return new Group(vl, balance); } public static void main(String[] args) { launch(args); } private class VisibleLight extends Group { private PhongMaterial material; private LightBase lightBase; private void makeConstantImage(WritableImage img, Color color) { int w = (int) img.getWidth(); int h = (int) img.getHeight(); ByteBuffer scanline = ByteBuffer.allocate(3 * w); byte r = (byte) ((int) Math.round(color.getRed() * 255.0)); byte g = (byte) ((int) Math.round(color.getGreen() * 255.0)); byte b = (byte) ((int) Math.round(color.getBlue() * 255.0)); for (int i = 0; i < w; i++) { scanline.put(r); scanline.put(g); scanline.put(b); } scanline.rewind(); img.getPixelWriter().setPixels(0, 0, w, w, PixelFormat.getByteRgbInstance(), scanline, 0); } public VisibleLight(Color color) { lightBase = new PointLight(); material = new PhongMaterial(Color.TRANSPARENT); material.setDiffuseMap(null); material.setBumpMap(null); material.setSpecularMap(null); material.setSpecularColor(Color.TRANSPARENT); setColor(color); final Sphere light = new Sphere(50); light.setScaleX(0.2); light.setScaleY(0.2); light.setScaleZ(0.2); light.setMaterial(material); this.getChildren().addAll(lightBase, light); } public final void setColor(Color color) { lightBase.setColor(color); WritableImage selfIllumMap = new WritableImage(64, 64); makeConstantImage(selfIllumMap, color); material.setSelfIlluminationMap(selfIllumMap); } } static { System.setProperty("prism.dirtyopts", "false"); } }