diff examples/E10_pong.nim @ 336:887ddc8d45fd

did: update examples to work with improved scenegraph/material api, notice removed complexity!
author Sam <sam@basx.dev>
date Tue, 05 Sep 2023 00:28:35 +0700
parents b145a05c2459
children 2ba3f18e7cad
line wrap: on
line diff
--- a/examples/E10_pong.nim	Mon Sep 04 00:55:35 2023 +0700
+++ b/examples/E10_pong.nim	Tue Sep 05 00:28:35 2023 +0700
@@ -1,4 +1,5 @@
 import std/times
+import std/tables
 
 import ../src/semicongine
 
@@ -8,8 +9,8 @@
   barWidth = 0.01'f
   ballcolor = hexToColorAlpha("B17F08").gamma(2.2).colorToHex()
   ballSize = 0.01'f
-  backgroundColor = hexToColorAlpha("FAC034FF").gamma(2.2)
   ballSpeed = 60'f
+  material = Material(name: "default")
 
 var
   level: Scene
@@ -17,41 +18,31 @@
 
 when isMainModule:
   var myengine = initEngine("Pong")
-  level = newScene("scene", newEntity("Level"))
-  var playerbarmesh = rect(color=barcolor)
-  var playerbar = newEntity("playerbar", {"mesh": Component(playerbarmesh)})
-  playerbar.transform = scale3d(barWidth, barSize, 1'f) * translate3d(0.5'f, 0'f, 0'f)
-  var player = newEntity("player", [], playerbar)
-  player.transform = translate3d(0'f, 0.3'f, 0'f)
-  level.root.add player
 
-  var ballmesh = circle(color=ballcolor)
-  var ball = newEntity("ball", {"mesh": Component(ballmesh)})
-  ball.transform = scale3d(ballSize, ballSize, 1'f) * translate3d(10'f, 10'f, 0'f)
-  level.root.add ball
+  var player = rect(color=barcolor, width=barWidth, height=barSize)
+  player.material = material
+  var ball = circle(color=ballcolor)
+  ball.material = material
+  level = Scene(name: "scene", meshes: @[ball, player])
 
   const
-    inputs = @[
-      attr[Vec3f]("position"),
-      attr[Vec4f]("color", memoryPerformanceHint=PreferFastWrite),
-      attr[Mat4]("transform", memoryPerformanceHint=PreferFastWrite, perInstance=true),
-    ]
-    intermediate = @[attr[Vec4f]("outcolor")]
-    uniforms = @[attr[Mat4]("projection")]
-    outputs = @[attr[Vec4f]("color")]
-    (vertexCode, fragmentCode) = compileVertexFragmentShaderSet(
-      inputs=inputs,
-      intermediate=intermediate,
-      outputs=outputs,
-      uniforms=uniforms,
+    shaderConfiguration = createShaderConfiguration(
+      inputs=[
+        attr[Vec3f]("position"),
+        attr[Vec4f]("color", memoryPerformanceHint=PreferFastWrite),
+        attr[Mat4]("transform", memoryPerformanceHint=PreferFastWrite, perInstance=true),
+      ],
+      intermediates=[attr[Vec4f]("outcolor")],
+      uniforms=[attr[Mat4]("projection")],
+      outputs=[attr[Vec4f]("color")],
       vertexCode="""outcolor = color; gl_Position = vec4(position, 1) * (transform * Uniforms.projection);""",
       fragmentCode="color = outcolor;",
     )
 
   # set up rendering
-  myengine.setRenderer(myengine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode, clearColor=backgroundColor))
-  myengine.addScene(level, inputs, @[], transformAttribute="transform")
+  myengine.initRenderer({"default": shaderConfiguration}.toTable)
   level.addShaderGlobal("projection", Unit4f32)
+  myengine.addScene(level)
 
   var
     winsize = myengine.getWindow().size
@@ -78,22 +69,21 @@
       height = float32(winsize[1]) / float32(winsize[0])
       width = 1'f
       setShaderGlobal(level, "projection", ortho(0, width, 0, height, 0, 1))
-    var player = level.root.firstWithName("player")
     if myengine.keyIsDown(Down) and (player.transform.col(3).y + barSize/2) < height:
-      player.transform = player.transform * translate3d(0'f, 1'f * dt, 0'f)
+      player.transform = player.transform * translate(0'f, 1'f * dt, 0'f)
     if myengine.keyIsDown(Up) and (player.transform.col(3).y - barSize/2) > 0:
-      player.transform = player.transform * translate3d(0'f, -1'f * dt, 0'f)
+      player.transform = player.transform * translate(0'f, -1'f * dt, 0'f)
 
     # bounce level
     if ball.transform.col(3).x + ballSize/2 > width: ballVelocity[0] = -ballVelocity[0]
     if ball.transform.col(3).y - ballSize/2 <= 0: ballVelocity[1] = -ballVelocity[1]
     if ball.transform.col(3).y + ballSize/2 > height: ballVelocity[1] = -ballVelocity[1]
 
-    ball.transform = ball.transform * translate3d(ballVelocity[0] * dt, ballVelocity[1] * dt, 0'f32)
+    ball.transform = ball.transform * translate(ballVelocity[0] * dt, ballVelocity[1] * dt, 0'f32)
 
     # loose
     if ball.transform.col(3).x - ballSize/2 <= 0:
-      ball.transform = scale3d(ballSize, ballSize, 1'f) * translate3d(30'f, 30'f, 0'f)
+      ball.transform = scale(ballSize, ballSize, 1'f) * translate(30'f, 30'f, 0'f)
       ballVelocity = newVec2f(1, 1).normalized * ballSpeed
 
     # bar