diff tests/test_collision.nim @ 790:a979e5c581de

add: scene/shader compatability check, fix collision code to work with new APIs
author Sam <sam@basx.dev>
date Tue, 29 Aug 2023 00:01:13 +0700
parents e45c2927ecca
children 05fb85ba97dd
line wrap: on
line diff
--- a/tests/test_collision.nim	Fri Aug 25 01:14:04 2023 +0700
+++ b/tests/test_collision.nim	Tue Aug 29 00:01:13 2023 +0700
@@ -1,67 +1,54 @@
+import std/tables
+
 import semicongine
 
-
 proc main() =
-  var scene = newScene("main", root=newEntity("rect"))
+  var scene = Scene(name: "main")
 
-  var obj1 = newEntity("Obj1", {
-    "mesh": Component(rect(color="f00f")),
-    "hitbox": Component(HitBox(transform: translate3d(-0.5, -0.5, -0.5)))
-  })
-  var obj2 = newEntity("Obj2", {
-    "mesh": Component(rect()),
-    "hitbox": Component(HitBox(transform: translate3d(-0.5, -0.5, -0.5)))
-  })
-  var obj3 = newEntity("Obj3", {
-    "mesh": Component(circle(color="0f0f")),
-    "hitbox": Component(HitSphere(radius: 0.5))
-  })
-  
-  scene.root.add obj2
-  scene.root.add obj1
-  scene.root.add obj3
-  obj1.transform = scale3d(0.8, 0.8)
-  obj3.transform = scale3d(0.1, 0.1)
+  scene.meshes.add rect(color="f00f")
+  scene.meshes.add rect()
+  scene.meshes.add circle(color="0f0f")
+  scene.meshes[1].transform = scale(0.8, 0.8)
+  scene.meshes[2].transform = scale(0.1, 0.1)
+  scene.addShaderGlobal("perspective", Unit4F32)
 
   const
-    vertexInput = @[
-      attr[Mat4]("transform", memoryPerformanceHint=PreferFastRead, perInstance=true),
-      attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead),
-      attr[Vec4f]("color", memoryPerformanceHint=PreferFastRead),
-    ]
-    intermediate = @[attr[Vec4f]("colorout"),]
-    uniforms = @[attr[Mat4]("perspective")]
-    fragOutput = @[attr[Vec4f]("fragcolor")]
-    (vertexCode, fragmentCode) = compileVertexFragmentShaderSet(
-      inputs=vertexInput,
-      intermediate=intermediate,
-      outputs=fragOutput,
-      uniforms=uniforms,
+    shaderConfiguration = createShaderConfiguration(
+      inputs=[
+        attr[Mat4]("transform", memoryPerformanceHint=PreferFastRead, perInstance=true),
+        attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead),
+        attr[Vec4f]("color", memoryPerformanceHint=PreferFastRead),
+      ],
+      intermediates=[attr[Vec4f]("colorout")],
+      uniforms=[attr[Mat4]("perspective")],
+      outputs=[attr[Vec4f]("fragcolor")],
       vertexCode="""gl_Position = vec4(position, 1.0) * (transform * Uniforms.perspective); colorout = color;""",
       fragmentCode="""fragcolor = colorout;""",
     )
 
   var engine = initEngine("Test collisions")
-  engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode))
-  engine.addScene(scene, vertexInput, @[], materialIndexAttribute="")
-  scene.addShaderGlobal("perspective", Unit4F32)
+
+  engine.initRenderer({"": shaderConfiguration}.toTable)
+  engine.addScene(scene)
 
   while engine.updateInputs() == Running and not engine.keyIsDown(Escape):
     if engine.windowWasResized():
       var winSize = engine.getWindow().size
       scene.setShaderGlobal("perspective", orthoWindowAspect(winSize[1] / winSize[0]))
-    if engine.keyIsDown(A): obj1.transform = obj1.transform * translate3d(-0.001,      0, 0)
-    if engine.keyIsDown(D): obj1.transform = obj1.transform * translate3d( 0.001,      0, 0)
-    if engine.keyIsDown(W): obj1.transform = obj1.transform * translate3d(     0, -0.001, 0)
-    if engine.keyIsDown(S): obj1.transform = obj1.transform * translate3d(     0,  0.001, 0)
-    if engine.keyIsDown(Q): obj1.transform = obj1.transform * rotate3d(-0.001, Z)
-    if engine.keyIsDown(Key.E): obj1.transform = obj1.transform * rotate3d( 0.001, Z)
+    if engine.keyIsDown(A): scene.meshes[0].transform = scene.meshes[0].transform * translate(-0.001,      0, 0)
+    if engine.keyIsDown(D): scene.meshes[0].transform = scene.meshes[0].transform * translate( 0.001,      0, 0)
+    if engine.keyIsDown(W): scene.meshes[0].transform = scene.meshes[0].transform * translate(     0, -0.001, 0)
+    if engine.keyIsDown(S): scene.meshes[0].transform = scene.meshes[0].transform * translate(     0,  0.001, 0)
+    if engine.keyIsDown(Q): scene.meshes[0].transform = scene.meshes[0].transform * rotate(-0.001, Z)
+    if engine.keyIsDown(Key.E): scene.meshes[0].transform = scene.meshes[0].transform * rotate( 0.001, Z)
 
-    if engine.keyIsDown(Key.Z): obj2.transform = obj2.transform * rotate3d(-0.001, Z)
-    if engine.keyIsDown(Key.X): obj2.transform = obj2.transform * rotate3d( 0.001, Z)
-    if engine.keyIsDown(Key.C): obj2.transform = obj2.transform * translate3d(0, -0.001, 0)
-    if engine.keyIsDown(Key.V): obj2.transform = obj2.transform * translate3d(0,  0.001, 0)
-    echo intersects(obj1["hitbox", HitBox()], obj3["hitbox", HitSphere()])
+    if engine.keyIsDown(Key.Z): scene.meshes[1].transform = scene.meshes[1].transform * rotate(-0.001, Z)
+    if engine.keyIsDown(Key.X): scene.meshes[1].transform = scene.meshes[1].transform * rotate( 0.001, Z)
+    if engine.keyIsDown(Key.C): scene.meshes[1].transform = scene.meshes[1].transform * translate(0, -0.001, 0)
+    if engine.keyIsDown(Key.V): scene.meshes[1].transform = scene.meshes[1].transform * translate(0,  0.001, 0)
+    let hitbox = Collider(theType: Box, transform: scene.meshes[0].transform * translate(-0.5, -0.5))
+    let hitsphere = Collider(theType: Sphere, transform: scene.meshes[2].transform, radius: 0.5)
+    echo intersects(hitbox, hitsphere)
     engine.renderScene(scene)
   engine.destroy()