diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 344ff24..d050c89 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -646,7 +646,6 @@ serializedVersion: 6 m_Component: - component: {fileID: 1714391592} - - component: {fileID: 1714391593} - component: {fileID: 1714391594} m_Layer: 0 m_Name: roads @@ -669,47 +668,6 @@ m_Father: {fileID: 0} m_RootOrder: 5 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} ---- !u!23 &1714391593 -MeshRenderer: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1714391591} - m_Enabled: 1 - m_CastShadows: 1 - m_ReceiveShadows: 1 - m_DynamicOccludee: 1 - m_MotionVectors: 1 - m_LightProbeUsage: 1 - m_ReflectionProbeUsage: 1 - m_RayTracingMode: 2 - m_RayTraceProcedural: 0 - m_RenderingLayerMask: 1 - m_RendererPriority: 0 - m_Materials: - - {fileID: 10302, guid: 0000000000000000f000000000000000, type: 0} - m_StaticBatchInfo: - firstSubMesh: 0 - subMeshCount: 0 - m_StaticBatchRoot: {fileID: 0} - m_ProbeAnchor: {fileID: 0} - m_LightProbeVolumeOverride: {fileID: 0} - m_ScaleInLightmap: 1 - m_ReceiveGI: 1 - m_PreserveUVs: 0 - m_IgnoreNormalsForChartDetection: 0 - m_ImportantGI: 0 - m_StitchLightmapSeams: 1 - m_SelectedEditorRenderState: 3 - m_MinimumChartSize: 4 - m_AutoUVMaxDistance: 0.5 - m_AutoUVMaxAngle: 89 - m_LightmapParameters: {fileID: 0} - m_SortingLayerID: 0 - m_SortingLayer: 0 - m_SortingOrder: 0 - m_AdditionalVertexStreams: {fileID: 0} --- !u!114 &1714391594 MonoBehaviour: m_ObjectHideFlags: 0 diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 344ff24..d050c89 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -646,7 +646,6 @@ serializedVersion: 6 m_Component: - component: {fileID: 1714391592} - - component: {fileID: 1714391593} - component: {fileID: 1714391594} m_Layer: 0 m_Name: roads @@ -669,47 +668,6 @@ m_Father: {fileID: 0} m_RootOrder: 5 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} ---- !u!23 &1714391593 -MeshRenderer: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1714391591} - m_Enabled: 1 - m_CastShadows: 1 - m_ReceiveShadows: 1 - m_DynamicOccludee: 1 - m_MotionVectors: 1 - m_LightProbeUsage: 1 - m_ReflectionProbeUsage: 1 - m_RayTracingMode: 2 - m_RayTraceProcedural: 0 - m_RenderingLayerMask: 1 - m_RendererPriority: 0 - m_Materials: - - {fileID: 10302, guid: 0000000000000000f000000000000000, type: 0} - m_StaticBatchInfo: - firstSubMesh: 0 - subMeshCount: 0 - m_StaticBatchRoot: {fileID: 0} - m_ProbeAnchor: {fileID: 0} - m_LightProbeVolumeOverride: {fileID: 0} - m_ScaleInLightmap: 1 - m_ReceiveGI: 1 - m_PreserveUVs: 0 - m_IgnoreNormalsForChartDetection: 0 - m_ImportantGI: 0 - m_StitchLightmapSeams: 1 - m_SelectedEditorRenderState: 3 - m_MinimumChartSize: 4 - m_AutoUVMaxDistance: 0.5 - m_AutoUVMaxAngle: 89 - m_LightmapParameters: {fileID: 0} - m_SortingLayerID: 0 - m_SortingLayer: 0 - m_SortingOrder: 0 - m_AdditionalVertexStreams: {fileID: 0} --- !u!114 &1714391594 MonoBehaviour: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Node.cs b/Assets/Scripts/Node.cs index 50f53fe..b95e3a5 100644 --- a/Assets/Scripts/Node.cs +++ b/Assets/Scripts/Node.cs @@ -18,6 +18,18 @@ this.position = position; } + public Node getOther(Node caller) { + if (roads.Count != 2) { + return null; + } + foreach (Road road in roads) { + if (! road.nodes.Contains(caller)) { + return road.nodes.Find(test => test != this); + } + } + return null; + } + override public int GetHashCode() { return position.GetHashCode(); } @@ -36,7 +48,21 @@ this.position = position; gameObject.transform.position = position; foreach (Road road in roads) { - road.update(); + road.update(true); + } + } + + public void lateUpdate(Road caller) { + foreach (Road road in roads) { + if (!road.Equals(caller)) { + road.update(false); + } + } + } + + public void update() { + foreach (Road road in roads) { + road.update(true); } } } diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 344ff24..d050c89 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -646,7 +646,6 @@ serializedVersion: 6 m_Component: - component: {fileID: 1714391592} - - component: {fileID: 1714391593} - component: {fileID: 1714391594} m_Layer: 0 m_Name: roads @@ -669,47 +668,6 @@ m_Father: {fileID: 0} m_RootOrder: 5 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} ---- !u!23 &1714391593 -MeshRenderer: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1714391591} - m_Enabled: 1 - m_CastShadows: 1 - m_ReceiveShadows: 1 - m_DynamicOccludee: 1 - m_MotionVectors: 1 - m_LightProbeUsage: 1 - m_ReflectionProbeUsage: 1 - m_RayTracingMode: 2 - m_RayTraceProcedural: 0 - m_RenderingLayerMask: 1 - m_RendererPriority: 0 - m_Materials: - - {fileID: 10302, guid: 0000000000000000f000000000000000, type: 0} - m_StaticBatchInfo: - firstSubMesh: 0 - subMeshCount: 0 - m_StaticBatchRoot: {fileID: 0} - m_ProbeAnchor: {fileID: 0} - m_LightProbeVolumeOverride: {fileID: 0} - m_ScaleInLightmap: 1 - m_ReceiveGI: 1 - m_PreserveUVs: 0 - m_IgnoreNormalsForChartDetection: 0 - m_ImportantGI: 0 - m_StitchLightmapSeams: 1 - m_SelectedEditorRenderState: 3 - m_MinimumChartSize: 4 - m_AutoUVMaxDistance: 0.5 - m_AutoUVMaxAngle: 89 - m_LightmapParameters: {fileID: 0} - m_SortingLayerID: 0 - m_SortingLayer: 0 - m_SortingOrder: 0 - m_AdditionalVertexStreams: {fileID: 0} --- !u!114 &1714391594 MonoBehaviour: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Node.cs b/Assets/Scripts/Node.cs index 50f53fe..b95e3a5 100644 --- a/Assets/Scripts/Node.cs +++ b/Assets/Scripts/Node.cs @@ -18,6 +18,18 @@ this.position = position; } + public Node getOther(Node caller) { + if (roads.Count != 2) { + return null; + } + foreach (Road road in roads) { + if (! road.nodes.Contains(caller)) { + return road.nodes.Find(test => test != this); + } + } + return null; + } + override public int GetHashCode() { return position.GetHashCode(); } @@ -36,7 +48,21 @@ this.position = position; gameObject.transform.position = position; foreach (Road road in roads) { - road.update(); + road.update(true); + } + } + + public void lateUpdate(Road caller) { + foreach (Road road in roads) { + if (!road.Equals(caller)) { + road.update(false); + } + } + } + + public void update() { + foreach (Road road in roads) { + road.update(true); } } } diff --git a/Assets/Scripts/Road.cs b/Assets/Scripts/Road.cs index 334c74b..6bfb7a9 100644 --- a/Assets/Scripts/Road.cs +++ b/Assets/Scripts/Road.cs @@ -3,13 +3,13 @@ using UnityEngine; public class Road { - public Node start, end; + public List nodes = new List(); FlatBezierRenderer bezierRenderer; Material material; public Road(Node start, Node end, Material material) { - this.start = start; - this.end = end; + nodes.Add(start); + nodes.Add(end); this.material = material; bezierRenderer = new FlatBezierRenderer(new Bezier(), 50, 0.2f); } @@ -21,14 +21,30 @@ child.AddComponent().material = material; child.AddComponent().mesh = bezierRenderer.mesh; child.transform.parent = parent; - update(); + update(true); } - public void update() { - bezierRenderer.bezier.A = start.position; - bezierRenderer.bezier.B = 0.5f * (start.position + end.position); - bezierRenderer.bezier.C = 0.5f * (end.position + start.position); - bezierRenderer.bezier.D = end.position; + public void update(bool updateOthers) { + bezierRenderer.bezier.A = nodes[0].position; + if (nodes[0].roads.Count == 2) { + if (updateOthers) { + nodes[0].lateUpdate(this); + } + bezierRenderer.bezier.B = nodes[0].position + + 0.25f * (nodes[1].position - nodes[0].getOther(nodes[1]).position); + } else { + bezierRenderer.bezier.B = 0.5f * (nodes[0].position + nodes[1].position); + } + if (nodes[1].roads.Count == 2) { + if (updateOthers) { + nodes[1].lateUpdate(this); + } + bezierRenderer.bezier.C = nodes[1].position + + 0.25f * (nodes[0].position - nodes[1].getOther(nodes[0]).position); + } else { + bezierRenderer.bezier.C = 0.5f * (nodes[0].position + nodes[1].position); + } + bezierRenderer.bezier.D = nodes[1].position; bezierRenderer.update(); } @@ -40,12 +56,15 @@ return true; } Road otherRoad = (Road) other; - return - (otherRoad.start.Equals(start)) && (otherRoad.end.Equals(end)) || - (otherRoad.start.Equals(end)) && (otherRoad.end.Equals(start)); + foreach (Node node in nodes) { + if (! otherRoad.nodes.Contains(node)) { + return false; + } + } + return true; } public override int GetHashCode() { - return start.GetHashCode() << 16 | end.GetHashCode(); + return nodes[0].GetHashCode() << 16 | nodes[1].GetHashCode(); } } diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 344ff24..d050c89 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -646,7 +646,6 @@ serializedVersion: 6 m_Component: - component: {fileID: 1714391592} - - component: {fileID: 1714391593} - component: {fileID: 1714391594} m_Layer: 0 m_Name: roads @@ -669,47 +668,6 @@ m_Father: {fileID: 0} m_RootOrder: 5 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} ---- !u!23 &1714391593 -MeshRenderer: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1714391591} - m_Enabled: 1 - m_CastShadows: 1 - m_ReceiveShadows: 1 - m_DynamicOccludee: 1 - m_MotionVectors: 1 - m_LightProbeUsage: 1 - m_ReflectionProbeUsage: 1 - m_RayTracingMode: 2 - m_RayTraceProcedural: 0 - m_RenderingLayerMask: 1 - m_RendererPriority: 0 - m_Materials: - - {fileID: 10302, guid: 0000000000000000f000000000000000, type: 0} - m_StaticBatchInfo: - firstSubMesh: 0 - subMeshCount: 0 - m_StaticBatchRoot: {fileID: 0} - m_ProbeAnchor: {fileID: 0} - m_LightProbeVolumeOverride: {fileID: 0} - m_ScaleInLightmap: 1 - m_ReceiveGI: 1 - m_PreserveUVs: 0 - m_IgnoreNormalsForChartDetection: 0 - m_ImportantGI: 0 - m_StitchLightmapSeams: 1 - m_SelectedEditorRenderState: 3 - m_MinimumChartSize: 4 - m_AutoUVMaxDistance: 0.5 - m_AutoUVMaxAngle: 89 - m_LightmapParameters: {fileID: 0} - m_SortingLayerID: 0 - m_SortingLayer: 0 - m_SortingOrder: 0 - m_AdditionalVertexStreams: {fileID: 0} --- !u!114 &1714391594 MonoBehaviour: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Node.cs b/Assets/Scripts/Node.cs index 50f53fe..b95e3a5 100644 --- a/Assets/Scripts/Node.cs +++ b/Assets/Scripts/Node.cs @@ -18,6 +18,18 @@ this.position = position; } + public Node getOther(Node caller) { + if (roads.Count != 2) { + return null; + } + foreach (Road road in roads) { + if (! road.nodes.Contains(caller)) { + return road.nodes.Find(test => test != this); + } + } + return null; + } + override public int GetHashCode() { return position.GetHashCode(); } @@ -36,7 +48,21 @@ this.position = position; gameObject.transform.position = position; foreach (Road road in roads) { - road.update(); + road.update(true); + } + } + + public void lateUpdate(Road caller) { + foreach (Road road in roads) { + if (!road.Equals(caller)) { + road.update(false); + } + } + } + + public void update() { + foreach (Road road in roads) { + road.update(true); } } } diff --git a/Assets/Scripts/Road.cs b/Assets/Scripts/Road.cs index 334c74b..6bfb7a9 100644 --- a/Assets/Scripts/Road.cs +++ b/Assets/Scripts/Road.cs @@ -3,13 +3,13 @@ using UnityEngine; public class Road { - public Node start, end; + public List nodes = new List(); FlatBezierRenderer bezierRenderer; Material material; public Road(Node start, Node end, Material material) { - this.start = start; - this.end = end; + nodes.Add(start); + nodes.Add(end); this.material = material; bezierRenderer = new FlatBezierRenderer(new Bezier(), 50, 0.2f); } @@ -21,14 +21,30 @@ child.AddComponent().material = material; child.AddComponent().mesh = bezierRenderer.mesh; child.transform.parent = parent; - update(); + update(true); } - public void update() { - bezierRenderer.bezier.A = start.position; - bezierRenderer.bezier.B = 0.5f * (start.position + end.position); - bezierRenderer.bezier.C = 0.5f * (end.position + start.position); - bezierRenderer.bezier.D = end.position; + public void update(bool updateOthers) { + bezierRenderer.bezier.A = nodes[0].position; + if (nodes[0].roads.Count == 2) { + if (updateOthers) { + nodes[0].lateUpdate(this); + } + bezierRenderer.bezier.B = nodes[0].position + + 0.25f * (nodes[1].position - nodes[0].getOther(nodes[1]).position); + } else { + bezierRenderer.bezier.B = 0.5f * (nodes[0].position + nodes[1].position); + } + if (nodes[1].roads.Count == 2) { + if (updateOthers) { + nodes[1].lateUpdate(this); + } + bezierRenderer.bezier.C = nodes[1].position + + 0.25f * (nodes[0].position - nodes[1].getOther(nodes[0]).position); + } else { + bezierRenderer.bezier.C = 0.5f * (nodes[0].position + nodes[1].position); + } + bezierRenderer.bezier.D = nodes[1].position; bezierRenderer.update(); } @@ -40,12 +56,15 @@ return true; } Road otherRoad = (Road) other; - return - (otherRoad.start.Equals(start)) && (otherRoad.end.Equals(end)) || - (otherRoad.start.Equals(end)) && (otherRoad.end.Equals(start)); + foreach (Node node in nodes) { + if (! otherRoad.nodes.Contains(node)) { + return false; + } + } + return true; } public override int GetHashCode() { - return start.GetHashCode() << 16 | end.GetHashCode(); + return nodes[0].GetHashCode() << 16 | nodes[1].GetHashCode(); } } diff --git a/Assets/Scripts/Roads.cs b/Assets/Scripts/Roads.cs index 4b54d35..cfa46b8 100644 --- a/Assets/Scripts/Roads.cs +++ b/Assets/Scripts/Roads.cs @@ -46,10 +46,10 @@ } Road road = new Road(startNode, endNode, material); if (!roads.Contains(road)) { - road.initialize(transform); - roads.Add(road); startNode.roads.Add(road); endNode.roads.Add(road); + roads.Add(road); + road.initialize(transform); } startNode = null; drawing = false;