Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ namespace Mapbox.Unity.MeshGeneration.Components
public class FeatureBehaviour : MonoBehaviour
{
public Transform Transform { get; set; }
public GameObject GameObject { get; set; }
public VectorFeatureUnity Data;

[Multiline(10)]
Expand All @@ -16,7 +15,6 @@ public class FeatureBehaviour : MonoBehaviour
public void Init(VectorFeatureUnity feature)
{
Transform = transform;
GameObject = gameObject;
Data = feature;
DataString = string.Join(" \r\n ", Data.Properties.Select(x => x.Key + " - " + x.Value.ToString()).ToArray());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Mapbox.Unity.MeshGeneration.Interfaces
public class TypeVisualizerTuple
{
public string Type;
public ModifierStack Stack;
public ModifierStackBase Stack;
}

[CreateAssetMenu(menuName = "Mapbox/Layer Visualizer/Vector Layer Visualizer")]
Expand All @@ -38,7 +38,7 @@ public override string Key
private List<FilterBase> Filters;

[SerializeField]
private ModifierStack _defaultStack;
private ModifierStackBase _defaultStack;
[SerializeField]
private List<TypeVisualizerTuple> Stacks;

Expand Down Expand Up @@ -71,6 +71,21 @@ public override void Create(VectorTileLayer layer, UnityTile tile)
if (!filterOut)
Build(feature, tile, _container);
}

var mergedStack = _defaultStack as MergedModifierStack;
if (mergedStack != null)
{
mergedStack.End(tile, _container);
}

for (int i = 0; i < Stacks.Count; i++)
{
mergedStack = Stacks[i].Stack as MergedModifierStack;
if (mergedStack != null)
{
mergedStack.End(tile, _container);
}
}
}

private void Build(VectorFeatureUnity feature, UnityTile tile, GameObject parent)
Expand Down Expand Up @@ -161,12 +176,12 @@ private void Build(VectorFeatureUnity feature, UnityTile tile, GameObject parent
GameObject go;
if (mod != null)
{
go = mod.Stack.Execute(feature, meshData, parent, mod.Type);
go = mod.Stack.Execute(tile, feature, meshData, parent, mod.Type);
}
else
{
if (_defaultStack != null)
go = _defaultStack.Execute(feature, meshData, parent, _key);
go = _defaultStack.Execute(tile, feature, meshData, parent, _key);
}
//go.layer = LayerMask.NameToLayer(_key);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class TextureModifier : GameObjectModifier

public override void Run(FeatureBehaviour fb)
{
var ts = fb.GameObject.AddComponent<TextureSelector>();
var ts = fb.gameObject.AddComponent<TextureSelector>();
ts.Initialize(fb, _textureTop, _useSatelliteTexture, _topMaterials, _textureSides, _sideMaterials);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System;
using Mapbox.Map;
using Mapbox.Unity.MeshGeneration.Modifiers;
using Mapbox.Unity.MeshGeneration.Data;
using Mapbox.Unity.MeshGeneration.Components;

namespace Mapbox.Unity.MeshGeneration.Modifiers
{
[CreateAssetMenu(menuName = "Mapbox/Modifiers/Merged Modifier Stack")]
public class MergedModifierStack : ModifierStackBase
{
public List<MeshModifier> MeshModifiers;
public List<GameObjectModifier> GoModifiers;

private Dictionary<UnityTile, int> _cacheVertexCount = new Dictionary<UnityTile, int>();
private Dictionary<UnityTile, List<MeshData>> _cached = new Dictionary<UnityTile, List<MeshData>>();
private Dictionary<UnityTile, int> _buildingCount = new Dictionary<UnityTile, int>();

public override GameObject Execute(UnityTile tile, VectorFeatureUnity feature, MeshData meshData, GameObject parent = null, string type = "")
{
if (!_cacheVertexCount.ContainsKey(tile))
{
_cacheVertexCount.Add(tile, 0);
_cached.Add(tile, new List<MeshData>());
_buildingCount.Add(tile, 0);
}

_buildingCount[tile]++;
foreach (MeshModifier mod in MeshModifiers.Where(x => x.Active))
{
mod.Run(feature, meshData);
}

GameObject go = null;
if (_cacheVertexCount[tile] < 60000)
{
_cacheVertexCount[tile] += meshData.Vertices.Count();
_cached[tile].Add(meshData);
}
else
{
go = End(tile, parent);
}

return go;
}

private GameObject CreateGameObject(MeshData data, GameObject main)
{
//Debug.Log("building count " + _buildingCount[tile]);
var go = new GameObject();
var mesh = go.AddComponent<MeshFilter>().mesh;
var rend = go.AddComponent<MeshRenderer>();
mesh.subMeshCount = data.Triangles.Count;

mesh.SetVertices(data.Vertices);
for (int i = 0; i < data.Triangles.Count; i++)
{
var triangle = data.Triangles[i];
mesh.SetTriangles(triangle, i);
}

for (int i = 0; i < data.UV.Count; i++)
{
var uv = data.UV[i];
mesh.SetUVs(i, uv);
}

mesh.RecalculateNormals();
go.transform.SetParent(main.transform, false);

var fb = go.AddComponent<FeatureBehaviour>();
foreach (GameObjectModifier mod in GoModifiers.Where(x => x.Active))
{
mod.Run(fb);
}

return go;
}

public GameObject End(UnityTile tile, GameObject parent)
{

var md = new MeshData();
md.UV = new List<List<Vector2>>() { new List<Vector2>(), new List<Vector2>() };
md.Triangles = new List<List<int>>() { new List<int>(), new List<int>() };
if (_cached.ContainsKey(tile))
{
foreach (var item in _cached[tile].Where(x => x.Vertices.Count > 3))
{
var st = md.Vertices.Count;
md.Vertices.AddRange(item.Vertices);
md.UV[0].AddRange(item.UV[0]);
if (item.UV.Count > 1)
md.UV[1].AddRange(item.UV[1]);
for (int i = 0; i < item.Triangles.Count; i++)
{
md.Triangles[i].AddRange(item.Triangles[i].Select(x => x + st));
}
}

if (md.Vertices.Count > 3)
{
GameObject go = null;
go = CreateGameObject(md, parent);
_cacheVertexCount[tile] = 0;
_cached[tile].Clear();
return go;
}

}
return null;
}
}
}

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

Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ namespace Mapbox.Unity.MeshGeneration.Modifiers
using Mapbox.Unity.MeshGeneration.Components;

[CreateAssetMenu(menuName = "Mapbox/Modifiers/Modifier Stack")]
public class ModifierStack : ScriptableObject
public class ModifierStack : ModifierStackBase
{
public List<MeshModifier> MeshModifiers;
public List<GameObjectModifier> GoModifiers;

public GameObject Execute(VectorFeatureUnity feature, MeshData meshData, GameObject parent = null, string type = "")
public override GameObject Execute(UnityTile tile, VectorFeatureUnity feature, MeshData meshData, GameObject parent = null, string type = "")
{
foreach (MeshModifier mod in MeshModifiers.Where(x => x.Active))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using UnityEngine;
using System.Collections;
using Mapbox.Map;
using Mapbox.Unity.MeshGeneration.Data;

namespace Mapbox.Unity.MeshGeneration.Modifiers
{
public abstract class ModifierStackBase : ScriptableObject
{
public abstract GameObject Execute(UnityTile tile, VectorFeatureUnity feature, MeshData meshData, GameObject parent = null, string type = "");
}
}

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

4 changes: 2 additions & 2 deletions sdkproject/Assets/Mapbox/Examples/Drive/Drive.unity
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ RenderSettings:
m_ReflectionIntensity: 1
m_CustomReflection: {fileID: 0}
m_Sun: {fileID: 0}
m_IndirectSpecularColor: {r: 0.44692504, g: 0.4967869, b: 0.57508504, a: 1}
m_IndirectSpecularColor: {r: 0.44692492, g: 0.4967869, b: 0.57508546, a: 1}
--- !u!157 &3
LightmapSettings:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -185,7 +185,7 @@ MonoBehaviour:
_snapYToZero: 1
LatLng: 37.781169, -122.409955
Zoom: 16
Range: {x: 1, y: 1, z: 1, w: 1}
Range: {x: 3, y: 3, z: 3, w: 3}
--- !u!4 &199863781
Transform:
m_ObjectHideFlags: 0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: ef7239c46ddb6ab469994291c9603c01, type: 3}
m_Name: DriveMergedBuildingStack
m_EditorClassIdentifier:
MeshModifiers:
- {fileID: 11400000, guid: 8d03f81530c3c1f4f9b1df48c9480748, type: 2}
- {fileID: 11400000, guid: 43492f8eaa132d044995aecfc9c08b6e, type: 2}
- {fileID: 11400000, guid: ead6d9ed3e6317b47a3719e7dfad3bcf, type: 2}
GoModifiers:
- {fileID: 11400000, guid: d507e2b14130f4a4aaa9433f44862e33, type: 2}

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

Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ MonoBehaviour:
m_Name: DriveBuildingVisualizer
m_EditorClassIdentifier:
Active: 1
SmoothMeshes: 0
_classificationKey:
_key: building
Filters: []
_defaultStack: {fileID: 11400000, guid: 15f2d806a81251046b3bb4b9446adc4c, type: 2}
_defaultStack: {fileID: 11400000, guid: 571ce5811f4b94246b9d4c96499d61b7, type: 2}
Stacks:
- Type: default
Stack: {fileID: 11400000, guid: 15f2d806a81251046b3bb4b9446adc4c, type: 2}
Stack: {fileID: 11400000, guid: 571ce5811f4b94246b9d4c96499d61b7, type: 2}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ MonoBehaviour:
m_Name: DriveTrafficVisualizer
m_EditorClassIdentifier:
Active: 1
SmoothMeshes: 0
_classificationKey: congestion
_key: traffic
Filters: []
Expand Down