diff tests/test_materials.nim @ 201:ab626e67a1ee

add: support for arrays of samplers
author Sam <sam@basx.dev>
date Mon, 08 May 2023 00:38:05 +0700
parents 1ba005328615
children 744285b47a4d
line wrap: on
line diff
--- a/tests/test_materials.nim	Sun May 07 18:13:39 2023 +0700
+++ b/tests/test_materials.nim	Mon May 08 00:38:05 2023 +0700
@@ -1,16 +1,28 @@
+import std/times
+
 import semicongine
 
 proc main() =
   var scene = newScene("main", root=newEntity("rect", rect()))
   let (R, W) = ([255'u8, 0'u8, 0'u8, 255'u8], [255'u8, 255'u8, 255'u8, 255'u8])
   let (RT, WT, PT) = (hexToColorAlpha("A51931").asPixel, hexToColorAlpha("F4F5F8").asPixel, hexToColorAlpha("2D2A4A").asPixel)
-  scene.addTexture("my_texture", TextureImage(width: 13, height: 5, imagedata: @[
-    R, R, R, R, R, W, RT, RT, RT, RT, RT, RT, RT,
-    R, R, W, R, R, W, WT, WT, WT, WT, WT, WT, WT,
-    R, W, W, W, R, W, PT, PT, PT, PT, PT, PT, PT,
-    R, R, W, R, R, W, WT, WT, WT, WT, WT, WT, WT,
-    R, R, R, R, R, W, RT, RT, RT, RT, RT, RT, RT,
-  ]))
+  let
+    t1 = TextureImage(width: 5, height: 5, imagedata: @[
+      R, R, R, R, R,
+      R, R, W, R, R,
+      R, W, W, W, R,
+      R, R, W, R, R,
+      R, R, R, R, R,
+    ])
+    t2 = TextureImage(width: 7, height: 5, imagedata: @[
+      RT, RT, RT, RT, RT, RT, RT,
+      WT, WT, WT, WT, WT, WT, WT,
+      PT, PT, PT, PT, PT, PT, PT,
+      WT, WT, WT, WT, WT, WT, WT,
+      RT, RT, RT, RT, RT, RT, RT,
+    ])
+  scene.addTextures("my_texture", @[t1, t2])
+  scene.addShaderGlobal("time", 0'f32)
   var m: Mesh = Mesh(scene.root.components[0])
 
   var engine = initEngine("Test materials")
@@ -20,11 +32,13 @@
       attr[Vec2f]("uv", memoryPerformanceHint=PreferFastRead),
     ]
     vertexOutput = @[attr[Vec2f]("uvout")]
-    samplers = @[attr[Sampler2DType]("my_texture")]
+    uniforms = @[attr[float32]("time")]
+    samplers = @[attr[Sampler2DType]("my_texture", arrayCount=2)]
     fragOutput = @[attr[Vec4f]("color")]
     vertexCode = compileGlslShader(
       stage=VK_SHADER_STAGE_VERTEX_BIT,
       inputs=vertexInput,
+      uniforms=uniforms,
       samplers=samplers,
       outputs=vertexOutput,
       main="""gl_Position = vec4(position, 1.0); uvout = uv;"""
@@ -32,13 +46,19 @@
     fragmentCode = compileGlslShader(
       stage=VK_SHADER_STAGE_FRAGMENT_BIT,
       inputs=vertexOutput,
+      uniforms=uniforms,
       samplers=samplers,
       outputs=fragOutput,
-      main="color = texture(my_texture, uvout);"
+      main="""
+float d = sin(Uniforms.time * 0.5) * 0.5 + 0.5;
+color = texture(my_texture[0], uvout) * (1 - d) + texture(my_texture[1], uvout) * d;
+"""
     )
   engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode))
   engine.addScene(scene, vertexInput)
+  var t = cpuTime()
   while engine.updateInputs() == Running and not engine.keyIsDown(Escape):
+    setShaderGlobal(scene, "time", float32(cpuTime() - t))
     engine.renderScene(scene)
   engine.destroy()