[orx-jvm] Move panel, gui, dnk3, keyframer, triangulation to orx-jvm
This commit is contained in:
98
orx-jvm/orx-dnk3/src/main/kotlin/tools/MeshCollapse.kt
Normal file
98
orx-jvm/orx-dnk3/src/main/kotlin/tools/MeshCollapse.kt
Normal file
@@ -0,0 +1,98 @@
|
||||
package org.openrndr.extra.dnk3.tools
|
||||
|
||||
import org.openrndr.draw.*
|
||||
import org.openrndr.extra.dnk3.Geometry
|
||||
import org.openrndr.extra.dnk3.Mesh
|
||||
import org.openrndr.extra.dnk3.MeshPrimitive
|
||||
import org.openrndr.extra.dnk3.PBRMaterial
|
||||
import java.nio.ByteBuffer
|
||||
import java.nio.ByteOrder
|
||||
|
||||
|
||||
private data class CollapseItem(val vertexFormats: List<VertexFormat>,
|
||||
val drawPrimitive: DrawPrimitive,
|
||||
val hasIndexBuffer: Boolean)
|
||||
|
||||
fun Mesh.collapse() {
|
||||
val grouped = primitives.groupBy {
|
||||
CollapseItem(it.geometry.vertexBuffers.map { it.vertexFormat }, it.geometry.primitive, it.geometry.indexBuffer != null)
|
||||
}
|
||||
|
||||
grouped.map {
|
||||
val vertexCount = it.value.sumBy { primitive ->
|
||||
primitive.geometry.vertexCount
|
||||
}
|
||||
|
||||
val indexCount = if (it.key.hasIndexBuffer)
|
||||
it.value.sumBy { primitive ->
|
||||
primitive.geometry.indexBuffer?.indexCount ?: 0
|
||||
}
|
||||
else 0
|
||||
|
||||
val collapsedVertices = it.key.vertexFormats.map {
|
||||
vertexBuffer(it, vertexCount)
|
||||
} + vertexBuffer(vertexFormat { attribute("fragmentID", VertexElementType.INT16) }, vertexCount)
|
||||
|
||||
|
||||
val fragmentBuffer = ByteBuffer.allocateDirect(vertexCount * 2)
|
||||
fragmentBuffer.order(ByteOrder.nativeOrder())
|
||||
|
||||
for (i in 0 until collapsedVertices.size) {
|
||||
var offset = 0
|
||||
for (fromPrimitive in it.value) {
|
||||
val fromBuffer = fromPrimitive.geometry.vertexBuffers[i]
|
||||
|
||||
val copy = ByteBuffer.allocateDirect(fromBuffer.vertexCount * fromBuffer.vertexFormat.size)
|
||||
copy.order(ByteOrder.nativeOrder())
|
||||
fromBuffer.read(copy)
|
||||
copy.rewind()
|
||||
|
||||
collapsedVertices[i].write(copy, offset)
|
||||
offset += copy.capacity()
|
||||
|
||||
for (v in 0 until fromBuffer.vertexCount) {
|
||||
fragmentBuffer.putShort(fromPrimitive.material.fragmentID.toShort())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val collapsedIndices = if (it.key.hasIndexBuffer) indexBuffer(indexCount, IndexType.INT32) else null
|
||||
|
||||
if (it.key.hasIndexBuffer) {
|
||||
var offset = 0
|
||||
val result = ByteBuffer.allocateDirect(4 * indexCount)
|
||||
result.order(ByteOrder.nativeOrder())
|
||||
|
||||
for (fromPrimitive in it.value) {
|
||||
val fromBuffer = fromPrimitive.geometry.indexBuffer!!
|
||||
when (fromBuffer.type) {
|
||||
IndexType.INT16 -> {
|
||||
val copy = ByteBuffer.allocateDirect(fromBuffer.indexCount * 2)
|
||||
fromBuffer.read(copy)
|
||||
copy.rewind()
|
||||
for (i in 0 until fromBuffer.indexCount) {
|
||||
val index = (copy.getShort().toInt() and 0xffff) + offset
|
||||
result.putInt(index)
|
||||
}
|
||||
}
|
||||
IndexType.INT32 -> {
|
||||
val copy = ByteBuffer.allocateDirect(fromBuffer.indexCount * 4)
|
||||
fromBuffer.read(copy)
|
||||
copy.rewind()
|
||||
for (i in 0 until fromBuffer.indexCount) {
|
||||
val index = copy.getInt() + offset
|
||||
result.putInt(index)
|
||||
}
|
||||
}
|
||||
}
|
||||
offset += fromPrimitive.geometry.vertexCount
|
||||
}
|
||||
}
|
||||
|
||||
val collapsedGeometry = Geometry(collapsedVertices, collapsedIndices, it.key.drawPrimitive, 0, if (collapsedIndices == null)
|
||||
vertexCount else indexCount
|
||||
)
|
||||
|
||||
MeshPrimitive(collapsedGeometry, PBRMaterial())
|
||||
}
|
||||
}
|
||||
84
orx-jvm/orx-dnk3/src/main/kotlin/tools/Skybox.kt
Normal file
84
orx-jvm/orx-dnk3/src/main/kotlin/tools/Skybox.kt
Normal file
@@ -0,0 +1,84 @@
|
||||
package org.openrndr.extra.dnk3.tools
|
||||
|
||||
import org.openrndr.draw.*
|
||||
import org.openrndr.extra.dnk3.*
|
||||
import org.openrndr.extras.meshgenerators.boxMesh
|
||||
|
||||
|
||||
data class SkyboxMaterial(val cubemap: Cubemap, val intensity: Double = 0.0) : Material {
|
||||
override val name: String = "skybox"
|
||||
override var doubleSided: Boolean = false
|
||||
override var transparent: Boolean = false
|
||||
override val fragmentID: Int = 0
|
||||
|
||||
override fun generateShadeStyle(materialContext: MaterialContext, primitiveContext: PrimitiveContext): ShadeStyle {
|
||||
return shadeStyle {
|
||||
vertexTransform = """
|
||||
vec2 i = vec2(1.0, 0.0);
|
||||
x_viewMatrix = x_viewNormalMatrix;
|
||||
""".trimIndent()
|
||||
|
||||
val combinerFS = materialContext.pass.combiners.map {
|
||||
it.generateShader()
|
||||
}.joinToString("\n")
|
||||
|
||||
fragmentPreamble = """
|
||||
vec4 f_diffuse = vec4(0.0, 0.0, 0.0, 1.0);
|
||||
vec3 f_specular = vec3(0.0);
|
||||
vec3 f_ambient = vec3(0.0);
|
||||
vec3 f_emission = vec3(0.0);
|
||||
int f_fragmentID = 0;
|
||||
vec4 m_color = vec4(1.0);
|
||||
vec4 f_fog = vec4(0.0);
|
||||
|
||||
""".trimIndent()
|
||||
fragmentTransform = """
|
||||
f_diffuse = texture(p_skybox, va_position);
|
||||
f_diffuse.rgb *= p_intensity;
|
||||
""" + combinerFS
|
||||
|
||||
suppressDefaultOutput = true
|
||||
val rt = RenderTarget.active
|
||||
materialContext.pass.combiners.map {
|
||||
if (rt is ProgramRenderTarget || materialContext.pass === DefaultPass || materialContext.pass === DefaultOpaquePass || materialContext.pass == DefaultTransparentPass || materialContext.pass == IrradianceProbePass) {
|
||||
this.output(it.targetOutput, ShadeStyleOutput(0))
|
||||
} else {
|
||||
val index = rt.colorAttachmentIndexByName(it.targetOutput)
|
||||
?: error("attachment ${it.targetOutput} not found")
|
||||
val type = rt.colorBuffer(index).type
|
||||
val format = rt.colorBuffer(index).format
|
||||
this.output(it.targetOutput, ShadeStyleOutput(index, format, type))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun applyToShadeStyle(context: MaterialContext, shadeStyle: ShadeStyle) {
|
||||
shadeStyle.parameter("skybox", cubemap)
|
||||
shadeStyle.parameter("intensity", intensity)
|
||||
}
|
||||
|
||||
|
||||
override fun hashCode(): Int {
|
||||
var result = intensity.hashCode()
|
||||
result = 31 * result + name.hashCode()
|
||||
result = 31 * result + doubleSided.hashCode()
|
||||
result = 31 * result + transparent.hashCode()
|
||||
result = 31 * result + fragmentID
|
||||
return result
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
fun Scene.addSkybox(cubemapUrl: String, size: Double = 100.0, intensity: Double = 1.0) {
|
||||
val cubemap = Cubemap.fromUrl(cubemapUrl, null, Session.active).apply { generateMipmaps() }
|
||||
val box = boxMesh(size, size, size, 1, 1, 1, true)
|
||||
val node = SceneNode()
|
||||
val material = SkyboxMaterial(cubemap, intensity)
|
||||
val geometry = Geometry(listOf(box), null, DrawPrimitive.TRIANGLES, 0, box.vertexCount)
|
||||
val primitive = MeshPrimitive(geometry, material)
|
||||
val mesh = Mesh(listOf(primitive))
|
||||
node.entities.add(mesh)
|
||||
root.children.add(node)
|
||||
}
|
||||
Reference in New Issue
Block a user