annotate tests/test_collision.nim @ 785:1880ab140165

add: first complete working version of multiple materials and shaders per scene, yipie :)
author Sam <sam@basx.dev>
date Sat, 19 Aug 2023 23:30:25 +0700
parents e45c2927ecca
children 69e18f69713b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
738
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
1 import semicongine
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
2
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
3
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
4 proc main() =
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
5 var scene = newScene("main", root=newEntity("rect"))
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
6
781
e45c2927ecca fix: collision tests
Sam <sam@basx.dev>
parents: 738
diff changeset
7 var obj1 = newEntity("Obj1", {
e45c2927ecca fix: collision tests
Sam <sam@basx.dev>
parents: 738
diff changeset
8 "mesh": Component(rect(color="f00f")),
e45c2927ecca fix: collision tests
Sam <sam@basx.dev>
parents: 738
diff changeset
9 "hitbox": Component(HitBox(transform: translate3d(-0.5, -0.5, -0.5)))
e45c2927ecca fix: collision tests
Sam <sam@basx.dev>
parents: 738
diff changeset
10 })
e45c2927ecca fix: collision tests
Sam <sam@basx.dev>
parents: 738
diff changeset
11 var obj2 = newEntity("Obj2", {
e45c2927ecca fix: collision tests
Sam <sam@basx.dev>
parents: 738
diff changeset
12 "mesh": Component(rect()),
e45c2927ecca fix: collision tests
Sam <sam@basx.dev>
parents: 738
diff changeset
13 "hitbox": Component(HitBox(transform: translate3d(-0.5, -0.5, -0.5)))
e45c2927ecca fix: collision tests
Sam <sam@basx.dev>
parents: 738
diff changeset
14 })
e45c2927ecca fix: collision tests
Sam <sam@basx.dev>
parents: 738
diff changeset
15 var obj3 = newEntity("Obj3", {
e45c2927ecca fix: collision tests
Sam <sam@basx.dev>
parents: 738
diff changeset
16 "mesh": Component(circle(color="0f0f")),
e45c2927ecca fix: collision tests
Sam <sam@basx.dev>
parents: 738
diff changeset
17 "hitbox": Component(HitSphere(radius: 0.5))
e45c2927ecca fix: collision tests
Sam <sam@basx.dev>
parents: 738
diff changeset
18 })
738
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
19
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
20 scene.root.add obj2
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
21 scene.root.add obj1
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
22 scene.root.add obj3
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
23 obj1.transform = scale3d(0.8, 0.8)
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
24 obj3.transform = scale3d(0.1, 0.1)
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
25
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
26 const
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
27 vertexInput = @[
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
28 attr[Mat4]("transform", memoryPerformanceHint=PreferFastRead, perInstance=true),
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
29 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead),
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
30 attr[Vec4f]("color", memoryPerformanceHint=PreferFastRead),
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
31 ]
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
32 intermediate = @[attr[Vec4f]("colorout"),]
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
33 uniforms = @[attr[Mat4]("perspective")]
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
34 fragOutput = @[attr[Vec4f]("fragcolor")]
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
35 (vertexCode, fragmentCode) = compileVertexFragmentShaderSet(
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
36 inputs=vertexInput,
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
37 intermediate=intermediate,
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
38 outputs=fragOutput,
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
39 uniforms=uniforms,
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
40 vertexCode="""gl_Position = vec4(position, 1.0) * (transform * Uniforms.perspective); colorout = color;""",
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
41 fragmentCode="""fragcolor = colorout;""",
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
42 )
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
43
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
44 var engine = initEngine("Test collisions")
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
45 engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode))
781
e45c2927ecca fix: collision tests
Sam <sam@basx.dev>
parents: 738
diff changeset
46 engine.addScene(scene, vertexInput, @[], materialIndexAttribute="")
738
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
47 scene.addShaderGlobal("perspective", Unit4F32)
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
48
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
49 while engine.updateInputs() == Running and not engine.keyIsDown(Escape):
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
50 if engine.windowWasResized():
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
51 var winSize = engine.getWindow().size
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
52 scene.setShaderGlobal("perspective", orthoWindowAspect(winSize[1] / winSize[0]))
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
53 if engine.keyIsDown(A): obj1.transform = obj1.transform * translate3d(-0.001, 0, 0)
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
54 if engine.keyIsDown(D): obj1.transform = obj1.transform * translate3d( 0.001, 0, 0)
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
55 if engine.keyIsDown(W): obj1.transform = obj1.transform * translate3d( 0, -0.001, 0)
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
56 if engine.keyIsDown(S): obj1.transform = obj1.transform * translate3d( 0, 0.001, 0)
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
57 if engine.keyIsDown(Q): obj1.transform = obj1.transform * rotate3d(-0.001, Z)
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
58 if engine.keyIsDown(Key.E): obj1.transform = obj1.transform * rotate3d( 0.001, Z)
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
59
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
60 if engine.keyIsDown(Key.Z): obj2.transform = obj2.transform * rotate3d(-0.001, Z)
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
61 if engine.keyIsDown(Key.X): obj2.transform = obj2.transform * rotate3d( 0.001, Z)
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
62 if engine.keyIsDown(Key.C): obj2.transform = obj2.transform * translate3d(0, -0.001, 0)
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
63 if engine.keyIsDown(Key.V): obj2.transform = obj2.transform * translate3d(0, 0.001, 0)
781
e45c2927ecca fix: collision tests
Sam <sam@basx.dev>
parents: 738
diff changeset
64 echo intersects(obj1["hitbox", HitBox()], obj3["hitbox", HitSphere()])
738
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
65 engine.renderScene(scene)
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
66 engine.destroy()
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
67
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
68
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
69 when isMainModule:
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
70 main()