comparison examples/E02_squares.nim @ 336:887ddc8d45fd

did: update examples to work with improved scenegraph/material api, notice removed complexity!
author Sam <sam@basx.dev>
date Tue, 05 Sep 2023 00:28:35 +0700
parents b145a05c2459
children c66503386e8b
comparison
equal deleted inserted replaced
335:f05b4bef44d1 336:887ddc8d45fd
1 import std/sequtils 1 import std/sequtils
2 import std/tables
2 import std/random 3 import std/random
3 4
4 import ../src/semicongine 5 import ../src/semicongine
5 6
6 7
40 indices[squareIndex * 2 + 0] = [uint16(vertIndex + 0), uint16(vertIndex + 1), uint16(vertIndex + 2)] 41 indices[squareIndex * 2 + 0] = [uint16(vertIndex + 0), uint16(vertIndex + 1), uint16(vertIndex + 2)]
41 indices[squareIndex * 2 + 1] = [uint16(vertIndex + 2), uint16(vertIndex + 3), uint16(vertIndex + 0)] 42 indices[squareIndex * 2 + 1] = [uint16(vertIndex + 2), uint16(vertIndex + 3), uint16(vertIndex + 0)]
42 43
43 44
44 const 45 const
45 inputs = @[ 46 shaderConfiguration = createShaderConfiguration(
46 attr[Vec3f]("position"), 47 inputs=[
47 attr[Vec4f]("color", memoryPerformanceHint=PreferFastWrite), 48 attr[Vec3f]("position"),
48 attr[uint32]("index"), 49 attr[Vec4f]("color", memoryPerformanceHint=PreferFastWrite),
49 ] 50 attr[uint32]("index"),
50 intermediate = @[attr[Vec4f]("outcolor")] 51 ],
51 uniforms = @[attr[float32]("time")] 52 intermediates=[attr[Vec4f]("outcolor")],
52 outputs = @[attr[Vec4f]("color")] 53 uniforms=[attr[float32]("time")],
53 (vertexCode, fragmentCode) = compileVertexFragmentShaderSet( 54 outputs=[attr[Vec4f]("color")],
54 inputs=inputs,
55 intermediate=intermediate,
56 outputs=outputs,
57 uniforms=uniforms,
58 vertexCode=""" 55 vertexCode="""
59 float pos_weight = index / 100.0; // add some gamma correction? 56 float pos_weight = index / 100.0; // add some gamma correction?
60 float t = sin(Uniforms.time * 0.5) * 0.5 + 0.5; 57 float t = sin(Uniforms.time * 0.5) * 0.5 + 0.5;
61 float v = min(1, max(0, pow(pos_weight - t, 2))); 58 float v = min(1, max(0, pow(pos_weight - t, 2)));
62 v = pow(1 - v, 3000); 59 v = pow(1 - v, 3000);
67 ) 64 )
68 var squaremesh = newMesh( 65 var squaremesh = newMesh(
69 positions=vertices, 66 positions=vertices,
70 indices=indices, 67 indices=indices,
71 colors=colors, 68 colors=colors,
69 material=Material(name: "default")
72 ) 70 )
73 setMeshData[uint32](squaremesh, "index", iValues.toSeq) 71 squaremesh[].initVertexAttribute("index", iValues.toSeq)
74 72
75 var myengine = initEngine("Squares") 73 var myengine = initEngine("Squares")
76 myengine.setRenderer(myengine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode)) 74 myengine.initRenderer({"default": shaderConfiguration}.toTable)
77 75
78 var scene = newScene("scene", newEntity("scene", [], newEntity("squares", {"mesh": Component(squaremesh)}))) 76 var scene = Scene(name: "scene", meshes: @[squaremesh])
79 myengine.addScene(scene, inputs, @[], transformAttribute="")
80 scene.addShaderGlobal("time", 0.0'f32) 77 scene.addShaderGlobal("time", 0.0'f32)
78 myengine.addScene(scene)
81 while myengine.updateInputs() == Running and not myengine.keyWasPressed(Escape): 79 while myengine.updateInputs() == Running and not myengine.keyWasPressed(Escape):
82 setShaderGlobal(scene, "time", getShaderGlobal[float32](scene, "time") + 0.0005'f) 80 scene.setShaderGlobal("time", getShaderGlobal[float32](scene, "time") + 0.0005'f)
83 myengine.renderScene(scene) 81 myengine.renderScene(scene)
84 82
85 myengine.destroy() 83 myengine.destroy()