Skip to content

Commit

Permalink
draft 2
Browse files Browse the repository at this point in the history
  • Loading branch information
bozmir committed Oct 30, 2024
1 parent c6758e2 commit b7da4cf
Show file tree
Hide file tree
Showing 12 changed files with 220 additions and 77 deletions.
2 changes: 1 addition & 1 deletion Assets/Prefabs/ObjImporter.prefab.meta

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

1 change: 0 additions & 1 deletion Assets/Scenes/Main.unity

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

29 changes: 25 additions & 4 deletions Assets/Scripts/Layers/LayerTypes/GeoJSONLineLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,36 @@ public List<Mesh> 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;
}

/// <summary>
/// the previous colors of all vertices will be stored into the vertexColor buffer parameter
/// </summary>
/// <param name="meshes"></param>
/// <param name="vertexColors"></param>
public void SetVisualisationColor(List<Mesh> 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<FeatureLineVisualisations> SpawnedVisualisations = new();

private bool randomizeColorPerFeature = false;
Expand Down
5 changes: 5 additions & 0 deletions Assets/Scripts/Layers/LayerTypes/GeoJSONPointLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,5 +146,10 @@ public override void DestroyLayerGameObject()

base.DestroyLayerGameObject();
}

public void SetVisualisationColor(List<Mesh> meshes, Color[] previousColors)
{

}
}
}
5 changes: 5 additions & 0 deletions Assets/Scripts/Layers/LayerTypes/GeoJSONPolygonLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,10 @@ private void RemoveFeature(FeaturePolygonVisualisations featureVisualisation)
featureVisualisation.DestroyAllVisualisations();
SpawnedVisualisations.Remove(featureVisualisation);
}

public void SetVisualisationColor(List<Mesh> meshes, Color[] previousColors)
{

}
}
}
21 changes: 8 additions & 13 deletions Assets/Scripts/Layers/LayerTypes/GeoJsonLayerGameObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Feature, GameObject> featureMeshes = new Dictionary<Feature, GameObject>();

private void ProcessObjectMapping(Feature feature, List<Mesh> meshes)
private void ProcessObjectMapping(IGeoJsonVisualisationLayer layer, Feature feature, List<Mesh> meshes)
{
GameObject parent;
if(featureMeshes.ContainsKey(feature))
Expand Down Expand Up @@ -347,15 +347,10 @@ private void ProcessObjectMapping(Feature feature, List<Mesh> meshes)
subObject.AddComponent<SphereCollider>().radius = 1f;
subObject.transform.SetParent(parent.transform);

ObjectMapping objectMapping = subObject.AddComponent<ObjectMapping>();
objectMapping.items = new List<ObjectMappingItem>();
string id = feature.Id;
objectMapping.items.Add(new ObjectMappingItem()
{
objectID = id,
firstVertex = 0,
verticesLength = meshes[i].vertices.Length,
});
FeatureMapping objectMapping = subObject.AddComponent<FeatureMapping>();
objectMapping.SetFeature(feature);
objectMapping.SetMeshes(meshes);
objectMapping.SetVisualisationLayer(layer);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ namespace Netherlands3D.Twin
public interface IGeoJsonVisualisationLayer
{
List<Mesh> GetMeshData(Feature feature);
void SetVisualisationColor(List<Mesh> meshes, Color[] previousColors);
}
}
55 changes: 37 additions & 18 deletions Assets/Scripts/LineRenderer3D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Netherlands3D.Coordinates;
using UnityEngine;
using UnityEngine.Rendering;
using Random = UnityEngine.Random;

namespace Netherlands3D.Twin
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -170,32 +176,45 @@ public int GetClosestLineIndex(Vector3 point)
/// <summary>
/// Set specific line color for the line closest to a given point.
/// </summary>
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;
}

/// <summary>
/// Set a specific line color by index of the line.
/// May be used for 'highlighting' a line, in combination with the ClosestLineToPoint method.
/// </summary>
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;
}
Expand Down Expand Up @@ -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);
Expand Down

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

116 changes: 77 additions & 39 deletions Assets/_Functionalities/ObjectInformation/Scripts/BagInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
/// <summary>
/// Find objectmapping by raycast and get the BAG ID
/// </summary>
Expand All @@ -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<ObjectMapping>();
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<ObjectMapping>();
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<ObjectMapping>();
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<FeatureMapping>();
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)
{
Expand Down Expand Up @@ -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();
Expand Down
Loading

0 comments on commit b7da4cf

Please sign in to comment.