view tests/test_materials.nim @ 812:27346eb7dd37

did: mini-API QoL improvment
author Sam <sam@basx.dev>
date Tue, 19 Sep 2023 23:53:04 +0700
parents 7a13941ba204
children 00231e014642
line wrap: on
line source

import std/times
import std/tables

import semicongine

let
  sampler = Sampler(magnification: VK_FILTER_NEAREST, minification: VK_FILTER_NEAREST)
  (RT, WT, PT) = (hexToColorAlpha("A51931").asPixel, hexToColorAlpha("F4F5F8").asPixel, hexToColorAlpha("2D2A4A").asPixel)
  thai = Image(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,
  ])
  swiss = loadImage("flag.png")
  material = Material(name: "material", textures: {
    "tex1": Texture(image: thai, sampler: sampler),
    "tex2": Texture(image: swiss, sampler: sampler),
  }.toTable)

proc main() =
  var flag = rect()
  flag.material = material
  var scene = Scene(name: "main", meshes: @[flag])
  scene.addShaderGlobalArray("test2", @[0'f32, 0'f32])

  var engine = initEngine("Test materials")

  const
    shaderConfiguration1 = createShaderConfiguration(
      inputs=[
        attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead),
        attr[Vec2f]("uv", memoryPerformanceHint=PreferFastRead),
      ],
      intermediates=[
        attr[Vec2f]("uvout"),
      ],
      uniforms=[attr[float32]("test2", arrayCount=2)],
      samplers = @[
        attr[Texture]("tex1", arrayCount=2),
        attr[Texture]("tex2", arrayCount=2),
      ],
      outputs=[attr[Vec4f]("color")],
      vertexCode="""
      gl_Position = vec4(position.x, position.y + sin(Uniforms.test2[1]) / Uniforms.test2[1] * 0.5, position.z, 1.0);
      uvout = uv;""",
      fragmentCode="""
      float d = sin(Uniforms.test2[0]) * 0.5 + 0.5;
      color = texture(tex1[0], uvout) * (1 - d) + texture(tex2[0], uvout) * d;
      """,
    )
  engine.initRenderer({
    "material": shaderConfiguration1,
  })
  engine.addScene(scene)
  var t = cpuTime()
  while engine.updateInputs() == Running and not engine.keyIsDown(Escape):
    var d = float32(cpuTime() - t)
    setShaderGlobalArray(scene, "test2", @[d, d * 2])
    engine.renderScene(scene)
  engine.destroy()


when isMainModule:
  main()