Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Created March 13, 2023 05:46
Show Gist options
  • Select an option

  • Save unitycoder/6985a2ab030dcff42050ea7ebed53726 to your computer and use it in GitHub Desktop.

Select an option

Save unitycoder/6985a2ab030dcff42050ea7ebed53726 to your computer and use it in GitHub Desktop.
Extrude 2D Sprite into 3D Mesh
// https://forum.unity.com/threads/how-to-convert-2d-lines-in-to-a-same-exact-shape-of-a-3d-object.870823/#post-6809294
#region License and Information
/*****
* A simple sprite extruder. It creates a 3d object out of a texture2d. The
* used material has to use a cutout shader as the front and back face are
* represented by a single quad. The script just builds the extruded edges
* and aligns them with the front face
*
*
* Copyright (c) 2015 Bunny83
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
*****/
#endregion License and Information
using UnityEngine;
using System.Collections.Generic;
[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
public class ExtrudeSprite : MonoBehaviour
{
private enum Edge { top, left, bottom, right };
public Material spriteMat;
public int alphaTheshold = 0;
public float depth = 0.0625f;
private Color32[] m_Colors;
private int m_Width;
private int m_Height;
private List<Vector3> m_Vertices = new List<Vector3>();
private List<Vector3> m_Normals = new List<Vector3>();
private List<Vector2> m_TexCoords = new List<Vector2>();
private bool HasPixel(int aX, int aY)
{
return m_Colors[aX + aY * m_Width].a > alphaTheshold;
}
void AddQuad(Vector3 aFirstEdgeP1, Vector3 aFirstEdgeP2, Vector3 aSecondRelative, Vector3 aNormal, Vector2 aUV1, Vector2 aUV2, bool aFlipUVs)
{
m_Vertices.Add(aFirstEdgeP1);
m_Vertices.Add(aFirstEdgeP2);
m_Vertices.Add(aFirstEdgeP2 + aSecondRelative);
m_Vertices.Add(aFirstEdgeP1 + aSecondRelative);
m_Normals.Add(aNormal);
m_Normals.Add(aNormal);
m_Normals.Add(aNormal);
m_Normals.Add(aNormal);
if (aFlipUVs)
{
m_TexCoords.Add(new Vector2(aUV1.x, aUV1.y));
m_TexCoords.Add(new Vector2(aUV2.x, aUV1.y));
m_TexCoords.Add(new Vector2(aUV2.x, aUV2.y));
m_TexCoords.Add(new Vector2(aUV1.x, aUV2.y));
}
else
{
m_TexCoords.Add(new Vector2(aUV1.x, aUV1.y));
m_TexCoords.Add(new Vector2(aUV1.x, aUV2.y));
m_TexCoords.Add(new Vector2(aUV2.x, aUV2.y));
m_TexCoords.Add(new Vector2(aUV2.x, aUV1.y));
}
}
void AddEdge(int aX, int aY, Edge aEdge)
{
Vector2 size = new Vector2(1.0f / m_Width, 1.0f / m_Height);
Vector2 uv = new Vector3(aX * size.x, aY * size.y);
Vector2 P = uv - Vector2.one * 0.5f;
uv += size * 0.5f;
Vector2 P2 = P;
Vector3 normal;
if (aEdge == Edge.top)
{
P += size;
P2.y += size.y;
normal = Vector3.up;
}
else if (aEdge == Edge.left)
{
P.y += size.y;
normal = Vector3.left;
}
else if (aEdge == Edge.bottom)
{
P2.x += size.x;
normal = Vector3.down;
}
else
{
P2 += size;
P.x += size.x;
normal = Vector3.right;
}
AddQuad(P, P2, Vector3.forward * depth, normal, uv, uv, false);
}
void GenerateMesh()
{
Texture2D tex = spriteMat.mainTexture as Texture2D;
m_Colors = tex.GetPixels32();
m_Width = tex.width;
m_Height = tex.height;
// first point , second point , relative 3. P, normal, lower UV, Upper UV, flipUV
AddQuad(new Vector3(-0.5f, -0.5f, 0), new Vector3(-0.5f, 0.5f, 0), Vector3.right, Vector3.back, Vector2.zero, Vector2.one, false);
AddQuad(new Vector3(-0.5f, -0.5f, depth), new Vector3(0.5f, -0.5f, depth), Vector3.up, Vector3.forward, Vector2.zero, Vector2.one, true);
for (int y = 0; y < m_Height; y++) // bottom to top
{
for (int x = 0; x < m_Width; x++) // left to right
{
if (HasPixel(x, y))
{
if (x == 0 || !HasPixel(x - 1, y))
AddEdge(x, y, Edge.left);
if (x == m_Width - 1 || !HasPixel(x + 1, y))
AddEdge(x, y, Edge.right);
if (y == 0 || !HasPixel(x, y - 1))
AddEdge(x, y, Edge.bottom);
if (y == m_Height - 1 || !HasPixel(x, y + 1))
AddEdge(x, y, Edge.top);
}
}
}
var mesh = new Mesh();
mesh.vertices = m_Vertices.ToArray();
mesh.normals = m_Normals.ToArray();
mesh.uv = m_TexCoords.ToArray();
int[] quads = new int[m_Vertices.Count];
for (int i = 0; i < quads.Length; i++)
quads[i] = i;
mesh.SetIndices(quads, MeshTopology.Quads, 0);
GetComponent<MeshFilter>().sharedMesh = mesh;
}
void Start()
{
GenerateMesh();
}
}
@unitycoder

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment