1
Graphics / migrating simple (flat mesh w/sprites) graphics from Unity to SFML.NET
« on: March 06, 2017, 10:33:45 am »
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.
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), ... };
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), ... };