Mercurial > games > semicongine
annotate tests/test_materials.nim @ 729:7b72da29ad8d
update shader to work correctly
author | Sam <sam@basx.dev> |
---|---|
date | Sun, 28 May 2023 18:11:17 +0700 |
parents | 5f7ec8d1bd33 |
children | bfcb41211c5b |
rev | line source |
---|---|
662 | 1 import std/times |
2 | |
660
c6414ade79f0
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
3 import semicongine |
c6414ade79f0
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
4 |
c6414ade79f0
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
5 proc main() = |
c6414ade79f0
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
6 var scene = newScene("main", root=newEntity("rect", rect())) |
661
9995702c34b2
add: few improvments for working with textures
Sam <sam@basx.dev>
parents:
660
diff
changeset
|
7 let (RT, WT, PT) = (hexToColorAlpha("A51931").asPixel, hexToColorAlpha("F4F5F8").asPixel, hexToColorAlpha("2D2A4A").asPixel) |
662 | 8 let |
705 | 9 # image from memory |
695
d2e8b5edea7e
add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
10 t1 = Image(width: 7, height: 5, imagedata: @[ |
662 | 11 RT, RT, RT, RT, RT, RT, RT, |
12 WT, WT, WT, WT, WT, WT, WT, | |
13 PT, PT, PT, PT, PT, PT, PT, | |
14 WT, WT, WT, WT, WT, WT, WT, | |
15 RT, RT, RT, RT, RT, RT, RT, | |
16 ]) | |
714 | 17 let |
705 | 18 # image from file |
695
d2e8b5edea7e
add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
19 t2 = loadImage("flag.png") |
714 | 20 |
705 | 21 var sampler = DefaultSampler() |
22 sampler.magnification = VK_FILTER_NEAREST | |
23 sampler.minification = VK_FILTER_NEAREST | |
24 scene.addTextures("my_texture", @[Texture(image: t1, sampler: sampler), Texture(image: t2, sampler: sampler)]) | |
691 | 25 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
|
26 |
c6414ade79f0
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
27 var engine = initEngine("Test materials") |
714 | 28 |
660
c6414ade79f0
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
29 const |
c6414ade79f0
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
30 vertexInput = @[ |
c6414ade79f0
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
31 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead), |
c6414ade79f0
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
32 attr[Vec2f]("uv", memoryPerformanceHint=PreferFastRead), |
c6414ade79f0
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
33 ] |
c6414ade79f0
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
34 vertexOutput = @[attr[Vec2f]("uvout")] |
691 | 35 uniforms = @[attr[float32]("test2", arrayCount=2)] |
662 | 36 samplers = @[attr[Sampler2DType]("my_texture", arrayCount=2)] |
660
c6414ade79f0
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
37 fragOutput = @[attr[Vec4f]("color")] |
c6414ade79f0
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
38 vertexCode = compileGlslShader( |
c6414ade79f0
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
39 stage=VK_SHADER_STAGE_VERTEX_BIT, |
c6414ade79f0
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
40 inputs=vertexInput, |
662 | 41 uniforms=uniforms, |
660
c6414ade79f0
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
42 samplers=samplers, |
c6414ade79f0
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
43 outputs=vertexOutput, |
691 | 44 main="""gl_Position = vec4(position.x, position.y + sin(Uniforms.test2[1]) / Uniforms.test2[1] * 0.5, position.z, 1.0); uvout = uv;""" |
660
c6414ade79f0
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
45 ) |
c6414ade79f0
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
46 fragmentCode = compileGlslShader( |
c6414ade79f0
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
47 stage=VK_SHADER_STAGE_FRAGMENT_BIT, |
c6414ade79f0
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
48 inputs=vertexOutput, |
662 | 49 uniforms=uniforms, |
660
c6414ade79f0
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
50 samplers=samplers, |
c6414ade79f0
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
51 outputs=fragOutput, |
662 | 52 main=""" |
691 | 53 float d = sin(Uniforms.test2[0]) * 0.5 + 0.5; |
662 | 54 color = texture(my_texture[0], uvout) * (1 - d) + texture(my_texture[1], uvout) * d; |
55 """ | |
660
c6414ade79f0
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
56 ) |
c6414ade79f0
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
57 engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode)) |
714 | 58 engine.addScene(scene, vertexInput, samplers) |
662 | 59 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
|
60 while engine.updateInputs() == Running and not engine.keyIsDown(Escape): |
691 | 61 var d = float32(cpuTime() - t) |
62 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
|
63 engine.renderScene(scene) |
c6414ade79f0
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
64 engine.destroy() |
c6414ade79f0
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
65 |
c6414ade79f0
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
66 |
c6414ade79f0
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
67 when isMainModule: |
c6414ade79f0
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
68 main() |