changeset 799:7ce4e28c71dd

fix: a few assert, comments, names; add: collider from pointlist, fix: gltf import
author Sam <sam@basx.dev>
date Wed, 06 Sep 2023 00:03:51 +0700
parents f13f748567db
children 668eed376029
files src/semicongine/animation.nim src/semicongine/collision.nim src/semicongine/mesh.nim src/semicongine/resources/mesh.nim
diffstat 4 files changed, 38 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- 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:
--- 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)
--- 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:
--- 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"):