Mercurial > games > semicongine
changeset 839:6f4cee1701bc
del: default material makes no sense, fix: gltf import orientation
author | Sam <sam@basx.dev> |
---|---|
date | Sun, 26 Nov 2023 23:09:19 +0700 |
parents | 970f74d5284e |
children | 44ec744fbedc d02a66171917 |
files | src/semicongine/material.nim src/semicongine/mesh.nim src/semicongine/resources/mesh.nim |
diffstat | 3 files changed, 29 insertions(+), 25 deletions(-) [+] |
line wrap: on
line diff
--- a/src/semicongine/material.nim Sun Nov 26 21:26:46 2023 +0700 +++ b/src/semicongine/material.nim Sun Nov 26 23:09:19 2023 +0700 @@ -101,14 +101,10 @@ proc initMaterialData*( materialType: MaterialType, name: string = "", - attributes: seq[(string, DataList)] = @[], + attributes: openArray[(string, DataList)] = @[], ): MaterialData = var theName = name if theName == "": theName = &"material instance of '{materialType}'" initMaterialData(materialType=materialType, name=theName, attributes=attributes.toTable) -let DEFAULT_MATERIAL* = MaterialData( - name: "default material" -) -
--- a/src/semicongine/mesh.nim Sun Nov 26 21:26:46 2023 +0700 +++ b/src/semicongine/mesh.nim Sun Nov 26 23:09:19 2023 +0700 @@ -11,6 +11,8 @@ import ./collision import ./material +const DEFAULT_POSITION_ATTRIBUTE = "position" + var instanceCounter* = 0 type @@ -132,7 +134,7 @@ uvs: openArray[Vec2f]=[], transform: Mat4=Unit4F32, instanceTransforms: openArray[Mat4]=[Unit4F32], - material: MaterialData=DEFAULT_MATERIAL, + material=EMPTY_MATERIAL.initMaterialData(), autoResize=true, name: string="" ): Mesh = @@ -158,14 +160,13 @@ vertexCount: positions.len, instanceTransforms: @instanceTransforms, transform: transform, + material: material ) - result[].initVertexAttribute("position", positions.toSeq) + result[].initVertexAttribute(DEFAULT_POSITION_ATTRIBUTE , positions.toSeq) if colors.len > 0: result[].initVertexAttribute("color", colors.toSeq) if uvs.len > 0: result[].initVertexAttribute("uv", uvs.toSeq) - `material=`(result[], material) - # assert all indices are valid for i in indices: assert int(i[0]) < result[].vertexCount @@ -189,7 +190,7 @@ uvs: openArray[Vec2f]=[], transform: Mat4=Unit4F32, instanceTransforms: openArray[Mat4]=[Unit4F32], - material: MaterialData=DEFAULT_MATERIAL, + material=EMPTY_MATERIAL.initMaterialData(), name: string="", ): Mesh = newMesh( @@ -378,11 +379,17 @@ else: raise newException(Exception, &"Attribute {attribute} is not defined for mesh {mesh}") -func getCollisionPoints*(mesh: MeshObject, positionAttribute="position"): seq[Vec3f] = +proc applyTransformToVertices*(mesh: var MeshObject, positionAttribute=DEFAULT_POSITION_ATTRIBUTE) = + for i in 0 ..< mesh.vertexData[positionAttribute].len: + setValue(mesh.vertexData[positionAttribute], i, mesh.transform * getValue[Vec3f](mesh.vertexData[positionAttribute], i)) + mesh.transform = Unit4 + + +func getCollisionPoints*(mesh: MeshObject, positionAttribute=DEFAULT_POSITION_ATTRIBUTE): seq[Vec3f] = for p in mesh[positionAttribute, Vec3f][]: result.add mesh.transform * p -func getCollider*(mesh: MeshObject, positionAttribute="position"): Collider = +func getCollider*(mesh: MeshObject, positionAttribute=DEFAULT_POSITION_ATTRIBUTE): Collider = return mesh.getCollisionPoints(positionAttribute).calculateCollider(Points) proc asNonIndexedMesh*(mesh: MeshObject): MeshObject = @@ -431,15 +438,15 @@ # GENERATORS ============================================================================ -proc rect*(width=1'f32, height=1'f32, color="ffffffff"): Mesh = +proc rect*(width=1'f32, height=1'f32, color="ffffffff", material=EMPTY_MATERIAL.initMaterialData()): Mesh = result = Mesh( vertexCount: 4, instanceTransforms: @[Unit4F32], indexType: Small, smallIndices: @[[0'u16, 1'u16, 2'u16], [2'u16, 3'u16, 0'u16]], name: &"rect-{instanceCounter}", + material: material, ) - `material=`(result[], DEFAULT_MATERIAL) inc instanceCounter let @@ -448,35 +455,35 @@ pos = @[newVec3f(-half_w, -half_h), newVec3f( half_w, -half_h), newVec3f( half_w, half_h), newVec3f(-half_w, half_h)] c = toRGBA(color) - result[].initVertexAttribute("position", pos) + result[].initVertexAttribute(DEFAULT_POSITION_ATTRIBUTE, pos) result[].initVertexAttribute("color", @[c, c, c, c]) result[].initVertexAttribute("uv", @[newVec2f(0, 0), newVec2f(1, 0), newVec2f(1, 1), newVec2f(0, 1)]) -proc tri*(width=1'f32, height=1'f32, color="ffffffff"): Mesh = +proc tri*(width=1'f32, height=1'f32, color="ffffffff", material=EMPTY_MATERIAL.initMaterialData()): Mesh = result = Mesh( vertexCount: 3, instanceTransforms: @[Unit4F32], name: &"tri-{instanceCounter}", + material: material, ) - `material=`(result[], DEFAULT_MATERIAL) inc instanceCounter let half_w = width / 2 half_h = height / 2 colorVec = toRGBA(color) - result[].initVertexAttribute("position", @[newVec3f(0, -half_h), newVec3f( half_w, half_h), newVec3f(-half_w, half_h)]) + result[].initVertexAttribute(DEFAULT_POSITION_ATTRIBUTE, @[newVec3f(0, -half_h), newVec3f( half_w, half_h), newVec3f(-half_w, half_h)]) result[].initVertexAttribute("color", @[colorVec, colorVec, colorVec]) -proc circle*(width=1'f32, height=1'f32, nSegments=12, color="ffffffff"): Mesh = +proc circle*(width=1'f32, height=1'f32, nSegments=12, color="ffffffff", material=EMPTY_MATERIAL.initMaterialData()): Mesh = assert nSegments >= 3 result = Mesh( vertexCount: 3 + nSegments, instanceTransforms: @[Unit4F32], indexType: Small, name: &"circle-{instanceCounter}", + material: material, ) - `material=`(result[], DEFAULT_MATERIAL) inc instanceCounter let @@ -492,7 +499,7 @@ col.add c result[].smallIndices.add [uint16(0), uint16(i + 1), uint16(i + 2)] - result[].initVertexAttribute("position", pos) + result[].initVertexAttribute(DEFAULT_POSITION_ATTRIBUTE, pos) result[].initVertexAttribute("color", col) # MESH TREES =============================================================================
--- a/src/semicongine/resources/mesh.nim Sun Nov 26 21:26:46 2023 +0700 +++ b/src/semicongine/resources/mesh.nim Sun Nov 26 23:09:19 2023 +0700 @@ -258,7 +258,7 @@ let materialId = primitiveNode["material"].getInt() result[].material = loadMaterial(root, root["materials"][materialId], defaultMaterial, mainBuffer) else: - result[].material = DEFAULT_MATERIAL + result[].material = EMPTY_MATERIAL.initMaterialData() if primitiveNode.hasKey("indices"): assert result[].indexType != None @@ -282,7 +282,7 @@ else: raise newException(Exception, &"Unsupported index data type: {data.thetype}") # TODO: getting from gltf to vulkan system is still messed up somehow, see other TODO - # transform[Vec3f](result[], "position", scale(1, -1, 1)) + transform[Vec3f](result[], "position", scale(1, -1, 1)) proc loadNode(root: JsonNode, node: JsonNode, defaultMaterial: MaterialType, mainBuffer: var seq[uint8]): MeshTree = result = MeshTree() @@ -321,7 +321,8 @@ float32(node["scale"][1].getFloat()), float32(node["scale"][2].getFloat()) ) - result.transform = t * r * s + result.transform = t * r * s + result.transform = scale(1, -1, 1) * result.transform # children if node.hasKey("children"): @@ -333,7 +334,7 @@ for nodeId in scenenode["nodes"]: result.children.add loadNode(root, root["nodes"][nodeId.getInt()], defaultMaterial, mainBuffer) # TODO: getting from gltf to vulkan system is still messed up somehow (i.e. not consistent for different files), see other TODO - result.transform = scale(1, -1, 1) + # result.transform = scale(1, -1, 1) result.updateTransforms()