Mercurial > games > semicongine
comparison old_tests/test_materials.nim @ 1203:6360c8d17ce0 compiletime-tests
did: preprations to add rendering tests
| author | sam <sam@basx.dev> | 
|---|---|
| date | Mon, 15 Jul 2024 20:06:42 +0700 | 
| parents | tests/test_materials.nim@114f395b9144 | 
| children | 
   comparison
  equal
  deleted
  inserted
  replaced
| 1202:a8864fe6fe6e | 1203:6360c8d17ce0 | 
|---|---|
| 1 import std/times | |
| 2 import std/tables | |
| 3 | |
| 4 import semicongine | |
| 5 | |
| 6 let | |
| 7 sampler = Sampler(magnification: VK_FILTER_NEAREST, minification: VK_FILTER_NEAREST) | |
| 8 (RT, WT, PT) = (ToRGBA("A51931").AsPixel, ToRGBA("F4F5F8").AsPixel, ToRGBA("2D2A4A").AsPixel) | |
| 9 thai = Image[RGBAPixel](width: 7, height: 5, imagedata: @[ | |
| 10 RT, RT, RT, RT, RT, RT, RT, | |
| 11 WT, WT, WT, WT, WT, WT, WT, | |
| 12 PT, PT, PT, PT, PT, PT, PT, | |
| 13 WT, WT, WT, WT, WT, WT, WT, | |
| 14 RT, RT, RT, RT, RT, RT, RT, | |
| 15 ]) | |
| 16 swiss = LoadImage[RGBAPixel]("flag.png") | |
| 17 doubleTextureMaterial = MaterialType( | |
| 18 name: "Double texture", | |
| 19 vertexAttributes: { | |
| 20 "position": Vec3F32, | |
| 21 "uv": Vec2F32, | |
| 22 }.toTable, | |
| 23 attributes: {"tex1": TextureType, "tex2": TextureType}.toTable | |
| 24 ) | |
| 25 material = InitMaterialData( | |
| 26 theType = doubleTextureMaterial, | |
| 27 name = "swiss-thai", | |
| 28 attributes = { | |
| 29 "tex1": InitDataList(@[Texture(colorImage: thai, sampler: sampler, isGrayscale: false)]), | |
| 30 "tex2": InitDataList(@[Texture(colorImage: swiss, sampler: sampler, isGrayscale: false)]), | |
| 31 } | |
| 32 ) | |
| 33 | |
| 34 proc main() = | |
| 35 var flag = Rect() | |
| 36 flag.material = material | |
| 37 var scene = Scene(name: "main", meshes: @[flag]) | |
| 38 scene.AddShaderGlobalArray("test2", @[NewVec4f(), NewVec4f()]) | |
| 39 | |
| 40 var engine = InitEngine("Test materials") | |
| 41 | |
| 42 const | |
| 43 shaderConfiguration1 = CreateShaderConfiguration( | |
| 44 name = "shader 1", | |
| 45 inputs = [ | |
| 46 Attr[Vec3f]("position", memoryPerformanceHint = PreferFastRead), | |
| 47 Attr[Vec2f]("uv", memoryPerformanceHint = PreferFastRead), | |
| 48 ], | |
| 49 intermediates = [ | |
| 50 Attr[Vec2f]("uvout"), | |
| 51 ], | |
| 52 uniforms = [Attr[Vec4f]("test2", arrayCount = 2)], | |
| 53 samplers = @[ | |
| 54 Attr[Texture]("tex1"), | |
| 55 Attr[Texture]("tex2"), | |
| 56 ], | |
| 57 outputs = [Attr[Vec4f]("color")], | |
| 58 vertexCode = """ | |
| 59 gl_Position = vec4(position.x, position.y + sin(Uniforms.test2[1].x) / Uniforms.test2[1].x * 0.5, position.z, 1.0); | |
| 60 uvout = uv;""", | |
| 61 fragmentCode = """ | |
| 62 float d = sin(Uniforms.test2[0].x) * 0.5 + 0.5; | |
| 63 color = texture(tex1, uvout) * (1 - d) + texture(tex2, uvout) * d; | |
| 64 """, | |
| 65 ) | |
| 66 engine.InitRenderer({ | |
| 67 doubleTextureMaterial: shaderConfiguration1, | |
| 68 }) | |
| 69 engine.LoadScene(scene) | |
| 70 var t = cpuTime() | |
| 71 while engine.UpdateInputs() and not KeyIsDown(Escape): | |
| 72 var d = float32(cpuTime() - t) | |
| 73 SetShaderGlobalArray(scene, "test2", @[NewVec4f(d), NewVec4f(d * 2)]) | |
| 74 engine.RenderScene(scene) | |
| 75 engine.Destroy() | |
| 76 | |
| 77 | |
| 78 when isMainModule: | |
| 79 main() | 
