Mercurial > games > semicongine
annotate tests/test_materials.nim @ 304:0c6e73fcc61f
Merge branch 'main' of github.com:saemideluxe/semicongine
| author | Sam <sam@basx.dev> |
|---|---|
| date | Tue, 27 Jun 2023 00:17:55 +0700 |
| parents | da0bd61abe91 |
| children | 4921ec86dcb4 |
| rev | line source |
|---|---|
| 201 | 1 import std/times |
|
272
bfcb41211c5b
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
253
diff
changeset
|
2 import std/tables |
| 201 | 3 |
|
199
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
4 import semicongine |
|
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
5 |
|
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
6 proc main() = |
| 302 | 7 var scene = newScene("main", root=newEntity("rect", {"mesh": Component(rect())})) |
|
200
1ba005328615
add: few improvments for working with textures
Sam <sam@basx.dev>
parents:
199
diff
changeset
|
8 let (RT, WT, PT) = (hexToColorAlpha("A51931").asPixel, hexToColorAlpha("F4F5F8").asPixel, hexToColorAlpha("2D2A4A").asPixel) |
| 201 | 9 let |
| 244 | 10 # image from memory |
|
234
3918e42bf26e
add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
11 t1 = Image(width: 7, height: 5, imagedata: @[ |
| 201 | 12 RT, RT, RT, RT, RT, RT, RT, |
| 13 WT, WT, WT, WT, WT, WT, WT, | |
| 14 PT, PT, PT, PT, PT, PT, PT, | |
| 15 WT, WT, WT, WT, WT, WT, WT, | |
| 16 RT, RT, RT, RT, RT, RT, RT, | |
| 17 ]) | |
| 253 | 18 let |
| 244 | 19 # image from file |
|
234
3918e42bf26e
add: png loading with LodePNG (thanks LodePNG-author, this is a super amazing piece of code!)
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
20 t2 = loadImage("flag.png") |
| 253 | 21 |
| 244 | 22 var sampler = DefaultSampler() |
| 23 sampler.magnification = VK_FILTER_NEAREST | |
| 24 sampler.minification = VK_FILTER_NEAREST | |
|
272
bfcb41211c5b
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
253
diff
changeset
|
25 scene.addMaterial(Material(name:"my material", textures: {"my_texture": Texture(image: t1, sampler: sampler)}.toTable)) |
|
bfcb41211c5b
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
253
diff
changeset
|
26 scene.addMaterial(Material(name:"my material", textures: {"my_texture": Texture(image: t2, sampler: sampler)}.toTable)) |
| 230 | 27 scene.addShaderGlobalArray("test2", @[0'f32, 0'f32]) |
|
199
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
28 |
|
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
29 var engine = initEngine("Test materials") |
| 253 | 30 |
|
199
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
31 const |
|
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
32 vertexInput = @[ |
|
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
33 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead), |
|
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
34 attr[Vec2f]("uv", memoryPerformanceHint=PreferFastRead), |
|
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
35 ] |
|
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
36 vertexOutput = @[attr[Vec2f]("uvout")] |
| 230 | 37 uniforms = @[attr[float32]("test2", arrayCount=2)] |
| 201 | 38 samplers = @[attr[Sampler2DType]("my_texture", arrayCount=2)] |
|
199
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
39 fragOutput = @[attr[Vec4f]("color")] |
|
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
40 vertexCode = compileGlslShader( |
|
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
41 stage=VK_SHADER_STAGE_VERTEX_BIT, |
|
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
42 inputs=vertexInput, |
| 201 | 43 uniforms=uniforms, |
|
199
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
44 samplers=samplers, |
|
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
45 outputs=vertexOutput, |
| 230 | 46 main="""gl_Position = vec4(position.x, position.y + sin(Uniforms.test2[1]) / Uniforms.test2[1] * 0.5, position.z, 1.0); uvout = uv;""" |
|
199
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
47 ) |
|
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
48 fragmentCode = compileGlslShader( |
|
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
49 stage=VK_SHADER_STAGE_FRAGMENT_BIT, |
|
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
50 inputs=vertexOutput, |
| 201 | 51 uniforms=uniforms, |
|
199
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
52 samplers=samplers, |
|
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
53 outputs=fragOutput, |
| 201 | 54 main=""" |
| 230 | 55 float d = sin(Uniforms.test2[0]) * 0.5 + 0.5; |
| 201 | 56 color = texture(my_texture[0], uvout) * (1 - d) + texture(my_texture[1], uvout) * d; |
| 57 """ | |
|
199
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
58 ) |
|
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
59 engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode)) |
|
272
bfcb41211c5b
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
253
diff
changeset
|
60 engine.addScene(scene, vertexInput, samplers, transformAttribute="") |
| 201 | 61 var t = cpuTime() |
|
199
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
62 while engine.updateInputs() == Running and not engine.keyIsDown(Escape): |
| 230 | 63 var d = float32(cpuTime() - t) |
| 64 setShaderGlobalArray(scene, "test2", @[d, d * 2]) | |
|
199
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
65 engine.renderScene(scene) |
|
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
66 engine.destroy() |
|
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
67 |
|
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
68 |
|
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
69 when isMainModule: |
|
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
70 main() |
