comparison tests/test_materials.nim @ 793:c31e42d72253

fix: material tests
author Sam <sam@basx.dev>
date Sun, 03 Sep 2023 17:46:40 +0700
parents 754835bf175e
children 2533f524bdb6
comparison
equal deleted inserted replaced
792:d65b62812d34 793:c31e42d72253
1 import std/times 1 import std/times
2 import std/tables 2 import std/tables
3 3
4 import semicongine 4 import semicongine
5 5
6 let
7 sampler = Sampler(magnification: VK_FILTER_NEAREST, minification: VK_FILTER_NEAREST)
8 (RT, WT, PT) = (hexToColorAlpha("A51931").asPixel, hexToColorAlpha("F4F5F8").asPixel, hexToColorAlpha("2D2A4A").asPixel)
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")
17 material = Material(name: "material", textures: {
18 "tex1": Texture(image: thai, sampler: sampler),
19 "tex2": Texture(image: swiss, sampler: sampler),
20 }.toTable)
21
6 proc main() = 22 proc main() =
7 var flag = rect() 23 var flag = rect()
8 flag.materials = @["material2"] 24 flag.material = material
9 var scene = newScene("main", root=newEntity("rect", {"mesh": Component(flag)})) 25 var scene = Scene(name: "main", meshes: @[flag])
10 let (RT, WT, PT) = (hexToColorAlpha("A51931").asPixel, hexToColorAlpha("F4F5F8").asPixel, hexToColorAlpha("2D2A4A").asPixel)
11 let
12 # image from memory
13 thai = Image(width: 7, height: 5, imagedata: @[
14 RT, RT, RT, RT, RT, RT, RT,
15 WT, WT, WT, WT, WT, WT, WT,
16 PT, PT, PT, PT, PT, PT, PT,
17 WT, WT, WT, WT, WT, WT, WT,
18 RT, RT, RT, RT, RT, RT, RT,
19 ])
20 let
21 # image from file
22 swiss = loadImage("flag.png")
23
24 var sampler = DefaultSampler()
25 sampler.magnification = VK_FILTER_NEAREST
26 sampler.minification = VK_FILTER_NEAREST
27 scene.addMaterial(Material(name:"material1", textures: {
28 "tex1": Texture(image: swiss, sampler: sampler),
29 "tex2": Texture(image: thai, sampler: sampler),
30 }.toTable))
31 scene.addMaterial(Material(name:"material2", textures: {
32 "tex1": Texture(image: thai, sampler: sampler),
33 "tex2": Texture(image: swiss, sampler: sampler),
34 }.toTable))
35 scene.addShaderGlobalArray("test2", @[0'f32, 0'f32]) 26 scene.addShaderGlobalArray("test2", @[0'f32, 0'f32])
36 27
37 var engine = initEngine("Test materials") 28 var engine = initEngine("Test materials")
38 29
39 const 30 const
40 vertexInput = @[ 31 shaderConfiguration1 = createShaderConfiguration(
41 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead), 32 inputs=[
42 attr[uint16]("materialIndex", memoryPerformanceHint=PreferFastRead, perInstance=true), 33 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead),
43 attr[Vec2f]("uv", memoryPerformanceHint=PreferFastRead), 34 attr[Vec2f]("uv", memoryPerformanceHint=PreferFastRead),
44 ] 35 ],
45 vertexOutput = @[attr[Vec2f]("uvout"), attr[uint16]("materialIndexOut", noInterpolation=true)] 36 intermediates=[
46 uniforms = @[attr[float32]("test2", arrayCount=2)] 37 attr[Vec2f]("uvout"),
47 samplers = @[ 38 ],
48 attr[Sampler2DType]("tex1", arrayCount=2), 39 uniforms=[attr[float32]("test2", arrayCount=2)],
49 attr[Sampler2DType]("tex2", arrayCount=2), 40 samplers = @[
50 ] 41 attr[Sampler2DType]("tex1", arrayCount=2),
51 fragOutput = @[attr[Vec4f]("color")] 42 attr[Sampler2DType]("tex2", arrayCount=2),
52 (vertexCode, fragmentCode) = compileVertexFragmentShaderSet( 43 ],
53 inputs=vertexInput, 44 outputs=[attr[Vec4f]("color")],
54 intermediate=vertexOutput,
55 outputs=fragOutput,
56 samplers=samplers,
57 uniforms=uniforms,
58 vertexCode=""" 45 vertexCode="""
59 gl_Position = vec4(position.x, position.y + sin(Uniforms.test2[1]) / Uniforms.test2[1] * 0.5, position.z, 1.0); 46 gl_Position = vec4(position.x, position.y + sin(Uniforms.test2[1]) / Uniforms.test2[1] * 0.5, position.z, 1.0);
60 uvout = uv; 47 uvout = uv;""",
61 materialIndexOut = materialIndex;""",
62 fragmentCode=""" 48 fragmentCode="""
63 float d = sin(Uniforms.test2[0]) * 0.5 + 0.5; 49 float d = sin(Uniforms.test2[0]) * 0.5 + 0.5;
64 color = texture(tex1[materialIndexOut], uvout) * (1 - d) + texture(tex2[materialIndexOut], uvout) * d; 50 color = texture(tex1[0], uvout) * (1 - d) + texture(tex2[0], uvout) * d;
65 """, 51 """,
66 ) 52 )
67 engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode)) 53 engine.initRenderer({
68 engine.addScene(scene, vertexInput, samplers, transformAttribute="") 54 "material": shaderConfiguration1,
55 }.toTable)
56 engine.addScene(scene)
69 var t = cpuTime() 57 var t = cpuTime()
70 while engine.updateInputs() == Running and not engine.keyIsDown(Escape): 58 while engine.updateInputs() == Running and not engine.keyIsDown(Escape):
71 var d = float32(cpuTime() - t) 59 var d = float32(cpuTime() - t)
72 setShaderGlobalArray(scene, "test2", @[d, d * 2]) 60 setShaderGlobalArray(scene, "test2", @[d, d * 2])
73 engine.renderScene(scene) 61 engine.renderScene(scene)