Mercurial > games > semicongine
comparison tests/test_materials.nim @ 316:b145a05c2459
add: changing rendering system, not finished yet, also upgrading to Nim 2
| author | Sam <sam@basx.dev> |
|---|---|
| date | Mon, 07 Aug 2023 00:23:00 +0700 |
| parents | 4921ec86dcb4 |
| children | e4528d97a687 |
comparison
equal
deleted
inserted
replaced
| 315:4921ec86dcb4 | 316:b145a05c2459 |
|---|---|
| 3 | 3 |
| 4 import semicongine | 4 import semicongine |
| 5 | 5 |
| 6 proc main() = | 6 proc main() = |
| 7 var flag = rect() | 7 var flag = rect() |
| 8 flag.materials = @["material2"] | |
| 8 var scene = newScene("main", root=newEntity("rect", {"mesh": Component(flag)})) | 9 var scene = newScene("main", root=newEntity("rect", {"mesh": Component(flag)})) |
| 9 let (RT, WT, PT) = (hexToColorAlpha("A51931").asPixel, hexToColorAlpha("F4F5F8").asPixel, hexToColorAlpha("2D2A4A").asPixel) | 10 let (RT, WT, PT) = (hexToColorAlpha("A51931").asPixel, hexToColorAlpha("F4F5F8").asPixel, hexToColorAlpha("2D2A4A").asPixel) |
| 10 let | 11 let |
| 11 # image from memory | 12 # image from memory |
| 12 thai = Image(width: 7, height: 5, imagedata: @[ | 13 thai = Image(width: 7, height: 5, imagedata: @[ |
| 22 | 23 |
| 23 var sampler = DefaultSampler() | 24 var sampler = DefaultSampler() |
| 24 sampler.magnification = VK_FILTER_NEAREST | 25 sampler.magnification = VK_FILTER_NEAREST |
| 25 sampler.minification = VK_FILTER_NEAREST | 26 sampler.minification = VK_FILTER_NEAREST |
| 26 scene.addMaterial(Material(name:"material1", textures: { | 27 scene.addMaterial(Material(name:"material1", textures: { |
| 27 "swissflag": Texture(image: swiss, sampler: sampler), | 28 "tex1": Texture(image: swiss, sampler: sampler), |
| 28 "thaiflag": Texture(image: thai, sampler: sampler), | 29 "tex2": Texture(image: thai, sampler: sampler), |
| 29 }.toTable)) | 30 }.toTable)) |
| 30 scene.addMaterial(Material(name:"material2", textures: { | 31 scene.addMaterial(Material(name:"material2", textures: { |
| 31 "swissflag": Texture(image: thai, sampler: sampler), | 32 "tex1": Texture(image: thai, sampler: sampler), |
| 32 "thaiflag": Texture(image: swiss, sampler: sampler), | 33 "tex2": Texture(image: swiss, sampler: sampler), |
| 33 }.toTable)) | 34 }.toTable)) |
| 34 scene.addShaderGlobalArray("test2", @[0'f32, 0'f32]) | 35 scene.addShaderGlobalArray("test2", @[0'f32, 0'f32]) |
| 35 | 36 |
| 36 var engine = initEngine("Test materials") | 37 var engine = initEngine("Test materials") |
| 37 | 38 |
| 38 const | 39 const |
| 39 vertexInput = @[ | 40 vertexInput = @[ |
| 40 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead), | 41 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead), |
| 42 attr[uint16]("materialIndex", memoryPerformanceHint=PreferFastRead, perInstance=true), | |
| 41 attr[Vec2f]("uv", memoryPerformanceHint=PreferFastRead), | 43 attr[Vec2f]("uv", memoryPerformanceHint=PreferFastRead), |
| 42 ] | 44 ] |
| 43 vertexOutput = @[attr[Vec2f]("uvout")] | 45 vertexOutput = @[attr[Vec2f]("uvout"), attr[uint16]("materialIndexOut", noInterpolation=true)] |
| 44 uniforms = @[attr[float32]("test2", arrayCount=2)] | 46 uniforms = @[attr[float32]("test2", arrayCount=2)] |
| 45 samplers = @[ | 47 samplers = @[ |
| 46 attr[Sampler2DType]("swissflag", arrayCount=2), | 48 attr[Sampler2DType]("tex1", arrayCount=2), |
| 47 attr[Sampler2DType]("thaiflag", arrayCount=2), | 49 attr[Sampler2DType]("tex2", arrayCount=2), |
| 48 ] | 50 ] |
| 49 fragOutput = @[attr[Vec4f]("color")] | 51 fragOutput = @[attr[Vec4f]("color")] |
| 50 vertexCode = compileGlslShader( | 52 (vertexCode, fragmentCode) = compileVertexFragmentShaderSet( |
| 51 stage=VK_SHADER_STAGE_VERTEX_BIT, | |
| 52 inputs=vertexInput, | 53 inputs=vertexInput, |
| 54 intermediate=vertexOutput, | |
| 55 outputs=fragOutput, | |
| 56 samplers=samplers, | |
| 53 uniforms=uniforms, | 57 uniforms=uniforms, |
| 54 samplers=samplers, | 58 vertexCode=""" |
| 55 outputs=vertexOutput, | 59 gl_Position = vec4(position.x, position.y + sin(Uniforms.test2[1]) / Uniforms.test2[1] * 0.5, position.z, 1.0); |
| 56 main="""gl_Position = vec4(position.x, position.y + sin(Uniforms.test2[1]) / Uniforms.test2[1] * 0.5, position.z, 1.0); uvout = uv;""" | 60 uvout = uv; |
| 57 ) | 61 materialIndexOut = materialIndex;""", |
| 58 fragmentCode = compileGlslShader( | 62 fragmentCode=""" |
| 59 stage=VK_SHADER_STAGE_FRAGMENT_BIT, | 63 float d = sin(Uniforms.test2[0]) * 0.5 + 0.5; |
| 60 inputs=vertexOutput, | 64 color = texture(tex1[materialIndexOut], uvout) * (1 - d) + texture(tex2[materialIndexOut], uvout) * d; |
| 61 uniforms=uniforms, | 65 """, |
| 62 samplers=samplers, | |
| 63 outputs=fragOutput, | |
| 64 main=""" | |
| 65 float d = sin(Uniforms.test2[0]) * 0.5 + 0.5; | |
| 66 color = texture(swissflag[1], uvout) * (1 - d) + texture(thaiflag[1], uvout) * d; | |
| 67 """ | |
| 68 ) | 66 ) |
| 69 engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode)) | 67 engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode)) |
| 70 engine.addScene(scene, vertexInput, samplers, transformAttribute="", materialIndexAttribute="") | 68 engine.addScene(scene, vertexInput, samplers, transformAttribute="") |
| 71 var t = cpuTime() | 69 var t = cpuTime() |
| 72 while engine.updateInputs() == Running and not engine.keyIsDown(Escape): | 70 while engine.updateInputs() == Running and not engine.keyIsDown(Escape): |
| 73 var d = float32(cpuTime() - t) | 71 var d = float32(cpuTime() - t) |
| 74 setShaderGlobalArray(scene, "test2", @[d, d * 2]) | 72 setShaderGlobalArray(scene, "test2", @[d, d * 2]) |
| 75 engine.renderScene(scene) | 73 engine.renderScene(scene) |
