comparison tests/test_collision.nim @ 329:69e18f69713b

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 89800309ab09
children 05fb85ba97dd
comparison
equal deleted inserted replaced
328:8d0ffcacc7e3 329:69e18f69713b
1 import std/tables
2
1 import semicongine 3 import semicongine
2 4
5 proc main() =
6 var scene = Scene(name: "main")
3 7
4 proc main() = 8 scene.meshes.add rect(color="f00f")
5 var scene = newScene("main", root=newEntity("rect")) 9 scene.meshes.add rect()
6 10 scene.meshes.add circle(color="0f0f")
7 var obj1 = newEntity("Obj1", { 11 scene.meshes[1].transform = scale(0.8, 0.8)
8 "mesh": Component(rect(color="f00f")), 12 scene.meshes[2].transform = scale(0.1, 0.1)
9 "hitbox": Component(HitBox(transform: translate3d(-0.5, -0.5, -0.5))) 13 scene.addShaderGlobal("perspective", Unit4F32)
10 })
11 var obj2 = newEntity("Obj2", {
12 "mesh": Component(rect()),
13 "hitbox": Component(HitBox(transform: translate3d(-0.5, -0.5, -0.5)))
14 })
15 var obj3 = newEntity("Obj3", {
16 "mesh": Component(circle(color="0f0f")),
17 "hitbox": Component(HitSphere(radius: 0.5))
18 })
19
20 scene.root.add obj2
21 scene.root.add obj1
22 scene.root.add obj3
23 obj1.transform = scale3d(0.8, 0.8)
24 obj3.transform = scale3d(0.1, 0.1)
25 14
26 const 15 const
27 vertexInput = @[ 16 shaderConfiguration = createShaderConfiguration(
28 attr[Mat4]("transform", memoryPerformanceHint=PreferFastRead, perInstance=true), 17 inputs=[
29 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead), 18 attr[Mat4]("transform", memoryPerformanceHint=PreferFastRead, perInstance=true),
30 attr[Vec4f]("color", memoryPerformanceHint=PreferFastRead), 19 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead),
31 ] 20 attr[Vec4f]("color", memoryPerformanceHint=PreferFastRead),
32 intermediate = @[attr[Vec4f]("colorout"),] 21 ],
33 uniforms = @[attr[Mat4]("perspective")] 22 intermediates=[attr[Vec4f]("colorout")],
34 fragOutput = @[attr[Vec4f]("fragcolor")] 23 uniforms=[attr[Mat4]("perspective")],
35 (vertexCode, fragmentCode) = compileVertexFragmentShaderSet( 24 outputs=[attr[Vec4f]("fragcolor")],
36 inputs=vertexInput,
37 intermediate=intermediate,
38 outputs=fragOutput,
39 uniforms=uniforms,
40 vertexCode="""gl_Position = vec4(position, 1.0) * (transform * Uniforms.perspective); colorout = color;""", 25 vertexCode="""gl_Position = vec4(position, 1.0) * (transform * Uniforms.perspective); colorout = color;""",
41 fragmentCode="""fragcolor = colorout;""", 26 fragmentCode="""fragcolor = colorout;""",
42 ) 27 )
43 28
44 var engine = initEngine("Test collisions") 29 var engine = initEngine("Test collisions")
45 engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode)) 30
46 engine.addScene(scene, vertexInput, @[], materialIndexAttribute="") 31 engine.initRenderer({"": shaderConfiguration}.toTable)
47 scene.addShaderGlobal("perspective", Unit4F32) 32 engine.addScene(scene)
48 33
49 while engine.updateInputs() == Running and not engine.keyIsDown(Escape): 34 while engine.updateInputs() == Running and not engine.keyIsDown(Escape):
50 if engine.windowWasResized(): 35 if engine.windowWasResized():
51 var winSize = engine.getWindow().size 36 var winSize = engine.getWindow().size
52 scene.setShaderGlobal("perspective", orthoWindowAspect(winSize[1] / winSize[0])) 37 scene.setShaderGlobal("perspective", orthoWindowAspect(winSize[1] / winSize[0]))
53 if engine.keyIsDown(A): obj1.transform = obj1.transform * translate3d(-0.001, 0, 0) 38 if engine.keyIsDown(A): scene.meshes[0].transform = scene.meshes[0].transform * translate(-0.001, 0, 0)
54 if engine.keyIsDown(D): obj1.transform = obj1.transform * translate3d( 0.001, 0, 0) 39 if engine.keyIsDown(D): scene.meshes[0].transform = scene.meshes[0].transform * translate( 0.001, 0, 0)
55 if engine.keyIsDown(W): obj1.transform = obj1.transform * translate3d( 0, -0.001, 0) 40 if engine.keyIsDown(W): scene.meshes[0].transform = scene.meshes[0].transform * translate( 0, -0.001, 0)
56 if engine.keyIsDown(S): obj1.transform = obj1.transform * translate3d( 0, 0.001, 0) 41 if engine.keyIsDown(S): scene.meshes[0].transform = scene.meshes[0].transform * translate( 0, 0.001, 0)
57 if engine.keyIsDown(Q): obj1.transform = obj1.transform * rotate3d(-0.001, Z) 42 if engine.keyIsDown(Q): scene.meshes[0].transform = scene.meshes[0].transform * rotate(-0.001, Z)
58 if engine.keyIsDown(Key.E): obj1.transform = obj1.transform * rotate3d( 0.001, Z) 43 if engine.keyIsDown(Key.E): scene.meshes[0].transform = scene.meshes[0].transform * rotate( 0.001, Z)
59 44
60 if engine.keyIsDown(Key.Z): obj2.transform = obj2.transform * rotate3d(-0.001, Z) 45 if engine.keyIsDown(Key.Z): scene.meshes[1].transform = scene.meshes[1].transform * rotate(-0.001, Z)
61 if engine.keyIsDown(Key.X): obj2.transform = obj2.transform * rotate3d( 0.001, Z) 46 if engine.keyIsDown(Key.X): scene.meshes[1].transform = scene.meshes[1].transform * rotate( 0.001, Z)
62 if engine.keyIsDown(Key.C): obj2.transform = obj2.transform * translate3d(0, -0.001, 0) 47 if engine.keyIsDown(Key.C): scene.meshes[1].transform = scene.meshes[1].transform * translate(0, -0.001, 0)
63 if engine.keyIsDown(Key.V): obj2.transform = obj2.transform * translate3d(0, 0.001, 0) 48 if engine.keyIsDown(Key.V): scene.meshes[1].transform = scene.meshes[1].transform * translate(0, 0.001, 0)
64 echo intersects(obj1["hitbox", HitBox()], obj3["hitbox", HitSphere()]) 49 let hitbox = Collider(theType: Box, transform: scene.meshes[0].transform * translate(-0.5, -0.5))
50 let hitsphere = Collider(theType: Sphere, transform: scene.meshes[2].transform, radius: 0.5)
51 echo intersects(hitbox, hitsphere)
65 engine.renderScene(scene) 52 engine.renderScene(scene)
66 engine.destroy() 53 engine.destroy()
67 54
68 55
69 when isMainModule: 56 when isMainModule: