Created
June 8, 2020 16:21
-
-
Save melMass/51df91f4880fac4f07dfb6ee3217a403 to your computer and use it in GitHub Desktop.
BMesh Unity Sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using static BMesh; | |
public class ProceduralMesh : MonoBehaviour | |
{ | |
public float tilling | |
{ | |
get { return _tilling; } | |
set | |
{ | |
_tilling = value; | |
UpdateBMesh(); | |
} | |
} | |
[SerializeField] | |
private float _tilling = 1; | |
public int width | |
{ | |
get { return _width; } | |
set | |
{ | |
_width = value; | |
UpdateBMesh(); | |
} | |
} | |
[SerializeField] | |
private int _width = 15; | |
public int height | |
{ | |
get { return _height; } | |
set | |
{ | |
_height = value; | |
UpdateBMesh(); | |
} | |
} | |
[SerializeField] | |
private int _height = 15; | |
BMesh mesh; // We keep a reference to the BMesh | |
BMesh GenerateGrid() | |
{ | |
// Create a new empty mesh | |
BMesh bm = new BMesh(); | |
for (int j = 0; j < height; ++j) | |
{ | |
for (int i = 0; i < width; ++i) | |
{ | |
bm.AddVertex(i, 0, j); // vertex # i + j * w | |
if (i > 0 && j > 0) bm.AddFace(i + j * width, i - 1 + j * width, i - 1 + (j - 1) * width, i + (j - 1) * width); | |
} | |
} | |
return bm; | |
} | |
[ContextMenu("Update BMesh")] | |
public void UpdateBMesh() | |
{ | |
mesh = GenerateGrid(); | |
// Sin points | |
foreach (Vertex v in mesh.vertices) | |
{ | |
v.point.y = Mathf.Sin((v.point.x + v.point.z) * tilling); // Vertical displacement | |
} | |
// Set the current mesh filter to use our generated mesh | |
BMeshUnity.SetInMeshFilter(mesh, GetComponent<MeshFilter>()); | |
} | |
// Start is called before the first frame update | |
public void Start() | |
{ | |
UpdateBMesh(); | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
} | |
//! In the editor, draw some debug information about the mesh | |
private void OnDrawGizmos() | |
{ | |
Gizmos.matrix = transform.localToWorldMatrix; | |
if (mesh != null) BMeshUnity.DrawGizmos(mesh); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEditor; | |
[CustomEditor(typeof(ProceduralMesh))] | |
public class ProceduralMeshEditor : Editor | |
{ | |
SerializedProperty width; | |
SerializedProperty height; | |
SerializedProperty tilling; | |
ProceduralMesh pm; | |
void OnEnable() | |
{ | |
width = serializedObject.FindProperty("_width"); | |
height = serializedObject.FindProperty("_height"); | |
tilling = serializedObject.FindProperty("_tilling"); | |
pm = (ProceduralMesh)target; | |
} | |
public override void OnInspectorGUI() | |
{ | |
// base.OnInspectorGUI(); // draw the original ui code | |
serializedObject.Update(); | |
GUILayout.BeginHorizontal(); | |
EditorGUILayout.PropertyField(width); | |
EditorGUILayout.PropertyField(height); | |
GUILayout.EndHorizontal(); | |
EditorGUILayout.PropertyField(tilling); | |
pm.width = width.intValue; | |
pm.height = height.intValue; | |
pm.tilling = tilling.floatValue; | |
serializedObject.ApplyModifiedProperties(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment