[orx-noise] Add hash functions
This commit is contained in:
@@ -10,6 +10,7 @@ kotlin {
|
||||
api(libs.openrndr.math)
|
||||
api(libs.openrndr.shape)
|
||||
api(project(":orx-mesh"))
|
||||
implementation(project(":orx-noise"))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,9 @@ package org.openrndr.extra.mesh.noise
|
||||
import org.openrndr.extra.mesh.IIndexedPolygon
|
||||
import org.openrndr.extra.mesh.IMeshData
|
||||
import org.openrndr.extra.mesh.IVertexData
|
||||
import org.openrndr.extra.noise.fhash1D
|
||||
import org.openrndr.extra.noise.uhash11
|
||||
import org.openrndr.extra.noise.uhash1D
|
||||
import org.openrndr.math.Vector3
|
||||
import kotlin.math.sqrt
|
||||
import kotlin.random.Random
|
||||
@@ -20,6 +23,23 @@ fun uniformBarycentric(random: Random = Random.Default): Vector3 {
|
||||
return Vector3(b0, b1, 1.0 - b0 - b1)
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a uniformly distributed barycentric coordinate
|
||||
* @param random a random number generator
|
||||
*/
|
||||
fun hashBarycentric(seed: Int, x: Int): Vector3 {
|
||||
val u = fhash1D(seed, x)
|
||||
val v = fhash1D(seed, u.toRawBits().toInt() - x)
|
||||
|
||||
|
||||
|
||||
val su0 = sqrt(u)
|
||||
val b0 = 1.0 - su0
|
||||
val b1 = v * su0
|
||||
return Vector3(b0, b1, 1.0 - b0 - b1)
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generate a uniformly distributed point that lies inside this [IIndexedPolygon]
|
||||
* @param vertexData vertex data used to resolve positions
|
||||
@@ -33,6 +53,19 @@ fun IIndexedPolygon.uniform(vertexData: IVertexData, random: Random = Random.Def
|
||||
return x[0] * b.x + x[1] * b.y + x[2] * b.z
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a uniformly distributed point that lies inside this [IIndexedPolygon]
|
||||
* @param vertexData vertex data used to resolve positions
|
||||
* @param random a random number generator
|
||||
*/
|
||||
fun IIndexedPolygon.hash(vertexData: IVertexData, seed:Int, x: Int): Vector3 {
|
||||
require(positions.size == 3) { "polygon must be a triangle"}
|
||||
|
||||
val s = vertexData.positions.slice(positions)
|
||||
val b = hashBarycentric(seed, x)
|
||||
return s[0] * b.x + s[1] * b.y + s[2] * b.z
|
||||
}
|
||||
|
||||
internal fun IIndexedPolygon.area(vertexData: IVertexData): Double {
|
||||
require(positions.size == 3) { "polygon must be a triangle"}
|
||||
val x = vertexData.positions.slice(positions)
|
||||
@@ -62,4 +95,27 @@ fun IMeshData.uniform(count: Int, random: Random = Random.Default): List<Vector3
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate points on the surface described by the mesh data
|
||||
*/
|
||||
fun IMeshData.hash(count: Int, seed:Int, x: Int): List<Vector3> {
|
||||
val triangulated = triangulate()
|
||||
val result = mutableListOf<Vector3>()
|
||||
val totalArea = triangulated.polygons.sumOf { it.area(vertexData) }
|
||||
val randoms = (0 until count).map {
|
||||
Pair(x + it, fhash1D(seed, x + it) * totalArea)
|
||||
}.sortedBy { it.second }
|
||||
|
||||
var idx = 0
|
||||
var sum = 0.0
|
||||
for (t in triangulated.polygons) {
|
||||
sum += t.area(vertexData)
|
||||
while (idx <= randoms.lastIndex && sum > randoms[idx].second) {
|
||||
result.add(t.hash(vertexData, seed xor 0x7f7f7f, randoms[idx].first))
|
||||
idx++
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
48
orx-mesh-noise/src/jvmDemo/kotlin/DemoMeshNoise02.kt
Normal file
48
orx-mesh-noise/src/jvmDemo/kotlin/DemoMeshNoise02.kt
Normal file
@@ -0,0 +1,48 @@
|
||||
import org.openrndr.application
|
||||
import org.openrndr.draw.DrawPrimitive
|
||||
import org.openrndr.draw.isolated
|
||||
import org.openrndr.draw.shadeStyle
|
||||
import org.openrndr.extra.camera.Orbital
|
||||
import org.openrndr.extra.mesh.noise.hash
|
||||
import org.openrndr.extra.objloader.loadOBJMeshData
|
||||
import org.openrndr.extra.mesh.noise.uniform
|
||||
import org.openrndr.extra.meshgenerators.sphereMesh
|
||||
import org.openrndr.math.Vector3
|
||||
import java.io.File
|
||||
import kotlin.math.cos
|
||||
import kotlin.random.Random
|
||||
|
||||
/**
|
||||
* Demonstrate uniform point on mesh generation using hash functions
|
||||
*/
|
||||
fun main() {
|
||||
application {
|
||||
configure {
|
||||
width = 720
|
||||
height = 720
|
||||
}
|
||||
program {
|
||||
val mesh = loadOBJMeshData(File("demo-data/obj-models/suzanne/Suzanne.obj")).toMeshData()
|
||||
|
||||
val sphere = sphereMesh(radius = 0.01)
|
||||
extend(Orbital()) {
|
||||
eye = Vector3(0.0, 0.0, 2.0)
|
||||
}
|
||||
extend {
|
||||
|
||||
val points = mesh.hash((1000 + (cos(seconds)*0.5+0.5)*9000).toInt(), 808, (seconds*10000).toInt())
|
||||
|
||||
|
||||
drawer.shadeStyle = shadeStyle {
|
||||
fragmentTransform = "x_fill = vec4(v_viewNormal*0.5+0.5, 1.0);"
|
||||
}
|
||||
for (point in points) {
|
||||
drawer.isolated {
|
||||
drawer.translate(point)
|
||||
drawer.vertexBuffer(sphere, DrawPrimitive.TRIANGLES)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user