Created
September 12, 2017 00:40
-
-
Save brnkhy/e578cee30ad43a07c0db2919da7dbfc0 to your computer and use it in GitHub Desktop.
Billboard Modifier
This file contains hidden or 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
namespace Mapbox.Unity.MeshGeneration.Modifiers | |
{ | |
using Mapbox.Unity.MeshGeneration.Components; | |
using UnityEngine; | |
[CreateAssetMenu(menuName = "Mapbox/Modifiers/Wall Billboard Modifier")] | |
public class WallBillboardModifier : GameObjectModifier | |
{ | |
public Material[] mat; | |
[SerializeField] | |
private float _minWallLength = 10; | |
[SerializeField] | |
private float _minWallHeight = 10; | |
[SerializeField] | |
bool _enableAutomatically; | |
[SerializeField] | |
string _layer; | |
public float SizeX = .5f; | |
public float SizeY = .5f; | |
public float PosX = .5f; | |
public float PosY = .5f; | |
public override void Run(FeatureBehaviour fb) | |
{ | |
if (fb.Data == null) | |
return; | |
var scale = fb.transform.lossyScale.x; | |
var pointList = fb.Data.Points; | |
var ad = fb.gameObject.AddComponent<AnchorData>(); | |
var height = 3f; | |
var verts = fb.GetComponent<MeshFilter>().mesh.vertices; | |
foreach (var item in verts) | |
{ | |
if (height < item.y) | |
height = item.y; | |
} | |
foreach (var points in pointList) | |
{ | |
for (int i = 0; i < points.Count; i++) | |
{ | |
var v1 = points[i]; | |
var v2 = points[(i + 1) % points.Count]; | |
var dif = v2 - v1; | |
if (dif.magnitude > _minWallLength && height > _minWallHeight) | |
{ | |
var minx = PosX - SizeX / 2; | |
var maxx = PosX + SizeX / 2; | |
var miny = PosY - SizeY / 2; | |
var maxy = PosY + SizeY / 2; | |
var dir = new Vector3(-dif.z, 0, dif.x).normalized * .1f; | |
ad.Vertices.Add((dir + Vector3.Lerp(v1, v2, minx) + new Vector3(0, height * maxy, 0))); | |
ad.Vertices.Add((dir + Vector3.Lerp(v1, v2, maxx) + new Vector3(0, height * maxy, 0))); | |
ad.Vertices.Add((dir + Vector3.Lerp(v1, v2, maxx) + new Vector3(0, height * miny, 0))); | |
ad.Vertices.Add((dir + Vector3.Lerp(v1, v2, minx) + new Vector3(0, height * miny, 0))); | |
} | |
} | |
} | |
ad.Layer = LayerMask.NameToLayer(_layer); | |
ad.Materials = mat; | |
if (_enableAutomatically) | |
{ | |
ad.CreateGameObject(_enableAutomatically); | |
} | |
} | |
} | |
} |
This file contains hidden or 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; | |
public class AnchorData : MonoBehaviour, ITouchable | |
{ | |
public int Layer; | |
public Material[] Materials; | |
public List<Vector3> Vertices = new List<Vector3>(); | |
List<GameObject> _visibleObjects = new List<GameObject>(); | |
float _currentAlpha; | |
Coroutine _fadeRoutine; | |
public string Description | |
{ | |
get | |
{ | |
return ""; | |
} | |
} | |
public void Activate() | |
{ | |
foreach (var go in _visibleObjects) | |
{ | |
go.SetActive(true); | |
} | |
} | |
public void CreateGameObject(bool enabledAutomatically) | |
{ | |
for (int i = 0; i < Vertices.Count; i += 4) | |
{ | |
var pos = transform.position + transform.lossyScale.x * (transform.rotation * (Vertices[i] + Vertices[i + 1] + Vertices[i + 2] + Vertices[i + 3]) / 4); | |
var norm = transform.rotation * Vector3.Cross(Vertices[i + 1] - Vertices[i], Vertices[i + 2] - Vertices[i + 1]); | |
var go = new GameObject(); | |
go.AddComponent<Canvas>(); | |
go.layer = Layer; | |
go.transform.SetParent(transform, false); | |
go.transform.position = pos; | |
go.transform.LookAt(pos + norm); | |
var width = (Vertices[i + 1] - Vertices[i]).magnitude; | |
var height = (Vertices[i + 2] - Vertices[i + 1]).magnitude; | |
//var min = Mathf.Min(width / 100, height / 100); | |
//go.transform.localScale = new Vector3(min, min); | |
(go.transform as RectTransform).sizeDelta = new Vector2(width, height); | |
} | |
} | |
IEnumerator FadeIn() | |
{ | |
var materials = new List<Material>(); | |
foreach (var mesh in _visibleObjects) | |
{ | |
materials.Add(mesh.GetComponent<MeshRenderer>().material); | |
} | |
while (_currentAlpha < 1f) | |
{ | |
_currentAlpha += .04f; | |
foreach (var material in materials) | |
{ | |
var color = material.color; | |
color.a = _currentAlpha; | |
material.color = color; | |
} | |
yield return null; | |
} | |
_fadeRoutine = null; | |
} | |
IEnumerator FadeOut() | |
{ | |
var materials = new List<Material>(); | |
foreach (var mesh in _visibleObjects) | |
{ | |
materials.Add(mesh.GetComponent<MeshRenderer>().material); | |
} | |
while (_currentAlpha > 0f) | |
{ | |
_currentAlpha -= .04f; | |
foreach (var material in materials) | |
{ | |
var color = material.color; | |
color.a = _currentAlpha; | |
material.color = color; | |
} | |
yield return null; | |
} | |
_fadeRoutine = null; | |
} | |
public void Deactivate() | |
{ | |
if (_fadeRoutine != null) | |
{ | |
StopCoroutine(_fadeRoutine); | |
} | |
_fadeRoutine = StartCoroutine(FadeOut()); | |
} | |
public void Focus(Vector3 cameraForward) | |
{ | |
if (_visibleObjects == null || _visibleObjects.Count < 1) | |
{ | |
CreateGameObject(true); | |
} | |
if (_fadeRoutine != null) | |
{ | |
StopCoroutine(_fadeRoutine); | |
} | |
_fadeRoutine = StartCoroutine(FadeIn()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment