# HG changeset patch # User Sam # Date 1693933431 -25200 # Node ID e5aca33dea19626cae904db5bd6c6aacbd16ba77 # Parent 677f3b5a29433a74cbe8612e12ff6bbdabb01be8 fix: a few assert, comments, names; add: collider from pointlist, fix: gltf import diff -r 677f3b5a2943 -r e5aca33dea19 src/semicongine/animation.nim --- a/src/semicongine/animation.nim Tue Sep 05 00:44:33 2023 +0700 +++ b/src/semicongine/animation.nim Wed Sep 06 00:03:51 2023 +0700 @@ -123,7 +123,7 @@ player.currentDirection = if player.animation.direction == Backward: -1 else : 1 player.currentIteration = player.animation.iterations -func newAnimator*[T](animation: Animation[T]): AnimationPlayer[T] = +func newAnimationPlayer*[T](animation: Animation[T]): AnimationPlayer[T] = result = AnimationPlayer[T]( animation: animation, playing: false,) result.resetPlayer() @@ -134,7 +134,7 @@ player.playing = false func advance*[T](player: var AnimationPlayer[T], dt: float32) = - # TODO: check, not 100% correct I think + # TODO: check this function, not 100% correct I think if player.playing: player.currentTime += float32(player.currentDirection) * dt if abs(player.currentTime) > player.animation.duration: diff -r 677f3b5a2943 -r e5aca33dea19 src/semicongine/collision.nim --- a/src/semicongine/collision.nim Tue Sep 05 00:44:33 2023 +0700 +++ b/src/semicongine/collision.nim Wed Sep 06 00:03:51 2023 +0700 @@ -5,12 +5,13 @@ type ColliderType* = enum - Box, Sphere + Box, Sphere, Points Collider* = object transform*: Mat4 = Unit4F32 case theType*: ColliderType of Box: discard of Sphere: radius*: float32 + of Points: points*: seq[Vec3f] func between(value, b1, b2: float32): bool = min(b1, b2) <= value and value <= max(b1, b2) @@ -39,6 +40,8 @@ ux.between(uP1, uP2) and vx.between(vP1, vP4) and wx.between(wP1, wP5) of Sphere: (collider.transform * x).length < (collider.transform * newVec3f()).length + of Points: + raise newException(Exception, "Points are not supported yet for 'contains'") # implementation of GJK, based on https://blog.winter.dev/2020/gjk-algorithm/ @@ -69,11 +72,13 @@ ) func findFurthestPoint(collider: Collider, direction: Vec3f): Vec3f = case collider.theType - of Sphere: - let directionNormalizedToSphere = ((direction / direction.length) * collider.radius) - collider.transform * directionNormalizedToSphere - of Box: - findFurthestPoint(collider.transform, direction) + of Sphere: + let directionNormalizedToSphere = ((direction / direction.length) * collider.radius) + collider.transform * directionNormalizedToSphere + of Box: + findFurthestPoint(collider.transform, direction) + of Points: + findFurthestPoint(collider.points, direction) func supportPoint(a, b: Collider, direction: Vec3f): Vec3f = a.findFurthestPoint(direction) - b.findFurthestPoint(-direction) @@ -390,9 +395,12 @@ scaleY = (maxY - minY) scaleZ = (maxZ - minZ) - result = Collider(theType: theType, transform: translate(minX, minY, minZ) * scale(scaleX, scaleY, scaleZ)) + if theType == Points: + result = Collider(theType: Points, points: points) + else: + result = Collider(theType: theType, transform: translate(minX, minY, minZ) * scale(scaleX, scaleY, scaleZ)) - if theType == Sphere: - result.transform = translate(center) - for p in points: - result.radius = max(result.radius, (p - center).length) + if theType == Sphere: + result.transform = translate(center) + for p in points: + result.radius = max(result.radius, (p - center).length) diff -r 677f3b5a2943 -r e5aca33dea19 src/semicongine/mesh.nim --- a/src/semicongine/mesh.nim Tue Sep 05 00:44:33 2023 +0700 +++ b/src/semicongine/mesh.nim Wed Sep 06 00:03:51 2023 +0700 @@ -90,7 +90,7 @@ ) * 3 func initVertexAttribute*[T](mesh: var MeshObject, attribute: string, value: seq[T]) = - assert not mesh.vertexData.contains(attribute) + assert not mesh.vertexData.contains(attribute) and not mesh.instanceData.contains(attribute) mesh.vertexData[attribute] = newDataList(thetype=getDataType[T]()) mesh.vertexData[attribute].initData(mesh.vertexCount) mesh.vertexData[attribute].setValues(value) @@ -99,12 +99,12 @@ func initVertexAttribute*[T](mesh: var MeshObject, attribute: string) = initVertexAttribute(mesh=mesh, attribute=attribute, value=default(T)) func initVertexAttribute*(mesh: var MeshObject, attribute: string, datatype: DataType) = - assert not mesh.vertexData.contains(attribute) + assert not mesh.vertexData.contains(attribute) and not mesh.instanceData.contains(attribute) mesh.vertexData[attribute] = newDataList(thetype=datatype) mesh.vertexData[attribute].initData(mesh.vertexCount) func initInstanceAttribute*[T](mesh: var MeshObject, attribute: string, value: seq[T]) = - assert not mesh.instanceData.contains(attribute) + assert not mesh.vertexData.contains(attribute) and not mesh.instanceData.contains(attribute) mesh.instanceData[attribute] = newDataList(thetype=getDataType[T]()) mesh.instanceData[attribute].initData(mesh.instanceCount) mesh.instanceData[attribute].setValues(value) @@ -113,7 +113,7 @@ func initInstanceAttribute*[T](mesh: var MeshObject, attribute: string) = initInstanceAttribute(mesh=mesh, attribute=attribute, value=default(T)) func initInstanceAttribute*(mesh: var MeshObject, attribute: string, datatype: DataType) = - assert not mesh.instanceData.contains(attribute) + assert not mesh.vertexData.contains(attribute) and not mesh.instanceData.contains(attribute) mesh.instanceData[attribute] = newDataList(thetype=datatype) mesh.instanceData[attribute].initData(mesh.instanceCount) @@ -373,6 +373,18 @@ transform*: Mat4 = Unit4F32 children*: seq[MeshTree] +func toStringRec*(tree: MeshTree, theindent=0): seq[string] = + if tree.mesh.isNil: + result.add "*" + else: + result.add indent($tree.mesh, theindent) + for child in tree.children: + result.add child.toStringRec(theindent + 4) + +func `$`*(tree: MeshTree): string = + toStringRec(tree).join("\n") + + proc toSeq*(tree: MeshTree): seq[Mesh] = var queue = @[tree] while queue.len > 0: diff -r 677f3b5a2943 -r e5aca33dea19 src/semicongine/resources/mesh.nim --- a/src/semicongine/resources/mesh.nim Tue Sep 05 00:44:33 2023 +0700 +++ b/src/semicongine/resources/mesh.nim Wed Sep 06 00:03:51 2023 +0700 @@ -215,6 +215,7 @@ mesh[].appendAttributeData(attribute.toLowerAscii, data) assert data.len == vertexCount or vertexCount == 0 vertexCount = data.len + mesh.vertexCount += vertexCount var materialId = 0'u16 if primitiveNode.hasKey("material"):