diff --git a/Assets/Prefabs/ObjImporter.prefab.meta b/Assets/Prefabs/ObjImporter.prefab.meta index 4ee63e84..5c226641 100644 --- a/Assets/Prefabs/ObjImporter.prefab.meta +++ b/Assets/Prefabs/ObjImporter.prefab.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 17813655602a70c46a78e7f4ec2e2b97 +guid: 465e8d834784b964eb02cea490bcf3a5 PrefabImporter: externalObjects: {} userData: diff --git a/Assets/Scenes/Main.unity b/Assets/Scenes/Main.unity index e039973c..6f68da95 100644 --- a/Assets/Scenes/Main.unity +++ b/Assets/Scenes/Main.unity @@ -520,7 +520,6 @@ MonoBehaviour: m_EditorClassIdentifier: arrowTransform: {fileID: 7732943800119484194} NorthColor: {r: 0.46540874, g: 0.106839046, b: 0.106839046, a: 1} - snapSpeed: 10 inputActionAsset: {fileID: -944628639613478452, guid: d5b2b155332606d44ac795422ed556b6, type: 3} --- !u!1001 &859080883 diff --git a/Assets/Scripts/Layers/LayerTypes/GeoJSONLineLayer.cs b/Assets/Scripts/Layers/LayerTypes/GeoJSONLineLayer.cs index 5f6470e7..7cd1c66f 100644 --- a/Assets/Scripts/Layers/LayerTypes/GeoJSONLineLayer.cs +++ b/Assets/Scripts/Layers/LayerTypes/GeoJSONLineLayer.cs @@ -35,15 +35,36 @@ public List GetMeshData(Feature feature) int[] triangles = new int[(points.Count - 2) * 3]; for (int i = 0; i < points.Count - 2; i++) { - triangles[i * 3] = 0; - triangles[i * 3 + 1] = i + 1; - triangles[i * 3 + 2] = i + 2; + triangles[i * 3] = 0; + triangles[i * 3 + 1] = i + 1; + triangles[i * 3 + 2] = i + 2; } - mesh.SetTriangles(triangles, 0); + mesh.SetTriangles(triangles, 0); } return meshes; } + /// + /// the previous colors of all vertices will be stored into the vertexColor buffer parameter + /// + /// + /// + public void SetVisualisationColor(List meshes, Color[] vertexColors) + { + int totalIndex = 0; + foreach(Mesh mesh in meshes) + { + Vector3[] vertices = mesh.vertices; + for (int i = 0; i < vertices.Length; i++) + { + Color previousColor; + lineRenderer3D.SetLineColorClosestToPoint(vertices[i], vertexColors[totalIndex], out previousColor); + vertexColors[totalIndex] = previousColor; + totalIndex++; + } + } + } + public List SpawnedVisualisations = new(); private bool randomizeColorPerFeature = false; diff --git a/Assets/Scripts/Layers/LayerTypes/GeoJSONPointLayer.cs b/Assets/Scripts/Layers/LayerTypes/GeoJSONPointLayer.cs index 5fc7179c..4bc681e0 100644 --- a/Assets/Scripts/Layers/LayerTypes/GeoJSONPointLayer.cs +++ b/Assets/Scripts/Layers/LayerTypes/GeoJSONPointLayer.cs @@ -146,5 +146,10 @@ public override void DestroyLayerGameObject() base.DestroyLayerGameObject(); } + + public void SetVisualisationColor(List meshes, Color[] previousColors) + { + + } } } \ No newline at end of file diff --git a/Assets/Scripts/Layers/LayerTypes/GeoJSONPolygonLayer.cs b/Assets/Scripts/Layers/LayerTypes/GeoJSONPolygonLayer.cs index c372adf4..0a7514e5 100644 --- a/Assets/Scripts/Layers/LayerTypes/GeoJSONPolygonLayer.cs +++ b/Assets/Scripts/Layers/LayerTypes/GeoJSONPolygonLayer.cs @@ -142,5 +142,10 @@ private void RemoveFeature(FeaturePolygonVisualisations featureVisualisation) featureVisualisation.DestroyAllVisualisations(); SpawnedVisualisations.Remove(featureVisualisation); } + + public void SetVisualisationColor(List meshes, Color[] previousColors) + { + + } } } \ No newline at end of file diff --git a/Assets/Scripts/Layers/LayerTypes/GeoJsonLayerGameObject.cs b/Assets/Scripts/Layers/LayerTypes/GeoJsonLayerGameObject.cs index 81147477..a23bfd2e 100644 --- a/Assets/Scripts/Layers/LayerTypes/GeoJsonLayerGameObject.cs +++ b/Assets/Scripts/Layers/LayerTypes/GeoJsonLayerGameObject.cs @@ -253,24 +253,24 @@ private void ProcessFeatureVisualisationForFeature(Feature feature) var polygonData = polygonFeaturesLayer?.GetMeshData(feature); if (polygonData != null) { - ProcessObjectMapping(feature, polygonData); + ProcessObjectMapping(polygonFeaturesLayer, feature, polygonData); } var lineData = lineFeaturesLayer?.GetMeshData(feature); if (lineData != null) { - ProcessObjectMapping(feature, lineData); + ProcessObjectMapping(lineFeaturesLayer, feature, lineData); } var pointData = pointFeaturesLayer?.GetMeshData(feature); if (pointData != null) { - ProcessObjectMapping(feature, pointData); + ProcessObjectMapping(pointFeaturesLayer, feature, pointData); } } private Dictionary featureMeshes = new Dictionary(); - private void ProcessObjectMapping(Feature feature, List meshes) + private void ProcessObjectMapping(IGeoJsonVisualisationLayer layer, Feature feature, List meshes) { GameObject parent; if(featureMeshes.ContainsKey(feature)) @@ -347,15 +347,10 @@ private void ProcessObjectMapping(Feature feature, List meshes) subObject.AddComponent().radius = 1f; subObject.transform.SetParent(parent.transform); - ObjectMapping objectMapping = subObject.AddComponent(); - objectMapping.items = new List(); - string id = feature.Id; - objectMapping.items.Add(new ObjectMappingItem() - { - objectID = id, - firstVertex = 0, - verticesLength = meshes[i].vertices.Length, - }); + FeatureMapping objectMapping = subObject.AddComponent(); + objectMapping.SetFeature(feature); + objectMapping.SetMeshes(meshes); + objectMapping.SetVisualisationLayer(layer); } } diff --git a/Assets/Scripts/Layers/LayerTypes/IGeoJsonVisualisationLayer.cs b/Assets/Scripts/Layers/LayerTypes/IGeoJsonVisualisationLayer.cs index bbed48db..caf836aa 100644 --- a/Assets/Scripts/Layers/LayerTypes/IGeoJsonVisualisationLayer.cs +++ b/Assets/Scripts/Layers/LayerTypes/IGeoJsonVisualisationLayer.cs @@ -9,5 +9,6 @@ namespace Netherlands3D.Twin public interface IGeoJsonVisualisationLayer { List GetMeshData(Feature feature); + void SetVisualisationColor(List meshes, Color[] previousColors); } } diff --git a/Assets/Scripts/LineRenderer3D.cs b/Assets/Scripts/LineRenderer3D.cs index 579dd3e2..4a7071dd 100644 --- a/Assets/Scripts/LineRenderer3D.cs +++ b/Assets/Scripts/LineRenderer3D.cs @@ -5,7 +5,6 @@ using Netherlands3D.Coordinates; using UnityEngine; using UnityEngine.Rendering; -using Random = UnityEngine.Random; namespace Netherlands3D.Twin { @@ -113,22 +112,29 @@ private void OnValidate() private void DrawLines() { + //for (var i = 0; i < segmentTransformMatrixCache.Count; i++) + //{ + // var lineTransforms = segmentTransformMatrixCache[i]; + // // if (hasColors) + // // { + // // MaterialPropertyBlock props = materialPropertyBlockCache[i]; + // // Graphics.DrawMeshInstanced(LineMesh, 0, LineMaterial, lineTransforms, props); + // // + // // if (DrawJoints) + // // Graphics.DrawMeshInstanced(JointMesh, 0, LineMaterial, lineJointTransforms, props); + // // + // // continue; + // // } + + // Graphics.DrawMeshInstanced(lineMesh, 0, LineMaterial, lineTransforms, null, ShadowCastingMode.Off, false, LayerMask.NameToLayer("Projected"), projectionCamera); + // // Graphics.DrawMeshInstanced(LineMesh, 0, LineMaterial, lineTransforms); + //} + + for (var i = 0; i < segmentTransformMatrixCache.Count; i++) { var lineTransforms = segmentTransformMatrixCache[i]; - // if (hasColors) - // { - // MaterialPropertyBlock props = materialPropertyBlockCache[i]; - // Graphics.DrawMeshInstanced(LineMesh, 0, LineMaterial, lineTransforms, props); - // - // if (DrawJoints) - // Graphics.DrawMeshInstanced(JointMesh, 0, LineMaterial, lineJointTransforms, props); - // - // continue; - // } - - Graphics.DrawMeshInstanced(lineMesh, 0, LineMaterial, lineTransforms, null, ShadowCastingMode.Off, false, LayerMask.NameToLayer("Projected"), projectionCamera); - // Graphics.DrawMeshInstanced(LineMesh, 0, LineMaterial, lineTransforms); + Graphics.DrawMeshInstanced(LineMesh, 0, LineMaterial, lineTransforms, hasColors ? materialPropertyBlockCache[i] : null, ShadowCastingMode.Off, false, LayerMask.NameToLayer("Projected"), projectionCamera); } if (DrawJoints) @@ -170,16 +176,17 @@ public int GetClosestLineIndex(Vector3 point) /// /// Set specific line color for the line closest to a given point. /// - public int SetLineColorClosestToPoint(Vector3 point, Color color) + public int SetLineColorClosestToPoint(Vector3 point, Color color, out Color previousColor) { int closestLineIndex = GetClosestLineIndex(point); if (closestLineIndex == -1) { Debug.LogWarning("No line found"); + previousColor = Color.clear; return -1; } - SetSpecificLineColorByIndex(closestLineIndex, color); + SetSpecificLineColorByIndex(closestLineIndex, color, out previousColor); return closestLineIndex; } @@ -187,15 +194,27 @@ public int SetLineColorClosestToPoint(Vector3 point, Color color) /// Set a specific line color by index of the line. /// May be used for 'highlighting' a line, in combination with the ClosestLineToPoint method. /// - public void SetSpecificLineColorByIndex(int index, Color color) + public void SetSpecificLineColorByIndex(int index, Color color, out Color previousColor) { + if(materialPropertyBlockCache == null || Lines.Count != materialPropertyBlockCache.Count) + { + Color[] colors = new Color[Lines.Count]; + for (int i = 0; i < colors.Length; i++) + { + colors[i] = color; + } + SetSpecificLineMaterialColors(colors); + } + if (index >= materialPropertyBlockCache.Count) { Debug.LogWarning($"Index {index} is out of range"); + previousColor = Color.clear; return; } MaterialPropertyBlock props = materialPropertyBlockCache[index]; + previousColor = materialPropertyBlockCache[index].GetColor("_BaseColor"); props.SetColor("_BaseColor", color); materialPropertyBlockCache[index] = props; } @@ -294,7 +313,7 @@ public void SetRandomLineColors() Color[] colors = new Color[Lines.Count]; for (int i = 0; i < colors.Length; i++) { - colors[i] = Random.ColorHSV(); + colors[i] = UnityEngine.Random.ColorHSV(); } SetSpecificLineMaterialColors(colors); diff --git a/Assets/_Functionalities/ObjImporter/Prefabs/ObjImporter.prefab.meta b/Assets/_Functionalities/ObjImporter/Prefabs/ObjImporter.prefab.meta index 4ee63e84..ad45ecfa 100644 --- a/Assets/_Functionalities/ObjImporter/Prefabs/ObjImporter.prefab.meta +++ b/Assets/_Functionalities/ObjImporter/Prefabs/ObjImporter.prefab.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 17813655602a70c46a78e7f4ec2e2b97 +guid: d61f0824e50bba149af0a26bee9971f2 PrefabImporter: externalObjects: {} userData: diff --git a/Assets/_Functionalities/ObjectInformation/Scripts/BagInspector.cs b/Assets/_Functionalities/ObjectInformation/Scripts/BagInspector.cs index 49869cbc..82d42abd 100644 --- a/Assets/_Functionalities/ObjectInformation/Scripts/BagInspector.cs +++ b/Assets/_Functionalities/ObjectInformation/Scripts/BagInspector.cs @@ -134,8 +134,9 @@ public static Vector3 NearestPointOnFiniteLine(Vector3 start, Vector3 end, Vecto //wat doen we met deselecteren? //custom object mapping object schrijven voor features en - + private FeatureMapping lastSelectedFeatureMapping; private float hitDistance = 100000f; + private float tubeHitRadius = 10f; /// /// Find objectmapping by raycast and get the BAG ID /// @@ -144,53 +145,62 @@ private void FindObjectMapping() // Raycast from pointer position using main camera var position = Pointer.current.position.ReadValue(); var ray = mainCamera.ScreenPointToRay(position); - if (!Physics.Raycast(ray, out RaycastHit hit, hitDistance)) return; - - var objectMapping = hit.collider.gameObject.GetComponent(); - if (!objectMapping) + if (Physics.Raycast(ray, out RaycastHit hit, hitDistance)) { - RaycastHit sphereHit = new RaycastHit(); - ObjectMapping targetMapping = null; - if (Physics.SphereCastNonAlloc(ray, 10, raycastHits, hitDistance) > 0) - { - Plane groundPlane = new Plane(Vector3.up, Vector3.zero); - groundPlane.Raycast(ray, out float distance); - Vector3 groundPosition = ray.GetPoint(distance); - float closest = float.MaxValue; - for(int i = 0; i < raycastHits.Length; i++) - { - if (raycastHits[i].collider != null) - { - ObjectMapping mapping = raycastHits[i].collider.gameObject.GetComponent(); - if (mapping != null) - { - float dist = Vector3.Distance(raycastHits[i].point, groundPosition); - if (dist < closest) - { - closest = dist; - targetMapping = mapping; - sphereHit = raycastHits[i]; - } - } - } - } - } - if (targetMapping != null) + //lets use a capsule cast here to ensure objects are hit (some objects for features are really small) and use a nonalloc to prevent memory allocations + var objectMapping = hit.collider.gameObject.GetComponent(); + if (!objectMapping) { - objectMapping = targetMapping; - lastWorldClickedPosition = sphereHit.point; - SelectBuildingOnHit(objectMapping.getObjectID(sphereHit.triangleIndex)); + DeselectBuilding(); } else { - DeselectBuilding(); + lastWorldClickedPosition = hit.point; + SelectBuildingOnHit(objectMapping.getObjectID(hit.triangleIndex)); return; } } - lastWorldClickedPosition = hit.point; - SelectBuildingOnHit(objectMapping.getObjectID(hit.triangleIndex)); - } + if (hit.collider == null) + return; + + Plane groundPlane = new Plane(Vector3.up, Vector3.zero); + groundPlane.Raycast(ray, out float distance); + Vector3 groundPosition = ray.GetPoint(distance); + FeatureMapping targetMapping = null; + //if (Physics.CapsuleCastNonAlloc(ray.origin, ray.GetPoint(hitDistance), tubeHitRadius, ray.direction, raycastHits, hitDistance) > 0) + if (Physics.SphereCastNonAlloc(groundPosition, tubeHitRadius, Vector3.up, raycastHits, hitDistance) > 0) + { + + float closest = float.MaxValue; + for (int i = 0; i < raycastHits.Length; i++) + { + if (raycastHits[i].collider != null) + { + FeatureMapping mapping = raycastHits[i].collider.gameObject.GetComponent(); + if (mapping != null) + { + float dist = Vector3.Distance(raycastHits[i].point, groundPosition); + if (dist < closest) + { + closest = dist; + targetMapping = mapping; + hit = raycastHits[i]; + } + } + } + } + } + if (targetMapping != null) + { + lastWorldClickedPosition = hit.point; + SelectFeatureOnHit(targetMapping); + } + else + { + DeselectFeature(); + } + } private void SelectBuildingOnHit(string bagId) { @@ -221,6 +231,34 @@ private void DeselectBuilding() selectionlayerExists = false; } + private void SelectFeatureOnHit(FeatureMapping mapping) + { + DeselectFeature(); + + contentPanel.SetActive(true); + placeholderPanel.SetActive(false); + selectionlayerExists = true; + + lastSelectedFeatureMapping = mapping; + //TODO populate the baginspector ui + //TODO notify the feature renderer to color rendering blue for feature + mapping.SelectFeature(); + } + + private void DeselectFeature() + { + if (lastSelectedFeatureMapping != null) + { + lastSelectedFeatureMapping.DeselectFeature(); + lastSelectedFeatureMapping = null; + } + + //TODO notify the feature renderer to return back normal coloring + contentPanel.SetActive(false); + placeholderPanel.SetActive(true); + selectionlayerExists = false; + } + private void OnDestroy() { DeselectBuilding(); diff --git a/Assets/_Functionalities/ObjectInformation/Scripts/FeatureMapping.cs b/Assets/_Functionalities/ObjectInformation/Scripts/FeatureMapping.cs new file mode 100644 index 00000000..ab273cff --- /dev/null +++ b/Assets/_Functionalities/ObjectInformation/Scripts/FeatureMapping.cs @@ -0,0 +1,49 @@ +using GeoJSON.Net.Feature; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.UIElements; + +namespace Netherlands3D.Twin +{ + public class FeatureMapping : MonoBehaviour + { + private Feature feature; + private List meshes; + private IGeoJsonVisualisationLayer visualisationLayer; + private Color[] vertexColors; + + public void SetFeature(Feature feature) + { + this.feature = feature; + } + + public void SetMeshes(List meshes) + { + this.meshes = meshes; + int totalVertexCount = 0; + foreach (Mesh m in meshes) + totalVertexCount += m.vertexCount; + vertexColors = new Color[totalVertexCount]; + } + + public void SetVisualisationLayer(IGeoJsonVisualisationLayer visualisationLayer) + { + this.visualisationLayer = visualisationLayer; + } + + public void SelectFeature() + { + for (int i = 0; i < vertexColors.Length; i++) + vertexColors[i] = Color.blue; + + visualisationLayer.SetVisualisationColor(meshes, vertexColors); + } + + public void DeselectFeature() + { + //todo get instance material formn the visualisation layer to set back colors and remove the previouscolors functions, its bad and prone to bugs :D + visualisationLayer.SetVisualisationColor(meshes, vertexColors); + } + } +} diff --git a/Assets/_Functionalities/ObjectInformation/Scripts/FeatureMapping.cs.meta b/Assets/_Functionalities/ObjectInformation/Scripts/FeatureMapping.cs.meta new file mode 100644 index 00000000..cae4bcad --- /dev/null +++ b/Assets/_Functionalities/ObjectInformation/Scripts/FeatureMapping.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c6f1f8be1e9d50444a49c8e14cefeb70 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: