Mercurial > games > semicongine
changeset 348:67081fbaf636
did: a ton of small improvments
author | Sam <sam@basx.dev> |
---|---|
date | Sun, 17 Sep 2023 21:09:38 +0700 |
parents | 5c22302f1485 |
children | 7df73da85181 |
files | src/semicongine/animation.nim src/semicongine/core/color.nim src/semicongine/core/matrix.nim src/semicongine/mesh.nim src/semicongine/renderer.nim tests/test_matrix.nim |
diffstat | 6 files changed, 50 insertions(+), 30 deletions(-) [+] |
line wrap: on
line diff
--- a/src/semicongine/animation.nim Thu Sep 14 23:59:10 2023 +0700 +++ b/src/semicongine/animation.nim Sun Sep 17 21:09:38 2023 +0700 @@ -133,7 +133,7 @@ func stop*(player: var AnimationPlayer) = player.playing = false -func advance*[T](player: var AnimationPlayer[T], dt: float32) = +func advance*[T](player: var AnimationPlayer[T], dt: float32): T = # TODO: check this function, not 100% correct I think if player.playing: player.currentTime += float32(player.currentDirection) * dt @@ -153,3 +153,4 @@ player.currentTime += float32(player.currentDirection) * dt * 2'f32 player.currentValue = valueAt(player.animation, (abs(player.currentTime) / player.animation.duration) mod high(AnimationTime)) + return player.currentValue
--- a/src/semicongine/core/color.nim Thu Sep 14 23:59:10 2023 +0700 +++ b/src/semicongine/core/color.nim Sun Sep 17 21:09:38 2023 +0700 @@ -3,20 +3,6 @@ import ./vector -func hexToColor*(value: string): Vec3f = - assert value != "" - var hex = value - if hex[0] == '#': - hex = hex[1 .. ^0] - assert hex.len == 3 or hex.len == 6 - if hex.len == 3: - hex = hex[0] & hex[0] & hex[1] & hex[1] & hex[2] & hex[2] - var r, g, b: uint8 - assert hex.len == 6 - discard parseHex(hex[0 .. 1], r) == 2 - discard parseHex(hex[2 .. 3], g) == 2 - discard parseHex(hex[4 .. 5], b) == 2 - return Vec3f([float32(r), float32(g), float32(b)]) / 255'f func colorToHex*(color: Vec3f): string = &"{int(color.r * 255):02X}{int(color.g * 255):02X}{int(color.b * 255):02X}" @@ -29,11 +15,11 @@ func asPixel*(color: Vec4f): array[4, uint8] = [uint8(color.r * 255), uint8(color.g * 255), uint8(color.b * 255), uint8(color.a * 255)] -func hexToColorAlpha*(value: string): Vec4f = +func toRGBA*(value: string): Vec4f = assert value != "" var hex = value if hex[0] == '#': - hex = hex[1 .. ^0] + hex = hex[1 .. ^1] # when 3 or 6 -> set alpha to 1.0 assert hex.len == 3 or hex.len == 6 or hex.len == 4 or hex.len == 8 if hex.len == 3: @@ -52,3 +38,21 @@ func gamma*[T: Vec3f|Vec4f](color: T, gamma: float32): T = return pow(color, gamma) + +const + Black* = toRGBA "#000000FF" + Silver* = toRGBA "#C0C0C0FF" + Gray* = toRGBA "#808080FF" + White* = toRGBA "#FFFFFFFF" + Maroon* = toRGBA "#800000FF" + Red* = toRGBA "#FF0000FF" + Purple* = toRGBA "#800080FF" + Fuchsia* = toRGBA "#FF00FFFF" + Green* = toRGBA "#008000FF" + Lime* = toRGBA "#00FF00FF" + Olive* = toRGBA "#808000FF" + Yellow* = toRGBA "#FFFF00FF" + Navy* = toRGBA "#000080FF" + Blue* = toRGBA "#0000FFFF" + Teal* = toRGBA "#008080FF" + Aqua* = toRGBA "#00FFFFFF"
--- a/src/semicongine/core/matrix.nim Thu Sep 14 23:59:10 2023 +0700 +++ b/src/semicongine/core/matrix.nim Sun Sep 17 21:09:38 2023 +0700 @@ -412,7 +412,7 @@ result.data[i] = rand(low(typeof(m.data[0])) .. high(typeof(m.data[0]))) proc randomized*[T: SomeFloat](m: mattype[T]): mattype[T] = for i in 0 ..< result.data.len: - result.data[i] = rand(1.0) + result.data[i] = rand(T(1.0)) makeRandomInit(TMat2) makeRandomInit(TMat23)
--- a/src/semicongine/mesh.nim Thu Sep 14 23:59:10 2023 +0700 +++ b/src/semicongine/mesh.nim Sun Sep 17 21:09:38 2023 +0700 @@ -26,6 +26,7 @@ material*: Material transform*: Mat4 = Unit4 instanceTransforms*: seq[Mat4] + applyMeshTransformToInstances*: bool = true # if true, the transform attribute for the shader will apply the instance transform AND the mesh transform, to each instance visible*: bool = true transformCache: seq[Mat4] vertexData: Table[string, DataList] @@ -38,8 +39,8 @@ textures*: Table[string, Texture] index*: uint16 # optional, may be used to index into uniform arrays in shader -let EMPTY_MATERIAL = Material( - name: "empty material" +let DEFAULT_MATERIAL = Material( + name: "default material" ) func `$`*(mesh: MeshObject): string = @@ -125,7 +126,7 @@ uvs: openArray[Vec2f]=[], transform: Mat4=Unit4F32, instanceTransforms: openArray[Mat4]=[Unit4F32], - material: Material=EMPTY_MATERIAL, + material: Material=DEFAULT_MATERIAL, autoResize=true, ): Mesh = assert colors.len == 0 or colors.len == positions.len @@ -175,7 +176,7 @@ uvs: openArray[Vec2f]=[], transform: Mat4=Unit4F32, instanceTransforms: openArray[Mat4]=[Unit4F32], - material: Material=EMPTY_MATERIAL, + material: Material=DEFAULT_MATERIAL, ): Mesh = newMesh( positions=positions, @@ -311,7 +312,11 @@ of Big: mesh.bigIndices.add([uint32(v1), uint32(v2), uint32(v3)]) proc updateInstanceTransforms*(mesh: var MeshObject, attribute: string) = - let currentTransforms = mesh.instanceTransforms.mapIt(mesh.transform * it) + var currentTransforms: seq[Mat4] + if mesh.applyMeshTransformToInstances: + currentTransforms = mesh.instanceTransforms.mapIt(mesh.transform * it) + else: + currentTransforms = mesh.instanceTransforms if currentTransforms != mesh.transformCache: mesh[attribute] = currentTransforms mesh.transformCache = currentTransforms @@ -398,36 +403,37 @@ instanceTransforms: @[Unit4F32], indexType: Small, smallIndices: @[[0'u16, 1'u16, 2'u16], [2'u16, 3'u16, 0'u16]], + material: DEFAULT_MATERIAL ) let half_w = width / 2 half_h = height / 2 pos = @[newVec3f(-half_w, -half_h), newVec3f( half_w, -half_h), newVec3f( half_w, half_h), newVec3f(-half_w, half_h)] - c = hexToColorAlpha(color) + c = toRGBA(color) result[].initVertexAttribute("position", pos) result[].initVertexAttribute("color", @[c, c, c, c]) result[].initVertexAttribute("uv", @[newVec2f(0, 0), newVec2f(1, 0), newVec2f(1, 1), newVec2f(0, 1)]) proc tri*(width=1'f32, height=1'f32, color="ffffffff"): Mesh = - result = Mesh(vertexCount: 3, instanceTransforms: @[Unit4F32]) + result = Mesh(vertexCount: 3, instanceTransforms: @[Unit4F32], material: DEFAULT_MATERIAL) let half_w = width / 2 half_h = height / 2 - colorVec = hexToColorAlpha(color) + colorVec = toRGBA(color) result[].initVertexAttribute("position", @[newVec3f(0, -half_h), newVec3f( half_w, half_h), newVec3f(-half_w, half_h)]) result[].initVertexAttribute("color", @[colorVec, colorVec, colorVec]) proc circle*(width=1'f32, height=1'f32, nSegments=12, color="ffffffff"): Mesh = assert nSegments >= 3 - result = Mesh(vertexCount: 3 + nSegments, instanceTransforms: @[Unit4F32], indexType: Small) + result = Mesh(vertexCount: 3 + nSegments, instanceTransforms: @[Unit4F32], indexType: Small, material: DEFAULT_MATERIAL) let half_w = width / 2 half_h = height / 2 - c = hexToColorAlpha(color) + c = toRGBA(color) step = (2'f32 * PI) / float32(nSegments) var pos = @[newVec3f(0, 0), newVec3f(0, half_h)]
--- a/src/semicongine/renderer.nim Thu Sep 14 23:59:10 2023 +0700 +++ b/src/semicongine/renderer.nim Sun Sep 17 21:09:38 2023 +0700 @@ -425,10 +425,9 @@ for i in 0 ..< renderer.renderPass.subpasses.len: for materialName, pipeline in renderer.renderPass.subpasses[i].pipelines.pairs: if scene.usesMaterial(materialName): - debug &"Start pipeline for {materialName}" + debug &"Start pipeline for '{materialName}'" commandBuffer.vkCmdBindPipeline(renderer.renderPass.subpasses[i].pipelineBindPoint, pipeline.vk) commandBuffer.vkCmdBindDescriptorSets(renderer.renderPass.subpasses[i].pipelineBindPoint, pipeline.layout, 0, 1, addr(renderer.scenedata[scene].descriptorSets[pipeline.vk][renderer.swapchain.currentInFlight].vk), 0, nil) - for (drawable, mesh) in renderer.scenedata[scene].drawables.filterIt(it[1].visible and it[1].material.name == materialName): drawable.draw(commandBuffer, vertexBuffers=renderer.scenedata[scene].vertexBuffers, indexBuffer=renderer.scenedata[scene].indexBuffer, pipeline.vk)
--- a/tests/test_matrix.nim Thu Sep 14 23:59:10 2023 +0700 +++ b/tests/test_matrix.nim Sun Sep 17 21:09:38 2023 +0700 @@ -136,5 +136,15 @@ echo TMat3[float32]().randomized() * One3.randomized() echo TMat4[float32]().randomized() * One4.randomized() + echo float32(rand(1'f32)) * TMat2[float32]().randomized() + echo TMat2[float]().randomized() * rand(1'f) + echo TMat2[float]().randomized() * rand(1'f) + echo TMat23[float]().randomized() * rand(1'f) + echo TMat23[float]().randomized() * rand(1'f) + echo TMat3[float]().randomized() * rand(1'f) + echo TMat34[float]().randomized() * rand(1'f) + echo TMat43[float]().randomized() * rand(1'f) + echo TMat4[float]().randomized() * rand(1'f) + randomize() testMatrix()