Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: migrating simple (flat mesh w/sprites) graphics from Unity to SFML.NET  (Read 1233 times)

0 Members and 1 Guest are viewing this topic.

sershe

  • Newbie
  • *
  • Posts: 1
    • View Profile
Hi.
I am pondering a migration of my project in early stages of development from Unity to WPF+SFML.NET (I've found a page claiming those go together ;)), because other than convenient graphics Unity is not to my liking.
I have hexagonal map drawing implemented in Unity, using a mesh with 4 triangles for each hex, "textured" using a spritesheet and UV coordinate magic; zooming and scrolling using an orthographic camera; and clicking on the map to select a hex.

What would be a good tutorial/equivalent classes to read documentation for, to port this code to SFML.NET?
Or if there's some code for an existing mesh drawing that is well abstracted somewhere, or in a tutorial, it would be very helpful as a guide to porting for me.

If it helps, with all the fluff removed my code basically looks like this - there are some constants (hexes are very repetitive ;)) , a loop where I generate the mesh data, then I assign it to unity MeshRenderer/Collider/etc. and it gets rendered. The last part is what I'm interested in, and the camera over that.

    Mesh hexMesh = meshRenderer.mesh; // Unity magic that I want to replace ;)
    hexMesh.clear();
    // Call addToRender for all cells
    meshRenderer.material.mainTexture = spriteSheetTexture; // More unity magic.
    hexMesh.triangles = triangles; // I generate 4 triangles from the 6 vertices for each hex
    hexMesh.vertices = vertices; hexMesh.uv = uvs; hexMesh.color;
   

    private void addToRender(HexCell cell, int cellIx, Color32 color) {
        float spriteX = spriteXIx * HEX_SIZE_X_FLOAT, spriteY = spriteYIx * HEX_SIZE_Y_FLOAT;
        for (int i = 0; i < 6; ++i) {
            vertices[cellIx * 6 + i] = cell.position + HexMetrics.CORNERS[i];
            colors[cellIx * 6 + i] = color;
            uvs[cellIx * 6 + i] = new Vector2( spriteX + CORNER_UV[i].x, spriteY + CORNER_UV[i].y);        }}}

        // Constants like this, not really Unity specific.
        // UV offsets  for each corner for a hex, from the corner of a given sprite in a spritesheet arranged as a grid of xSprites x yStripes rectangular sprites.
    float HEX_SIZE_X_FLOAT = 1f / xSprites, HEX_SIZE_Y_FLOAT = 1f / yStripes;
    CORNER_UV = new Vector2[6] { new Vector2(0.5f * HEX_SIZE_X_FLOAT, HEX_SIZE_Y_FLOAT), new Vector2(HEX_SIZE_X_FLOAT, 0.75f * HEX_SIZE_Y_FLOAT), ... };
    // Hex sizes in world coordinates
   public const float outerRadius = 10f, innerRadius = outerRadius * 0.866025404f;
    // Hex corner offsets from the central position.
    CORNERS = new Vector3[6] { new Vector3(0f, 0f, outerRadius), new Vector3(innerRadius, 0f, 0.5f * outerRadius), ... };
 
« Last Edit: March 06, 2017, 10:43:11 am by sershe »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10998
    • View Profile
    • development blog
    • Email
migrating simple (flat mesh w/sprites) graphics from Unity to SFML.NET
« Reply #1 on: March 06, 2017, 12:20:03 pm »
Assuming you're operating in a 2D space, check out the vertex array.
If you're starting fresh with SFML it's a good idea to read all the tutorials (except maybe the system parts, since they are provided natively by .NET).
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/