Skip to content

Instantly share code, notes, and snippets.

@jpsarda
Created October 6, 2017 12:30
Show Gist options
  • Save jpsarda/86b4dbd0391e5cbc760ce33242e95ce3 to your computer and use it in GitHub Desktop.
Save jpsarda/86b4dbd0391e5cbc760ce33242e95ce3 to your computer and use it in GitHub Desktop.
Build dynamically a plane mesh from its 4 corners position in world and displaying an image filling the plane .
using FuretCompany.Utils;
using UnityEngine;
public class ImagePlane : MonoBehaviour
{
public string imageResourceName;
protected Vector3 _topLeft;
public Vector3 topLeft { get { return _topLeft; } set { if (value != _topLeft) { _topLeft = value; _meshDirty = true; } } }
protected Vector3 _topRight;
public Vector3 topRight { get { return _topRight; } set { if (value != _topRight) { _topRight = value; _meshDirty = true; } } }
protected Vector3 _bottomRight;
public Vector3 bottomRight { get { return _bottomRight; } set { if (value != _bottomRight) { _bottomRight = value; _meshDirty = true; } } }
protected Vector3 _bottomLeft;
public Vector3 bottomLeft { get { return _bottomLeft; } set { if (value != _bottomLeft) { _bottomLeft = value; _meshDirty = true; } } }
protected bool _meshDirty=false;
protected MeshRenderer meshRenderer;
protected Rigidbody rigidBody;
protected MeshFilter meshFilter;
//protected BoxCollider boxCollider;
protected MeshCollider meshCollider;
// Use this for initialization
void Start() {
if (AppManager.Instance.debugMode) Debug.Log("ImagePlane.Start " + imageResourceName + " topLeft=" + topLeft + " bottomRight=" + bottomRight);
// Grab the mesh renderer that's on the same object as this script.
//rigidBody =gameObject.AddComponent<Rigidbody>();
meshFilter=gameObject.AddComponent<MeshFilter>();
//boxCollider=gameObject.AddComponent<BoxCollider>();
meshCollider = gameObject.AddComponent<MeshCollider>();
meshRenderer =gameObject.AddComponent<MeshRenderer>();
meshFilter.mesh = CreateMesh();
_meshDirty = false;
meshCollider.sharedMesh = meshFilter.mesh;
meshCollider.convex = true;
meshCollider.inflateMesh = true;
meshRenderer.material.shader = Shader.Find("Particles/Additive");
Texture2D tex;
tex = Tools.LoadPNG(imageResourceName);
if (tex==null) tex = new Texture2D(1, 1);
tex.SetPixel(0, 0, Color.green);
tex.Apply();
meshRenderer.material.mainTexture = tex;
meshRenderer.material.color = Color.green;
}
void UpdateMesh()
{
meshFilter.mesh.vertices = new Vector3[] {
bottomLeft,
bottomRight,
topRight,
topLeft
};
meshFilter.mesh.RecalculateBounds();
meshCollider.sharedMesh = null;
meshCollider.sharedMesh = meshFilter.mesh;
//boxCollider.center = meshRenderer.bounds.center;
//boxCollider.size = meshRenderer.bounds.size;
_meshDirty = false;
//Debug.Log("UpdateMesh topLeft="+ topLeft+ " bottomRight=" + bottomRight);
}
Mesh CreateMesh()
{
Mesh m = new Mesh();
m.name = "ImagePlaneMesh";
m.vertices = new Vector3[] {
bottomLeft,
bottomRight,
topRight,
topLeft
};
m.uv = new Vector2[] {
new Vector2 (0, 0),
new Vector2 (1, 0),
new Vector2(1, 1),
new Vector2 (0,1)
};
m.triangles = new int[] { 0, 1, 2, 0, 2, 3 };
m.RecalculateNormals();
return m;
}
// Update is called once per frame
void Update() {
if (_meshDirty)
{
UpdateMesh();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment