Mercurial > games > semicongine
annotate tests/test_materials.nim @ 872:1ee397815b0b
did: image & font refactoring, add texture-atlas-packing
author | Sam <sam@basx.dev> |
---|---|
date | Sat, 20 Jan 2024 20:17:03 +0700 |
parents | 252cbc10b25f |
children | 91e018270832 |
rev | line source |
---|---|
662 | 1 import std/times |
733
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
714
diff
changeset
|
2 import std/tables |
662 | 3 |
660
c6414ade79f0
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
4 import semicongine |
c6414ade79f0
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
5 |
793 | 6 let |
7 sampler = Sampler(magnification: VK_FILTER_NEAREST, minification: VK_FILTER_NEAREST) | |
813 | 8 (RT, WT, PT) = (toRGBA("A51931").asPixel, toRGBA("F4F5F8").asPixel, toRGBA("2D2A4A").asPixel) |
793 | 9 thai = Image(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("flag.png") | |
834 | 17 doubleTextureMaterial = MaterialType( |
18 name:"Double texture", | |
845
19b5051d6eb7
fix: cleanup when multiple textures use same vulkan image
Sam <sam@basx.dev>
parents:
837
diff
changeset
|
19 vertexAttributes: { |
834 | 20 "position": Vec3F32, |
21 "uv": Vec2F32, | |
22 }.toTable, | |
23 attributes: {"tex1": TextureType, "tex2": TextureType}.toTable | |
24 ) | |
868
252cbc10b25f
did: improve/refactor some of the material API
Sam <sam@basx.dev>
parents:
845
diff
changeset
|
25 material = initMaterialData( |
252cbc10b25f
did: improve/refactor some of the material API
Sam <sam@basx.dev>
parents:
845
diff
changeset
|
26 theType=doubleTextureMaterial, |
252cbc10b25f
did: improve/refactor some of the material API
Sam <sam@basx.dev>
parents:
845
diff
changeset
|
27 name="swiss-thai", |
252cbc10b25f
did: improve/refactor some of the material API
Sam <sam@basx.dev>
parents:
845
diff
changeset
|
28 attributes={ |
834 | 29 "tex1": initDataList(@[Texture(image: thai, sampler: sampler)]), |
30 "tex2": initDataList(@[Texture(image: swiss, sampler: sampler)]), | |
868
252cbc10b25f
did: improve/refactor some of the material API
Sam <sam@basx.dev>
parents:
845
diff
changeset
|
31 } |
834 | 32 ) |
793 | 33 |
660
c6414ade79f0
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
34 proc main() = |
776
002d9c576756
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
763
diff
changeset
|
35 var flag = rect() |
834 | 36 flag.material = material |
793 | 37 var scene = Scene(name: "main", meshes: @[flag]) |
691 | 38 scene.addShaderGlobalArray("test2", @[0'f32, 0'f32]) |
660
c6414ade79f0
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
39 |
c6414ade79f0
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
40 var engine = initEngine("Test materials") |
714 | 41 |
660
c6414ade79f0
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
42 const |
793 | 43 shaderConfiguration1 = createShaderConfiguration( |
44 inputs=[ | |
45 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead), | |
46 attr[Vec2f]("uv", memoryPerformanceHint=PreferFastRead), | |
47 ], | |
48 intermediates=[ | |
49 attr[Vec2f]("uvout"), | |
50 ], | |
51 uniforms=[attr[float32]("test2", arrayCount=2)], | |
52 samplers = @[ | |
837 | 53 attr[Texture]("tex1", arrayCount=1), |
54 attr[Texture]("tex2", arrayCount=1), | |
793 | 55 ], |
56 outputs=[attr[Vec4f]("color")], | |
777
754835bf175e
add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents:
776
diff
changeset
|
57 vertexCode=""" |
754835bf175e
add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents:
776
diff
changeset
|
58 gl_Position = vec4(position.x, position.y + sin(Uniforms.test2[1]) / Uniforms.test2[1] * 0.5, position.z, 1.0); |
793 | 59 uvout = uv;""", |
777
754835bf175e
add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents:
776
diff
changeset
|
60 fragmentCode=""" |
754835bf175e
add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents:
776
diff
changeset
|
61 float d = sin(Uniforms.test2[0]) * 0.5 + 0.5; |
793 | 62 color = texture(tex1[0], uvout) * (1 - d) + texture(tex2[0], uvout) * d; |
777
754835bf175e
add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents:
776
diff
changeset
|
63 """, |
660
c6414ade79f0
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
64 ) |
793 | 65 engine.initRenderer({ |
834 | 66 doubleTextureMaterial: shaderConfiguration1, |
805 | 67 }) |
834 | 68 engine.loadScene(scene) |
662 | 69 var t = cpuTime() |
660
c6414ade79f0
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
70 while engine.updateInputs() == Running and not engine.keyIsDown(Escape): |
691 | 71 var d = float32(cpuTime() - t) |
72 setShaderGlobalArray(scene, "test2", @[d, d * 2]) | |
660
c6414ade79f0
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
73 engine.renderScene(scene) |
c6414ade79f0
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
74 engine.destroy() |
c6414ade79f0
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
75 |
c6414ade79f0
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
76 |
c6414ade79f0
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
77 when isMainModule: |
c6414ade79f0
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
78 main() |