-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[orx-mesh, orx-mesh-noise] Add orx-mesh-noise
- Loading branch information
Showing
22 changed files
with
191 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
plugins { | ||
org.openrndr.extra.convention.`kotlin-multiplatform` | ||
} | ||
|
||
kotlin { | ||
sourceSets { | ||
val commonMain by getting { | ||
dependencies { | ||
api(libs.openrndr.application) | ||
api(libs.openrndr.math) | ||
api(libs.openrndr.shape) | ||
api(project(":orx-mesh")) | ||
} | ||
} | ||
|
||
val jvmDemo by getting { | ||
dependencies { | ||
api(libs.openrndr.shape) | ||
implementation(project(":orx-shapes")) | ||
implementation(project(":orx-mesh")) | ||
implementation(project(":orx-mesh-generators")) | ||
implementation(project(":orx-obj-loader")) | ||
implementation(project(":orx-noise")) | ||
implementation(project(":orx-camera")) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
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.math.Vector3 | ||
import kotlin.math.sqrt | ||
import kotlin.random.Random | ||
|
||
/** | ||
* Generate a uniformly distributed barycentric coordinate | ||
* @param random a random number generator | ||
*/ | ||
fun uniformBarycentric(random: Random = Random.Default): Vector3 { | ||
val u = random.nextDouble() | ||
val v = random.nextDouble() | ||
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 | ||
* @param random a random number generator | ||
*/ | ||
fun IIndexedPolygon.uniform(vertexData: IVertexData, random: Random = Random.Default): Vector3 { | ||
require(positions.size == 3) { "polygon must be a triangle"} | ||
|
||
val x = vertexData.positions.slice(positions) | ||
val b = uniformBarycentric(random) | ||
return x[0] * b.x + x[1] * b.y + x[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) | ||
val u = x[1] - x[0] | ||
val v = x[2] - x[0] | ||
return u.areaBetween(v) / 2.0 | ||
} | ||
|
||
/** | ||
* Generate points on the surface described by the mesh data | ||
*/ | ||
fun IMeshData.uniform(count: Int, random: Random = Random.Default): List<Vector3> { | ||
val triangulated = triangulate() | ||
val result = mutableListOf<Vector3>() | ||
val totalArea = triangulated.polygons.sumOf { it.area(vertexData) } | ||
val randoms = (0 until count).map { | ||
random.nextDouble(totalArea) | ||
}.sorted() | ||
|
||
var idx = 0 | ||
var sum = 0.0 | ||
for (t in triangulated.polygons) { | ||
sum += t.area(vertexData) | ||
while (idx <= randoms.lastIndex && sum > randoms[idx]) { | ||
result.add(t.uniform(vertexData, random)) | ||
idx++ | ||
} | ||
} | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
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.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.random.Random | ||
|
||
/** | ||
* Demonstrate uniform point on mesh generation | ||
*/ | ||
fun main() { | ||
application { | ||
configure { | ||
width = 720 | ||
height = 720 | ||
} | ||
program { | ||
val mesh = loadOBJMeshData(File("demo-data/obj-models/suzanne/Suzanne.obj")).toMeshData() | ||
val points = mesh.uniform(1000, Random(0)) | ||
|
||
val sphere = sphereMesh(radius = 0.1) | ||
extend(Orbital()) { | ||
eye = Vector3(0.0, 0.0, 2.0) | ||
} | ||
extend { | ||
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) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package org.openrndr.extra.objloader | ||
package org.openrndr.extra.mesh | ||
|
||
import org.openrndr.math.Vector3 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters