comparison tests/test_materials.nim @ 315:4921ec86dcb4

did: preparations to refactor material system, still tons to do
author Sam <sam@basx.dev>
date Sun, 23 Jul 2023 19:59:47 +0700
parents da0bd61abe91
children b145a05c2459
comparison
equal deleted inserted replaced
314:325823d1b61e 315:4921ec86dcb4
2 import std/tables 2 import std/tables
3 3
4 import semicongine 4 import semicongine
5 5
6 proc main() = 6 proc main() =
7 var scene = newScene("main", root=newEntity("rect", {"mesh": Component(rect())})) 7 var flag = rect()
8 var scene = newScene("main", root=newEntity("rect", {"mesh": Component(flag)}))
8 let (RT, WT, PT) = (hexToColorAlpha("A51931").asPixel, hexToColorAlpha("F4F5F8").asPixel, hexToColorAlpha("2D2A4A").asPixel) 9 let (RT, WT, PT) = (hexToColorAlpha("A51931").asPixel, hexToColorAlpha("F4F5F8").asPixel, hexToColorAlpha("2D2A4A").asPixel)
9 let 10 let
10 # image from memory 11 # image from memory
11 t1 = Image(width: 7, height: 5, imagedata: @[ 12 thai = Image(width: 7, height: 5, imagedata: @[
12 RT, RT, RT, RT, RT, RT, RT, 13 RT, RT, RT, RT, RT, RT, RT,
13 WT, WT, WT, WT, WT, WT, WT, 14 WT, WT, WT, WT, WT, WT, WT,
14 PT, PT, PT, PT, PT, PT, PT, 15 PT, PT, PT, PT, PT, PT, PT,
15 WT, WT, WT, WT, WT, WT, WT, 16 WT, WT, WT, WT, WT, WT, WT,
16 RT, RT, RT, RT, RT, RT, RT, 17 RT, RT, RT, RT, RT, RT, RT,
17 ]) 18 ])
18 let 19 let
19 # image from file 20 # image from file
20 t2 = loadImage("flag.png") 21 swiss = loadImage("flag.png")
21 22
22 var sampler = DefaultSampler() 23 var sampler = DefaultSampler()
23 sampler.magnification = VK_FILTER_NEAREST 24 sampler.magnification = VK_FILTER_NEAREST
24 sampler.minification = VK_FILTER_NEAREST 25 sampler.minification = VK_FILTER_NEAREST
25 scene.addMaterial(Material(name:"my material", textures: {"my_texture": Texture(image: t1, sampler: sampler)}.toTable)) 26 scene.addMaterial(Material(name:"material1", textures: {
26 scene.addMaterial(Material(name:"my material", textures: {"my_texture": Texture(image: t2, sampler: sampler)}.toTable)) 27 "swissflag": Texture(image: swiss, sampler: sampler),
28 "thaiflag": Texture(image: thai, sampler: sampler),
29 }.toTable))
30 scene.addMaterial(Material(name:"material2", textures: {
31 "swissflag": Texture(image: thai, sampler: sampler),
32 "thaiflag": Texture(image: swiss, sampler: sampler),
33 }.toTable))
27 scene.addShaderGlobalArray("test2", @[0'f32, 0'f32]) 34 scene.addShaderGlobalArray("test2", @[0'f32, 0'f32])
28 35
29 var engine = initEngine("Test materials") 36 var engine = initEngine("Test materials")
30 37
31 const 38 const
33 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead), 40 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead),
34 attr[Vec2f]("uv", memoryPerformanceHint=PreferFastRead), 41 attr[Vec2f]("uv", memoryPerformanceHint=PreferFastRead),
35 ] 42 ]
36 vertexOutput = @[attr[Vec2f]("uvout")] 43 vertexOutput = @[attr[Vec2f]("uvout")]
37 uniforms = @[attr[float32]("test2", arrayCount=2)] 44 uniforms = @[attr[float32]("test2", arrayCount=2)]
38 samplers = @[attr[Sampler2DType]("my_texture", arrayCount=2)] 45 samplers = @[
46 attr[Sampler2DType]("swissflag", arrayCount=2),
47 attr[Sampler2DType]("thaiflag", arrayCount=2),
48 ]
39 fragOutput = @[attr[Vec4f]("color")] 49 fragOutput = @[attr[Vec4f]("color")]
40 vertexCode = compileGlslShader( 50 vertexCode = compileGlslShader(
41 stage=VK_SHADER_STAGE_VERTEX_BIT, 51 stage=VK_SHADER_STAGE_VERTEX_BIT,
42 inputs=vertexInput, 52 inputs=vertexInput,
43 uniforms=uniforms, 53 uniforms=uniforms,
51 uniforms=uniforms, 61 uniforms=uniforms,
52 samplers=samplers, 62 samplers=samplers,
53 outputs=fragOutput, 63 outputs=fragOutput,
54 main=""" 64 main="""
55 float d = sin(Uniforms.test2[0]) * 0.5 + 0.5; 65 float d = sin(Uniforms.test2[0]) * 0.5 + 0.5;
56 color = texture(my_texture[0], uvout) * (1 - d) + texture(my_texture[1], uvout) * d; 66 color = texture(swissflag[1], uvout) * (1 - d) + texture(thaiflag[1], uvout) * d;
57 """ 67 """
58 ) 68 )
59 engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode)) 69 engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode))
60 engine.addScene(scene, vertexInput, samplers, transformAttribute="") 70 engine.addScene(scene, vertexInput, samplers, transformAttribute="", materialIndexAttribute="")
61 var t = cpuTime() 71 var t = cpuTime()
62 while engine.updateInputs() == Running and not engine.keyIsDown(Escape): 72 while engine.updateInputs() == Running and not engine.keyIsDown(Escape):
63 var d = float32(cpuTime() - t) 73 var d = float32(cpuTime() - t)
64 setShaderGlobalArray(scene, "test2", @[d, d * 2]) 74 setShaderGlobalArray(scene, "test2", @[d, d * 2])
65 engine.renderScene(scene) 75 engine.renderScene(scene)