Thursday, 5 November 2020

JavaFX: DisplacementMap effect

 

DisplacementMap effect shifts each pixel by a distance specified by the first two bands of of the specified FloatMap.

 

What is FloatMap?

FloatMap is a buffer that contains floating point data, intended for use as a parameter to effects such as DisplacementMap.

 

DisplcementMap provides following constructors to get an instance of DisplacementMap.

 

public DisplacementMap()

public DisplacementMap(FloatMap mapData)

DisplacementMap(FloatMap mapData, double offsetX, double offsetY, double scaleX, double scaleY)

mapData: specifies the map data for this displacement map effect. This specifies how to map pixels.

offsetX: specifies the offset by which all x coordinate offset values in the FloatMap are displaced after they are scaled

offsetY: specifies the offset by which all y coordinate offset values in the FloatMap are displaced after they are scaled

scaleX: specifies the scale factor by which all x coordinate offset values in the FloatMap are multiplied

scaleY: specifies the scale factor by which all y coordinate offset values in the FloatMap are multiplied

 

Follow step-by-step procedure to get an idea of DisplacementMap effect.

 

Step 1: Define FloatMap instance.

FloatMap floatMap1 = new FloatMap(300, 100);

 

Step 2: Populate FloatMap with samples.

for (int i = 0; i < width; i++) {

         double v1 = (Math.sin(i / 20.0 * Math.PI) - 0.5) / 40.0;

         for (int j = 0; j < height; j++) {

                  floatMap1.setSamples(i, j, 0.0f, (float) v1);

         }

}

 

Step 3: Instantiate DisplacementMap with floatmap instance.

DisplacementMap displacementMap1 = new DisplacementMap(floatMap1);

displacementMap1.setScaleX(5);

displacementMap1.setScaleY(2);

 

Step 4: Set DisplacementMap effect to the ui widget.

text1.setEffect(displacementMap1);

 

Find the below working application.

 

displacementMapDemo.fxml

<?import javafx.scene.*?>
<?import javafx.scene.shape.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<Group xmlns:fx="http://javafx.com/fxml" fx:controller="com.sample.app.effects.controller.DisplacementMapController">
	
	<Text fx:id = "text1" text="Learning JavaFX"  fill="lightgreen"
	style="-fx-font-weight: bold; -fx-font-size: 50; -fx-font-family: Verdana; -fx-font-style: italic"
	stroke="darkblue" strokeWidth="2" 
	underline="true"
	x="50" y="50"/>
	
	<Text fx:id = "text2" text="Learning JavaFX"  fill="lightgreen"
	style="-fx-font-weight: bold; -fx-font-size: 50; -fx-font-family: Verdana; -fx-font-style: italic"
	stroke="darkblue" strokeWidth="2" 
	underline="true"
	x="50" y="150"/>
	
	<Text fx:id = "text3" text="Learning JavaFX"  fill="lightgreen"
	style="-fx-font-weight: bold; -fx-font-size: 50; -fx-font-family: Verdana; -fx-font-style: italic"
	stroke="darkblue" strokeWidth="2" 
	underline="true"
	x="50" y="250"/>

</Group>

 

DisplacementMapController.java

package com.sample.app.effects.controller;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.effect.DisplacementMap;
import javafx.scene.effect.FloatMap;
import javafx.scene.text.Text;

public class DisplacementMapController implements Initializable {

	@FXML
	private Text text1;

	@FXML
	private Text text2;

	@FXML
	private Text text3;

	@Override
	public void initialize(URL location, ResourceBundle resources) {
		int width = 300;
		int height = 100;

		FloatMap floatMap1 = new FloatMap(300, 100);
		FloatMap floatMap2 = new FloatMap(300, 100);
		FloatMap floatMap3 = new FloatMap(300, 100);

		for (int i = 0; i < width; i++) {
			double v1 = (Math.sin(i / 20.0 * Math.PI) - 0.5) / 40.0;
			double v2 = (Math.sin(i / 30.0 * Math.PI) - 0.5) / 40.0;
			double v3 = (Math.sin(i / 40.0 * Math.PI) - 0.5) / 40.0;
			for (int j = 0; j < height; j++) {
				floatMap1.setSamples(i, j, 0.0f, (float) v1);
				floatMap2.setSamples(i, j, 0.0f, (float) v2);
				floatMap3.setSamples(i, j, 0.0f, (float) v3);
			}
		}

		DisplacementMap displacementMap1 = new DisplacementMap(floatMap1);
		displacementMap1.setScaleX(5);
		displacementMap1.setScaleY(2);

		DisplacementMap displacementMap2 = new DisplacementMap(floatMap2);
		displacementMap2.setScaleX(5);
		displacementMap2.setScaleY(2);

		DisplacementMap displacementMap3 = new DisplacementMap(floatMap3);
		displacementMap3.setScaleX(5);
		displacementMap3.setScaleY(2);

		text1.setEffect(displacementMap1);
		text2.setEffect(displacementMap2);
		text3.setEffect(displacementMap3);

	}

}

 

DisplacementMapDemo.java

package com.sample.app.effects.controller;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class DisplacementMapDemo extends Application {

	private static final String FXML_FILE = "/displacementMapDemo.fxml";
	private static final String STAGE_TITLE = "Displacement Map Effect";

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

	}

	@Override
	public void start(Stage primaryStage) throws Exception {

		Parent root = (Parent) FXMLLoader.load(this.getClass().getResource(FXML_FILE));

		Scene scene = new Scene(root, 700, 400, Color.WHITE);

		primaryStage.setTitle(STAGE_TITLE);
		primaryStage.setScene(scene);
		primaryStage.show();
	}

}

 

Output


 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment