[orx-dnk3] Added DSL
This commit is contained in:
86
orx-dnk3/src/demo/kotlin/DemoDSL01.kt
Normal file
86
orx-dnk3/src/demo/kotlin/DemoDSL01.kt
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
import org.openrndr.application
|
||||||
|
import org.openrndr.color.ColorRGBa
|
||||||
|
import org.openrndr.extensions.SingleScreenshot
|
||||||
|
import org.openrndr.extra.dnk3.dsl.*
|
||||||
|
import org.openrndr.extra.dnk3.renderers.dryRenderer
|
||||||
|
import org.openrndr.extra.dnk3.tools.addSkybox
|
||||||
|
import org.openrndr.extras.camera.Orbital
|
||||||
|
import org.openrndr.extras.meshgenerators.boxMesh
|
||||||
|
import org.openrndr.extras.meshgenerators.groundPlaneMesh
|
||||||
|
import org.openrndr.math.Vector3
|
||||||
|
import org.openrndr.math.transforms.transform
|
||||||
|
|
||||||
|
fun main() = application {
|
||||||
|
configure {
|
||||||
|
width = 1280
|
||||||
|
height = 720
|
||||||
|
}
|
||||||
|
|
||||||
|
program {
|
||||||
|
if (System.getProperty("takeScreenshot") == "true") {
|
||||||
|
extend(SingleScreenshot()) {
|
||||||
|
this.outputFile = System.getProperty("screenshotPath")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
extend(Orbital()) {
|
||||||
|
eye = Vector3(4.0, 4.0, 4.0)
|
||||||
|
}
|
||||||
|
|
||||||
|
val renderer = dryRenderer()
|
||||||
|
val scene = scene {
|
||||||
|
|
||||||
|
addSkybox("file:demo-data/cubemaps/garage_iem.dds")
|
||||||
|
|
||||||
|
root.hemisphereLight {
|
||||||
|
upColor = ColorRGBa.WHITE.shade(0.1)
|
||||||
|
downColor = ColorRGBa.BLACK
|
||||||
|
}
|
||||||
|
|
||||||
|
root.node {
|
||||||
|
transform = transform {
|
||||||
|
translate(0.0, 2.0, 0.0)
|
||||||
|
}
|
||||||
|
|
||||||
|
pointLight {
|
||||||
|
constantAttenuation = 0.0
|
||||||
|
quadraticAttenuation = 1.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
root.node {
|
||||||
|
simpleMesh {
|
||||||
|
vertexBuffer = groundPlaneMesh(100.0, 100.0)
|
||||||
|
material = pbrMaterial {
|
||||||
|
color = ColorRGBa.GREEN
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (j in -3..3) {
|
||||||
|
for (i in -3..3) {
|
||||||
|
root.node {
|
||||||
|
transform = transform {
|
||||||
|
translate(i * 2.0, 1.0, j * 2.0)
|
||||||
|
}
|
||||||
|
update {
|
||||||
|
transform = transform {
|
||||||
|
translate(i * 2.0, 1.0, j * 2.0)
|
||||||
|
rotate(Vector3.UNIT_Z, seconds* 45.0 + i * 20.0 + j * 50.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
simpleMesh {
|
||||||
|
vertexBuffer = boxMesh()
|
||||||
|
material = pbrMaterial {
|
||||||
|
color = ColorRGBa.WHITE
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
extend {
|
||||||
|
drawer.clear(ColorRGBa.BLACK)
|
||||||
|
renderer.draw(drawer, scene)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
12
orx-dnk3/src/main/kotlin/dsl/PBRMaterialBuilder.kt
Normal file
12
orx-dnk3/src/main/kotlin/dsl/PBRMaterialBuilder.kt
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package org.openrndr.extra.dnk3.dsl
|
||||||
|
|
||||||
|
import org.openrndr.extra.dnk3.PBRMaterial
|
||||||
|
|
||||||
|
fun pbrMaterial(builder: PBRMaterial.() -> Unit): PBRMaterial {
|
||||||
|
return PBRMaterial().apply { builder() }
|
||||||
|
}
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
pbrMaterial {
|
||||||
|
}
|
||||||
|
}
|
||||||
80
orx-dnk3/src/main/kotlin/dsl/SceneBuilder.kt
Normal file
80
orx-dnk3/src/main/kotlin/dsl/SceneBuilder.kt
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
package org.openrndr.extra.dnk3.dsl
|
||||||
|
|
||||||
|
import kotlinx.coroutines.yield
|
||||||
|
import org.openrndr.draw.DrawPrimitive
|
||||||
|
import org.openrndr.draw.VertexBuffer
|
||||||
|
import org.openrndr.extra.dnk3.*
|
||||||
|
import org.openrndr.launch
|
||||||
|
|
||||||
|
fun scene(builder: Scene.() -> Unit): Scene {
|
||||||
|
val scene = Scene()
|
||||||
|
scene.builder()
|
||||||
|
return scene
|
||||||
|
}
|
||||||
|
|
||||||
|
fun SceneNode.node(builder: SceneNode.() -> Unit): SceneNode {
|
||||||
|
val node = SceneNode()
|
||||||
|
node.builder()
|
||||||
|
children.add(node)
|
||||||
|
return node
|
||||||
|
}
|
||||||
|
|
||||||
|
fun SceneNode.hemisphereLight(builder: HemisphereLight.() -> Unit) : HemisphereLight {
|
||||||
|
val hemisphereLight = HemisphereLight()
|
||||||
|
hemisphereLight.builder()
|
||||||
|
entities.add(hemisphereLight)
|
||||||
|
return hemisphereLight
|
||||||
|
}
|
||||||
|
|
||||||
|
fun SceneNode.directionalLight(buider: DirectionalLight.() -> Unit): DirectionalLight {
|
||||||
|
val directionalLight = DirectionalLight()
|
||||||
|
directionalLight.buider()
|
||||||
|
this.entities.add(directionalLight)
|
||||||
|
return directionalLight
|
||||||
|
}
|
||||||
|
|
||||||
|
fun SceneNode.pointLight(builder: PointLight.() -> Unit): PointLight {
|
||||||
|
val pointLight = PointLight()
|
||||||
|
pointLight.builder()
|
||||||
|
this.entities.add(pointLight)
|
||||||
|
return pointLight
|
||||||
|
}
|
||||||
|
|
||||||
|
fun SceneNode.spotLight(builder: SpotLight.() -> Unit): SpotLight {
|
||||||
|
val spotLight = SpotLight()
|
||||||
|
spotLight.builder()
|
||||||
|
this.entities.add(spotLight)
|
||||||
|
return spotLight
|
||||||
|
}
|
||||||
|
|
||||||
|
class SimpleMeshBuilder {
|
||||||
|
var vertexBuffer: VertexBuffer? = null
|
||||||
|
var primitive = DrawPrimitive.TRIANGLES
|
||||||
|
var material: Material? = null
|
||||||
|
fun build(): Mesh {
|
||||||
|
val geometry = Geometry(
|
||||||
|
listOf(vertexBuffer ?: error("no vertex buffer")),
|
||||||
|
null,
|
||||||
|
primitive,
|
||||||
|
0,
|
||||||
|
vertexBuffer?.vertexCount ?: error("no vertex buffer")
|
||||||
|
)
|
||||||
|
val primitive = MeshPrimitive(geometry, material ?: error("no material"))
|
||||||
|
return Mesh(listOf(primitive))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Scene.update(function: ()->Unit) {
|
||||||
|
dispatcher.launch {
|
||||||
|
while (true) {
|
||||||
|
function()
|
||||||
|
yield()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun SceneNode.simpleMesh(builder: SimpleMeshBuilder.() -> Unit): Mesh {
|
||||||
|
val mesh = SimpleMeshBuilder().apply { builder() }.build()
|
||||||
|
entities.add(mesh)
|
||||||
|
return mesh
|
||||||
|
}
|
||||||
@@ -33,7 +33,7 @@ data class SkyboxMaterial(val cubemap: Cubemap, val intensity: Double = 0.0) : M
|
|||||||
|
|
||||||
""".trimIndent()
|
""".trimIndent()
|
||||||
fragmentTransform = """
|
fragmentTransform = """
|
||||||
f_diffuse = texture(p_skybox, va_position);
|
f_diffuse = texture(p_skybox, va_position);
|
||||||
f_diffuse.rgb *= p_intensity;
|
f_diffuse.rgb *= p_intensity;
|
||||||
""" + combinerFS
|
""" + combinerFS
|
||||||
|
|
||||||
@@ -71,8 +71,8 @@ data class SkyboxMaterial(val cubemap: Cubemap, val intensity: Double = 0.0) : M
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Scene.addSkybox(cubemapUrl: String, size: Double = 100.0, intensity:Double = 1.0) {
|
fun Scene.addSkybox(cubemapUrl: String, size: Double = 100.0, intensity: Double = 1.0) {
|
||||||
val cubemap = Cubemap.fromUrl(cubemapUrl, Session.active)
|
val cubemap = Cubemap.fromUrl(cubemapUrl, Session.active).apply { generateMipmaps() }
|
||||||
val box = boxMesh(size, size, size, 1, 1, 1, true)
|
val box = boxMesh(size, size, size, 1, 1, 1, true)
|
||||||
val node = SceneNode()
|
val node = SceneNode()
|
||||||
val material = SkyboxMaterial(cubemap, intensity)
|
val material = SkyboxMaterial(cubemap, intensity)
|
||||||
|
|||||||
Reference in New Issue
Block a user