277
|
1 import semicongine
|
|
2
|
|
3
|
|
4 proc main() =
|
|
5 var scene = newScene("main", root=newEntity("rect"))
|
|
6
|
320
|
7 var obj1 = newEntity("Obj1", {
|
|
8 "mesh": Component(rect(color="f00f")),
|
|
9 "hitbox": Component(HitBox(transform: translate3d(-0.5, -0.5, -0.5)))
|
|
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 })
|
277
|
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
|
|
26 const
|
|
27 vertexInput = @[
|
|
28 attr[Mat4]("transform", memoryPerformanceHint=PreferFastRead, perInstance=true),
|
|
29 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead),
|
|
30 attr[Vec4f]("color", memoryPerformanceHint=PreferFastRead),
|
|
31 ]
|
|
32 intermediate = @[attr[Vec4f]("colorout"),]
|
|
33 uniforms = @[attr[Mat4]("perspective")]
|
|
34 fragOutput = @[attr[Vec4f]("fragcolor")]
|
|
35 (vertexCode, fragmentCode) = compileVertexFragmentShaderSet(
|
|
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;""",
|
|
41 fragmentCode="""fragcolor = colorout;""",
|
|
42 )
|
|
43
|
|
44 var engine = initEngine("Test collisions")
|
|
45 engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode))
|
320
|
46 engine.addScene(scene, vertexInput, @[], materialIndexAttribute="")
|
277
|
47 scene.addShaderGlobal("perspective", Unit4F32)
|
|
48
|
|
49 while engine.updateInputs() == Running and not engine.keyIsDown(Escape):
|
|
50 if engine.windowWasResized():
|
|
51 var winSize = engine.getWindow().size
|
|
52 scene.setShaderGlobal("perspective", orthoWindowAspect(winSize[1] / winSize[0]))
|
|
53 if engine.keyIsDown(A): obj1.transform = obj1.transform * translate3d(-0.001, 0, 0)
|
|
54 if engine.keyIsDown(D): obj1.transform = obj1.transform * translate3d( 0.001, 0, 0)
|
|
55 if engine.keyIsDown(W): obj1.transform = obj1.transform * translate3d( 0, -0.001, 0)
|
|
56 if engine.keyIsDown(S): obj1.transform = obj1.transform * translate3d( 0, 0.001, 0)
|
|
57 if engine.keyIsDown(Q): obj1.transform = obj1.transform * rotate3d(-0.001, Z)
|
|
58 if engine.keyIsDown(Key.E): obj1.transform = obj1.transform * rotate3d( 0.001, Z)
|
|
59
|
|
60 if engine.keyIsDown(Key.Z): obj2.transform = obj2.transform * rotate3d(-0.001, Z)
|
|
61 if engine.keyIsDown(Key.X): obj2.transform = obj2.transform * rotate3d( 0.001, Z)
|
|
62 if engine.keyIsDown(Key.C): obj2.transform = obj2.transform * translate3d(0, -0.001, 0)
|
|
63 if engine.keyIsDown(Key.V): obj2.transform = obj2.transform * translate3d(0, 0.001, 0)
|
320
|
64 echo intersects(obj1["hitbox", HitBox()], obj3["hitbox", HitSphere()])
|
277
|
65 engine.renderScene(scene)
|
|
66 engine.destroy()
|
|
67
|
|
68
|
|
69 when isMainModule:
|
|
70 main()
|