diff --git a/README.md b/README.md index 24f5da977..b188ab2dc 100644 --- a/README.md +++ b/README.md @@ -1,42 +1,42 @@ -# ORX (OPENRNDR EXTRA) - -[![](https://jitpack.io/v/openrndr/orx.svg)](https://jitpack.io/#openrndr/orx) - -A growing library of assorted data structures, algorithms and utilities. - -- [`orx-camera`](orx-camera/README.md), 3d camera and controls -- [`orx-compositor`](orx-compositor/README.md), a simple toolkit to make composite (layered) images -- [`orx-filter-extension`](orx-filter-extension/README.md), Program extension method that provides Filter based `extend()` -- [`orx-integral-image`](orx-integral-image/README.md), a CPU-based implementation for integral images (summed area tables) -- `orx-jumpflood`, a filter/shader based implementation of the jump flood algorithm for finding fast approximate (directional) distance fields -- `orx-kdtree`, a kd-tree implementation for fast nearest point searches -- [`orx-mesh-generators`](orx-mesh-generators/README.md), triangular mesh generators -- [`orx-noise`](orx-noise/README.md), library for random number generation and noise -- [`orx-no-clear`](orx-no-clear/README.md), a simple extension that provides drawing without clearing the background -- [`orx-obj-loader`](orx-obj-loader/README.md), simple Wavefront .obj mesh loader - -## Usage -ORX 0.0.19 is built against OPENRNDR 0.3.32, make sure you use this version in your project. Because OPENRNDR's API is pre 1.0 it tends to change from time to time. - -The easiest way to add ORX to your project is through the use of Jitpack. [Jitpack](http://jitpack.io) is a service that pulls Gradle based libraries from Github, builds them and serves the jar files. - -To setup Jitpack support in your project all you have to do is add the Jitpack repository to your `repositories {}`. It is advised to have the jitpack repository as the last entry. -``` -repositories { - maven { url 'https://jitpack.io' } -} -``` - -You can then add any of the ORX artefacts to your `dependencies {}`: -``` -dependencies { - compile 'com.github.openrndr.orx::v0.0.19' -} -``` - -For example if you want to use the `orx-no-clear` artifact one would use: -``` -dependencies { - compile 'com.github.openrndr.orx:orx-no-clear:v0.0.19' -} -``` +# ORX (OPENRNDR EXTRA) + +[![](https://jitpack.io/v/openrndr/orx.svg)](https://jitpack.io/#openrndr/orx) + +A growing library of assorted data structures, algorithms and utilities. + +- [`orx-camera`](orx-camera/README.md), 3d camera and controls +- [`orx-compositor`](orx-compositor/README.md), a simple toolkit to make composite (layered) images +- [`orx-filter-extension`](orx-filter-extension/README.md), Program extension method that provides Filter based `extend()` +- [`orx-integral-image`](orx-integral-image/README.md), a CPU-based implementation for integral images (summed area tables) +- `orx-jumpflood`, a filter/shader based implementation of the jump flood algorithm for finding fast approximate (directional) distance fields +- `orx-kdtree`, a kd-tree implementation for fast nearest point searches +- [`orx-mesh-generators`](orx-mesh-generators/README.md), triangular mesh generators +- [`orx-noise`](orx-noise/README.md), library for random number generation and noise +- [`orx-no-clear`](orx-no-clear/README.md), a simple extension that provides drawing without clearing the background +- [`orx-obj-loader`](orx-obj-loader/README.md), simple Wavefront .obj mesh loader + +## Usage +ORX 0.0.19 is built against OPENRNDR 0.3.32, make sure you use this version in your project. Because OPENRNDR's API is pre 1.0 it tends to change from time to time. + +The easiest way to add ORX to your project is through the use of Jitpack. [Jitpack](http://jitpack.io) is a service that pulls Gradle based libraries from Github, builds them and serves the jar files. + +To setup Jitpack support in your project all you have to do is add the Jitpack repository to your `repositories {}`. It is advised to have the jitpack repository as the last entry. +``` +repositories { + maven { url 'https://jitpack.io' } +} +``` + +You can then add any of the ORX artefacts to your `dependencies {}`: +``` +dependencies { + compile 'com.github.openrndr.orx::v0.0.19' +} +``` + +For example if you want to use the `orx-no-clear` artifact one would use: +``` +dependencies { + compile 'com.github.openrndr.orx:orx-no-clear:v0.0.19' +} +``` diff --git a/ShaderError.txt b/ShaderError.txt new file mode 100644 index 000000000..ffda7e1a0 --- /dev/null +++ b/ShaderError.txt @@ -0,0 +1,24 @@ +#version 330 core + +uniform sampler2D tex0; +in vec2 v_texCoord0; + +out vec4 o_color; + +void main() { + vec2 step = 1.0 / textureSize(tex0, 0); + float ref = step(0.5 , texture(tex0, v_texCoord0).r); + vec4 outc = vec4(-1.0, -1.0, 0.0, 1.0); + + float contour = 0.0; + for (y = -1; y <= 1; ++y) { + for (x = -1; x <= 1; ++x) { + float smp = step(0.5, texture(tex0, v_texCoord0 + vec2(x,y) * step).r); + if (smp != ref) { + contour = 1.0; + } + } + } + + o_color = vec4(contour, contour, contour, 1.0); +} \ No newline at end of file diff --git a/build.gradle b/build.gradle index fe20fff46..989870562 100644 --- a/build.gradle +++ b/build.gradle @@ -1,67 +1,67 @@ -plugins { - id 'org.jetbrains.kotlin.jvm' version '1.3.10' -} - -allprojects { - group 'org.openrndr.extra' - version '0.0.19' -} - -repositories { - mavenLocal() - mavenCentral() -} - -ext { - openrndrVersion = "0.3.32-rc1" -} - -subprojects { - - apply plugin: 'kotlin' - apply plugin: 'maven' - apply plugin: 'maven-publish' - repositories { - mavenLocal() - mavenCentral() - maven { - url = "https://dl.bintray.com/openrndr/openrndr" - } - } - - dependencies { - compile "org.openrndr:openrndr-core:$openrndrVersion" - compile "org.openrndr:openrndr-filter:$openrndrVersion" - compile "org.openrndr:openrndr-shape:$openrndrVersion" - compile group: 'org.jetbrains.kotlinx', name: 'kotlinx-coroutines-core', version: '1.0.1' - } - - publishing { - publications { - mavenJava(MavenPublication) { - from components.java - - artifact sourceJar - } - } - } - - task sourceJar(type: Jar) { - classifier = 'sources' - from sourceSets.main.kotlin - } - - - -} - -dependencies { - compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8" -} - -compileKotlin { - kotlinOptions.jvmTarget = "1.8" -} -compileTestKotlin { - kotlinOptions.jvmTarget = "1.8" +plugins { + id 'org.jetbrains.kotlin.jvm' version '1.3.10' +} + +allprojects { + group 'org.openrndr.extra' + version '0.0.20' +} + +repositories { + mavenLocal() + mavenCentral() +} + +ext { + openrndrVersion = "0.3.32-rc1" +} + +subprojects { + + apply plugin: 'kotlin' + apply plugin: 'maven' + apply plugin: 'maven-publish' + repositories { + mavenLocal() + mavenCentral() + maven { + url = "https://dl.bintray.com/openrndr/openrndr" + } + } + + dependencies { + compile "org.openrndr:openrndr-core:$openrndrVersion" + compile "org.openrndr:openrndr-filter:$openrndrVersion" + compile "org.openrndr:openrndr-shape:$openrndrVersion" + compile group: 'org.jetbrains.kotlinx', name: 'kotlinx-coroutines-core', version: '1.0.1' + } + + publishing { + publications { + mavenJava(MavenPublication) { + from components.java + + artifact sourceJar + } + } + } + + task sourceJar(type: Jar) { + classifier = 'sources' + from sourceSets.main.kotlin + } + + + +} + +dependencies { + compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8" +} + +compileKotlin { + kotlinOptions.jvmTarget = "1.8" +} +compileTestKotlin { + kotlinOptions.jvmTarget = "1.8" } \ No newline at end of file diff --git a/orx-camera/README.md b/orx-camera/README.md index 17b752cc4..c9acabc9b 100644 --- a/orx-camera/README.md +++ b/orx-camera/README.md @@ -1,3 +1,3 @@ -# orx-camera - +# orx-camera + 3D camera and controls for OPENRNDR. This supersedes the to be deprecated functionality in OPENRNDR. \ No newline at end of file diff --git a/orx-camera/src/main/kotlin/Debug3D.kt b/orx-camera/src/main/kotlin/Debug3D.kt index d29786957..f19878c32 100644 --- a/orx-camera/src/main/kotlin/Debug3D.kt +++ b/orx-camera/src/main/kotlin/Debug3D.kt @@ -1,74 +1,74 @@ -package org.openrndr.extras.camera - -import org.openrndr.Extension -import org.openrndr.Program -import org.openrndr.color.ColorRGBa -import org.openrndr.draw.* -import org.openrndr.math.Matrix44 -import org.openrndr.math.Vector3 - -@Suppress("unused") -class Debug3D(eye: Vector3 = Vector3(0.0, 0.0, 10.0), lookAt: Vector3 = Vector3.ZERO, private val fov: Double = 90.0) : Extension { - - override var enabled: Boolean = true - var showGrid = false - val orbitalCamera = OrbitalCamera(eye, lookAt) - private val orbitalControls = OrbitalControls(orbitalCamera) - private var lastSeconds: Double = -1.0 - - private val grid = vertexBuffer( - vertexFormat { - position(3) - } - , 4 * 21).apply { - put { - for (x in -10..10) { - write(Vector3(x.toDouble(), 0.0, -10.0)) - write(Vector3(x.toDouble(), 0.0, 10.0)) - write(Vector3(-10.0, 0.0, x.toDouble())) - write(Vector3(10.0, 0.0, x.toDouble())) - } - } - } - - override fun beforeDraw(drawer: Drawer, program: Program) { - if (lastSeconds == -1.0) lastSeconds = program.seconds - - val delta = program.seconds - lastSeconds - lastSeconds = program.seconds - orbitalCamera.update(delta) - - drawer.background(ColorRGBa.BLACK) - drawer.perspective(fov, program.window.size.x / program.window.size.y, 0.1, 1000.0) - drawer.view = orbitalCamera.viewMatrix() - - if (showGrid) { - drawer.isolated { - drawer.fill = ColorRGBa.WHITE - drawer.stroke = ColorRGBa.WHITE - drawer.vertexBuffer(grid, DrawPrimitive.LINES) - - // Axis cross - drawer.fill = ColorRGBa.RED - drawer.lineSegment(Vector3.ZERO, Vector3.UNIT_X) - - drawer.fill = ColorRGBa.GREEN - drawer.lineSegment(Vector3.ZERO, Vector3.UNIT_Y) - - drawer.fill = ColorRGBa.BLUE - drawer.lineSegment(Vector3.ZERO, Vector3.UNIT_Z) - } - } - } - - override fun afterDraw(drawer: Drawer, program: Program) { - drawer.isolated { - drawer.view = Matrix44.IDENTITY - drawer.ortho() - } - } - - override fun setup(program: Program) { - orbitalControls.setup(program) - } +package org.openrndr.extras.camera + +import org.openrndr.Extension +import org.openrndr.Program +import org.openrndr.color.ColorRGBa +import org.openrndr.draw.* +import org.openrndr.math.Matrix44 +import org.openrndr.math.Vector3 + +@Suppress("unused") +class Debug3D(eye: Vector3 = Vector3(0.0, 0.0, 10.0), lookAt: Vector3 = Vector3.ZERO, fov:Double = 90.0) : Extension { + + override var enabled: Boolean = true + var showGrid = false + val orbitalCamera = OrbitalCamera(eye, lookAt, 90.0) + private val orbitalControls = OrbitalControls(orbitalCamera) + private var lastSeconds: Double = -1.0 + + private val grid = vertexBuffer( + vertexFormat { + position(3) + } + , 4 * 21).apply { + put { + for (x in -10..10) { + write(Vector3(x.toDouble(), 0.0, -10.0)) + write(Vector3(x.toDouble(), 0.0, 10.0)) + write(Vector3(-10.0, 0.0, x.toDouble())) + write(Vector3(10.0, 0.0, x.toDouble())) + } + } + } + + override fun beforeDraw(drawer: Drawer, program: Program) { + if (lastSeconds == -1.0) lastSeconds = program.seconds + + val delta = program.seconds - lastSeconds + lastSeconds = program.seconds + orbitalCamera.update(delta) + + drawer.background(ColorRGBa.BLACK) + drawer.perspective(orbitalCamera.fov, program.window.size.x / program.window.size.y, 0.1, 1000.0) + drawer.view = orbitalCamera.viewMatrix() + + if (showGrid) { + drawer.isolated { + drawer.fill = ColorRGBa.WHITE + drawer.stroke = ColorRGBa.WHITE + drawer.vertexBuffer(grid, DrawPrimitive.LINES) + + // Axis cross + drawer.fill = ColorRGBa.RED + drawer.lineSegment(Vector3.ZERO, Vector3.UNIT_X) + + drawer.fill = ColorRGBa.GREEN + drawer.lineSegment(Vector3.ZERO, Vector3.UNIT_Y) + + drawer.fill = ColorRGBa.BLUE + drawer.lineSegment(Vector3.ZERO, Vector3.UNIT_Z) + } + } + } + + override fun afterDraw(drawer: Drawer, program: Program) { + drawer.isolated { + drawer.view = Matrix44.IDENTITY + drawer.ortho() + } + } + + override fun setup(program: Program) { + orbitalControls.setup(program) + } } \ No newline at end of file diff --git a/orx-camera/src/main/kotlin/OrbitalCamera.kt b/orx-camera/src/main/kotlin/OrbitalCamera.kt index 15e210ade..dbc8117b1 100644 --- a/orx-camera/src/main/kotlin/OrbitalCamera.kt +++ b/orx-camera/src/main/kotlin/OrbitalCamera.kt @@ -1,120 +1,139 @@ -package org.openrndr.extras.camera - -import org.openrndr.* -import org.openrndr.color.ColorRGBa -import org.openrndr.draw.DrawPrimitive -import org.openrndr.draw.Drawer -import org.openrndr.draw.isolated -import org.openrndr.draw.vertexBuffer -import org.openrndr.draw.vertexFormat -import org.openrndr.math.Matrix44 -import org.openrndr.math.Spherical -import org.openrndr.math.Vector2 -import org.openrndr.math.Vector3 -import org.openrndr.math.transforms.lookAt as lookAt_ - -class OrbitalCamera(eye: Vector3, lookAt: Vector3) { - - // current position in spherical coordinates - var spherical = Spherical.fromVector(eye) - private set - var lookAt = lookAt - private set - - private var sphericalEnd = Spherical.fromVector(eye) - private var lookAtEnd = lookAt.copy() - private var dirty: Boolean = true - - var dampingFactor = 0.05 - var zoomSpeed = 1.0 - - fun rotate(rotX: Double, rotY: Double) { - sphericalEnd += Spherical(0.0, rotX, rotY) - sphericalEnd = sphericalEnd.makeSafe() - dirty = true - } - - fun rotateTo(rotX: Double, rotY: Double) { - sphericalEnd = sphericalEnd.copy(theta = rotX, phi = rotY) - sphericalEnd = sphericalEnd.makeSafe() - dirty = true - } - - fun rotateTo(eye: Vector3) { - sphericalEnd = Spherical.fromVector(eye) - sphericalEnd = sphericalEnd.makeSafe() - dirty = true - } - - fun dollyIn() { - val zoomScale = Math.pow(0.95, zoomSpeed) - dolly(sphericalEnd.radius * zoomScale - sphericalEnd.radius) - } - - fun dollyOut() { - val zoomScale = Math.pow(0.95, zoomSpeed) - dolly(sphericalEnd.radius / zoomScale - sphericalEnd.radius) - } - - private fun dolly(distance: Double) { - sphericalEnd += Spherical(distance, 0.0, 0.0) - dirty = true - } - - fun pan(x: Double, y: Double, z: Double) { - val view = viewMatrix() - val xColumn = Vector3(view.c0r0, view.c1r0, view.c2r0) * x - val yColumn = Vector3(view.c0r1, view.c1r1, view.c2r1) * y - val zColumn = Vector3(view.c0r2, view.c1r2, view.c2r2) * z - lookAtEnd += xColumn + yColumn + zColumn - dirty = true - } - - fun panTo(target : Vector3) { - lookAtEnd = target - dirty = true - } - - fun dollyTo(distance: Double) { - sphericalEnd = sphericalEnd.copy(radius = distance ) - dirty = true - } - - fun update(timeDelta: Double) { - if (!dirty) return - dirty = false - - val dampingFactor = dampingFactor * timeDelta / 0.0060 - val sphericalDelta = sphericalEnd - spherical - val lookAtDelta = lookAtEnd - lookAt - - if ( - Math.abs(sphericalEnd.radius) > EPSILON || - Math.abs(sphericalEnd.theta) > EPSILON || - Math.abs(sphericalEnd.phi) > EPSILON || - Math.abs(lookAtDelta.x) > EPSILON || - Math.abs(lookAtDelta.y) > EPSILON || - Math.abs(lookAtDelta.z) > EPSILON - ) { - - spherical += (sphericalDelta * dampingFactor) - lookAt += (lookAtDelta * dampingFactor) - dirty = true - - } else { - spherical = sphericalEnd.copy() - lookAt = lookAtEnd.copy() - } - spherical = spherical.makeSafe() - } - - fun viewMatrix(): Matrix44 { - return lookAt_(Vector3.fromSpherical(spherical) + lookAt, lookAt, Vector3.UNIT_Y) - } - - companion object { - private const val EPSILON = 0.000001 - } -} - - +package org.openrndr.extras.camera + +import org.openrndr.* +import org.openrndr.color.ColorRGBa +import org.openrndr.draw.DrawPrimitive +import org.openrndr.draw.Drawer +import org.openrndr.draw.isolated +import org.openrndr.draw.vertexBuffer +import org.openrndr.draw.vertexFormat +import org.openrndr.math.Matrix44 +import org.openrndr.math.Spherical +import org.openrndr.math.Vector2 +import org.openrndr.math.Vector3 +import org.openrndr.math.transforms.lookAt as lookAt_ + +class OrbitalCamera(eye: Vector3, lookAt: Vector3, var fov:Double) { + + + // current position in spherical coordinates + var spherical = Spherical.fromVector(eye) + private set + var lookAt = lookAt + private set + + private var sphericalEnd = Spherical.fromVector(eye) + private var lookAtEnd = lookAt.copy() + private var dirty: Boolean = true + + var fovEnd = fov + + var dampingFactor = 0.05 + var zoomSpeed = 1.0 + + fun setView(lookAt: Vector3, spherical: Spherical, fov:Double) { + this.lookAt = lookAt + this.lookAtEnd = lookAt + this.spherical = spherical + this.sphericalEnd = spherical + this.fov = fov + this.fovEnd= fov + } + + fun rotate(rotX: Double, rotY: Double) { + sphericalEnd += Spherical(0.0, rotX, rotY) + sphericalEnd = sphericalEnd.makeSafe() + dirty = true + } + + fun rotateTo(rotX: Double, rotY: Double) { + sphericalEnd = sphericalEnd.copy(theta = rotX, phi = rotY) + sphericalEnd = sphericalEnd.makeSafe() + dirty = true + } + + fun rotateTo(eye: Vector3) { + sphericalEnd = Spherical.fromVector(eye) + sphericalEnd = sphericalEnd.makeSafe() + dirty = true + } + + fun dollyIn() { + val zoomScale = Math.pow(0.95, zoomSpeed) + dolly(sphericalEnd.radius * zoomScale - sphericalEnd.radius) + } + + fun dollyOut() { + val zoomScale = Math.pow(0.95, zoomSpeed) + dolly(sphericalEnd.radius / zoomScale - sphericalEnd.radius) + } + + fun dolly(distance: Double) { + sphericalEnd += Spherical(distance, 0.0, 0.0) + dirty = true + } + + fun pan(x: Double, y: Double, z: Double) { + val view = viewMatrix() + val xColumn = Vector3(view.c0r0, view.c1r0, view.c2r0) * x + val yColumn = Vector3(view.c0r1, view.c1r1, view.c2r1) * y + val zColumn = Vector3(view.c0r2, view.c1r2, view.c2r2) * z + lookAtEnd += xColumn + yColumn + zColumn + dirty = true + } + + fun panTo(target : Vector3) { + lookAtEnd = target + dirty = true + } + + fun dollyTo(distance: Double) { + sphericalEnd = sphericalEnd.copy(radius = distance ) + dirty = true + } + + fun zoom(degrees: Double) { + fovEnd += degrees + } + + fun update(timeDelta: Double) { + if (!dirty) return + dirty = false + + val dampingFactor = dampingFactor * timeDelta / 0.0060 + val sphericalDelta = sphericalEnd - spherical + val lookAtDelta = lookAtEnd - lookAt + val fovDelta = fovEnd - fov + if ( + Math.abs(sphericalEnd.radius) > EPSILON || + Math.abs(sphericalEnd.theta) > EPSILON || + Math.abs(sphericalEnd.phi) > EPSILON || + Math.abs(lookAtDelta.x) > EPSILON || + Math.abs(lookAtDelta.y) > EPSILON || + Math.abs(lookAtDelta.z) > EPSILON || + Math.abs(fovDelta) > EPSILON + ) { + + fov += (fovDelta * dampingFactor) + spherical += (sphericalDelta * dampingFactor) + spherical = spherical.makeSafe() + lookAt += (lookAtDelta * dampingFactor) + dirty = true + + } else { + spherical = sphericalEnd.copy() + lookAt = lookAtEnd.copy() + } + spherical = spherical.makeSafe() + } + + fun viewMatrix(): Matrix44 { + return lookAt_(Vector3.fromSpherical(spherical) + lookAt, lookAt, Vector3.UNIT_Y) + } + + companion object { + private const val EPSILON = 0.000001 + } +} + + diff --git a/orx-camera/src/main/kotlin/OrbitalControls.kt b/orx-camera/src/main/kotlin/OrbitalControls.kt index 10ef3c799..24ce23a57 100644 --- a/orx-camera/src/main/kotlin/OrbitalControls.kt +++ b/orx-camera/src/main/kotlin/OrbitalControls.kt @@ -1,100 +1,128 @@ -package org.openrndr.extras.camera - -import org.openrndr.* -import org.openrndr.math.Vector2 -import org.openrndr.math.Vector3 - -class OrbitalControls(val orbitalCamera: OrbitalCamera) { - enum class STATE { - NONE, - ROTATE, - PAN, - } - - private var state = STATE.NONE - var fov = 90.0 - - private lateinit var program: Program - private lateinit var lastMousePosition: Vector2 - - private fun mouseScrolled(event: MouseEvent) { - - if (Math.abs(event.rotation.x) > 0.1) return - - when { - event.rotation.y > 0 -> orbitalCamera.dollyIn() - event.rotation.y < 0 -> orbitalCamera.dollyOut() - } - } - - private fun mouseMoved(event: MouseEvent) { - - if (state == STATE.NONE) return - val delta = lastMousePosition - event.position - lastMousePosition = event.position - - if (state == STATE.PAN) { - - val offset = Vector3.fromSpherical(orbitalCamera.spherical) - orbitalCamera.lookAt - - // half of the fov is center to top of screen - val targetDistance = offset.length * Math.tan((fov / 2) * Math.PI / 180) - val panX = (2 * delta.x * targetDistance / program.window.size.x) - val panY = (2 * delta.y * targetDistance / program.window.size.y) - - orbitalCamera.pan(panX, -panY, 0.0) - - } else { - val rotX = 2 * Math.PI * delta.x / program.window.size.x - val rotY = 2 * Math.PI * delta.y / program.window.size.y - orbitalCamera.rotate(rotX, rotY) - } - - } - - private fun mouseButtonDown(event: MouseEvent) { - val previousState = state - - when (event.button) { - MouseButton.LEFT -> { - state = STATE.ROTATE - } - MouseButton.RIGHT -> { - state = STATE.PAN - } - MouseButton.CENTER -> { - } - MouseButton.NONE -> { - } - } - - if (previousState == STATE.NONE) { - lastMousePosition = event.position - } - } - - fun keyPressed(keyEvent: KeyEvent) { - if (keyEvent.key == KEY_ARROW_RIGHT) { - orbitalCamera.pan(1.0, 0.0, 0.0) - } - if (keyEvent.key == KEY_ARROW_LEFT) { - orbitalCamera.pan(-1.0, 0.0, 0.0) - } - if (keyEvent.key == KEY_ARROW_UP) { - orbitalCamera.pan(0.0, 1.0, 0.0) - } - if (keyEvent.key == KEY_ARROW_DOWN) { - orbitalCamera.pan(0.0, -1.0, 0.0) - } - } - - fun setup(program: Program) { - this.program = program - program.mouse.moved.listen { mouseMoved(it) } - program.mouse.buttonDown.listen { mouseButtonDown(it) } - program.mouse.buttonUp.listen { state = STATE.NONE } - program.mouse.scrolled.listen { mouseScrolled(it) } - program.keyboard.keyDown.listen { keyPressed(it) } - program.keyboard.keyRepeat.listen{ keyPressed(it) } - } -} +package org.openrndr.extras.camera + +import org.openrndr.* +import org.openrndr.math.Vector2 +import org.openrndr.math.Vector3 + +class OrbitalControls(val orbitalCamera: OrbitalCamera) { + enum class STATE { + NONE, + ROTATE, + PAN, + } + + private var state = STATE.NONE + var fov = 90.0 + + private lateinit var program: Program + private lateinit var lastMousePosition: Vector2 + + private fun mouseScrolled(event: MouseEvent) { + + if (Math.abs(event.rotation.x) > 0.1) return + + when { + event.rotation.y > 0 -> orbitalCamera.dollyIn() + event.rotation.y < 0 -> orbitalCamera.dollyOut() + } + } + + private fun mouseMoved(event: MouseEvent) { + + if (state == STATE.NONE) return + val delta = lastMousePosition - event.position + lastMousePosition = event.position + + if (state == STATE.PAN) { + + val offset = Vector3.fromSpherical(orbitalCamera.spherical) - orbitalCamera.lookAt + + // half of the fov is center to top of screen + val targetDistance = offset.length * Math.tan((fov / 2) * Math.PI / 180) + val panX = (2 * delta.x * targetDistance / program.window.size.x) + val panY = (2 * delta.y * targetDistance / program.window.size.y) + + orbitalCamera.pan(panX, -panY, 0.0) + + } else { + val rotX = 2 * Math.PI * delta.x / program.window.size.x + val rotY = 2 * Math.PI * delta.y / program.window.size.y + orbitalCamera.rotate(rotX, rotY) + } + + } + + private fun mouseButtonDown(event: MouseEvent) { + val previousState = state + + when (event.button) { + MouseButton.LEFT -> { + state = STATE.ROTATE + } + MouseButton.RIGHT -> { + state = STATE.PAN + } + MouseButton.CENTER -> { + } + MouseButton.NONE -> { + } + } + + if (previousState == STATE.NONE) { + lastMousePosition = event.position + } + } + + fun keyPressed(keyEvent: KeyEvent) { + if (keyEvent.key == KEY_ARROW_RIGHT) { + orbitalCamera.pan(1.0, 0.0, 0.0) + } + if (keyEvent.key == KEY_ARROW_LEFT) { + orbitalCamera.pan(-1.0, 0.0, 0.0) + } + if (keyEvent.key == KEY_ARROW_UP) { + orbitalCamera.pan(0.0, 1.0, 0.0) + } + if (keyEvent.key == KEY_ARROW_DOWN) { + orbitalCamera.pan(0.0, -1.0, 0.0) + } + + if (keyEvent.name == "q") { + orbitalCamera.pan(0.0, -1.0, 0.0) + } + if (keyEvent.name == "e") { + orbitalCamera.pan(0.0, 1.0, 0.0) + } + if (keyEvent.name == "w") { + orbitalCamera.pan(0.0, 0.0, -1.0) + } + if (keyEvent.name == "s") { + orbitalCamera.pan(0.0, 0.0, 1.0) + } + if (keyEvent.name == "a") { + orbitalCamera.pan(-1.0, 0.0, 0.0) + } + if (keyEvent.name == "d") { + orbitalCamera.pan(1.0, 0.0, 0.0) + } + + if (keyEvent.key == KEY_PAGE_UP) { + orbitalCamera.zoom(1.0) + } + + if (keyEvent.key == KEY_PAGE_DOWN) { + orbitalCamera.zoom(-1.0) + } + + } + + fun setup(program: Program) { + this.program = program + program.mouse.moved.listen { mouseMoved(it) } + program.mouse.buttonDown.listen { mouseButtonDown(it) } + program.mouse.buttonUp.listen { state = STATE.NONE } + program.mouse.scrolled.listen { mouseScrolled(it) } + program.keyboard.keyDown.listen { keyPressed(it) } + program.keyboard.keyRepeat.listen{ keyPressed(it) } + } +} diff --git a/orx-compositor/src/main/kotlin/Compositor.kt b/orx-compositor/src/main/kotlin/Compositor.kt index a7c383236..bf5f94c36 100644 --- a/orx-compositor/src/main/kotlin/Compositor.kt +++ b/orx-compositor/src/main/kotlin/Compositor.kt @@ -1,138 +1,138 @@ -package org.openrndr.extra.compositor - -import org.openrndr.color.ColorRGBa -import org.openrndr.draw.* -import org.openrndr.math.Matrix44 - - -private val postBufferCache = mutableListOf() - -fun RenderTarget.deepDestroy() { - val cbcopy = colorBuffers.map { it} - val dbcopy = depthBuffer - detachDepthBuffer() - detachColorBuffers() - cbcopy.forEach { - it.destroy() - } - dbcopy?.destroy() - destroy() -} - -/** - * A single layer representation - */ -class Layer internal constructor() { - var drawFunc: () -> Unit = {} - val children: MutableList = mutableListOf() - var blendFilter: Pair Unit>? = null - val postFilters: MutableList Unit>> = mutableListOf() - - private var layerTarget:RenderTarget? = null - - /** - * draw the layer - */ - fun draw(drawer: Drawer) { - val rt = RenderTarget.active - - val llt = layerTarget - if (llt == null || (llt.width != rt.width || llt.height != rt.height)) { - layerTarget?.deepDestroy() - layerTarget = renderTarget(rt.width, rt.height) { - colorBuffer() - depthBuffer() - } - } - - layerTarget?.let { target -> - drawer.isolatedWithTarget(target) { - drawer.background(ColorRGBa.TRANSPARENT) - drawFunc() - children.forEach { - it.draw(drawer) - } - } - - if (postFilters.size > 0) { - val sizeMismatch = if (postBufferCache.isNotEmpty()) { - postBufferCache[0].width != rt.width || postBufferCache[0].height != rt.height - } else { - false - } - - if (sizeMismatch) { - postBufferCache.forEach { it.destroy() } - postBufferCache.clear() - } - - if (postBufferCache.isEmpty()) { - postBufferCache += colorBuffer(rt.width, rt.height) - postBufferCache += colorBuffer(rt.width, rt.height) - } - } - - val layerPost = postFilters.let { filters -> - val targets = postBufferCache - val result = filters.foldIndexed(target.colorBuffer(0)) { i, source, filter -> - val target = targets[i % targets.size] - filter.first.apply(filter.second) - filter.first.apply(source, target) - target - } - result - } - - val lblend = blendFilter - if (lblend == null) { - drawer.isolatedWithTarget(rt) { - //drawer.ortho(rt) - drawer.ortho() - drawer.view = Matrix44.IDENTITY - drawer.model = Matrix44.IDENTITY - drawer.image(layerPost, layerPost.bounds, drawer.bounds) - } - } else { - lblend.first.apply(lblend.second) - lblend.first.apply(arrayOf(rt.colorBuffer(0), layerPost), rt.colorBuffer(0)) - } - } - } -} - -/** - * create a layer within the composition - */ -fun Layer.layer(function: Layer.() -> Unit) { - children.add(Layer().apply { function() }) -} - -/** - * set the draw contents of the layer - */ -fun Layer.draw(function: () -> Unit) { - drawFunc = function -} - -/** - * add a post-processing filter to the layer - */ -fun Layer.post(filter: F, configure: F.() -> Unit = {}) { - postFilters.add(Pair(filter as Filter, configure as Filter.() -> Unit)) -} - -/** - * add a blend filter to the layer - */ -fun Layer.blend(filter: F, configure: F.() -> Unit = {}) { - blendFilter = Pair(filter as Filter, configure as Filter.() -> Unit) -} - -/** - * create a layered composition - */ -fun compose(function: Layer.() -> Unit): Layer { - val root = Layer() - root.function() - return root +package org.openrndr.extra.compositor + +import org.openrndr.color.ColorRGBa +import org.openrndr.draw.* +import org.openrndr.math.Matrix44 + + +private val postBufferCache = mutableListOf() + +fun RenderTarget.deepDestroy() { + val cbcopy = colorBuffers.map { it} + val dbcopy = depthBuffer + detachDepthBuffer() + detachColorBuffers() + cbcopy.forEach { + it.destroy() + } + dbcopy?.destroy() + destroy() +} + +/** + * A single layer representation + */ +class Layer internal constructor() { + var drawFunc: () -> Unit = {} + val children: MutableList = mutableListOf() + var blendFilter: Pair Unit>? = null + val postFilters: MutableList Unit>> = mutableListOf() + + private var layerTarget:RenderTarget? = null + + /** + * draw the layer + */ + fun draw(drawer: Drawer) { + val rt = RenderTarget.active + + val llt = layerTarget + if (llt == null || (llt.width != rt.width || llt.height != rt.height)) { + layerTarget?.deepDestroy() + layerTarget = renderTarget(rt.width, rt.height) { + colorBuffer() + depthBuffer() + } + } + + layerTarget?.let { target -> + drawer.isolatedWithTarget(target) { + drawer.background(ColorRGBa.TRANSPARENT) + drawFunc() + children.forEach { + it.draw(drawer) + } + } + + if (postFilters.size > 0) { + val sizeMismatch = if (postBufferCache.isNotEmpty()) { + postBufferCache[0].width != rt.width || postBufferCache[0].height != rt.height + } else { + false + } + + if (sizeMismatch) { + postBufferCache.forEach { it.destroy() } + postBufferCache.clear() + } + + if (postBufferCache.isEmpty()) { + postBufferCache += colorBuffer(rt.width, rt.height) + postBufferCache += colorBuffer(rt.width, rt.height) + } + } + + val layerPost = postFilters.let { filters -> + val targets = postBufferCache + val result = filters.foldIndexed(target.colorBuffer(0)) { i, source, filter -> + val target = targets[i % targets.size] + filter.first.apply(filter.second) + filter.first.apply(source, target) + target + } + result + } + + val lblend = blendFilter + if (lblend == null) { + drawer.isolatedWithTarget(rt) { + //drawer.ortho(rt) + drawer.ortho() + drawer.view = Matrix44.IDENTITY + drawer.model = Matrix44.IDENTITY + drawer.image(layerPost, layerPost.bounds, drawer.bounds) + } + } else { + lblend.first.apply(lblend.second) + lblend.first.apply(arrayOf(rt.colorBuffer(0), layerPost), rt.colorBuffer(0)) + } + } + } +} + +/** + * create a layer within the composition + */ +fun Layer.layer(function: Layer.() -> Unit) { + children.add(Layer().apply { function() }) +} + +/** + * set the draw contents of the layer + */ +fun Layer.draw(function: () -> Unit) { + drawFunc = function +} + +/** + * add a post-processing filter to the layer + */ +fun Layer.post(filter: F, configure: F.() -> Unit = {}) { + postFilters.add(Pair(filter as Filter, configure as Filter.() -> Unit)) +} + +/** + * add a blend filter to the layer + */ +fun Layer.blend(filter: F, configure: F.() -> Unit = {}) { + blendFilter = Pair(filter as Filter, configure as Filter.() -> Unit) +} + +/** + * create a layered composition + */ +fun compose(function: Layer.() -> Unit): Layer { + val root = Layer() + root.function() + return root } \ No newline at end of file diff --git a/orx-examples/build.gradle b/orx-examples/build.gradle new file mode 100644 index 000000000..5ddae2504 --- /dev/null +++ b/orx-examples/build.gradle @@ -0,0 +1,31 @@ +plugins { + id 'org.jetbrains.kotlin.jvm' version '1.2.71' +} + +group 'org.openrndr.extra' +version '0.0.1' + +repositories { + mavenCentral() + maven { + url "https://dl.bintray.com/openrndr/openrndr" + } +} + +ext.openrndrVersion = "0.3.26" +ext.orxVersion = "0.0.5" +dependencies { + compile "org.openrndr.extra:orx:0.0.1" + compile "org.openrndr.extra:orx-jumpflood:$orxVersion" + compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8" + compile "org.openrndr:openrndr-core:$openrndrVersion" + runtime "org.openrndr:openrndr-gl3:$openrndrVersion" + runtime "org.openrndr:openrndr-gl3-natives-windows:$openrndrVersion" +} + +compileKotlin { + kotlinOptions.jvmTarget = "1.8" +} +compileTestKotlin { + kotlinOptions.jvmTarget = "1.8" +} \ No newline at end of file diff --git a/orx-examples/gradle/wrapper/gradle-wrapper.jar b/orx-examples/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 000000000..1948b9074 Binary files /dev/null and b/orx-examples/gradle/wrapper/gradle-wrapper.jar differ diff --git a/orx-examples/gradle/wrapper/gradle-wrapper.properties b/orx-examples/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 000000000..b1443f313 --- /dev/null +++ b/orx-examples/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Fri Oct 05 14:22:23 CEST 2018 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-4.8-all.zip diff --git a/orx-examples/gradlew b/orx-examples/gradlew new file mode 100644 index 000000000..cccdd3d51 --- /dev/null +++ b/orx-examples/gradlew @@ -0,0 +1,172 @@ +#!/usr/bin/env sh + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=$(save "$@") + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then + cd "$(dirname "$0")" +fi + +exec "$JAVACMD" "$@" diff --git a/orx-examples/gradlew.bat b/orx-examples/gradlew.bat new file mode 100644 index 000000000..e95643d6a --- /dev/null +++ b/orx-examples/gradlew.bat @@ -0,0 +1,84 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/orx-examples/settings.gradle b/orx-examples/settings.gradle new file mode 100644 index 000000000..b794b9266 --- /dev/null +++ b/orx-examples/settings.gradle @@ -0,0 +1,2 @@ +rootProject.name = 'orx-examples' + diff --git a/orx-examples/src/main/kotlin/jumpfil-001.kt b/orx-examples/src/main/kotlin/jumpfil-001.kt new file mode 100644 index 000000000..b4e688a91 --- /dev/null +++ b/orx-examples/src/main/kotlin/jumpfil-001.kt @@ -0,0 +1,56 @@ +import org.openrndr.Program +import org.openrndr.application +import org.openrndr.color.ColorRGBa +import org.openrndr.configuration +import org.openrndr.draw.ColorType +import org.openrndr.draw.colorBuffer +import org.openrndr.extra.jumpfill.EncodePoints +import org.openrndr.extra.jumpfill.JumpFlood + +class JumpFill001 : Program() { + + + var drawFunc = {} + override fun setup() { + + val encodePoints = EncodePoints() + val jumpFill = JumpFlood() + + val input = colorBuffer(512, 1024) + val coordinates = + listOf(colorBuffer(input.width, input.height, type = ColorType.FLOAT32), + colorBuffer(input.width, input.height, type = ColorType.FLOAT32)) + + for (i in 0 until 100) { + input.shadow[(Math.random() * input.width).toInt(), (Math.random() * input.height).toInt()] = + ColorRGBa.WHITE + } + input.shadow.upload() + + drawFunc = { + encodePoints.apply(input, coordinates[0]) + drawer.image(coordinates[0]) + jumpFill.maxSteps = 10 + for (i in 0 until 10) { + jumpFill.step = i + jumpFill.apply(coordinates[i % 2], coordinates[(i + 1) % 2]) + } + + drawer.image(coordinates[0]) + + } + } + + override fun draw() { + drawFunc() + } +} + +fun main(args: Array) { + application(JumpFill001(), configuration { + + width = 1024 + height = 1024 + + }) +} \ No newline at end of file diff --git a/orx-examples/src/main/kotlin/jumpfil-002.kt b/orx-examples/src/main/kotlin/jumpfil-002.kt new file mode 100644 index 000000000..0ebd8b754 --- /dev/null +++ b/orx-examples/src/main/kotlin/jumpfil-002.kt @@ -0,0 +1,45 @@ +import org.openrndr.Program +import org.openrndr.application +import org.openrndr.color.ColorRGBa +import org.openrndr.configuration +import org.openrndr.draw.colorBuffer +import org.openrndr.extra.jumpfill.contourPoints +import org.openrndr.extra.jumpfill.jumpFlood +import org.openrndr.extra.jumpfill.threshold + +class JumpFill002 : Program() { + + var drawFunc = {} + override fun setup() { + + val input = colorBuffer(512, 1024) + + for (i in 0 until 3) { + input.shadow[(Math.random() * input.width).toInt(), (Math.random() * input.height).toInt()] = + ColorRGBa.WHITE + } + input.shadow.upload() + + val result = jumpFlood(drawer, input) + +// threshold.apply(result, result) + // contourPoints.apply(result, result) + + drawFunc = { + drawer.image(result) + } + } + + override fun draw() { + drawFunc() + } +} + +fun main(args: Array) { + application(JumpFill002(), configuration { + + width = 1024 + height = 1024 + + }) +} \ No newline at end of file diff --git a/orx-integral-image/src/main/kotlin/FastIntegralImage.kt b/orx-integral-image/src/main/kotlin/FastIntegralImage.kt index feaad8f41..9aed460eb 100644 --- a/orx-integral-image/src/main/kotlin/FastIntegralImage.kt +++ b/orx-integral-image/src/main/kotlin/FastIntegralImage.kt @@ -1,77 +1,77 @@ -package org.openrndr.extra.integralimage - -import org.openrndr.draw.* -import org.openrndr.filter.blend.passthrough -import org.openrndr.math.Vector2 -import org.openrndr.resourceUrl - - -class FastIntegralImageFilter : Filter(filterShaderFromUrl(resourceUrl( - "/shaders/gl3/integral-image.frag" -))) { - var passIndex: Int by parameters - var passDirection: Vector2 by parameters - var sampleCount: Int by parameters - var sampleCountBase: Int by parameters -} - -class FastIntegralImage : Filter(filterShaderFromUrl(resourceUrl( - "/shaders/gl3/integral-image.frag" -))) { - - var intermediate: ColorBuffer? = null - val filter = FastIntegralImageFilter() - - private fun sampleCounts(size:Int, sampleCountBase:Int) : List { - var remainder = size - val sampleCounts = mutableListOf() - while (remainder > 0) { - sampleCounts += if (remainder >= sampleCountBase) { - sampleCountBase - } else { - remainder - } - remainder /= sampleCountBase - } - return sampleCounts - } - - override fun apply(source: Array, target: Array) { - - val sampleCountBase = 16 - val xSampleCounts = sampleCounts(source[0].width, sampleCountBase) - val ySampleCounts = sampleCounts(source[0].height, sampleCountBase) - - val li = intermediate - if (li == null || (li.width != source[0].width || li.height != source[0].height)) { - intermediate?.destroy() - intermediate = colorBuffer(source[0].width, source[0].height, 1.0, ColorFormat.RGBa, ColorType.FLOAT32) - } - - val targets = arrayOf(target, arrayOf(intermediate!!)) - - var targetIndex = 0 - - filter.sampleCountBase = sampleCountBase - - filter.passDirection = Vector2.UNIT_X - for (pass in 0 until xSampleCounts.size) { - filter.sampleCount = xSampleCounts[pass] - filter.passIndex = pass - filter.apply( if (pass == 0) source else targets[targetIndex%2], targets[(targetIndex+1)%2]) - targetIndex++ - } - - filter.passDirection = Vector2.UNIT_Y - for (pass in 0 until ySampleCounts.size) { - filter.sampleCount = ySampleCounts[pass] - filter.passIndex = pass - filter.apply( targets[targetIndex%2], targets[(targetIndex+1)%2]) - targetIndex++ - } - - if (targetIndex%2 == 1) { - passthrough.apply(targets[1], targets[0]) - } - } +package org.openrndr.extra.integralimage + +import org.openrndr.draw.* +import org.openrndr.filter.blend.passthrough +import org.openrndr.math.Vector2 +import org.openrndr.resourceUrl + + +class FastIntegralImageFilter : Filter(filterShaderFromUrl(resourceUrl( + "/shaders/gl3/integral-image.frag" +))) { + var passIndex: Int by parameters + var passDirection: Vector2 by parameters + var sampleCount: Int by parameters + var sampleCountBase: Int by parameters +} + +class FastIntegralImage : Filter(filterShaderFromUrl(resourceUrl( + "/shaders/gl3/integral-image.frag" +))) { + + var intermediate: ColorBuffer? = null + val filter = FastIntegralImageFilter() + + private fun sampleCounts(size:Int, sampleCountBase:Int) : List { + var remainder = size + val sampleCounts = mutableListOf() + while (remainder > 0) { + sampleCounts += if (remainder >= sampleCountBase) { + sampleCountBase + } else { + remainder + } + remainder /= sampleCountBase + } + return sampleCounts + } + + override fun apply(source: Array, target: Array) { + + val sampleCountBase = 16 + val xSampleCounts = sampleCounts(source[0].width, sampleCountBase) + val ySampleCounts = sampleCounts(source[0].height, sampleCountBase) + + val li = intermediate + if (li == null || (li.width != source[0].width || li.height != source[0].height)) { + intermediate?.destroy() + intermediate = colorBuffer(source[0].width, source[0].height, 1.0, ColorFormat.RGBa, ColorType.FLOAT32) + } + + val targets = arrayOf(target, arrayOf(intermediate!!)) + + var targetIndex = 0 + + filter.sampleCountBase = sampleCountBase + + filter.passDirection = Vector2.UNIT_X + for (pass in 0 until xSampleCounts.size) { + filter.sampleCount = xSampleCounts[pass] + filter.passIndex = pass + filter.apply( if (pass == 0) source else targets[targetIndex%2], targets[(targetIndex+1)%2]) + targetIndex++ + } + + filter.passDirection = Vector2.UNIT_Y + for (pass in 0 until ySampleCounts.size) { + filter.sampleCount = ySampleCounts[pass] + filter.passIndex = pass + filter.apply( targets[targetIndex%2], targets[(targetIndex+1)%2]) + targetIndex++ + } + + if (targetIndex%2 == 1) { + passthrough.apply(targets[1], targets[0]) + } + } } \ No newline at end of file diff --git a/orx-integral-image/src/main/resources/shaders/gl3/integral-image.frag b/orx-integral-image/src/main/resources/shaders/gl3/integral-image.frag index 0b3049201..a6dd5bdb6 100644 --- a/orx-integral-image/src/main/resources/shaders/gl3/integral-image.frag +++ b/orx-integral-image/src/main/resources/shaders/gl3/integral-image.frag @@ -1,26 +1,26 @@ -#version 330 core - -uniform sampler2D tex0; -in vec2 v_texCoord0; -out vec4 o_color; - -uniform int passIndex; -uniform int sampleCount; -uniform int sampleCountBase; -uniform vec2 passDirection; - - -void main() { - vec2 passOffset = vec2(pow(sampleCountBase, passIndex)) * (1.0/textureSize(tex0, 0)) * passDirection; - - vec2 uv0 = v_texCoord0; - vec4 result = vec4(0.0); - for (int i = 0; i < sampleCount; ++i) { - vec2 readUV = v_texCoord0 - vec2(i *passOffset); - float factor = step(0.0, readUV.x) * step(0.0, readUV.y); - result += factor * texture(tex0, readUV); - } - - o_color = result; - +#version 330 core + +uniform sampler2D tex0; +in vec2 v_texCoord0; +out vec4 o_color; + +uniform int passIndex; +uniform int sampleCount; +uniform int sampleCountBase; +uniform vec2 passDirection; + + +void main() { + vec2 passOffset = vec2(pow(sampleCountBase, passIndex)) * (1.0/textureSize(tex0, 0)) * passDirection; + + vec2 uv0 = v_texCoord0; + vec4 result = vec4(0.0); + for (int i = 0; i < sampleCount; ++i) { + vec2 readUV = v_texCoord0 - vec2(i *passOffset); + float factor = step(0.0, readUV.x) * step(0.0, readUV.y); + result += factor * texture(tex0, readUV); + } + + o_color = result; + } \ No newline at end of file diff --git a/orx-jumpflood/src/main/kotlin/JumpFlood.kt b/orx-jumpflood/src/main/kotlin/JumpFlood.kt index 41a978f6f..75c2153c1 100644 --- a/orx-jumpflood/src/main/kotlin/JumpFlood.kt +++ b/orx-jumpflood/src/main/kotlin/JumpFlood.kt @@ -1,128 +1,128 @@ -package org.openrndr.extra.jumpfill - -import org.openrndr.color.ColorRGBa -import org.openrndr.draw.* -import org.openrndr.math.Matrix44 -import org.openrndr.math.Vector2 -import org.openrndr.resourceUrl - -class EncodePoints : Filter(filterShaderFromUrl(resourceUrl("/shaders/gl3/encode-points.frag"))) -class JumpFlood : Filter(filterShaderFromUrl(resourceUrl("/shaders/gl3/jumpflood.frag"))) { - var maxSteps: Int by parameters - var step: Int by parameters -} - -class PixelDistance : Filter(filterShaderFromUrl(resourceUrl("/shaders/gl3/pixel-distance.frag"))) -class ContourPoints : Filter(filterShaderFromUrl(resourceUrl("/shaders/gl3/contour-points.frag"))) -class Threshold : Filter(filterShaderFromUrl(resourceUrl("/shaders/gl3/threshold.frag"))) { - var threshold by parameters - - init { - threshold = 0.5 - } -} - -val encodePoints by lazy { EncodePoints() } -val jumpFlood by lazy { JumpFlood() } -val pixelDistance by lazy { PixelDistance() } -val contourPoints by lazy { ContourPoints() } -val threshold by lazy { Threshold() } - - -class JumpFlooder(val width: Int, val height: Int) { - private val dimension = Math.max(width, height) - private val exp = Math.ceil(Math.log(dimension.toDouble()) / Math.log(2.0)).toInt() - private val squareDim = Math.pow(2.0, exp.toDouble()).toInt() - - private val coordinates = - listOf(colorBuffer(squareDim, squareDim, format = ColorFormat.RGB, type = ColorType.FLOAT32), - colorBuffer(squareDim, squareDim, format = ColorFormat.RGB, type = ColorType.FLOAT32)) - - private val final = renderTarget(width, height) { - colorBuffer(type = ColorType.FLOAT32) - } - - val result: ColorBuffer get() = final.colorBuffer(0) - - private val square = renderTarget(squareDim, squareDim) { - colorBuffer() - } - - private var contourUsed = false - private val thresholded by lazy { colorBuffer(width, height) } - private val edges by lazy { colorBuffer(width, height) } - - fun distanceToContour(drawer: Drawer, input: ColorBuffer, thresholdValue: Double = 0.5): ColorBuffer { - threshold.threshold = thresholdValue - threshold.apply(input, thresholded) - contourPoints.apply(thresholded, edges) - contourUsed = true - return jumpFlood(drawer, edges) - } - - fun directions(xRange: IntProgression = 0 until width, yRange: IntProgression = 0 until height): Array> { - result.shadow.download() - return result.shadow.mapIndexed(xRange, yRange) { _, _, r, g, _, _ -> Vector2(r, g) } - } - - - fun jumpFlood(drawer: Drawer, input: ColorBuffer): ColorBuffer { - if (input.width != width || input.height != height) { - throw IllegalArgumentException("dimensions mismatch") - } - - drawer.isolatedWithTarget(square) { - drawer.background(ColorRGBa.BLACK) - drawer.ortho(square) - drawer.view = Matrix44.IDENTITY - drawer.model = Matrix44.IDENTITY - drawer.image(input) - } - encodePoints.apply(square.colorBuffer(0), coordinates[0]) - - for (i in 0 until exp) { - jumpFlood.step = i - jumpFlood.apply(coordinates[i % 2], coordinates[(i + 1) % 2]) - } - - pixelDistance.apply( arrayOf(coordinates[exp % 2], thresholded), coordinates[exp % 2]) - drawer.isolatedWithTarget(final) { - drawer.background(ColorRGBa.BLACK) - drawer.ortho(final) - drawer.view = Matrix44.IDENTITY - drawer.model = Matrix44.IDENTITY - drawer.image(coordinates[exp % 2]) - } - return result - } - - fun destroy(destroyFinal: Boolean = true) { - coordinates.forEach { it.destroy() } - - square.colorBuffer(0).destroy() - square.detachColorBuffers() - square.destroy() - - if (destroyFinal) { - final.colorBuffer(0).destroy() - } - final.detachColorBuffers() - - final.destroy() - - if (contourUsed) { - edges.destroy() - thresholded.destroy() - } - - } - -} - -fun jumpFlood(drawer: Drawer, points: ColorBuffer): ColorBuffer { - val jumpFlooder = JumpFlooder(points.width, points.height) - jumpFlooder.jumpFlood(drawer, points) - val result = jumpFlooder.result - jumpFlooder.destroy(false) - return result +package org.openrndr.extra.jumpfill + +import org.openrndr.color.ColorRGBa +import org.openrndr.draw.* +import org.openrndr.math.Matrix44 +import org.openrndr.math.Vector2 +import org.openrndr.resourceUrl + +class EncodePoints : Filter(filterShaderFromUrl(resourceUrl("/shaders/gl3/encode-points.frag"))) +class JumpFlood : Filter(filterShaderFromUrl(resourceUrl("/shaders/gl3/jumpflood.frag"))) { + var maxSteps: Int by parameters + var step: Int by parameters +} + +class PixelDistance : Filter(filterShaderFromUrl(resourceUrl("/shaders/gl3/pixel-distance.frag"))) +class ContourPoints : Filter(filterShaderFromUrl(resourceUrl("/shaders/gl3/contour-points.frag"))) +class Threshold : Filter(filterShaderFromUrl(resourceUrl("/shaders/gl3/threshold.frag"))) { + var threshold by parameters + + init { + threshold = 0.5 + } +} + +val encodePoints by lazy { EncodePoints() } +val jumpFlood by lazy { JumpFlood() } +val pixelDistance by lazy { PixelDistance() } +val contourPoints by lazy { ContourPoints() } +val threshold by lazy { Threshold() } + + +class JumpFlooder(val width: Int, val height: Int) { + private val dimension = Math.max(width, height) + private val exp = Math.ceil(Math.log(dimension.toDouble()) / Math.log(2.0)).toInt() + private val squareDim = Math.pow(2.0, exp.toDouble()).toInt() + + private val coordinates = + listOf(colorBuffer(squareDim, squareDim, format = ColorFormat.RGB, type = ColorType.FLOAT32), + colorBuffer(squareDim, squareDim, format = ColorFormat.RGB, type = ColorType.FLOAT32)) + + private val final = renderTarget(width, height) { + colorBuffer(type = ColorType.FLOAT32) + } + + val result: ColorBuffer get() = final.colorBuffer(0) + + private val square = renderTarget(squareDim, squareDim) { + colorBuffer() + } + + private var contourUsed = false + val thresholded by lazy { colorBuffer(width, height) } + val edges by lazy { colorBuffer(width, height) } + + fun distanceToContour(drawer: Drawer, input: ColorBuffer, thresholdValue: Double = 0.5): ColorBuffer { + threshold.threshold = thresholdValue + threshold.apply(input, thresholded) + contourPoints.apply(thresholded, edges) + contourUsed = true + return jumpFlood(drawer, edges) + } + + fun directions(xRange: IntProgression = 0 until width, yRange: IntProgression = 0 until height): Array> { + result.shadow.download() + return result.shadow.mapIndexed(xRange, yRange) { _, _, r, g, _, _ -> Vector2(r, g) } + } + + + fun jumpFlood(drawer: Drawer, input: ColorBuffer): ColorBuffer { + if (input.width != width || input.height != height) { + throw IllegalArgumentException("dimensions mismatch") + } + + drawer.isolatedWithTarget(square) { + drawer.background(ColorRGBa.BLACK) + drawer.ortho(square) + drawer.view = Matrix44.IDENTITY + drawer.model = Matrix44.IDENTITY + drawer.image(input) + } + encodePoints.apply(square.colorBuffer(0), coordinates[0]) + + for (i in 0 until exp) { + jumpFlood.step = i + jumpFlood.apply(coordinates[i % 2], coordinates[(i + 1) % 2]) + } + + pixelDistance.apply(arrayOf(coordinates[exp % 2], thresholded), coordinates[exp % 2]) + drawer.isolatedWithTarget(final) { + drawer.background(ColorRGBa.BLACK) + drawer.ortho(final) + drawer.view = Matrix44.IDENTITY + drawer.model = Matrix44.IDENTITY + drawer.image(coordinates[exp % 2]) + } + return result + } + + fun destroy(destroyFinal: Boolean = true) { + coordinates.forEach { it.destroy() } + + square.colorBuffer(0).destroy() + square.detachColorBuffers() + square.destroy() + + if (destroyFinal) { + final.colorBuffer(0).destroy() + } + final.detachColorBuffers() + + final.destroy() + + if (contourUsed) { + edges.destroy() + thresholded.destroy() + } + + } + +} + +fun jumpFlood(drawer: Drawer, points: ColorBuffer): ColorBuffer { + val jumpFlooder = JumpFlooder(points.width, points.height) + jumpFlooder.jumpFlood(drawer, points) + val result = jumpFlooder.result + jumpFlooder.destroy(false) + return result } \ No newline at end of file diff --git a/orx-jumpflood/src/main/resources/shaders/gl3/pixel-distance.frag b/orx-jumpflood/src/main/resources/shaders/gl3/pixel-distance.frag index 2d81a52b5..7eebf70a9 100644 --- a/orx-jumpflood/src/main/resources/shaders/gl3/pixel-distance.frag +++ b/orx-jumpflood/src/main/resources/shaders/gl3/pixel-distance.frag @@ -1,16 +1,16 @@ -#version 330 core - -uniform sampler2D tex0; -uniform sampler2D tex1; -in vec2 v_texCoord0; - -out vec4 o_color; - -void main() { - vec2 size = textureSize(tex0, 0); - vec2 pixelPosition = v_texCoord0; - vec2 centroidPixelPosition = texture(tex0, v_texCoord0).xy; - vec2 pixelDistance = (centroidPixelPosition - pixelPosition) * size * vec2(1.0, -1.0); - float threshold = texture(tex1, v_texCoord0).r; - o_color = vec4(pixelDistance, threshold, 1.0); +#version 330 core + +uniform sampler2D tex0; +uniform sampler2D tex1; +in vec2 v_texCoord0; + +out vec4 o_color; + +void main() { + vec2 size = textureSize(tex0, 0); + vec2 pixelPosition = v_texCoord0; + vec2 centroidPixelPosition = texture(tex0, v_texCoord0).xy; + vec2 pixelDistance = (centroidPixelPosition - pixelPosition) * size * vec2(1.0, -1.0); + float threshold = texture(tex1, v_texCoord0).r; + o_color = vec4(pixelDistance, threshold, 1.0); } \ No newline at end of file diff --git a/orx-mesh-generators/src/main/kotlin/Box.kt b/orx-mesh-generators/src/main/kotlin/Box.kt index 4463ec93c..7be33aefe 100644 --- a/orx-mesh-generators/src/main/kotlin/Box.kt +++ b/orx-mesh-generators/src/main/kotlin/Box.kt @@ -1,61 +1,61 @@ -package org.openrndr.extras.meshgenerators - -import org.openrndr.draw.VertexBuffer -import org.openrndr.math.Vector3 - -fun boxMesh(width: Double = 1.0, height: Double = 1.0, depth: Double = 1.0, - widthSegments: Int = 1, heightSegments: Int = 1, depthSegments: Int = 1, - invert: Boolean = false): VertexBuffer { - val vb = meshVertexBuffer(widthSegments * heightSegments * 6 * 2 + - widthSegments * depthSegments * 6 * 2 + - heightSegments * depthSegments * 6 * 2) - vb.put { - generateBox(width, height, depth, - widthSegments, heightSegments, depthSegments, - invert, bufferWriter(this)) - } - return vb -} - -fun generateBox(width: Double = 1.0, height: Double = 1.0, depth: Double = 1.0, - widthSegments: Int = 1, heightSegments: Int = 1, depthSegments: Int = 1, - invert: Boolean = false, - writer: VertexWriter) { - - val sign = if (invert) -1.0 else 1.0 - // +x -- ZY - generatePlane(Vector3(width / 2.0 * sign, 0.0, 0.0), - Vector3.UNIT_Z, Vector3.UNIT_Y, Vector3.UNIT_X, - -depth, -height, - depthSegments, heightSegments, writer) - - // -x -- ZY - generatePlane(Vector3(-width / 2.0 * sign, 0.0, 0.0), - Vector3.UNIT_Z, Vector3.UNIT_Y, -Vector3.UNIT_X, - -depth, height, - depthSegments, heightSegments, writer) - - // +y -- XZ - generatePlane(Vector3(0.0, height / 2.0 * sign, 0.0), - Vector3.UNIT_X, Vector3.UNIT_Z, Vector3.UNIT_Y, - width, depth, - widthSegments, depthSegments, writer) - - // -y -- XZ - generatePlane(Vector3(0.0, -height / 2.0 * sign, 0.0), - Vector3.UNIT_X, Vector3.UNIT_Z, -Vector3.UNIT_Y, - width, -depth, - widthSegments, depthSegments, writer) - - // +z -- XY - generatePlane(Vector3(0.0, 0.0, depth / 2.0 * sign), - Vector3.UNIT_X, Vector3.UNIT_Y, Vector3.UNIT_Z, - -width, height, - widthSegments, heightSegments, writer) - - // -z -- XY - generatePlane(Vector3(0.0, 0.0, -depth / 2.0 * sign), - Vector3.UNIT_X, Vector3.UNIT_Y, -Vector3.UNIT_Z, - width, height, - widthSegments, heightSegments, writer) +package org.openrndr.extras.meshgenerators + +import org.openrndr.draw.VertexBuffer +import org.openrndr.math.Vector3 + +fun boxMesh(width: Double = 1.0, height: Double = 1.0, depth: Double = 1.0, + widthSegments: Int = 1, heightSegments: Int = 1, depthSegments: Int = 1, + invert: Boolean = false): VertexBuffer { + val vb = meshVertexBuffer(widthSegments * heightSegments * 6 * 2 + + widthSegments * depthSegments * 6 * 2 + + heightSegments * depthSegments * 6 * 2) + vb.put { + generateBox(width, height, depth, + widthSegments, heightSegments, depthSegments, + invert, bufferWriter(this)) + } + return vb +} + +fun generateBox(width: Double = 1.0, height: Double = 1.0, depth: Double = 1.0, + widthSegments: Int = 1, heightSegments: Int = 1, depthSegments: Int = 1, + invert: Boolean = false, + writer: VertexWriter) { + + val sign = if (invert) -1.0 else 1.0 + // +x -- ZY + generatePlane(Vector3(width / 2.0 * sign, 0.0, 0.0), + Vector3.UNIT_Z, Vector3.UNIT_Y, Vector3.UNIT_X, + -depth, -height, + depthSegments, heightSegments, writer) + + // -x -- ZY + generatePlane(Vector3(-width / 2.0 * sign, 0.0, 0.0), + Vector3.UNIT_Z, Vector3.UNIT_Y, -Vector3.UNIT_X, + -depth, height, + depthSegments, heightSegments, writer) + + // +y -- XZ + generatePlane(Vector3(0.0, height / 2.0 * sign, 0.0), + Vector3.UNIT_X, Vector3.UNIT_Z, Vector3.UNIT_Y, + width, depth, + widthSegments, depthSegments, writer) + + // -y -- XZ + generatePlane(Vector3(0.0, -height / 2.0 * sign, 0.0), + Vector3.UNIT_X, Vector3.UNIT_Z, -Vector3.UNIT_Y, + width, -depth, + widthSegments, depthSegments, writer) + + // +z -- XY + generatePlane(Vector3(0.0, 0.0, depth / 2.0 * sign), + Vector3.UNIT_X, Vector3.UNIT_Y, Vector3.UNIT_Z, + -width, height, + widthSegments, heightSegments, writer) + + // -z -- XY + generatePlane(Vector3(0.0, 0.0, -depth / 2.0 * sign), + Vector3.UNIT_X, Vector3.UNIT_Y, -Vector3.UNIT_Z, + width, height, + widthSegments, heightSegments, writer) } \ No newline at end of file diff --git a/orx-mesh-generators/src/main/kotlin/Cap.kt b/orx-mesh-generators/src/main/kotlin/Cap.kt index 26ce263d1..65f7a1162 100644 --- a/orx-mesh-generators/src/main/kotlin/Cap.kt +++ b/orx-mesh-generators/src/main/kotlin/Cap.kt @@ -1,101 +1,101 @@ -package org.openrndr.extras.meshgenerators - -import org.openrndr.math.Vector2 -import org.openrndr.math.Vector3 -import org.openrndr.math.transforms.rotateY - -fun generateCap(sides: Int, radius: Double, enveloppe: List = listOf(Vector2(0.0, 0.0), Vector2(1.0, 0.0)), writer: VertexWriter) { - val maxX = enveloppe.maxBy { it.x } ?: Vector2(1.0, 0.0) - val a = maxX.x - - val cleanEnveloppe = enveloppe.map { Vector2((it.x / a) * radius, it.y) } - - val normals2D = enveloppe.zipWithNext().map { - val d = it.second - it.first - d.normalized.perpendicular - } - - val basePositions = cleanEnveloppe.map { Vector3(it.x, it.y, 0.0) } - val baseNormals = normals2D.map { Vector3(it.x, it.y, 0.0) } - - for (side in 0 until sides) { - val r0 = rotateY(360.0 / sides * side) - val r1 = rotateY(360.0 / sides * (side + 1)) - - val v0 = basePositions.map { (r0 * it.xyz0).xyz } - val v1 = basePositions.map { (r1 * it.xyz0).xyz } - val n0 = baseNormals.map { (r0 * it.xyz0).xyz } - val n1 = baseNormals.map { (r1 * it.xyz0).xyz } - - for (segment in 0 until basePositions.size - 1) { - - val p00 = v0[segment] - val p01 = v0[segment+1] - val p10 = v1[segment] - val p11 = v1[segment+1] - - val nn0 = n0[segment] - val nn1 = n1[segment] - - writer(p00, nn0, Vector2.ZERO) - writer(p01, nn0, Vector2.ZERO) - writer(p11, nn1, Vector2.ZERO) - - writer(p11, nn1, Vector2.ZERO) - writer(p10, nn1, Vector2.ZERO) - writer(p00, nn0, Vector2.ZERO) - } - } -} - -fun generateRevolve(sides: Int, length: Double, enveloppe: List = listOf(Vector2(1.0, 0.0), Vector2(1.0, 1.0)), writer: VertexWriter) { - val maxY = enveloppe.maxBy { it.y } ?: Vector2(0.0, 1.0) - val a = maxY.y - - val cleanEnveloppe = enveloppe.map { Vector2((it.x), (it.y/a - 0.5) * length ) } - - val normals2D = enveloppe.zipWithNext().map { - val d = it.second - it.first - d.normalized.perpendicular * Vector2(1.0, -1.0) - - } - - val extended = listOf(normals2D[0]) + normals2D + normals2D[normals2D.size-1] - -// extended.zipW - - println(normals2D.joinToString(", ")) - - val basePositions = cleanEnveloppe.map { Vector3(it.x, it.y, 0.0) } - val baseNormals = normals2D.map { Vector3(it.x, it.y, 0.0) } - - for (side in 0 until sides) { - val r0 = rotateY(360.0 / sides * side) - val r1 = rotateY(360.0 / sides * (side + 1)) - - val v0 = basePositions.map { (r0 * it.xyz0).xyz } - val v1 = basePositions.map { (r1 * it.xyz0).xyz } - val n0 = baseNormals.map { (r0 * it.xyz0).xyz } - val n1 = baseNormals.map { (r1 * it.xyz0).xyz } - - for (segment in 0 until basePositions.size - 1) { - - val p00 = v0[segment] - val p01 = v0[segment+1] - val p10 = v1[segment] - val p11 = v1[segment+1] - - val nn0 = n0[segment] - val nn1 = n1[segment] - - writer(p00, nn0, Vector2.ZERO) - writer(p10, nn1, Vector2.ZERO) - writer(p11, nn1, Vector2.ZERO) - - writer(p11, nn1, Vector2.ZERO) - writer(p01, nn0, Vector2.ZERO) - - writer(p00, nn0, Vector2.ZERO) - } - } +package org.openrndr.extras.meshgenerators + +import org.openrndr.math.Vector2 +import org.openrndr.math.Vector3 +import org.openrndr.math.transforms.rotateY + +fun generateCap(sides: Int, radius: Double, enveloppe: List = listOf(Vector2(0.0, 0.0), Vector2(1.0, 0.0)), writer: VertexWriter) { + val maxX = enveloppe.maxBy { it.x } ?: Vector2(1.0, 0.0) + val a = maxX.x + + val cleanEnveloppe = enveloppe.map { Vector2((it.x / a) * radius, it.y) } + + val normals2D = enveloppe.zipWithNext().map { + val d = it.second - it.first + d.normalized.perpendicular + } + + val basePositions = cleanEnveloppe.map { Vector3(it.x, it.y, 0.0) } + val baseNormals = normals2D.map { Vector3(it.x, it.y, 0.0) } + + for (side in 0 until sides) { + val r0 = rotateY(360.0 / sides * side) + val r1 = rotateY(360.0 / sides * (side + 1)) + + val v0 = basePositions.map { (r0 * it.xyz0).xyz } + val v1 = basePositions.map { (r1 * it.xyz0).xyz } + val n0 = baseNormals.map { (r0 * it.xyz0).xyz } + val n1 = baseNormals.map { (r1 * it.xyz0).xyz } + + for (segment in 0 until basePositions.size - 1) { + + val p00 = v0[segment] + val p01 = v0[segment+1] + val p10 = v1[segment] + val p11 = v1[segment+1] + + val nn0 = n0[segment] + val nn1 = n1[segment] + + writer(p00, nn0, Vector2.ZERO) + writer(p01, nn0, Vector2.ZERO) + writer(p11, nn1, Vector2.ZERO) + + writer(p11, nn1, Vector2.ZERO) + writer(p10, nn1, Vector2.ZERO) + writer(p00, nn0, Vector2.ZERO) + } + } +} + +fun generateRevolve(sides: Int, length: Double, enveloppe: List = listOf(Vector2(1.0, 0.0), Vector2(1.0, 1.0)), writer: VertexWriter) { + val maxY = enveloppe.maxBy { it.y } ?: Vector2(0.0, 1.0) + val a = maxY.y + + val cleanEnveloppe = enveloppe.map { Vector2((it.x), (it.y/a - 0.5) * length ) } + + val normals2D = enveloppe.zipWithNext().map { + val d = it.second - it.first + d.normalized.perpendicular * Vector2(1.0, -1.0) + + } + + val extended = listOf(normals2D[0]) + normals2D + normals2D[normals2D.size-1] + +// extended.zipW + + println(normals2D.joinToString(", ")) + + val basePositions = cleanEnveloppe.map { Vector3(it.x, it.y, 0.0) } + val baseNormals = normals2D.map { Vector3(it.x, it.y, 0.0) } + + for (side in 0 until sides) { + val r0 = rotateY(360.0 / sides * side) + val r1 = rotateY(360.0 / sides * (side + 1)) + + val v0 = basePositions.map { (r0 * it.xyz0).xyz } + val v1 = basePositions.map { (r1 * it.xyz0).xyz } + val n0 = baseNormals.map { (r0 * it.xyz0).xyz } + val n1 = baseNormals.map { (r1 * it.xyz0).xyz } + + for (segment in 0 until basePositions.size - 1) { + + val p00 = v0[segment] + val p01 = v0[segment+1] + val p10 = v1[segment] + val p11 = v1[segment+1] + + val nn0 = n0[segment] + val nn1 = n1[segment] + + writer(p00, nn0, Vector2.ZERO) + writer(p10, nn1, Vector2.ZERO) + writer(p11, nn1, Vector2.ZERO) + + writer(p11, nn1, Vector2.ZERO) + writer(p01, nn0, Vector2.ZERO) + + writer(p00, nn0, Vector2.ZERO) + } + } } \ No newline at end of file diff --git a/orx-mesh-generators/src/main/kotlin/Cylinder.kt b/orx-mesh-generators/src/main/kotlin/Cylinder.kt index 89d1f2811..c42bf7d70 100644 --- a/orx-mesh-generators/src/main/kotlin/Cylinder.kt +++ b/orx-mesh-generators/src/main/kotlin/Cylinder.kt @@ -1,82 +1,82 @@ -package org.openrndr.extras.meshgenerators - -import org.openrndr.draw.VertexBuffer -import org.openrndr.math.Vector2 -import org.openrndr.math.Vector3 -import org.openrndr.math.mix -import org.openrndr.math.transforms.rotateZ - -fun cylinderMesh(sides: Int = 16, segments: Int = 16, radius: Double = 1.0, length: Double, invert: Boolean = false): VertexBuffer { - val vertexCount = 6 * sides * segments - val vb = meshVertexBuffer(vertexCount) - vb.put { - generateCylinder(sides, segments, radius, length, invert, bufferWriter(this)) - } - return vb -} - -fun generateCylinder(sides: Int, segments: Int, radius: Double, length: Double, invert: Boolean = false, vertexWriter: VertexWriter) { - return generateTaperedCylinder(sides, segments, radius, radius, length, invert, vertexWriter) -} - -fun generateTaperedCylinder(sides: Int, segments: Int, radiusStart: Double, radiusEnd:Double, length: Double, invert: Boolean = false, vertexWriter: VertexWriter) { - val dphi = (Math.PI * 2) / sides - val ddeg = (360.0) / sides - - val invertFactor = if (invert) -1.0 else 1.0 - - val dr = radiusEnd - radiusStart - - val baseNormal = Vector2(length, dr).normalized.perpendicular.let { Vector3(x=it.y, y=0.0, z=it.x)} - //val baseNormal = Vector3(1.0, 0.0, 0.0) - - for (segment in 0 until segments) { - - val radius0 = mix(radiusStart, radiusEnd, segment*1.0/segments) - val radius1 = mix(radiusStart, radiusEnd, (segment+1)*1.0/segments) - val z0 = (length / segments) * segment - length/2.0 - val z1 = (length / segments) * (segment + 1) - length/2.0 - - - for (side in 0 until sides) { - val x00 = Math.cos(side * dphi) * radius0 - val x10 = Math.cos(side * dphi + dphi) * radius0 - val y00 = Math.sin(side * dphi) * radius0 - val y10 = Math.sin(side * dphi + dphi) * radius0 - - val x01 = Math.cos(side * dphi) * radius1 - val x11 = Math.cos(side * dphi + dphi) * radius1 - val y01 = Math.sin(side * dphi) * radius1 - val y11 = Math.sin(side * dphi + dphi) * radius1 - - - val u0 = (segment + 0.0) / segments - val u1 = (segment + 1.0) / segments - val v0 = (side + 0.0) / sides - val v1 = (side + 1.0) / sides - - - val n0 = (rotateZ(side * ddeg) * baseNormal.xyz0).xyz.normalized * invertFactor - val n1 = (rotateZ((side+1) * ddeg) * baseNormal.xyz0).xyz.normalized * invertFactor - - - if (!invert) { - vertexWriter(Vector3(x00, y00, z0), n0, Vector2(u0, v0)) - vertexWriter(Vector3(x10, y10, z0), n1, Vector2(u0, v1)) - vertexWriter(Vector3(x11, y11, z1), n1, Vector2(u1, v1)) - - vertexWriter(Vector3(x11, y11, z1), n1, Vector2(u1, v1)) - vertexWriter(Vector3(x01, y01, z1), n0, Vector2(u1, v0)) - vertexWriter(Vector3(x00, y00, z0), n0, Vector2(u0, v0)) - } else { - vertexWriter(Vector3(x00, y00, z0), n0, Vector2(u0, v0)) - vertexWriter(Vector3(x01, y01, z1), n0, Vector2(u1, v0)) - vertexWriter(Vector3(x11, y11, z1), n1, Vector2(u1, v1)) - - vertexWriter(Vector3(x11, y11, z1), n1, Vector2(u1, v1)) - vertexWriter(Vector3(x10, y10, z0), n1, Vector2(u0, v1)) - vertexWriter(Vector3(x00, y00, z0), n0, Vector2(u0, v0)) - } - } - } +package org.openrndr.extras.meshgenerators + +import org.openrndr.draw.VertexBuffer +import org.openrndr.math.Vector2 +import org.openrndr.math.Vector3 +import org.openrndr.math.mix +import org.openrndr.math.transforms.rotateZ + +fun cylinderMesh(sides: Int = 16, segments: Int = 16, radius: Double = 1.0, length: Double, invert: Boolean = false): VertexBuffer { + val vertexCount = 6 * sides * segments + val vb = meshVertexBuffer(vertexCount) + vb.put { + generateCylinder(sides, segments, radius, length, invert, bufferWriter(this)) + } + return vb +} + +fun generateCylinder(sides: Int, segments: Int, radius: Double, length: Double, invert: Boolean = false, vertexWriter: VertexWriter) { + return generateTaperedCylinder(sides, segments, radius, radius, length, invert, vertexWriter) +} + +fun generateTaperedCylinder(sides: Int, segments: Int, radiusStart: Double, radiusEnd:Double, length: Double, invert: Boolean = false, vertexWriter: VertexWriter) { + val dphi = (Math.PI * 2) / sides + val ddeg = (360.0) / sides + + val invertFactor = if (invert) -1.0 else 1.0 + + val dr = radiusEnd - radiusStart + + val baseNormal = Vector2(length, dr).normalized.perpendicular.let { Vector3(x=it.y, y=0.0, z=it.x)} + //val baseNormal = Vector3(1.0, 0.0, 0.0) + + for (segment in 0 until segments) { + + val radius0 = mix(radiusStart, radiusEnd, segment*1.0/segments) + val radius1 = mix(radiusStart, radiusEnd, (segment+1)*1.0/segments) + val z0 = (length / segments) * segment - length/2.0 + val z1 = (length / segments) * (segment + 1) - length/2.0 + + + for (side in 0 until sides) { + val x00 = Math.cos(side * dphi) * radius0 + val x10 = Math.cos(side * dphi + dphi) * radius0 + val y00 = Math.sin(side * dphi) * radius0 + val y10 = Math.sin(side * dphi + dphi) * radius0 + + val x01 = Math.cos(side * dphi) * radius1 + val x11 = Math.cos(side * dphi + dphi) * radius1 + val y01 = Math.sin(side * dphi) * radius1 + val y11 = Math.sin(side * dphi + dphi) * radius1 + + + val u0 = (segment + 0.0) / segments + val u1 = (segment + 1.0) / segments + val v0 = (side + 0.0) / sides + val v1 = (side + 1.0) / sides + + + val n0 = (rotateZ(side * ddeg) * baseNormal.xyz0).xyz.normalized * invertFactor + val n1 = (rotateZ((side+1) * ddeg) * baseNormal.xyz0).xyz.normalized * invertFactor + + + if (!invert) { + vertexWriter(Vector3(x00, y00, z0), n0, Vector2(u0, v0)) + vertexWriter(Vector3(x10, y10, z0), n1, Vector2(u0, v1)) + vertexWriter(Vector3(x11, y11, z1), n1, Vector2(u1, v1)) + + vertexWriter(Vector3(x11, y11, z1), n1, Vector2(u1, v1)) + vertexWriter(Vector3(x01, y01, z1), n0, Vector2(u1, v0)) + vertexWriter(Vector3(x00, y00, z0), n0, Vector2(u0, v0)) + } else { + vertexWriter(Vector3(x00, y00, z0), n0, Vector2(u0, v0)) + vertexWriter(Vector3(x01, y01, z1), n0, Vector2(u1, v0)) + vertexWriter(Vector3(x11, y11, z1), n1, Vector2(u1, v1)) + + vertexWriter(Vector3(x11, y11, z1), n1, Vector2(u1, v1)) + vertexWriter(Vector3(x10, y10, z0), n1, Vector2(u0, v1)) + vertexWriter(Vector3(x00, y00, z0), n0, Vector2(u0, v0)) + } + } + } } \ No newline at end of file diff --git a/orx-mesh-generators/src/main/kotlin/GeneratorBuffer.kt b/orx-mesh-generators/src/main/kotlin/GeneratorBuffer.kt index 4b2b39386..790f44ab0 100644 --- a/orx-mesh-generators/src/main/kotlin/GeneratorBuffer.kt +++ b/orx-mesh-generators/src/main/kotlin/GeneratorBuffer.kt @@ -1,172 +1,172 @@ -package org.openrndr.extras.meshgenerators - -import org.openrndr.draw.VertexBuffer -import org.openrndr.draw.vertexBuffer -import org.openrndr.draw.vertexFormat -import org.openrndr.math.Matrix44 -import org.openrndr.math.Vector2 -import org.openrndr.math.Vector3 -import org.openrndr.math.transforms.rotate -import org.openrndr.math.transforms.transform -import org.openrndr.shape.Shape -import java.nio.ByteBuffer -import java.nio.ByteOrder - -class GeneratorBuffer { - class VertexData(val position: Vector3, val normal: Vector3, val texCoord: Vector2) - - var data = mutableListOf() - - fun write(position: Vector3, normal: Vector3, texCoord: Vector2) { - data.add(VertexData(position, normal, texCoord)) - } - - fun concat(other: GeneratorBuffer) { - data.addAll(other.data) - } - - fun transform(m: Matrix44) { - data = data.map { - VertexData((m * (it.position.xyz1)).xyz, (m * (it.normal.xyz0)).xyz, it.texCoord) - }.toMutableList() - } - - fun toByteBuffer(): ByteBuffer { - val bb = ByteBuffer.allocateDirect(data.size * (3 * 4 + 3 * 4 + 2 * 4)) - bb.order(ByteOrder.nativeOrder()) - bb.rewind() - for (d in data) { - bb.putFloat(d.position.x.toFloat()) - bb.putFloat(d.position.y.toFloat()) - bb.putFloat(d.position.z.toFloat()) - - bb.putFloat(d.normal.x.toFloat()) - bb.putFloat(d.normal.y.toFloat()) - bb.putFloat(d.normal.z.toFloat()) - - bb.putFloat(d.texCoord.x.toFloat()) - bb.putFloat(d.texCoord.y.toFloat()) - } - return bb - } -} - -fun GeneratorBuffer.sphere(sides: Int, segments: Int, radius: Double, invert: Boolean = false) { - generateSphere(sides, segments, radius, invert, this::write) -} - -fun GeneratorBuffer.hemisphere(sides: Int, segments: Int, radius: Double, invert: Boolean = false) { - generateHemisphere(sides, segments, radius, invert, this::write) -} - -enum class GridCoordinates { - INDEX, - UNIPOLAR, - BIPOLAR, -} - -fun GeneratorBuffer.grid(width: Int, height: Int, coordinates: GridCoordinates = GridCoordinates.BIPOLAR, builder: GeneratorBuffer.(u: Double, v: Double) -> Unit) { - for (v in 0 until height) { - for (u in 0 until width) { - group { - when (coordinates) { - GridCoordinates.INDEX -> this.builder(u * 1.0, v * 1.0) - GridCoordinates.BIPOLAR -> this.builder(2 * u / (width - 1.0) - 1, - 2 * v / (height - 1.0) - 1) - GridCoordinates.UNIPOLAR -> this.builder(u / (width - 1.0), v / (height - 1.0)) - } - } - } - } -} - -fun GeneratorBuffer.twist(degreesPerUnit: Double, start: Double, axis: Vector3 = Vector3.UNIT_Y) { - data = data.map { - val p = it.position.projectedOn(axis) - val t = if (axis.x != 0.0) p.x / axis.x else if (axis.y != 0.0) p.y / axis.y else if (axis.z != 0.0) p.z / axis.z else - throw IllegalArgumentException("0 axis") - val r = rotate(axis, t * degreesPerUnit) - GeneratorBuffer.VertexData((r * it.position.xyz1).xyz, (r * it.normal.xyz0).xyz, it.texCoord) - }.toMutableList() -} - -fun GeneratorBuffer.grid(width: Int, height: Int, depth: Int, coordinates: GridCoordinates = GridCoordinates.BIPOLAR, builder: GeneratorBuffer.(u: Double, v: Double, w: Double) -> Unit) { - for (w in 0 until depth) { - for (v in 0 until height) { - for (u in 0 until width) { - group { - when (coordinates) { - GridCoordinates.INDEX -> this.builder(u * 1.0, v * 1.0, w * 1.0) - GridCoordinates.BIPOLAR -> this.builder(2 * u / (width - 1.0) - 1, - 2 * v / (height - 1.0) - 1, 2 * w / (depth - 1.0) - 1) - GridCoordinates.UNIPOLAR -> this.builder(u / (width - 1.0), v / (height - 1.0), w / (depth - 1.0)) - } - } - } - } - } -} - -fun GeneratorBuffer.box(width: Double, height: Double, depth: Double, widthSegments: Int = 1, heightSegments: Int = 1, depthSegments: Int = 1, invert: Boolean = false) { - generateBox(width, height, depth, widthSegments, heightSegments, depthSegments, invert, this::write) -} - -fun GeneratorBuffer.cylinder(sides: Int, segments: Int, radius: Double, length: Double, invert: Boolean = false) { - generateCylinder(sides, segments, radius, length, invert, this::write) -} - -fun GeneratorBuffer.taperedCylinder(sides: Int, segments: Int, startRadius: Double, endRadius: Double, length: Double, invert: Boolean = false) { - generateTaperedCylinder(sides, segments, startRadius, endRadius, length, invert, this::write) -} - -fun GeneratorBuffer.cap(sides: Int, radius: Double, enveloppe: List) { - generateCap(sides, radius, enveloppe, this::write) -} - -fun GeneratorBuffer.revolve(sides:Int, length:Double, enveloppe: List) { - generateRevolve(sides, length, enveloppe, this::write) -} - -fun GeneratorBuffer.extrudeShape(shape: Shape, length: Double, scale: Double = 1.0, distanceTolerance: Double = 0.5) { - extrudeShape(shape, -length / 2.0, length / 2.0, scale, scale, true, true, distanceTolerance, false, this::write) -} - -fun meshGenerator(builder: GeneratorBuffer.() -> Unit): VertexBuffer { - val gb = GeneratorBuffer() - gb.builder() - - val vb = vertexBuffer(vertexFormat { - position(3) - normal(3) - textureCoordinate(2) - }, gb.data.size) - - val bb = gb.toByteBuffer() - bb.rewind() - vb.write(bb) - return vb -} - -fun generator(builder: GeneratorBuffer.() -> Unit): GeneratorBuffer { - val gb = GeneratorBuffer() - gb.builder() - return gb -} - -fun GeneratorBuffer.group(builder: GeneratorBuffer.() -> Unit) { - val gb = GeneratorBuffer() - gb.builder() - this.concat(gb) -} - -fun main(args: Array) { - val gb = generator { - box(20.0, 20.0, 20.0) - group { - box(40.0, 40.0, 40.0) - transform(transform { - translate(0.0, 20.0, 0.0) - }) - } - } +package org.openrndr.extras.meshgenerators + +import org.openrndr.draw.VertexBuffer +import org.openrndr.draw.vertexBuffer +import org.openrndr.draw.vertexFormat +import org.openrndr.math.Matrix44 +import org.openrndr.math.Vector2 +import org.openrndr.math.Vector3 +import org.openrndr.math.transforms.rotate +import org.openrndr.math.transforms.transform +import org.openrndr.shape.Shape +import java.nio.ByteBuffer +import java.nio.ByteOrder + +class GeneratorBuffer { + class VertexData(val position: Vector3, val normal: Vector3, val texCoord: Vector2) + + var data = mutableListOf() + + fun write(position: Vector3, normal: Vector3, texCoord: Vector2) { + data.add(VertexData(position, normal, texCoord)) + } + + fun concat(other: GeneratorBuffer) { + data.addAll(other.data) + } + + fun transform(m: Matrix44) { + data = data.map { + VertexData((m * (it.position.xyz1)).xyz, (m * (it.normal.xyz0)).xyz, it.texCoord) + }.toMutableList() + } + + fun toByteBuffer(): ByteBuffer { + val bb = ByteBuffer.allocateDirect(data.size * (3 * 4 + 3 * 4 + 2 * 4)) + bb.order(ByteOrder.nativeOrder()) + bb.rewind() + for (d in data) { + bb.putFloat(d.position.x.toFloat()) + bb.putFloat(d.position.y.toFloat()) + bb.putFloat(d.position.z.toFloat()) + + bb.putFloat(d.normal.x.toFloat()) + bb.putFloat(d.normal.y.toFloat()) + bb.putFloat(d.normal.z.toFloat()) + + bb.putFloat(d.texCoord.x.toFloat()) + bb.putFloat(d.texCoord.y.toFloat()) + } + return bb + } +} + +fun GeneratorBuffer.sphere(sides: Int, segments: Int, radius: Double, invert: Boolean = false) { + generateSphere(sides, segments, radius, invert, this::write) +} + +fun GeneratorBuffer.hemisphere(sides: Int, segments: Int, radius: Double, invert: Boolean = false) { + generateHemisphere(sides, segments, radius, invert, this::write) +} + +enum class GridCoordinates { + INDEX, + UNIPOLAR, + BIPOLAR, +} + +fun GeneratorBuffer.grid(width: Int, height: Int, coordinates: GridCoordinates = GridCoordinates.BIPOLAR, builder: GeneratorBuffer.(u: Double, v: Double) -> Unit) { + for (v in 0 until height) { + for (u in 0 until width) { + group { + when (coordinates) { + GridCoordinates.INDEX -> this.builder(u * 1.0, v * 1.0) + GridCoordinates.BIPOLAR -> this.builder(2 * u / (width - 1.0) - 1, + 2 * v / (height - 1.0) - 1) + GridCoordinates.UNIPOLAR -> this.builder(u / (width - 1.0), v / (height - 1.0)) + } + } + } + } +} + +fun GeneratorBuffer.twist(degreesPerUnit: Double, start: Double, axis: Vector3 = Vector3.UNIT_Y) { + data = data.map { + val p = it.position.projectedOn(axis) + val t = if (axis.x != 0.0) p.x / axis.x else if (axis.y != 0.0) p.y / axis.y else if (axis.z != 0.0) p.z / axis.z else + throw IllegalArgumentException("0 axis") + val r = rotate(axis, t * degreesPerUnit) + GeneratorBuffer.VertexData((r * it.position.xyz1).xyz, (r * it.normal.xyz0).xyz, it.texCoord) + }.toMutableList() +} + +fun GeneratorBuffer.grid(width: Int, height: Int, depth: Int, coordinates: GridCoordinates = GridCoordinates.BIPOLAR, builder: GeneratorBuffer.(u: Double, v: Double, w: Double) -> Unit) { + for (w in 0 until depth) { + for (v in 0 until height) { + for (u in 0 until width) { + group { + when (coordinates) { + GridCoordinates.INDEX -> this.builder(u * 1.0, v * 1.0, w * 1.0) + GridCoordinates.BIPOLAR -> this.builder(2 * u / (width - 1.0) - 1, + 2 * v / (height - 1.0) - 1, 2 * w / (depth - 1.0) - 1) + GridCoordinates.UNIPOLAR -> this.builder(u / (width - 1.0), v / (height - 1.0), w / (depth - 1.0)) + } + } + } + } + } +} + +fun GeneratorBuffer.box(width: Double, height: Double, depth: Double, widthSegments: Int = 1, heightSegments: Int = 1, depthSegments: Int = 1, invert: Boolean = false) { + generateBox(width, height, depth, widthSegments, heightSegments, depthSegments, invert, this::write) +} + +fun GeneratorBuffer.cylinder(sides: Int, segments: Int, radius: Double, length: Double, invert: Boolean = false) { + generateCylinder(sides, segments, radius, length, invert, this::write) +} + +fun GeneratorBuffer.taperedCylinder(sides: Int, segments: Int, startRadius: Double, endRadius: Double, length: Double, invert: Boolean = false) { + generateTaperedCylinder(sides, segments, startRadius, endRadius, length, invert, this::write) +} + +fun GeneratorBuffer.cap(sides: Int, radius: Double, enveloppe: List) { + generateCap(sides, radius, enveloppe, this::write) +} + +fun GeneratorBuffer.revolve(sides:Int, length:Double, enveloppe: List) { + generateRevolve(sides, length, enveloppe, this::write) +} + +fun GeneratorBuffer.extrudeShape(shape: Shape, length: Double, scale: Double = 1.0, distanceTolerance: Double = 0.5) { + extrudeShape(shape, -length / 2.0, length / 2.0, scale, scale, true, true, distanceTolerance, false, this::write) +} + +fun meshGenerator(builder: GeneratorBuffer.() -> Unit): VertexBuffer { + val gb = GeneratorBuffer() + gb.builder() + + val vb = vertexBuffer(vertexFormat { + position(3) + normal(3) + textureCoordinate(2) + }, gb.data.size) + + val bb = gb.toByteBuffer() + bb.rewind() + vb.write(bb) + return vb +} + +fun generator(builder: GeneratorBuffer.() -> Unit): GeneratorBuffer { + val gb = GeneratorBuffer() + gb.builder() + return gb +} + +fun GeneratorBuffer.group(builder: GeneratorBuffer.() -> Unit) { + val gb = GeneratorBuffer() + gb.builder() + this.concat(gb) +} + +fun main(args: Array) { + val gb = generator { + box(20.0, 20.0, 20.0) + group { + box(40.0, 40.0, 40.0) + transform(transform { + translate(0.0, 20.0, 0.0) + }) + } + } } \ No newline at end of file diff --git a/orx-mesh-generators/src/main/kotlin/Plane.kt b/orx-mesh-generators/src/main/kotlin/Plane.kt index 50dbf4b9e..f5b1b0d8e 100644 --- a/orx-mesh-generators/src/main/kotlin/Plane.kt +++ b/orx-mesh-generators/src/main/kotlin/Plane.kt @@ -1,84 +1,84 @@ -package org.openrndr.extras.meshgenerators - -import org.openrndr.draw.VertexBuffer -import org.openrndr.math.Vector2 -import org.openrndr.math.Vector3 - -fun planeMesh(center: Vector3, - right: Vector3, - forward: Vector3, - up: Vector3 = forward.cross(right).normalized, - width: Double = 1.0, height: Double = 1.0, - widthSegments: Int = 1, heightSegments: Int = 1): VertexBuffer { - - val vertexCount = (widthSegments * heightSegments) * 6 - val vb = meshVertexBuffer(vertexCount) - vb.put { - generatePlane(center, right, forward, up, - width, height, widthSegments, heightSegments, bufferWriter(this)) - } - return vb -} - -/** - * generates a finite plane with its center at (0,0,0) and spanning the xz-plane - */ -fun groundPlaneMesh(width: Double = 1.0, - height: Double = 1.0, - widthSegments: Int = 1, - heightSegments: Int = 1): VertexBuffer { - return planeMesh(Vector3.ZERO, Vector3.UNIT_X, Vector3.UNIT_Z, Vector3.UNIT_Y, - width, height, widthSegments, heightSegments) -} - -/** - * generates a finite plane with its center at (0,0,0) and spanning the xy-plane - */ -fun wallPlaneMesh(width: Double = 1.0, - height: Double = 1.0, - widthSegments: Int = 1, - heightSegments: Int = 1): VertexBuffer { - return planeMesh(Vector3.ZERO, Vector3.UNIT_X, Vector3.UNIT_Y, Vector3.UNIT_Z, - width, height, widthSegments, heightSegments) -} - - -fun generatePlane(center: Vector3, - right: Vector3, - forward: Vector3, - up: Vector3 = forward.cross(right).normalized, - - width: Double = 1.0, height: Double = 1.0, - widthSegments: Int = 1, heightSegments: Int = 2, - writer: VertexWriter) { - - val forwardStep = forward.normalized * (height / heightSegments) - val rightStep = right.normalized * (width / widthSegments) - - val corner = center - forward.normalized * (height*0.5) - right.normalized * (width * 0.5) - - val step = Vector2(1.0 / width, 1.0 / height) - - for (v in 0 until heightSegments) { - for (u in 0 until widthSegments) { - - val uv00 = Vector2(u + 0.0, v + 0.0) * step - val uv01 = Vector2(u + 0.0, v + 0.1) * step - val uv10 = Vector2(u + 1.0, v + 0.0) * step - val uv11 = Vector2(u + 1.0, v + 0.1) * step - - val c00 = corner + forwardStep * v.toDouble() + rightStep * u.toDouble() - val c01 = corner + forwardStep * (v + 1).toDouble() + rightStep * u.toDouble() - val c10 = corner + forwardStep * v.toDouble() + rightStep * (u + 1).toDouble() - val c11 = corner + forwardStep * (v + 1).toDouble() + rightStep * (u + 1).toDouble() - - writer(c11, up, uv00) - writer(c10, up, uv10) - writer(c00, up, uv11) - - writer(c00, up, uv11) - writer(c01, up, uv01) - writer(c11, up, uv00) - } - } +package org.openrndr.extras.meshgenerators + +import org.openrndr.draw.VertexBuffer +import org.openrndr.math.Vector2 +import org.openrndr.math.Vector3 + +fun planeMesh(center: Vector3, + right: Vector3, + forward: Vector3, + up: Vector3 = forward.cross(right).normalized, + width: Double = 1.0, height: Double = 1.0, + widthSegments: Int = 1, heightSegments: Int = 1): VertexBuffer { + + val vertexCount = (widthSegments * heightSegments) * 6 + val vb = meshVertexBuffer(vertexCount) + vb.put { + generatePlane(center, right, forward, up, + width, height, widthSegments, heightSegments, bufferWriter(this)) + } + return vb +} + +/** + * generates a finite plane with its center at (0,0,0) and spanning the xz-plane + */ +fun groundPlaneMesh(width: Double = 1.0, + height: Double = 1.0, + widthSegments: Int = 1, + heightSegments: Int = 1): VertexBuffer { + return planeMesh(Vector3.ZERO, Vector3.UNIT_X, Vector3.UNIT_Z, Vector3.UNIT_Y, + width, height, widthSegments, heightSegments) +} + +/** + * generates a finite plane with its center at (0,0,0) and spanning the xy-plane + */ +fun wallPlaneMesh(width: Double = 1.0, + height: Double = 1.0, + widthSegments: Int = 1, + heightSegments: Int = 1): VertexBuffer { + return planeMesh(Vector3.ZERO, Vector3.UNIT_X, Vector3.UNIT_Y, Vector3.UNIT_Z, + width, height, widthSegments, heightSegments) +} + + +fun generatePlane(center: Vector3, + right: Vector3, + forward: Vector3, + up: Vector3 = forward.cross(right).normalized, + + width: Double = 1.0, height: Double = 1.0, + widthSegments: Int = 1, heightSegments: Int = 2, + writer: VertexWriter) { + + val forwardStep = forward.normalized * (height / heightSegments) + val rightStep = right.normalized * (width / widthSegments) + + val corner = center - forward.normalized * (height*0.5) - right.normalized * (width * 0.5) + + val step = Vector2(1.0 / width, 1.0 / height) + + for (v in 0 until heightSegments) { + for (u in 0 until widthSegments) { + + val uv00 = Vector2(u + 0.0, v + 0.0) * step + val uv01 = Vector2(u + 0.0, v + 0.1) * step + val uv10 = Vector2(u + 1.0, v + 0.0) * step + val uv11 = Vector2(u + 1.0, v + 0.1) * step + + val c00 = corner + forwardStep * v.toDouble() + rightStep * u.toDouble() + val c01 = corner + forwardStep * (v + 1).toDouble() + rightStep * u.toDouble() + val c10 = corner + forwardStep * v.toDouble() + rightStep * (u + 1).toDouble() + val c11 = corner + forwardStep * (v + 1).toDouble() + rightStep * (u + 1).toDouble() + + writer(c11, up, uv00) + writer(c10, up, uv10) + writer(c00, up, uv11) + + writer(c00, up, uv11) + writer(c01, up, uv01) + writer(c11, up, uv00) + } + } } \ No newline at end of file diff --git a/orx-mesh-generators/src/main/kotlin/Sphere.kt b/orx-mesh-generators/src/main/kotlin/Sphere.kt index 20ef6f1dc..6f8b577a2 100644 --- a/orx-mesh-generators/src/main/kotlin/Sphere.kt +++ b/orx-mesh-generators/src/main/kotlin/Sphere.kt @@ -1,84 +1,84 @@ -package org.openrndr.extras.meshgenerators - -import org.openrndr.draw.VertexBuffer -import org.openrndr.math.Spherical -import org.openrndr.math.Vector2 - -fun sphereMesh(sides: Int = 16, segments: Int = 16, radius: Double = 1.0, invert: Boolean = false): VertexBuffer { - val vertexCount = 2 * sides * 3 + Math.max(0, (segments - 2)) * sides * 6 - val vb = meshVertexBuffer(vertexCount) - vb.put { - generateSphere(sides, segments, radius, invert, bufferWriter(this)) - } - return vb -} - - -fun generateSphere(sides: Int, segments: Int, radius: Double = 1.0, invert: Boolean = false, writer: VertexWriter) { - val inverter = if (invert) -1.0 else 1.0 - for (t in 0 until segments) { - for (s in 0 until sides) { - val st00 = Spherical(radius, s * Math.PI * 2.0 / sides, t * Math.PI / segments) - val st01 = Spherical(radius, s * Math.PI * 2.0 / sides, (t + 1) * Math.PI / segments) - val st10 = Spherical(radius, (s + 1) * Math.PI * 2.0 / sides, t * Math.PI / segments) - val st11 = Spherical(radius, (s + 1) * Math.PI * 2.0 / sides, (t + 1) * Math.PI / segments) - - val thetaMax = Math.PI - val phiMax = Math.PI * 2.0 - - when (t) { - 0 -> { - writer(st00.cartesian, st00.cartesian.normalized * inverter, Vector2(st00.phi / phiMax, st00.theta / thetaMax)) - writer(st01.cartesian, st01.cartesian.normalized * inverter, Vector2(st01.phi / phiMax, st01.theta / thetaMax)) - writer(st11.cartesian, st11.cartesian.normalized * inverter, Vector2(st11.phi / phiMax, st11.theta / thetaMax)) - } - segments - 1 -> { - writer(st11.cartesian, st11.cartesian.normalized * inverter, Vector2(st11.phi / phiMax, st11.theta / thetaMax)) - writer(st10.cartesian, st10.cartesian.normalized * inverter, Vector2(st10.phi / phiMax, st10.theta / thetaMax)) - writer(st00.cartesian, st00.cartesian.normalized * inverter, Vector2(st00.phi / phiMax, st00.theta / thetaMax)) - } - else -> { - writer(st00.cartesian, st00.cartesian.normalized * inverter, Vector2(st00.phi / phiMax, st00.theta / thetaMax)) - writer(st01.cartesian, st01.cartesian.normalized * inverter, Vector2(st01.phi / phiMax, st01.theta / thetaMax)) - writer(st11.cartesian, st11.cartesian.normalized * inverter, Vector2(st11.phi / phiMax, st11.theta / thetaMax)) - - writer(st11.cartesian, st11.cartesian.normalized * inverter, Vector2(st11.phi / phiMax, st11.theta / thetaMax)) - writer(st10.cartesian, st10.cartesian.normalized * inverter, Vector2(st10.phi / phiMax, st10.theta / thetaMax)) - writer(st00.cartesian, st00.cartesian.normalized * inverter, Vector2(st00.phi / phiMax, st00.theta / thetaMax)) - } - } - } - } -} - -fun generateHemisphere(sides: Int, segments: Int, radius: Double = 1.0, invert: Boolean = false, writer: VertexWriter) { - val inverter = if (invert) -1.0 else 1.0 - for (t in 0 until segments) { - for (s in 0 until sides) { - val st00 = Spherical(radius, s * Math.PI * 2.0 / sides, t * Math.PI*0.5 / segments) - val st01 = Spherical(radius, s * Math.PI * 2.0 / sides, (t + 1) * Math.PI*0.5 / segments) - val st10 = Spherical(radius, (s + 1) * Math.PI * 2.0 / sides, t * Math.PI*0.5 / segments) - val st11 = Spherical(radius, (s + 1) * Math.PI * 2.0 / sides, (t + 1) * Math.PI*0.5 / segments) - - val thetaMax = Math.PI * 0.5 - val phiMax = Math.PI * 2.0 - - when (t) { - 0 -> { - writer(st00.cartesian, st00.cartesian.normalized * inverter, Vector2(st00.phi / phiMax, st00.theta / thetaMax)) - writer(st01.cartesian, st01.cartesian.normalized * inverter, Vector2(st01.phi / phiMax, st01.theta / thetaMax)) - writer(st11.cartesian, st11.cartesian.normalized * inverter, Vector2(st11.phi / phiMax, st11.theta / thetaMax)) - } - else -> { - writer(st00.cartesian, st00.cartesian.normalized * inverter, Vector2(st00.phi / phiMax, st00.theta / thetaMax)) - writer(st01.cartesian, st01.cartesian.normalized * inverter, Vector2(st01.phi / phiMax, st01.theta / thetaMax)) - writer(st11.cartesian, st11.cartesian.normalized * inverter, Vector2(st11.phi / phiMax, st11.theta / thetaMax)) - - writer(st11.cartesian, st11.cartesian.normalized * inverter, Vector2(st11.phi / phiMax, st11.theta / thetaMax)) - writer(st10.cartesian, st10.cartesian.normalized * inverter, Vector2(st10.phi / phiMax, st10.theta / thetaMax)) - writer(st00.cartesian, st00.cartesian.normalized * inverter, Vector2(st00.phi / phiMax, st00.theta / thetaMax)) - } - } - } - } +package org.openrndr.extras.meshgenerators + +import org.openrndr.draw.VertexBuffer +import org.openrndr.math.Spherical +import org.openrndr.math.Vector2 + +fun sphereMesh(sides: Int = 16, segments: Int = 16, radius: Double = 1.0, invert: Boolean = false): VertexBuffer { + val vertexCount = 2 * sides * 3 + Math.max(0, (segments - 2)) * sides * 6 + val vb = meshVertexBuffer(vertexCount) + vb.put { + generateSphere(sides, segments, radius, invert, bufferWriter(this)) + } + return vb +} + + +fun generateSphere(sides: Int, segments: Int, radius: Double = 1.0, invert: Boolean = false, writer: VertexWriter) { + val inverter = if (invert) -1.0 else 1.0 + for (t in 0 until segments) { + for (s in 0 until sides) { + val st00 = Spherical(radius, s * Math.PI * 2.0 / sides, t * Math.PI / segments) + val st01 = Spherical(radius, s * Math.PI * 2.0 / sides, (t + 1) * Math.PI / segments) + val st10 = Spherical(radius, (s + 1) * Math.PI * 2.0 / sides, t * Math.PI / segments) + val st11 = Spherical(radius, (s + 1) * Math.PI * 2.0 / sides, (t + 1) * Math.PI / segments) + + val thetaMax = Math.PI + val phiMax = Math.PI * 2.0 + + when (t) { + 0 -> { + writer(st00.cartesian, st00.cartesian.normalized * inverter, Vector2(st00.phi / phiMax, st00.theta / thetaMax)) + writer(st01.cartesian, st01.cartesian.normalized * inverter, Vector2(st01.phi / phiMax, st01.theta / thetaMax)) + writer(st11.cartesian, st11.cartesian.normalized * inverter, Vector2(st11.phi / phiMax, st11.theta / thetaMax)) + } + segments - 1 -> { + writer(st11.cartesian, st11.cartesian.normalized * inverter, Vector2(st11.phi / phiMax, st11.theta / thetaMax)) + writer(st10.cartesian, st10.cartesian.normalized * inverter, Vector2(st10.phi / phiMax, st10.theta / thetaMax)) + writer(st00.cartesian, st00.cartesian.normalized * inverter, Vector2(st00.phi / phiMax, st00.theta / thetaMax)) + } + else -> { + writer(st00.cartesian, st00.cartesian.normalized * inverter, Vector2(st00.phi / phiMax, st00.theta / thetaMax)) + writer(st01.cartesian, st01.cartesian.normalized * inverter, Vector2(st01.phi / phiMax, st01.theta / thetaMax)) + writer(st11.cartesian, st11.cartesian.normalized * inverter, Vector2(st11.phi / phiMax, st11.theta / thetaMax)) + + writer(st11.cartesian, st11.cartesian.normalized * inverter, Vector2(st11.phi / phiMax, st11.theta / thetaMax)) + writer(st10.cartesian, st10.cartesian.normalized * inverter, Vector2(st10.phi / phiMax, st10.theta / thetaMax)) + writer(st00.cartesian, st00.cartesian.normalized * inverter, Vector2(st00.phi / phiMax, st00.theta / thetaMax)) + } + } + } + } +} + +fun generateHemisphere(sides: Int, segments: Int, radius: Double = 1.0, invert: Boolean = false, writer: VertexWriter) { + val inverter = if (invert) -1.0 else 1.0 + for (t in 0 until segments) { + for (s in 0 until sides) { + val st00 = Spherical(radius, s * Math.PI * 2.0 / sides, t * Math.PI*0.5 / segments) + val st01 = Spherical(radius, s * Math.PI * 2.0 / sides, (t + 1) * Math.PI*0.5 / segments) + val st10 = Spherical(radius, (s + 1) * Math.PI * 2.0 / sides, t * Math.PI*0.5 / segments) + val st11 = Spherical(radius, (s + 1) * Math.PI * 2.0 / sides, (t + 1) * Math.PI*0.5 / segments) + + val thetaMax = Math.PI * 0.5 + val phiMax = Math.PI * 2.0 + + when (t) { + 0 -> { + writer(st00.cartesian, st00.cartesian.normalized * inverter, Vector2(st00.phi / phiMax, st00.theta / thetaMax)) + writer(st01.cartesian, st01.cartesian.normalized * inverter, Vector2(st01.phi / phiMax, st01.theta / thetaMax)) + writer(st11.cartesian, st11.cartesian.normalized * inverter, Vector2(st11.phi / phiMax, st11.theta / thetaMax)) + } + else -> { + writer(st00.cartesian, st00.cartesian.normalized * inverter, Vector2(st00.phi / phiMax, st00.theta / thetaMax)) + writer(st01.cartesian, st01.cartesian.normalized * inverter, Vector2(st01.phi / phiMax, st01.theta / thetaMax)) + writer(st11.cartesian, st11.cartesian.normalized * inverter, Vector2(st11.phi / phiMax, st11.theta / thetaMax)) + + writer(st11.cartesian, st11.cartesian.normalized * inverter, Vector2(st11.phi / phiMax, st11.theta / thetaMax)) + writer(st10.cartesian, st10.cartesian.normalized * inverter, Vector2(st10.phi / phiMax, st10.theta / thetaMax)) + writer(st00.cartesian, st00.cartesian.normalized * inverter, Vector2(st00.phi / phiMax, st00.theta / thetaMax)) + } + } + } + } } \ No newline at end of file diff --git a/orx-no-clear/README.md b/orx-no-clear/README.md index 6de33c339..4f435d5ea 100644 --- a/orx-no-clear/README.md +++ b/orx-no-clear/README.md @@ -1,54 +1,54 @@ -# orx-no-clear - -A simple OPENRNDR Extension that provides the classical drawing-without-clearing-the-screen functionality that -OPENRNDR does not support natively. - -#### Usage - -```kotlin -fun main() = application { - configure { - title = "NoClearProgram" - } - program { - backgroundColor = ColorRGBa.PINK - extend(NoClear()) - extend { - drawer.circle(Math.cos(seconds) * width / 2.0 + width / 2.0, Math.sin(seconds * 0.24) * height / 2.0 + height / 2.0, 20.0) - } - } -} -``` - -#### Usage with additional configuration -Optionally, a static `backdrop` may be setup by providing custom code. - -- Example 1. Customising the backdrop with an image -```kotlin -extend(NoClear()) { - val img = loadImage("data\\backdrop.png") - backdrop = { - drawer.image(img, 0.0, 0.0, width * 1.0, height * 1.0) - } -} -``` - -- Example 2. Customising the backdrop with a checker-board pattern -```kotlin -extend(NoClear()) { - backdrop = { - val xw = width / 8.0 - val yh = height / 8.0 - drawer.fill = ColorRGBa.RED - (0..7).forEach { row -> - (0..7).forEach { col -> - if ((row + col) % 2 == 0) { - drawer.rectangle(row * xw, col * yh, xw, yh) - } - } - } - } -} -``` - +# orx-no-clear + +A simple OPENRNDR Extension that provides the classical drawing-without-clearing-the-screen functionality that +OPENRNDR does not support natively. + +#### Usage + +```kotlin +fun main() = application { + configure { + title = "NoClearProgram" + } + program { + backgroundColor = ColorRGBa.PINK + extend(NoClear()) + extend { + drawer.circle(Math.cos(seconds) * width / 2.0 + width / 2.0, Math.sin(seconds * 0.24) * height / 2.0 + height / 2.0, 20.0) + } + } +} +``` + +#### Usage with additional configuration +Optionally, a static `backdrop` may be setup by providing custom code. + +- Example 1. Customising the backdrop with an image +```kotlin +extend(NoClear()) { + val img = loadImage("data\\backdrop.png") + backdrop = { + drawer.image(img, 0.0, 0.0, width * 1.0, height * 1.0) + } +} +``` + +- Example 2. Customising the backdrop with a checker-board pattern +```kotlin +extend(NoClear()) { + backdrop = { + val xw = width / 8.0 + val yh = height / 8.0 + drawer.fill = ColorRGBa.RED + (0..7).forEach { row -> + (0..7).forEach { col -> + if ((row + col) % 2 == 0) { + drawer.rectangle(row * xw, col * yh, xw, yh) + } + } + } + } +} +``` + NB! any submitted _lambda expression_ must be valid within the `renderTarget` context. \ No newline at end of file diff --git a/orx-no-clear/src/main/kotlin/NoClear.kt b/orx-no-clear/src/main/kotlin/NoClear.kt index 9a1872826..b68d5283e 100644 --- a/orx-no-clear/src/main/kotlin/NoClear.kt +++ b/orx-no-clear/src/main/kotlin/NoClear.kt @@ -1,57 +1,57 @@ -package org.openrndr.extra.noclear - -import org.openrndr.Extension -import org.openrndr.Program -import org.openrndr.color.ColorRGBa -import org.openrndr.draw.Drawer -import org.openrndr.draw.RenderTarget -import org.openrndr.draw.isolated -import org.openrndr.draw.renderTarget -import org.openrndr.math.Matrix44 - -class NoClear : Extension { - override var enabled: Boolean = true - private var renderTarget: RenderTarget? = null - - /** - * code-block to draw an optional custom backdrop - */ - var backdrop: (() -> Unit)? = null - - override fun beforeDraw(drawer: Drawer, program: Program) { - if (program.width > 0 && program.height > 0) { // only if the window is not minimised - if (renderTarget == null || renderTarget?.width != program.width || renderTarget?.height != program.height) { - renderTarget?.let { - it.colorBuffer(0).destroy() - it.detachColorBuffers() - it.destroy() - } - renderTarget = renderTarget(program.width, program.height) { - colorBuffer() - depthBuffer() - } - - renderTarget?.let { - drawer.withTarget(it) { - background(program.backgroundColor ?: ColorRGBa.TRANSPARENT) - backdrop?.invoke() // draw custom backdrop - } - } - } - } - renderTarget?.bind() - } - - override fun afterDraw(drawer: Drawer, program: Program) { - renderTarget?.unbind() - - renderTarget?.let { - drawer.isolated { - drawer.ortho() - drawer.view = Matrix44.IDENTITY - drawer.model = Matrix44.IDENTITY - drawer.image(it.colorBuffer(0)) - } - } - } +package org.openrndr.extra.noclear + +import org.openrndr.Extension +import org.openrndr.Program +import org.openrndr.color.ColorRGBa +import org.openrndr.draw.Drawer +import org.openrndr.draw.RenderTarget +import org.openrndr.draw.isolated +import org.openrndr.draw.renderTarget +import org.openrndr.math.Matrix44 + +class NoClear : Extension { + override var enabled: Boolean = true + private var renderTarget: RenderTarget? = null + + /** + * code-block to draw an optional custom backdrop + */ + var backdrop: (() -> Unit)? = null + + override fun beforeDraw(drawer: Drawer, program: Program) { + if (program.width > 0 && program.height > 0) { // only if the window is not minimised + if (renderTarget == null || renderTarget?.width != program.width || renderTarget?.height != program.height) { + renderTarget?.let { + it.colorBuffer(0).destroy() + it.detachColorBuffers() + it.destroy() + } + renderTarget = renderTarget(program.width, program.height) { + colorBuffer() + depthBuffer() + } + + renderTarget?.let { + drawer.withTarget(it) { + background(program.backgroundColor ?: ColorRGBa.TRANSPARENT) + backdrop?.invoke() // draw custom backdrop + } + } + } + } + renderTarget?.bind() + } + + override fun afterDraw(drawer: Drawer, program: Program) { + renderTarget?.unbind() + + renderTarget?.let { + drawer.isolated { + drawer.ortho() + drawer.view = Matrix44.IDENTITY + drawer.model = Matrix44.IDENTITY + drawer.image(it.colorBuffer(0)) + } + } + } } \ No newline at end of file diff --git a/orx-noise/README.md b/orx-noise/README.md index 7fe04b105..59d285640 100644 --- a/orx-noise/README.md +++ b/orx-noise/README.md @@ -1,123 +1,123 @@ -# orx-noise - -A collection of noisy functions - -## Uniform random numbers - -```kotlin -val sua = Double.uniform() -val sub = Double.uniform(-1.0, 1.0) - -val v2ua = Vector2.uniform() -val v2ub = Vector2.uniform(-1.0, 1.0) -val v2uc = Vector2.uniform(Vector2(0.0, 0.0), Vector2(1.0, 1.0)) -val v2ur = Vector2.uniformRing(0.5, 1.0) - -val v3ua = Vector3.uniform() -val v3ub = Vector3.uniform(-1.0, 1.0) -val v3uc = Vector3.uniform(Vector3(0.0, 0.0, 0.0), Vector3(1.0, 1.0, 1.0)) -val v3ur = Vector3.uniformRing(0.5, 1.0) - -val v4ua = Vector4.uniform() -val v4ub = Vector4.uniform(-1.0, 1.0) -val v4uc = Vector4.uniform(Vector4(0.0, 0.0, 0.0, 0.0), Vector4(1.0, 1.0, 1.0, 1.0)) -val v4ur = Vector4.uniformRing(0.5, 1.0) - -val ringSamples = List(500) { Vector2.uniformRing() } -``` - -## Multi-dimensional noise - -These are a mostly straight port from FastNoise-Java but have a slightly different interface. - -### Perlin noise -``` -// -- 2d -val v0 = perlinLinear(seed, x, y) -val v1 = perlinQuintic(seed, x, y) -val v2 = perlinHermite(seed, x, y) - -// -- 3d -val v3 = perlinLinear(seed, x, y, z) -val v4 = perlinQuintic(seed, x, y, z) -val v5 = perlinHermite(seed, x, y, z) -``` - -### Value noise -``` -// -- 2d -val v0 = valueLinear(seed, x, y) -val v1 = valueQuintic(seed, x, y) -val v2 = valueHermite(seed, x, y) - -// -- 3d -val v3 = valueLinear(seed, x, y, z) -val v4 = valueQuintic(seed, x, y, z) -val v5 = valueHermite(seed, x, y ,z) -``` - -### Simplex noise -``` -// -- 2d -val v0 = simplexLinear(seed, x, y) -val v1 = simplexQuintic(seed, x, y) -val v2 = simplexHermite(seed, x, y) - -// -- 3d -val v3 = simplexLinear(seed, x, y, z) -val v4 = simplexQuintic(seed, x, y, z) -val v5 = simplexHermite(seed, x, y ,z) -``` - -### Cubic noise -``` -// -- 2d -val v0 = cubicLinear(seed, x, y) -val v1 = cubicQuintic(seed, x, y) -val v2 = cubicHermite(seed, x, y) - -// -- 3d -val v3 = cubicLinear(seed, x, y, z) -val v4 = cubicQuintic(seed, x, y, z) -val v5 = cubicHermite(seed, x, y ,z) -``` - -### Fractal noise - -The library provides 3 functions with which fractal noise can be composed. - -#### Fractal brownian motion (FBM) - -``` -val v0 = fbm(seed, x, y, ::perlinLinear, octaves, lacunarity, gain) -val v1 = fbm(seed, x, y, ::simplexLinear, octaves, lacunarity, gain) -val v2 = fbm(seed, x, y, ::valueLinear, octaves, lacunarity, gain) - -val v3 = fbm(seed, x, y, z, ::perlinLinear, octaves, lacunarity, gain) -val v4 = fbm(seed, x, y, z, ::simplexLinear, octaves, lacunarity, gain) -val v5 = fbm(seed, x, y, z, ::valueLinear, octaves, lacunarity, gain) -``` - -#### Rigid - -``` -val v0 = rigid(seed, x, y, ::perlinLinear, octaves, lacunarity, gain) -val v1 = rigid(seed, x, y, ::simplexLinear, octaves, lacunarity, gain) -val v2 = rigid(seed, x, y, ::valueLinear, octaves, lacunarity, gain) - -val v3 = rigid(seed, x, y, z, ::perlinLinear, octaves, lacunarity, gain) -val v4 = rigid(seed, x, y, z, ::simplexLinear, octaves, lacunarity, gain) -val v5 = rigid(seed, x, y, z, ::valueLinear, octaves, lacunarity, gain) -``` - -#### Billow - -``` -val v0 = billow(seed, x, y, ::perlinLinear, octaves, lacunarity, gain) -val v1 = billow(seed, x, y, ::perlinLinear, octaves, lacunarity, gain) -val v2 = billow(seed, x, y, ::perlinLinear, octaves, lacunarity, gain) - -val v3 = billow(seed, x, y, z, ::perlinLinear, octaves, lacunarity, gain) -val v4 = billow(seed, x, y, z, ::perlinLinear, octaves, lacunarity, gain) -val v5 = billow(seed, x, y, z, ::perlinLinear, octaves, lacunarity, gain) +# orx-noise + +A collection of noisy functions + +## Uniform random numbers + +```kotlin +val sua = Double.uniform() +val sub = Double.uniform(-1.0, 1.0) + +val v2ua = Vector2.uniform() +val v2ub = Vector2.uniform(-1.0, 1.0) +val v2uc = Vector2.uniform(Vector2(0.0, 0.0), Vector2(1.0, 1.0)) +val v2ur = Vector2.uniformRing(0.5, 1.0) + +val v3ua = Vector3.uniform() +val v3ub = Vector3.uniform(-1.0, 1.0) +val v3uc = Vector3.uniform(Vector3(0.0, 0.0, 0.0), Vector3(1.0, 1.0, 1.0)) +val v3ur = Vector3.uniformRing(0.5, 1.0) + +val v4ua = Vector4.uniform() +val v4ub = Vector4.uniform(-1.0, 1.0) +val v4uc = Vector4.uniform(Vector4(0.0, 0.0, 0.0, 0.0), Vector4(1.0, 1.0, 1.0, 1.0)) +val v4ur = Vector4.uniformRing(0.5, 1.0) + +val ringSamples = List(500) { Vector2.uniformRing() } +``` + +## Multi-dimensional noise + +These are a mostly straight port from FastNoise-Java but have a slightly different interface. + +### Perlin noise +``` +// -- 2d +val v0 = perlinLinear(seed, x, y) +val v1 = perlinQuintic(seed, x, y) +val v2 = perlinHermite(seed, x, y) + +// -- 3d +val v3 = perlinLinear(seed, x, y, z) +val v4 = perlinQuintic(seed, x, y, z) +val v5 = perlinHermite(seed, x, y, z) +``` + +### Value noise +``` +// -- 2d +val v0 = valueLinear(seed, x, y) +val v1 = valueQuintic(seed, x, y) +val v2 = valueHermite(seed, x, y) + +// -- 3d +val v3 = valueLinear(seed, x, y, z) +val v4 = valueQuintic(seed, x, y, z) +val v5 = valueHermite(seed, x, y ,z) +``` + +### Simplex noise +``` +// -- 2d +val v0 = simplexLinear(seed, x, y) +val v1 = simplexQuintic(seed, x, y) +val v2 = simplexHermite(seed, x, y) + +// -- 3d +val v3 = simplexLinear(seed, x, y, z) +val v4 = simplexQuintic(seed, x, y, z) +val v5 = simplexHermite(seed, x, y ,z) +``` + +### Cubic noise +``` +// -- 2d +val v0 = cubicLinear(seed, x, y) +val v1 = cubicQuintic(seed, x, y) +val v2 = cubicHermite(seed, x, y) + +// -- 3d +val v3 = cubicLinear(seed, x, y, z) +val v4 = cubicQuintic(seed, x, y, z) +val v5 = cubicHermite(seed, x, y ,z) +``` + +### Fractal noise + +The library provides 3 functions with which fractal noise can be composed. + +#### Fractal brownian motion (FBM) + +``` +val v0 = fbm(seed, x, y, ::perlinLinear, octaves, lacunarity, gain) +val v1 = fbm(seed, x, y, ::simplexLinear, octaves, lacunarity, gain) +val v2 = fbm(seed, x, y, ::valueLinear, octaves, lacunarity, gain) + +val v3 = fbm(seed, x, y, z, ::perlinLinear, octaves, lacunarity, gain) +val v4 = fbm(seed, x, y, z, ::simplexLinear, octaves, lacunarity, gain) +val v5 = fbm(seed, x, y, z, ::valueLinear, octaves, lacunarity, gain) +``` + +#### Rigid + +``` +val v0 = rigid(seed, x, y, ::perlinLinear, octaves, lacunarity, gain) +val v1 = rigid(seed, x, y, ::simplexLinear, octaves, lacunarity, gain) +val v2 = rigid(seed, x, y, ::valueLinear, octaves, lacunarity, gain) + +val v3 = rigid(seed, x, y, z, ::perlinLinear, octaves, lacunarity, gain) +val v4 = rigid(seed, x, y, z, ::simplexLinear, octaves, lacunarity, gain) +val v5 = rigid(seed, x, y, z, ::valueLinear, octaves, lacunarity, gain) +``` + +#### Billow + +``` +val v0 = billow(seed, x, y, ::perlinLinear, octaves, lacunarity, gain) +val v1 = billow(seed, x, y, ::perlinLinear, octaves, lacunarity, gain) +val v2 = billow(seed, x, y, ::perlinLinear, octaves, lacunarity, gain) + +val v3 = billow(seed, x, y, z, ::perlinLinear, octaves, lacunarity, gain) +val v4 = billow(seed, x, y, z, ::perlinLinear, octaves, lacunarity, gain) +val v5 = billow(seed, x, y, z, ::perlinLinear, octaves, lacunarity, gain) ``` \ No newline at end of file diff --git a/orx-noise/src/main/kotlin/CubicNoise2D.kt b/orx-noise/src/main/kotlin/CubicNoise2D.kt index f20299cc1..afcb1b427 100644 --- a/orx-noise/src/main/kotlin/CubicNoise2D.kt +++ b/orx-noise/src/main/kotlin/CubicNoise2D.kt @@ -1,28 +1,28 @@ -package org.openrndr.extra.noise - -private const val CUBIC_2D_BOUNDING = 1 / (1.5 * 1.5).toFloat() -fun cubic(seed: Int, x: Double, y: Double): Double { - val x1 = x.fastFloor() - val y1 = y.fastFloor() - - val x0 = x1 - 1 - val y0 = y1 - 1 - val x2 = x1 + 1 - val y2 = y1 + 1 - val x3 = x1 + 2 - val y3 = y1 + 2 - - val xs = x - x1.toDouble() - val ys = y - y1.toDouble() - - return cubic( - cubic(valCoord2D(seed, x0, y0), valCoord2D(seed, x1, y0), valCoord2D(seed, x2, y0), valCoord2D(seed, x3, y0), - xs), - cubic(valCoord2D(seed, x0, y1), valCoord2D(seed, x1, y1), valCoord2D(seed, x2, y1), valCoord2D(seed, x3, y1), - xs), - cubic(valCoord2D(seed, x0, y2), valCoord2D(seed, x1, y2), valCoord2D(seed, x2, y2), valCoord2D(seed, x3, y2), - xs), - cubic(valCoord2D(seed, x0, y3), valCoord2D(seed, x1, y3), valCoord2D(seed, x2, y3), valCoord2D(seed, x3, y3), - xs), - ys) * CUBIC_2D_BOUNDING +package org.openrndr.extra.noise + +private const val CUBIC_2D_BOUNDING = 1 / (1.5 * 1.5).toFloat() +fun cubic(seed: Int, x: Double, y: Double): Double { + val x1 = x.fastFloor() + val y1 = y.fastFloor() + + val x0 = x1 - 1 + val y0 = y1 - 1 + val x2 = x1 + 1 + val y2 = y1 + 1 + val x3 = x1 + 2 + val y3 = y1 + 2 + + val xs = x - x1.toDouble() + val ys = y - y1.toDouble() + + return cubic( + cubic(valCoord2D(seed, x0, y0), valCoord2D(seed, x1, y0), valCoord2D(seed, x2, y0), valCoord2D(seed, x3, y0), + xs), + cubic(valCoord2D(seed, x0, y1), valCoord2D(seed, x1, y1), valCoord2D(seed, x2, y1), valCoord2D(seed, x3, y1), + xs), + cubic(valCoord2D(seed, x0, y2), valCoord2D(seed, x1, y2), valCoord2D(seed, x2, y2), valCoord2D(seed, x3, y2), + xs), + cubic(valCoord2D(seed, x0, y3), valCoord2D(seed, x1, y3), valCoord2D(seed, x2, y3), valCoord2D(seed, x3, y3), + xs), + ys) * CUBIC_2D_BOUNDING } \ No newline at end of file diff --git a/orx-noise/src/main/kotlin/CubicNoise3D.kt b/orx-noise/src/main/kotlin/CubicNoise3D.kt index 584b75874..de22f689e 100644 --- a/orx-noise/src/main/kotlin/CubicNoise3D.kt +++ b/orx-noise/src/main/kotlin/CubicNoise3D.kt @@ -1,51 +1,51 @@ -package org.openrndr.extra.noise - - -private const val CUBIC_3D_BOUNDING = 1 / (1.5 * 1.5 * 1.5).toFloat() - -fun cubic(seed: Int, x: Double, y: Double, z: Double): Double { - val x1 = x.fastFloor() - val y1 = y.fastFloor() - val z1 = z.fastFloor() - - val x0 = x1 - 1 - val y0 = y1 - 1 - val z0 = z1 - 1 - val x2 = x1 + 1 - val y2 = y1 + 1 - val z2 = z1 + 1 - val x3 = x1 + 2 - val y3 = y1 + 2 - val z3 = z1 + 2 - - val xs = x - x1.toFloat() - val ys = y - y1.toFloat() - val zs = z - z1.toFloat() - - return cubic( - cubic( - cubic(valCoord3D(seed, x0, y0, z0), valCoord3D(seed, x1, y0, z0), valCoord3D(seed, x2, y0, z0), valCoord3D(seed, x3, y0, z0), xs), - cubic(valCoord3D(seed, x0, y1, z0), valCoord3D(seed, x1, y1, z0), valCoord3D(seed, x2, y1, z0), valCoord3D(seed, x3, y1, z0), xs), - cubic(valCoord3D(seed, x0, y2, z0), valCoord3D(seed, x1, y2, z0), valCoord3D(seed, x2, y2, z0), valCoord3D(seed, x3, y2, z0), xs), - cubic(valCoord3D(seed, x0, y3, z0), valCoord3D(seed, x1, y3, z0), valCoord3D(seed, x2, y3, z0), valCoord3D(seed, x3, y3, z0), xs), - ys), - cubic( - cubic(valCoord3D(seed, x0, y0, z1), valCoord3D(seed, x1, y0, z1), valCoord3D(seed, x2, y0, z1), valCoord3D(seed, x3, y0, z1), xs), - cubic(valCoord3D(seed, x0, y1, z1), valCoord3D(seed, x1, y1, z1), valCoord3D(seed, x2, y1, z1), valCoord3D(seed, x3, y1, z1), xs), - cubic(valCoord3D(seed, x0, y2, z1), valCoord3D(seed, x1, y2, z1), valCoord3D(seed, x2, y2, z1), valCoord3D(seed, x3, y2, z1), xs), - cubic(valCoord3D(seed, x0, y3, z1), valCoord3D(seed, x1, y3, z1), valCoord3D(seed, x2, y3, z1), valCoord3D(seed, x3, y3, z1), xs), - ys), - cubic( - cubic(valCoord3D(seed, x0, y0, z2), valCoord3D(seed, x1, y0, z2), valCoord3D(seed, x2, y0, z2), valCoord3D(seed, x3, y0, z2), xs), - cubic(valCoord3D(seed, x0, y1, z2), valCoord3D(seed, x1, y1, z2), valCoord3D(seed, x2, y1, z2), valCoord3D(seed, x3, y1, z2), xs), - cubic(valCoord3D(seed, x0, y2, z2), valCoord3D(seed, x1, y2, z2), valCoord3D(seed, x2, y2, z2), valCoord3D(seed, x3, y2, z2), xs), - cubic(valCoord3D(seed, x0, y3, z2), valCoord3D(seed, x1, y3, z2), valCoord3D(seed, x2, y3, z2), valCoord3D(seed, x3, y3, z2), xs), - ys), - cubic( - cubic(valCoord3D(seed, x0, y0, z3), valCoord3D(seed, x1, y0, z3), valCoord3D(seed, x2, y0, z3), valCoord3D(seed, x3, y0, z3), xs), - cubic(valCoord3D(seed, x0, y1, z3), valCoord3D(seed, x1, y1, z3), valCoord3D(seed, x2, y1, z3), valCoord3D(seed, x3, y1, z3), xs), - cubic(valCoord3D(seed, x0, y2, z3), valCoord3D(seed, x1, y2, z3), valCoord3D(seed, x2, y2, z3), valCoord3D(seed, x3, y2, z3), xs), - cubic(valCoord3D(seed, x0, y3, z3), valCoord3D(seed, x1, y3, z3), valCoord3D(seed, x2, y3, z3), valCoord3D(seed, x3, y3, z3), xs), - ys), - zs) * CUBIC_3D_BOUNDING +package org.openrndr.extra.noise + + +private const val CUBIC_3D_BOUNDING = 1 / (1.5 * 1.5 * 1.5).toFloat() + +fun cubic(seed: Int, x: Double, y: Double, z: Double): Double { + val x1 = x.fastFloor() + val y1 = y.fastFloor() + val z1 = z.fastFloor() + + val x0 = x1 - 1 + val y0 = y1 - 1 + val z0 = z1 - 1 + val x2 = x1 + 1 + val y2 = y1 + 1 + val z2 = z1 + 1 + val x3 = x1 + 2 + val y3 = y1 + 2 + val z3 = z1 + 2 + + val xs = x - x1.toFloat() + val ys = y - y1.toFloat() + val zs = z - z1.toFloat() + + return cubic( + cubic( + cubic(valCoord3D(seed, x0, y0, z0), valCoord3D(seed, x1, y0, z0), valCoord3D(seed, x2, y0, z0), valCoord3D(seed, x3, y0, z0), xs), + cubic(valCoord3D(seed, x0, y1, z0), valCoord3D(seed, x1, y1, z0), valCoord3D(seed, x2, y1, z0), valCoord3D(seed, x3, y1, z0), xs), + cubic(valCoord3D(seed, x0, y2, z0), valCoord3D(seed, x1, y2, z0), valCoord3D(seed, x2, y2, z0), valCoord3D(seed, x3, y2, z0), xs), + cubic(valCoord3D(seed, x0, y3, z0), valCoord3D(seed, x1, y3, z0), valCoord3D(seed, x2, y3, z0), valCoord3D(seed, x3, y3, z0), xs), + ys), + cubic( + cubic(valCoord3D(seed, x0, y0, z1), valCoord3D(seed, x1, y0, z1), valCoord3D(seed, x2, y0, z1), valCoord3D(seed, x3, y0, z1), xs), + cubic(valCoord3D(seed, x0, y1, z1), valCoord3D(seed, x1, y1, z1), valCoord3D(seed, x2, y1, z1), valCoord3D(seed, x3, y1, z1), xs), + cubic(valCoord3D(seed, x0, y2, z1), valCoord3D(seed, x1, y2, z1), valCoord3D(seed, x2, y2, z1), valCoord3D(seed, x3, y2, z1), xs), + cubic(valCoord3D(seed, x0, y3, z1), valCoord3D(seed, x1, y3, z1), valCoord3D(seed, x2, y3, z1), valCoord3D(seed, x3, y3, z1), xs), + ys), + cubic( + cubic(valCoord3D(seed, x0, y0, z2), valCoord3D(seed, x1, y0, z2), valCoord3D(seed, x2, y0, z2), valCoord3D(seed, x3, y0, z2), xs), + cubic(valCoord3D(seed, x0, y1, z2), valCoord3D(seed, x1, y1, z2), valCoord3D(seed, x2, y1, z2), valCoord3D(seed, x3, y1, z2), xs), + cubic(valCoord3D(seed, x0, y2, z2), valCoord3D(seed, x1, y2, z2), valCoord3D(seed, x2, y2, z2), valCoord3D(seed, x3, y2, z2), xs), + cubic(valCoord3D(seed, x0, y3, z2), valCoord3D(seed, x1, y3, z2), valCoord3D(seed, x2, y3, z2), valCoord3D(seed, x3, y3, z2), xs), + ys), + cubic( + cubic(valCoord3D(seed, x0, y0, z3), valCoord3D(seed, x1, y0, z3), valCoord3D(seed, x2, y0, z3), valCoord3D(seed, x3, y0, z3), xs), + cubic(valCoord3D(seed, x0, y1, z3), valCoord3D(seed, x1, y1, z3), valCoord3D(seed, x2, y1, z3), valCoord3D(seed, x3, y1, z3), xs), + cubic(valCoord3D(seed, x0, y2, z3), valCoord3D(seed, x1, y2, z3), valCoord3D(seed, x2, y2, z3), valCoord3D(seed, x3, y2, z3), xs), + cubic(valCoord3D(seed, x0, y3, z3), valCoord3D(seed, x1, y3, z3), valCoord3D(seed, x2, y3, z3), valCoord3D(seed, x3, y3, z3), xs), + ys), + zs) * CUBIC_3D_BOUNDING } \ No newline at end of file diff --git a/orx-noise/src/main/kotlin/Fractal.kt b/orx-noise/src/main/kotlin/Fractal.kt index 8bb926fb8..78ed02570 100644 --- a/orx-noise/src/main/kotlin/Fractal.kt +++ b/orx-noise/src/main/kotlin/Fractal.kt @@ -1,105 +1,105 @@ -package org.openrndr.extra.noise - -inline fun fbm(seed: Int, x: Double, y: Double, z: Double, crossinline noise: (Int, Double, Double, Double) -> Double, - octaves: Int = 8, lacunarity: Double = 0.5, gain: Double = 0.5): Double { - var sum = noise(seed, x, y, z) - var amp = 1.0 - - var x = x - var y = y - var z = z - for (i in 1 until octaves) { - x *= lacunarity - y *= lacunarity - z *= lacunarity - amp *= gain - sum += noise(seed + i, x, y, z) * amp - } - return sum -} - - -inline fun fbm(seed: Int, x: Double, y: Double, crossinline noise: (Int, Double, Double) -> Double, - octaves: Int = 8, lacunarity: Double = 0.5, gain: Double = 0.5): Double { - var sum = noise(seed, x, y) - var amp = 1.0 - - var x = x - var y = y - for (i in 1 until octaves) { - x *= lacunarity - y *= lacunarity - amp *= gain - sum += noise(seed + i, x, y) * amp - } - return sum -} - -inline fun billow(seed: Int, x: Double, y: Double, z: Double, crossinline noise: (Int, Double, Double, Double) -> Double, - octaves: Int = 8, lacunarity: Double = 0.5, gain: Double = 0.5) : Double { - var sum = Math.abs(noise(seed, x, y, z) * 2.0 - 1.0) - var amp = 1.0 - - var x = x - var y = y - var z = z - for (i in 1 until octaves) { - x *= lacunarity - y *= lacunarity - z *= lacunarity - amp *= gain - sum += Math.abs(noise(seed + i, x, y, z) * 2.0 - 1.0) * amp - } - return sum -} - -inline fun billow(seed: Int, x: Double, y: Double, crossinline noise: (Int, Double, Double) -> Double, - octaves: Int = 8, lacunarity: Double = 0.5, gain: Double = 0.5) : Double { - var sum = Math.abs(noise(seed, x, y) * 2.0 - 1.0) - var amp = 1.0 - - var x = x - var y = y - for (i in 1 until octaves) { - x *= lacunarity - y *= lacunarity - amp *= gain - sum += Math.abs(noise(seed + i, x, y) * 2.0 - 1.0) * amp - } - return sum -} - -inline fun rigid(seed: Int, x: Double, y: Double, crossinline noise: (Int, Double, Double) -> Double, - octaves: Int = 8, lacunarity: Double = 0.5, gain: Double = 0.5): Double { - var sum = 1.0 - Math.abs(noise(seed, x, y)) - var amp = 1.0 - - var x = x - var y = y - for (i in 1 until octaves) { - x *= lacunarity - y *= lacunarity - amp *= gain - sum -= (1.0 - Math.abs(noise(seed + i, x, y))) * amp - } - return sum -} - -inline fun rigid(seed: Int, x: Double, y: Double, z: Double, crossinline noise: (Int, Double, Double, Double) -> Double, - octaves: Int = 8, lacunarity: Double = 0.5, gain: Double = 0.5): Double { - var sum = 1.0 - Math.abs(noise(seed, x, y, z)) - var amp = 1.0 - - var x = x - var y = y - var z = z - for (i in 1 until octaves) { - x *= lacunarity - y *= lacunarity - z *= lacunarity - amp *= gain - sum -= (1.0 - Math.abs(noise(seed + i, x, y, z))) * amp - } - return sum -} - +package org.openrndr.extra.noise + +inline fun fbm(seed: Int, x: Double, y: Double, z: Double, crossinline noise: (Int, Double, Double, Double) -> Double, + octaves: Int = 8, lacunarity: Double = 0.5, gain: Double = 0.5): Double { + var sum = noise(seed, x, y, z) + var amp = 1.0 + + var x = x + var y = y + var z = z + for (i in 1 until octaves) { + x *= lacunarity + y *= lacunarity + z *= lacunarity + amp *= gain + sum += noise(seed + i, x, y, z) * amp + } + return sum +} + + +inline fun fbm(seed: Int, x: Double, y: Double, crossinline noise: (Int, Double, Double) -> Double, + octaves: Int = 8, lacunarity: Double = 0.5, gain: Double = 0.5): Double { + var sum = noise(seed, x, y) + var amp = 1.0 + + var x = x + var y = y + for (i in 1 until octaves) { + x *= lacunarity + y *= lacunarity + amp *= gain + sum += noise(seed + i, x, y) * amp + } + return sum +} + +inline fun billow(seed: Int, x: Double, y: Double, z: Double, crossinline noise: (Int, Double, Double, Double) -> Double, + octaves: Int = 8, lacunarity: Double = 0.5, gain: Double = 0.5) : Double { + var sum = Math.abs(noise(seed, x, y, z) * 2.0 - 1.0) + var amp = 1.0 + + var x = x + var y = y + var z = z + for (i in 1 until octaves) { + x *= lacunarity + y *= lacunarity + z *= lacunarity + amp *= gain + sum += Math.abs(noise(seed + i, x, y, z) * 2.0 - 1.0) * amp + } + return sum +} + +inline fun billow(seed: Int, x: Double, y: Double, crossinline noise: (Int, Double, Double) -> Double, + octaves: Int = 8, lacunarity: Double = 0.5, gain: Double = 0.5) : Double { + var sum = Math.abs(noise(seed, x, y) * 2.0 - 1.0) + var amp = 1.0 + + var x = x + var y = y + for (i in 1 until octaves) { + x *= lacunarity + y *= lacunarity + amp *= gain + sum += Math.abs(noise(seed + i, x, y) * 2.0 - 1.0) * amp + } + return sum +} + +inline fun rigid(seed: Int, x: Double, y: Double, crossinline noise: (Int, Double, Double) -> Double, + octaves: Int = 8, lacunarity: Double = 0.5, gain: Double = 0.5): Double { + var sum = 1.0 - Math.abs(noise(seed, x, y)) + var amp = 1.0 + + var x = x + var y = y + for (i in 1 until octaves) { + x *= lacunarity + y *= lacunarity + amp *= gain + sum -= (1.0 - Math.abs(noise(seed + i, x, y))) * amp + } + return sum +} + +inline fun rigid(seed: Int, x: Double, y: Double, z: Double, crossinline noise: (Int, Double, Double, Double) -> Double, + octaves: Int = 8, lacunarity: Double = 0.5, gain: Double = 0.5): Double { + var sum = 1.0 - Math.abs(noise(seed, x, y, z)) + var amp = 1.0 + + var x = x + var y = y + var z = z + for (i in 1 until octaves) { + x *= lacunarity + y *= lacunarity + z *= lacunarity + amp *= gain + sum -= (1.0 - Math.abs(noise(seed + i, x, y, z))) * amp + } + return sum +} + diff --git a/orx-noise/src/main/kotlin/GradCoord.kt b/orx-noise/src/main/kotlin/GradCoord.kt index 14ba46c97..91d84c868 100644 --- a/orx-noise/src/main/kotlin/GradCoord.kt +++ b/orx-noise/src/main/kotlin/GradCoord.kt @@ -1,156 +1,156 @@ -package org.openrndr.extra.noise - -import org.openrndr.math.Vector2 -import org.openrndr.math.Vector3 - - - -private const val X_PRIME = 1619 -private const val Y_PRIME = 31337 -private const val Z_PRIME = 6971 -private const val W_PRIME = 1013 - - -private val GRAD_2D = arrayOf(Vector2(-1.0, -1.0), Vector2(1.0, -1.0), Vector2(-1.0, 1.0), - Vector2(1.0, 1.0), Vector2(0.0, -1.0), Vector2(-1.0, 0.0), - Vector2(0.0, 1.0), Vector2(1.0, 0.0)) - - -private val GRAD_3D = arrayOf( - Vector3(1.0, 1.0, 0.0), Vector3(-1.0, 1.0, 0.0), Vector3(1.0, -1.0, 0.0), Vector3(-1.0, -1.0, 0.0), - Vector3(1.0, 0.0, 1.0), Vector3(-1.0, 0.0, 1.0), Vector3(1.0, 0.0, -1.0), Vector3(-1.0, 0.0, -1.0), - Vector3(0.0, 1.0, 1.0), Vector3(0.0, -1.0, 1.0), Vector3(0.0, 1.0, -1.0), Vector3(0.0, -1.0, -1.0), - Vector3(1.0, 1.0, 0.0), Vector3(0.0, -1.0, 1.0), Vector3(-1.0, 1.0, 0.0), Vector3(0.0, -1.0, -1.0)) - -fun gradCoord2D(seed: Int, x: Int, y: Int, xd: Double, yd: Double): Double { - var hash = seed - hash = hash xor X_PRIME * x - hash = hash xor Y_PRIME * y - - hash = hash * hash * hash * 60493 - hash = hash shr 13 xor hash - - val (x1, y1) = GRAD_2D[hash and 7] - - return xd * x1 + yd * y1 -} - -fun gradCoord3D(seed: Int, x: Int, y: Int, z: Int, xd: Double, yd: Double, zd: Double): Double { - var hash = seed - hash = hash xor X_PRIME * x - hash = hash xor Y_PRIME * y - hash = hash xor Z_PRIME * z - - hash *= hash * hash * 60493 - hash = hash shr 13 xor hash - - val g = GRAD_3D[hash and 15] - - return xd * g.x + yd * g.y + zd * g.z -} - - -fun gradCoord4D(seed: Int, x: Int, y: Int, z: Int, w: Int, xd: Double, yd: Double, zd: Double, wd: Double): Double { - var hash = seed - hash = hash xor X_PRIME * x - hash = hash xor Y_PRIME * y - hash = hash xor Z_PRIME * z - hash = hash xor W_PRIME * w - - hash = hash * hash * hash * 60493 - hash = hash shr 13 xor hash - - hash = hash and 31 - var a = yd - var b = zd - var c = wd // X,Y,Z - when (hash shr 3) { - // OR, DEPENDING ON HIGH ORDER 2 BITS: - 1 -> { - a = wd - b = xd - c = yd - } - 2 -> { - a = zd - b = wd - c = xd - } - 3 -> { - a = yd - b = zd - c = wd - } - }// W,X,Y - // Z,W,X - // Y,Z,W - return (if (hash and 4 == 0) -a else a) + (if (hash and 2 == 0) -b else b) + if (hash and 1 == 0) -c else c -} - - -fun hash2D(seed: Int, x: Int, y: Int): Int { - var hash = seed - hash = hash xor X_PRIME * x - hash = hash xor Y_PRIME * y - - hash = hash * hash * hash * 60493 - hash = hash shr 13 xor hash - - return hash -} - -fun hash3D(seed: Int, x: Int, y: Int, z: Int): Int { - var hash = seed - hash = hash xor X_PRIME * x - hash = hash xor Y_PRIME * y - hash = hash xor Z_PRIME * z - - hash = hash * hash * hash * 60493 - hash = hash shr 13 xor hash - - return hash -} - -fun hash4D(seed: Int, x: Int, y: Int, z: Int, w: Int): Int { - var hash = seed - hash = hash xor X_PRIME * x - hash = hash xor Y_PRIME * y - hash = hash xor Z_PRIME * z - hash = hash xor W_PRIME * w - - hash = hash * hash * hash * 60493 - hash = hash shr 13 xor hash - return hash -} - - -fun valCoord2D(seed: Int, x: Int, y: Int): Double { - var n = seed - n = n xor X_PRIME * x - n = n xor Y_PRIME * y - - return n * n * n * 60493 / 2147483648.0 -} - -fun valCoord3D(seed: Int, x: Int, y: Int, z: Int): Double { - var n = seed - n = n xor X_PRIME * x - n = n xor Y_PRIME * y - n = n xor Z_PRIME * z - - return n * n * n * 60493 / 2147483648.0 -} - - -private fun valCoord4D(seed: Int, x: Int, y: Int, z: Int, w: Int): Double { - var n = seed - n = n xor X_PRIME * x - n = n xor Y_PRIME * y - n = n xor Z_PRIME * z - n = n xor W_PRIME * w - - return n * n * n * 60493 / 2147483648.0 -} - -private val CELL_2D = arrayOf(Vector2(-0.4313539279, 0.1281943404), Vector2(-0.1733316799, 0.415278375), Vector2(-0.2821957395, -0.3505218461), Vector2(-0.2806473808, 0.3517627718), Vector2(0.3125508975, -0.3237467165), Vector2(0.3383018443, -0.2967353402), Vector2(-0.4393982022, -0.09710417025), Vector2(-0.4460443703, -0.05953502905), Vector2(-0.302223039, 0.3334085102), Vector2(-0.212681052, -0.3965687458), Vector2(-0.2991156529, 0.3361990872), Vector2(0.2293323691, 0.3871778202), Vector2(0.4475439151, -0.04695150755), Vector2(0.1777518, 0.41340573), Vector2(0.1688522499, -0.4171197882), Vector2(-0.0976597166, 0.4392750616), Vector2(0.08450188373, 0.4419948321), Vector2(-0.4098760448, -0.1857461384), Vector2(0.3476585782, -0.2857157906), Vector2(-0.3350670039, -0.30038326), Vector2(0.2298190031, -0.3868891648), Vector2(-0.01069924099, 0.449872789), Vector2(-0.4460141246, -0.05976119672), Vector2(0.3650293864, 0.2631606867), Vector2(-0.349479423, 0.2834856838), Vector2(-0.4122720642, 0.1803655873), Vector2(-0.267327811, 0.3619887311), Vector2(0.322124041, -0.3142230135), Vector2(0.2880445931, -0.3457315612), Vector2(0.3892170926, -0.2258540565), Vector2(0.4492085018, -0.02667811596), Vector2(-0.4497724772, 0.01430799601), Vector2(0.1278175387, -0.4314657307), Vector2(-0.03572100503, 0.4485799926), Vector2(-0.4297407068, -0.1335025276), Vector2(-0.3217817723, 0.3145735065), Vector2(-0.3057158873, 0.3302087162), Vector2(-0.414503978, 0.1751754899), Vector2(-0.3738139881, 0.2505256519), Vector2(0.2236891408, -0.3904653228), Vector2(0.002967775577, -0.4499902136), Vector2(0.1747128327, -0.4146991995), Vector2(-0.4423772489, -0.08247647938), Vector2(-0.2763960987, -0.355112935), Vector2(-0.4019385906, -0.2023496216), Vector2(0.3871414161, -0.2293938184), Vector2(-0.430008727, 0.1326367019), Vector2(-0.03037574274, -0.4489736231), Vector2(-0.3486181573, 0.2845441624), Vector2(0.04553517144, -0.4476902368), Vector2(-0.0375802926, 0.4484280562), Vector2(0.3266408905, 0.3095250049), Vector2(0.06540017593, -0.4452222108), Vector2(0.03409025829, 0.448706869), Vector2(-0.4449193635, 0.06742966669), Vector2(-0.4255936157, -0.1461850686), Vector2(0.449917292, 0.008627302568), Vector2(0.05242606404, 0.4469356864), Vector2(-0.4495305179, -0.02055026661), Vector2(-0.1204775703, 0.4335725488), Vector2(-0.341986385, -0.2924813028), Vector2(0.3865320182, 0.2304191809), Vector2(0.04506097811, -0.447738214), Vector2(-0.06283465979, 0.4455915232), Vector2(0.3932600341, -0.2187385324), Vector2(0.4472261803, -0.04988730975), Vector2(0.3753571011, -0.2482076684), Vector2(-0.273662295, 0.357223947), Vector2(0.1700461538, 0.4166344988), Vector2(0.4102692229, 0.1848760794), Vector2(0.323227187, -0.3130881435), Vector2(-0.2882310238, -0.3455761521), Vector2(0.2050972664, 0.4005435199), Vector2(0.4414085979, -0.08751256895), Vector2(-0.1684700334, 0.4172743077), Vector2(-0.003978032396, 0.4499824166), Vector2(-0.2055133639, 0.4003301853), Vector2(-0.006095674897, -0.4499587123), Vector2(-0.1196228124, -0.4338091548), Vector2(0.3901528491, -0.2242337048), Vector2(0.01723531752, 0.4496698165), Vector2(-0.3015070339, 0.3340561458), Vector2(-0.01514262423, -0.4497451511), Vector2(-0.4142574071, -0.1757577897), Vector2(-0.1916377265, -0.4071547394), Vector2(0.3749248747, 0.2488600778), Vector2(-0.2237774255, 0.3904147331), Vector2(-0.4166343106, -0.1700466149), Vector2(0.3619171625, 0.267424695), Vector2(0.1891126846, -0.4083336779), Vector2(-0.3127425077, 0.323561623), Vector2(-0.3281807787, 0.307891826), Vector2(-0.2294806661, 0.3870899429), Vector2(-0.3445266136, 0.2894847362), Vector2(-0.4167095422, -0.1698621719), Vector2(-0.257890321, -0.3687717212), Vector2(-0.3612037825, 0.2683874578), Vector2(0.2267996491, 0.3886668486), Vector2(0.207157062, 0.3994821043), Vector2(0.08355176718, -0.4421754202), Vector2(-0.4312233307, 0.1286329626), Vector2(0.3257055497, 0.3105090899), Vector2(0.177701095, -0.4134275279), Vector2(-0.445182522, 0.06566979625), Vector2(0.3955143435, 0.2146355146), Vector2(-0.4264613988, 0.1436338239), Vector2(-0.3793799665, -0.2420141339), Vector2(0.04617599081, -0.4476245948), Vector2(-0.371405428, -0.2540826796), Vector2(0.2563570295, -0.3698392535), Vector2(0.03476646309, 0.4486549822), Vector2(-0.3065454405, 0.3294387544), Vector2(-0.2256979823, 0.3893076172), Vector2(0.4116448463, -0.1817925206), Vector2(-0.2907745828, -0.3434387019), Vector2(0.2842278468, -0.348876097), Vector2(0.3114589359, -0.3247973695), Vector2(0.4464155859, -0.0566844308), Vector2(-0.3037334033, -0.3320331606), Vector2(0.4079607166, 0.1899159123), Vector2(-0.3486948919, -0.2844501228), Vector2(0.3264821436, 0.3096924441), Vector2(0.3211142406, 0.3152548881), Vector2(0.01183382662, 0.4498443737), Vector2(0.4333844092, 0.1211526057), Vector2(0.3118668416, 0.324405723), Vector2(-0.272753471, 0.3579183483), Vector2(-0.422228622, -0.1556373694), Vector2(-0.1009700099, -0.4385260051), Vector2(-0.2741171231, -0.3568750521), Vector2(-0.1465125133, 0.4254810025), Vector2(0.2302279044, -0.3866459777), Vector2(-0.3699435608, 0.2562064828), Vector2(0.105700352, -0.4374099171), Vector2(-0.2646713633, 0.3639355292), Vector2(0.3521828122, 0.2801200935), Vector2(-0.1864187807, -0.4095705534), Vector2(0.1994492955, -0.4033856449), Vector2(0.3937065066, 0.2179339044), Vector2(-0.3226158377, 0.3137180602), Vector2(0.3796235338, 0.2416318948), Vector2(0.1482921929, 0.4248640083), Vector2(-0.407400394, 0.1911149365), Vector2(0.4212853031, 0.1581729856), Vector2(-0.2621297173, 0.3657704353), Vector2(-0.2536986953, -0.3716678248), Vector2(-0.2100236383, 0.3979825013), Vector2(0.3624152444, 0.2667493029), Vector2(-0.3645038479, -0.2638881295), Vector2(0.2318486784, 0.3856762766), Vector2(-0.3260457004, 0.3101519002), Vector2(-0.2130045332, -0.3963950918), Vector2(0.3814998766, -0.2386584257), Vector2(-0.342977305, 0.2913186713), Vector2(-0.4355865605, 0.1129794154), Vector2(-0.2104679605, 0.3977477059), Vector2(0.3348364681, -0.3006402163), Vector2(0.3430468811, 0.2912367377), Vector2(-0.2291836801, -0.3872658529), Vector2(0.2547707298, -0.3709337882), Vector2(0.4236174945, -0.151816397), Vector2(-0.15387742, 0.4228731957), Vector2(-0.4407449312, 0.09079595574), Vector2(-0.06805276192, -0.444824484), Vector2(0.4453517192, -0.06451237284), Vector2(0.2562464609, -0.3699158705), Vector2(0.3278198355, -0.3082761026), Vector2(-0.4122774207, -0.1803533432), Vector2(0.3354090914, -0.3000012356), Vector2(0.446632869, -0.05494615882), Vector2(-0.1608953296, 0.4202531296), Vector2(-0.09463954939, 0.4399356268), Vector2(-0.02637688324, -0.4492262904), Vector2(0.447102804, -0.05098119915), Vector2(-0.4365670908, 0.1091291678), Vector2(-0.3959858651, 0.2137643437), Vector2(-0.4240048207, -0.1507312575), Vector2(-0.3882794568, 0.2274622243), Vector2(-0.4283652566, -0.1378521198), Vector2(0.3303888091, 0.305521251), Vector2(0.3321434919, -0.3036127481), Vector2(-0.413021046, -0.1786438231), Vector2(0.08403060337, -0.4420846725), Vector2(-0.3822882919, 0.2373934748), Vector2(-0.3712395594, -0.2543249683), Vector2(0.4472363971, -0.04979563372), Vector2(-0.4466591209, 0.05473234629), Vector2(0.0486272539, -0.4473649407), Vector2(-0.4203101295, -0.1607463688), Vector2(0.2205360833, 0.39225481), Vector2(-0.3624900666, 0.2666476169), Vector2(-0.4036086833, -0.1989975647), Vector2(0.2152727807, 0.3951678503), Vector2(-0.4359392962, -0.1116106179), Vector2(0.4178354266, 0.1670735057), Vector2(0.2007630161, 0.4027334247), Vector2(-0.07278067175, -0.4440754146), Vector2(0.3644748615, -0.2639281632), Vector2(-0.4317451775, 0.126870413), Vector2(-0.297436456, 0.3376855855), Vector2(-0.2998672222, 0.3355289094), Vector2(-0.2673674124, 0.3619594822), Vector2(0.2808423357, 0.3516071423), Vector2(0.3498946567, 0.2829730186), Vector2(-0.2229685561, 0.390877248), Vector2(0.3305823267, 0.3053118493), Vector2(-0.2436681211, -0.3783197679), Vector2(-0.03402776529, 0.4487116125), Vector2(-0.319358823, 0.3170330301), Vector2(0.4454633477, -0.06373700535), Vector2(0.4483504221, 0.03849544189), Vector2(-0.4427358436, -0.08052932871), Vector2(0.05452298565, 0.4466847255), Vector2(-0.2812560807, 0.3512762688), Vector2(0.1266696921, 0.4318041097), Vector2(-0.3735981243, 0.2508474468), Vector2(0.2959708351, -0.3389708908), Vector2(-0.3714377181, 0.254035473), Vector2(-0.404467102, -0.1972469604), Vector2(0.1636165687, -0.419201167), Vector2(0.3289185495, -0.3071035458), Vector2(-0.2494824991, -0.3745109914), Vector2(0.03283133272, 0.4488007393), Vector2(-0.166306057, -0.4181414777), Vector2(-0.106833179, 0.4371346153), Vector2(0.06440260376, -0.4453676062), Vector2(-0.4483230967, 0.03881238203), Vector2(-0.421377757, -0.1579265206), Vector2(0.05097920662, -0.4471030312), Vector2(0.2050584153, -0.4005634111), Vector2(0.4178098529, -0.167137449), Vector2(-0.3565189504, -0.2745801121), Vector2(0.4478398129, 0.04403977727), Vector2(-0.3399999602, -0.2947881053), Vector2(0.3767121994, 0.2461461331), Vector2(-0.3138934434, 0.3224451987), Vector2(-0.1462001792, -0.4255884251), Vector2(0.3970290489, -0.2118205239), Vector2(0.4459149305, -0.06049689889), Vector2(-0.4104889426, -0.1843877112), Vector2(0.1475103971, -0.4251360756), Vector2(0.09258030352, 0.4403735771), Vector2(-0.1589664637, -0.4209865359), Vector2(0.2482445008, 0.3753327428), Vector2(0.4383624232, -0.1016778537), Vector2(0.06242802956, 0.4456486745), Vector2(0.2846591015, -0.3485243118), Vector2(-0.344202744, -0.2898697484), Vector2(0.1198188883, -0.4337550392), Vector2(-0.243590703, 0.3783696201), Vector2(0.2958191174, -0.3391033025), Vector2(-0.1164007991, 0.4346847754), Vector2(0.1274037151, -0.4315881062), Vector2(0.368047306, 0.2589231171), Vector2(0.2451436949, 0.3773652989), Vector2(-0.4314509715, 0.12786735)) -private val CELL_3D = arrayOf(Vector3(0.1453787434, -0.4149781685, -0.0956981749), Vector3(-0.01242829687, -0.1457918398, -0.4255470325), Vector3(0.2877979582, -0.02606483451, -0.3449535616), Vector3(-0.07732986802, 0.2377094325, 0.3741848704), Vector3(0.1107205875, -0.3552302079, -0.2530858567), Vector3(0.2755209141, 0.2640521179, -0.238463215), Vector3(0.294168941, 0.1526064594, 0.3044271714), Vector3(0.4000921098, -0.2034056362, 0.03244149937), Vector3(-0.1697304074, 0.3970864695, -0.1265461359), Vector3(-0.1483224484, -0.3859694688, 0.1775613147), Vector3(0.2623596946, -0.2354852944, 0.2796677792), Vector3(-0.2709003183, 0.3505271138, -0.07901746678), Vector3(-0.03516550699, 0.3885234328, 0.2243054374), Vector3(-0.1267712655, 0.1920044036, 0.3867342179), Vector3(0.02952021915, 0.4409685861, 0.08470692262), Vector3(-0.2806854217, -0.266996757, 0.2289725438), Vector3(-0.171159547, 0.2141185563, 0.3568720405), Vector3(0.2113227183, 0.3902405947, -0.07453178509), Vector3(-0.1024352839, 0.2128044156, -0.3830421561), Vector3(-0.3304249877, -0.1566986703, 0.2622305365), Vector3(0.2091111325, 0.3133278055, -0.2461670583), Vector3(0.344678154, -0.1944240454, -0.2142341261), Vector3(0.1984478035, -0.3214342325, -0.2445373252), Vector3(-0.2929008603, 0.2262915116, 0.2559320961), Vector3(-0.1617332831, 0.006314769776, -0.4198838754), Vector3(-0.3582060271, -0.148303178, -0.2284613961), Vector3(-0.1852067326, -0.3454119342, -0.2211087107), Vector3(0.3046301062, 0.1026310383, 0.314908508), Vector3(-0.03816768434, -0.2551766358, -0.3686842991), Vector3(-0.4084952196, 0.1805950793, 0.05492788837), Vector3(-0.02687443361, -0.2749741471, 0.3551999201), Vector3(-0.03801098351, 0.3277859044, 0.3059600725), Vector3(0.2371120802, 0.2900386767, -0.2493099024), Vector3(0.4447660503, 0.03946930643, 0.05590469027), Vector3(0.01985147278, -0.01503183293, -0.4493105419), Vector3(0.4274339143, 0.03345994256, -0.1366772882), Vector3(-0.2072988631, 0.2871414597, -0.2776273824), Vector3(-0.3791240978, 0.1281177671, 0.2057929936), Vector3(-0.2098721267, -0.1007087278, -0.3851122467), Vector3(0.01582798878, 0.4263894424, 0.1429738373), Vector3(-0.1888129464, -0.3160996813, -0.2587096108), Vector3(0.1612988974, -0.1974805082, -0.3707885038), Vector3(-0.08974491322, 0.229148752, -0.3767448739), Vector3(0.07041229526, 0.4150230285, -0.1590534329), Vector3(-0.1082925611, -0.1586061639, 0.4069604477), Vector3(0.2474100658, -0.3309414609, 0.1782302128), Vector3(-0.1068836661, -0.2701644537, -0.3436379634), Vector3(0.2396452163, 0.06803600538, -0.3747549496), Vector3(-0.3063886072, 0.2597428179, 0.2028785103), Vector3(0.1593342891, -0.3114350249, -0.2830561951), Vector3(0.2709690528, 0.1412648683, -0.3303331794), Vector3(-0.1519780427, 0.3623355133, 0.2193527988), Vector3(0.1699773681, 0.3456012883, 0.2327390037), Vector3(-0.1986155616, 0.3836276443, -0.1260225743), Vector3(-0.1887482106, -0.2050154888, -0.353330953), Vector3(0.2659103394, 0.3015631259, -0.2021172246), Vector3(-0.08838976154, -0.4288819642, -0.1036702021), Vector3(-0.04201869311, 0.3099592485, 0.3235115047), Vector3(-0.3230334656, 0.201549922, -0.2398478873), Vector3(0.2612720941, 0.2759854499, -0.2409749453), Vector3(0.385713046, 0.2193460345, 0.07491837764), Vector3(0.07654967953, 0.3721732183, 0.241095919), Vector3(0.4317038818, -0.02577753072, 0.1243675091), Vector3(-0.2890436293, -0.3418179959, -0.04598084447), Vector3(-0.2201947582, 0.383023377, -0.08548310451), Vector3(0.4161322773, -0.1669634289, -0.03817251927), Vector3(0.2204718095, 0.02654238946, -0.391391981), Vector3(-0.1040307469, 0.3890079625, -0.2008741118), Vector3(-0.1432122615, 0.371614387, -0.2095065525), Vector3(0.3978380468, -0.06206669342, 0.2009293758), Vector3(-0.2599274663, 0.2616724959, -0.2578084893), Vector3(0.4032618332, -0.1124593585, 0.1650235939), Vector3(-0.08953470255, -0.3048244735, 0.3186935478), Vector3(0.118937202, -0.2875221847, 0.325092195), Vector3(0.02167047076, -0.03284630549, -0.4482761547), Vector3(-0.3411343612, 0.2500031105, 0.1537068389), Vector3(0.3162964612, 0.3082064153, -0.08640228117), Vector3(0.2355138889, -0.3439334267, -0.1695376245), Vector3(-0.02874541518, -0.3955933019, 0.2125550295), Vector3(-0.2461455173, 0.02020282325, -0.3761704803), Vector3(0.04208029445, -0.4470439576, 0.02968078139), Vector3(0.2727458746, 0.2288471896, -0.2752065618), Vector3(-0.1347522818, -0.02720848277, -0.4284874806), Vector3(0.3829624424, 0.1231931484, -0.2016512234), Vector3(-0.3547613644, 0.1271702173, 0.2459107769), Vector3(0.2305790207, 0.3063895591, 0.2354968222), Vector3(-0.08323845599, -0.1922245118, 0.3982726409), Vector3(0.2993663085, -0.2619918095, -0.2103333191), Vector3(-0.2154865723, 0.2706747713, 0.287751117), Vector3(0.01683355354, -0.2680655787, -0.3610505186), Vector3(0.05240429123, 0.4335128183, -0.1087217856), Vector3(0.00940104872, -0.4472890582, 0.04841609928), Vector3(0.3465688735, 0.01141914583, -0.2868093776), Vector3(-0.3706867948, -0.2551104378, 0.003156692623), Vector3(0.2741169781, 0.2139972417, -0.2855959784), Vector3(0.06413433865, 0.1708718512, 0.4113266307), Vector3(-0.388187972, -0.03973280434, -0.2241236325), Vector3(0.06419469312, -0.2803682491, 0.3460819069), Vector3(-0.1986120739, -0.3391173584, 0.2192091725), Vector3(-0.203203009, -0.3871641506, 0.1063600375), Vector3(-0.1389736354, -0.2775901578, -0.3257760473), Vector3(-0.06555641638, 0.342253257, -0.2847192729), Vector3(-0.2529246486, -0.2904227915, 0.2327739768), Vector3(0.1444476522, 0.1069184044, 0.4125570634), Vector3(-0.3643780054, -0.2447099973, -0.09922543227), Vector3(0.4286142488, -0.1358496089, -0.01829506817), Vector3(0.165872923, -0.3136808464, -0.2767498872), Vector3(0.2219610524, -0.3658139958, 0.1393320198), Vector3(0.04322940318, -0.3832730794, 0.2318037215), Vector3(-0.08481269795, -0.4404869674, -0.03574965489), Vector3(0.1822082075, -0.3953259299, 0.1140946023), Vector3(-0.3269323334, 0.3036542563, 0.05838957105), Vector3(-0.4080485344, 0.04227858267, -0.184956522), Vector3(0.2676025294, -0.01299671652, 0.36155217), Vector3(0.3024892441, -0.1009990293, -0.3174892964), Vector3(0.1448494052, 0.425921681, -0.0104580805), Vector3(0.4198402157, 0.08062320474, 0.1404780841), Vector3(-0.3008872161, -0.333040905, -0.03241355801), Vector3(0.3639310428, -0.1291284382, -0.2310412139), Vector3(0.3295806598, 0.0184175994, -0.3058388149), Vector3(0.2776259487, -0.2974929052, -0.1921504723), Vector3(0.4149000507, -0.144793182, -0.09691688386), Vector3(0.145016715, -0.0398992945, 0.4241205002), Vector3(0.09299023471, -0.299732164, -0.3225111565), Vector3(0.1028907093, -0.361266869, 0.247789732), Vector3(0.2683057049, -0.07076041213, -0.3542668666), Vector3(-0.4227307273, -0.07933161816, -0.1323073187), Vector3(-0.1781224702, 0.1806857196, -0.3716517945), Vector3(0.4390788626, -0.02841848598, -0.09435116353), Vector3(0.2972583585, 0.2382799621, -0.2394997452), Vector3(-0.1707002821, 0.2215845691, 0.3525077196), Vector3(0.3806686614, 0.1471852559, -0.1895464869), Vector3(-0.1751445661, -0.274887877, 0.3102596268), Vector3(-0.2227237566, -0.2316778837, 0.3149912482), Vector3(0.1369633021, 0.1341343041, -0.4071228836), Vector3(-0.3529503428, -0.2472893463, -0.129514612), Vector3(-0.2590744185, -0.2985577559, -0.2150435121), Vector3(-0.3784019401, 0.2199816631, -0.1044989934), Vector3(-0.05635805671, 0.1485737441, 0.4210102279), Vector3(0.3251428613, 0.09666046873, -0.2957006485), Vector3(-0.4190995804, 0.1406751354, -0.08405978803), Vector3(-0.3253150961, -0.3080335042, -0.04225456877), Vector3(0.2857945863, -0.05796152095, 0.3427271751), Vector3(-0.2733604046, 0.1973770973, -0.2980207554), Vector3(0.219003657, 0.2410037886, -0.3105713639), Vector3(0.3182767252, -0.271342949, 0.1660509868), Vector3(-0.03222023115, -0.3331161506, -0.300824678), Vector3(-0.3087780231, 0.1992794134, -0.2596995338), Vector3(-0.06487611647, -0.4311322747, 0.1114273361), Vector3(0.3921171432, -0.06294284106, -0.2116183942), Vector3(-0.1606404506, -0.358928121, -0.2187812825), Vector3(-0.03767771199, -0.2290351443, 0.3855169162), Vector3(0.1394866832, -0.3602213994, 0.2308332918), Vector3(-0.4345093872, 0.005751117145, 0.1169124335), Vector3(-0.1044637494, 0.4168128432, -0.1336202785), Vector3(0.2658727501, 0.2551943237, 0.2582393035), Vector3(0.2051461999, 0.1975390727, 0.3484154868), Vector3(-0.266085566, 0.23483312, 0.2766800993), Vector3(0.07849405464, -0.3300346342, -0.2956616708), Vector3(-0.2160686338, 0.05376451292, -0.3910546287), Vector3(-0.185779186, 0.2148499206, 0.3490352499), Vector3(0.02492421743, -0.3229954284, -0.3123343347), Vector3(-0.120167831, 0.4017266681, 0.1633259825), Vector3(-0.02160084693, -0.06885389554, 0.4441762538), Vector3(0.2597670064, 0.3096300784, 0.1978643903), Vector3(-0.1611553854, -0.09823036005, 0.4085091653), Vector3(-0.3278896792, 0.1461670309, 0.2713366126), Vector3(0.2822734956, 0.03754421121, -0.3484423997), Vector3(0.03169341113, 0.347405252, -0.2842624114), Vector3(0.2202613604, -0.3460788041, -0.1849713341), Vector3(0.2933396046, 0.3031973659, 0.1565989581), Vector3(-0.3194922995, 0.2453752201, -0.200538455), Vector3(-0.3441586045, -0.1698856132, -0.2349334659), Vector3(0.2703645948, -0.3574277231, 0.04060059933), Vector3(0.2298568861, 0.3744156221, 0.0973588921), Vector3(0.09326603877, -0.3170108894, 0.3054595587), Vector3(-0.1116165319, -0.2985018719, 0.3177080142), Vector3(0.2172907365, -0.3460005203, -0.1885958001), Vector3(0.1991339479, 0.3820341668, -0.1299829458), Vector3(-0.0541918155, -0.2103145071, 0.39412061), Vector3(0.08871336998, 0.2012117383, 0.3926114802), Vector3(0.2787673278, 0.3505404674, 0.04370535101), Vector3(-0.322166438, 0.3067213525, 0.06804996813), Vector3(-0.4277366384, 0.132066775, 0.04582286686), Vector3(0.240131882, -0.1612516055, 0.344723946), Vector3(0.1448607981, -0.2387819045, 0.3528435224), Vector3(-0.3837065682, -0.2206398454, 0.08116235683), Vector3(-0.4382627882, -0.09082753406, -0.04664855374), Vector3(-0.37728353, 0.05445141085, 0.2391488697), Vector3(0.1259579313, 0.348394558, 0.2554522098), Vector3(-0.1406285511, -0.270877371, -0.3306796947), Vector3(-0.1580694418, 0.4162931958, -0.06491553533), Vector3(0.2477612106, -0.2927867412, -0.2353514536), Vector3(0.2916132853, 0.3312535401, 0.08793624968), Vector3(0.07365265219, -0.1666159848, 0.411478311), Vector3(-0.26126526, -0.2422237692, 0.2748965434), Vector3(-0.3721862032, 0.252790166, 0.008634938242), Vector3(-0.3691191571, -0.255281188, 0.03290232422), Vector3(0.2278441737, -0.3358364886, 0.1944244981), Vector3(0.363398169, -0.2310190248, 0.1306597909), Vector3(-0.304231482, -0.2698452035, 0.1926830856), Vector3(-0.3199312232, 0.316332536, -0.008816977938), Vector3(0.2874852279, 0.1642275508, -0.304764754), Vector3(-0.1451096801, 0.3277541114, -0.2720669462), Vector3(0.3220090754, 0.0511344108, 0.3101538769), Vector3(-0.1247400865, -0.04333605335, -0.4301882115), Vector3(-0.2829555867, -0.3056190617, -0.1703910946), Vector3(0.1069384374, 0.3491024667, -0.2630430352), Vector3(-0.1420661144, -0.3055376754, -0.2982682484), Vector3(-0.250548338, 0.3156466809, -0.2002316239), Vector3(0.3265787872, 0.1871229129, 0.2466400438), Vector3(0.07646097258, -0.3026690852, 0.324106687), Vector3(0.3451771584, 0.2757120714, -0.0856480183), Vector3(0.298137964, 0.2852657134, 0.179547284), Vector3(0.2812250376, 0.3466716415, 0.05684409612), Vector3(0.4390345476, -0.09790429955, -0.01278335452), Vector3(0.2148373234, 0.1850172527, 0.3494474791), Vector3(0.2595421179, -0.07946825393, 0.3589187731), Vector3(0.3182823114, -0.307355516, -0.08203022006), Vector3(-0.4089859285, -0.04647718411, 0.1818526372), Vector3(-0.2826749061, 0.07417482322, 0.3421885344), Vector3(0.3483864637, 0.225442246, -0.1740766085), Vector3(-0.3226415069, -0.1420585388, -0.2796816575), Vector3(0.4330734858, -0.118868561, -0.02859407492), Vector3(-0.08717822568, -0.3909896417, -0.2050050172), Vector3(-0.2149678299, 0.3939973956, -0.03247898316), Vector3(-0.2687330705, 0.322686276, -0.1617284888), Vector3(0.2105665099, -0.1961317136, -0.3459683451), Vector3(0.4361845915, -0.1105517485, 0.004616608544), Vector3(0.05333333359, -0.313639498, -0.3182543336), Vector3(-0.05986216652, 0.1361029153, -0.4247264031), Vector3(0.3664988455, 0.2550543014, -0.05590974511), Vector3(-0.2341015558, -0.182405731, 0.3382670703), Vector3(-0.04730947785, -0.4222150243, -0.1483114513), Vector3(-0.2391566239, -0.2577696514, -0.2808182972), Vector3(-0.1242081035, 0.4256953395, -0.07652336246), Vector3(0.2614832715, -0.3650179274, 0.02980623099), Vector3(-0.2728794681, -0.3499628774, 0.07458404908), Vector3(0.007892900508, -0.1672771315, 0.4176793787), Vector3(-0.01730330376, 0.2978486637, -0.3368779738), Vector3(0.2054835762, -0.3252600376, -0.2334146693), Vector3(-0.3231994983, 0.1564282844, -0.2712420987), Vector3(-0.2669545963, 0.2599343665, -0.2523278991), Vector3(-0.05554372779, 0.3170813944, -0.3144428146), Vector3(-0.2083935713, -0.310922837, -0.2497981362), Vector3(0.06989323478, -0.3156141536, 0.3130537363), Vector3(0.3847566193, -0.1605309138, -0.1693876312), Vector3(-0.3026215288, -0.3001537679, -0.1443188342), Vector3(0.3450735512, 0.08611519592, 0.2756962409), Vector3(0.1814473292, -0.2788782453, -0.3029914042), Vector3(-0.03855010448, 0.09795110726, 0.4375151083), Vector3(0.3533670318, 0.2665752752, 0.08105160988), Vector3(-0.007945601311, 0.140359426, -0.4274764309), Vector3(0.4063099273, -0.1491768253, -0.1231199324), Vector3(-0.2016773589, 0.008816271194, -0.4021797064), Vector3(-0.07527055435, -0.425643481, -0.1251477955)) +package org.openrndr.extra.noise + +import org.openrndr.math.Vector2 +import org.openrndr.math.Vector3 + + + +private const val X_PRIME = 1619 +private const val Y_PRIME = 31337 +private const val Z_PRIME = 6971 +private const val W_PRIME = 1013 + + +private val GRAD_2D = arrayOf(Vector2(-1.0, -1.0), Vector2(1.0, -1.0), Vector2(-1.0, 1.0), + Vector2(1.0, 1.0), Vector2(0.0, -1.0), Vector2(-1.0, 0.0), + Vector2(0.0, 1.0), Vector2(1.0, 0.0)) + + +private val GRAD_3D = arrayOf( + Vector3(1.0, 1.0, 0.0), Vector3(-1.0, 1.0, 0.0), Vector3(1.0, -1.0, 0.0), Vector3(-1.0, -1.0, 0.0), + Vector3(1.0, 0.0, 1.0), Vector3(-1.0, 0.0, 1.0), Vector3(1.0, 0.0, -1.0), Vector3(-1.0, 0.0, -1.0), + Vector3(0.0, 1.0, 1.0), Vector3(0.0, -1.0, 1.0), Vector3(0.0, 1.0, -1.0), Vector3(0.0, -1.0, -1.0), + Vector3(1.0, 1.0, 0.0), Vector3(0.0, -1.0, 1.0), Vector3(-1.0, 1.0, 0.0), Vector3(0.0, -1.0, -1.0)) + +fun gradCoord2D(seed: Int, x: Int, y: Int, xd: Double, yd: Double): Double { + var hash = seed + hash = hash xor X_PRIME * x + hash = hash xor Y_PRIME * y + + hash = hash * hash * hash * 60493 + hash = hash shr 13 xor hash + + val (x1, y1) = GRAD_2D[hash and 7] + + return xd * x1 + yd * y1 +} + +fun gradCoord3D(seed: Int, x: Int, y: Int, z: Int, xd: Double, yd: Double, zd: Double): Double { + var hash = seed + hash = hash xor X_PRIME * x + hash = hash xor Y_PRIME * y + hash = hash xor Z_PRIME * z + + hash *= hash * hash * 60493 + hash = hash shr 13 xor hash + + val g = GRAD_3D[hash and 15] + + return xd * g.x + yd * g.y + zd * g.z +} + + +fun gradCoord4D(seed: Int, x: Int, y: Int, z: Int, w: Int, xd: Double, yd: Double, zd: Double, wd: Double): Double { + var hash = seed + hash = hash xor X_PRIME * x + hash = hash xor Y_PRIME * y + hash = hash xor Z_PRIME * z + hash = hash xor W_PRIME * w + + hash = hash * hash * hash * 60493 + hash = hash shr 13 xor hash + + hash = hash and 31 + var a = yd + var b = zd + var c = wd // X,Y,Z + when (hash shr 3) { + // OR, DEPENDING ON HIGH ORDER 2 BITS: + 1 -> { + a = wd + b = xd + c = yd + } + 2 -> { + a = zd + b = wd + c = xd + } + 3 -> { + a = yd + b = zd + c = wd + } + }// W,X,Y + // Z,W,X + // Y,Z,W + return (if (hash and 4 == 0) -a else a) + (if (hash and 2 == 0) -b else b) + if (hash and 1 == 0) -c else c +} + + +fun hash2D(seed: Int, x: Int, y: Int): Int { + var hash = seed + hash = hash xor X_PRIME * x + hash = hash xor Y_PRIME * y + + hash = hash * hash * hash * 60493 + hash = hash shr 13 xor hash + + return hash +} + +fun hash3D(seed: Int, x: Int, y: Int, z: Int): Int { + var hash = seed + hash = hash xor X_PRIME * x + hash = hash xor Y_PRIME * y + hash = hash xor Z_PRIME * z + + hash = hash * hash * hash * 60493 + hash = hash shr 13 xor hash + + return hash +} + +fun hash4D(seed: Int, x: Int, y: Int, z: Int, w: Int): Int { + var hash = seed + hash = hash xor X_PRIME * x + hash = hash xor Y_PRIME * y + hash = hash xor Z_PRIME * z + hash = hash xor W_PRIME * w + + hash = hash * hash * hash * 60493 + hash = hash shr 13 xor hash + return hash +} + + +fun valCoord2D(seed: Int, x: Int, y: Int): Double { + var n = seed + n = n xor X_PRIME * x + n = n xor Y_PRIME * y + + return n * n * n * 60493 / 2147483648.0 +} + +fun valCoord3D(seed: Int, x: Int, y: Int, z: Int): Double { + var n = seed + n = n xor X_PRIME * x + n = n xor Y_PRIME * y + n = n xor Z_PRIME * z + + return n * n * n * 60493 / 2147483648.0 +} + + +private fun valCoord4D(seed: Int, x: Int, y: Int, z: Int, w: Int): Double { + var n = seed + n = n xor X_PRIME * x + n = n xor Y_PRIME * y + n = n xor Z_PRIME * z + n = n xor W_PRIME * w + + return n * n * n * 60493 / 2147483648.0 +} + +private val CELL_2D = arrayOf(Vector2(-0.4313539279, 0.1281943404), Vector2(-0.1733316799, 0.415278375), Vector2(-0.2821957395, -0.3505218461), Vector2(-0.2806473808, 0.3517627718), Vector2(0.3125508975, -0.3237467165), Vector2(0.3383018443, -0.2967353402), Vector2(-0.4393982022, -0.09710417025), Vector2(-0.4460443703, -0.05953502905), Vector2(-0.302223039, 0.3334085102), Vector2(-0.212681052, -0.3965687458), Vector2(-0.2991156529, 0.3361990872), Vector2(0.2293323691, 0.3871778202), Vector2(0.4475439151, -0.04695150755), Vector2(0.1777518, 0.41340573), Vector2(0.1688522499, -0.4171197882), Vector2(-0.0976597166, 0.4392750616), Vector2(0.08450188373, 0.4419948321), Vector2(-0.4098760448, -0.1857461384), Vector2(0.3476585782, -0.2857157906), Vector2(-0.3350670039, -0.30038326), Vector2(0.2298190031, -0.3868891648), Vector2(-0.01069924099, 0.449872789), Vector2(-0.4460141246, -0.05976119672), Vector2(0.3650293864, 0.2631606867), Vector2(-0.349479423, 0.2834856838), Vector2(-0.4122720642, 0.1803655873), Vector2(-0.267327811, 0.3619887311), Vector2(0.322124041, -0.3142230135), Vector2(0.2880445931, -0.3457315612), Vector2(0.3892170926, -0.2258540565), Vector2(0.4492085018, -0.02667811596), Vector2(-0.4497724772, 0.01430799601), Vector2(0.1278175387, -0.4314657307), Vector2(-0.03572100503, 0.4485799926), Vector2(-0.4297407068, -0.1335025276), Vector2(-0.3217817723, 0.3145735065), Vector2(-0.3057158873, 0.3302087162), Vector2(-0.414503978, 0.1751754899), Vector2(-0.3738139881, 0.2505256519), Vector2(0.2236891408, -0.3904653228), Vector2(0.002967775577, -0.4499902136), Vector2(0.1747128327, -0.4146991995), Vector2(-0.4423772489, -0.08247647938), Vector2(-0.2763960987, -0.355112935), Vector2(-0.4019385906, -0.2023496216), Vector2(0.3871414161, -0.2293938184), Vector2(-0.430008727, 0.1326367019), Vector2(-0.03037574274, -0.4489736231), Vector2(-0.3486181573, 0.2845441624), Vector2(0.04553517144, -0.4476902368), Vector2(-0.0375802926, 0.4484280562), Vector2(0.3266408905, 0.3095250049), Vector2(0.06540017593, -0.4452222108), Vector2(0.03409025829, 0.448706869), Vector2(-0.4449193635, 0.06742966669), Vector2(-0.4255936157, -0.1461850686), Vector2(0.449917292, 0.008627302568), Vector2(0.05242606404, 0.4469356864), Vector2(-0.4495305179, -0.02055026661), Vector2(-0.1204775703, 0.4335725488), Vector2(-0.341986385, -0.2924813028), Vector2(0.3865320182, 0.2304191809), Vector2(0.04506097811, -0.447738214), Vector2(-0.06283465979, 0.4455915232), Vector2(0.3932600341, -0.2187385324), Vector2(0.4472261803, -0.04988730975), Vector2(0.3753571011, -0.2482076684), Vector2(-0.273662295, 0.357223947), Vector2(0.1700461538, 0.4166344988), Vector2(0.4102692229, 0.1848760794), Vector2(0.323227187, -0.3130881435), Vector2(-0.2882310238, -0.3455761521), Vector2(0.2050972664, 0.4005435199), Vector2(0.4414085979, -0.08751256895), Vector2(-0.1684700334, 0.4172743077), Vector2(-0.003978032396, 0.4499824166), Vector2(-0.2055133639, 0.4003301853), Vector2(-0.006095674897, -0.4499587123), Vector2(-0.1196228124, -0.4338091548), Vector2(0.3901528491, -0.2242337048), Vector2(0.01723531752, 0.4496698165), Vector2(-0.3015070339, 0.3340561458), Vector2(-0.01514262423, -0.4497451511), Vector2(-0.4142574071, -0.1757577897), Vector2(-0.1916377265, -0.4071547394), Vector2(0.3749248747, 0.2488600778), Vector2(-0.2237774255, 0.3904147331), Vector2(-0.4166343106, -0.1700466149), Vector2(0.3619171625, 0.267424695), Vector2(0.1891126846, -0.4083336779), Vector2(-0.3127425077, 0.323561623), Vector2(-0.3281807787, 0.307891826), Vector2(-0.2294806661, 0.3870899429), Vector2(-0.3445266136, 0.2894847362), Vector2(-0.4167095422, -0.1698621719), Vector2(-0.257890321, -0.3687717212), Vector2(-0.3612037825, 0.2683874578), Vector2(0.2267996491, 0.3886668486), Vector2(0.207157062, 0.3994821043), Vector2(0.08355176718, -0.4421754202), Vector2(-0.4312233307, 0.1286329626), Vector2(0.3257055497, 0.3105090899), Vector2(0.177701095, -0.4134275279), Vector2(-0.445182522, 0.06566979625), Vector2(0.3955143435, 0.2146355146), Vector2(-0.4264613988, 0.1436338239), Vector2(-0.3793799665, -0.2420141339), Vector2(0.04617599081, -0.4476245948), Vector2(-0.371405428, -0.2540826796), Vector2(0.2563570295, -0.3698392535), Vector2(0.03476646309, 0.4486549822), Vector2(-0.3065454405, 0.3294387544), Vector2(-0.2256979823, 0.3893076172), Vector2(0.4116448463, -0.1817925206), Vector2(-0.2907745828, -0.3434387019), Vector2(0.2842278468, -0.348876097), Vector2(0.3114589359, -0.3247973695), Vector2(0.4464155859, -0.0566844308), Vector2(-0.3037334033, -0.3320331606), Vector2(0.4079607166, 0.1899159123), Vector2(-0.3486948919, -0.2844501228), Vector2(0.3264821436, 0.3096924441), Vector2(0.3211142406, 0.3152548881), Vector2(0.01183382662, 0.4498443737), Vector2(0.4333844092, 0.1211526057), Vector2(0.3118668416, 0.324405723), Vector2(-0.272753471, 0.3579183483), Vector2(-0.422228622, -0.1556373694), Vector2(-0.1009700099, -0.4385260051), Vector2(-0.2741171231, -0.3568750521), Vector2(-0.1465125133, 0.4254810025), Vector2(0.2302279044, -0.3866459777), Vector2(-0.3699435608, 0.2562064828), Vector2(0.105700352, -0.4374099171), Vector2(-0.2646713633, 0.3639355292), Vector2(0.3521828122, 0.2801200935), Vector2(-0.1864187807, -0.4095705534), Vector2(0.1994492955, -0.4033856449), Vector2(0.3937065066, 0.2179339044), Vector2(-0.3226158377, 0.3137180602), Vector2(0.3796235338, 0.2416318948), Vector2(0.1482921929, 0.4248640083), Vector2(-0.407400394, 0.1911149365), Vector2(0.4212853031, 0.1581729856), Vector2(-0.2621297173, 0.3657704353), Vector2(-0.2536986953, -0.3716678248), Vector2(-0.2100236383, 0.3979825013), Vector2(0.3624152444, 0.2667493029), Vector2(-0.3645038479, -0.2638881295), Vector2(0.2318486784, 0.3856762766), Vector2(-0.3260457004, 0.3101519002), Vector2(-0.2130045332, -0.3963950918), Vector2(0.3814998766, -0.2386584257), Vector2(-0.342977305, 0.2913186713), Vector2(-0.4355865605, 0.1129794154), Vector2(-0.2104679605, 0.3977477059), Vector2(0.3348364681, -0.3006402163), Vector2(0.3430468811, 0.2912367377), Vector2(-0.2291836801, -0.3872658529), Vector2(0.2547707298, -0.3709337882), Vector2(0.4236174945, -0.151816397), Vector2(-0.15387742, 0.4228731957), Vector2(-0.4407449312, 0.09079595574), Vector2(-0.06805276192, -0.444824484), Vector2(0.4453517192, -0.06451237284), Vector2(0.2562464609, -0.3699158705), Vector2(0.3278198355, -0.3082761026), Vector2(-0.4122774207, -0.1803533432), Vector2(0.3354090914, -0.3000012356), Vector2(0.446632869, -0.05494615882), Vector2(-0.1608953296, 0.4202531296), Vector2(-0.09463954939, 0.4399356268), Vector2(-0.02637688324, -0.4492262904), Vector2(0.447102804, -0.05098119915), Vector2(-0.4365670908, 0.1091291678), Vector2(-0.3959858651, 0.2137643437), Vector2(-0.4240048207, -0.1507312575), Vector2(-0.3882794568, 0.2274622243), Vector2(-0.4283652566, -0.1378521198), Vector2(0.3303888091, 0.305521251), Vector2(0.3321434919, -0.3036127481), Vector2(-0.413021046, -0.1786438231), Vector2(0.08403060337, -0.4420846725), Vector2(-0.3822882919, 0.2373934748), Vector2(-0.3712395594, -0.2543249683), Vector2(0.4472363971, -0.04979563372), Vector2(-0.4466591209, 0.05473234629), Vector2(0.0486272539, -0.4473649407), Vector2(-0.4203101295, -0.1607463688), Vector2(0.2205360833, 0.39225481), Vector2(-0.3624900666, 0.2666476169), Vector2(-0.4036086833, -0.1989975647), Vector2(0.2152727807, 0.3951678503), Vector2(-0.4359392962, -0.1116106179), Vector2(0.4178354266, 0.1670735057), Vector2(0.2007630161, 0.4027334247), Vector2(-0.07278067175, -0.4440754146), Vector2(0.3644748615, -0.2639281632), Vector2(-0.4317451775, 0.126870413), Vector2(-0.297436456, 0.3376855855), Vector2(-0.2998672222, 0.3355289094), Vector2(-0.2673674124, 0.3619594822), Vector2(0.2808423357, 0.3516071423), Vector2(0.3498946567, 0.2829730186), Vector2(-0.2229685561, 0.390877248), Vector2(0.3305823267, 0.3053118493), Vector2(-0.2436681211, -0.3783197679), Vector2(-0.03402776529, 0.4487116125), Vector2(-0.319358823, 0.3170330301), Vector2(0.4454633477, -0.06373700535), Vector2(0.4483504221, 0.03849544189), Vector2(-0.4427358436, -0.08052932871), Vector2(0.05452298565, 0.4466847255), Vector2(-0.2812560807, 0.3512762688), Vector2(0.1266696921, 0.4318041097), Vector2(-0.3735981243, 0.2508474468), Vector2(0.2959708351, -0.3389708908), Vector2(-0.3714377181, 0.254035473), Vector2(-0.404467102, -0.1972469604), Vector2(0.1636165687, -0.419201167), Vector2(0.3289185495, -0.3071035458), Vector2(-0.2494824991, -0.3745109914), Vector2(0.03283133272, 0.4488007393), Vector2(-0.166306057, -0.4181414777), Vector2(-0.106833179, 0.4371346153), Vector2(0.06440260376, -0.4453676062), Vector2(-0.4483230967, 0.03881238203), Vector2(-0.421377757, -0.1579265206), Vector2(0.05097920662, -0.4471030312), Vector2(0.2050584153, -0.4005634111), Vector2(0.4178098529, -0.167137449), Vector2(-0.3565189504, -0.2745801121), Vector2(0.4478398129, 0.04403977727), Vector2(-0.3399999602, -0.2947881053), Vector2(0.3767121994, 0.2461461331), Vector2(-0.3138934434, 0.3224451987), Vector2(-0.1462001792, -0.4255884251), Vector2(0.3970290489, -0.2118205239), Vector2(0.4459149305, -0.06049689889), Vector2(-0.4104889426, -0.1843877112), Vector2(0.1475103971, -0.4251360756), Vector2(0.09258030352, 0.4403735771), Vector2(-0.1589664637, -0.4209865359), Vector2(0.2482445008, 0.3753327428), Vector2(0.4383624232, -0.1016778537), Vector2(0.06242802956, 0.4456486745), Vector2(0.2846591015, -0.3485243118), Vector2(-0.344202744, -0.2898697484), Vector2(0.1198188883, -0.4337550392), Vector2(-0.243590703, 0.3783696201), Vector2(0.2958191174, -0.3391033025), Vector2(-0.1164007991, 0.4346847754), Vector2(0.1274037151, -0.4315881062), Vector2(0.368047306, 0.2589231171), Vector2(0.2451436949, 0.3773652989), Vector2(-0.4314509715, 0.12786735)) +private val CELL_3D = arrayOf(Vector3(0.1453787434, -0.4149781685, -0.0956981749), Vector3(-0.01242829687, -0.1457918398, -0.4255470325), Vector3(0.2877979582, -0.02606483451, -0.3449535616), Vector3(-0.07732986802, 0.2377094325, 0.3741848704), Vector3(0.1107205875, -0.3552302079, -0.2530858567), Vector3(0.2755209141, 0.2640521179, -0.238463215), Vector3(0.294168941, 0.1526064594, 0.3044271714), Vector3(0.4000921098, -0.2034056362, 0.03244149937), Vector3(-0.1697304074, 0.3970864695, -0.1265461359), Vector3(-0.1483224484, -0.3859694688, 0.1775613147), Vector3(0.2623596946, -0.2354852944, 0.2796677792), Vector3(-0.2709003183, 0.3505271138, -0.07901746678), Vector3(-0.03516550699, 0.3885234328, 0.2243054374), Vector3(-0.1267712655, 0.1920044036, 0.3867342179), Vector3(0.02952021915, 0.4409685861, 0.08470692262), Vector3(-0.2806854217, -0.266996757, 0.2289725438), Vector3(-0.171159547, 0.2141185563, 0.3568720405), Vector3(0.2113227183, 0.3902405947, -0.07453178509), Vector3(-0.1024352839, 0.2128044156, -0.3830421561), Vector3(-0.3304249877, -0.1566986703, 0.2622305365), Vector3(0.2091111325, 0.3133278055, -0.2461670583), Vector3(0.344678154, -0.1944240454, -0.2142341261), Vector3(0.1984478035, -0.3214342325, -0.2445373252), Vector3(-0.2929008603, 0.2262915116, 0.2559320961), Vector3(-0.1617332831, 0.006314769776, -0.4198838754), Vector3(-0.3582060271, -0.148303178, -0.2284613961), Vector3(-0.1852067326, -0.3454119342, -0.2211087107), Vector3(0.3046301062, 0.1026310383, 0.314908508), Vector3(-0.03816768434, -0.2551766358, -0.3686842991), Vector3(-0.4084952196, 0.1805950793, 0.05492788837), Vector3(-0.02687443361, -0.2749741471, 0.3551999201), Vector3(-0.03801098351, 0.3277859044, 0.3059600725), Vector3(0.2371120802, 0.2900386767, -0.2493099024), Vector3(0.4447660503, 0.03946930643, 0.05590469027), Vector3(0.01985147278, -0.01503183293, -0.4493105419), Vector3(0.4274339143, 0.03345994256, -0.1366772882), Vector3(-0.2072988631, 0.2871414597, -0.2776273824), Vector3(-0.3791240978, 0.1281177671, 0.2057929936), Vector3(-0.2098721267, -0.1007087278, -0.3851122467), Vector3(0.01582798878, 0.4263894424, 0.1429738373), Vector3(-0.1888129464, -0.3160996813, -0.2587096108), Vector3(0.1612988974, -0.1974805082, -0.3707885038), Vector3(-0.08974491322, 0.229148752, -0.3767448739), Vector3(0.07041229526, 0.4150230285, -0.1590534329), Vector3(-0.1082925611, -0.1586061639, 0.4069604477), Vector3(0.2474100658, -0.3309414609, 0.1782302128), Vector3(-0.1068836661, -0.2701644537, -0.3436379634), Vector3(0.2396452163, 0.06803600538, -0.3747549496), Vector3(-0.3063886072, 0.2597428179, 0.2028785103), Vector3(0.1593342891, -0.3114350249, -0.2830561951), Vector3(0.2709690528, 0.1412648683, -0.3303331794), Vector3(-0.1519780427, 0.3623355133, 0.2193527988), Vector3(0.1699773681, 0.3456012883, 0.2327390037), Vector3(-0.1986155616, 0.3836276443, -0.1260225743), Vector3(-0.1887482106, -0.2050154888, -0.353330953), Vector3(0.2659103394, 0.3015631259, -0.2021172246), Vector3(-0.08838976154, -0.4288819642, -0.1036702021), Vector3(-0.04201869311, 0.3099592485, 0.3235115047), Vector3(-0.3230334656, 0.201549922, -0.2398478873), Vector3(0.2612720941, 0.2759854499, -0.2409749453), Vector3(0.385713046, 0.2193460345, 0.07491837764), Vector3(0.07654967953, 0.3721732183, 0.241095919), Vector3(0.4317038818, -0.02577753072, 0.1243675091), Vector3(-0.2890436293, -0.3418179959, -0.04598084447), Vector3(-0.2201947582, 0.383023377, -0.08548310451), Vector3(0.4161322773, -0.1669634289, -0.03817251927), Vector3(0.2204718095, 0.02654238946, -0.391391981), Vector3(-0.1040307469, 0.3890079625, -0.2008741118), Vector3(-0.1432122615, 0.371614387, -0.2095065525), Vector3(0.3978380468, -0.06206669342, 0.2009293758), Vector3(-0.2599274663, 0.2616724959, -0.2578084893), Vector3(0.4032618332, -0.1124593585, 0.1650235939), Vector3(-0.08953470255, -0.3048244735, 0.3186935478), Vector3(0.118937202, -0.2875221847, 0.325092195), Vector3(0.02167047076, -0.03284630549, -0.4482761547), Vector3(-0.3411343612, 0.2500031105, 0.1537068389), Vector3(0.3162964612, 0.3082064153, -0.08640228117), Vector3(0.2355138889, -0.3439334267, -0.1695376245), Vector3(-0.02874541518, -0.3955933019, 0.2125550295), Vector3(-0.2461455173, 0.02020282325, -0.3761704803), Vector3(0.04208029445, -0.4470439576, 0.02968078139), Vector3(0.2727458746, 0.2288471896, -0.2752065618), Vector3(-0.1347522818, -0.02720848277, -0.4284874806), Vector3(0.3829624424, 0.1231931484, -0.2016512234), Vector3(-0.3547613644, 0.1271702173, 0.2459107769), Vector3(0.2305790207, 0.3063895591, 0.2354968222), Vector3(-0.08323845599, -0.1922245118, 0.3982726409), Vector3(0.2993663085, -0.2619918095, -0.2103333191), Vector3(-0.2154865723, 0.2706747713, 0.287751117), Vector3(0.01683355354, -0.2680655787, -0.3610505186), Vector3(0.05240429123, 0.4335128183, -0.1087217856), Vector3(0.00940104872, -0.4472890582, 0.04841609928), Vector3(0.3465688735, 0.01141914583, -0.2868093776), Vector3(-0.3706867948, -0.2551104378, 0.003156692623), Vector3(0.2741169781, 0.2139972417, -0.2855959784), Vector3(0.06413433865, 0.1708718512, 0.4113266307), Vector3(-0.388187972, -0.03973280434, -0.2241236325), Vector3(0.06419469312, -0.2803682491, 0.3460819069), Vector3(-0.1986120739, -0.3391173584, 0.2192091725), Vector3(-0.203203009, -0.3871641506, 0.1063600375), Vector3(-0.1389736354, -0.2775901578, -0.3257760473), Vector3(-0.06555641638, 0.342253257, -0.2847192729), Vector3(-0.2529246486, -0.2904227915, 0.2327739768), Vector3(0.1444476522, 0.1069184044, 0.4125570634), Vector3(-0.3643780054, -0.2447099973, -0.09922543227), Vector3(0.4286142488, -0.1358496089, -0.01829506817), Vector3(0.165872923, -0.3136808464, -0.2767498872), Vector3(0.2219610524, -0.3658139958, 0.1393320198), Vector3(0.04322940318, -0.3832730794, 0.2318037215), Vector3(-0.08481269795, -0.4404869674, -0.03574965489), Vector3(0.1822082075, -0.3953259299, 0.1140946023), Vector3(-0.3269323334, 0.3036542563, 0.05838957105), Vector3(-0.4080485344, 0.04227858267, -0.184956522), Vector3(0.2676025294, -0.01299671652, 0.36155217), Vector3(0.3024892441, -0.1009990293, -0.3174892964), Vector3(0.1448494052, 0.425921681, -0.0104580805), Vector3(0.4198402157, 0.08062320474, 0.1404780841), Vector3(-0.3008872161, -0.333040905, -0.03241355801), Vector3(0.3639310428, -0.1291284382, -0.2310412139), Vector3(0.3295806598, 0.0184175994, -0.3058388149), Vector3(0.2776259487, -0.2974929052, -0.1921504723), Vector3(0.4149000507, -0.144793182, -0.09691688386), Vector3(0.145016715, -0.0398992945, 0.4241205002), Vector3(0.09299023471, -0.299732164, -0.3225111565), Vector3(0.1028907093, -0.361266869, 0.247789732), Vector3(0.2683057049, -0.07076041213, -0.3542668666), Vector3(-0.4227307273, -0.07933161816, -0.1323073187), Vector3(-0.1781224702, 0.1806857196, -0.3716517945), Vector3(0.4390788626, -0.02841848598, -0.09435116353), Vector3(0.2972583585, 0.2382799621, -0.2394997452), Vector3(-0.1707002821, 0.2215845691, 0.3525077196), Vector3(0.3806686614, 0.1471852559, -0.1895464869), Vector3(-0.1751445661, -0.274887877, 0.3102596268), Vector3(-0.2227237566, -0.2316778837, 0.3149912482), Vector3(0.1369633021, 0.1341343041, -0.4071228836), Vector3(-0.3529503428, -0.2472893463, -0.129514612), Vector3(-0.2590744185, -0.2985577559, -0.2150435121), Vector3(-0.3784019401, 0.2199816631, -0.1044989934), Vector3(-0.05635805671, 0.1485737441, 0.4210102279), Vector3(0.3251428613, 0.09666046873, -0.2957006485), Vector3(-0.4190995804, 0.1406751354, -0.08405978803), Vector3(-0.3253150961, -0.3080335042, -0.04225456877), Vector3(0.2857945863, -0.05796152095, 0.3427271751), Vector3(-0.2733604046, 0.1973770973, -0.2980207554), Vector3(0.219003657, 0.2410037886, -0.3105713639), Vector3(0.3182767252, -0.271342949, 0.1660509868), Vector3(-0.03222023115, -0.3331161506, -0.300824678), Vector3(-0.3087780231, 0.1992794134, -0.2596995338), Vector3(-0.06487611647, -0.4311322747, 0.1114273361), Vector3(0.3921171432, -0.06294284106, -0.2116183942), Vector3(-0.1606404506, -0.358928121, -0.2187812825), Vector3(-0.03767771199, -0.2290351443, 0.3855169162), Vector3(0.1394866832, -0.3602213994, 0.2308332918), Vector3(-0.4345093872, 0.005751117145, 0.1169124335), Vector3(-0.1044637494, 0.4168128432, -0.1336202785), Vector3(0.2658727501, 0.2551943237, 0.2582393035), Vector3(0.2051461999, 0.1975390727, 0.3484154868), Vector3(-0.266085566, 0.23483312, 0.2766800993), Vector3(0.07849405464, -0.3300346342, -0.2956616708), Vector3(-0.2160686338, 0.05376451292, -0.3910546287), Vector3(-0.185779186, 0.2148499206, 0.3490352499), Vector3(0.02492421743, -0.3229954284, -0.3123343347), Vector3(-0.120167831, 0.4017266681, 0.1633259825), Vector3(-0.02160084693, -0.06885389554, 0.4441762538), Vector3(0.2597670064, 0.3096300784, 0.1978643903), Vector3(-0.1611553854, -0.09823036005, 0.4085091653), Vector3(-0.3278896792, 0.1461670309, 0.2713366126), Vector3(0.2822734956, 0.03754421121, -0.3484423997), Vector3(0.03169341113, 0.347405252, -0.2842624114), Vector3(0.2202613604, -0.3460788041, -0.1849713341), Vector3(0.2933396046, 0.3031973659, 0.1565989581), Vector3(-0.3194922995, 0.2453752201, -0.200538455), Vector3(-0.3441586045, -0.1698856132, -0.2349334659), Vector3(0.2703645948, -0.3574277231, 0.04060059933), Vector3(0.2298568861, 0.3744156221, 0.0973588921), Vector3(0.09326603877, -0.3170108894, 0.3054595587), Vector3(-0.1116165319, -0.2985018719, 0.3177080142), Vector3(0.2172907365, -0.3460005203, -0.1885958001), Vector3(0.1991339479, 0.3820341668, -0.1299829458), Vector3(-0.0541918155, -0.2103145071, 0.39412061), Vector3(0.08871336998, 0.2012117383, 0.3926114802), Vector3(0.2787673278, 0.3505404674, 0.04370535101), Vector3(-0.322166438, 0.3067213525, 0.06804996813), Vector3(-0.4277366384, 0.132066775, 0.04582286686), Vector3(0.240131882, -0.1612516055, 0.344723946), Vector3(0.1448607981, -0.2387819045, 0.3528435224), Vector3(-0.3837065682, -0.2206398454, 0.08116235683), Vector3(-0.4382627882, -0.09082753406, -0.04664855374), Vector3(-0.37728353, 0.05445141085, 0.2391488697), Vector3(0.1259579313, 0.348394558, 0.2554522098), Vector3(-0.1406285511, -0.270877371, -0.3306796947), Vector3(-0.1580694418, 0.4162931958, -0.06491553533), Vector3(0.2477612106, -0.2927867412, -0.2353514536), Vector3(0.2916132853, 0.3312535401, 0.08793624968), Vector3(0.07365265219, -0.1666159848, 0.411478311), Vector3(-0.26126526, -0.2422237692, 0.2748965434), Vector3(-0.3721862032, 0.252790166, 0.008634938242), Vector3(-0.3691191571, -0.255281188, 0.03290232422), Vector3(0.2278441737, -0.3358364886, 0.1944244981), Vector3(0.363398169, -0.2310190248, 0.1306597909), Vector3(-0.304231482, -0.2698452035, 0.1926830856), Vector3(-0.3199312232, 0.316332536, -0.008816977938), Vector3(0.2874852279, 0.1642275508, -0.304764754), Vector3(-0.1451096801, 0.3277541114, -0.2720669462), Vector3(0.3220090754, 0.0511344108, 0.3101538769), Vector3(-0.1247400865, -0.04333605335, -0.4301882115), Vector3(-0.2829555867, -0.3056190617, -0.1703910946), Vector3(0.1069384374, 0.3491024667, -0.2630430352), Vector3(-0.1420661144, -0.3055376754, -0.2982682484), Vector3(-0.250548338, 0.3156466809, -0.2002316239), Vector3(0.3265787872, 0.1871229129, 0.2466400438), Vector3(0.07646097258, -0.3026690852, 0.324106687), Vector3(0.3451771584, 0.2757120714, -0.0856480183), Vector3(0.298137964, 0.2852657134, 0.179547284), Vector3(0.2812250376, 0.3466716415, 0.05684409612), Vector3(0.4390345476, -0.09790429955, -0.01278335452), Vector3(0.2148373234, 0.1850172527, 0.3494474791), Vector3(0.2595421179, -0.07946825393, 0.3589187731), Vector3(0.3182823114, -0.307355516, -0.08203022006), Vector3(-0.4089859285, -0.04647718411, 0.1818526372), Vector3(-0.2826749061, 0.07417482322, 0.3421885344), Vector3(0.3483864637, 0.225442246, -0.1740766085), Vector3(-0.3226415069, -0.1420585388, -0.2796816575), Vector3(0.4330734858, -0.118868561, -0.02859407492), Vector3(-0.08717822568, -0.3909896417, -0.2050050172), Vector3(-0.2149678299, 0.3939973956, -0.03247898316), Vector3(-0.2687330705, 0.322686276, -0.1617284888), Vector3(0.2105665099, -0.1961317136, -0.3459683451), Vector3(0.4361845915, -0.1105517485, 0.004616608544), Vector3(0.05333333359, -0.313639498, -0.3182543336), Vector3(-0.05986216652, 0.1361029153, -0.4247264031), Vector3(0.3664988455, 0.2550543014, -0.05590974511), Vector3(-0.2341015558, -0.182405731, 0.3382670703), Vector3(-0.04730947785, -0.4222150243, -0.1483114513), Vector3(-0.2391566239, -0.2577696514, -0.2808182972), Vector3(-0.1242081035, 0.4256953395, -0.07652336246), Vector3(0.2614832715, -0.3650179274, 0.02980623099), Vector3(-0.2728794681, -0.3499628774, 0.07458404908), Vector3(0.007892900508, -0.1672771315, 0.4176793787), Vector3(-0.01730330376, 0.2978486637, -0.3368779738), Vector3(0.2054835762, -0.3252600376, -0.2334146693), Vector3(-0.3231994983, 0.1564282844, -0.2712420987), Vector3(-0.2669545963, 0.2599343665, -0.2523278991), Vector3(-0.05554372779, 0.3170813944, -0.3144428146), Vector3(-0.2083935713, -0.310922837, -0.2497981362), Vector3(0.06989323478, -0.3156141536, 0.3130537363), Vector3(0.3847566193, -0.1605309138, -0.1693876312), Vector3(-0.3026215288, -0.3001537679, -0.1443188342), Vector3(0.3450735512, 0.08611519592, 0.2756962409), Vector3(0.1814473292, -0.2788782453, -0.3029914042), Vector3(-0.03855010448, 0.09795110726, 0.4375151083), Vector3(0.3533670318, 0.2665752752, 0.08105160988), Vector3(-0.007945601311, 0.140359426, -0.4274764309), Vector3(0.4063099273, -0.1491768253, -0.1231199324), Vector3(-0.2016773589, 0.008816271194, -0.4021797064), Vector3(-0.07527055435, -0.425643481, -0.1251477955)) diff --git a/orx-noise/src/main/kotlin/Interpolation.kt b/orx-noise/src/main/kotlin/Interpolation.kt index bf68e1334..10ee5f823 100644 --- a/orx-noise/src/main/kotlin/Interpolation.kt +++ b/orx-noise/src/main/kotlin/Interpolation.kt @@ -1,24 +1,24 @@ -package org.openrndr.extra.noise - - - -fun hermite(t: Double): Double { - return t * t * (3 - 2 * t) -} - -fun quintic(t: Double): Double { - return t * t * t * (t * (t * 6 - 15) + 10) -} - -fun cubic(a: Double, b: Double, c: Double, d: Double, t: Double) : Double { - val p = d - c - (a - b) - return t * t * t * p + t * t * (a - b - p) + t * (c - a) + b -} - -fun linear(x: Double) : Double { - return x -} - -fun lerp(left: Double, right: Double, x: Double): Double { - return left * (1.0 - x) + right * x +package org.openrndr.extra.noise + + + +fun hermite(t: Double): Double { + return t * t * (3 - 2 * t) +} + +fun quintic(t: Double): Double { + return t * t * t * (t * (t * 6 - 15) + 10) +} + +fun cubic(a: Double, b: Double, c: Double, d: Double, t: Double) : Double { + val p = d - c - (a - b) + return t * t * t * p + t * t * (a - b - p) + t * (c - a) + b +} + +fun linear(x: Double) : Double { + return x +} + +fun lerp(left: Double, right: Double, x: Double): Double { + return left * (1.0 - x) + right * x } \ No newline at end of file diff --git a/orx-noise/src/main/kotlin/MathUtils.kt b/orx-noise/src/main/kotlin/MathUtils.kt index b0f3a5a4c..71ceafd1d 100644 --- a/orx-noise/src/main/kotlin/MathUtils.kt +++ b/orx-noise/src/main/kotlin/MathUtils.kt @@ -1,6 +1,6 @@ -package org.openrndr.extra.noise - - -fun Double.fastFloor(): Int { - return if (this >= 0) this.toInt() else this.toInt() - 1 +package org.openrndr.extra.noise + + +fun Double.fastFloor(): Int { + return if (this >= 0) this.toInt() else this.toInt() - 1 } \ No newline at end of file diff --git a/orx-noise/src/main/kotlin/PerlinNoise2D.kt b/orx-noise/src/main/kotlin/PerlinNoise2D.kt index 23a3ceeb1..0398ba796 100644 --- a/orx-noise/src/main/kotlin/PerlinNoise2D.kt +++ b/orx-noise/src/main/kotlin/PerlinNoise2D.kt @@ -1,26 +1,26 @@ -package org.openrndr.extra.noise - -fun perlinLinear(seed: Int, x: Double, y: Double) = perlin(seed, x, y, ::linear) -fun perlinQuintic(seed: Int, x: Double, y: Double) = perlin(seed, x, y, ::quintic) -fun perlinHermite(seed: Int, x: Double, y: Double) = perlin(seed, x, y, ::hermite) - -private fun perlin(seed: Int, x: Double, y: Double, interpolator: (Double) -> Double): Double { - val x0 = x.fastFloor() - val y0 = y.fastFloor() - val x1 = x0 + 1 - val y1 = y0 + 1 - - val xs = interpolator(x - x0) - val ys = interpolator(y - y0) - - - val xd0 = x - x0 - val yd0 = y - y0 - val xd1 = xd0 - 1 - val yd1 = yd0 - 1 - - val xf0 = lerp(gradCoord2D(seed, x0, y0, xd0, yd0), gradCoord2D(seed, x1, y0, xd1, yd0), xs) - val xf1 = lerp(gradCoord2D(seed, x0, y1, xd0, yd1), gradCoord2D(seed, x1, y1, xd1, yd1), xs) - - return lerp(xf0, xf1, ys) +package org.openrndr.extra.noise + +fun perlinLinear(seed: Int, x: Double, y: Double) = perlin(seed, x, y, ::linear) +fun perlinQuintic(seed: Int, x: Double, y: Double) = perlin(seed, x, y, ::quintic) +fun perlinHermite(seed: Int, x: Double, y: Double) = perlin(seed, x, y, ::hermite) + +private fun perlin(seed: Int, x: Double, y: Double, interpolator: (Double) -> Double): Double { + val x0 = x.fastFloor() + val y0 = y.fastFloor() + val x1 = x0 + 1 + val y1 = y0 + 1 + + val xs = interpolator(x - x0) + val ys = interpolator(y - y0) + + + val xd0 = x - x0 + val yd0 = y - y0 + val xd1 = xd0 - 1 + val yd1 = yd0 - 1 + + val xf0 = lerp(gradCoord2D(seed, x0, y0, xd0, yd0), gradCoord2D(seed, x1, y0, xd1, yd0), xs) + val xf1 = lerp(gradCoord2D(seed, x0, y1, xd0, yd1), gradCoord2D(seed, x1, y1, xd1, yd1), xs) + + return lerp(xf0, xf1, ys) } \ No newline at end of file diff --git a/orx-noise/src/main/kotlin/PerlinNoise3D.kt b/orx-noise/src/main/kotlin/PerlinNoise3D.kt index f07dbafa9..d2eb43c91 100644 --- a/orx-noise/src/main/kotlin/PerlinNoise3D.kt +++ b/orx-noise/src/main/kotlin/PerlinNoise3D.kt @@ -1,35 +1,35 @@ -package org.openrndr.extra.noise - -fun perlinLinear(seed: Int, x: Double, y: Double, z: Double) = perlin(seed, x, y, z, ::linear) -fun perlinQuintic(seed: Int, x: Double, y: Double, z: Double) = perlin(seed, x, y, z, ::quintic) -fun perlinHermite(seed: Int, x: Double, y: Double, z: Double) = perlin(seed, x, y, z, ::hermite) - -inline fun perlin(seed: Int, x: Double, y: Double, z: Double, crossinline interpolator: (Double) -> Double = ::linear): Double { - val x0 = x.fastFloor() - val y0 = y.fastFloor() - val z0 = z.fastFloor() - val x1 = x0 + 1 - val y1 = y0 + 1 - val z1 = z0 + 1 - - val xs: Double = interpolator(x - x0) - val ys: Double = interpolator(y - y0) - val zs: Double = interpolator(z - z0) - - val xd0 = x - x0 - val yd0 = y - y0 - val zd0 = z - z0 - val xd1 = xd0 - 1 - val yd1 = yd0 - 1 - val zd1 = zd0 - 1 - - val xf00 = lerp(gradCoord3D(seed, x0, y0, z0, xd0, yd0, zd0), gradCoord3D(seed, x1, y0, z0, xd1, yd0, zd0), xs) - val xf10 = lerp(gradCoord3D(seed, x0, y1, z0, xd0, yd1, zd0), gradCoord3D(seed, x1, y1, z0, xd1, yd1, zd0), xs) - val xf01 = lerp(gradCoord3D(seed, x0, y0, z1, xd0, yd0, zd1), gradCoord3D(seed, x1, y0, z1, xd1, yd0, zd1), xs) - val xf11 = lerp(gradCoord3D(seed, x0, y1, z1, xd0, yd1, zd1), gradCoord3D(seed, x1, y1, z1, xd1, yd1, zd1), xs) - - val yf0 = lerp(xf00, xf10, ys) - val yf1 = lerp(xf01, xf11, ys) - - return lerp(yf0, yf1, zs) +package org.openrndr.extra.noise + +fun perlinLinear(seed: Int, x: Double, y: Double, z: Double) = perlin(seed, x, y, z, ::linear) +fun perlinQuintic(seed: Int, x: Double, y: Double, z: Double) = perlin(seed, x, y, z, ::quintic) +fun perlinHermite(seed: Int, x: Double, y: Double, z: Double) = perlin(seed, x, y, z, ::hermite) + +inline fun perlin(seed: Int, x: Double, y: Double, z: Double, crossinline interpolator: (Double) -> Double = ::linear): Double { + val x0 = x.fastFloor() + val y0 = y.fastFloor() + val z0 = z.fastFloor() + val x1 = x0 + 1 + val y1 = y0 + 1 + val z1 = z0 + 1 + + val xs: Double = interpolator(x - x0) + val ys: Double = interpolator(y - y0) + val zs: Double = interpolator(z - z0) + + val xd0 = x - x0 + val yd0 = y - y0 + val zd0 = z - z0 + val xd1 = xd0 - 1 + val yd1 = yd0 - 1 + val zd1 = zd0 - 1 + + val xf00 = lerp(gradCoord3D(seed, x0, y0, z0, xd0, yd0, zd0), gradCoord3D(seed, x1, y0, z0, xd1, yd0, zd0), xs) + val xf10 = lerp(gradCoord3D(seed, x0, y1, z0, xd0, yd1, zd0), gradCoord3D(seed, x1, y1, z0, xd1, yd1, zd0), xs) + val xf01 = lerp(gradCoord3D(seed, x0, y0, z1, xd0, yd0, zd1), gradCoord3D(seed, x1, y0, z1, xd1, yd0, zd1), xs) + val xf11 = lerp(gradCoord3D(seed, x0, y1, z1, xd0, yd1, zd1), gradCoord3D(seed, x1, y1, z1, xd1, yd1, zd1), xs) + + val yf0 = lerp(xf00, xf10, ys) + val yf1 = lerp(xf01, xf11, ys) + + return lerp(yf0, yf1, zs) } \ No newline at end of file diff --git a/orx-noise/src/main/kotlin/SimplexNoise2D.kt b/orx-noise/src/main/kotlin/SimplexNoise2D.kt index a61b35c65..880ab52dd 100644 --- a/orx-noise/src/main/kotlin/SimplexNoise2D.kt +++ b/orx-noise/src/main/kotlin/SimplexNoise2D.kt @@ -1,63 +1,63 @@ -package org.openrndr.extra.noise - - -private val G2 = 1.0 / 4.0 -private val F2 = 1.0 / 2.0 - -fun simplex(seed: Int, x: Double, y: Double): Double { - var t = (x + y) * F2 - val i = (x + t).fastFloor() - val j = (y + t).fastFloor() - - t = ((i + j) * G2) - val X0 = i - t - val Y0 = j - t - - val x0 = x - X0 - val y0 = y - Y0 - - val i1: Int - val j1: Int - if (x0 > y0) { - i1 = 1 - j1 = 0 - } else { - i1 = 0 - j1 = 1 - } - - val x1 = x0 - i1 + G2 - val y1 = y0 - j1 + G2 - val x2 = x0 - 1 + F2 - val y2 = y0 - 1 + F2 - - val n0: Double - val n1: Double - val n2: Double - - t = 0.5 - x0 * x0 - y0 * y0 - if (t < 0) - n0 = 0.0 - else { - t *= t - n0 = t * t * gradCoord2D(seed, i, j, x0, y0) - } - - t = 0.5 - x1 * x1 - y1 * y1 - if (t < 0) - n1 = 0.0 - else { - t *= t - n1 = t * t * gradCoord2D(seed, i + i1, j + j1, x1, y1) - } - - t = 0.5 - x2 * x2 - y2 * y2 - if (t < 0) - n2 = 0.0 - else { - t *= t - n2 = t * t * gradCoord2D(seed, i + 1, j + 1, x2, y2) - } - - return 50.0 * (n0 + n1 + n2) +package org.openrndr.extra.noise + + +private val G2 = 1.0 / 4.0 +private val F2 = 1.0 / 2.0 + +fun simplex(seed: Int, x: Double, y: Double): Double { + var t = (x + y) * F2 + val i = (x + t).fastFloor() + val j = (y + t).fastFloor() + + t = ((i + j) * G2) + val X0 = i - t + val Y0 = j - t + + val x0 = x - X0 + val y0 = y - Y0 + + val i1: Int + val j1: Int + if (x0 > y0) { + i1 = 1 + j1 = 0 + } else { + i1 = 0 + j1 = 1 + } + + val x1 = x0 - i1 + G2 + val y1 = y0 - j1 + G2 + val x2 = x0 - 1 + F2 + val y2 = y0 - 1 + F2 + + val n0: Double + val n1: Double + val n2: Double + + t = 0.5 - x0 * x0 - y0 * y0 + if (t < 0) + n0 = 0.0 + else { + t *= t + n0 = t * t * gradCoord2D(seed, i, j, x0, y0) + } + + t = 0.5 - x1 * x1 - y1 * y1 + if (t < 0) + n1 = 0.0 + else { + t *= t + n1 = t * t * gradCoord2D(seed, i + i1, j + j1, x1, y1) + } + + t = 0.5 - x2 * x2 - y2 * y2 + if (t < 0) + n2 = 0.0 + else { + t *= t + n2 = t * t * gradCoord2D(seed, i + 1, j + 1, x2, y2) + } + + return 50.0 * (n0 + n1 + n2) } \ No newline at end of file diff --git a/orx-noise/src/main/kotlin/SimplexNoise3D.kt b/orx-noise/src/main/kotlin/SimplexNoise3D.kt index af718c926..7afb4ffab 100644 --- a/orx-noise/src/main/kotlin/SimplexNoise3D.kt +++ b/orx-noise/src/main/kotlin/SimplexNoise3D.kt @@ -1,94 +1,94 @@ -package org.openrndr.extra.noise - -fun simplex(seed: Int, x: Double, y: Double, z: Double): Double { - - var t = (x + y + z) / 3.0 - val i = (x + t).fastFloor() - val j = (y + t).fastFloor() - val k = (z + t).fastFloor() - - val t2 = (i + j + k) / 6.0 - val x0 = x - (i - t2) - val y0 = y - (j - t2) - val z0 = z - (k - t2) - - val i1: Int - val j1: Int - val k1: Int - - val i2: Int - val j2: Int - val k2: Int - - if (x0 >= y0) { - when { - y0 >= z0 -> { - i1 = 1; j1 = 0; k1 = 0; i2 = 1; j2 = 1; k2 = 0; } - x0 >= z0 -> { - i1 = 1; j1 = 0; k1 = 0; i2 = 1; j2 = 0; k2 = 1; } - else -> { - i1 = 0; j1 = 0; k1 = 1; i2 = 1; j2 = 0; k2 = 1; } - } - } else { - when { - y0 < z0 -> { - i1 = 0; j1 = 0; k1 = 1; i2 = 0; j2 = 1; k2 = 1; } - x0 < z0 -> { - i1 = 0; j1 = 1; k1 = 0; i2 = 0; j2 = 1; k2 = 1; } - else -> { - i1 = 0; j1 = 1; k1 = 0; i2 = 1; j2 = 1; k2 = 0; } - } - } - val x1 = x0 - i1 + 1.0 / 6.0 - val y1 = y0 - j1 + 1.0 / 6.0 - val z1 = z0 - k1 + 1.0 / 6.0 - val x2 = x0 - i2 + 1.0 / 3.0 - val y2 = y0 - j2 + 1.0 / 3.0 - val z2 = z0 - k2 + 1.0 / 3.0 - val x3 = x0 + ((1.0 / 6.0) * 3.0 - 1.0) - val y3 = y0 + ((1.0 / 6.0) * 3.0 - 1.0) - val z3 = z0 + ((1.0 / 6.0) * 3.0 - 1.0) - - val n0: Double - run { - var t = 0.6 * x0 * x0 - y0 * y0 - z0 * z0 - if (t < 0) { - n0 = 0.0 - } else { - t *= t - n0 = t * t * gradCoord3D(seed, i, j, k, x0, y0, z0) - } - } - val n1: Double - run { - var t = 0.6 * x1 * x1 - y1 * y1 - z1 * z1 - if (t < 0) { - n1 = 0.0 - } else { - t *= t - n1 = t * t * gradCoord3D(seed, i + i1, j + j1, k + k1, x1, y1, z1) - } - } - val n2: Double - run { - var t = 0.6 * x2 * x2 - y2 * y2 - z2 * z2 - if (t < 0) { - n2 = 0.0 - } else { - t *= t - n2 = t * t * gradCoord3D(seed, i + i2, j + j2, k + k2, x2, y2, z2) - } - } - - val n3: Double - run { - var t = 0.6 - x3 * x3 - y3 * y3 - z3 * z3 - if (t < 0) - n3 = 0.0 - else { - t *= t - n3 = t * t * gradCoord3D(seed, i + 1, j + 1, k + 1, x3, y3, z3) - } - } - return 32 * (n0 + n1 + n2 + n3) +package org.openrndr.extra.noise + +fun simplex(seed: Int, x: Double, y: Double, z: Double): Double { + + var t = (x + y + z) / 3.0 + val i = (x + t).fastFloor() + val j = (y + t).fastFloor() + val k = (z + t).fastFloor() + + val t2 = (i + j + k) / 6.0 + val x0 = x - (i - t2) + val y0 = y - (j - t2) + val z0 = z - (k - t2) + + val i1: Int + val j1: Int + val k1: Int + + val i2: Int + val j2: Int + val k2: Int + + if (x0 >= y0) { + when { + y0 >= z0 -> { + i1 = 1; j1 = 0; k1 = 0; i2 = 1; j2 = 1; k2 = 0; } + x0 >= z0 -> { + i1 = 1; j1 = 0; k1 = 0; i2 = 1; j2 = 0; k2 = 1; } + else -> { + i1 = 0; j1 = 0; k1 = 1; i2 = 1; j2 = 0; k2 = 1; } + } + } else { + when { + y0 < z0 -> { + i1 = 0; j1 = 0; k1 = 1; i2 = 0; j2 = 1; k2 = 1; } + x0 < z0 -> { + i1 = 0; j1 = 1; k1 = 0; i2 = 0; j2 = 1; k2 = 1; } + else -> { + i1 = 0; j1 = 1; k1 = 0; i2 = 1; j2 = 1; k2 = 0; } + } + } + val x1 = x0 - i1 + 1.0 / 6.0 + val y1 = y0 - j1 + 1.0 / 6.0 + val z1 = z0 - k1 + 1.0 / 6.0 + val x2 = x0 - i2 + 1.0 / 3.0 + val y2 = y0 - j2 + 1.0 / 3.0 + val z2 = z0 - k2 + 1.0 / 3.0 + val x3 = x0 + ((1.0 / 6.0) * 3.0 - 1.0) + val y3 = y0 + ((1.0 / 6.0) * 3.0 - 1.0) + val z3 = z0 + ((1.0 / 6.0) * 3.0 - 1.0) + + val n0: Double + run { + var t = 0.6 * x0 * x0 - y0 * y0 - z0 * z0 + if (t < 0) { + n0 = 0.0 + } else { + t *= t + n0 = t * t * gradCoord3D(seed, i, j, k, x0, y0, z0) + } + } + val n1: Double + run { + var t = 0.6 * x1 * x1 - y1 * y1 - z1 * z1 + if (t < 0) { + n1 = 0.0 + } else { + t *= t + n1 = t * t * gradCoord3D(seed, i + i1, j + j1, k + k1, x1, y1, z1) + } + } + val n2: Double + run { + var t = 0.6 * x2 * x2 - y2 * y2 - z2 * z2 + if (t < 0) { + n2 = 0.0 + } else { + t *= t + n2 = t * t * gradCoord3D(seed, i + i2, j + j2, k + k2, x2, y2, z2) + } + } + + val n3: Double + run { + var t = 0.6 - x3 * x3 - y3 * y3 - z3 * z3 + if (t < 0) + n3 = 0.0 + else { + t *= t + n3 = t * t * gradCoord3D(seed, i + 1, j + 1, k + 1, x3, y3, z3) + } + } + return 32 * (n0 + n1 + n2 + n3) } \ No newline at end of file diff --git a/orx-noise/src/main/kotlin/UniformRandom.kt b/orx-noise/src/main/kotlin/UniformRandom.kt index 05e9f0b7c..e621b74a7 100644 --- a/orx-noise/src/main/kotlin/UniformRandom.kt +++ b/orx-noise/src/main/kotlin/UniformRandom.kt @@ -1,71 +1,71 @@ -package org.openrndr.extra.noise - -import org.openrndr.math.Vector2 -import org.openrndr.math.Vector3 -import org.openrndr.math.Vector4 - - -fun Double.Companion.uniform(min: Double = -1.0, max: Double = 1.0): Double { - return (Math.random() * (max - min)) + min -} - -fun Vector2.Companion.uniform(min: Vector2 = -ONE, max: Vector2 = ONE): Vector2 { - return Vector2(Double.uniform(min.x, max.x), Double.uniform(min.y, max.y)) -} - -fun Vector2.Companion.uniform(min: Double = -1.0, max: Double = 1.0) = - Vector2.uniform(Vector2(min, min), Vector2(max, max)) - -fun Vector2.Companion.uniformRing(innerRadius: Double = 0.0, outerRadius: Double = 1.0): Vector2 { - while (true) { - uniform(-outerRadius, outerRadius).let { - val squaredLength = it.squaredLength - if (squaredLength >= innerRadius * innerRadius && squaredLength < outerRadius * outerRadius) { - return it - } - } - } -} - -fun Vector3.Companion.uniform(min: Double = -1.0, max: Double = 1.0): Vector3 = - Vector3.uniform(Vector3(min, min, min), Vector3(max, max, max)) - -fun Vector3.Companion.uniform(min: Vector3 = -ONE, max: Vector3 = ONE): Vector3 { - return Vector3(Double.uniform(min.x, max.x), Double.uniform(min.y, max.y), Double.uniform(min.z, max.z)) -} - -// squared length 'polyfill' for OPENRNDR 0.3.30 -private val Vector3.squaredLength__: Double get() = x * x + y * y + z * z - -fun Vector3.Companion.uniformRing(innerRadius: Double = 0.0, outerRadius: Double = 1.0): Vector3 { - while (true) { - uniform(-outerRadius, outerRadius).let { - val squaredLength = it.squaredLength__ - if (squaredLength >= innerRadius * innerRadius && squaredLength < outerRadius * outerRadius) { - return it - } - } - } -} - -// squared length 'polyfill' for OPENRNDR 0.3.30 -private val Vector4.squaredLength__: Double get() = x * x + y * y + z * z + w * w - -fun Vector4.Companion.uniform(min: Double = -1.0, max: Double = 1.0): Vector4 = - Vector4.uniform(Vector4(min, min, min, min), Vector4(max, max,max, max)) - -fun Vector4.Companion.uniform(min: Vector4 = -ONE, max: Vector4 = ONE): Vector4 { - return Vector4(Double.uniform(min.x, max.x), Double.uniform(min.y, max.y), Double.uniform(min.z, max.z), Double.uniform(min.w, max.w)) -} - -fun Vector4.Companion.uniformRing(innerRadius: Double = 0.0, outerRadius: Double = 1.0): Vector4 { - while (true) { - uniform(-outerRadius, outerRadius).let { - val squaredLength = it.squaredLength__ - if (squaredLength >= innerRadius * innerRadius && squaredLength < outerRadius * outerRadius) { - return it - } - } - } -} - +package org.openrndr.extra.noise + +import org.openrndr.math.Vector2 +import org.openrndr.math.Vector3 +import org.openrndr.math.Vector4 + + +fun Double.Companion.uniform(min: Double = -1.0, max: Double = 1.0): Double { + return (Math.random() * (max - min)) + min +} + +fun Vector2.Companion.uniform(min: Vector2 = -ONE, max: Vector2 = ONE): Vector2 { + return Vector2(Double.uniform(min.x, max.x), Double.uniform(min.y, max.y)) +} + +fun Vector2.Companion.uniform(min: Double = -1.0, max: Double = 1.0) = + Vector2.uniform(Vector2(min, min), Vector2(max, max)) + +fun Vector2.Companion.uniformRing(innerRadius: Double = 0.0, outerRadius: Double = 1.0): Vector2 { + while (true) { + uniform(-outerRadius, outerRadius).let { + val squaredLength = it.squaredLength + if (squaredLength >= innerRadius * innerRadius && squaredLength < outerRadius * outerRadius) { + return it + } + } + } +} + +fun Vector3.Companion.uniform(min: Double = -1.0, max: Double = 1.0): Vector3 = + Vector3.uniform(Vector3(min, min, min), Vector3(max, max, max)) + +fun Vector3.Companion.uniform(min: Vector3 = -ONE, max: Vector3 = ONE): Vector3 { + return Vector3(Double.uniform(min.x, max.x), Double.uniform(min.y, max.y), Double.uniform(min.z, max.z)) +} + +// squared length 'polyfill' for OPENRNDR 0.3.30 +private val Vector3.squaredLength__: Double get() = x * x + y * y + z * z + +fun Vector3.Companion.uniformRing(innerRadius: Double = 0.0, outerRadius: Double = 1.0): Vector3 { + while (true) { + uniform(-outerRadius, outerRadius).let { + val squaredLength = it.squaredLength__ + if (squaredLength >= innerRadius * innerRadius && squaredLength < outerRadius * outerRadius) { + return it + } + } + } +} + +// squared length 'polyfill' for OPENRNDR 0.3.30 +private val Vector4.squaredLength__: Double get() = x * x + y * y + z * z + w * w + +fun Vector4.Companion.uniform(min: Double = -1.0, max: Double = 1.0): Vector4 = + Vector4.uniform(Vector4(min, min, min, min), Vector4(max, max,max, max)) + +fun Vector4.Companion.uniform(min: Vector4 = -ONE, max: Vector4 = ONE): Vector4 { + return Vector4(Double.uniform(min.x, max.x), Double.uniform(min.y, max.y), Double.uniform(min.z, max.z), Double.uniform(min.w, max.w)) +} + +fun Vector4.Companion.uniformRing(innerRadius: Double = 0.0, outerRadius: Double = 1.0): Vector4 { + while (true) { + uniform(-outerRadius, outerRadius).let { + val squaredLength = it.squaredLength__ + if (squaredLength >= innerRadius * innerRadius && squaredLength < outerRadius * outerRadius) { + return it + } + } + } +} + diff --git a/orx-noise/src/main/kotlin/ValueNoise2D.kt b/orx-noise/src/main/kotlin/ValueNoise2D.kt index 6070d6317..222000efc 100644 --- a/orx-noise/src/main/kotlin/ValueNoise2D.kt +++ b/orx-noise/src/main/kotlin/ValueNoise2D.kt @@ -1,21 +1,21 @@ -package org.openrndr.extra.noise - -fun valueLinear(seed: Int, x:Double, y:Double) = value(seed, x, y, ::linear) -fun valueQuintic(seed: Int, x:Double, y:Double) = value(seed, x, y, ::quintic) -fun valueHermite(seed: Int, x:Double, y:Double) = value(seed, x, y, ::hermite) - - -inline fun value(seed: Int, x: Double, y: Double, crossinline interpolation: (Double) -> Double = ::linear): Double { - val x0 = x.fastFloor() - val y0 = y.fastFloor() - val x1 = x0 + 1 - val y1 = y0 + 1 - - val xs = interpolation(x - x0) - val ys = interpolation(y - y0) - - val xf0 = lerp(valCoord2D(seed, x0, y0), valCoord2D(seed, x1, y0), xs) - val xf1 = lerp(valCoord2D(seed, x0, y1), valCoord2D(seed, x1, y1), xs) - - return lerp(xf0, xf1, ys) +package org.openrndr.extra.noise + +fun valueLinear(seed: Int, x:Double, y:Double) = value(seed, x, y, ::linear) +fun valueQuintic(seed: Int, x:Double, y:Double) = value(seed, x, y, ::quintic) +fun valueHermite(seed: Int, x:Double, y:Double) = value(seed, x, y, ::hermite) + + +inline fun value(seed: Int, x: Double, y: Double, crossinline interpolation: (Double) -> Double = ::linear): Double { + val x0 = x.fastFloor() + val y0 = y.fastFloor() + val x1 = x0 + 1 + val y1 = y0 + 1 + + val xs = interpolation(x - x0) + val ys = interpolation(y - y0) + + val xf0 = lerp(valCoord2D(seed, x0, y0), valCoord2D(seed, x1, y0), xs) + val xf1 = lerp(valCoord2D(seed, x0, y1), valCoord2D(seed, x1, y1), xs) + + return lerp(xf0, xf1, ys) } \ No newline at end of file diff --git a/orx-noise/src/main/kotlin/ValueNoise3D.kt b/orx-noise/src/main/kotlin/ValueNoise3D.kt index 642d021d9..790fd1285 100644 --- a/orx-noise/src/main/kotlin/ValueNoise3D.kt +++ b/orx-noise/src/main/kotlin/ValueNoise3D.kt @@ -1,29 +1,29 @@ -package org.openrndr.extra.noise - -fun valueLinear(seed: Int, x:Double, y:Double, z:Double) = value(seed, x, y, z, ::linear) -fun valueQuintic(seed: Int, x:Double, y:Double, z:Double) = value(seed, x, y, z, ::quintic) -fun valueHermite(seed: Int, x:Double, y:Double, z:Double) = value(seed, x, y, z, ::hermite) - -inline fun value(seed:Int, x: Double, y: Double, z: Double, crossinline interpolation:(Double)->Double = ::linear) : Double { - val x0 = x.fastFloor() - val y0 = y.fastFloor() - val z0 = z.fastFloor() - val x1 = x0 + 1 - val y1 = y0 + 1 - val z1 = z0 + 1 - - val xs = interpolation(x - x0) - val ys = interpolation(y - y0) - val zs = interpolation(z - z0) - - - val xf00 = lerp(valCoord3D(seed, x0, y0, z0), valCoord3D(seed, x1, y0, z0), xs) - val xf10 = lerp(valCoord3D(seed, x0, y1, z0), valCoord3D(seed, x1, y1, z0), xs) - val xf01 = lerp(valCoord3D(seed, x0, y0, z1), valCoord3D(seed, x1, y0, z1), xs) - val xf11 = lerp(valCoord3D(seed, x0, y1, z1), valCoord3D(seed, x1, y1, z1), xs) - - val yf0 = lerp(xf00, xf10, ys) - val yf1 = lerp(xf01, xf11, ys) - - return lerp(yf0, yf1, zs) +package org.openrndr.extra.noise + +fun valueLinear(seed: Int, x:Double, y:Double, z:Double) = value(seed, x, y, z, ::linear) +fun valueQuintic(seed: Int, x:Double, y:Double, z:Double) = value(seed, x, y, z, ::quintic) +fun valueHermite(seed: Int, x:Double, y:Double, z:Double) = value(seed, x, y, z, ::hermite) + +inline fun value(seed:Int, x: Double, y: Double, z: Double, crossinline interpolation:(Double)->Double = ::linear) : Double { + val x0 = x.fastFloor() + val y0 = y.fastFloor() + val z0 = z.fastFloor() + val x1 = x0 + 1 + val y1 = y0 + 1 + val z1 = z0 + 1 + + val xs = interpolation(x - x0) + val ys = interpolation(y - y0) + val zs = interpolation(z - z0) + + + val xf00 = lerp(valCoord3D(seed, x0, y0, z0), valCoord3D(seed, x1, y0, z0), xs) + val xf10 = lerp(valCoord3D(seed, x0, y1, z0), valCoord3D(seed, x1, y1, z0), xs) + val xf01 = lerp(valCoord3D(seed, x0, y0, z1), valCoord3D(seed, x1, y0, z1), xs) + val xf11 = lerp(valCoord3D(seed, x0, y1, z1), valCoord3D(seed, x1, y1, z1), xs) + + val yf0 = lerp(xf00, xf10, ys) + val yf1 = lerp(xf01, xf11, ys) + + return lerp(yf0, yf1, zs) } \ No newline at end of file