[orx-dnk3] Added DSL

This commit is contained in:
Edwin Jakobs
2020-07-26 15:33:30 +02:00
parent 6c4d130c85
commit 01991dc015
4 changed files with 181 additions and 3 deletions

View 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)
}
}
}

View 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 {
}
}

View 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
}

View File

@@ -33,7 +33,7 @@ data class SkyboxMaterial(val cubemap: Cubemap, val intensity: Double = 0.0) : M
""".trimIndent()
fragmentTransform = """
f_diffuse = texture(p_skybox, va_position);
f_diffuse = texture(p_skybox, va_position);
f_diffuse.rgb *= p_intensity;
""" + 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) {
val cubemap = Cubemap.fromUrl(cubemapUrl, Session.active)
fun Scene.addSkybox(cubemapUrl: String, size: Double = 100.0, intensity: Double = 1.0) {
val cubemap = Cubemap.fromUrl(cubemapUrl, Session.active).apply { generateMipmaps() }
val box = boxMesh(size, size, size, 1, 1, 1, true)
val node = SceneNode()
val material = SkyboxMaterial(cubemap, intensity)