diff --git a/Assets/Scripts/Layers/LayerTypes/GeoJSONLineLayer.cs b/Assets/Scripts/Layers/LayerTypes/GeoJSONLineLayer.cs index 33a8c8d2..e2e06360 100644 --- a/Assets/Scripts/Layers/LayerTypes/GeoJSONLineLayer.cs +++ b/Assets/Scripts/Layers/LayerTypes/GeoJSONLineLayer.cs @@ -18,6 +18,9 @@ public partial class GeoJSONLineLayer : LayerGameObject { public List SpawnedVisualisations = new(); + private bool randomizeColorPerFeature = false; + public bool RandomizeColorPerFeature { get => randomizeColorPerFeature; set => randomizeColorPerFeature = value; } + [SerializeField] private LineRenderer3D lineRenderer3D; public LineRenderer3D LineRenderer3D @@ -30,7 +33,7 @@ public LineRenderer3D LineRenderer3D // Destroy(lineRenderer3D.gameObject); lineRenderer3D = value; } - } + } public override void OnLayerActiveInHierarchyChanged(bool activeInHierarchy) { @@ -46,6 +49,9 @@ public void AddAndVisualizeFeature(Feature feature, CoordinateSystem original var newFeatureVisualisation = new FeatureLineVisualisations() { feature = feature }; + // Create visual with random color if enabled + lineRenderer3D.LineMaterial = GetMaterialInstance(); + if (feature.Geometry is MultiLineString multiLineString) { var newLines = GeoJSONGeometryVisualizerUtility.VisualizeMultiLineString(multiLineString, originalCoordinateSystem, lineRenderer3D); @@ -60,6 +66,24 @@ public void AddAndVisualizeFeature(Feature feature, CoordinateSystem original SpawnedVisualisations.Add(newFeatureVisualisation); } + private Material GetMaterialInstance() + { + Material featureMaterialInstance; + // Create material with random color if randomize per feature is enabled + if (RandomizeColorPerFeature) + { + var randomColor = UnityEngine.Random.ColorHSV(); + randomColor.a = LayerData.Color.a; + + featureMaterialInstance = new Material(lineRenderer3D.LineMaterial) { color = randomColor }; + return featureMaterialInstance; + } + + // Default to material with layer color + featureMaterialInstance = new Material(lineRenderer3D.LineMaterial) { color = LayerData.Color }; + return featureMaterialInstance; + } + /// /// Checks the Bounds of the visualisations and checks them against the camera frustum /// to remove visualisations that are out of view diff --git a/Assets/Scripts/Layers/LayerTypes/GeoJSONPointLayer.cs b/Assets/Scripts/Layers/LayerTypes/GeoJSONPointLayer.cs index 55901d9b..c2f78cac 100644 --- a/Assets/Scripts/Layers/LayerTypes/GeoJSONPointLayer.cs +++ b/Assets/Scripts/Layers/LayerTypes/GeoJSONPointLayer.cs @@ -15,6 +15,9 @@ public partial class GeoJSONPointLayer : LayerGameObject { public List SpawnedVisualisations = new(); + private bool randomizeColorPerFeature = false; + public bool RandomizeColorPerFeature { get => randomizeColorPerFeature; set => randomizeColorPerFeature = value; } + [SerializeField] private BatchedMeshInstanceRenderer pointRenderer3D; public BatchedMeshInstanceRenderer PointRenderer3D @@ -28,7 +31,7 @@ public BatchedMeshInstanceRenderer PointRenderer3D pointRenderer3D = value; } } - + public override void OnLayerActiveInHierarchyChanged(bool activeInHierarchy) { pointRenderer3D.gameObject.SetActive(activeInHierarchy); @@ -42,7 +45,10 @@ public void AddAndVisualizeFeature(Feature feature, CoordinateSystem original return; var newFeatureVisualisation = new FeaturePointVisualisations() { feature = feature }; - + + // Create visual with random color if enabled + pointRenderer3D.Material = GetMaterialInstance(); + if (feature.Geometry is MultiPoint multiPoint) { var newPointCollection = GeoJSONGeometryVisualizerUtility.VisualizeMultiPoint(multiPoint, originalCoordinateSystem, PointRenderer3D); @@ -57,7 +63,25 @@ public void AddAndVisualizeFeature(Feature feature, CoordinateSystem original SpawnedVisualisations.Add(newFeatureVisualisation); } - /// + private Material GetMaterialInstance() + { + Material featureMaterialInstance; + // Create material with random color if randomize per feature is enabled + if (RandomizeColorPerFeature) + { + var randomColor = UnityEngine.Random.ColorHSV(); + randomColor.a = LayerData.Color.a; + + featureMaterialInstance = new Material(pointRenderer3D.Material) { color = randomColor }; + return featureMaterialInstance; + } + + // Default to material with layer color + featureMaterialInstance = new Material(pointRenderer3D.Material) { color = LayerData.Color }; + return featureMaterialInstance; + } + + /// /// Checks the Bounds of the visualisations and checks them against the camera frustum /// to remove visualisations that are out of view /// diff --git a/Assets/Scripts/Layers/LayerTypes/GeoJsonLayerGameObject.cs b/Assets/Scripts/Layers/LayerTypes/GeoJsonLayerGameObject.cs index 1fddb69d..3ff16182 100644 --- a/Assets/Scripts/Layers/LayerTypes/GeoJsonLayerGameObject.cs +++ b/Assets/Scripts/Layers/LayerTypes/GeoJsonLayerGameObject.cs @@ -46,12 +46,16 @@ public class GeoJsonLayerGameObject : LayerGameObject, ILayerWithPropertyData LayerPropertyData ILayerWithPropertyData.PropertyData => urlPropertyData; protected virtual void Awake() + { + LoadDefaultValues(); + } + + protected virtual void LoadDefaultValues() { //GeoJSON layer+visual colors are set to random colors until user can pick colors in UI var randomLayerColor = Color.HSVToRGB(UnityEngine.Random.value, UnityEngine.Random.Range(0.5f, 1f), 1); randomLayerColor.a = 0.5f; LayerData.Color = randomLayerColor; - } ///