Skip to content

Commit

Permalink
fixed non-default editor model materials
Browse files Browse the repository at this point in the history
  • Loading branch information
fabmax committed Sep 10, 2024
1 parent 372e9b4 commit bad50ba
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import de.fabmax.kool.scene.animation.*
import de.fabmax.kool.util.*
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import kotlin.math.max
import kotlin.math.min

suspend fun GltfFile(data: Uint8Buffer, filePath: String, assetLoader: AssetLoader = Assets.defaultLoader): Result<GltfFile> {
Expand Down Expand Up @@ -632,7 +631,11 @@ data class GltfFile(
isInstanced = true
}
if (mesh.skin != null) {
enableArmature(max(cfg.materialConfig.fixedNumberOfJoints, mesh.skin.nodes.size))
if (cfg.materialConfig.fixedNumberOfJoints > 0) {
enableArmatureFixedNumberOfBones(cfg.materialConfig.fixedNumberOfJoints)
} else {
enableArmature(mesh.skin.nodes.size)
}
if (cfg.materialConfig.fixedNumberOfJoints > 0 && cfg.materialConfig.fixedNumberOfJoints < mesh.skin.nodes.size) {
logW("GltfFile") { "\"${model.name}\": Number of joints exceeds the material config's fixedNumberOfJoints (mesh has ${mesh.skin.nodes.size}, materialConfig.fixedNumberOfJoints is ${cfg.materialConfig.fixedNumberOfJoints})" }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,13 @@ data class BasicVertexConfig(
val displacementCfg: PropertyBlockConfig.Builder = PropertyBlockConfig.Builder("displacement").apply { constProperty(0f) }
var modelMatrixComposition = listOf<ModelMatrixComposition>()

fun enableArmature(maxNumberOfBones: Int = 32): Builder {
this.maxNumberOfBones = maxNumberOfBones
fun enableArmatureFixedNumberOfBones(fixedNumberOfBones: Int): Builder {
this.maxNumberOfBones = fixedNumberOfBones
return this
}

fun enableArmature(numberOfBones: Int): Builder {
this.maxNumberOfBones = (numberOfBones + 63) and 63.inv()
return this
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@ open class DepthMapPass(
)

companion object {
var defaultMaxNumberOfJoints = 16

val defaultRenderPassAttachmentConfig = AttachmentConfig(
ColorAttachmentNone,
DepthAttachmentTexture()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import de.fabmax.kool.modules.ksl.blocks.vertexTransformBlock
import de.fabmax.kool.modules.ksl.lang.*
import de.fabmax.kool.pipeline.*
import de.fabmax.kool.scene.Mesh
import kotlin.math.max

fun depthShader(cfgBlock: DepthShader.Config.() -> Unit): DepthShader {
val cfg = DepthShader.Config().apply(cfgBlock)
Expand Down Expand Up @@ -110,7 +109,7 @@ open class DepthShader(val cfg: Config) : KslShader(depthShaderProg(cfg), cfg.pi
vertices {
isInstanced = mesh.instances != null
mesh.skin?.let {
enableArmature(max(DepthMapPass.defaultMaxNumberOfJoints, it.nodes.size))
enableArmature(it.nodes.size)
}
morphAttributes += mesh.geometry.getMorphAttributes()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,31 +91,34 @@ class SceneShaderData(val scene: EditorScene, private val isNotifying: Boolean =
}

class SceneShaderCache : EditorScene.SceneShaderDataListener {
private val materialShaders = mutableMapOf<EntityId, MutableMap<MeshLayoutKey, DrawShader>>()
private val materialShaders = mutableMapOf<EntityId, MutableMap<MeshLayoutInfo, DrawShader>>()

override fun onSceneShaderDataChanged(scene: EditorScene, sceneShaderData: SceneShaderData) {
materialShaders.keys.forEach { matId ->
scene.project.materialsById[matId]?.onSceneShaderDataChanged(scene, sceneShaderData)
}
}

fun getShaderCache(material: MaterialComponent): MutableMap<MeshLayoutKey, DrawShader>? {
fun getShaderCache(material: MaterialComponent): MutableMap<MeshLayoutInfo, DrawShader>? {
return materialShaders[material.id]
}

fun getOrPutShaderCache(material: MaterialComponent): MutableMap<MeshLayoutKey, DrawShader> {
fun getOrPutShaderCache(material: MaterialComponent): MutableMap<MeshLayoutInfo, DrawShader> {
return materialShaders.getOrPut(material.id) { mutableMapOf() }
}
}

fun MeshLayoutKey(mesh: Mesh): MeshLayoutKey = MeshLayoutKey(
fun MeshLayoutInfo(mesh: Mesh): MeshLayoutInfo = MeshLayoutInfo(
vertexLayout = mesh.geometry.vertexAttributes,
instanceLayout = mesh.instances?.instanceAttributes ?: emptyList(),
primitiveType = mesh.geometry.primitiveType
primitiveType = mesh.geometry.primitiveType,
numJoints = ((mesh.skin?.nodes?.size ?: 0) + 63) and 63.inv()
//todo: morphWeights = mesh.geometry.getMorphAttributes()
)

data class MeshLayoutKey(
data class MeshLayoutInfo(
val vertexLayout: List<Attribute>,
val instanceLayout: List<Attribute>,
val primitiveType: PrimitiveType
val primitiveType: PrimitiveType,
val numJoints: Int
)
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ class MaterialComponent(
suspend fun applyMaterialTo(mesh: Mesh, sceneShaderData: SceneShaderData, modelMats: List<ModelMatrixComposition>): Boolean {
mesh.isCastingShadow = shaderData.genericSettings.isCastingShadow

val meshKey = MeshLayoutKey(mesh)
val meshKey = MeshLayoutInfo(mesh)
val shader = sceneShaderData.shaderCache.getOrPutShaderCache(this).getOrPut(meshKey) {
logT { "Creating new material shader $name (for mesh: ${mesh.name})" }
data.createShader(sceneShaderData, modelMats)
data.createShader(meshKey, modelMats, sceneShaderData)
}
if (mesh.shader == shader) {
return true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package de.fabmax.kool.editor.components

import de.fabmax.kool.editor.api.MeshLayoutInfo
import de.fabmax.kool.editor.api.SceneShaderData
import de.fabmax.kool.editor.data.*
import de.fabmax.kool.modules.ksl.KslPbrShader
import de.fabmax.kool.modules.ksl.KslPbrSplatShader
import de.fabmax.kool.modules.ksl.ModelMatrixComposition
import de.fabmax.kool.pipeline.DrawShader

suspend fun MaterialComponentData.createShader(sceneShaderData: SceneShaderData, modelMats: List<ModelMatrixComposition>): DrawShader {
return shaderData.createShader(sceneShaderData, modelMats)
suspend fun MaterialComponentData.createShader(
meshLayoutInfo: MeshLayoutInfo,
modelMats: List<ModelMatrixComposition>,
sceneShaderData: SceneShaderData,
): DrawShader {
return shaderData.createShader(meshLayoutInfo, modelMats, sceneShaderData)
}

suspend fun MaterialComponentData.updateShader(shader: DrawShader?, sceneShaderData: SceneShaderData): Boolean {
Expand All @@ -17,11 +22,11 @@ suspend fun MaterialComponentData.updateShader(shader: DrawShader?, sceneShaderD

fun MaterialComponentData.matchesShader(shader: DrawShader?): Boolean = shaderData.matchesShader(shader)

suspend fun MaterialShaderData.createShader(sceneShaderData: SceneShaderData, modelMats: List<ModelMatrixComposition>): DrawShader = when (this) {
is PbrShaderData -> createPbrShader(sceneShaderData, modelMats)
suspend fun MaterialShaderData.createShader(meshLayoutInfo: MeshLayoutInfo, modelMats: List<ModelMatrixComposition>, sceneShaderData: SceneShaderData): DrawShader = when (this) {
is PbrShaderData -> createPbrShader(meshLayoutInfo, modelMats, sceneShaderData)
is BlinnPhongShaderData -> TODO()
is UnlitShaderData -> TODO()
is PbrSplatShaderData -> createPbrSplatShader(sceneShaderData, modelMats)
is PbrSplatShaderData -> createPbrSplatShader(meshLayoutInfo, modelMats, sceneShaderData)
}

suspend fun MaterialShaderData.updateShader(shader: DrawShader?, sceneShaderData: SceneShaderData): Boolean = when (this) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package de.fabmax.kool.editor.components

import de.fabmax.kool.editor.api.AppAssets
import de.fabmax.kool.editor.api.AssetReference
import de.fabmax.kool.editor.api.SceneShaderData
import de.fabmax.kool.editor.api.loadTexture2dOrNull
import de.fabmax.kool.editor.api.*
import de.fabmax.kool.editor.data.*
import de.fabmax.kool.modules.ksl.KslLitShader
import de.fabmax.kool.modules.ksl.KslPbrShader
Expand All @@ -13,7 +10,11 @@ import de.fabmax.kool.pipeline.*
import de.fabmax.kool.util.Color
import de.fabmax.kool.util.logW

suspend fun PbrShaderData.createPbrShader(sceneShaderData: SceneShaderData, modelMats: List<ModelMatrixComposition>): KslPbrShader {
suspend fun PbrShaderData.createPbrShader(
meshLayoutInfo: MeshLayoutInfo,
modelMats: List<ModelMatrixComposition>,
sceneShaderData: SceneShaderData
): KslPbrShader {
val shader = KslPbrShader {
pipeline {
if (genericSettings.isTwoSided) {
Expand All @@ -23,6 +24,9 @@ suspend fun PbrShaderData.createPbrShader(sceneShaderData: SceneShaderData, mode
vertices {
isInstanced = true
modelMatrixComposition = modelMats
if (meshLayoutInfo.numJoints > 0) {
enableArmature(meshLayoutInfo.numJoints)
}
}
color {
when (val color = baseColor) {
Expand Down Expand Up @@ -61,7 +65,6 @@ suspend fun PbrShaderData.createPbrShader(sceneShaderData: SceneShaderData, mode
aoMap?.let {
ao { textureProperty(channel = it.singleChannelIndex, textureName = armTexNames.ao) }
}

displacementMap?.let {
vertices {
displacement {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package de.fabmax.kool.editor.components

import de.fabmax.kool.editor.api.AppAssets
import de.fabmax.kool.editor.api.AssetReference
import de.fabmax.kool.editor.api.SceneShaderData
import de.fabmax.kool.editor.api.loadTexture2dOrNull
import de.fabmax.kool.editor.api.*
import de.fabmax.kool.editor.data.*
import de.fabmax.kool.math.deg
import de.fabmax.kool.modules.ksl.KslLitShader
Expand All @@ -15,7 +12,11 @@ import de.fabmax.kool.util.Color
import de.fabmax.kool.util.logE
import de.fabmax.kool.util.logW

suspend fun PbrSplatShaderData.createPbrSplatShader(sceneShaderData: SceneShaderData, modelMats: List<ModelMatrixComposition>): KslPbrSplatShader {
suspend fun PbrSplatShaderData.createPbrSplatShader(
meshLayoutInfo: MeshLayoutInfo,
modelMats: List<ModelMatrixComposition>,
sceneShaderData: SceneShaderData
): KslPbrSplatShader {
val shader = KslPbrSplatShader {
pipeline {
if (genericSettings.isTwoSided) {
Expand All @@ -25,6 +26,9 @@ suspend fun PbrSplatShaderData.createPbrSplatShader(sceneShaderData: SceneShader
vertices {
isInstanced = true
modelMatrixComposition = modelMats
if (meshLayoutInfo.numJoints > 0) {
enableArmature(meshLayoutInfo.numJoints)
}
}
lighting {
maxNumberOfLights = sceneShaderData.maxNumberOfLights
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class SelectionOverlay(val overlay: OverlayScene) : Node("Selection overlay"), E
meshComponent.sceneNode?.let { sceneNode ->
val meshSelection = when (sceneNode) {
is Mesh -> listOf(selectedMeshes.getOrPut(sceneNode.id) { SelectedMeshes(sceneNode.id) })
is Model -> sceneNode.meshes.values.map { selectedMeshes.getOrPut(it.id) { SelectedMeshes(it.id) } }
is Model -> sceneNode.meshes.values.map { selectedMeshes.getOrPut(it.id) { SelectedMeshes(sceneNode.id) } }
else -> emptyList()
}
val selectionType = if (entity in selection) MeshSelectionType.PRIMARY else MeshSelectionType.CHILD
Expand All @@ -161,8 +161,7 @@ class SelectionOverlay(val overlay: OverlayScene) : Node("Selection overlay"), E
}

inner class SelectionPass : OffscreenRenderPass2d(
// drawNode will be replaced by content scene, once it is loaded
Node(),
Node(), // drawNode will be replaced by content scene, once it is loaded
colorAttachmentDefaultDepth(TexFormat.RG),
Vec2i(128),
name = "selection-overlay"
Expand Down

0 comments on commit bad50ba

Please sign in to comment.