Mercurial > games > semicongine
annotate 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 |
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() = |
315
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
302
diff
changeset
|
7 var flag = rect() |
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
302
diff
changeset
|
8 var scene = newScene("main", root=newEntity("rect", {"mesh": Component(flag)})) |
200
1ba005328615
add: few improvments for working with textures
Sam <sam@basx.dev>
parents:
199
diff
changeset
|
9 let (RT, WT, PT) = (hexToColorAlpha("A51931").asPixel, hexToColorAlpha("F4F5F8").asPixel, hexToColorAlpha("2D2A4A").asPixel) |
201 | 10 let |
244 | 11 # image from memory |
315
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
302
diff
changeset
|
12 thai = Image(width: 7, height: 5, imagedata: @[ |
201 | 13 RT, RT, RT, RT, RT, RT, RT, |
14 WT, WT, WT, WT, WT, WT, WT, | |
15 PT, PT, PT, PT, PT, PT, PT, | |
16 WT, WT, WT, WT, WT, WT, WT, | |
17 RT, RT, RT, RT, RT, RT, RT, | |
18 ]) | |
253 | 19 let |
244 | 20 # image from file |
315
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
302
diff
changeset
|
21 swiss = loadImage("flag.png") |
253 | 22 |
244 | 23 var sampler = DefaultSampler() |
24 sampler.magnification = VK_FILTER_NEAREST | |
25 sampler.minification = VK_FILTER_NEAREST | |
315
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
302
diff
changeset
|
26 scene.addMaterial(Material(name:"material1", textures: { |
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
302
diff
changeset
|
27 "swissflag": Texture(image: swiss, sampler: sampler), |
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
302
diff
changeset
|
28 "thaiflag": Texture(image: thai, sampler: sampler), |
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
302
diff
changeset
|
29 }.toTable)) |
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
302
diff
changeset
|
30 scene.addMaterial(Material(name:"material2", textures: { |
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
302
diff
changeset
|
31 "swissflag": Texture(image: thai, sampler: sampler), |
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
302
diff
changeset
|
32 "thaiflag": Texture(image: swiss, sampler: sampler), |
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
302
diff
changeset
|
33 }.toTable)) |
230 | 34 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
|
35 |
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
36 var engine = initEngine("Test materials") |
253 | 37 |
199
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
38 const |
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
39 vertexInput = @[ |
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
40 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
|
41 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
|
42 ] |
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
43 vertexOutput = @[attr[Vec2f]("uvout")] |
230 | 44 uniforms = @[attr[float32]("test2", arrayCount=2)] |
315
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
302
diff
changeset
|
45 samplers = @[ |
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
302
diff
changeset
|
46 attr[Sampler2DType]("swissflag", arrayCount=2), |
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
302
diff
changeset
|
47 attr[Sampler2DType]("thaiflag", arrayCount=2), |
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
302
diff
changeset
|
48 ] |
199
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
49 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
|
50 vertexCode = compileGlslShader( |
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
51 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
|
52 inputs=vertexInput, |
201 | 53 uniforms=uniforms, |
199
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
54 samplers=samplers, |
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
55 outputs=vertexOutput, |
230 | 56 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
|
57 ) |
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
58 fragmentCode = compileGlslShader( |
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
59 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
|
60 inputs=vertexOutput, |
201 | 61 uniforms=uniforms, |
199
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
62 samplers=samplers, |
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
63 outputs=fragOutput, |
201 | 64 main=""" |
230 | 65 float d = sin(Uniforms.test2[0]) * 0.5 + 0.5; |
315
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
302
diff
changeset
|
66 color = texture(swissflag[1], uvout) * (1 - d) + texture(thaiflag[1], uvout) * d; |
201 | 67 """ |
199
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 engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode)) |
315
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
302
diff
changeset
|
70 engine.addScene(scene, vertexInput, samplers, transformAttribute="", materialIndexAttribute="") |
201 | 71 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
|
72 while engine.updateInputs() == Running and not engine.keyIsDown(Escape): |
230 | 73 var d = float32(cpuTime() - t) |
74 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
|
75 engine.renderScene(scene) |
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
76 engine.destroy() |
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
77 |
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
78 |
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
79 when isMainModule: |
da68fe12f600
add: new test file to test to do future test with materials etc.
Sam <sam@basx.dev>
parents:
diff
changeset
|
80 main() |