Skip to content

Commit

Permalink
Added a default material for generated meshes. Added a ShapeEditorTar…
Browse files Browse the repository at this point in the history
…get MonoBehaviour that can receive built meshes.
  • Loading branch information
Henry00IS committed Dec 25, 2021
1 parent 5725ced commit ff07d9c
Show file tree
Hide file tree
Showing 18 changed files with 465 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ sysinfo.txt
# Builds
*.apk
*.unitypackage

# Substance
.autosave/
78 changes: 78 additions & 0 deletions Materials/ShapeEditorDefault.mat
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!21 &2100000
Material:
serializedVersion: 6
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: ShapeEditorDefault
m_Shader: {fileID: 46, guid: 0000000000000000f000000000000000, type: 0}
m_ShaderKeywords: _METALLICGLOSSMAP _NORMALMAP
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: -1
stringTagMap: {}
disabledShaderPasses: []
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _BumpMap:
m_Texture: {fileID: 2800000, guid: 785f3fb21ee25d643b81d536fcf61951, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailAlbedoMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailMask:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailNormalMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _EmissionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MainTex:
m_Texture: {fileID: 2800000, guid: 7a723bf287be9ea47801680e62258c8e, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MetallicGlossMap:
m_Texture: {fileID: 2800000, guid: 2178d4ef43869dc4b8916febd5b2b3d1, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _OcclusionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _ParallaxMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Floats:
- _BumpScale: 1
- _Cutoff: 0.5
- _DetailNormalMapScale: 1
- _DstBlend: 0
- _GlossMapScale: 1
- _Glossiness: 0.5
- _GlossyReflections: 1
- _Metallic: 0
- _Mode: 0
- _OcclusionStrength: 1
- _Parallax: 0.02
- _SmoothnessTextureChannel: 0
- _SpecularHighlights: 1
- _SrcBlend: 1
- _UVSec: 0
- _ZWrite: 1
m_Colors:
- _Color: {r: 1, g: 1, b: 1, a: 1}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
m_BuildTextureStacks: []
8 changes: 8 additions & 0 deletions Materials/ShapeEditorDefault.mat.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Resources/ShapeEditorResources.asset
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@ MonoBehaviour:
shapeEditorZoomOut: {fileID: 2800000, guid: 62299d3e26649b442aed157a222ab744, type: 3}
shapeEditorFontSegoeUI14Xml: {fileID: 4900000, guid: 96c44120173bdb24081e0676c015daee, type: 3}
shapeEditorFontSegoeUI14: {fileID: 2800000, guid: 4c29aef198600c443825fa46c0b76761, type: 3}
shapeEditorDefaultMaterial: {fileID: 2100000, guid: 10af3fe77d7a84a4d951b38b54533f7a, type: 2}
shapeEditorGridMaterial: {fileID: 2100000, guid: 021e83c5ba89e754d8fb2194fde8819e, type: 2}
shapeEditorGuiMaterial: {fileID: 2100000, guid: d1dffd627a249ca498ba91dd9d46daf2, type: 2}
27 changes: 27 additions & 0 deletions Scripts/Editor/ShapeEditorTarget.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#if UNITY_EDITOR

using UnityEngine;

namespace AeternumGames.ShapeEditor
{
/// <summary>
/// Represents a mesh target that, when selected, can receive meshes created by the 2D Shape Editor.
/// </summary>
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
public class ShapeEditorTarget : MonoBehaviour
{
public void OnShapeEditorMesh(Mesh mesh)
{
var meshFilter = GetComponent<MeshFilter>();
var meshRenderer = GetComponent<MeshRenderer>();
meshFilter.mesh = mesh;
if (!meshRenderer.sharedMaterial)
{
meshRenderer.sharedMaterial = ShapeEditorResources.Instance.shapeEditorDefaultMaterial;
}
}
}
}

#endif
11 changes: 11 additions & 0 deletions Scripts/Editor/ShapeEditorTarget.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions Scripts/Editor/ShapeEditorWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -386,9 +386,15 @@ internal void OnCreatePolygonMeshTest()
// generate a mesh.
var mesh = MeshGenerator.CreatePolygonMesh(convexPolygons);

var go = new GameObject("2D Shape Editor Polygon");
go.AddComponent<MeshFilter>().sharedMesh = mesh;
go.AddComponent<MeshRenderer>();//.material = ???
var transform = Selection.activeTransform;
if (transform)
{
var target = transform.GetComponent<ShapeEditorTarget>();
if (target)
{
target.OnShapeEditorMesh(mesh);
}
}
}
}
}
Expand Down
25 changes: 23 additions & 2 deletions Scripts/ShapeEditor/Generators/MeshGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ public static Mesh CreatePolygonMesh(List<Vertices> convexPolygons)
foreach (var polygon in convexPolygons)
{
var vertexCount = polygon.Count;
Debug.Log(vertexCount);

for (int i = 0; i < vertexCount; i++)
{
vertices.Add(new Vector3(polygon[i].x, -polygon[i].y));
Expand All @@ -44,9 +42,32 @@ public static Mesh CreatePolygonMesh(List<Vertices> convexPolygons)

mesh.SetVertices(vertices);
mesh.SetTriangles(triangles, 0);
mesh.SetUVs(0, GenerateUV0_SabreCSG(vertices));

mesh.RecalculateNormals();
mesh.RecalculateTangents();

return mesh;
}

private static Vector2[] GenerateUV0_SabreCSG(List<Vector3> vertices)
{
var results = new Vector2[vertices.Count];

var plane = new Plane(vertices[0], vertices[1], vertices[2]);

Vector3 planeNormal = plane.normal;
Quaternion cancellingRotation = Quaternion.Inverse(Quaternion.LookRotation(-planeNormal));
// Sets the UV at each point to the position on the plane
for (int i = 0; i < results.Length; i++)
{
Vector3 position = vertices[i];
Vector2 uv = new Vector3(0.5f, 0.5f, 0f) + (cancellingRotation * position);
results[i] = uv;
}

return results;
}
}
}

Expand Down
1 change: 1 addition & 0 deletions Scripts/Utilities/ShapeEditorResources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ private static void LoadResources()
public TextAsset shapeEditorFontSegoeUI14Xml;
public Texture2D shapeEditorFontSegoeUI14;

public Material shapeEditorDefaultMaterial;
public Material shapeEditorGridMaterial;
public Material shapeEditorGuiMaterial;

Expand Down
8 changes: 8 additions & 0 deletions Textures/Default.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Textures/Default/ShapeEditorDefault.sbs

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions Textures/Default/ShapeEditorDefault.sbs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Textures/Default/ShapeEditorDefaultColor.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
96 changes: 96 additions & 0 deletions Textures/Default/ShapeEditorDefaultColor.png.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Textures/Default/ShapeEditorDefaultMetallic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit ff07d9c

Please sign in to comment.