|
| 1 | +package com.projecturanus.noisevisualizer |
| 2 | + |
| 3 | +import javafx.animation.TranslateTransition |
| 4 | +import javafx.application.Application |
| 5 | +import javafx.beans.property.BooleanProperty |
| 6 | +import javafx.scene.* |
| 7 | +import javafx.scene.control.Label |
| 8 | +import javafx.scene.input.KeyCode |
| 9 | +import javafx.scene.paint.Color |
| 10 | +import javafx.scene.paint.PhongMaterial |
| 11 | +import javafx.scene.shape.Box |
| 12 | +import javafx.scene.transform.Rotate |
| 13 | +import javafx.stage.Stage |
| 14 | +import javafx.util.Duration |
| 15 | +import org.controlsfx.control.RangeSlider |
| 16 | +import tornadofx.* |
| 17 | +import java.util.* |
| 18 | +import kotlin.math.abs |
| 19 | + |
| 20 | +private var blockMap = (0 until 16).map { (0 until 256).map { arrayListOf<Box>() }.toCollection(arrayListOf()) }.toCollection(arrayListOf()) |
| 21 | + |
| 22 | +private var mousePosX: Double = 0.0 |
| 23 | +private var mousePosY: Double = 0.0 |
| 24 | +private var mouseOldX: Double = 0.0 |
| 25 | +private var mouseOldY: Double = 0.0 |
| 26 | + |
| 27 | +val noise = FastNoise(Random().nextInt()) |
| 28 | + |
| 29 | +// Coloring threshold |
| 30 | +var lowValueColoring = 0.0 |
| 31 | +var highValueColoring = 0.0 |
| 32 | + |
| 33 | +// Filtering threshold |
| 34 | +var lowValueFiltering = 0.0 |
| 35 | +var highValueFiltering = 0.0 |
| 36 | + |
| 37 | +var enableColoring: BooleanProperty = false.toProperty() |
| 38 | +var enableFiltering: BooleanProperty = false.toProperty() |
| 39 | + |
| 40 | +class NoiseVisualizer : Application() { |
| 41 | + |
| 42 | + override fun start(primaryStage: Stage) { |
| 43 | + println(blockMap) |
| 44 | + val root = Group() |
| 45 | + for (x in 0 until 16) { |
| 46 | + for (y in 0 until 128) { |
| 47 | + for (z in 0 until 16) { |
| 48 | + // Creating an object of the class Box |
| 49 | + val box = Box() |
| 50 | + box.setOnMouseMoved { } |
| 51 | + box.width = 50.0 |
| 52 | + box.height = 50.0 |
| 53 | + box.depth = 50.0 |
| 54 | + box.userData = Triple(x, y, z) |
| 55 | + box.material = PhongMaterial() |
| 56 | + box.translateX = x * 70.0 |
| 57 | + box.translateY = y * 70.0 |
| 58 | + box.translateZ = z * 70.0 |
| 59 | + blockMap[x][y].add(z, box) |
| 60 | + root.children.add(box) |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + syncColor() |
| 65 | + |
| 66 | + val scene = Scene(root, 800.0, 600.0, true, SceneAntialiasing.BALANCED) |
| 67 | + |
| 68 | + val rotateX = Rotate(30.0, 500.0, 500.0, 500.0, Rotate.X_AXIS) |
| 69 | + val rotateY = Rotate(20.0, 500.0, 500.0, 500.0, Rotate.Y_AXIS) |
| 70 | + root.transforms.addAll(rotateX, rotateY) |
| 71 | + root.children.add(AmbientLight(Color.WHITE)) |
| 72 | + |
| 73 | + scene.setOnMousePressed { me -> |
| 74 | + mouseOldX = me.sceneX |
| 75 | + mouseOldY = me.sceneY |
| 76 | + } |
| 77 | + scene.setOnMouseDragged { me -> |
| 78 | + mousePosX = me.sceneX |
| 79 | + mousePosY = me.sceneY |
| 80 | + rotateX.angle = rotateX.angle - (mousePosY - mouseOldY) |
| 81 | + rotateY.angle = rotateY.angle + (mousePosX - mouseOldX) |
| 82 | + mouseOldX = mousePosX |
| 83 | + mouseOldY = mousePosY |
| 84 | + } |
| 85 | + val camera = PerspectiveCamera(true) |
| 86 | + camera.nearClip = 0.1 |
| 87 | + camera.farClip = 10000.0 |
| 88 | + camera.translateX = 0.0 |
| 89 | + camera.translateY = 0.0 |
| 90 | + camera.translateZ = -100.0 |
| 91 | + scene.setOnKeyPressed { |
| 92 | + val animation = TranslateTransition(Duration.millis(100.0)) |
| 93 | + animation.node = camera |
| 94 | + when (it.code) { |
| 95 | + KeyCode.W -> animation.byZ = 300.0 |
| 96 | + KeyCode.A -> animation.byX = -300.0 |
| 97 | + KeyCode.S -> animation.byZ = -300.0 |
| 98 | + KeyCode.D -> animation.byX = 300.0 |
| 99 | + } |
| 100 | + animation.play() |
| 101 | + } |
| 102 | + scene.fill = Color.WHITE |
| 103 | + scene.camera = camera |
| 104 | + |
| 105 | + primaryStage.scene = scene |
| 106 | + primaryStage.show() |
| 107 | + showConfigStage() |
| 108 | + } |
| 109 | + |
| 110 | + fun showConfigStage() { |
| 111 | + val stage = Stage() |
| 112 | + val pane = ConfigView().root |
| 113 | + val scene = Scene(pane, 600.0, 400.0) |
| 114 | + stage.scene = scene |
| 115 | + stage.show() |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +fun syncColor() { |
| 120 | + for (x in 0 until 16) { |
| 121 | + for (y in 0 until 128) { |
| 122 | + for (z in 0 until 16) { |
| 123 | + val box = blockMap[x][y][z] |
| 124 | + val noise = noise.GetNoise(x.toFloat(), y.toFloat(), z.toFloat()).toDouble() |
| 125 | + box.visibleProperty().value = true |
| 126 | + (box.material as PhongMaterial).diffuseColor = Color.grayRgb(abs(noise * 255).toInt()) |
| 127 | + if (enableFiltering.value) { |
| 128 | + if (noise !in lowValueFiltering..highValueFiltering) { |
| 129 | + box.visibleProperty().value = false |
| 130 | + } |
| 131 | + } |
| 132 | + if (enableColoring.value) { |
| 133 | + if (noise in lowValueColoring..highValueColoring) { |
| 134 | + (box.material as PhongMaterial).diffuseColor = Color.rgb(abs(noise * 255).toInt() + 1, 0, 0) |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +class ConfigView : View() { |
| 143 | + override val root = gridpane { |
| 144 | + paddingAll = 10.0 |
| 145 | + row { |
| 146 | + label { text = "Frequency" } |
| 147 | + val slider = slider(0, 1) |
| 148 | + slider.valueProperty().addListener { observable, oldValue, newValue -> noise.m_frequency = newValue.toFloat() } |
| 149 | + label(slider.valueProperty()) |
| 150 | + } |
| 151 | + row { |
| 152 | + label { text = "Noise type" } |
| 153 | + combobox(null, FastNoise.NoiseType.values().toList()) { |
| 154 | + value = FastNoise.NoiseType.Perlin |
| 155 | + selectionModel.selectedItemProperty().onChange { noise.m_noiseType = it ?: FastNoise.NoiseType.Perlin } |
| 156 | + } |
| 157 | + } |
| 158 | + row { |
| 159 | + label { text = "Interpolation (Only in Fractal noise)" } |
| 160 | + combobox(null, FastNoise.Interp.values().toList()) { |
| 161 | + value = FastNoise.Interp.Quintic |
| 162 | + selectionModel.selectedItemProperty().onChange { noise.m_interp = it ?: FastNoise.Interp.Quintic } |
| 163 | + } |
| 164 | + } |
| 165 | + row { |
| 166 | + checkbox { |
| 167 | + text = "Enable coloring" |
| 168 | + selectedProperty().value = false |
| 169 | + enableColoring.bind(selectedProperty()) |
| 170 | + } |
| 171 | + } |
| 172 | + row { |
| 173 | + label { text = "Light up threshold" } |
| 174 | + lateinit var range: Label |
| 175 | + RangeSlider(-1.0, 1.0, -0.4, 0.6).attachTo(this) { |
| 176 | + disableProperty().value = true |
| 177 | + enableColoring.onChange { disableProperty().set(!it) } |
| 178 | + lowValueProperty().onChange { lowValueColoring = it; range.text = "$lowValue~$highValue" } |
| 179 | + highValueProperty().onChange { highValueColoring = it; range.text = "$lowValue~$highValue" } |
| 180 | + } |
| 181 | + range = label() |
| 182 | + } |
| 183 | + row { |
| 184 | + checkbox { |
| 185 | + text = "Enable filtering" |
| 186 | + selectedProperty().value = false |
| 187 | + enableFiltering.bind(selectedProperty()) |
| 188 | + } |
| 189 | + } |
| 190 | + row { |
| 191 | + label { text = "Filtering threshold" } |
| 192 | + lateinit var range: Label |
| 193 | + RangeSlider(-1.0, 1.0, -0.4, 0.6).attachTo(this) { |
| 194 | + disableProperty().value = true |
| 195 | + enableFiltering.onChange { disableProperty().set(!it) } |
| 196 | + lowValueProperty().onChange { lowValueFiltering = it; range.text = "$lowValue~$highValue" } |
| 197 | + highValueProperty().onChange { highValueFiltering = it; range.text = "$lowValue~$highValue" } |
| 198 | + } |
| 199 | + range = label() |
| 200 | + } |
| 201 | + row { |
| 202 | + button("Rebuild").setOnMouseClicked { syncColor() } |
| 203 | + } |
| 204 | + } |
| 205 | +} |
| 206 | + |
| 207 | +fun main(args: Array<String>) { |
| 208 | + Application.launch(NoiseVisualizer::class.java, *args) |
| 209 | +} |
0 commit comments