diff --git a/Assets/Scripts/Bezier.cs b/Assets/Scripts/Bezier.cs deleted file mode 100644 index ea19006..0000000 --- a/Assets/Scripts/Bezier.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Bezier { - public Vector3 A, B, C, D; - - public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { - this.A = A; - this.B = B; - this.C = C; - this.D = D; - } - - public Bezier() { - this.A = Vector3.zero; - this.B = Vector3.zero; - this.C = Vector3.zero; - this.D = Vector3.zero; - } - - public Vector3 getPosition(float t) { - float T = 1.0f - t; - return - A * ( T*T*T) + - B * (3*T*T*t) + - C * (3*T*t*t) + - D * ( t*t*t); - } - - public Vector3 getDirection(float t) { - float T = 1.0f - t; - return - A * (-3*T*T ) + - B * ( 3*T*T - 6*T*t) + - C * (-3*t*t + 6*T*t) + - D * ( 3*t*t ); - } -} diff --git a/Assets/Scripts/Bezier.cs b/Assets/Scripts/Bezier.cs deleted file mode 100644 index ea19006..0000000 --- a/Assets/Scripts/Bezier.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Bezier { - public Vector3 A, B, C, D; - - public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { - this.A = A; - this.B = B; - this.C = C; - this.D = D; - } - - public Bezier() { - this.A = Vector3.zero; - this.B = Vector3.zero; - this.C = Vector3.zero; - this.D = Vector3.zero; - } - - public Vector3 getPosition(float t) { - float T = 1.0f - t; - return - A * ( T*T*T) + - B * (3*T*T*t) + - C * (3*T*t*t) + - D * ( t*t*t); - } - - public Vector3 getDirection(float t) { - float T = 1.0f - t; - return - A * (-3*T*T ) + - B * ( 3*T*T - 6*T*t) + - C * (-3*t*t + 6*T*t) + - D * ( 3*t*t ); - } -} diff --git a/Assets/Scripts/Bezier.cs.meta b/Assets/Scripts/Bezier.cs.meta deleted file mode 100644 index 14630e2..0000000 --- a/Assets/Scripts/Bezier.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 284cf9d52e385aa4ebba97eb8b43b8a6 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Bezier.cs b/Assets/Scripts/Bezier.cs deleted file mode 100644 index ea19006..0000000 --- a/Assets/Scripts/Bezier.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Bezier { - public Vector3 A, B, C, D; - - public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { - this.A = A; - this.B = B; - this.C = C; - this.D = D; - } - - public Bezier() { - this.A = Vector3.zero; - this.B = Vector3.zero; - this.C = Vector3.zero; - this.D = Vector3.zero; - } - - public Vector3 getPosition(float t) { - float T = 1.0f - t; - return - A * ( T*T*T) + - B * (3*T*T*t) + - C * (3*T*t*t) + - D * ( t*t*t); - } - - public Vector3 getDirection(float t) { - float T = 1.0f - t; - return - A * (-3*T*T ) + - B * ( 3*T*T - 6*T*t) + - C * (-3*t*t + 6*T*t) + - D * ( 3*t*t ); - } -} diff --git a/Assets/Scripts/Bezier.cs.meta b/Assets/Scripts/Bezier.cs.meta deleted file mode 100644 index 14630e2..0000000 --- a/Assets/Scripts/Bezier.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 284cf9d52e385aa4ebba97eb8b43b8a6 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatBezierRenderer.cs b/Assets/Scripts/FlatBezierRenderer.cs deleted file mode 100644 index b8dad15..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatBezierRenderer { - private int percision; - private float width; - public Bezier bezier; - public Mesh mesh = new Mesh(); - public FlatBezierRenderer(Bezier bezier, int percision, float width) { - this.percision = percision; - this.width = width; - this.bezier = bezier; - update(); - } - - public void update() { - float realWidth = width / 2; - int vertexCount = 6 * (percision+1); - Vector3[] vertices = new Vector3[vertexCount / 3]; - int[] indices = new int[vertexCount]; - Vector3 position = bezier.getPosition(0.0f); - Vector3 tangent = getTangent(0.0f); - vertices[0] = position + tangent * realWidth; - vertices[1] = position - tangent * realWidth; - for (int i = 1; i <= percision; i++) { - float t = (float) i / percision; - indices[6*i ] = 2*i ; - indices[6*i+1] = 2*i-1; - indices[6*i+2] = 2*i-2; - indices[6*i+3] = 2*i+1; - indices[6*i+4] = 2*i-1; - indices[6*i+5] = 2*i ; - - position = bezier.getPosition(t); - tangent = getTangent(t); - vertices[2*i ] = position + tangent * realWidth; - vertices[2*i+1] = position - tangent * realWidth; - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - mesh.Optimize(); - } - - private Vector3 getTangent(float t) { - Vector3 direction = bezier.getDirection(t); - direction.Normalize(); - return new Vector3(-direction.z, 0, direction.x); - } -} diff --git a/Assets/Scripts/Bezier.cs b/Assets/Scripts/Bezier.cs deleted file mode 100644 index ea19006..0000000 --- a/Assets/Scripts/Bezier.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Bezier { - public Vector3 A, B, C, D; - - public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { - this.A = A; - this.B = B; - this.C = C; - this.D = D; - } - - public Bezier() { - this.A = Vector3.zero; - this.B = Vector3.zero; - this.C = Vector3.zero; - this.D = Vector3.zero; - } - - public Vector3 getPosition(float t) { - float T = 1.0f - t; - return - A * ( T*T*T) + - B * (3*T*T*t) + - C * (3*T*t*t) + - D * ( t*t*t); - } - - public Vector3 getDirection(float t) { - float T = 1.0f - t; - return - A * (-3*T*T ) + - B * ( 3*T*T - 6*T*t) + - C * (-3*t*t + 6*T*t) + - D * ( 3*t*t ); - } -} diff --git a/Assets/Scripts/Bezier.cs.meta b/Assets/Scripts/Bezier.cs.meta deleted file mode 100644 index 14630e2..0000000 --- a/Assets/Scripts/Bezier.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 284cf9d52e385aa4ebba97eb8b43b8a6 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatBezierRenderer.cs b/Assets/Scripts/FlatBezierRenderer.cs deleted file mode 100644 index b8dad15..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatBezierRenderer { - private int percision; - private float width; - public Bezier bezier; - public Mesh mesh = new Mesh(); - public FlatBezierRenderer(Bezier bezier, int percision, float width) { - this.percision = percision; - this.width = width; - this.bezier = bezier; - update(); - } - - public void update() { - float realWidth = width / 2; - int vertexCount = 6 * (percision+1); - Vector3[] vertices = new Vector3[vertexCount / 3]; - int[] indices = new int[vertexCount]; - Vector3 position = bezier.getPosition(0.0f); - Vector3 tangent = getTangent(0.0f); - vertices[0] = position + tangent * realWidth; - vertices[1] = position - tangent * realWidth; - for (int i = 1; i <= percision; i++) { - float t = (float) i / percision; - indices[6*i ] = 2*i ; - indices[6*i+1] = 2*i-1; - indices[6*i+2] = 2*i-2; - indices[6*i+3] = 2*i+1; - indices[6*i+4] = 2*i-1; - indices[6*i+5] = 2*i ; - - position = bezier.getPosition(t); - tangent = getTangent(t); - vertices[2*i ] = position + tangent * realWidth; - vertices[2*i+1] = position - tangent * realWidth; - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - mesh.Optimize(); - } - - private Vector3 getTangent(float t) { - Vector3 direction = bezier.getDirection(t); - direction.Normalize(); - return new Vector3(-direction.z, 0, direction.x); - } -} diff --git a/Assets/Scripts/FlatBezierRenderer.cs.meta b/Assets/Scripts/FlatBezierRenderer.cs.meta deleted file mode 100644 index 1fe6547..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 9bfb303c44d15ef468dc0d7334241e94 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Bezier.cs b/Assets/Scripts/Bezier.cs deleted file mode 100644 index ea19006..0000000 --- a/Assets/Scripts/Bezier.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Bezier { - public Vector3 A, B, C, D; - - public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { - this.A = A; - this.B = B; - this.C = C; - this.D = D; - } - - public Bezier() { - this.A = Vector3.zero; - this.B = Vector3.zero; - this.C = Vector3.zero; - this.D = Vector3.zero; - } - - public Vector3 getPosition(float t) { - float T = 1.0f - t; - return - A * ( T*T*T) + - B * (3*T*T*t) + - C * (3*T*t*t) + - D * ( t*t*t); - } - - public Vector3 getDirection(float t) { - float T = 1.0f - t; - return - A * (-3*T*T ) + - B * ( 3*T*T - 6*T*t) + - C * (-3*t*t + 6*T*t) + - D * ( 3*t*t ); - } -} diff --git a/Assets/Scripts/Bezier.cs.meta b/Assets/Scripts/Bezier.cs.meta deleted file mode 100644 index 14630e2..0000000 --- a/Assets/Scripts/Bezier.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 284cf9d52e385aa4ebba97eb8b43b8a6 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatBezierRenderer.cs b/Assets/Scripts/FlatBezierRenderer.cs deleted file mode 100644 index b8dad15..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatBezierRenderer { - private int percision; - private float width; - public Bezier bezier; - public Mesh mesh = new Mesh(); - public FlatBezierRenderer(Bezier bezier, int percision, float width) { - this.percision = percision; - this.width = width; - this.bezier = bezier; - update(); - } - - public void update() { - float realWidth = width / 2; - int vertexCount = 6 * (percision+1); - Vector3[] vertices = new Vector3[vertexCount / 3]; - int[] indices = new int[vertexCount]; - Vector3 position = bezier.getPosition(0.0f); - Vector3 tangent = getTangent(0.0f); - vertices[0] = position + tangent * realWidth; - vertices[1] = position - tangent * realWidth; - for (int i = 1; i <= percision; i++) { - float t = (float) i / percision; - indices[6*i ] = 2*i ; - indices[6*i+1] = 2*i-1; - indices[6*i+2] = 2*i-2; - indices[6*i+3] = 2*i+1; - indices[6*i+4] = 2*i-1; - indices[6*i+5] = 2*i ; - - position = bezier.getPosition(t); - tangent = getTangent(t); - vertices[2*i ] = position + tangent * realWidth; - vertices[2*i+1] = position - tangent * realWidth; - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - mesh.Optimize(); - } - - private Vector3 getTangent(float t) { - Vector3 direction = bezier.getDirection(t); - direction.Normalize(); - return new Vector3(-direction.z, 0, direction.x); - } -} diff --git a/Assets/Scripts/FlatBezierRenderer.cs.meta b/Assets/Scripts/FlatBezierRenderer.cs.meta deleted file mode 100644 index 1fe6547..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 9bfb303c44d15ef468dc0d7334241e94 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatCircleRenderer.cs b/Assets/Scripts/FlatCircleRenderer.cs deleted file mode 100644 index 363b663..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatCircleRenderer { - public Mesh mesh = new Mesh(); - public float radius, width; - private int segments; - - public FlatCircleRenderer(float radius, float width, int segments) { - this.radius = radius; - this.width = width; - this.segments = segments; - update(); - } - - public void update() { - Vector3[] vertices = new Vector3[2*segments+6]; - int[] indices = new int[6*segments]; - for (int i = 0; i < segments; i++) { - float alpha = (float) i / segments * 2 * Mathf.PI; - indices[6*i ] = 2*i ; - indices[6*i+3] = 2*i ; - indices[6*i+5] = 2*i+1; - if (i == 0) { - indices[6*i+1] = 2*segments-2; - indices[6*i+2] = 2*segments-1; - indices[6*i+4] = 2*segments-1; - } else { - indices[6*i+1] = 2*i-2; - indices[6*i+2] = 2*i-1; - indices[6*i+4] = 2*i-1; - } - Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); - vertices[2*i ] = direction * radius; - vertices[2*i+1] = direction * (radius + width); - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - } -} diff --git a/Assets/Scripts/Bezier.cs b/Assets/Scripts/Bezier.cs deleted file mode 100644 index ea19006..0000000 --- a/Assets/Scripts/Bezier.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Bezier { - public Vector3 A, B, C, D; - - public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { - this.A = A; - this.B = B; - this.C = C; - this.D = D; - } - - public Bezier() { - this.A = Vector3.zero; - this.B = Vector3.zero; - this.C = Vector3.zero; - this.D = Vector3.zero; - } - - public Vector3 getPosition(float t) { - float T = 1.0f - t; - return - A * ( T*T*T) + - B * (3*T*T*t) + - C * (3*T*t*t) + - D * ( t*t*t); - } - - public Vector3 getDirection(float t) { - float T = 1.0f - t; - return - A * (-3*T*T ) + - B * ( 3*T*T - 6*T*t) + - C * (-3*t*t + 6*T*t) + - D * ( 3*t*t ); - } -} diff --git a/Assets/Scripts/Bezier.cs.meta b/Assets/Scripts/Bezier.cs.meta deleted file mode 100644 index 14630e2..0000000 --- a/Assets/Scripts/Bezier.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 284cf9d52e385aa4ebba97eb8b43b8a6 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatBezierRenderer.cs b/Assets/Scripts/FlatBezierRenderer.cs deleted file mode 100644 index b8dad15..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatBezierRenderer { - private int percision; - private float width; - public Bezier bezier; - public Mesh mesh = new Mesh(); - public FlatBezierRenderer(Bezier bezier, int percision, float width) { - this.percision = percision; - this.width = width; - this.bezier = bezier; - update(); - } - - public void update() { - float realWidth = width / 2; - int vertexCount = 6 * (percision+1); - Vector3[] vertices = new Vector3[vertexCount / 3]; - int[] indices = new int[vertexCount]; - Vector3 position = bezier.getPosition(0.0f); - Vector3 tangent = getTangent(0.0f); - vertices[0] = position + tangent * realWidth; - vertices[1] = position - tangent * realWidth; - for (int i = 1; i <= percision; i++) { - float t = (float) i / percision; - indices[6*i ] = 2*i ; - indices[6*i+1] = 2*i-1; - indices[6*i+2] = 2*i-2; - indices[6*i+3] = 2*i+1; - indices[6*i+4] = 2*i-1; - indices[6*i+5] = 2*i ; - - position = bezier.getPosition(t); - tangent = getTangent(t); - vertices[2*i ] = position + tangent * realWidth; - vertices[2*i+1] = position - tangent * realWidth; - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - mesh.Optimize(); - } - - private Vector3 getTangent(float t) { - Vector3 direction = bezier.getDirection(t); - direction.Normalize(); - return new Vector3(-direction.z, 0, direction.x); - } -} diff --git a/Assets/Scripts/FlatBezierRenderer.cs.meta b/Assets/Scripts/FlatBezierRenderer.cs.meta deleted file mode 100644 index 1fe6547..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 9bfb303c44d15ef468dc0d7334241e94 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatCircleRenderer.cs b/Assets/Scripts/FlatCircleRenderer.cs deleted file mode 100644 index 363b663..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatCircleRenderer { - public Mesh mesh = new Mesh(); - public float radius, width; - private int segments; - - public FlatCircleRenderer(float radius, float width, int segments) { - this.radius = radius; - this.width = width; - this.segments = segments; - update(); - } - - public void update() { - Vector3[] vertices = new Vector3[2*segments+6]; - int[] indices = new int[6*segments]; - for (int i = 0; i < segments; i++) { - float alpha = (float) i / segments * 2 * Mathf.PI; - indices[6*i ] = 2*i ; - indices[6*i+3] = 2*i ; - indices[6*i+5] = 2*i+1; - if (i == 0) { - indices[6*i+1] = 2*segments-2; - indices[6*i+2] = 2*segments-1; - indices[6*i+4] = 2*segments-1; - } else { - indices[6*i+1] = 2*i-2; - indices[6*i+2] = 2*i-1; - indices[6*i+4] = 2*i-1; - } - Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); - vertices[2*i ] = direction * radius; - vertices[2*i+1] = direction * (radius + width); - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - } -} diff --git a/Assets/Scripts/FlatCircleRenderer.cs.meta b/Assets/Scripts/FlatCircleRenderer.cs.meta deleted file mode 100644 index d10c238..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0fe81c7924a85b142a4a86201b08c241 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Bezier.cs b/Assets/Scripts/Bezier.cs deleted file mode 100644 index ea19006..0000000 --- a/Assets/Scripts/Bezier.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Bezier { - public Vector3 A, B, C, D; - - public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { - this.A = A; - this.B = B; - this.C = C; - this.D = D; - } - - public Bezier() { - this.A = Vector3.zero; - this.B = Vector3.zero; - this.C = Vector3.zero; - this.D = Vector3.zero; - } - - public Vector3 getPosition(float t) { - float T = 1.0f - t; - return - A * ( T*T*T) + - B * (3*T*T*t) + - C * (3*T*t*t) + - D * ( t*t*t); - } - - public Vector3 getDirection(float t) { - float T = 1.0f - t; - return - A * (-3*T*T ) + - B * ( 3*T*T - 6*T*t) + - C * (-3*t*t + 6*T*t) + - D * ( 3*t*t ); - } -} diff --git a/Assets/Scripts/Bezier.cs.meta b/Assets/Scripts/Bezier.cs.meta deleted file mode 100644 index 14630e2..0000000 --- a/Assets/Scripts/Bezier.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 284cf9d52e385aa4ebba97eb8b43b8a6 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatBezierRenderer.cs b/Assets/Scripts/FlatBezierRenderer.cs deleted file mode 100644 index b8dad15..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatBezierRenderer { - private int percision; - private float width; - public Bezier bezier; - public Mesh mesh = new Mesh(); - public FlatBezierRenderer(Bezier bezier, int percision, float width) { - this.percision = percision; - this.width = width; - this.bezier = bezier; - update(); - } - - public void update() { - float realWidth = width / 2; - int vertexCount = 6 * (percision+1); - Vector3[] vertices = new Vector3[vertexCount / 3]; - int[] indices = new int[vertexCount]; - Vector3 position = bezier.getPosition(0.0f); - Vector3 tangent = getTangent(0.0f); - vertices[0] = position + tangent * realWidth; - vertices[1] = position - tangent * realWidth; - for (int i = 1; i <= percision; i++) { - float t = (float) i / percision; - indices[6*i ] = 2*i ; - indices[6*i+1] = 2*i-1; - indices[6*i+2] = 2*i-2; - indices[6*i+3] = 2*i+1; - indices[6*i+4] = 2*i-1; - indices[6*i+5] = 2*i ; - - position = bezier.getPosition(t); - tangent = getTangent(t); - vertices[2*i ] = position + tangent * realWidth; - vertices[2*i+1] = position - tangent * realWidth; - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - mesh.Optimize(); - } - - private Vector3 getTangent(float t) { - Vector3 direction = bezier.getDirection(t); - direction.Normalize(); - return new Vector3(-direction.z, 0, direction.x); - } -} diff --git a/Assets/Scripts/FlatBezierRenderer.cs.meta b/Assets/Scripts/FlatBezierRenderer.cs.meta deleted file mode 100644 index 1fe6547..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 9bfb303c44d15ef468dc0d7334241e94 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatCircleRenderer.cs b/Assets/Scripts/FlatCircleRenderer.cs deleted file mode 100644 index 363b663..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatCircleRenderer { - public Mesh mesh = new Mesh(); - public float radius, width; - private int segments; - - public FlatCircleRenderer(float radius, float width, int segments) { - this.radius = radius; - this.width = width; - this.segments = segments; - update(); - } - - public void update() { - Vector3[] vertices = new Vector3[2*segments+6]; - int[] indices = new int[6*segments]; - for (int i = 0; i < segments; i++) { - float alpha = (float) i / segments * 2 * Mathf.PI; - indices[6*i ] = 2*i ; - indices[6*i+3] = 2*i ; - indices[6*i+5] = 2*i+1; - if (i == 0) { - indices[6*i+1] = 2*segments-2; - indices[6*i+2] = 2*segments-1; - indices[6*i+4] = 2*segments-1; - } else { - indices[6*i+1] = 2*i-2; - indices[6*i+2] = 2*i-1; - indices[6*i+4] = 2*i-1; - } - Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); - vertices[2*i ] = direction * radius; - vertices[2*i+1] = direction * (radius + width); - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - } -} diff --git a/Assets/Scripts/FlatCircleRenderer.cs.meta b/Assets/Scripts/FlatCircleRenderer.cs.meta deleted file mode 100644 index d10c238..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0fe81c7924a85b142a4a86201b08c241 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Node.cs b/Assets/Scripts/Node.cs deleted file mode 100644 index e3c4cbd..0000000 --- a/Assets/Scripts/Node.cs +++ /dev/null @@ -1,81 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Node { - public FlatCircleRenderer circle = new FlatCircleRenderer(0.2f, 0.05f, 32); - public FlatCircleRenderer fullCircle = new FlatCircleRenderer(0f, 1f, 32); - - public Vector3 position; - public List roads = new List(); - public GameObject gameObject; - - public Node(Vector3 position, Transform parent, Config config) { - gameObject = new GameObject(); - gameObject.transform.position = position; - gameObject.transform.parent = parent; - gameObject.AddComponent().radius = 0.5f; - gameObject.layer = 7; - - GameObject nodeCircle = new GameObject(); - nodeCircle.AddComponent().material = config.roadEditMaterial; - nodeCircle.AddComponent().mesh = circle.mesh; - nodeCircle.transform.parent = gameObject.transform; - nodeCircle.transform.localPosition = Vector3.zero; - - GameObject nodeRoad = new GameObject(); - nodeRoad.AddComponent().material = config.roadMaterial; - nodeRoad.AddComponent().mesh = fullCircle.mesh; - nodeRoad.transform.parent = gameObject.transform; - nodeRoad.transform.localPosition = Vector3.zero; - 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(); - } - - override public bool Equals(object other) { - if ((other == null) || ! this.GetType().Equals(other.GetType())) { - return false; - } - if (other == this) { - return true; - } - return ((Node)other).position.Equals(position); - } - - public void pull(Vector3 position) { - this.position = position; - gameObject.transform.position = position; - foreach (Road road in roads) { - 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/Bezier.cs b/Assets/Scripts/Bezier.cs deleted file mode 100644 index ea19006..0000000 --- a/Assets/Scripts/Bezier.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Bezier { - public Vector3 A, B, C, D; - - public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { - this.A = A; - this.B = B; - this.C = C; - this.D = D; - } - - public Bezier() { - this.A = Vector3.zero; - this.B = Vector3.zero; - this.C = Vector3.zero; - this.D = Vector3.zero; - } - - public Vector3 getPosition(float t) { - float T = 1.0f - t; - return - A * ( T*T*T) + - B * (3*T*T*t) + - C * (3*T*t*t) + - D * ( t*t*t); - } - - public Vector3 getDirection(float t) { - float T = 1.0f - t; - return - A * (-3*T*T ) + - B * ( 3*T*T - 6*T*t) + - C * (-3*t*t + 6*T*t) + - D * ( 3*t*t ); - } -} diff --git a/Assets/Scripts/Bezier.cs.meta b/Assets/Scripts/Bezier.cs.meta deleted file mode 100644 index 14630e2..0000000 --- a/Assets/Scripts/Bezier.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 284cf9d52e385aa4ebba97eb8b43b8a6 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatBezierRenderer.cs b/Assets/Scripts/FlatBezierRenderer.cs deleted file mode 100644 index b8dad15..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatBezierRenderer { - private int percision; - private float width; - public Bezier bezier; - public Mesh mesh = new Mesh(); - public FlatBezierRenderer(Bezier bezier, int percision, float width) { - this.percision = percision; - this.width = width; - this.bezier = bezier; - update(); - } - - public void update() { - float realWidth = width / 2; - int vertexCount = 6 * (percision+1); - Vector3[] vertices = new Vector3[vertexCount / 3]; - int[] indices = new int[vertexCount]; - Vector3 position = bezier.getPosition(0.0f); - Vector3 tangent = getTangent(0.0f); - vertices[0] = position + tangent * realWidth; - vertices[1] = position - tangent * realWidth; - for (int i = 1; i <= percision; i++) { - float t = (float) i / percision; - indices[6*i ] = 2*i ; - indices[6*i+1] = 2*i-1; - indices[6*i+2] = 2*i-2; - indices[6*i+3] = 2*i+1; - indices[6*i+4] = 2*i-1; - indices[6*i+5] = 2*i ; - - position = bezier.getPosition(t); - tangent = getTangent(t); - vertices[2*i ] = position + tangent * realWidth; - vertices[2*i+1] = position - tangent * realWidth; - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - mesh.Optimize(); - } - - private Vector3 getTangent(float t) { - Vector3 direction = bezier.getDirection(t); - direction.Normalize(); - return new Vector3(-direction.z, 0, direction.x); - } -} diff --git a/Assets/Scripts/FlatBezierRenderer.cs.meta b/Assets/Scripts/FlatBezierRenderer.cs.meta deleted file mode 100644 index 1fe6547..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 9bfb303c44d15ef468dc0d7334241e94 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatCircleRenderer.cs b/Assets/Scripts/FlatCircleRenderer.cs deleted file mode 100644 index 363b663..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatCircleRenderer { - public Mesh mesh = new Mesh(); - public float radius, width; - private int segments; - - public FlatCircleRenderer(float radius, float width, int segments) { - this.radius = radius; - this.width = width; - this.segments = segments; - update(); - } - - public void update() { - Vector3[] vertices = new Vector3[2*segments+6]; - int[] indices = new int[6*segments]; - for (int i = 0; i < segments; i++) { - float alpha = (float) i / segments * 2 * Mathf.PI; - indices[6*i ] = 2*i ; - indices[6*i+3] = 2*i ; - indices[6*i+5] = 2*i+1; - if (i == 0) { - indices[6*i+1] = 2*segments-2; - indices[6*i+2] = 2*segments-1; - indices[6*i+4] = 2*segments-1; - } else { - indices[6*i+1] = 2*i-2; - indices[6*i+2] = 2*i-1; - indices[6*i+4] = 2*i-1; - } - Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); - vertices[2*i ] = direction * radius; - vertices[2*i+1] = direction * (radius + width); - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - } -} diff --git a/Assets/Scripts/FlatCircleRenderer.cs.meta b/Assets/Scripts/FlatCircleRenderer.cs.meta deleted file mode 100644 index d10c238..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0fe81c7924a85b142a4a86201b08c241 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Node.cs b/Assets/Scripts/Node.cs deleted file mode 100644 index e3c4cbd..0000000 --- a/Assets/Scripts/Node.cs +++ /dev/null @@ -1,81 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Node { - public FlatCircleRenderer circle = new FlatCircleRenderer(0.2f, 0.05f, 32); - public FlatCircleRenderer fullCircle = new FlatCircleRenderer(0f, 1f, 32); - - public Vector3 position; - public List roads = new List(); - public GameObject gameObject; - - public Node(Vector3 position, Transform parent, Config config) { - gameObject = new GameObject(); - gameObject.transform.position = position; - gameObject.transform.parent = parent; - gameObject.AddComponent().radius = 0.5f; - gameObject.layer = 7; - - GameObject nodeCircle = new GameObject(); - nodeCircle.AddComponent().material = config.roadEditMaterial; - nodeCircle.AddComponent().mesh = circle.mesh; - nodeCircle.transform.parent = gameObject.transform; - nodeCircle.transform.localPosition = Vector3.zero; - - GameObject nodeRoad = new GameObject(); - nodeRoad.AddComponent().material = config.roadMaterial; - nodeRoad.AddComponent().mesh = fullCircle.mesh; - nodeRoad.transform.parent = gameObject.transform; - nodeRoad.transform.localPosition = Vector3.zero; - 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(); - } - - override public bool Equals(object other) { - if ((other == null) || ! this.GetType().Equals(other.GetType())) { - return false; - } - if (other == this) { - return true; - } - return ((Node)other).position.Equals(position); - } - - public void pull(Vector3 position) { - this.position = position; - gameObject.transform.position = position; - foreach (Road road in roads) { - 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/Node.cs.meta b/Assets/Scripts/Node.cs.meta deleted file mode 100644 index 8bbedf1..0000000 --- a/Assets/Scripts/Node.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 3cbed1dad7ec67d4dbe8602aea1e2979 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Bezier.cs b/Assets/Scripts/Bezier.cs deleted file mode 100644 index ea19006..0000000 --- a/Assets/Scripts/Bezier.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Bezier { - public Vector3 A, B, C, D; - - public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { - this.A = A; - this.B = B; - this.C = C; - this.D = D; - } - - public Bezier() { - this.A = Vector3.zero; - this.B = Vector3.zero; - this.C = Vector3.zero; - this.D = Vector3.zero; - } - - public Vector3 getPosition(float t) { - float T = 1.0f - t; - return - A * ( T*T*T) + - B * (3*T*T*t) + - C * (3*T*t*t) + - D * ( t*t*t); - } - - public Vector3 getDirection(float t) { - float T = 1.0f - t; - return - A * (-3*T*T ) + - B * ( 3*T*T - 6*T*t) + - C * (-3*t*t + 6*T*t) + - D * ( 3*t*t ); - } -} diff --git a/Assets/Scripts/Bezier.cs.meta b/Assets/Scripts/Bezier.cs.meta deleted file mode 100644 index 14630e2..0000000 --- a/Assets/Scripts/Bezier.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 284cf9d52e385aa4ebba97eb8b43b8a6 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatBezierRenderer.cs b/Assets/Scripts/FlatBezierRenderer.cs deleted file mode 100644 index b8dad15..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatBezierRenderer { - private int percision; - private float width; - public Bezier bezier; - public Mesh mesh = new Mesh(); - public FlatBezierRenderer(Bezier bezier, int percision, float width) { - this.percision = percision; - this.width = width; - this.bezier = bezier; - update(); - } - - public void update() { - float realWidth = width / 2; - int vertexCount = 6 * (percision+1); - Vector3[] vertices = new Vector3[vertexCount / 3]; - int[] indices = new int[vertexCount]; - Vector3 position = bezier.getPosition(0.0f); - Vector3 tangent = getTangent(0.0f); - vertices[0] = position + tangent * realWidth; - vertices[1] = position - tangent * realWidth; - for (int i = 1; i <= percision; i++) { - float t = (float) i / percision; - indices[6*i ] = 2*i ; - indices[6*i+1] = 2*i-1; - indices[6*i+2] = 2*i-2; - indices[6*i+3] = 2*i+1; - indices[6*i+4] = 2*i-1; - indices[6*i+5] = 2*i ; - - position = bezier.getPosition(t); - tangent = getTangent(t); - vertices[2*i ] = position + tangent * realWidth; - vertices[2*i+1] = position - tangent * realWidth; - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - mesh.Optimize(); - } - - private Vector3 getTangent(float t) { - Vector3 direction = bezier.getDirection(t); - direction.Normalize(); - return new Vector3(-direction.z, 0, direction.x); - } -} diff --git a/Assets/Scripts/FlatBezierRenderer.cs.meta b/Assets/Scripts/FlatBezierRenderer.cs.meta deleted file mode 100644 index 1fe6547..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 9bfb303c44d15ef468dc0d7334241e94 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatCircleRenderer.cs b/Assets/Scripts/FlatCircleRenderer.cs deleted file mode 100644 index 363b663..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatCircleRenderer { - public Mesh mesh = new Mesh(); - public float radius, width; - private int segments; - - public FlatCircleRenderer(float radius, float width, int segments) { - this.radius = radius; - this.width = width; - this.segments = segments; - update(); - } - - public void update() { - Vector3[] vertices = new Vector3[2*segments+6]; - int[] indices = new int[6*segments]; - for (int i = 0; i < segments; i++) { - float alpha = (float) i / segments * 2 * Mathf.PI; - indices[6*i ] = 2*i ; - indices[6*i+3] = 2*i ; - indices[6*i+5] = 2*i+1; - if (i == 0) { - indices[6*i+1] = 2*segments-2; - indices[6*i+2] = 2*segments-1; - indices[6*i+4] = 2*segments-1; - } else { - indices[6*i+1] = 2*i-2; - indices[6*i+2] = 2*i-1; - indices[6*i+4] = 2*i-1; - } - Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); - vertices[2*i ] = direction * radius; - vertices[2*i+1] = direction * (radius + width); - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - } -} diff --git a/Assets/Scripts/FlatCircleRenderer.cs.meta b/Assets/Scripts/FlatCircleRenderer.cs.meta deleted file mode 100644 index d10c238..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0fe81c7924a85b142a4a86201b08c241 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Node.cs b/Assets/Scripts/Node.cs deleted file mode 100644 index e3c4cbd..0000000 --- a/Assets/Scripts/Node.cs +++ /dev/null @@ -1,81 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Node { - public FlatCircleRenderer circle = new FlatCircleRenderer(0.2f, 0.05f, 32); - public FlatCircleRenderer fullCircle = new FlatCircleRenderer(0f, 1f, 32); - - public Vector3 position; - public List roads = new List(); - public GameObject gameObject; - - public Node(Vector3 position, Transform parent, Config config) { - gameObject = new GameObject(); - gameObject.transform.position = position; - gameObject.transform.parent = parent; - gameObject.AddComponent().radius = 0.5f; - gameObject.layer = 7; - - GameObject nodeCircle = new GameObject(); - nodeCircle.AddComponent().material = config.roadEditMaterial; - nodeCircle.AddComponent().mesh = circle.mesh; - nodeCircle.transform.parent = gameObject.transform; - nodeCircle.transform.localPosition = Vector3.zero; - - GameObject nodeRoad = new GameObject(); - nodeRoad.AddComponent().material = config.roadMaterial; - nodeRoad.AddComponent().mesh = fullCircle.mesh; - nodeRoad.transform.parent = gameObject.transform; - nodeRoad.transform.localPosition = Vector3.zero; - 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(); - } - - override public bool Equals(object other) { - if ((other == null) || ! this.GetType().Equals(other.GetType())) { - return false; - } - if (other == this) { - return true; - } - return ((Node)other).position.Equals(position); - } - - public void pull(Vector3 position) { - this.position = position; - gameObject.transform.position = position; - foreach (Road road in roads) { - 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/Node.cs.meta b/Assets/Scripts/Node.cs.meta deleted file mode 100644 index 8bbedf1..0000000 --- a/Assets/Scripts/Node.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 3cbed1dad7ec67d4dbe8602aea1e2979 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Primitives.meta b/Assets/Scripts/Primitives.meta new file mode 100644 index 0000000..0cf8895 --- /dev/null +++ b/Assets/Scripts/Primitives.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 6d1a8c7f33189a240b4d749356f53399 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Bezier.cs b/Assets/Scripts/Bezier.cs deleted file mode 100644 index ea19006..0000000 --- a/Assets/Scripts/Bezier.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Bezier { - public Vector3 A, B, C, D; - - public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { - this.A = A; - this.B = B; - this.C = C; - this.D = D; - } - - public Bezier() { - this.A = Vector3.zero; - this.B = Vector3.zero; - this.C = Vector3.zero; - this.D = Vector3.zero; - } - - public Vector3 getPosition(float t) { - float T = 1.0f - t; - return - A * ( T*T*T) + - B * (3*T*T*t) + - C * (3*T*t*t) + - D * ( t*t*t); - } - - public Vector3 getDirection(float t) { - float T = 1.0f - t; - return - A * (-3*T*T ) + - B * ( 3*T*T - 6*T*t) + - C * (-3*t*t + 6*T*t) + - D * ( 3*t*t ); - } -} diff --git a/Assets/Scripts/Bezier.cs.meta b/Assets/Scripts/Bezier.cs.meta deleted file mode 100644 index 14630e2..0000000 --- a/Assets/Scripts/Bezier.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 284cf9d52e385aa4ebba97eb8b43b8a6 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatBezierRenderer.cs b/Assets/Scripts/FlatBezierRenderer.cs deleted file mode 100644 index b8dad15..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatBezierRenderer { - private int percision; - private float width; - public Bezier bezier; - public Mesh mesh = new Mesh(); - public FlatBezierRenderer(Bezier bezier, int percision, float width) { - this.percision = percision; - this.width = width; - this.bezier = bezier; - update(); - } - - public void update() { - float realWidth = width / 2; - int vertexCount = 6 * (percision+1); - Vector3[] vertices = new Vector3[vertexCount / 3]; - int[] indices = new int[vertexCount]; - Vector3 position = bezier.getPosition(0.0f); - Vector3 tangent = getTangent(0.0f); - vertices[0] = position + tangent * realWidth; - vertices[1] = position - tangent * realWidth; - for (int i = 1; i <= percision; i++) { - float t = (float) i / percision; - indices[6*i ] = 2*i ; - indices[6*i+1] = 2*i-1; - indices[6*i+2] = 2*i-2; - indices[6*i+3] = 2*i+1; - indices[6*i+4] = 2*i-1; - indices[6*i+5] = 2*i ; - - position = bezier.getPosition(t); - tangent = getTangent(t); - vertices[2*i ] = position + tangent * realWidth; - vertices[2*i+1] = position - tangent * realWidth; - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - mesh.Optimize(); - } - - private Vector3 getTangent(float t) { - Vector3 direction = bezier.getDirection(t); - direction.Normalize(); - return new Vector3(-direction.z, 0, direction.x); - } -} diff --git a/Assets/Scripts/FlatBezierRenderer.cs.meta b/Assets/Scripts/FlatBezierRenderer.cs.meta deleted file mode 100644 index 1fe6547..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 9bfb303c44d15ef468dc0d7334241e94 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatCircleRenderer.cs b/Assets/Scripts/FlatCircleRenderer.cs deleted file mode 100644 index 363b663..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatCircleRenderer { - public Mesh mesh = new Mesh(); - public float radius, width; - private int segments; - - public FlatCircleRenderer(float radius, float width, int segments) { - this.radius = radius; - this.width = width; - this.segments = segments; - update(); - } - - public void update() { - Vector3[] vertices = new Vector3[2*segments+6]; - int[] indices = new int[6*segments]; - for (int i = 0; i < segments; i++) { - float alpha = (float) i / segments * 2 * Mathf.PI; - indices[6*i ] = 2*i ; - indices[6*i+3] = 2*i ; - indices[6*i+5] = 2*i+1; - if (i == 0) { - indices[6*i+1] = 2*segments-2; - indices[6*i+2] = 2*segments-1; - indices[6*i+4] = 2*segments-1; - } else { - indices[6*i+1] = 2*i-2; - indices[6*i+2] = 2*i-1; - indices[6*i+4] = 2*i-1; - } - Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); - vertices[2*i ] = direction * radius; - vertices[2*i+1] = direction * (radius + width); - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - } -} diff --git a/Assets/Scripts/FlatCircleRenderer.cs.meta b/Assets/Scripts/FlatCircleRenderer.cs.meta deleted file mode 100644 index d10c238..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0fe81c7924a85b142a4a86201b08c241 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Node.cs b/Assets/Scripts/Node.cs deleted file mode 100644 index e3c4cbd..0000000 --- a/Assets/Scripts/Node.cs +++ /dev/null @@ -1,81 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Node { - public FlatCircleRenderer circle = new FlatCircleRenderer(0.2f, 0.05f, 32); - public FlatCircleRenderer fullCircle = new FlatCircleRenderer(0f, 1f, 32); - - public Vector3 position; - public List roads = new List(); - public GameObject gameObject; - - public Node(Vector3 position, Transform parent, Config config) { - gameObject = new GameObject(); - gameObject.transform.position = position; - gameObject.transform.parent = parent; - gameObject.AddComponent().radius = 0.5f; - gameObject.layer = 7; - - GameObject nodeCircle = new GameObject(); - nodeCircle.AddComponent().material = config.roadEditMaterial; - nodeCircle.AddComponent().mesh = circle.mesh; - nodeCircle.transform.parent = gameObject.transform; - nodeCircle.transform.localPosition = Vector3.zero; - - GameObject nodeRoad = new GameObject(); - nodeRoad.AddComponent().material = config.roadMaterial; - nodeRoad.AddComponent().mesh = fullCircle.mesh; - nodeRoad.transform.parent = gameObject.transform; - nodeRoad.transform.localPosition = Vector3.zero; - 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(); - } - - override public bool Equals(object other) { - if ((other == null) || ! this.GetType().Equals(other.GetType())) { - return false; - } - if (other == this) { - return true; - } - return ((Node)other).position.Equals(position); - } - - public void pull(Vector3 position) { - this.position = position; - gameObject.transform.position = position; - foreach (Road road in roads) { - 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/Node.cs.meta b/Assets/Scripts/Node.cs.meta deleted file mode 100644 index 8bbedf1..0000000 --- a/Assets/Scripts/Node.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 3cbed1dad7ec67d4dbe8602aea1e2979 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Primitives.meta b/Assets/Scripts/Primitives.meta new file mode 100644 index 0000000..0cf8895 --- /dev/null +++ b/Assets/Scripts/Primitives.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 6d1a8c7f33189a240b4d749356f53399 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/Bezier.cs b/Assets/Scripts/Primitives/Bezier.cs new file mode 100644 index 0000000..ea19006 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs @@ -0,0 +1,39 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Bezier { + public Vector3 A, B, C, D; + + public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { + this.A = A; + this.B = B; + this.C = C; + this.D = D; + } + + public Bezier() { + this.A = Vector3.zero; + this.B = Vector3.zero; + this.C = Vector3.zero; + this.D = Vector3.zero; + } + + public Vector3 getPosition(float t) { + float T = 1.0f - t; + return + A * ( T*T*T) + + B * (3*T*T*t) + + C * (3*T*t*t) + + D * ( t*t*t); + } + + public Vector3 getDirection(float t) { + float T = 1.0f - t; + return + A * (-3*T*T ) + + B * ( 3*T*T - 6*T*t) + + C * (-3*t*t + 6*T*t) + + D * ( 3*t*t ); + } +} diff --git a/Assets/Scripts/Bezier.cs b/Assets/Scripts/Bezier.cs deleted file mode 100644 index ea19006..0000000 --- a/Assets/Scripts/Bezier.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Bezier { - public Vector3 A, B, C, D; - - public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { - this.A = A; - this.B = B; - this.C = C; - this.D = D; - } - - public Bezier() { - this.A = Vector3.zero; - this.B = Vector3.zero; - this.C = Vector3.zero; - this.D = Vector3.zero; - } - - public Vector3 getPosition(float t) { - float T = 1.0f - t; - return - A * ( T*T*T) + - B * (3*T*T*t) + - C * (3*T*t*t) + - D * ( t*t*t); - } - - public Vector3 getDirection(float t) { - float T = 1.0f - t; - return - A * (-3*T*T ) + - B * ( 3*T*T - 6*T*t) + - C * (-3*t*t + 6*T*t) + - D * ( 3*t*t ); - } -} diff --git a/Assets/Scripts/Bezier.cs.meta b/Assets/Scripts/Bezier.cs.meta deleted file mode 100644 index 14630e2..0000000 --- a/Assets/Scripts/Bezier.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 284cf9d52e385aa4ebba97eb8b43b8a6 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatBezierRenderer.cs b/Assets/Scripts/FlatBezierRenderer.cs deleted file mode 100644 index b8dad15..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatBezierRenderer { - private int percision; - private float width; - public Bezier bezier; - public Mesh mesh = new Mesh(); - public FlatBezierRenderer(Bezier bezier, int percision, float width) { - this.percision = percision; - this.width = width; - this.bezier = bezier; - update(); - } - - public void update() { - float realWidth = width / 2; - int vertexCount = 6 * (percision+1); - Vector3[] vertices = new Vector3[vertexCount / 3]; - int[] indices = new int[vertexCount]; - Vector3 position = bezier.getPosition(0.0f); - Vector3 tangent = getTangent(0.0f); - vertices[0] = position + tangent * realWidth; - vertices[1] = position - tangent * realWidth; - for (int i = 1; i <= percision; i++) { - float t = (float) i / percision; - indices[6*i ] = 2*i ; - indices[6*i+1] = 2*i-1; - indices[6*i+2] = 2*i-2; - indices[6*i+3] = 2*i+1; - indices[6*i+4] = 2*i-1; - indices[6*i+5] = 2*i ; - - position = bezier.getPosition(t); - tangent = getTangent(t); - vertices[2*i ] = position + tangent * realWidth; - vertices[2*i+1] = position - tangent * realWidth; - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - mesh.Optimize(); - } - - private Vector3 getTangent(float t) { - Vector3 direction = bezier.getDirection(t); - direction.Normalize(); - return new Vector3(-direction.z, 0, direction.x); - } -} diff --git a/Assets/Scripts/FlatBezierRenderer.cs.meta b/Assets/Scripts/FlatBezierRenderer.cs.meta deleted file mode 100644 index 1fe6547..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 9bfb303c44d15ef468dc0d7334241e94 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatCircleRenderer.cs b/Assets/Scripts/FlatCircleRenderer.cs deleted file mode 100644 index 363b663..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatCircleRenderer { - public Mesh mesh = new Mesh(); - public float radius, width; - private int segments; - - public FlatCircleRenderer(float radius, float width, int segments) { - this.radius = radius; - this.width = width; - this.segments = segments; - update(); - } - - public void update() { - Vector3[] vertices = new Vector3[2*segments+6]; - int[] indices = new int[6*segments]; - for (int i = 0; i < segments; i++) { - float alpha = (float) i / segments * 2 * Mathf.PI; - indices[6*i ] = 2*i ; - indices[6*i+3] = 2*i ; - indices[6*i+5] = 2*i+1; - if (i == 0) { - indices[6*i+1] = 2*segments-2; - indices[6*i+2] = 2*segments-1; - indices[6*i+4] = 2*segments-1; - } else { - indices[6*i+1] = 2*i-2; - indices[6*i+2] = 2*i-1; - indices[6*i+4] = 2*i-1; - } - Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); - vertices[2*i ] = direction * radius; - vertices[2*i+1] = direction * (radius + width); - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - } -} diff --git a/Assets/Scripts/FlatCircleRenderer.cs.meta b/Assets/Scripts/FlatCircleRenderer.cs.meta deleted file mode 100644 index d10c238..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0fe81c7924a85b142a4a86201b08c241 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Node.cs b/Assets/Scripts/Node.cs deleted file mode 100644 index e3c4cbd..0000000 --- a/Assets/Scripts/Node.cs +++ /dev/null @@ -1,81 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Node { - public FlatCircleRenderer circle = new FlatCircleRenderer(0.2f, 0.05f, 32); - public FlatCircleRenderer fullCircle = new FlatCircleRenderer(0f, 1f, 32); - - public Vector3 position; - public List roads = new List(); - public GameObject gameObject; - - public Node(Vector3 position, Transform parent, Config config) { - gameObject = new GameObject(); - gameObject.transform.position = position; - gameObject.transform.parent = parent; - gameObject.AddComponent().radius = 0.5f; - gameObject.layer = 7; - - GameObject nodeCircle = new GameObject(); - nodeCircle.AddComponent().material = config.roadEditMaterial; - nodeCircle.AddComponent().mesh = circle.mesh; - nodeCircle.transform.parent = gameObject.transform; - nodeCircle.transform.localPosition = Vector3.zero; - - GameObject nodeRoad = new GameObject(); - nodeRoad.AddComponent().material = config.roadMaterial; - nodeRoad.AddComponent().mesh = fullCircle.mesh; - nodeRoad.transform.parent = gameObject.transform; - nodeRoad.transform.localPosition = Vector3.zero; - 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(); - } - - override public bool Equals(object other) { - if ((other == null) || ! this.GetType().Equals(other.GetType())) { - return false; - } - if (other == this) { - return true; - } - return ((Node)other).position.Equals(position); - } - - public void pull(Vector3 position) { - this.position = position; - gameObject.transform.position = position; - foreach (Road road in roads) { - 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/Node.cs.meta b/Assets/Scripts/Node.cs.meta deleted file mode 100644 index 8bbedf1..0000000 --- a/Assets/Scripts/Node.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 3cbed1dad7ec67d4dbe8602aea1e2979 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Primitives.meta b/Assets/Scripts/Primitives.meta new file mode 100644 index 0000000..0cf8895 --- /dev/null +++ b/Assets/Scripts/Primitives.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 6d1a8c7f33189a240b4d749356f53399 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/Bezier.cs b/Assets/Scripts/Primitives/Bezier.cs new file mode 100644 index 0000000..ea19006 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs @@ -0,0 +1,39 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Bezier { + public Vector3 A, B, C, D; + + public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { + this.A = A; + this.B = B; + this.C = C; + this.D = D; + } + + public Bezier() { + this.A = Vector3.zero; + this.B = Vector3.zero; + this.C = Vector3.zero; + this.D = Vector3.zero; + } + + public Vector3 getPosition(float t) { + float T = 1.0f - t; + return + A * ( T*T*T) + + B * (3*T*T*t) + + C * (3*T*t*t) + + D * ( t*t*t); + } + + public Vector3 getDirection(float t) { + float T = 1.0f - t; + return + A * (-3*T*T ) + + B * ( 3*T*T - 6*T*t) + + C * (-3*t*t + 6*T*t) + + D * ( 3*t*t ); + } +} diff --git a/Assets/Scripts/Primitives/Bezier.cs.meta b/Assets/Scripts/Primitives/Bezier.cs.meta new file mode 100644 index 0000000..14630e2 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 284cf9d52e385aa4ebba97eb8b43b8a6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Bezier.cs b/Assets/Scripts/Bezier.cs deleted file mode 100644 index ea19006..0000000 --- a/Assets/Scripts/Bezier.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Bezier { - public Vector3 A, B, C, D; - - public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { - this.A = A; - this.B = B; - this.C = C; - this.D = D; - } - - public Bezier() { - this.A = Vector3.zero; - this.B = Vector3.zero; - this.C = Vector3.zero; - this.D = Vector3.zero; - } - - public Vector3 getPosition(float t) { - float T = 1.0f - t; - return - A * ( T*T*T) + - B * (3*T*T*t) + - C * (3*T*t*t) + - D * ( t*t*t); - } - - public Vector3 getDirection(float t) { - float T = 1.0f - t; - return - A * (-3*T*T ) + - B * ( 3*T*T - 6*T*t) + - C * (-3*t*t + 6*T*t) + - D * ( 3*t*t ); - } -} diff --git a/Assets/Scripts/Bezier.cs.meta b/Assets/Scripts/Bezier.cs.meta deleted file mode 100644 index 14630e2..0000000 --- a/Assets/Scripts/Bezier.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 284cf9d52e385aa4ebba97eb8b43b8a6 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatBezierRenderer.cs b/Assets/Scripts/FlatBezierRenderer.cs deleted file mode 100644 index b8dad15..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatBezierRenderer { - private int percision; - private float width; - public Bezier bezier; - public Mesh mesh = new Mesh(); - public FlatBezierRenderer(Bezier bezier, int percision, float width) { - this.percision = percision; - this.width = width; - this.bezier = bezier; - update(); - } - - public void update() { - float realWidth = width / 2; - int vertexCount = 6 * (percision+1); - Vector3[] vertices = new Vector3[vertexCount / 3]; - int[] indices = new int[vertexCount]; - Vector3 position = bezier.getPosition(0.0f); - Vector3 tangent = getTangent(0.0f); - vertices[0] = position + tangent * realWidth; - vertices[1] = position - tangent * realWidth; - for (int i = 1; i <= percision; i++) { - float t = (float) i / percision; - indices[6*i ] = 2*i ; - indices[6*i+1] = 2*i-1; - indices[6*i+2] = 2*i-2; - indices[6*i+3] = 2*i+1; - indices[6*i+4] = 2*i-1; - indices[6*i+5] = 2*i ; - - position = bezier.getPosition(t); - tangent = getTangent(t); - vertices[2*i ] = position + tangent * realWidth; - vertices[2*i+1] = position - tangent * realWidth; - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - mesh.Optimize(); - } - - private Vector3 getTangent(float t) { - Vector3 direction = bezier.getDirection(t); - direction.Normalize(); - return new Vector3(-direction.z, 0, direction.x); - } -} diff --git a/Assets/Scripts/FlatBezierRenderer.cs.meta b/Assets/Scripts/FlatBezierRenderer.cs.meta deleted file mode 100644 index 1fe6547..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 9bfb303c44d15ef468dc0d7334241e94 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatCircleRenderer.cs b/Assets/Scripts/FlatCircleRenderer.cs deleted file mode 100644 index 363b663..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatCircleRenderer { - public Mesh mesh = new Mesh(); - public float radius, width; - private int segments; - - public FlatCircleRenderer(float radius, float width, int segments) { - this.radius = radius; - this.width = width; - this.segments = segments; - update(); - } - - public void update() { - Vector3[] vertices = new Vector3[2*segments+6]; - int[] indices = new int[6*segments]; - for (int i = 0; i < segments; i++) { - float alpha = (float) i / segments * 2 * Mathf.PI; - indices[6*i ] = 2*i ; - indices[6*i+3] = 2*i ; - indices[6*i+5] = 2*i+1; - if (i == 0) { - indices[6*i+1] = 2*segments-2; - indices[6*i+2] = 2*segments-1; - indices[6*i+4] = 2*segments-1; - } else { - indices[6*i+1] = 2*i-2; - indices[6*i+2] = 2*i-1; - indices[6*i+4] = 2*i-1; - } - Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); - vertices[2*i ] = direction * radius; - vertices[2*i+1] = direction * (radius + width); - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - } -} diff --git a/Assets/Scripts/FlatCircleRenderer.cs.meta b/Assets/Scripts/FlatCircleRenderer.cs.meta deleted file mode 100644 index d10c238..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0fe81c7924a85b142a4a86201b08c241 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Node.cs b/Assets/Scripts/Node.cs deleted file mode 100644 index e3c4cbd..0000000 --- a/Assets/Scripts/Node.cs +++ /dev/null @@ -1,81 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Node { - public FlatCircleRenderer circle = new FlatCircleRenderer(0.2f, 0.05f, 32); - public FlatCircleRenderer fullCircle = new FlatCircleRenderer(0f, 1f, 32); - - public Vector3 position; - public List roads = new List(); - public GameObject gameObject; - - public Node(Vector3 position, Transform parent, Config config) { - gameObject = new GameObject(); - gameObject.transform.position = position; - gameObject.transform.parent = parent; - gameObject.AddComponent().radius = 0.5f; - gameObject.layer = 7; - - GameObject nodeCircle = new GameObject(); - nodeCircle.AddComponent().material = config.roadEditMaterial; - nodeCircle.AddComponent().mesh = circle.mesh; - nodeCircle.transform.parent = gameObject.transform; - nodeCircle.transform.localPosition = Vector3.zero; - - GameObject nodeRoad = new GameObject(); - nodeRoad.AddComponent().material = config.roadMaterial; - nodeRoad.AddComponent().mesh = fullCircle.mesh; - nodeRoad.transform.parent = gameObject.transform; - nodeRoad.transform.localPosition = Vector3.zero; - 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(); - } - - override public bool Equals(object other) { - if ((other == null) || ! this.GetType().Equals(other.GetType())) { - return false; - } - if (other == this) { - return true; - } - return ((Node)other).position.Equals(position); - } - - public void pull(Vector3 position) { - this.position = position; - gameObject.transform.position = position; - foreach (Road road in roads) { - 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/Node.cs.meta b/Assets/Scripts/Node.cs.meta deleted file mode 100644 index 8bbedf1..0000000 --- a/Assets/Scripts/Node.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 3cbed1dad7ec67d4dbe8602aea1e2979 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Primitives.meta b/Assets/Scripts/Primitives.meta new file mode 100644 index 0000000..0cf8895 --- /dev/null +++ b/Assets/Scripts/Primitives.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 6d1a8c7f33189a240b4d749356f53399 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/Bezier.cs b/Assets/Scripts/Primitives/Bezier.cs new file mode 100644 index 0000000..ea19006 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs @@ -0,0 +1,39 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Bezier { + public Vector3 A, B, C, D; + + public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { + this.A = A; + this.B = B; + this.C = C; + this.D = D; + } + + public Bezier() { + this.A = Vector3.zero; + this.B = Vector3.zero; + this.C = Vector3.zero; + this.D = Vector3.zero; + } + + public Vector3 getPosition(float t) { + float T = 1.0f - t; + return + A * ( T*T*T) + + B * (3*T*T*t) + + C * (3*T*t*t) + + D * ( t*t*t); + } + + public Vector3 getDirection(float t) { + float T = 1.0f - t; + return + A * (-3*T*T ) + + B * ( 3*T*T - 6*T*t) + + C * (-3*t*t + 6*T*t) + + D * ( 3*t*t ); + } +} diff --git a/Assets/Scripts/Primitives/Bezier.cs.meta b/Assets/Scripts/Primitives/Bezier.cs.meta new file mode 100644 index 0000000..14630e2 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 284cf9d52e385aa4ebba97eb8b43b8a6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs b/Assets/Scripts/Primitives/FlatBezierRenderer.cs new file mode 100644 index 0000000..b8dad15 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs @@ -0,0 +1,51 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FlatBezierRenderer { + private int percision; + private float width; + public Bezier bezier; + public Mesh mesh = new Mesh(); + public FlatBezierRenderer(Bezier bezier, int percision, float width) { + this.percision = percision; + this.width = width; + this.bezier = bezier; + update(); + } + + public void update() { + float realWidth = width / 2; + int vertexCount = 6 * (percision+1); + Vector3[] vertices = new Vector3[vertexCount / 3]; + int[] indices = new int[vertexCount]; + Vector3 position = bezier.getPosition(0.0f); + Vector3 tangent = getTangent(0.0f); + vertices[0] = position + tangent * realWidth; + vertices[1] = position - tangent * realWidth; + for (int i = 1; i <= percision; i++) { + float t = (float) i / percision; + indices[6*i ] = 2*i ; + indices[6*i+1] = 2*i-1; + indices[6*i+2] = 2*i-2; + indices[6*i+3] = 2*i+1; + indices[6*i+4] = 2*i-1; + indices[6*i+5] = 2*i ; + + position = bezier.getPosition(t); + tangent = getTangent(t); + vertices[2*i ] = position + tangent * realWidth; + vertices[2*i+1] = position - tangent * realWidth; + } + mesh.Clear(); + mesh.vertices = vertices; + mesh.triangles = indices; + mesh.Optimize(); + } + + private Vector3 getTangent(float t) { + Vector3 direction = bezier.getDirection(t); + direction.Normalize(); + return new Vector3(-direction.z, 0, direction.x); + } +} diff --git a/Assets/Scripts/Bezier.cs b/Assets/Scripts/Bezier.cs deleted file mode 100644 index ea19006..0000000 --- a/Assets/Scripts/Bezier.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Bezier { - public Vector3 A, B, C, D; - - public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { - this.A = A; - this.B = B; - this.C = C; - this.D = D; - } - - public Bezier() { - this.A = Vector3.zero; - this.B = Vector3.zero; - this.C = Vector3.zero; - this.D = Vector3.zero; - } - - public Vector3 getPosition(float t) { - float T = 1.0f - t; - return - A * ( T*T*T) + - B * (3*T*T*t) + - C * (3*T*t*t) + - D * ( t*t*t); - } - - public Vector3 getDirection(float t) { - float T = 1.0f - t; - return - A * (-3*T*T ) + - B * ( 3*T*T - 6*T*t) + - C * (-3*t*t + 6*T*t) + - D * ( 3*t*t ); - } -} diff --git a/Assets/Scripts/Bezier.cs.meta b/Assets/Scripts/Bezier.cs.meta deleted file mode 100644 index 14630e2..0000000 --- a/Assets/Scripts/Bezier.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 284cf9d52e385aa4ebba97eb8b43b8a6 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatBezierRenderer.cs b/Assets/Scripts/FlatBezierRenderer.cs deleted file mode 100644 index b8dad15..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatBezierRenderer { - private int percision; - private float width; - public Bezier bezier; - public Mesh mesh = new Mesh(); - public FlatBezierRenderer(Bezier bezier, int percision, float width) { - this.percision = percision; - this.width = width; - this.bezier = bezier; - update(); - } - - public void update() { - float realWidth = width / 2; - int vertexCount = 6 * (percision+1); - Vector3[] vertices = new Vector3[vertexCount / 3]; - int[] indices = new int[vertexCount]; - Vector3 position = bezier.getPosition(0.0f); - Vector3 tangent = getTangent(0.0f); - vertices[0] = position + tangent * realWidth; - vertices[1] = position - tangent * realWidth; - for (int i = 1; i <= percision; i++) { - float t = (float) i / percision; - indices[6*i ] = 2*i ; - indices[6*i+1] = 2*i-1; - indices[6*i+2] = 2*i-2; - indices[6*i+3] = 2*i+1; - indices[6*i+4] = 2*i-1; - indices[6*i+5] = 2*i ; - - position = bezier.getPosition(t); - tangent = getTangent(t); - vertices[2*i ] = position + tangent * realWidth; - vertices[2*i+1] = position - tangent * realWidth; - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - mesh.Optimize(); - } - - private Vector3 getTangent(float t) { - Vector3 direction = bezier.getDirection(t); - direction.Normalize(); - return new Vector3(-direction.z, 0, direction.x); - } -} diff --git a/Assets/Scripts/FlatBezierRenderer.cs.meta b/Assets/Scripts/FlatBezierRenderer.cs.meta deleted file mode 100644 index 1fe6547..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 9bfb303c44d15ef468dc0d7334241e94 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatCircleRenderer.cs b/Assets/Scripts/FlatCircleRenderer.cs deleted file mode 100644 index 363b663..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatCircleRenderer { - public Mesh mesh = new Mesh(); - public float radius, width; - private int segments; - - public FlatCircleRenderer(float radius, float width, int segments) { - this.radius = radius; - this.width = width; - this.segments = segments; - update(); - } - - public void update() { - Vector3[] vertices = new Vector3[2*segments+6]; - int[] indices = new int[6*segments]; - for (int i = 0; i < segments; i++) { - float alpha = (float) i / segments * 2 * Mathf.PI; - indices[6*i ] = 2*i ; - indices[6*i+3] = 2*i ; - indices[6*i+5] = 2*i+1; - if (i == 0) { - indices[6*i+1] = 2*segments-2; - indices[6*i+2] = 2*segments-1; - indices[6*i+4] = 2*segments-1; - } else { - indices[6*i+1] = 2*i-2; - indices[6*i+2] = 2*i-1; - indices[6*i+4] = 2*i-1; - } - Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); - vertices[2*i ] = direction * radius; - vertices[2*i+1] = direction * (radius + width); - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - } -} diff --git a/Assets/Scripts/FlatCircleRenderer.cs.meta b/Assets/Scripts/FlatCircleRenderer.cs.meta deleted file mode 100644 index d10c238..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0fe81c7924a85b142a4a86201b08c241 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Node.cs b/Assets/Scripts/Node.cs deleted file mode 100644 index e3c4cbd..0000000 --- a/Assets/Scripts/Node.cs +++ /dev/null @@ -1,81 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Node { - public FlatCircleRenderer circle = new FlatCircleRenderer(0.2f, 0.05f, 32); - public FlatCircleRenderer fullCircle = new FlatCircleRenderer(0f, 1f, 32); - - public Vector3 position; - public List roads = new List(); - public GameObject gameObject; - - public Node(Vector3 position, Transform parent, Config config) { - gameObject = new GameObject(); - gameObject.transform.position = position; - gameObject.transform.parent = parent; - gameObject.AddComponent().radius = 0.5f; - gameObject.layer = 7; - - GameObject nodeCircle = new GameObject(); - nodeCircle.AddComponent().material = config.roadEditMaterial; - nodeCircle.AddComponent().mesh = circle.mesh; - nodeCircle.transform.parent = gameObject.transform; - nodeCircle.transform.localPosition = Vector3.zero; - - GameObject nodeRoad = new GameObject(); - nodeRoad.AddComponent().material = config.roadMaterial; - nodeRoad.AddComponent().mesh = fullCircle.mesh; - nodeRoad.transform.parent = gameObject.transform; - nodeRoad.transform.localPosition = Vector3.zero; - 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(); - } - - override public bool Equals(object other) { - if ((other == null) || ! this.GetType().Equals(other.GetType())) { - return false; - } - if (other == this) { - return true; - } - return ((Node)other).position.Equals(position); - } - - public void pull(Vector3 position) { - this.position = position; - gameObject.transform.position = position; - foreach (Road road in roads) { - 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/Node.cs.meta b/Assets/Scripts/Node.cs.meta deleted file mode 100644 index 8bbedf1..0000000 --- a/Assets/Scripts/Node.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 3cbed1dad7ec67d4dbe8602aea1e2979 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Primitives.meta b/Assets/Scripts/Primitives.meta new file mode 100644 index 0000000..0cf8895 --- /dev/null +++ b/Assets/Scripts/Primitives.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 6d1a8c7f33189a240b4d749356f53399 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/Bezier.cs b/Assets/Scripts/Primitives/Bezier.cs new file mode 100644 index 0000000..ea19006 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs @@ -0,0 +1,39 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Bezier { + public Vector3 A, B, C, D; + + public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { + this.A = A; + this.B = B; + this.C = C; + this.D = D; + } + + public Bezier() { + this.A = Vector3.zero; + this.B = Vector3.zero; + this.C = Vector3.zero; + this.D = Vector3.zero; + } + + public Vector3 getPosition(float t) { + float T = 1.0f - t; + return + A * ( T*T*T) + + B * (3*T*T*t) + + C * (3*T*t*t) + + D * ( t*t*t); + } + + public Vector3 getDirection(float t) { + float T = 1.0f - t; + return + A * (-3*T*T ) + + B * ( 3*T*T - 6*T*t) + + C * (-3*t*t + 6*T*t) + + D * ( 3*t*t ); + } +} diff --git a/Assets/Scripts/Primitives/Bezier.cs.meta b/Assets/Scripts/Primitives/Bezier.cs.meta new file mode 100644 index 0000000..14630e2 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 284cf9d52e385aa4ebba97eb8b43b8a6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs b/Assets/Scripts/Primitives/FlatBezierRenderer.cs new file mode 100644 index 0000000..b8dad15 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs @@ -0,0 +1,51 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FlatBezierRenderer { + private int percision; + private float width; + public Bezier bezier; + public Mesh mesh = new Mesh(); + public FlatBezierRenderer(Bezier bezier, int percision, float width) { + this.percision = percision; + this.width = width; + this.bezier = bezier; + update(); + } + + public void update() { + float realWidth = width / 2; + int vertexCount = 6 * (percision+1); + Vector3[] vertices = new Vector3[vertexCount / 3]; + int[] indices = new int[vertexCount]; + Vector3 position = bezier.getPosition(0.0f); + Vector3 tangent = getTangent(0.0f); + vertices[0] = position + tangent * realWidth; + vertices[1] = position - tangent * realWidth; + for (int i = 1; i <= percision; i++) { + float t = (float) i / percision; + indices[6*i ] = 2*i ; + indices[6*i+1] = 2*i-1; + indices[6*i+2] = 2*i-2; + indices[6*i+3] = 2*i+1; + indices[6*i+4] = 2*i-1; + indices[6*i+5] = 2*i ; + + position = bezier.getPosition(t); + tangent = getTangent(t); + vertices[2*i ] = position + tangent * realWidth; + vertices[2*i+1] = position - tangent * realWidth; + } + mesh.Clear(); + mesh.vertices = vertices; + mesh.triangles = indices; + mesh.Optimize(); + } + + private Vector3 getTangent(float t) { + Vector3 direction = bezier.getDirection(t); + direction.Normalize(); + return new Vector3(-direction.z, 0, direction.x); + } +} diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta b/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta new file mode 100644 index 0000000..1fe6547 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9bfb303c44d15ef468dc0d7334241e94 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Bezier.cs b/Assets/Scripts/Bezier.cs deleted file mode 100644 index ea19006..0000000 --- a/Assets/Scripts/Bezier.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Bezier { - public Vector3 A, B, C, D; - - public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { - this.A = A; - this.B = B; - this.C = C; - this.D = D; - } - - public Bezier() { - this.A = Vector3.zero; - this.B = Vector3.zero; - this.C = Vector3.zero; - this.D = Vector3.zero; - } - - public Vector3 getPosition(float t) { - float T = 1.0f - t; - return - A * ( T*T*T) + - B * (3*T*T*t) + - C * (3*T*t*t) + - D * ( t*t*t); - } - - public Vector3 getDirection(float t) { - float T = 1.0f - t; - return - A * (-3*T*T ) + - B * ( 3*T*T - 6*T*t) + - C * (-3*t*t + 6*T*t) + - D * ( 3*t*t ); - } -} diff --git a/Assets/Scripts/Bezier.cs.meta b/Assets/Scripts/Bezier.cs.meta deleted file mode 100644 index 14630e2..0000000 --- a/Assets/Scripts/Bezier.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 284cf9d52e385aa4ebba97eb8b43b8a6 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatBezierRenderer.cs b/Assets/Scripts/FlatBezierRenderer.cs deleted file mode 100644 index b8dad15..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatBezierRenderer { - private int percision; - private float width; - public Bezier bezier; - public Mesh mesh = new Mesh(); - public FlatBezierRenderer(Bezier bezier, int percision, float width) { - this.percision = percision; - this.width = width; - this.bezier = bezier; - update(); - } - - public void update() { - float realWidth = width / 2; - int vertexCount = 6 * (percision+1); - Vector3[] vertices = new Vector3[vertexCount / 3]; - int[] indices = new int[vertexCount]; - Vector3 position = bezier.getPosition(0.0f); - Vector3 tangent = getTangent(0.0f); - vertices[0] = position + tangent * realWidth; - vertices[1] = position - tangent * realWidth; - for (int i = 1; i <= percision; i++) { - float t = (float) i / percision; - indices[6*i ] = 2*i ; - indices[6*i+1] = 2*i-1; - indices[6*i+2] = 2*i-2; - indices[6*i+3] = 2*i+1; - indices[6*i+4] = 2*i-1; - indices[6*i+5] = 2*i ; - - position = bezier.getPosition(t); - tangent = getTangent(t); - vertices[2*i ] = position + tangent * realWidth; - vertices[2*i+1] = position - tangent * realWidth; - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - mesh.Optimize(); - } - - private Vector3 getTangent(float t) { - Vector3 direction = bezier.getDirection(t); - direction.Normalize(); - return new Vector3(-direction.z, 0, direction.x); - } -} diff --git a/Assets/Scripts/FlatBezierRenderer.cs.meta b/Assets/Scripts/FlatBezierRenderer.cs.meta deleted file mode 100644 index 1fe6547..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 9bfb303c44d15ef468dc0d7334241e94 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatCircleRenderer.cs b/Assets/Scripts/FlatCircleRenderer.cs deleted file mode 100644 index 363b663..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatCircleRenderer { - public Mesh mesh = new Mesh(); - public float radius, width; - private int segments; - - public FlatCircleRenderer(float radius, float width, int segments) { - this.radius = radius; - this.width = width; - this.segments = segments; - update(); - } - - public void update() { - Vector3[] vertices = new Vector3[2*segments+6]; - int[] indices = new int[6*segments]; - for (int i = 0; i < segments; i++) { - float alpha = (float) i / segments * 2 * Mathf.PI; - indices[6*i ] = 2*i ; - indices[6*i+3] = 2*i ; - indices[6*i+5] = 2*i+1; - if (i == 0) { - indices[6*i+1] = 2*segments-2; - indices[6*i+2] = 2*segments-1; - indices[6*i+4] = 2*segments-1; - } else { - indices[6*i+1] = 2*i-2; - indices[6*i+2] = 2*i-1; - indices[6*i+4] = 2*i-1; - } - Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); - vertices[2*i ] = direction * radius; - vertices[2*i+1] = direction * (radius + width); - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - } -} diff --git a/Assets/Scripts/FlatCircleRenderer.cs.meta b/Assets/Scripts/FlatCircleRenderer.cs.meta deleted file mode 100644 index d10c238..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0fe81c7924a85b142a4a86201b08c241 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Node.cs b/Assets/Scripts/Node.cs deleted file mode 100644 index e3c4cbd..0000000 --- a/Assets/Scripts/Node.cs +++ /dev/null @@ -1,81 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Node { - public FlatCircleRenderer circle = new FlatCircleRenderer(0.2f, 0.05f, 32); - public FlatCircleRenderer fullCircle = new FlatCircleRenderer(0f, 1f, 32); - - public Vector3 position; - public List roads = new List(); - public GameObject gameObject; - - public Node(Vector3 position, Transform parent, Config config) { - gameObject = new GameObject(); - gameObject.transform.position = position; - gameObject.transform.parent = parent; - gameObject.AddComponent().radius = 0.5f; - gameObject.layer = 7; - - GameObject nodeCircle = new GameObject(); - nodeCircle.AddComponent().material = config.roadEditMaterial; - nodeCircle.AddComponent().mesh = circle.mesh; - nodeCircle.transform.parent = gameObject.transform; - nodeCircle.transform.localPosition = Vector3.zero; - - GameObject nodeRoad = new GameObject(); - nodeRoad.AddComponent().material = config.roadMaterial; - nodeRoad.AddComponent().mesh = fullCircle.mesh; - nodeRoad.transform.parent = gameObject.transform; - nodeRoad.transform.localPosition = Vector3.zero; - 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(); - } - - override public bool Equals(object other) { - if ((other == null) || ! this.GetType().Equals(other.GetType())) { - return false; - } - if (other == this) { - return true; - } - return ((Node)other).position.Equals(position); - } - - public void pull(Vector3 position) { - this.position = position; - gameObject.transform.position = position; - foreach (Road road in roads) { - 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/Node.cs.meta b/Assets/Scripts/Node.cs.meta deleted file mode 100644 index 8bbedf1..0000000 --- a/Assets/Scripts/Node.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 3cbed1dad7ec67d4dbe8602aea1e2979 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Primitives.meta b/Assets/Scripts/Primitives.meta new file mode 100644 index 0000000..0cf8895 --- /dev/null +++ b/Assets/Scripts/Primitives.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 6d1a8c7f33189a240b4d749356f53399 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/Bezier.cs b/Assets/Scripts/Primitives/Bezier.cs new file mode 100644 index 0000000..ea19006 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs @@ -0,0 +1,39 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Bezier { + public Vector3 A, B, C, D; + + public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { + this.A = A; + this.B = B; + this.C = C; + this.D = D; + } + + public Bezier() { + this.A = Vector3.zero; + this.B = Vector3.zero; + this.C = Vector3.zero; + this.D = Vector3.zero; + } + + public Vector3 getPosition(float t) { + float T = 1.0f - t; + return + A * ( T*T*T) + + B * (3*T*T*t) + + C * (3*T*t*t) + + D * ( t*t*t); + } + + public Vector3 getDirection(float t) { + float T = 1.0f - t; + return + A * (-3*T*T ) + + B * ( 3*T*T - 6*T*t) + + C * (-3*t*t + 6*T*t) + + D * ( 3*t*t ); + } +} diff --git a/Assets/Scripts/Primitives/Bezier.cs.meta b/Assets/Scripts/Primitives/Bezier.cs.meta new file mode 100644 index 0000000..14630e2 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 284cf9d52e385aa4ebba97eb8b43b8a6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs b/Assets/Scripts/Primitives/FlatBezierRenderer.cs new file mode 100644 index 0000000..b8dad15 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs @@ -0,0 +1,51 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FlatBezierRenderer { + private int percision; + private float width; + public Bezier bezier; + public Mesh mesh = new Mesh(); + public FlatBezierRenderer(Bezier bezier, int percision, float width) { + this.percision = percision; + this.width = width; + this.bezier = bezier; + update(); + } + + public void update() { + float realWidth = width / 2; + int vertexCount = 6 * (percision+1); + Vector3[] vertices = new Vector3[vertexCount / 3]; + int[] indices = new int[vertexCount]; + Vector3 position = bezier.getPosition(0.0f); + Vector3 tangent = getTangent(0.0f); + vertices[0] = position + tangent * realWidth; + vertices[1] = position - tangent * realWidth; + for (int i = 1; i <= percision; i++) { + float t = (float) i / percision; + indices[6*i ] = 2*i ; + indices[6*i+1] = 2*i-1; + indices[6*i+2] = 2*i-2; + indices[6*i+3] = 2*i+1; + indices[6*i+4] = 2*i-1; + indices[6*i+5] = 2*i ; + + position = bezier.getPosition(t); + tangent = getTangent(t); + vertices[2*i ] = position + tangent * realWidth; + vertices[2*i+1] = position - tangent * realWidth; + } + mesh.Clear(); + mesh.vertices = vertices; + mesh.triangles = indices; + mesh.Optimize(); + } + + private Vector3 getTangent(float t) { + Vector3 direction = bezier.getDirection(t); + direction.Normalize(); + return new Vector3(-direction.z, 0, direction.x); + } +} diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta b/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta new file mode 100644 index 0000000..1fe6547 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9bfb303c44d15ef468dc0d7334241e94 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/FlatCircleRenderer.cs b/Assets/Scripts/Primitives/FlatCircleRenderer.cs new file mode 100644 index 0000000..363b663 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatCircleRenderer.cs @@ -0,0 +1,42 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FlatCircleRenderer { + public Mesh mesh = new Mesh(); + public float radius, width; + private int segments; + + public FlatCircleRenderer(float radius, float width, int segments) { + this.radius = radius; + this.width = width; + this.segments = segments; + update(); + } + + public void update() { + Vector3[] vertices = new Vector3[2*segments+6]; + int[] indices = new int[6*segments]; + for (int i = 0; i < segments; i++) { + float alpha = (float) i / segments * 2 * Mathf.PI; + indices[6*i ] = 2*i ; + indices[6*i+3] = 2*i ; + indices[6*i+5] = 2*i+1; + if (i == 0) { + indices[6*i+1] = 2*segments-2; + indices[6*i+2] = 2*segments-1; + indices[6*i+4] = 2*segments-1; + } else { + indices[6*i+1] = 2*i-2; + indices[6*i+2] = 2*i-1; + indices[6*i+4] = 2*i-1; + } + Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); + vertices[2*i ] = direction * radius; + vertices[2*i+1] = direction * (radius + width); + } + mesh.Clear(); + mesh.vertices = vertices; + mesh.triangles = indices; + } +} diff --git a/Assets/Scripts/Bezier.cs b/Assets/Scripts/Bezier.cs deleted file mode 100644 index ea19006..0000000 --- a/Assets/Scripts/Bezier.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Bezier { - public Vector3 A, B, C, D; - - public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { - this.A = A; - this.B = B; - this.C = C; - this.D = D; - } - - public Bezier() { - this.A = Vector3.zero; - this.B = Vector3.zero; - this.C = Vector3.zero; - this.D = Vector3.zero; - } - - public Vector3 getPosition(float t) { - float T = 1.0f - t; - return - A * ( T*T*T) + - B * (3*T*T*t) + - C * (3*T*t*t) + - D * ( t*t*t); - } - - public Vector3 getDirection(float t) { - float T = 1.0f - t; - return - A * (-3*T*T ) + - B * ( 3*T*T - 6*T*t) + - C * (-3*t*t + 6*T*t) + - D * ( 3*t*t ); - } -} diff --git a/Assets/Scripts/Bezier.cs.meta b/Assets/Scripts/Bezier.cs.meta deleted file mode 100644 index 14630e2..0000000 --- a/Assets/Scripts/Bezier.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 284cf9d52e385aa4ebba97eb8b43b8a6 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatBezierRenderer.cs b/Assets/Scripts/FlatBezierRenderer.cs deleted file mode 100644 index b8dad15..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatBezierRenderer { - private int percision; - private float width; - public Bezier bezier; - public Mesh mesh = new Mesh(); - public FlatBezierRenderer(Bezier bezier, int percision, float width) { - this.percision = percision; - this.width = width; - this.bezier = bezier; - update(); - } - - public void update() { - float realWidth = width / 2; - int vertexCount = 6 * (percision+1); - Vector3[] vertices = new Vector3[vertexCount / 3]; - int[] indices = new int[vertexCount]; - Vector3 position = bezier.getPosition(0.0f); - Vector3 tangent = getTangent(0.0f); - vertices[0] = position + tangent * realWidth; - vertices[1] = position - tangent * realWidth; - for (int i = 1; i <= percision; i++) { - float t = (float) i / percision; - indices[6*i ] = 2*i ; - indices[6*i+1] = 2*i-1; - indices[6*i+2] = 2*i-2; - indices[6*i+3] = 2*i+1; - indices[6*i+4] = 2*i-1; - indices[6*i+5] = 2*i ; - - position = bezier.getPosition(t); - tangent = getTangent(t); - vertices[2*i ] = position + tangent * realWidth; - vertices[2*i+1] = position - tangent * realWidth; - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - mesh.Optimize(); - } - - private Vector3 getTangent(float t) { - Vector3 direction = bezier.getDirection(t); - direction.Normalize(); - return new Vector3(-direction.z, 0, direction.x); - } -} diff --git a/Assets/Scripts/FlatBezierRenderer.cs.meta b/Assets/Scripts/FlatBezierRenderer.cs.meta deleted file mode 100644 index 1fe6547..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 9bfb303c44d15ef468dc0d7334241e94 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatCircleRenderer.cs b/Assets/Scripts/FlatCircleRenderer.cs deleted file mode 100644 index 363b663..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatCircleRenderer { - public Mesh mesh = new Mesh(); - public float radius, width; - private int segments; - - public FlatCircleRenderer(float radius, float width, int segments) { - this.radius = radius; - this.width = width; - this.segments = segments; - update(); - } - - public void update() { - Vector3[] vertices = new Vector3[2*segments+6]; - int[] indices = new int[6*segments]; - for (int i = 0; i < segments; i++) { - float alpha = (float) i / segments * 2 * Mathf.PI; - indices[6*i ] = 2*i ; - indices[6*i+3] = 2*i ; - indices[6*i+5] = 2*i+1; - if (i == 0) { - indices[6*i+1] = 2*segments-2; - indices[6*i+2] = 2*segments-1; - indices[6*i+4] = 2*segments-1; - } else { - indices[6*i+1] = 2*i-2; - indices[6*i+2] = 2*i-1; - indices[6*i+4] = 2*i-1; - } - Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); - vertices[2*i ] = direction * radius; - vertices[2*i+1] = direction * (radius + width); - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - } -} diff --git a/Assets/Scripts/FlatCircleRenderer.cs.meta b/Assets/Scripts/FlatCircleRenderer.cs.meta deleted file mode 100644 index d10c238..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0fe81c7924a85b142a4a86201b08c241 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Node.cs b/Assets/Scripts/Node.cs deleted file mode 100644 index e3c4cbd..0000000 --- a/Assets/Scripts/Node.cs +++ /dev/null @@ -1,81 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Node { - public FlatCircleRenderer circle = new FlatCircleRenderer(0.2f, 0.05f, 32); - public FlatCircleRenderer fullCircle = new FlatCircleRenderer(0f, 1f, 32); - - public Vector3 position; - public List roads = new List(); - public GameObject gameObject; - - public Node(Vector3 position, Transform parent, Config config) { - gameObject = new GameObject(); - gameObject.transform.position = position; - gameObject.transform.parent = parent; - gameObject.AddComponent().radius = 0.5f; - gameObject.layer = 7; - - GameObject nodeCircle = new GameObject(); - nodeCircle.AddComponent().material = config.roadEditMaterial; - nodeCircle.AddComponent().mesh = circle.mesh; - nodeCircle.transform.parent = gameObject.transform; - nodeCircle.transform.localPosition = Vector3.zero; - - GameObject nodeRoad = new GameObject(); - nodeRoad.AddComponent().material = config.roadMaterial; - nodeRoad.AddComponent().mesh = fullCircle.mesh; - nodeRoad.transform.parent = gameObject.transform; - nodeRoad.transform.localPosition = Vector3.zero; - 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(); - } - - override public bool Equals(object other) { - if ((other == null) || ! this.GetType().Equals(other.GetType())) { - return false; - } - if (other == this) { - return true; - } - return ((Node)other).position.Equals(position); - } - - public void pull(Vector3 position) { - this.position = position; - gameObject.transform.position = position; - foreach (Road road in roads) { - 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/Node.cs.meta b/Assets/Scripts/Node.cs.meta deleted file mode 100644 index 8bbedf1..0000000 --- a/Assets/Scripts/Node.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 3cbed1dad7ec67d4dbe8602aea1e2979 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Primitives.meta b/Assets/Scripts/Primitives.meta new file mode 100644 index 0000000..0cf8895 --- /dev/null +++ b/Assets/Scripts/Primitives.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 6d1a8c7f33189a240b4d749356f53399 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/Bezier.cs b/Assets/Scripts/Primitives/Bezier.cs new file mode 100644 index 0000000..ea19006 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs @@ -0,0 +1,39 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Bezier { + public Vector3 A, B, C, D; + + public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { + this.A = A; + this.B = B; + this.C = C; + this.D = D; + } + + public Bezier() { + this.A = Vector3.zero; + this.B = Vector3.zero; + this.C = Vector3.zero; + this.D = Vector3.zero; + } + + public Vector3 getPosition(float t) { + float T = 1.0f - t; + return + A * ( T*T*T) + + B * (3*T*T*t) + + C * (3*T*t*t) + + D * ( t*t*t); + } + + public Vector3 getDirection(float t) { + float T = 1.0f - t; + return + A * (-3*T*T ) + + B * ( 3*T*T - 6*T*t) + + C * (-3*t*t + 6*T*t) + + D * ( 3*t*t ); + } +} diff --git a/Assets/Scripts/Primitives/Bezier.cs.meta b/Assets/Scripts/Primitives/Bezier.cs.meta new file mode 100644 index 0000000..14630e2 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 284cf9d52e385aa4ebba97eb8b43b8a6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs b/Assets/Scripts/Primitives/FlatBezierRenderer.cs new file mode 100644 index 0000000..b8dad15 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs @@ -0,0 +1,51 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FlatBezierRenderer { + private int percision; + private float width; + public Bezier bezier; + public Mesh mesh = new Mesh(); + public FlatBezierRenderer(Bezier bezier, int percision, float width) { + this.percision = percision; + this.width = width; + this.bezier = bezier; + update(); + } + + public void update() { + float realWidth = width / 2; + int vertexCount = 6 * (percision+1); + Vector3[] vertices = new Vector3[vertexCount / 3]; + int[] indices = new int[vertexCount]; + Vector3 position = bezier.getPosition(0.0f); + Vector3 tangent = getTangent(0.0f); + vertices[0] = position + tangent * realWidth; + vertices[1] = position - tangent * realWidth; + for (int i = 1; i <= percision; i++) { + float t = (float) i / percision; + indices[6*i ] = 2*i ; + indices[6*i+1] = 2*i-1; + indices[6*i+2] = 2*i-2; + indices[6*i+3] = 2*i+1; + indices[6*i+4] = 2*i-1; + indices[6*i+5] = 2*i ; + + position = bezier.getPosition(t); + tangent = getTangent(t); + vertices[2*i ] = position + tangent * realWidth; + vertices[2*i+1] = position - tangent * realWidth; + } + mesh.Clear(); + mesh.vertices = vertices; + mesh.triangles = indices; + mesh.Optimize(); + } + + private Vector3 getTangent(float t) { + Vector3 direction = bezier.getDirection(t); + direction.Normalize(); + return new Vector3(-direction.z, 0, direction.x); + } +} diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta b/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta new file mode 100644 index 0000000..1fe6547 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9bfb303c44d15ef468dc0d7334241e94 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/FlatCircleRenderer.cs b/Assets/Scripts/Primitives/FlatCircleRenderer.cs new file mode 100644 index 0000000..363b663 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatCircleRenderer.cs @@ -0,0 +1,42 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FlatCircleRenderer { + public Mesh mesh = new Mesh(); + public float radius, width; + private int segments; + + public FlatCircleRenderer(float radius, float width, int segments) { + this.radius = radius; + this.width = width; + this.segments = segments; + update(); + } + + public void update() { + Vector3[] vertices = new Vector3[2*segments+6]; + int[] indices = new int[6*segments]; + for (int i = 0; i < segments; i++) { + float alpha = (float) i / segments * 2 * Mathf.PI; + indices[6*i ] = 2*i ; + indices[6*i+3] = 2*i ; + indices[6*i+5] = 2*i+1; + if (i == 0) { + indices[6*i+1] = 2*segments-2; + indices[6*i+2] = 2*segments-1; + indices[6*i+4] = 2*segments-1; + } else { + indices[6*i+1] = 2*i-2; + indices[6*i+2] = 2*i-1; + indices[6*i+4] = 2*i-1; + } + Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); + vertices[2*i ] = direction * radius; + vertices[2*i+1] = direction * (radius + width); + } + mesh.Clear(); + mesh.vertices = vertices; + mesh.triangles = indices; + } +} diff --git a/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta b/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta new file mode 100644 index 0000000..d10c238 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0fe81c7924a85b142a4a86201b08c241 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Bezier.cs b/Assets/Scripts/Bezier.cs deleted file mode 100644 index ea19006..0000000 --- a/Assets/Scripts/Bezier.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Bezier { - public Vector3 A, B, C, D; - - public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { - this.A = A; - this.B = B; - this.C = C; - this.D = D; - } - - public Bezier() { - this.A = Vector3.zero; - this.B = Vector3.zero; - this.C = Vector3.zero; - this.D = Vector3.zero; - } - - public Vector3 getPosition(float t) { - float T = 1.0f - t; - return - A * ( T*T*T) + - B * (3*T*T*t) + - C * (3*T*t*t) + - D * ( t*t*t); - } - - public Vector3 getDirection(float t) { - float T = 1.0f - t; - return - A * (-3*T*T ) + - B * ( 3*T*T - 6*T*t) + - C * (-3*t*t + 6*T*t) + - D * ( 3*t*t ); - } -} diff --git a/Assets/Scripts/Bezier.cs.meta b/Assets/Scripts/Bezier.cs.meta deleted file mode 100644 index 14630e2..0000000 --- a/Assets/Scripts/Bezier.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 284cf9d52e385aa4ebba97eb8b43b8a6 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatBezierRenderer.cs b/Assets/Scripts/FlatBezierRenderer.cs deleted file mode 100644 index b8dad15..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatBezierRenderer { - private int percision; - private float width; - public Bezier bezier; - public Mesh mesh = new Mesh(); - public FlatBezierRenderer(Bezier bezier, int percision, float width) { - this.percision = percision; - this.width = width; - this.bezier = bezier; - update(); - } - - public void update() { - float realWidth = width / 2; - int vertexCount = 6 * (percision+1); - Vector3[] vertices = new Vector3[vertexCount / 3]; - int[] indices = new int[vertexCount]; - Vector3 position = bezier.getPosition(0.0f); - Vector3 tangent = getTangent(0.0f); - vertices[0] = position + tangent * realWidth; - vertices[1] = position - tangent * realWidth; - for (int i = 1; i <= percision; i++) { - float t = (float) i / percision; - indices[6*i ] = 2*i ; - indices[6*i+1] = 2*i-1; - indices[6*i+2] = 2*i-2; - indices[6*i+3] = 2*i+1; - indices[6*i+4] = 2*i-1; - indices[6*i+5] = 2*i ; - - position = bezier.getPosition(t); - tangent = getTangent(t); - vertices[2*i ] = position + tangent * realWidth; - vertices[2*i+1] = position - tangent * realWidth; - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - mesh.Optimize(); - } - - private Vector3 getTangent(float t) { - Vector3 direction = bezier.getDirection(t); - direction.Normalize(); - return new Vector3(-direction.z, 0, direction.x); - } -} diff --git a/Assets/Scripts/FlatBezierRenderer.cs.meta b/Assets/Scripts/FlatBezierRenderer.cs.meta deleted file mode 100644 index 1fe6547..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 9bfb303c44d15ef468dc0d7334241e94 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatCircleRenderer.cs b/Assets/Scripts/FlatCircleRenderer.cs deleted file mode 100644 index 363b663..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatCircleRenderer { - public Mesh mesh = new Mesh(); - public float radius, width; - private int segments; - - public FlatCircleRenderer(float radius, float width, int segments) { - this.radius = radius; - this.width = width; - this.segments = segments; - update(); - } - - public void update() { - Vector3[] vertices = new Vector3[2*segments+6]; - int[] indices = new int[6*segments]; - for (int i = 0; i < segments; i++) { - float alpha = (float) i / segments * 2 * Mathf.PI; - indices[6*i ] = 2*i ; - indices[6*i+3] = 2*i ; - indices[6*i+5] = 2*i+1; - if (i == 0) { - indices[6*i+1] = 2*segments-2; - indices[6*i+2] = 2*segments-1; - indices[6*i+4] = 2*segments-1; - } else { - indices[6*i+1] = 2*i-2; - indices[6*i+2] = 2*i-1; - indices[6*i+4] = 2*i-1; - } - Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); - vertices[2*i ] = direction * radius; - vertices[2*i+1] = direction * (radius + width); - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - } -} diff --git a/Assets/Scripts/FlatCircleRenderer.cs.meta b/Assets/Scripts/FlatCircleRenderer.cs.meta deleted file mode 100644 index d10c238..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0fe81c7924a85b142a4a86201b08c241 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Node.cs b/Assets/Scripts/Node.cs deleted file mode 100644 index e3c4cbd..0000000 --- a/Assets/Scripts/Node.cs +++ /dev/null @@ -1,81 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Node { - public FlatCircleRenderer circle = new FlatCircleRenderer(0.2f, 0.05f, 32); - public FlatCircleRenderer fullCircle = new FlatCircleRenderer(0f, 1f, 32); - - public Vector3 position; - public List roads = new List(); - public GameObject gameObject; - - public Node(Vector3 position, Transform parent, Config config) { - gameObject = new GameObject(); - gameObject.transform.position = position; - gameObject.transform.parent = parent; - gameObject.AddComponent().radius = 0.5f; - gameObject.layer = 7; - - GameObject nodeCircle = new GameObject(); - nodeCircle.AddComponent().material = config.roadEditMaterial; - nodeCircle.AddComponent().mesh = circle.mesh; - nodeCircle.transform.parent = gameObject.transform; - nodeCircle.transform.localPosition = Vector3.zero; - - GameObject nodeRoad = new GameObject(); - nodeRoad.AddComponent().material = config.roadMaterial; - nodeRoad.AddComponent().mesh = fullCircle.mesh; - nodeRoad.transform.parent = gameObject.transform; - nodeRoad.transform.localPosition = Vector3.zero; - 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(); - } - - override public bool Equals(object other) { - if ((other == null) || ! this.GetType().Equals(other.GetType())) { - return false; - } - if (other == this) { - return true; - } - return ((Node)other).position.Equals(position); - } - - public void pull(Vector3 position) { - this.position = position; - gameObject.transform.position = position; - foreach (Road road in roads) { - 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/Node.cs.meta b/Assets/Scripts/Node.cs.meta deleted file mode 100644 index 8bbedf1..0000000 --- a/Assets/Scripts/Node.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 3cbed1dad7ec67d4dbe8602aea1e2979 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Primitives.meta b/Assets/Scripts/Primitives.meta new file mode 100644 index 0000000..0cf8895 --- /dev/null +++ b/Assets/Scripts/Primitives.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 6d1a8c7f33189a240b4d749356f53399 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/Bezier.cs b/Assets/Scripts/Primitives/Bezier.cs new file mode 100644 index 0000000..ea19006 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs @@ -0,0 +1,39 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Bezier { + public Vector3 A, B, C, D; + + public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { + this.A = A; + this.B = B; + this.C = C; + this.D = D; + } + + public Bezier() { + this.A = Vector3.zero; + this.B = Vector3.zero; + this.C = Vector3.zero; + this.D = Vector3.zero; + } + + public Vector3 getPosition(float t) { + float T = 1.0f - t; + return + A * ( T*T*T) + + B * (3*T*T*t) + + C * (3*T*t*t) + + D * ( t*t*t); + } + + public Vector3 getDirection(float t) { + float T = 1.0f - t; + return + A * (-3*T*T ) + + B * ( 3*T*T - 6*T*t) + + C * (-3*t*t + 6*T*t) + + D * ( 3*t*t ); + } +} diff --git a/Assets/Scripts/Primitives/Bezier.cs.meta b/Assets/Scripts/Primitives/Bezier.cs.meta new file mode 100644 index 0000000..14630e2 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 284cf9d52e385aa4ebba97eb8b43b8a6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs b/Assets/Scripts/Primitives/FlatBezierRenderer.cs new file mode 100644 index 0000000..b8dad15 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs @@ -0,0 +1,51 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FlatBezierRenderer { + private int percision; + private float width; + public Bezier bezier; + public Mesh mesh = new Mesh(); + public FlatBezierRenderer(Bezier bezier, int percision, float width) { + this.percision = percision; + this.width = width; + this.bezier = bezier; + update(); + } + + public void update() { + float realWidth = width / 2; + int vertexCount = 6 * (percision+1); + Vector3[] vertices = new Vector3[vertexCount / 3]; + int[] indices = new int[vertexCount]; + Vector3 position = bezier.getPosition(0.0f); + Vector3 tangent = getTangent(0.0f); + vertices[0] = position + tangent * realWidth; + vertices[1] = position - tangent * realWidth; + for (int i = 1; i <= percision; i++) { + float t = (float) i / percision; + indices[6*i ] = 2*i ; + indices[6*i+1] = 2*i-1; + indices[6*i+2] = 2*i-2; + indices[6*i+3] = 2*i+1; + indices[6*i+4] = 2*i-1; + indices[6*i+5] = 2*i ; + + position = bezier.getPosition(t); + tangent = getTangent(t); + vertices[2*i ] = position + tangent * realWidth; + vertices[2*i+1] = position - tangent * realWidth; + } + mesh.Clear(); + mesh.vertices = vertices; + mesh.triangles = indices; + mesh.Optimize(); + } + + private Vector3 getTangent(float t) { + Vector3 direction = bezier.getDirection(t); + direction.Normalize(); + return new Vector3(-direction.z, 0, direction.x); + } +} diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta b/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta new file mode 100644 index 0000000..1fe6547 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9bfb303c44d15ef468dc0d7334241e94 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/FlatCircleRenderer.cs b/Assets/Scripts/Primitives/FlatCircleRenderer.cs new file mode 100644 index 0000000..363b663 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatCircleRenderer.cs @@ -0,0 +1,42 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FlatCircleRenderer { + public Mesh mesh = new Mesh(); + public float radius, width; + private int segments; + + public FlatCircleRenderer(float radius, float width, int segments) { + this.radius = radius; + this.width = width; + this.segments = segments; + update(); + } + + public void update() { + Vector3[] vertices = new Vector3[2*segments+6]; + int[] indices = new int[6*segments]; + for (int i = 0; i < segments; i++) { + float alpha = (float) i / segments * 2 * Mathf.PI; + indices[6*i ] = 2*i ; + indices[6*i+3] = 2*i ; + indices[6*i+5] = 2*i+1; + if (i == 0) { + indices[6*i+1] = 2*segments-2; + indices[6*i+2] = 2*segments-1; + indices[6*i+4] = 2*segments-1; + } else { + indices[6*i+1] = 2*i-2; + indices[6*i+2] = 2*i-1; + indices[6*i+4] = 2*i-1; + } + Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); + vertices[2*i ] = direction * radius; + vertices[2*i+1] = direction * (radius + width); + } + mesh.Clear(); + mesh.vertices = vertices; + mesh.triangles = indices; + } +} diff --git a/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta b/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta new file mode 100644 index 0000000..d10c238 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0fe81c7924a85b142a4a86201b08c241 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Road.cs b/Assets/Scripts/Road.cs deleted file mode 100644 index 0af2218..0000000 --- a/Assets/Scripts/Road.cs +++ /dev/null @@ -1,85 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Road { - public List nodes = new List(); - Bezier path; - FlatBezierRenderer pathLine, roadBody; - Config config; - MeshCollider collider; - - public Road(Node start, Node end, Config config) { - nodes.Add(start); - nodes.Add(end); - path = new Bezier(); - pathLine = new FlatBezierRenderer(path, 50, 0.05f); - roadBody = new FlatBezierRenderer(path, 50, 2f); - this.config = config; - } - - - public void initialize(Transform parent) { - GameObject child = new GameObject(); - child.transform.parent = parent; - GameObject lineChild = new GameObject(); - lineChild.transform.position = Vector3.zero; - lineChild.AddComponent().material = config.roadEditMaterial; - lineChild.AddComponent().mesh = pathLine.mesh; - lineChild.transform.parent = child.transform; - - GameObject roadChild = new GameObject(); - collider = roadChild.AddComponent(); - roadChild.transform.position = Vector3.zero; - roadChild.AddComponent().material = config.roadMaterial; - roadChild.transform.parent = child.transform; - roadChild.layer = 8; - roadChild.AddComponent().mesh = roadBody.mesh; - roadChild.AddComponent().road = this; - } - - public void update(bool updateOthers) { - path.A = nodes[0].position; - path.D = nodes[1].position; - if (nodes[0].roads.Count == 2) { - path.B = nodes[0].position + - 0.25f * (nodes[1].position - nodes[0].getOther(nodes[1]).position); - } else { - path.B = 0.5f * (nodes[0].position + nodes[1].position); - } - if (nodes[1].roads.Count == 2) { - path.C = nodes[1].position + - 0.25f * (nodes[0].position - nodes[1].getOther(nodes[0]).position); - } else { - path.C = 0.5f * (nodes[0].position + nodes[1].position); - } - if (updateOthers) { - foreach (Node node in nodes) { - node.lateUpdate(this); - } - } - pathLine.update(); - roadBody.update(); - collider.sharedMesh = roadBody.mesh; - } - - override public bool Equals(object other) { - if (other == null || !(other is Road)) { - return false; - } - if (other == this) { - return true; - } - Road otherRoad = (Road) other; - foreach (Node node in nodes) { - if (! otherRoad.nodes.Contains(node)) { - return false; - } - } - return true; - } - - public override int GetHashCode() { - return nodes[0].GetHashCode() << 16 | nodes[1].GetHashCode(); - } -} diff --git a/Assets/Scripts/Bezier.cs b/Assets/Scripts/Bezier.cs deleted file mode 100644 index ea19006..0000000 --- a/Assets/Scripts/Bezier.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Bezier { - public Vector3 A, B, C, D; - - public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { - this.A = A; - this.B = B; - this.C = C; - this.D = D; - } - - public Bezier() { - this.A = Vector3.zero; - this.B = Vector3.zero; - this.C = Vector3.zero; - this.D = Vector3.zero; - } - - public Vector3 getPosition(float t) { - float T = 1.0f - t; - return - A * ( T*T*T) + - B * (3*T*T*t) + - C * (3*T*t*t) + - D * ( t*t*t); - } - - public Vector3 getDirection(float t) { - float T = 1.0f - t; - return - A * (-3*T*T ) + - B * ( 3*T*T - 6*T*t) + - C * (-3*t*t + 6*T*t) + - D * ( 3*t*t ); - } -} diff --git a/Assets/Scripts/Bezier.cs.meta b/Assets/Scripts/Bezier.cs.meta deleted file mode 100644 index 14630e2..0000000 --- a/Assets/Scripts/Bezier.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 284cf9d52e385aa4ebba97eb8b43b8a6 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatBezierRenderer.cs b/Assets/Scripts/FlatBezierRenderer.cs deleted file mode 100644 index b8dad15..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatBezierRenderer { - private int percision; - private float width; - public Bezier bezier; - public Mesh mesh = new Mesh(); - public FlatBezierRenderer(Bezier bezier, int percision, float width) { - this.percision = percision; - this.width = width; - this.bezier = bezier; - update(); - } - - public void update() { - float realWidth = width / 2; - int vertexCount = 6 * (percision+1); - Vector3[] vertices = new Vector3[vertexCount / 3]; - int[] indices = new int[vertexCount]; - Vector3 position = bezier.getPosition(0.0f); - Vector3 tangent = getTangent(0.0f); - vertices[0] = position + tangent * realWidth; - vertices[1] = position - tangent * realWidth; - for (int i = 1; i <= percision; i++) { - float t = (float) i / percision; - indices[6*i ] = 2*i ; - indices[6*i+1] = 2*i-1; - indices[6*i+2] = 2*i-2; - indices[6*i+3] = 2*i+1; - indices[6*i+4] = 2*i-1; - indices[6*i+5] = 2*i ; - - position = bezier.getPosition(t); - tangent = getTangent(t); - vertices[2*i ] = position + tangent * realWidth; - vertices[2*i+1] = position - tangent * realWidth; - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - mesh.Optimize(); - } - - private Vector3 getTangent(float t) { - Vector3 direction = bezier.getDirection(t); - direction.Normalize(); - return new Vector3(-direction.z, 0, direction.x); - } -} diff --git a/Assets/Scripts/FlatBezierRenderer.cs.meta b/Assets/Scripts/FlatBezierRenderer.cs.meta deleted file mode 100644 index 1fe6547..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 9bfb303c44d15ef468dc0d7334241e94 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatCircleRenderer.cs b/Assets/Scripts/FlatCircleRenderer.cs deleted file mode 100644 index 363b663..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatCircleRenderer { - public Mesh mesh = new Mesh(); - public float radius, width; - private int segments; - - public FlatCircleRenderer(float radius, float width, int segments) { - this.radius = radius; - this.width = width; - this.segments = segments; - update(); - } - - public void update() { - Vector3[] vertices = new Vector3[2*segments+6]; - int[] indices = new int[6*segments]; - for (int i = 0; i < segments; i++) { - float alpha = (float) i / segments * 2 * Mathf.PI; - indices[6*i ] = 2*i ; - indices[6*i+3] = 2*i ; - indices[6*i+5] = 2*i+1; - if (i == 0) { - indices[6*i+1] = 2*segments-2; - indices[6*i+2] = 2*segments-1; - indices[6*i+4] = 2*segments-1; - } else { - indices[6*i+1] = 2*i-2; - indices[6*i+2] = 2*i-1; - indices[6*i+4] = 2*i-1; - } - Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); - vertices[2*i ] = direction * radius; - vertices[2*i+1] = direction * (radius + width); - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - } -} diff --git a/Assets/Scripts/FlatCircleRenderer.cs.meta b/Assets/Scripts/FlatCircleRenderer.cs.meta deleted file mode 100644 index d10c238..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0fe81c7924a85b142a4a86201b08c241 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Node.cs b/Assets/Scripts/Node.cs deleted file mode 100644 index e3c4cbd..0000000 --- a/Assets/Scripts/Node.cs +++ /dev/null @@ -1,81 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Node { - public FlatCircleRenderer circle = new FlatCircleRenderer(0.2f, 0.05f, 32); - public FlatCircleRenderer fullCircle = new FlatCircleRenderer(0f, 1f, 32); - - public Vector3 position; - public List roads = new List(); - public GameObject gameObject; - - public Node(Vector3 position, Transform parent, Config config) { - gameObject = new GameObject(); - gameObject.transform.position = position; - gameObject.transform.parent = parent; - gameObject.AddComponent().radius = 0.5f; - gameObject.layer = 7; - - GameObject nodeCircle = new GameObject(); - nodeCircle.AddComponent().material = config.roadEditMaterial; - nodeCircle.AddComponent().mesh = circle.mesh; - nodeCircle.transform.parent = gameObject.transform; - nodeCircle.transform.localPosition = Vector3.zero; - - GameObject nodeRoad = new GameObject(); - nodeRoad.AddComponent().material = config.roadMaterial; - nodeRoad.AddComponent().mesh = fullCircle.mesh; - nodeRoad.transform.parent = gameObject.transform; - nodeRoad.transform.localPosition = Vector3.zero; - 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(); - } - - override public bool Equals(object other) { - if ((other == null) || ! this.GetType().Equals(other.GetType())) { - return false; - } - if (other == this) { - return true; - } - return ((Node)other).position.Equals(position); - } - - public void pull(Vector3 position) { - this.position = position; - gameObject.transform.position = position; - foreach (Road road in roads) { - 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/Node.cs.meta b/Assets/Scripts/Node.cs.meta deleted file mode 100644 index 8bbedf1..0000000 --- a/Assets/Scripts/Node.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 3cbed1dad7ec67d4dbe8602aea1e2979 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Primitives.meta b/Assets/Scripts/Primitives.meta new file mode 100644 index 0000000..0cf8895 --- /dev/null +++ b/Assets/Scripts/Primitives.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 6d1a8c7f33189a240b4d749356f53399 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/Bezier.cs b/Assets/Scripts/Primitives/Bezier.cs new file mode 100644 index 0000000..ea19006 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs @@ -0,0 +1,39 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Bezier { + public Vector3 A, B, C, D; + + public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { + this.A = A; + this.B = B; + this.C = C; + this.D = D; + } + + public Bezier() { + this.A = Vector3.zero; + this.B = Vector3.zero; + this.C = Vector3.zero; + this.D = Vector3.zero; + } + + public Vector3 getPosition(float t) { + float T = 1.0f - t; + return + A * ( T*T*T) + + B * (3*T*T*t) + + C * (3*T*t*t) + + D * ( t*t*t); + } + + public Vector3 getDirection(float t) { + float T = 1.0f - t; + return + A * (-3*T*T ) + + B * ( 3*T*T - 6*T*t) + + C * (-3*t*t + 6*T*t) + + D * ( 3*t*t ); + } +} diff --git a/Assets/Scripts/Primitives/Bezier.cs.meta b/Assets/Scripts/Primitives/Bezier.cs.meta new file mode 100644 index 0000000..14630e2 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 284cf9d52e385aa4ebba97eb8b43b8a6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs b/Assets/Scripts/Primitives/FlatBezierRenderer.cs new file mode 100644 index 0000000..b8dad15 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs @@ -0,0 +1,51 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FlatBezierRenderer { + private int percision; + private float width; + public Bezier bezier; + public Mesh mesh = new Mesh(); + public FlatBezierRenderer(Bezier bezier, int percision, float width) { + this.percision = percision; + this.width = width; + this.bezier = bezier; + update(); + } + + public void update() { + float realWidth = width / 2; + int vertexCount = 6 * (percision+1); + Vector3[] vertices = new Vector3[vertexCount / 3]; + int[] indices = new int[vertexCount]; + Vector3 position = bezier.getPosition(0.0f); + Vector3 tangent = getTangent(0.0f); + vertices[0] = position + tangent * realWidth; + vertices[1] = position - tangent * realWidth; + for (int i = 1; i <= percision; i++) { + float t = (float) i / percision; + indices[6*i ] = 2*i ; + indices[6*i+1] = 2*i-1; + indices[6*i+2] = 2*i-2; + indices[6*i+3] = 2*i+1; + indices[6*i+4] = 2*i-1; + indices[6*i+5] = 2*i ; + + position = bezier.getPosition(t); + tangent = getTangent(t); + vertices[2*i ] = position + tangent * realWidth; + vertices[2*i+1] = position - tangent * realWidth; + } + mesh.Clear(); + mesh.vertices = vertices; + mesh.triangles = indices; + mesh.Optimize(); + } + + private Vector3 getTangent(float t) { + Vector3 direction = bezier.getDirection(t); + direction.Normalize(); + return new Vector3(-direction.z, 0, direction.x); + } +} diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta b/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta new file mode 100644 index 0000000..1fe6547 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9bfb303c44d15ef468dc0d7334241e94 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/FlatCircleRenderer.cs b/Assets/Scripts/Primitives/FlatCircleRenderer.cs new file mode 100644 index 0000000..363b663 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatCircleRenderer.cs @@ -0,0 +1,42 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FlatCircleRenderer { + public Mesh mesh = new Mesh(); + public float radius, width; + private int segments; + + public FlatCircleRenderer(float radius, float width, int segments) { + this.radius = radius; + this.width = width; + this.segments = segments; + update(); + } + + public void update() { + Vector3[] vertices = new Vector3[2*segments+6]; + int[] indices = new int[6*segments]; + for (int i = 0; i < segments; i++) { + float alpha = (float) i / segments * 2 * Mathf.PI; + indices[6*i ] = 2*i ; + indices[6*i+3] = 2*i ; + indices[6*i+5] = 2*i+1; + if (i == 0) { + indices[6*i+1] = 2*segments-2; + indices[6*i+2] = 2*segments-1; + indices[6*i+4] = 2*segments-1; + } else { + indices[6*i+1] = 2*i-2; + indices[6*i+2] = 2*i-1; + indices[6*i+4] = 2*i-1; + } + Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); + vertices[2*i ] = direction * radius; + vertices[2*i+1] = direction * (radius + width); + } + mesh.Clear(); + mesh.vertices = vertices; + mesh.triangles = indices; + } +} diff --git a/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta b/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta new file mode 100644 index 0000000..d10c238 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0fe81c7924a85b142a4a86201b08c241 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Road.cs b/Assets/Scripts/Road.cs deleted file mode 100644 index 0af2218..0000000 --- a/Assets/Scripts/Road.cs +++ /dev/null @@ -1,85 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Road { - public List nodes = new List(); - Bezier path; - FlatBezierRenderer pathLine, roadBody; - Config config; - MeshCollider collider; - - public Road(Node start, Node end, Config config) { - nodes.Add(start); - nodes.Add(end); - path = new Bezier(); - pathLine = new FlatBezierRenderer(path, 50, 0.05f); - roadBody = new FlatBezierRenderer(path, 50, 2f); - this.config = config; - } - - - public void initialize(Transform parent) { - GameObject child = new GameObject(); - child.transform.parent = parent; - GameObject lineChild = new GameObject(); - lineChild.transform.position = Vector3.zero; - lineChild.AddComponent().material = config.roadEditMaterial; - lineChild.AddComponent().mesh = pathLine.mesh; - lineChild.transform.parent = child.transform; - - GameObject roadChild = new GameObject(); - collider = roadChild.AddComponent(); - roadChild.transform.position = Vector3.zero; - roadChild.AddComponent().material = config.roadMaterial; - roadChild.transform.parent = child.transform; - roadChild.layer = 8; - roadChild.AddComponent().mesh = roadBody.mesh; - roadChild.AddComponent().road = this; - } - - public void update(bool updateOthers) { - path.A = nodes[0].position; - path.D = nodes[1].position; - if (nodes[0].roads.Count == 2) { - path.B = nodes[0].position + - 0.25f * (nodes[1].position - nodes[0].getOther(nodes[1]).position); - } else { - path.B = 0.5f * (nodes[0].position + nodes[1].position); - } - if (nodes[1].roads.Count == 2) { - path.C = nodes[1].position + - 0.25f * (nodes[0].position - nodes[1].getOther(nodes[0]).position); - } else { - path.C = 0.5f * (nodes[0].position + nodes[1].position); - } - if (updateOthers) { - foreach (Node node in nodes) { - node.lateUpdate(this); - } - } - pathLine.update(); - roadBody.update(); - collider.sharedMesh = roadBody.mesh; - } - - override public bool Equals(object other) { - if (other == null || !(other is Road)) { - return false; - } - if (other == this) { - return true; - } - Road otherRoad = (Road) other; - foreach (Node node in nodes) { - if (! otherRoad.nodes.Contains(node)) { - return false; - } - } - return true; - } - - public override int GetHashCode() { - return nodes[0].GetHashCode() << 16 | nodes[1].GetHashCode(); - } -} diff --git a/Assets/Scripts/Road.cs.meta b/Assets/Scripts/Road.cs.meta deleted file mode 100644 index 24dc94b..0000000 --- a/Assets/Scripts/Road.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0ab512a631c0cc4468e77b6a2dfeb31f -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Bezier.cs b/Assets/Scripts/Bezier.cs deleted file mode 100644 index ea19006..0000000 --- a/Assets/Scripts/Bezier.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Bezier { - public Vector3 A, B, C, D; - - public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { - this.A = A; - this.B = B; - this.C = C; - this.D = D; - } - - public Bezier() { - this.A = Vector3.zero; - this.B = Vector3.zero; - this.C = Vector3.zero; - this.D = Vector3.zero; - } - - public Vector3 getPosition(float t) { - float T = 1.0f - t; - return - A * ( T*T*T) + - B * (3*T*T*t) + - C * (3*T*t*t) + - D * ( t*t*t); - } - - public Vector3 getDirection(float t) { - float T = 1.0f - t; - return - A * (-3*T*T ) + - B * ( 3*T*T - 6*T*t) + - C * (-3*t*t + 6*T*t) + - D * ( 3*t*t ); - } -} diff --git a/Assets/Scripts/Bezier.cs.meta b/Assets/Scripts/Bezier.cs.meta deleted file mode 100644 index 14630e2..0000000 --- a/Assets/Scripts/Bezier.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 284cf9d52e385aa4ebba97eb8b43b8a6 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatBezierRenderer.cs b/Assets/Scripts/FlatBezierRenderer.cs deleted file mode 100644 index b8dad15..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatBezierRenderer { - private int percision; - private float width; - public Bezier bezier; - public Mesh mesh = new Mesh(); - public FlatBezierRenderer(Bezier bezier, int percision, float width) { - this.percision = percision; - this.width = width; - this.bezier = bezier; - update(); - } - - public void update() { - float realWidth = width / 2; - int vertexCount = 6 * (percision+1); - Vector3[] vertices = new Vector3[vertexCount / 3]; - int[] indices = new int[vertexCount]; - Vector3 position = bezier.getPosition(0.0f); - Vector3 tangent = getTangent(0.0f); - vertices[0] = position + tangent * realWidth; - vertices[1] = position - tangent * realWidth; - for (int i = 1; i <= percision; i++) { - float t = (float) i / percision; - indices[6*i ] = 2*i ; - indices[6*i+1] = 2*i-1; - indices[6*i+2] = 2*i-2; - indices[6*i+3] = 2*i+1; - indices[6*i+4] = 2*i-1; - indices[6*i+5] = 2*i ; - - position = bezier.getPosition(t); - tangent = getTangent(t); - vertices[2*i ] = position + tangent * realWidth; - vertices[2*i+1] = position - tangent * realWidth; - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - mesh.Optimize(); - } - - private Vector3 getTangent(float t) { - Vector3 direction = bezier.getDirection(t); - direction.Normalize(); - return new Vector3(-direction.z, 0, direction.x); - } -} diff --git a/Assets/Scripts/FlatBezierRenderer.cs.meta b/Assets/Scripts/FlatBezierRenderer.cs.meta deleted file mode 100644 index 1fe6547..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 9bfb303c44d15ef468dc0d7334241e94 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatCircleRenderer.cs b/Assets/Scripts/FlatCircleRenderer.cs deleted file mode 100644 index 363b663..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatCircleRenderer { - public Mesh mesh = new Mesh(); - public float radius, width; - private int segments; - - public FlatCircleRenderer(float radius, float width, int segments) { - this.radius = radius; - this.width = width; - this.segments = segments; - update(); - } - - public void update() { - Vector3[] vertices = new Vector3[2*segments+6]; - int[] indices = new int[6*segments]; - for (int i = 0; i < segments; i++) { - float alpha = (float) i / segments * 2 * Mathf.PI; - indices[6*i ] = 2*i ; - indices[6*i+3] = 2*i ; - indices[6*i+5] = 2*i+1; - if (i == 0) { - indices[6*i+1] = 2*segments-2; - indices[6*i+2] = 2*segments-1; - indices[6*i+4] = 2*segments-1; - } else { - indices[6*i+1] = 2*i-2; - indices[6*i+2] = 2*i-1; - indices[6*i+4] = 2*i-1; - } - Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); - vertices[2*i ] = direction * radius; - vertices[2*i+1] = direction * (radius + width); - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - } -} diff --git a/Assets/Scripts/FlatCircleRenderer.cs.meta b/Assets/Scripts/FlatCircleRenderer.cs.meta deleted file mode 100644 index d10c238..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0fe81c7924a85b142a4a86201b08c241 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Node.cs b/Assets/Scripts/Node.cs deleted file mode 100644 index e3c4cbd..0000000 --- a/Assets/Scripts/Node.cs +++ /dev/null @@ -1,81 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Node { - public FlatCircleRenderer circle = new FlatCircleRenderer(0.2f, 0.05f, 32); - public FlatCircleRenderer fullCircle = new FlatCircleRenderer(0f, 1f, 32); - - public Vector3 position; - public List roads = new List(); - public GameObject gameObject; - - public Node(Vector3 position, Transform parent, Config config) { - gameObject = new GameObject(); - gameObject.transform.position = position; - gameObject.transform.parent = parent; - gameObject.AddComponent().radius = 0.5f; - gameObject.layer = 7; - - GameObject nodeCircle = new GameObject(); - nodeCircle.AddComponent().material = config.roadEditMaterial; - nodeCircle.AddComponent().mesh = circle.mesh; - nodeCircle.transform.parent = gameObject.transform; - nodeCircle.transform.localPosition = Vector3.zero; - - GameObject nodeRoad = new GameObject(); - nodeRoad.AddComponent().material = config.roadMaterial; - nodeRoad.AddComponent().mesh = fullCircle.mesh; - nodeRoad.transform.parent = gameObject.transform; - nodeRoad.transform.localPosition = Vector3.zero; - 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(); - } - - override public bool Equals(object other) { - if ((other == null) || ! this.GetType().Equals(other.GetType())) { - return false; - } - if (other == this) { - return true; - } - return ((Node)other).position.Equals(position); - } - - public void pull(Vector3 position) { - this.position = position; - gameObject.transform.position = position; - foreach (Road road in roads) { - 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/Node.cs.meta b/Assets/Scripts/Node.cs.meta deleted file mode 100644 index 8bbedf1..0000000 --- a/Assets/Scripts/Node.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 3cbed1dad7ec67d4dbe8602aea1e2979 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Primitives.meta b/Assets/Scripts/Primitives.meta new file mode 100644 index 0000000..0cf8895 --- /dev/null +++ b/Assets/Scripts/Primitives.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 6d1a8c7f33189a240b4d749356f53399 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/Bezier.cs b/Assets/Scripts/Primitives/Bezier.cs new file mode 100644 index 0000000..ea19006 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs @@ -0,0 +1,39 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Bezier { + public Vector3 A, B, C, D; + + public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { + this.A = A; + this.B = B; + this.C = C; + this.D = D; + } + + public Bezier() { + this.A = Vector3.zero; + this.B = Vector3.zero; + this.C = Vector3.zero; + this.D = Vector3.zero; + } + + public Vector3 getPosition(float t) { + float T = 1.0f - t; + return + A * ( T*T*T) + + B * (3*T*T*t) + + C * (3*T*t*t) + + D * ( t*t*t); + } + + public Vector3 getDirection(float t) { + float T = 1.0f - t; + return + A * (-3*T*T ) + + B * ( 3*T*T - 6*T*t) + + C * (-3*t*t + 6*T*t) + + D * ( 3*t*t ); + } +} diff --git a/Assets/Scripts/Primitives/Bezier.cs.meta b/Assets/Scripts/Primitives/Bezier.cs.meta new file mode 100644 index 0000000..14630e2 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 284cf9d52e385aa4ebba97eb8b43b8a6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs b/Assets/Scripts/Primitives/FlatBezierRenderer.cs new file mode 100644 index 0000000..b8dad15 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs @@ -0,0 +1,51 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FlatBezierRenderer { + private int percision; + private float width; + public Bezier bezier; + public Mesh mesh = new Mesh(); + public FlatBezierRenderer(Bezier bezier, int percision, float width) { + this.percision = percision; + this.width = width; + this.bezier = bezier; + update(); + } + + public void update() { + float realWidth = width / 2; + int vertexCount = 6 * (percision+1); + Vector3[] vertices = new Vector3[vertexCount / 3]; + int[] indices = new int[vertexCount]; + Vector3 position = bezier.getPosition(0.0f); + Vector3 tangent = getTangent(0.0f); + vertices[0] = position + tangent * realWidth; + vertices[1] = position - tangent * realWidth; + for (int i = 1; i <= percision; i++) { + float t = (float) i / percision; + indices[6*i ] = 2*i ; + indices[6*i+1] = 2*i-1; + indices[6*i+2] = 2*i-2; + indices[6*i+3] = 2*i+1; + indices[6*i+4] = 2*i-1; + indices[6*i+5] = 2*i ; + + position = bezier.getPosition(t); + tangent = getTangent(t); + vertices[2*i ] = position + tangent * realWidth; + vertices[2*i+1] = position - tangent * realWidth; + } + mesh.Clear(); + mesh.vertices = vertices; + mesh.triangles = indices; + mesh.Optimize(); + } + + private Vector3 getTangent(float t) { + Vector3 direction = bezier.getDirection(t); + direction.Normalize(); + return new Vector3(-direction.z, 0, direction.x); + } +} diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta b/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta new file mode 100644 index 0000000..1fe6547 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9bfb303c44d15ef468dc0d7334241e94 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/FlatCircleRenderer.cs b/Assets/Scripts/Primitives/FlatCircleRenderer.cs new file mode 100644 index 0000000..363b663 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatCircleRenderer.cs @@ -0,0 +1,42 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FlatCircleRenderer { + public Mesh mesh = new Mesh(); + public float radius, width; + private int segments; + + public FlatCircleRenderer(float radius, float width, int segments) { + this.radius = radius; + this.width = width; + this.segments = segments; + update(); + } + + public void update() { + Vector3[] vertices = new Vector3[2*segments+6]; + int[] indices = new int[6*segments]; + for (int i = 0; i < segments; i++) { + float alpha = (float) i / segments * 2 * Mathf.PI; + indices[6*i ] = 2*i ; + indices[6*i+3] = 2*i ; + indices[6*i+5] = 2*i+1; + if (i == 0) { + indices[6*i+1] = 2*segments-2; + indices[6*i+2] = 2*segments-1; + indices[6*i+4] = 2*segments-1; + } else { + indices[6*i+1] = 2*i-2; + indices[6*i+2] = 2*i-1; + indices[6*i+4] = 2*i-1; + } + Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); + vertices[2*i ] = direction * radius; + vertices[2*i+1] = direction * (radius + width); + } + mesh.Clear(); + mesh.vertices = vertices; + mesh.triangles = indices; + } +} diff --git a/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta b/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta new file mode 100644 index 0000000..d10c238 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0fe81c7924a85b142a4a86201b08c241 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Road.cs b/Assets/Scripts/Road.cs deleted file mode 100644 index 0af2218..0000000 --- a/Assets/Scripts/Road.cs +++ /dev/null @@ -1,85 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Road { - public List nodes = new List(); - Bezier path; - FlatBezierRenderer pathLine, roadBody; - Config config; - MeshCollider collider; - - public Road(Node start, Node end, Config config) { - nodes.Add(start); - nodes.Add(end); - path = new Bezier(); - pathLine = new FlatBezierRenderer(path, 50, 0.05f); - roadBody = new FlatBezierRenderer(path, 50, 2f); - this.config = config; - } - - - public void initialize(Transform parent) { - GameObject child = new GameObject(); - child.transform.parent = parent; - GameObject lineChild = new GameObject(); - lineChild.transform.position = Vector3.zero; - lineChild.AddComponent().material = config.roadEditMaterial; - lineChild.AddComponent().mesh = pathLine.mesh; - lineChild.transform.parent = child.transform; - - GameObject roadChild = new GameObject(); - collider = roadChild.AddComponent(); - roadChild.transform.position = Vector3.zero; - roadChild.AddComponent().material = config.roadMaterial; - roadChild.transform.parent = child.transform; - roadChild.layer = 8; - roadChild.AddComponent().mesh = roadBody.mesh; - roadChild.AddComponent().road = this; - } - - public void update(bool updateOthers) { - path.A = nodes[0].position; - path.D = nodes[1].position; - if (nodes[0].roads.Count == 2) { - path.B = nodes[0].position + - 0.25f * (nodes[1].position - nodes[0].getOther(nodes[1]).position); - } else { - path.B = 0.5f * (nodes[0].position + nodes[1].position); - } - if (nodes[1].roads.Count == 2) { - path.C = nodes[1].position + - 0.25f * (nodes[0].position - nodes[1].getOther(nodes[0]).position); - } else { - path.C = 0.5f * (nodes[0].position + nodes[1].position); - } - if (updateOthers) { - foreach (Node node in nodes) { - node.lateUpdate(this); - } - } - pathLine.update(); - roadBody.update(); - collider.sharedMesh = roadBody.mesh; - } - - override public bool Equals(object other) { - if (other == null || !(other is Road)) { - return false; - } - if (other == this) { - return true; - } - Road otherRoad = (Road) other; - foreach (Node node in nodes) { - if (! otherRoad.nodes.Contains(node)) { - return false; - } - } - return true; - } - - public override int GetHashCode() { - return nodes[0].GetHashCode() << 16 | nodes[1].GetHashCode(); - } -} diff --git a/Assets/Scripts/Road.cs.meta b/Assets/Scripts/Road.cs.meta deleted file mode 100644 index 24dc94b..0000000 --- a/Assets/Scripts/Road.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0ab512a631c0cc4468e77b6a2dfeb31f -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/RoadData.cs b/Assets/Scripts/RoadData.cs deleted file mode 100644 index eb620ba..0000000 --- a/Assets/Scripts/RoadData.cs +++ /dev/null @@ -1,7 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class RoadData : MonoBehaviour { - public Road road; -} diff --git a/Assets/Scripts/Bezier.cs b/Assets/Scripts/Bezier.cs deleted file mode 100644 index ea19006..0000000 --- a/Assets/Scripts/Bezier.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Bezier { - public Vector3 A, B, C, D; - - public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { - this.A = A; - this.B = B; - this.C = C; - this.D = D; - } - - public Bezier() { - this.A = Vector3.zero; - this.B = Vector3.zero; - this.C = Vector3.zero; - this.D = Vector3.zero; - } - - public Vector3 getPosition(float t) { - float T = 1.0f - t; - return - A * ( T*T*T) + - B * (3*T*T*t) + - C * (3*T*t*t) + - D * ( t*t*t); - } - - public Vector3 getDirection(float t) { - float T = 1.0f - t; - return - A * (-3*T*T ) + - B * ( 3*T*T - 6*T*t) + - C * (-3*t*t + 6*T*t) + - D * ( 3*t*t ); - } -} diff --git a/Assets/Scripts/Bezier.cs.meta b/Assets/Scripts/Bezier.cs.meta deleted file mode 100644 index 14630e2..0000000 --- a/Assets/Scripts/Bezier.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 284cf9d52e385aa4ebba97eb8b43b8a6 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatBezierRenderer.cs b/Assets/Scripts/FlatBezierRenderer.cs deleted file mode 100644 index b8dad15..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatBezierRenderer { - private int percision; - private float width; - public Bezier bezier; - public Mesh mesh = new Mesh(); - public FlatBezierRenderer(Bezier bezier, int percision, float width) { - this.percision = percision; - this.width = width; - this.bezier = bezier; - update(); - } - - public void update() { - float realWidth = width / 2; - int vertexCount = 6 * (percision+1); - Vector3[] vertices = new Vector3[vertexCount / 3]; - int[] indices = new int[vertexCount]; - Vector3 position = bezier.getPosition(0.0f); - Vector3 tangent = getTangent(0.0f); - vertices[0] = position + tangent * realWidth; - vertices[1] = position - tangent * realWidth; - for (int i = 1; i <= percision; i++) { - float t = (float) i / percision; - indices[6*i ] = 2*i ; - indices[6*i+1] = 2*i-1; - indices[6*i+2] = 2*i-2; - indices[6*i+3] = 2*i+1; - indices[6*i+4] = 2*i-1; - indices[6*i+5] = 2*i ; - - position = bezier.getPosition(t); - tangent = getTangent(t); - vertices[2*i ] = position + tangent * realWidth; - vertices[2*i+1] = position - tangent * realWidth; - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - mesh.Optimize(); - } - - private Vector3 getTangent(float t) { - Vector3 direction = bezier.getDirection(t); - direction.Normalize(); - return new Vector3(-direction.z, 0, direction.x); - } -} diff --git a/Assets/Scripts/FlatBezierRenderer.cs.meta b/Assets/Scripts/FlatBezierRenderer.cs.meta deleted file mode 100644 index 1fe6547..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 9bfb303c44d15ef468dc0d7334241e94 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatCircleRenderer.cs b/Assets/Scripts/FlatCircleRenderer.cs deleted file mode 100644 index 363b663..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatCircleRenderer { - public Mesh mesh = new Mesh(); - public float radius, width; - private int segments; - - public FlatCircleRenderer(float radius, float width, int segments) { - this.radius = radius; - this.width = width; - this.segments = segments; - update(); - } - - public void update() { - Vector3[] vertices = new Vector3[2*segments+6]; - int[] indices = new int[6*segments]; - for (int i = 0; i < segments; i++) { - float alpha = (float) i / segments * 2 * Mathf.PI; - indices[6*i ] = 2*i ; - indices[6*i+3] = 2*i ; - indices[6*i+5] = 2*i+1; - if (i == 0) { - indices[6*i+1] = 2*segments-2; - indices[6*i+2] = 2*segments-1; - indices[6*i+4] = 2*segments-1; - } else { - indices[6*i+1] = 2*i-2; - indices[6*i+2] = 2*i-1; - indices[6*i+4] = 2*i-1; - } - Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); - vertices[2*i ] = direction * radius; - vertices[2*i+1] = direction * (radius + width); - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - } -} diff --git a/Assets/Scripts/FlatCircleRenderer.cs.meta b/Assets/Scripts/FlatCircleRenderer.cs.meta deleted file mode 100644 index d10c238..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0fe81c7924a85b142a4a86201b08c241 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Node.cs b/Assets/Scripts/Node.cs deleted file mode 100644 index e3c4cbd..0000000 --- a/Assets/Scripts/Node.cs +++ /dev/null @@ -1,81 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Node { - public FlatCircleRenderer circle = new FlatCircleRenderer(0.2f, 0.05f, 32); - public FlatCircleRenderer fullCircle = new FlatCircleRenderer(0f, 1f, 32); - - public Vector3 position; - public List roads = new List(); - public GameObject gameObject; - - public Node(Vector3 position, Transform parent, Config config) { - gameObject = new GameObject(); - gameObject.transform.position = position; - gameObject.transform.parent = parent; - gameObject.AddComponent().radius = 0.5f; - gameObject.layer = 7; - - GameObject nodeCircle = new GameObject(); - nodeCircle.AddComponent().material = config.roadEditMaterial; - nodeCircle.AddComponent().mesh = circle.mesh; - nodeCircle.transform.parent = gameObject.transform; - nodeCircle.transform.localPosition = Vector3.zero; - - GameObject nodeRoad = new GameObject(); - nodeRoad.AddComponent().material = config.roadMaterial; - nodeRoad.AddComponent().mesh = fullCircle.mesh; - nodeRoad.transform.parent = gameObject.transform; - nodeRoad.transform.localPosition = Vector3.zero; - 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(); - } - - override public bool Equals(object other) { - if ((other == null) || ! this.GetType().Equals(other.GetType())) { - return false; - } - if (other == this) { - return true; - } - return ((Node)other).position.Equals(position); - } - - public void pull(Vector3 position) { - this.position = position; - gameObject.transform.position = position; - foreach (Road road in roads) { - 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/Node.cs.meta b/Assets/Scripts/Node.cs.meta deleted file mode 100644 index 8bbedf1..0000000 --- a/Assets/Scripts/Node.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 3cbed1dad7ec67d4dbe8602aea1e2979 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Primitives.meta b/Assets/Scripts/Primitives.meta new file mode 100644 index 0000000..0cf8895 --- /dev/null +++ b/Assets/Scripts/Primitives.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 6d1a8c7f33189a240b4d749356f53399 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/Bezier.cs b/Assets/Scripts/Primitives/Bezier.cs new file mode 100644 index 0000000..ea19006 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs @@ -0,0 +1,39 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Bezier { + public Vector3 A, B, C, D; + + public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { + this.A = A; + this.B = B; + this.C = C; + this.D = D; + } + + public Bezier() { + this.A = Vector3.zero; + this.B = Vector3.zero; + this.C = Vector3.zero; + this.D = Vector3.zero; + } + + public Vector3 getPosition(float t) { + float T = 1.0f - t; + return + A * ( T*T*T) + + B * (3*T*T*t) + + C * (3*T*t*t) + + D * ( t*t*t); + } + + public Vector3 getDirection(float t) { + float T = 1.0f - t; + return + A * (-3*T*T ) + + B * ( 3*T*T - 6*T*t) + + C * (-3*t*t + 6*T*t) + + D * ( 3*t*t ); + } +} diff --git a/Assets/Scripts/Primitives/Bezier.cs.meta b/Assets/Scripts/Primitives/Bezier.cs.meta new file mode 100644 index 0000000..14630e2 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 284cf9d52e385aa4ebba97eb8b43b8a6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs b/Assets/Scripts/Primitives/FlatBezierRenderer.cs new file mode 100644 index 0000000..b8dad15 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs @@ -0,0 +1,51 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FlatBezierRenderer { + private int percision; + private float width; + public Bezier bezier; + public Mesh mesh = new Mesh(); + public FlatBezierRenderer(Bezier bezier, int percision, float width) { + this.percision = percision; + this.width = width; + this.bezier = bezier; + update(); + } + + public void update() { + float realWidth = width / 2; + int vertexCount = 6 * (percision+1); + Vector3[] vertices = new Vector3[vertexCount / 3]; + int[] indices = new int[vertexCount]; + Vector3 position = bezier.getPosition(0.0f); + Vector3 tangent = getTangent(0.0f); + vertices[0] = position + tangent * realWidth; + vertices[1] = position - tangent * realWidth; + for (int i = 1; i <= percision; i++) { + float t = (float) i / percision; + indices[6*i ] = 2*i ; + indices[6*i+1] = 2*i-1; + indices[6*i+2] = 2*i-2; + indices[6*i+3] = 2*i+1; + indices[6*i+4] = 2*i-1; + indices[6*i+5] = 2*i ; + + position = bezier.getPosition(t); + tangent = getTangent(t); + vertices[2*i ] = position + tangent * realWidth; + vertices[2*i+1] = position - tangent * realWidth; + } + mesh.Clear(); + mesh.vertices = vertices; + mesh.triangles = indices; + mesh.Optimize(); + } + + private Vector3 getTangent(float t) { + Vector3 direction = bezier.getDirection(t); + direction.Normalize(); + return new Vector3(-direction.z, 0, direction.x); + } +} diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta b/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta new file mode 100644 index 0000000..1fe6547 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9bfb303c44d15ef468dc0d7334241e94 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/FlatCircleRenderer.cs b/Assets/Scripts/Primitives/FlatCircleRenderer.cs new file mode 100644 index 0000000..363b663 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatCircleRenderer.cs @@ -0,0 +1,42 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FlatCircleRenderer { + public Mesh mesh = new Mesh(); + public float radius, width; + private int segments; + + public FlatCircleRenderer(float radius, float width, int segments) { + this.radius = radius; + this.width = width; + this.segments = segments; + update(); + } + + public void update() { + Vector3[] vertices = new Vector3[2*segments+6]; + int[] indices = new int[6*segments]; + for (int i = 0; i < segments; i++) { + float alpha = (float) i / segments * 2 * Mathf.PI; + indices[6*i ] = 2*i ; + indices[6*i+3] = 2*i ; + indices[6*i+5] = 2*i+1; + if (i == 0) { + indices[6*i+1] = 2*segments-2; + indices[6*i+2] = 2*segments-1; + indices[6*i+4] = 2*segments-1; + } else { + indices[6*i+1] = 2*i-2; + indices[6*i+2] = 2*i-1; + indices[6*i+4] = 2*i-1; + } + Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); + vertices[2*i ] = direction * radius; + vertices[2*i+1] = direction * (radius + width); + } + mesh.Clear(); + mesh.vertices = vertices; + mesh.triangles = indices; + } +} diff --git a/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta b/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta new file mode 100644 index 0000000..d10c238 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0fe81c7924a85b142a4a86201b08c241 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Road.cs b/Assets/Scripts/Road.cs deleted file mode 100644 index 0af2218..0000000 --- a/Assets/Scripts/Road.cs +++ /dev/null @@ -1,85 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Road { - public List nodes = new List(); - Bezier path; - FlatBezierRenderer pathLine, roadBody; - Config config; - MeshCollider collider; - - public Road(Node start, Node end, Config config) { - nodes.Add(start); - nodes.Add(end); - path = new Bezier(); - pathLine = new FlatBezierRenderer(path, 50, 0.05f); - roadBody = new FlatBezierRenderer(path, 50, 2f); - this.config = config; - } - - - public void initialize(Transform parent) { - GameObject child = new GameObject(); - child.transform.parent = parent; - GameObject lineChild = new GameObject(); - lineChild.transform.position = Vector3.zero; - lineChild.AddComponent().material = config.roadEditMaterial; - lineChild.AddComponent().mesh = pathLine.mesh; - lineChild.transform.parent = child.transform; - - GameObject roadChild = new GameObject(); - collider = roadChild.AddComponent(); - roadChild.transform.position = Vector3.zero; - roadChild.AddComponent().material = config.roadMaterial; - roadChild.transform.parent = child.transform; - roadChild.layer = 8; - roadChild.AddComponent().mesh = roadBody.mesh; - roadChild.AddComponent().road = this; - } - - public void update(bool updateOthers) { - path.A = nodes[0].position; - path.D = nodes[1].position; - if (nodes[0].roads.Count == 2) { - path.B = nodes[0].position + - 0.25f * (nodes[1].position - nodes[0].getOther(nodes[1]).position); - } else { - path.B = 0.5f * (nodes[0].position + nodes[1].position); - } - if (nodes[1].roads.Count == 2) { - path.C = nodes[1].position + - 0.25f * (nodes[0].position - nodes[1].getOther(nodes[0]).position); - } else { - path.C = 0.5f * (nodes[0].position + nodes[1].position); - } - if (updateOthers) { - foreach (Node node in nodes) { - node.lateUpdate(this); - } - } - pathLine.update(); - roadBody.update(); - collider.sharedMesh = roadBody.mesh; - } - - override public bool Equals(object other) { - if (other == null || !(other is Road)) { - return false; - } - if (other == this) { - return true; - } - Road otherRoad = (Road) other; - foreach (Node node in nodes) { - if (! otherRoad.nodes.Contains(node)) { - return false; - } - } - return true; - } - - public override int GetHashCode() { - return nodes[0].GetHashCode() << 16 | nodes[1].GetHashCode(); - } -} diff --git a/Assets/Scripts/Road.cs.meta b/Assets/Scripts/Road.cs.meta deleted file mode 100644 index 24dc94b..0000000 --- a/Assets/Scripts/Road.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0ab512a631c0cc4468e77b6a2dfeb31f -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/RoadData.cs b/Assets/Scripts/RoadData.cs deleted file mode 100644 index eb620ba..0000000 --- a/Assets/Scripts/RoadData.cs +++ /dev/null @@ -1,7 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class RoadData : MonoBehaviour { - public Road road; -} diff --git a/Assets/Scripts/RoadData.cs.meta b/Assets/Scripts/RoadData.cs.meta deleted file mode 100644 index 2948b97..0000000 --- a/Assets/Scripts/RoadData.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 6bfac654eff68754c88b877a9b88ae42 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Bezier.cs b/Assets/Scripts/Bezier.cs deleted file mode 100644 index ea19006..0000000 --- a/Assets/Scripts/Bezier.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Bezier { - public Vector3 A, B, C, D; - - public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { - this.A = A; - this.B = B; - this.C = C; - this.D = D; - } - - public Bezier() { - this.A = Vector3.zero; - this.B = Vector3.zero; - this.C = Vector3.zero; - this.D = Vector3.zero; - } - - public Vector3 getPosition(float t) { - float T = 1.0f - t; - return - A * ( T*T*T) + - B * (3*T*T*t) + - C * (3*T*t*t) + - D * ( t*t*t); - } - - public Vector3 getDirection(float t) { - float T = 1.0f - t; - return - A * (-3*T*T ) + - B * ( 3*T*T - 6*T*t) + - C * (-3*t*t + 6*T*t) + - D * ( 3*t*t ); - } -} diff --git a/Assets/Scripts/Bezier.cs.meta b/Assets/Scripts/Bezier.cs.meta deleted file mode 100644 index 14630e2..0000000 --- a/Assets/Scripts/Bezier.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 284cf9d52e385aa4ebba97eb8b43b8a6 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatBezierRenderer.cs b/Assets/Scripts/FlatBezierRenderer.cs deleted file mode 100644 index b8dad15..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatBezierRenderer { - private int percision; - private float width; - public Bezier bezier; - public Mesh mesh = new Mesh(); - public FlatBezierRenderer(Bezier bezier, int percision, float width) { - this.percision = percision; - this.width = width; - this.bezier = bezier; - update(); - } - - public void update() { - float realWidth = width / 2; - int vertexCount = 6 * (percision+1); - Vector3[] vertices = new Vector3[vertexCount / 3]; - int[] indices = new int[vertexCount]; - Vector3 position = bezier.getPosition(0.0f); - Vector3 tangent = getTangent(0.0f); - vertices[0] = position + tangent * realWidth; - vertices[1] = position - tangent * realWidth; - for (int i = 1; i <= percision; i++) { - float t = (float) i / percision; - indices[6*i ] = 2*i ; - indices[6*i+1] = 2*i-1; - indices[6*i+2] = 2*i-2; - indices[6*i+3] = 2*i+1; - indices[6*i+4] = 2*i-1; - indices[6*i+5] = 2*i ; - - position = bezier.getPosition(t); - tangent = getTangent(t); - vertices[2*i ] = position + tangent * realWidth; - vertices[2*i+1] = position - tangent * realWidth; - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - mesh.Optimize(); - } - - private Vector3 getTangent(float t) { - Vector3 direction = bezier.getDirection(t); - direction.Normalize(); - return new Vector3(-direction.z, 0, direction.x); - } -} diff --git a/Assets/Scripts/FlatBezierRenderer.cs.meta b/Assets/Scripts/FlatBezierRenderer.cs.meta deleted file mode 100644 index 1fe6547..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 9bfb303c44d15ef468dc0d7334241e94 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatCircleRenderer.cs b/Assets/Scripts/FlatCircleRenderer.cs deleted file mode 100644 index 363b663..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatCircleRenderer { - public Mesh mesh = new Mesh(); - public float radius, width; - private int segments; - - public FlatCircleRenderer(float radius, float width, int segments) { - this.radius = radius; - this.width = width; - this.segments = segments; - update(); - } - - public void update() { - Vector3[] vertices = new Vector3[2*segments+6]; - int[] indices = new int[6*segments]; - for (int i = 0; i < segments; i++) { - float alpha = (float) i / segments * 2 * Mathf.PI; - indices[6*i ] = 2*i ; - indices[6*i+3] = 2*i ; - indices[6*i+5] = 2*i+1; - if (i == 0) { - indices[6*i+1] = 2*segments-2; - indices[6*i+2] = 2*segments-1; - indices[6*i+4] = 2*segments-1; - } else { - indices[6*i+1] = 2*i-2; - indices[6*i+2] = 2*i-1; - indices[6*i+4] = 2*i-1; - } - Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); - vertices[2*i ] = direction * radius; - vertices[2*i+1] = direction * (radius + width); - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - } -} diff --git a/Assets/Scripts/FlatCircleRenderer.cs.meta b/Assets/Scripts/FlatCircleRenderer.cs.meta deleted file mode 100644 index d10c238..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0fe81c7924a85b142a4a86201b08c241 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Node.cs b/Assets/Scripts/Node.cs deleted file mode 100644 index e3c4cbd..0000000 --- a/Assets/Scripts/Node.cs +++ /dev/null @@ -1,81 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Node { - public FlatCircleRenderer circle = new FlatCircleRenderer(0.2f, 0.05f, 32); - public FlatCircleRenderer fullCircle = new FlatCircleRenderer(0f, 1f, 32); - - public Vector3 position; - public List roads = new List(); - public GameObject gameObject; - - public Node(Vector3 position, Transform parent, Config config) { - gameObject = new GameObject(); - gameObject.transform.position = position; - gameObject.transform.parent = parent; - gameObject.AddComponent().radius = 0.5f; - gameObject.layer = 7; - - GameObject nodeCircle = new GameObject(); - nodeCircle.AddComponent().material = config.roadEditMaterial; - nodeCircle.AddComponent().mesh = circle.mesh; - nodeCircle.transform.parent = gameObject.transform; - nodeCircle.transform.localPosition = Vector3.zero; - - GameObject nodeRoad = new GameObject(); - nodeRoad.AddComponent().material = config.roadMaterial; - nodeRoad.AddComponent().mesh = fullCircle.mesh; - nodeRoad.transform.parent = gameObject.transform; - nodeRoad.transform.localPosition = Vector3.zero; - 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(); - } - - override public bool Equals(object other) { - if ((other == null) || ! this.GetType().Equals(other.GetType())) { - return false; - } - if (other == this) { - return true; - } - return ((Node)other).position.Equals(position); - } - - public void pull(Vector3 position) { - this.position = position; - gameObject.transform.position = position; - foreach (Road road in roads) { - 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/Node.cs.meta b/Assets/Scripts/Node.cs.meta deleted file mode 100644 index 8bbedf1..0000000 --- a/Assets/Scripts/Node.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 3cbed1dad7ec67d4dbe8602aea1e2979 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Primitives.meta b/Assets/Scripts/Primitives.meta new file mode 100644 index 0000000..0cf8895 --- /dev/null +++ b/Assets/Scripts/Primitives.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 6d1a8c7f33189a240b4d749356f53399 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/Bezier.cs b/Assets/Scripts/Primitives/Bezier.cs new file mode 100644 index 0000000..ea19006 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs @@ -0,0 +1,39 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Bezier { + public Vector3 A, B, C, D; + + public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { + this.A = A; + this.B = B; + this.C = C; + this.D = D; + } + + public Bezier() { + this.A = Vector3.zero; + this.B = Vector3.zero; + this.C = Vector3.zero; + this.D = Vector3.zero; + } + + public Vector3 getPosition(float t) { + float T = 1.0f - t; + return + A * ( T*T*T) + + B * (3*T*T*t) + + C * (3*T*t*t) + + D * ( t*t*t); + } + + public Vector3 getDirection(float t) { + float T = 1.0f - t; + return + A * (-3*T*T ) + + B * ( 3*T*T - 6*T*t) + + C * (-3*t*t + 6*T*t) + + D * ( 3*t*t ); + } +} diff --git a/Assets/Scripts/Primitives/Bezier.cs.meta b/Assets/Scripts/Primitives/Bezier.cs.meta new file mode 100644 index 0000000..14630e2 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 284cf9d52e385aa4ebba97eb8b43b8a6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs b/Assets/Scripts/Primitives/FlatBezierRenderer.cs new file mode 100644 index 0000000..b8dad15 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs @@ -0,0 +1,51 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FlatBezierRenderer { + private int percision; + private float width; + public Bezier bezier; + public Mesh mesh = new Mesh(); + public FlatBezierRenderer(Bezier bezier, int percision, float width) { + this.percision = percision; + this.width = width; + this.bezier = bezier; + update(); + } + + public void update() { + float realWidth = width / 2; + int vertexCount = 6 * (percision+1); + Vector3[] vertices = new Vector3[vertexCount / 3]; + int[] indices = new int[vertexCount]; + Vector3 position = bezier.getPosition(0.0f); + Vector3 tangent = getTangent(0.0f); + vertices[0] = position + tangent * realWidth; + vertices[1] = position - tangent * realWidth; + for (int i = 1; i <= percision; i++) { + float t = (float) i / percision; + indices[6*i ] = 2*i ; + indices[6*i+1] = 2*i-1; + indices[6*i+2] = 2*i-2; + indices[6*i+3] = 2*i+1; + indices[6*i+4] = 2*i-1; + indices[6*i+5] = 2*i ; + + position = bezier.getPosition(t); + tangent = getTangent(t); + vertices[2*i ] = position + tangent * realWidth; + vertices[2*i+1] = position - tangent * realWidth; + } + mesh.Clear(); + mesh.vertices = vertices; + mesh.triangles = indices; + mesh.Optimize(); + } + + private Vector3 getTangent(float t) { + Vector3 direction = bezier.getDirection(t); + direction.Normalize(); + return new Vector3(-direction.z, 0, direction.x); + } +} diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta b/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta new file mode 100644 index 0000000..1fe6547 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9bfb303c44d15ef468dc0d7334241e94 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/FlatCircleRenderer.cs b/Assets/Scripts/Primitives/FlatCircleRenderer.cs new file mode 100644 index 0000000..363b663 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatCircleRenderer.cs @@ -0,0 +1,42 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FlatCircleRenderer { + public Mesh mesh = new Mesh(); + public float radius, width; + private int segments; + + public FlatCircleRenderer(float radius, float width, int segments) { + this.radius = radius; + this.width = width; + this.segments = segments; + update(); + } + + public void update() { + Vector3[] vertices = new Vector3[2*segments+6]; + int[] indices = new int[6*segments]; + for (int i = 0; i < segments; i++) { + float alpha = (float) i / segments * 2 * Mathf.PI; + indices[6*i ] = 2*i ; + indices[6*i+3] = 2*i ; + indices[6*i+5] = 2*i+1; + if (i == 0) { + indices[6*i+1] = 2*segments-2; + indices[6*i+2] = 2*segments-1; + indices[6*i+4] = 2*segments-1; + } else { + indices[6*i+1] = 2*i-2; + indices[6*i+2] = 2*i-1; + indices[6*i+4] = 2*i-1; + } + Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); + vertices[2*i ] = direction * radius; + vertices[2*i+1] = direction * (radius + width); + } + mesh.Clear(); + mesh.vertices = vertices; + mesh.triangles = indices; + } +} diff --git a/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta b/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta new file mode 100644 index 0000000..d10c238 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0fe81c7924a85b142a4a86201b08c241 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Road.cs b/Assets/Scripts/Road.cs deleted file mode 100644 index 0af2218..0000000 --- a/Assets/Scripts/Road.cs +++ /dev/null @@ -1,85 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Road { - public List nodes = new List(); - Bezier path; - FlatBezierRenderer pathLine, roadBody; - Config config; - MeshCollider collider; - - public Road(Node start, Node end, Config config) { - nodes.Add(start); - nodes.Add(end); - path = new Bezier(); - pathLine = new FlatBezierRenderer(path, 50, 0.05f); - roadBody = new FlatBezierRenderer(path, 50, 2f); - this.config = config; - } - - - public void initialize(Transform parent) { - GameObject child = new GameObject(); - child.transform.parent = parent; - GameObject lineChild = new GameObject(); - lineChild.transform.position = Vector3.zero; - lineChild.AddComponent().material = config.roadEditMaterial; - lineChild.AddComponent().mesh = pathLine.mesh; - lineChild.transform.parent = child.transform; - - GameObject roadChild = new GameObject(); - collider = roadChild.AddComponent(); - roadChild.transform.position = Vector3.zero; - roadChild.AddComponent().material = config.roadMaterial; - roadChild.transform.parent = child.transform; - roadChild.layer = 8; - roadChild.AddComponent().mesh = roadBody.mesh; - roadChild.AddComponent().road = this; - } - - public void update(bool updateOthers) { - path.A = nodes[0].position; - path.D = nodes[1].position; - if (nodes[0].roads.Count == 2) { - path.B = nodes[0].position + - 0.25f * (nodes[1].position - nodes[0].getOther(nodes[1]).position); - } else { - path.B = 0.5f * (nodes[0].position + nodes[1].position); - } - if (nodes[1].roads.Count == 2) { - path.C = nodes[1].position + - 0.25f * (nodes[0].position - nodes[1].getOther(nodes[0]).position); - } else { - path.C = 0.5f * (nodes[0].position + nodes[1].position); - } - if (updateOthers) { - foreach (Node node in nodes) { - node.lateUpdate(this); - } - } - pathLine.update(); - roadBody.update(); - collider.sharedMesh = roadBody.mesh; - } - - override public bool Equals(object other) { - if (other == null || !(other is Road)) { - return false; - } - if (other == this) { - return true; - } - Road otherRoad = (Road) other; - foreach (Node node in nodes) { - if (! otherRoad.nodes.Contains(node)) { - return false; - } - } - return true; - } - - public override int GetHashCode() { - return nodes[0].GetHashCode() << 16 | nodes[1].GetHashCode(); - } -} diff --git a/Assets/Scripts/Road.cs.meta b/Assets/Scripts/Road.cs.meta deleted file mode 100644 index 24dc94b..0000000 --- a/Assets/Scripts/Road.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0ab512a631c0cc4468e77b6a2dfeb31f -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/RoadData.cs b/Assets/Scripts/RoadData.cs deleted file mode 100644 index eb620ba..0000000 --- a/Assets/Scripts/RoadData.cs +++ /dev/null @@ -1,7 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class RoadData : MonoBehaviour { - public Road road; -} diff --git a/Assets/Scripts/RoadData.cs.meta b/Assets/Scripts/RoadData.cs.meta deleted file mode 100644 index 2948b97..0000000 --- a/Assets/Scripts/RoadData.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 6bfac654eff68754c88b877a9b88ae42 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/RoadEdit.cs b/Assets/Scripts/RoadEdit.cs deleted file mode 100644 index a6cc73e..0000000 --- a/Assets/Scripts/RoadEdit.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class RoadEdit : MonoBehaviour { - public GameObject start; - public GameObject startDirection; - public GameObject end; - public GameObject endDirection; - public Camera mainCamera; - - private GameObject currentlyPulling = null; - public Bezier bezier; - private FlatBezierRenderer pathRenderer; - public Material highlight, noHighlight; - - void Start() { - } - - void updateRoad(Vector3 position) { - } - - void Update() { - Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); - if (Input.GetAxis("Fire1") != 0.0f) { - if (currentlyPulling == null && Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, 1 << 7)) { - currentlyPulling = hit.transform.gameObject; - currentlyPulling.GetComponent().material = highlight; - } - } else { - if (currentlyPulling != null) { - currentlyPulling.GetComponent().material = noHighlight; - } - currentlyPulling = null; - } - if (currentlyPulling != null) { - Physics.Raycast(ray, out RaycastHit hit_, Mathf.Infinity, 1 << 6); - Vector3 position = new Vector3(hit_.point.x, 0.0f, hit_.point.z); - currentlyPulling.transform.position = position; - updateRoad(position); - } - } -} diff --git a/Assets/Scripts/Bezier.cs b/Assets/Scripts/Bezier.cs deleted file mode 100644 index ea19006..0000000 --- a/Assets/Scripts/Bezier.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Bezier { - public Vector3 A, B, C, D; - - public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { - this.A = A; - this.B = B; - this.C = C; - this.D = D; - } - - public Bezier() { - this.A = Vector3.zero; - this.B = Vector3.zero; - this.C = Vector3.zero; - this.D = Vector3.zero; - } - - public Vector3 getPosition(float t) { - float T = 1.0f - t; - return - A * ( T*T*T) + - B * (3*T*T*t) + - C * (3*T*t*t) + - D * ( t*t*t); - } - - public Vector3 getDirection(float t) { - float T = 1.0f - t; - return - A * (-3*T*T ) + - B * ( 3*T*T - 6*T*t) + - C * (-3*t*t + 6*T*t) + - D * ( 3*t*t ); - } -} diff --git a/Assets/Scripts/Bezier.cs.meta b/Assets/Scripts/Bezier.cs.meta deleted file mode 100644 index 14630e2..0000000 --- a/Assets/Scripts/Bezier.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 284cf9d52e385aa4ebba97eb8b43b8a6 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatBezierRenderer.cs b/Assets/Scripts/FlatBezierRenderer.cs deleted file mode 100644 index b8dad15..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatBezierRenderer { - private int percision; - private float width; - public Bezier bezier; - public Mesh mesh = new Mesh(); - public FlatBezierRenderer(Bezier bezier, int percision, float width) { - this.percision = percision; - this.width = width; - this.bezier = bezier; - update(); - } - - public void update() { - float realWidth = width / 2; - int vertexCount = 6 * (percision+1); - Vector3[] vertices = new Vector3[vertexCount / 3]; - int[] indices = new int[vertexCount]; - Vector3 position = bezier.getPosition(0.0f); - Vector3 tangent = getTangent(0.0f); - vertices[0] = position + tangent * realWidth; - vertices[1] = position - tangent * realWidth; - for (int i = 1; i <= percision; i++) { - float t = (float) i / percision; - indices[6*i ] = 2*i ; - indices[6*i+1] = 2*i-1; - indices[6*i+2] = 2*i-2; - indices[6*i+3] = 2*i+1; - indices[6*i+4] = 2*i-1; - indices[6*i+5] = 2*i ; - - position = bezier.getPosition(t); - tangent = getTangent(t); - vertices[2*i ] = position + tangent * realWidth; - vertices[2*i+1] = position - tangent * realWidth; - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - mesh.Optimize(); - } - - private Vector3 getTangent(float t) { - Vector3 direction = bezier.getDirection(t); - direction.Normalize(); - return new Vector3(-direction.z, 0, direction.x); - } -} diff --git a/Assets/Scripts/FlatBezierRenderer.cs.meta b/Assets/Scripts/FlatBezierRenderer.cs.meta deleted file mode 100644 index 1fe6547..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 9bfb303c44d15ef468dc0d7334241e94 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatCircleRenderer.cs b/Assets/Scripts/FlatCircleRenderer.cs deleted file mode 100644 index 363b663..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatCircleRenderer { - public Mesh mesh = new Mesh(); - public float radius, width; - private int segments; - - public FlatCircleRenderer(float radius, float width, int segments) { - this.radius = radius; - this.width = width; - this.segments = segments; - update(); - } - - public void update() { - Vector3[] vertices = new Vector3[2*segments+6]; - int[] indices = new int[6*segments]; - for (int i = 0; i < segments; i++) { - float alpha = (float) i / segments * 2 * Mathf.PI; - indices[6*i ] = 2*i ; - indices[6*i+3] = 2*i ; - indices[6*i+5] = 2*i+1; - if (i == 0) { - indices[6*i+1] = 2*segments-2; - indices[6*i+2] = 2*segments-1; - indices[6*i+4] = 2*segments-1; - } else { - indices[6*i+1] = 2*i-2; - indices[6*i+2] = 2*i-1; - indices[6*i+4] = 2*i-1; - } - Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); - vertices[2*i ] = direction * radius; - vertices[2*i+1] = direction * (radius + width); - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - } -} diff --git a/Assets/Scripts/FlatCircleRenderer.cs.meta b/Assets/Scripts/FlatCircleRenderer.cs.meta deleted file mode 100644 index d10c238..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0fe81c7924a85b142a4a86201b08c241 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Node.cs b/Assets/Scripts/Node.cs deleted file mode 100644 index e3c4cbd..0000000 --- a/Assets/Scripts/Node.cs +++ /dev/null @@ -1,81 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Node { - public FlatCircleRenderer circle = new FlatCircleRenderer(0.2f, 0.05f, 32); - public FlatCircleRenderer fullCircle = new FlatCircleRenderer(0f, 1f, 32); - - public Vector3 position; - public List roads = new List(); - public GameObject gameObject; - - public Node(Vector3 position, Transform parent, Config config) { - gameObject = new GameObject(); - gameObject.transform.position = position; - gameObject.transform.parent = parent; - gameObject.AddComponent().radius = 0.5f; - gameObject.layer = 7; - - GameObject nodeCircle = new GameObject(); - nodeCircle.AddComponent().material = config.roadEditMaterial; - nodeCircle.AddComponent().mesh = circle.mesh; - nodeCircle.transform.parent = gameObject.transform; - nodeCircle.transform.localPosition = Vector3.zero; - - GameObject nodeRoad = new GameObject(); - nodeRoad.AddComponent().material = config.roadMaterial; - nodeRoad.AddComponent().mesh = fullCircle.mesh; - nodeRoad.transform.parent = gameObject.transform; - nodeRoad.transform.localPosition = Vector3.zero; - 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(); - } - - override public bool Equals(object other) { - if ((other == null) || ! this.GetType().Equals(other.GetType())) { - return false; - } - if (other == this) { - return true; - } - return ((Node)other).position.Equals(position); - } - - public void pull(Vector3 position) { - this.position = position; - gameObject.transform.position = position; - foreach (Road road in roads) { - 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/Node.cs.meta b/Assets/Scripts/Node.cs.meta deleted file mode 100644 index 8bbedf1..0000000 --- a/Assets/Scripts/Node.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 3cbed1dad7ec67d4dbe8602aea1e2979 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Primitives.meta b/Assets/Scripts/Primitives.meta new file mode 100644 index 0000000..0cf8895 --- /dev/null +++ b/Assets/Scripts/Primitives.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 6d1a8c7f33189a240b4d749356f53399 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/Bezier.cs b/Assets/Scripts/Primitives/Bezier.cs new file mode 100644 index 0000000..ea19006 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs @@ -0,0 +1,39 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Bezier { + public Vector3 A, B, C, D; + + public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { + this.A = A; + this.B = B; + this.C = C; + this.D = D; + } + + public Bezier() { + this.A = Vector3.zero; + this.B = Vector3.zero; + this.C = Vector3.zero; + this.D = Vector3.zero; + } + + public Vector3 getPosition(float t) { + float T = 1.0f - t; + return + A * ( T*T*T) + + B * (3*T*T*t) + + C * (3*T*t*t) + + D * ( t*t*t); + } + + public Vector3 getDirection(float t) { + float T = 1.0f - t; + return + A * (-3*T*T ) + + B * ( 3*T*T - 6*T*t) + + C * (-3*t*t + 6*T*t) + + D * ( 3*t*t ); + } +} diff --git a/Assets/Scripts/Primitives/Bezier.cs.meta b/Assets/Scripts/Primitives/Bezier.cs.meta new file mode 100644 index 0000000..14630e2 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 284cf9d52e385aa4ebba97eb8b43b8a6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs b/Assets/Scripts/Primitives/FlatBezierRenderer.cs new file mode 100644 index 0000000..b8dad15 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs @@ -0,0 +1,51 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FlatBezierRenderer { + private int percision; + private float width; + public Bezier bezier; + public Mesh mesh = new Mesh(); + public FlatBezierRenderer(Bezier bezier, int percision, float width) { + this.percision = percision; + this.width = width; + this.bezier = bezier; + update(); + } + + public void update() { + float realWidth = width / 2; + int vertexCount = 6 * (percision+1); + Vector3[] vertices = new Vector3[vertexCount / 3]; + int[] indices = new int[vertexCount]; + Vector3 position = bezier.getPosition(0.0f); + Vector3 tangent = getTangent(0.0f); + vertices[0] = position + tangent * realWidth; + vertices[1] = position - tangent * realWidth; + for (int i = 1; i <= percision; i++) { + float t = (float) i / percision; + indices[6*i ] = 2*i ; + indices[6*i+1] = 2*i-1; + indices[6*i+2] = 2*i-2; + indices[6*i+3] = 2*i+1; + indices[6*i+4] = 2*i-1; + indices[6*i+5] = 2*i ; + + position = bezier.getPosition(t); + tangent = getTangent(t); + vertices[2*i ] = position + tangent * realWidth; + vertices[2*i+1] = position - tangent * realWidth; + } + mesh.Clear(); + mesh.vertices = vertices; + mesh.triangles = indices; + mesh.Optimize(); + } + + private Vector3 getTangent(float t) { + Vector3 direction = bezier.getDirection(t); + direction.Normalize(); + return new Vector3(-direction.z, 0, direction.x); + } +} diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta b/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta new file mode 100644 index 0000000..1fe6547 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9bfb303c44d15ef468dc0d7334241e94 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/FlatCircleRenderer.cs b/Assets/Scripts/Primitives/FlatCircleRenderer.cs new file mode 100644 index 0000000..363b663 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatCircleRenderer.cs @@ -0,0 +1,42 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FlatCircleRenderer { + public Mesh mesh = new Mesh(); + public float radius, width; + private int segments; + + public FlatCircleRenderer(float radius, float width, int segments) { + this.radius = radius; + this.width = width; + this.segments = segments; + update(); + } + + public void update() { + Vector3[] vertices = new Vector3[2*segments+6]; + int[] indices = new int[6*segments]; + for (int i = 0; i < segments; i++) { + float alpha = (float) i / segments * 2 * Mathf.PI; + indices[6*i ] = 2*i ; + indices[6*i+3] = 2*i ; + indices[6*i+5] = 2*i+1; + if (i == 0) { + indices[6*i+1] = 2*segments-2; + indices[6*i+2] = 2*segments-1; + indices[6*i+4] = 2*segments-1; + } else { + indices[6*i+1] = 2*i-2; + indices[6*i+2] = 2*i-1; + indices[6*i+4] = 2*i-1; + } + Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); + vertices[2*i ] = direction * radius; + vertices[2*i+1] = direction * (radius + width); + } + mesh.Clear(); + mesh.vertices = vertices; + mesh.triangles = indices; + } +} diff --git a/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta b/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta new file mode 100644 index 0000000..d10c238 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0fe81c7924a85b142a4a86201b08c241 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Road.cs b/Assets/Scripts/Road.cs deleted file mode 100644 index 0af2218..0000000 --- a/Assets/Scripts/Road.cs +++ /dev/null @@ -1,85 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Road { - public List nodes = new List(); - Bezier path; - FlatBezierRenderer pathLine, roadBody; - Config config; - MeshCollider collider; - - public Road(Node start, Node end, Config config) { - nodes.Add(start); - nodes.Add(end); - path = new Bezier(); - pathLine = new FlatBezierRenderer(path, 50, 0.05f); - roadBody = new FlatBezierRenderer(path, 50, 2f); - this.config = config; - } - - - public void initialize(Transform parent) { - GameObject child = new GameObject(); - child.transform.parent = parent; - GameObject lineChild = new GameObject(); - lineChild.transform.position = Vector3.zero; - lineChild.AddComponent().material = config.roadEditMaterial; - lineChild.AddComponent().mesh = pathLine.mesh; - lineChild.transform.parent = child.transform; - - GameObject roadChild = new GameObject(); - collider = roadChild.AddComponent(); - roadChild.transform.position = Vector3.zero; - roadChild.AddComponent().material = config.roadMaterial; - roadChild.transform.parent = child.transform; - roadChild.layer = 8; - roadChild.AddComponent().mesh = roadBody.mesh; - roadChild.AddComponent().road = this; - } - - public void update(bool updateOthers) { - path.A = nodes[0].position; - path.D = nodes[1].position; - if (nodes[0].roads.Count == 2) { - path.B = nodes[0].position + - 0.25f * (nodes[1].position - nodes[0].getOther(nodes[1]).position); - } else { - path.B = 0.5f * (nodes[0].position + nodes[1].position); - } - if (nodes[1].roads.Count == 2) { - path.C = nodes[1].position + - 0.25f * (nodes[0].position - nodes[1].getOther(nodes[0]).position); - } else { - path.C = 0.5f * (nodes[0].position + nodes[1].position); - } - if (updateOthers) { - foreach (Node node in nodes) { - node.lateUpdate(this); - } - } - pathLine.update(); - roadBody.update(); - collider.sharedMesh = roadBody.mesh; - } - - override public bool Equals(object other) { - if (other == null || !(other is Road)) { - return false; - } - if (other == this) { - return true; - } - Road otherRoad = (Road) other; - foreach (Node node in nodes) { - if (! otherRoad.nodes.Contains(node)) { - return false; - } - } - return true; - } - - public override int GetHashCode() { - return nodes[0].GetHashCode() << 16 | nodes[1].GetHashCode(); - } -} diff --git a/Assets/Scripts/Road.cs.meta b/Assets/Scripts/Road.cs.meta deleted file mode 100644 index 24dc94b..0000000 --- a/Assets/Scripts/Road.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0ab512a631c0cc4468e77b6a2dfeb31f -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/RoadData.cs b/Assets/Scripts/RoadData.cs deleted file mode 100644 index eb620ba..0000000 --- a/Assets/Scripts/RoadData.cs +++ /dev/null @@ -1,7 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class RoadData : MonoBehaviour { - public Road road; -} diff --git a/Assets/Scripts/RoadData.cs.meta b/Assets/Scripts/RoadData.cs.meta deleted file mode 100644 index 2948b97..0000000 --- a/Assets/Scripts/RoadData.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 6bfac654eff68754c88b877a9b88ae42 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/RoadEdit.cs b/Assets/Scripts/RoadEdit.cs deleted file mode 100644 index a6cc73e..0000000 --- a/Assets/Scripts/RoadEdit.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class RoadEdit : MonoBehaviour { - public GameObject start; - public GameObject startDirection; - public GameObject end; - public GameObject endDirection; - public Camera mainCamera; - - private GameObject currentlyPulling = null; - public Bezier bezier; - private FlatBezierRenderer pathRenderer; - public Material highlight, noHighlight; - - void Start() { - } - - void updateRoad(Vector3 position) { - } - - void Update() { - Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); - if (Input.GetAxis("Fire1") != 0.0f) { - if (currentlyPulling == null && Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, 1 << 7)) { - currentlyPulling = hit.transform.gameObject; - currentlyPulling.GetComponent().material = highlight; - } - } else { - if (currentlyPulling != null) { - currentlyPulling.GetComponent().material = noHighlight; - } - currentlyPulling = null; - } - if (currentlyPulling != null) { - Physics.Raycast(ray, out RaycastHit hit_, Mathf.Infinity, 1 << 6); - Vector3 position = new Vector3(hit_.point.x, 0.0f, hit_.point.z); - currentlyPulling.transform.position = position; - updateRoad(position); - } - } -} diff --git a/Assets/Scripts/RoadEdit.cs.meta b/Assets/Scripts/RoadEdit.cs.meta deleted file mode 100644 index 4e09d01..0000000 --- a/Assets/Scripts/RoadEdit.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 68e9412fa432f2e468d5d7447e99fd89 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Bezier.cs b/Assets/Scripts/Bezier.cs deleted file mode 100644 index ea19006..0000000 --- a/Assets/Scripts/Bezier.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Bezier { - public Vector3 A, B, C, D; - - public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { - this.A = A; - this.B = B; - this.C = C; - this.D = D; - } - - public Bezier() { - this.A = Vector3.zero; - this.B = Vector3.zero; - this.C = Vector3.zero; - this.D = Vector3.zero; - } - - public Vector3 getPosition(float t) { - float T = 1.0f - t; - return - A * ( T*T*T) + - B * (3*T*T*t) + - C * (3*T*t*t) + - D * ( t*t*t); - } - - public Vector3 getDirection(float t) { - float T = 1.0f - t; - return - A * (-3*T*T ) + - B * ( 3*T*T - 6*T*t) + - C * (-3*t*t + 6*T*t) + - D * ( 3*t*t ); - } -} diff --git a/Assets/Scripts/Bezier.cs.meta b/Assets/Scripts/Bezier.cs.meta deleted file mode 100644 index 14630e2..0000000 --- a/Assets/Scripts/Bezier.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 284cf9d52e385aa4ebba97eb8b43b8a6 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatBezierRenderer.cs b/Assets/Scripts/FlatBezierRenderer.cs deleted file mode 100644 index b8dad15..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatBezierRenderer { - private int percision; - private float width; - public Bezier bezier; - public Mesh mesh = new Mesh(); - public FlatBezierRenderer(Bezier bezier, int percision, float width) { - this.percision = percision; - this.width = width; - this.bezier = bezier; - update(); - } - - public void update() { - float realWidth = width / 2; - int vertexCount = 6 * (percision+1); - Vector3[] vertices = new Vector3[vertexCount / 3]; - int[] indices = new int[vertexCount]; - Vector3 position = bezier.getPosition(0.0f); - Vector3 tangent = getTangent(0.0f); - vertices[0] = position + tangent * realWidth; - vertices[1] = position - tangent * realWidth; - for (int i = 1; i <= percision; i++) { - float t = (float) i / percision; - indices[6*i ] = 2*i ; - indices[6*i+1] = 2*i-1; - indices[6*i+2] = 2*i-2; - indices[6*i+3] = 2*i+1; - indices[6*i+4] = 2*i-1; - indices[6*i+5] = 2*i ; - - position = bezier.getPosition(t); - tangent = getTangent(t); - vertices[2*i ] = position + tangent * realWidth; - vertices[2*i+1] = position - tangent * realWidth; - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - mesh.Optimize(); - } - - private Vector3 getTangent(float t) { - Vector3 direction = bezier.getDirection(t); - direction.Normalize(); - return new Vector3(-direction.z, 0, direction.x); - } -} diff --git a/Assets/Scripts/FlatBezierRenderer.cs.meta b/Assets/Scripts/FlatBezierRenderer.cs.meta deleted file mode 100644 index 1fe6547..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 9bfb303c44d15ef468dc0d7334241e94 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatCircleRenderer.cs b/Assets/Scripts/FlatCircleRenderer.cs deleted file mode 100644 index 363b663..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatCircleRenderer { - public Mesh mesh = new Mesh(); - public float radius, width; - private int segments; - - public FlatCircleRenderer(float radius, float width, int segments) { - this.radius = radius; - this.width = width; - this.segments = segments; - update(); - } - - public void update() { - Vector3[] vertices = new Vector3[2*segments+6]; - int[] indices = new int[6*segments]; - for (int i = 0; i < segments; i++) { - float alpha = (float) i / segments * 2 * Mathf.PI; - indices[6*i ] = 2*i ; - indices[6*i+3] = 2*i ; - indices[6*i+5] = 2*i+1; - if (i == 0) { - indices[6*i+1] = 2*segments-2; - indices[6*i+2] = 2*segments-1; - indices[6*i+4] = 2*segments-1; - } else { - indices[6*i+1] = 2*i-2; - indices[6*i+2] = 2*i-1; - indices[6*i+4] = 2*i-1; - } - Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); - vertices[2*i ] = direction * radius; - vertices[2*i+1] = direction * (radius + width); - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - } -} diff --git a/Assets/Scripts/FlatCircleRenderer.cs.meta b/Assets/Scripts/FlatCircleRenderer.cs.meta deleted file mode 100644 index d10c238..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0fe81c7924a85b142a4a86201b08c241 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Node.cs b/Assets/Scripts/Node.cs deleted file mode 100644 index e3c4cbd..0000000 --- a/Assets/Scripts/Node.cs +++ /dev/null @@ -1,81 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Node { - public FlatCircleRenderer circle = new FlatCircleRenderer(0.2f, 0.05f, 32); - public FlatCircleRenderer fullCircle = new FlatCircleRenderer(0f, 1f, 32); - - public Vector3 position; - public List roads = new List(); - public GameObject gameObject; - - public Node(Vector3 position, Transform parent, Config config) { - gameObject = new GameObject(); - gameObject.transform.position = position; - gameObject.transform.parent = parent; - gameObject.AddComponent().radius = 0.5f; - gameObject.layer = 7; - - GameObject nodeCircle = new GameObject(); - nodeCircle.AddComponent().material = config.roadEditMaterial; - nodeCircle.AddComponent().mesh = circle.mesh; - nodeCircle.transform.parent = gameObject.transform; - nodeCircle.transform.localPosition = Vector3.zero; - - GameObject nodeRoad = new GameObject(); - nodeRoad.AddComponent().material = config.roadMaterial; - nodeRoad.AddComponent().mesh = fullCircle.mesh; - nodeRoad.transform.parent = gameObject.transform; - nodeRoad.transform.localPosition = Vector3.zero; - 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(); - } - - override public bool Equals(object other) { - if ((other == null) || ! this.GetType().Equals(other.GetType())) { - return false; - } - if (other == this) { - return true; - } - return ((Node)other).position.Equals(position); - } - - public void pull(Vector3 position) { - this.position = position; - gameObject.transform.position = position; - foreach (Road road in roads) { - 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/Node.cs.meta b/Assets/Scripts/Node.cs.meta deleted file mode 100644 index 8bbedf1..0000000 --- a/Assets/Scripts/Node.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 3cbed1dad7ec67d4dbe8602aea1e2979 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Primitives.meta b/Assets/Scripts/Primitives.meta new file mode 100644 index 0000000..0cf8895 --- /dev/null +++ b/Assets/Scripts/Primitives.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 6d1a8c7f33189a240b4d749356f53399 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/Bezier.cs b/Assets/Scripts/Primitives/Bezier.cs new file mode 100644 index 0000000..ea19006 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs @@ -0,0 +1,39 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Bezier { + public Vector3 A, B, C, D; + + public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { + this.A = A; + this.B = B; + this.C = C; + this.D = D; + } + + public Bezier() { + this.A = Vector3.zero; + this.B = Vector3.zero; + this.C = Vector3.zero; + this.D = Vector3.zero; + } + + public Vector3 getPosition(float t) { + float T = 1.0f - t; + return + A * ( T*T*T) + + B * (3*T*T*t) + + C * (3*T*t*t) + + D * ( t*t*t); + } + + public Vector3 getDirection(float t) { + float T = 1.0f - t; + return + A * (-3*T*T ) + + B * ( 3*T*T - 6*T*t) + + C * (-3*t*t + 6*T*t) + + D * ( 3*t*t ); + } +} diff --git a/Assets/Scripts/Primitives/Bezier.cs.meta b/Assets/Scripts/Primitives/Bezier.cs.meta new file mode 100644 index 0000000..14630e2 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 284cf9d52e385aa4ebba97eb8b43b8a6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs b/Assets/Scripts/Primitives/FlatBezierRenderer.cs new file mode 100644 index 0000000..b8dad15 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs @@ -0,0 +1,51 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FlatBezierRenderer { + private int percision; + private float width; + public Bezier bezier; + public Mesh mesh = new Mesh(); + public FlatBezierRenderer(Bezier bezier, int percision, float width) { + this.percision = percision; + this.width = width; + this.bezier = bezier; + update(); + } + + public void update() { + float realWidth = width / 2; + int vertexCount = 6 * (percision+1); + Vector3[] vertices = new Vector3[vertexCount / 3]; + int[] indices = new int[vertexCount]; + Vector3 position = bezier.getPosition(0.0f); + Vector3 tangent = getTangent(0.0f); + vertices[0] = position + tangent * realWidth; + vertices[1] = position - tangent * realWidth; + for (int i = 1; i <= percision; i++) { + float t = (float) i / percision; + indices[6*i ] = 2*i ; + indices[6*i+1] = 2*i-1; + indices[6*i+2] = 2*i-2; + indices[6*i+3] = 2*i+1; + indices[6*i+4] = 2*i-1; + indices[6*i+5] = 2*i ; + + position = bezier.getPosition(t); + tangent = getTangent(t); + vertices[2*i ] = position + tangent * realWidth; + vertices[2*i+1] = position - tangent * realWidth; + } + mesh.Clear(); + mesh.vertices = vertices; + mesh.triangles = indices; + mesh.Optimize(); + } + + private Vector3 getTangent(float t) { + Vector3 direction = bezier.getDirection(t); + direction.Normalize(); + return new Vector3(-direction.z, 0, direction.x); + } +} diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta b/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta new file mode 100644 index 0000000..1fe6547 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9bfb303c44d15ef468dc0d7334241e94 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/FlatCircleRenderer.cs b/Assets/Scripts/Primitives/FlatCircleRenderer.cs new file mode 100644 index 0000000..363b663 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatCircleRenderer.cs @@ -0,0 +1,42 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FlatCircleRenderer { + public Mesh mesh = new Mesh(); + public float radius, width; + private int segments; + + public FlatCircleRenderer(float radius, float width, int segments) { + this.radius = radius; + this.width = width; + this.segments = segments; + update(); + } + + public void update() { + Vector3[] vertices = new Vector3[2*segments+6]; + int[] indices = new int[6*segments]; + for (int i = 0; i < segments; i++) { + float alpha = (float) i / segments * 2 * Mathf.PI; + indices[6*i ] = 2*i ; + indices[6*i+3] = 2*i ; + indices[6*i+5] = 2*i+1; + if (i == 0) { + indices[6*i+1] = 2*segments-2; + indices[6*i+2] = 2*segments-1; + indices[6*i+4] = 2*segments-1; + } else { + indices[6*i+1] = 2*i-2; + indices[6*i+2] = 2*i-1; + indices[6*i+4] = 2*i-1; + } + Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); + vertices[2*i ] = direction * radius; + vertices[2*i+1] = direction * (radius + width); + } + mesh.Clear(); + mesh.vertices = vertices; + mesh.triangles = indices; + } +} diff --git a/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta b/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta new file mode 100644 index 0000000..d10c238 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0fe81c7924a85b142a4a86201b08c241 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Road.cs b/Assets/Scripts/Road.cs deleted file mode 100644 index 0af2218..0000000 --- a/Assets/Scripts/Road.cs +++ /dev/null @@ -1,85 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Road { - public List nodes = new List(); - Bezier path; - FlatBezierRenderer pathLine, roadBody; - Config config; - MeshCollider collider; - - public Road(Node start, Node end, Config config) { - nodes.Add(start); - nodes.Add(end); - path = new Bezier(); - pathLine = new FlatBezierRenderer(path, 50, 0.05f); - roadBody = new FlatBezierRenderer(path, 50, 2f); - this.config = config; - } - - - public void initialize(Transform parent) { - GameObject child = new GameObject(); - child.transform.parent = parent; - GameObject lineChild = new GameObject(); - lineChild.transform.position = Vector3.zero; - lineChild.AddComponent().material = config.roadEditMaterial; - lineChild.AddComponent().mesh = pathLine.mesh; - lineChild.transform.parent = child.transform; - - GameObject roadChild = new GameObject(); - collider = roadChild.AddComponent(); - roadChild.transform.position = Vector3.zero; - roadChild.AddComponent().material = config.roadMaterial; - roadChild.transform.parent = child.transform; - roadChild.layer = 8; - roadChild.AddComponent().mesh = roadBody.mesh; - roadChild.AddComponent().road = this; - } - - public void update(bool updateOthers) { - path.A = nodes[0].position; - path.D = nodes[1].position; - if (nodes[0].roads.Count == 2) { - path.B = nodes[0].position + - 0.25f * (nodes[1].position - nodes[0].getOther(nodes[1]).position); - } else { - path.B = 0.5f * (nodes[0].position + nodes[1].position); - } - if (nodes[1].roads.Count == 2) { - path.C = nodes[1].position + - 0.25f * (nodes[0].position - nodes[1].getOther(nodes[0]).position); - } else { - path.C = 0.5f * (nodes[0].position + nodes[1].position); - } - if (updateOthers) { - foreach (Node node in nodes) { - node.lateUpdate(this); - } - } - pathLine.update(); - roadBody.update(); - collider.sharedMesh = roadBody.mesh; - } - - override public bool Equals(object other) { - if (other == null || !(other is Road)) { - return false; - } - if (other == this) { - return true; - } - Road otherRoad = (Road) other; - foreach (Node node in nodes) { - if (! otherRoad.nodes.Contains(node)) { - return false; - } - } - return true; - } - - public override int GetHashCode() { - return nodes[0].GetHashCode() << 16 | nodes[1].GetHashCode(); - } -} diff --git a/Assets/Scripts/Road.cs.meta b/Assets/Scripts/Road.cs.meta deleted file mode 100644 index 24dc94b..0000000 --- a/Assets/Scripts/Road.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0ab512a631c0cc4468e77b6a2dfeb31f -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/RoadData.cs b/Assets/Scripts/RoadData.cs deleted file mode 100644 index eb620ba..0000000 --- a/Assets/Scripts/RoadData.cs +++ /dev/null @@ -1,7 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class RoadData : MonoBehaviour { - public Road road; -} diff --git a/Assets/Scripts/RoadData.cs.meta b/Assets/Scripts/RoadData.cs.meta deleted file mode 100644 index 2948b97..0000000 --- a/Assets/Scripts/RoadData.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 6bfac654eff68754c88b877a9b88ae42 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/RoadEdit.cs b/Assets/Scripts/RoadEdit.cs deleted file mode 100644 index a6cc73e..0000000 --- a/Assets/Scripts/RoadEdit.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class RoadEdit : MonoBehaviour { - public GameObject start; - public GameObject startDirection; - public GameObject end; - public GameObject endDirection; - public Camera mainCamera; - - private GameObject currentlyPulling = null; - public Bezier bezier; - private FlatBezierRenderer pathRenderer; - public Material highlight, noHighlight; - - void Start() { - } - - void updateRoad(Vector3 position) { - } - - void Update() { - Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); - if (Input.GetAxis("Fire1") != 0.0f) { - if (currentlyPulling == null && Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, 1 << 7)) { - currentlyPulling = hit.transform.gameObject; - currentlyPulling.GetComponent().material = highlight; - } - } else { - if (currentlyPulling != null) { - currentlyPulling.GetComponent().material = noHighlight; - } - currentlyPulling = null; - } - if (currentlyPulling != null) { - Physics.Raycast(ray, out RaycastHit hit_, Mathf.Infinity, 1 << 6); - Vector3 position = new Vector3(hit_.point.x, 0.0f, hit_.point.z); - currentlyPulling.transform.position = position; - updateRoad(position); - } - } -} diff --git a/Assets/Scripts/RoadEdit.cs.meta b/Assets/Scripts/RoadEdit.cs.meta deleted file mode 100644 index 4e09d01..0000000 --- a/Assets/Scripts/RoadEdit.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 68e9412fa432f2e468d5d7447e99fd89 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Roads.cs b/Assets/Scripts/Roads.cs deleted file mode 100644 index acd376a..0000000 --- a/Assets/Scripts/Roads.cs +++ /dev/null @@ -1,102 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Roads : MonoBehaviour { - public List nodes = new List(); - public List roads = new List(); - public FlatBezierRenderer roadRenderer; - public Camera mainCamera; - private bool drawing = false; - public Material material; - public Config config; - - void Start() { - } - - private Node startNode; - - private void startRoad() { - Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out RaycastHit hit, Mathf.Infinity, 1 << 6); - foreach (Node node in nodes) { - if ((hit.point - node.position).magnitude < 1.0f) { - startNode = node; - drawing = true; - return; - } - } - Vector3 position = new Vector3(hit.point.x, 0.0f, hit.point.z); - startNode = new Node(position, transform, config); - nodes.Add(startNode); - drawing = true; - } - - private void endRoad() { - Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out RaycastHit hit, Mathf.Infinity, 1 << 6); - Node endNode = null; - foreach (Node node in nodes) { - if ((hit.point - node.position).magnitude < 1.0f) { - endNode = node; - break; - } - } - if (endNode == null) { - Vector3 position = new Vector3(hit.point.x, 0.0f, hit.point.z); - endNode = new Node(position, transform, config); - nodes.Add(endNode); - } - Road road = new Road(startNode, endNode, config); - if (!roads.Contains(road) && startNode != endNode) { - startNode.roads.Add(road); - endNode.roads.Add(road); - roads.Add(road); - road.initialize(transform); - road.update(true); - } - startNode = null; - drawing = false; - } - - Node pulling = null; - - void Update() { - Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); - RaycastHit hit; - if (config.mode == Mode.DrawRoad) { - if (Input.GetAxis("Fire1") != 0.0f) { - if (!drawing) { - startRoad(); - } - } else if (drawing) { - endRoad(); - } - } else if (config.mode == Mode.DeleteRoad) { - if (!drawing && Input.GetAxis("Fire1") != 0.0f) { - if (Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 8)) { - drawing = true; - Road road = hit.transform.gameObject.GetComponent().road; - roads.Remove(road); - foreach (Node node in road.nodes) { - node.roads.Remove(road); - node.update(); - } - Destroy(hit.transform.parent.gameObject); - } - } else if (drawing && Input.GetAxis("Fire1") == 0.0f) { - drawing = false; - } - } - if (Input.GetAxis("Fire2") != 0.0f) { - if (pulling == null && Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 7)) { - pulling = nodes.Find(node => node.gameObject == hit.transform.gameObject); - } - } else { - pulling = null; - } - if (pulling != null) { - Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 6); - Vector3 position = new Vector3(hit.point.x, 0.0f, hit.point.z); - pulling.pull(position); - } - } -} diff --git a/Assets/Scripts/Bezier.cs b/Assets/Scripts/Bezier.cs deleted file mode 100644 index ea19006..0000000 --- a/Assets/Scripts/Bezier.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Bezier { - public Vector3 A, B, C, D; - - public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { - this.A = A; - this.B = B; - this.C = C; - this.D = D; - } - - public Bezier() { - this.A = Vector3.zero; - this.B = Vector3.zero; - this.C = Vector3.zero; - this.D = Vector3.zero; - } - - public Vector3 getPosition(float t) { - float T = 1.0f - t; - return - A * ( T*T*T) + - B * (3*T*T*t) + - C * (3*T*t*t) + - D * ( t*t*t); - } - - public Vector3 getDirection(float t) { - float T = 1.0f - t; - return - A * (-3*T*T ) + - B * ( 3*T*T - 6*T*t) + - C * (-3*t*t + 6*T*t) + - D * ( 3*t*t ); - } -} diff --git a/Assets/Scripts/Bezier.cs.meta b/Assets/Scripts/Bezier.cs.meta deleted file mode 100644 index 14630e2..0000000 --- a/Assets/Scripts/Bezier.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 284cf9d52e385aa4ebba97eb8b43b8a6 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatBezierRenderer.cs b/Assets/Scripts/FlatBezierRenderer.cs deleted file mode 100644 index b8dad15..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatBezierRenderer { - private int percision; - private float width; - public Bezier bezier; - public Mesh mesh = new Mesh(); - public FlatBezierRenderer(Bezier bezier, int percision, float width) { - this.percision = percision; - this.width = width; - this.bezier = bezier; - update(); - } - - public void update() { - float realWidth = width / 2; - int vertexCount = 6 * (percision+1); - Vector3[] vertices = new Vector3[vertexCount / 3]; - int[] indices = new int[vertexCount]; - Vector3 position = bezier.getPosition(0.0f); - Vector3 tangent = getTangent(0.0f); - vertices[0] = position + tangent * realWidth; - vertices[1] = position - tangent * realWidth; - for (int i = 1; i <= percision; i++) { - float t = (float) i / percision; - indices[6*i ] = 2*i ; - indices[6*i+1] = 2*i-1; - indices[6*i+2] = 2*i-2; - indices[6*i+3] = 2*i+1; - indices[6*i+4] = 2*i-1; - indices[6*i+5] = 2*i ; - - position = bezier.getPosition(t); - tangent = getTangent(t); - vertices[2*i ] = position + tangent * realWidth; - vertices[2*i+1] = position - tangent * realWidth; - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - mesh.Optimize(); - } - - private Vector3 getTangent(float t) { - Vector3 direction = bezier.getDirection(t); - direction.Normalize(); - return new Vector3(-direction.z, 0, direction.x); - } -} diff --git a/Assets/Scripts/FlatBezierRenderer.cs.meta b/Assets/Scripts/FlatBezierRenderer.cs.meta deleted file mode 100644 index 1fe6547..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 9bfb303c44d15ef468dc0d7334241e94 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatCircleRenderer.cs b/Assets/Scripts/FlatCircleRenderer.cs deleted file mode 100644 index 363b663..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatCircleRenderer { - public Mesh mesh = new Mesh(); - public float radius, width; - private int segments; - - public FlatCircleRenderer(float radius, float width, int segments) { - this.radius = radius; - this.width = width; - this.segments = segments; - update(); - } - - public void update() { - Vector3[] vertices = new Vector3[2*segments+6]; - int[] indices = new int[6*segments]; - for (int i = 0; i < segments; i++) { - float alpha = (float) i / segments * 2 * Mathf.PI; - indices[6*i ] = 2*i ; - indices[6*i+3] = 2*i ; - indices[6*i+5] = 2*i+1; - if (i == 0) { - indices[6*i+1] = 2*segments-2; - indices[6*i+2] = 2*segments-1; - indices[6*i+4] = 2*segments-1; - } else { - indices[6*i+1] = 2*i-2; - indices[6*i+2] = 2*i-1; - indices[6*i+4] = 2*i-1; - } - Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); - vertices[2*i ] = direction * radius; - vertices[2*i+1] = direction * (radius + width); - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - } -} diff --git a/Assets/Scripts/FlatCircleRenderer.cs.meta b/Assets/Scripts/FlatCircleRenderer.cs.meta deleted file mode 100644 index d10c238..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0fe81c7924a85b142a4a86201b08c241 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Node.cs b/Assets/Scripts/Node.cs deleted file mode 100644 index e3c4cbd..0000000 --- a/Assets/Scripts/Node.cs +++ /dev/null @@ -1,81 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Node { - public FlatCircleRenderer circle = new FlatCircleRenderer(0.2f, 0.05f, 32); - public FlatCircleRenderer fullCircle = new FlatCircleRenderer(0f, 1f, 32); - - public Vector3 position; - public List roads = new List(); - public GameObject gameObject; - - public Node(Vector3 position, Transform parent, Config config) { - gameObject = new GameObject(); - gameObject.transform.position = position; - gameObject.transform.parent = parent; - gameObject.AddComponent().radius = 0.5f; - gameObject.layer = 7; - - GameObject nodeCircle = new GameObject(); - nodeCircle.AddComponent().material = config.roadEditMaterial; - nodeCircle.AddComponent().mesh = circle.mesh; - nodeCircle.transform.parent = gameObject.transform; - nodeCircle.transform.localPosition = Vector3.zero; - - GameObject nodeRoad = new GameObject(); - nodeRoad.AddComponent().material = config.roadMaterial; - nodeRoad.AddComponent().mesh = fullCircle.mesh; - nodeRoad.transform.parent = gameObject.transform; - nodeRoad.transform.localPosition = Vector3.zero; - 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(); - } - - override public bool Equals(object other) { - if ((other == null) || ! this.GetType().Equals(other.GetType())) { - return false; - } - if (other == this) { - return true; - } - return ((Node)other).position.Equals(position); - } - - public void pull(Vector3 position) { - this.position = position; - gameObject.transform.position = position; - foreach (Road road in roads) { - 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/Node.cs.meta b/Assets/Scripts/Node.cs.meta deleted file mode 100644 index 8bbedf1..0000000 --- a/Assets/Scripts/Node.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 3cbed1dad7ec67d4dbe8602aea1e2979 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Primitives.meta b/Assets/Scripts/Primitives.meta new file mode 100644 index 0000000..0cf8895 --- /dev/null +++ b/Assets/Scripts/Primitives.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 6d1a8c7f33189a240b4d749356f53399 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/Bezier.cs b/Assets/Scripts/Primitives/Bezier.cs new file mode 100644 index 0000000..ea19006 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs @@ -0,0 +1,39 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Bezier { + public Vector3 A, B, C, D; + + public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { + this.A = A; + this.B = B; + this.C = C; + this.D = D; + } + + public Bezier() { + this.A = Vector3.zero; + this.B = Vector3.zero; + this.C = Vector3.zero; + this.D = Vector3.zero; + } + + public Vector3 getPosition(float t) { + float T = 1.0f - t; + return + A * ( T*T*T) + + B * (3*T*T*t) + + C * (3*T*t*t) + + D * ( t*t*t); + } + + public Vector3 getDirection(float t) { + float T = 1.0f - t; + return + A * (-3*T*T ) + + B * ( 3*T*T - 6*T*t) + + C * (-3*t*t + 6*T*t) + + D * ( 3*t*t ); + } +} diff --git a/Assets/Scripts/Primitives/Bezier.cs.meta b/Assets/Scripts/Primitives/Bezier.cs.meta new file mode 100644 index 0000000..14630e2 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 284cf9d52e385aa4ebba97eb8b43b8a6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs b/Assets/Scripts/Primitives/FlatBezierRenderer.cs new file mode 100644 index 0000000..b8dad15 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs @@ -0,0 +1,51 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FlatBezierRenderer { + private int percision; + private float width; + public Bezier bezier; + public Mesh mesh = new Mesh(); + public FlatBezierRenderer(Bezier bezier, int percision, float width) { + this.percision = percision; + this.width = width; + this.bezier = bezier; + update(); + } + + public void update() { + float realWidth = width / 2; + int vertexCount = 6 * (percision+1); + Vector3[] vertices = new Vector3[vertexCount / 3]; + int[] indices = new int[vertexCount]; + Vector3 position = bezier.getPosition(0.0f); + Vector3 tangent = getTangent(0.0f); + vertices[0] = position + tangent * realWidth; + vertices[1] = position - tangent * realWidth; + for (int i = 1; i <= percision; i++) { + float t = (float) i / percision; + indices[6*i ] = 2*i ; + indices[6*i+1] = 2*i-1; + indices[6*i+2] = 2*i-2; + indices[6*i+3] = 2*i+1; + indices[6*i+4] = 2*i-1; + indices[6*i+5] = 2*i ; + + position = bezier.getPosition(t); + tangent = getTangent(t); + vertices[2*i ] = position + tangent * realWidth; + vertices[2*i+1] = position - tangent * realWidth; + } + mesh.Clear(); + mesh.vertices = vertices; + mesh.triangles = indices; + mesh.Optimize(); + } + + private Vector3 getTangent(float t) { + Vector3 direction = bezier.getDirection(t); + direction.Normalize(); + return new Vector3(-direction.z, 0, direction.x); + } +} diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta b/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta new file mode 100644 index 0000000..1fe6547 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9bfb303c44d15ef468dc0d7334241e94 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/FlatCircleRenderer.cs b/Assets/Scripts/Primitives/FlatCircleRenderer.cs new file mode 100644 index 0000000..363b663 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatCircleRenderer.cs @@ -0,0 +1,42 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FlatCircleRenderer { + public Mesh mesh = new Mesh(); + public float radius, width; + private int segments; + + public FlatCircleRenderer(float radius, float width, int segments) { + this.radius = radius; + this.width = width; + this.segments = segments; + update(); + } + + public void update() { + Vector3[] vertices = new Vector3[2*segments+6]; + int[] indices = new int[6*segments]; + for (int i = 0; i < segments; i++) { + float alpha = (float) i / segments * 2 * Mathf.PI; + indices[6*i ] = 2*i ; + indices[6*i+3] = 2*i ; + indices[6*i+5] = 2*i+1; + if (i == 0) { + indices[6*i+1] = 2*segments-2; + indices[6*i+2] = 2*segments-1; + indices[6*i+4] = 2*segments-1; + } else { + indices[6*i+1] = 2*i-2; + indices[6*i+2] = 2*i-1; + indices[6*i+4] = 2*i-1; + } + Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); + vertices[2*i ] = direction * radius; + vertices[2*i+1] = direction * (radius + width); + } + mesh.Clear(); + mesh.vertices = vertices; + mesh.triangles = indices; + } +} diff --git a/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta b/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta new file mode 100644 index 0000000..d10c238 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0fe81c7924a85b142a4a86201b08c241 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Road.cs b/Assets/Scripts/Road.cs deleted file mode 100644 index 0af2218..0000000 --- a/Assets/Scripts/Road.cs +++ /dev/null @@ -1,85 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Road { - public List nodes = new List(); - Bezier path; - FlatBezierRenderer pathLine, roadBody; - Config config; - MeshCollider collider; - - public Road(Node start, Node end, Config config) { - nodes.Add(start); - nodes.Add(end); - path = new Bezier(); - pathLine = new FlatBezierRenderer(path, 50, 0.05f); - roadBody = new FlatBezierRenderer(path, 50, 2f); - this.config = config; - } - - - public void initialize(Transform parent) { - GameObject child = new GameObject(); - child.transform.parent = parent; - GameObject lineChild = new GameObject(); - lineChild.transform.position = Vector3.zero; - lineChild.AddComponent().material = config.roadEditMaterial; - lineChild.AddComponent().mesh = pathLine.mesh; - lineChild.transform.parent = child.transform; - - GameObject roadChild = new GameObject(); - collider = roadChild.AddComponent(); - roadChild.transform.position = Vector3.zero; - roadChild.AddComponent().material = config.roadMaterial; - roadChild.transform.parent = child.transform; - roadChild.layer = 8; - roadChild.AddComponent().mesh = roadBody.mesh; - roadChild.AddComponent().road = this; - } - - public void update(bool updateOthers) { - path.A = nodes[0].position; - path.D = nodes[1].position; - if (nodes[0].roads.Count == 2) { - path.B = nodes[0].position + - 0.25f * (nodes[1].position - nodes[0].getOther(nodes[1]).position); - } else { - path.B = 0.5f * (nodes[0].position + nodes[1].position); - } - if (nodes[1].roads.Count == 2) { - path.C = nodes[1].position + - 0.25f * (nodes[0].position - nodes[1].getOther(nodes[0]).position); - } else { - path.C = 0.5f * (nodes[0].position + nodes[1].position); - } - if (updateOthers) { - foreach (Node node in nodes) { - node.lateUpdate(this); - } - } - pathLine.update(); - roadBody.update(); - collider.sharedMesh = roadBody.mesh; - } - - override public bool Equals(object other) { - if (other == null || !(other is Road)) { - return false; - } - if (other == this) { - return true; - } - Road otherRoad = (Road) other; - foreach (Node node in nodes) { - if (! otherRoad.nodes.Contains(node)) { - return false; - } - } - return true; - } - - public override int GetHashCode() { - return nodes[0].GetHashCode() << 16 | nodes[1].GetHashCode(); - } -} diff --git a/Assets/Scripts/Road.cs.meta b/Assets/Scripts/Road.cs.meta deleted file mode 100644 index 24dc94b..0000000 --- a/Assets/Scripts/Road.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0ab512a631c0cc4468e77b6a2dfeb31f -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/RoadData.cs b/Assets/Scripts/RoadData.cs deleted file mode 100644 index eb620ba..0000000 --- a/Assets/Scripts/RoadData.cs +++ /dev/null @@ -1,7 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class RoadData : MonoBehaviour { - public Road road; -} diff --git a/Assets/Scripts/RoadData.cs.meta b/Assets/Scripts/RoadData.cs.meta deleted file mode 100644 index 2948b97..0000000 --- a/Assets/Scripts/RoadData.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 6bfac654eff68754c88b877a9b88ae42 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/RoadEdit.cs b/Assets/Scripts/RoadEdit.cs deleted file mode 100644 index a6cc73e..0000000 --- a/Assets/Scripts/RoadEdit.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class RoadEdit : MonoBehaviour { - public GameObject start; - public GameObject startDirection; - public GameObject end; - public GameObject endDirection; - public Camera mainCamera; - - private GameObject currentlyPulling = null; - public Bezier bezier; - private FlatBezierRenderer pathRenderer; - public Material highlight, noHighlight; - - void Start() { - } - - void updateRoad(Vector3 position) { - } - - void Update() { - Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); - if (Input.GetAxis("Fire1") != 0.0f) { - if (currentlyPulling == null && Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, 1 << 7)) { - currentlyPulling = hit.transform.gameObject; - currentlyPulling.GetComponent().material = highlight; - } - } else { - if (currentlyPulling != null) { - currentlyPulling.GetComponent().material = noHighlight; - } - currentlyPulling = null; - } - if (currentlyPulling != null) { - Physics.Raycast(ray, out RaycastHit hit_, Mathf.Infinity, 1 << 6); - Vector3 position = new Vector3(hit_.point.x, 0.0f, hit_.point.z); - currentlyPulling.transform.position = position; - updateRoad(position); - } - } -} diff --git a/Assets/Scripts/RoadEdit.cs.meta b/Assets/Scripts/RoadEdit.cs.meta deleted file mode 100644 index 4e09d01..0000000 --- a/Assets/Scripts/RoadEdit.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 68e9412fa432f2e468d5d7447e99fd89 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Roads.cs b/Assets/Scripts/Roads.cs deleted file mode 100644 index acd376a..0000000 --- a/Assets/Scripts/Roads.cs +++ /dev/null @@ -1,102 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Roads : MonoBehaviour { - public List nodes = new List(); - public List roads = new List(); - public FlatBezierRenderer roadRenderer; - public Camera mainCamera; - private bool drawing = false; - public Material material; - public Config config; - - void Start() { - } - - private Node startNode; - - private void startRoad() { - Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out RaycastHit hit, Mathf.Infinity, 1 << 6); - foreach (Node node in nodes) { - if ((hit.point - node.position).magnitude < 1.0f) { - startNode = node; - drawing = true; - return; - } - } - Vector3 position = new Vector3(hit.point.x, 0.0f, hit.point.z); - startNode = new Node(position, transform, config); - nodes.Add(startNode); - drawing = true; - } - - private void endRoad() { - Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out RaycastHit hit, Mathf.Infinity, 1 << 6); - Node endNode = null; - foreach (Node node in nodes) { - if ((hit.point - node.position).magnitude < 1.0f) { - endNode = node; - break; - } - } - if (endNode == null) { - Vector3 position = new Vector3(hit.point.x, 0.0f, hit.point.z); - endNode = new Node(position, transform, config); - nodes.Add(endNode); - } - Road road = new Road(startNode, endNode, config); - if (!roads.Contains(road) && startNode != endNode) { - startNode.roads.Add(road); - endNode.roads.Add(road); - roads.Add(road); - road.initialize(transform); - road.update(true); - } - startNode = null; - drawing = false; - } - - Node pulling = null; - - void Update() { - Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); - RaycastHit hit; - if (config.mode == Mode.DrawRoad) { - if (Input.GetAxis("Fire1") != 0.0f) { - if (!drawing) { - startRoad(); - } - } else if (drawing) { - endRoad(); - } - } else if (config.mode == Mode.DeleteRoad) { - if (!drawing && Input.GetAxis("Fire1") != 0.0f) { - if (Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 8)) { - drawing = true; - Road road = hit.transform.gameObject.GetComponent().road; - roads.Remove(road); - foreach (Node node in road.nodes) { - node.roads.Remove(road); - node.update(); - } - Destroy(hit.transform.parent.gameObject); - } - } else if (drawing && Input.GetAxis("Fire1") == 0.0f) { - drawing = false; - } - } - if (Input.GetAxis("Fire2") != 0.0f) { - if (pulling == null && Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 7)) { - pulling = nodes.Find(node => node.gameObject == hit.transform.gameObject); - } - } else { - pulling = null; - } - if (pulling != null) { - Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 6); - Vector3 position = new Vector3(hit.point.x, 0.0f, hit.point.z); - pulling.pull(position); - } - } -} diff --git a/Assets/Scripts/Roads.cs.meta b/Assets/Scripts/Roads.cs.meta deleted file mode 100644 index f26a401..0000000 --- a/Assets/Scripts/Roads.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 7635c530aa8ef31498f100f4e7465bea -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Bezier.cs b/Assets/Scripts/Bezier.cs deleted file mode 100644 index ea19006..0000000 --- a/Assets/Scripts/Bezier.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Bezier { - public Vector3 A, B, C, D; - - public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { - this.A = A; - this.B = B; - this.C = C; - this.D = D; - } - - public Bezier() { - this.A = Vector3.zero; - this.B = Vector3.zero; - this.C = Vector3.zero; - this.D = Vector3.zero; - } - - public Vector3 getPosition(float t) { - float T = 1.0f - t; - return - A * ( T*T*T) + - B * (3*T*T*t) + - C * (3*T*t*t) + - D * ( t*t*t); - } - - public Vector3 getDirection(float t) { - float T = 1.0f - t; - return - A * (-3*T*T ) + - B * ( 3*T*T - 6*T*t) + - C * (-3*t*t + 6*T*t) + - D * ( 3*t*t ); - } -} diff --git a/Assets/Scripts/Bezier.cs.meta b/Assets/Scripts/Bezier.cs.meta deleted file mode 100644 index 14630e2..0000000 --- a/Assets/Scripts/Bezier.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 284cf9d52e385aa4ebba97eb8b43b8a6 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatBezierRenderer.cs b/Assets/Scripts/FlatBezierRenderer.cs deleted file mode 100644 index b8dad15..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatBezierRenderer { - private int percision; - private float width; - public Bezier bezier; - public Mesh mesh = new Mesh(); - public FlatBezierRenderer(Bezier bezier, int percision, float width) { - this.percision = percision; - this.width = width; - this.bezier = bezier; - update(); - } - - public void update() { - float realWidth = width / 2; - int vertexCount = 6 * (percision+1); - Vector3[] vertices = new Vector3[vertexCount / 3]; - int[] indices = new int[vertexCount]; - Vector3 position = bezier.getPosition(0.0f); - Vector3 tangent = getTangent(0.0f); - vertices[0] = position + tangent * realWidth; - vertices[1] = position - tangent * realWidth; - for (int i = 1; i <= percision; i++) { - float t = (float) i / percision; - indices[6*i ] = 2*i ; - indices[6*i+1] = 2*i-1; - indices[6*i+2] = 2*i-2; - indices[6*i+3] = 2*i+1; - indices[6*i+4] = 2*i-1; - indices[6*i+5] = 2*i ; - - position = bezier.getPosition(t); - tangent = getTangent(t); - vertices[2*i ] = position + tangent * realWidth; - vertices[2*i+1] = position - tangent * realWidth; - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - mesh.Optimize(); - } - - private Vector3 getTangent(float t) { - Vector3 direction = bezier.getDirection(t); - direction.Normalize(); - return new Vector3(-direction.z, 0, direction.x); - } -} diff --git a/Assets/Scripts/FlatBezierRenderer.cs.meta b/Assets/Scripts/FlatBezierRenderer.cs.meta deleted file mode 100644 index 1fe6547..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 9bfb303c44d15ef468dc0d7334241e94 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatCircleRenderer.cs b/Assets/Scripts/FlatCircleRenderer.cs deleted file mode 100644 index 363b663..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatCircleRenderer { - public Mesh mesh = new Mesh(); - public float radius, width; - private int segments; - - public FlatCircleRenderer(float radius, float width, int segments) { - this.radius = radius; - this.width = width; - this.segments = segments; - update(); - } - - public void update() { - Vector3[] vertices = new Vector3[2*segments+6]; - int[] indices = new int[6*segments]; - for (int i = 0; i < segments; i++) { - float alpha = (float) i / segments * 2 * Mathf.PI; - indices[6*i ] = 2*i ; - indices[6*i+3] = 2*i ; - indices[6*i+5] = 2*i+1; - if (i == 0) { - indices[6*i+1] = 2*segments-2; - indices[6*i+2] = 2*segments-1; - indices[6*i+4] = 2*segments-1; - } else { - indices[6*i+1] = 2*i-2; - indices[6*i+2] = 2*i-1; - indices[6*i+4] = 2*i-1; - } - Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); - vertices[2*i ] = direction * radius; - vertices[2*i+1] = direction * (radius + width); - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - } -} diff --git a/Assets/Scripts/FlatCircleRenderer.cs.meta b/Assets/Scripts/FlatCircleRenderer.cs.meta deleted file mode 100644 index d10c238..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0fe81c7924a85b142a4a86201b08c241 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Node.cs b/Assets/Scripts/Node.cs deleted file mode 100644 index e3c4cbd..0000000 --- a/Assets/Scripts/Node.cs +++ /dev/null @@ -1,81 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Node { - public FlatCircleRenderer circle = new FlatCircleRenderer(0.2f, 0.05f, 32); - public FlatCircleRenderer fullCircle = new FlatCircleRenderer(0f, 1f, 32); - - public Vector3 position; - public List roads = new List(); - public GameObject gameObject; - - public Node(Vector3 position, Transform parent, Config config) { - gameObject = new GameObject(); - gameObject.transform.position = position; - gameObject.transform.parent = parent; - gameObject.AddComponent().radius = 0.5f; - gameObject.layer = 7; - - GameObject nodeCircle = new GameObject(); - nodeCircle.AddComponent().material = config.roadEditMaterial; - nodeCircle.AddComponent().mesh = circle.mesh; - nodeCircle.transform.parent = gameObject.transform; - nodeCircle.transform.localPosition = Vector3.zero; - - GameObject nodeRoad = new GameObject(); - nodeRoad.AddComponent().material = config.roadMaterial; - nodeRoad.AddComponent().mesh = fullCircle.mesh; - nodeRoad.transform.parent = gameObject.transform; - nodeRoad.transform.localPosition = Vector3.zero; - 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(); - } - - override public bool Equals(object other) { - if ((other == null) || ! this.GetType().Equals(other.GetType())) { - return false; - } - if (other == this) { - return true; - } - return ((Node)other).position.Equals(position); - } - - public void pull(Vector3 position) { - this.position = position; - gameObject.transform.position = position; - foreach (Road road in roads) { - 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/Node.cs.meta b/Assets/Scripts/Node.cs.meta deleted file mode 100644 index 8bbedf1..0000000 --- a/Assets/Scripts/Node.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 3cbed1dad7ec67d4dbe8602aea1e2979 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Primitives.meta b/Assets/Scripts/Primitives.meta new file mode 100644 index 0000000..0cf8895 --- /dev/null +++ b/Assets/Scripts/Primitives.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 6d1a8c7f33189a240b4d749356f53399 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/Bezier.cs b/Assets/Scripts/Primitives/Bezier.cs new file mode 100644 index 0000000..ea19006 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs @@ -0,0 +1,39 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Bezier { + public Vector3 A, B, C, D; + + public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { + this.A = A; + this.B = B; + this.C = C; + this.D = D; + } + + public Bezier() { + this.A = Vector3.zero; + this.B = Vector3.zero; + this.C = Vector3.zero; + this.D = Vector3.zero; + } + + public Vector3 getPosition(float t) { + float T = 1.0f - t; + return + A * ( T*T*T) + + B * (3*T*T*t) + + C * (3*T*t*t) + + D * ( t*t*t); + } + + public Vector3 getDirection(float t) { + float T = 1.0f - t; + return + A * (-3*T*T ) + + B * ( 3*T*T - 6*T*t) + + C * (-3*t*t + 6*T*t) + + D * ( 3*t*t ); + } +} diff --git a/Assets/Scripts/Primitives/Bezier.cs.meta b/Assets/Scripts/Primitives/Bezier.cs.meta new file mode 100644 index 0000000..14630e2 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 284cf9d52e385aa4ebba97eb8b43b8a6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs b/Assets/Scripts/Primitives/FlatBezierRenderer.cs new file mode 100644 index 0000000..b8dad15 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs @@ -0,0 +1,51 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FlatBezierRenderer { + private int percision; + private float width; + public Bezier bezier; + public Mesh mesh = new Mesh(); + public FlatBezierRenderer(Bezier bezier, int percision, float width) { + this.percision = percision; + this.width = width; + this.bezier = bezier; + update(); + } + + public void update() { + float realWidth = width / 2; + int vertexCount = 6 * (percision+1); + Vector3[] vertices = new Vector3[vertexCount / 3]; + int[] indices = new int[vertexCount]; + Vector3 position = bezier.getPosition(0.0f); + Vector3 tangent = getTangent(0.0f); + vertices[0] = position + tangent * realWidth; + vertices[1] = position - tangent * realWidth; + for (int i = 1; i <= percision; i++) { + float t = (float) i / percision; + indices[6*i ] = 2*i ; + indices[6*i+1] = 2*i-1; + indices[6*i+2] = 2*i-2; + indices[6*i+3] = 2*i+1; + indices[6*i+4] = 2*i-1; + indices[6*i+5] = 2*i ; + + position = bezier.getPosition(t); + tangent = getTangent(t); + vertices[2*i ] = position + tangent * realWidth; + vertices[2*i+1] = position - tangent * realWidth; + } + mesh.Clear(); + mesh.vertices = vertices; + mesh.triangles = indices; + mesh.Optimize(); + } + + private Vector3 getTangent(float t) { + Vector3 direction = bezier.getDirection(t); + direction.Normalize(); + return new Vector3(-direction.z, 0, direction.x); + } +} diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta b/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta new file mode 100644 index 0000000..1fe6547 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9bfb303c44d15ef468dc0d7334241e94 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/FlatCircleRenderer.cs b/Assets/Scripts/Primitives/FlatCircleRenderer.cs new file mode 100644 index 0000000..363b663 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatCircleRenderer.cs @@ -0,0 +1,42 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FlatCircleRenderer { + public Mesh mesh = new Mesh(); + public float radius, width; + private int segments; + + public FlatCircleRenderer(float radius, float width, int segments) { + this.radius = radius; + this.width = width; + this.segments = segments; + update(); + } + + public void update() { + Vector3[] vertices = new Vector3[2*segments+6]; + int[] indices = new int[6*segments]; + for (int i = 0; i < segments; i++) { + float alpha = (float) i / segments * 2 * Mathf.PI; + indices[6*i ] = 2*i ; + indices[6*i+3] = 2*i ; + indices[6*i+5] = 2*i+1; + if (i == 0) { + indices[6*i+1] = 2*segments-2; + indices[6*i+2] = 2*segments-1; + indices[6*i+4] = 2*segments-1; + } else { + indices[6*i+1] = 2*i-2; + indices[6*i+2] = 2*i-1; + indices[6*i+4] = 2*i-1; + } + Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); + vertices[2*i ] = direction * radius; + vertices[2*i+1] = direction * (radius + width); + } + mesh.Clear(); + mesh.vertices = vertices; + mesh.triangles = indices; + } +} diff --git a/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta b/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta new file mode 100644 index 0000000..d10c238 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0fe81c7924a85b142a4a86201b08c241 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Road.cs b/Assets/Scripts/Road.cs deleted file mode 100644 index 0af2218..0000000 --- a/Assets/Scripts/Road.cs +++ /dev/null @@ -1,85 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Road { - public List nodes = new List(); - Bezier path; - FlatBezierRenderer pathLine, roadBody; - Config config; - MeshCollider collider; - - public Road(Node start, Node end, Config config) { - nodes.Add(start); - nodes.Add(end); - path = new Bezier(); - pathLine = new FlatBezierRenderer(path, 50, 0.05f); - roadBody = new FlatBezierRenderer(path, 50, 2f); - this.config = config; - } - - - public void initialize(Transform parent) { - GameObject child = new GameObject(); - child.transform.parent = parent; - GameObject lineChild = new GameObject(); - lineChild.transform.position = Vector3.zero; - lineChild.AddComponent().material = config.roadEditMaterial; - lineChild.AddComponent().mesh = pathLine.mesh; - lineChild.transform.parent = child.transform; - - GameObject roadChild = new GameObject(); - collider = roadChild.AddComponent(); - roadChild.transform.position = Vector3.zero; - roadChild.AddComponent().material = config.roadMaterial; - roadChild.transform.parent = child.transform; - roadChild.layer = 8; - roadChild.AddComponent().mesh = roadBody.mesh; - roadChild.AddComponent().road = this; - } - - public void update(bool updateOthers) { - path.A = nodes[0].position; - path.D = nodes[1].position; - if (nodes[0].roads.Count == 2) { - path.B = nodes[0].position + - 0.25f * (nodes[1].position - nodes[0].getOther(nodes[1]).position); - } else { - path.B = 0.5f * (nodes[0].position + nodes[1].position); - } - if (nodes[1].roads.Count == 2) { - path.C = nodes[1].position + - 0.25f * (nodes[0].position - nodes[1].getOther(nodes[0]).position); - } else { - path.C = 0.5f * (nodes[0].position + nodes[1].position); - } - if (updateOthers) { - foreach (Node node in nodes) { - node.lateUpdate(this); - } - } - pathLine.update(); - roadBody.update(); - collider.sharedMesh = roadBody.mesh; - } - - override public bool Equals(object other) { - if (other == null || !(other is Road)) { - return false; - } - if (other == this) { - return true; - } - Road otherRoad = (Road) other; - foreach (Node node in nodes) { - if (! otherRoad.nodes.Contains(node)) { - return false; - } - } - return true; - } - - public override int GetHashCode() { - return nodes[0].GetHashCode() << 16 | nodes[1].GetHashCode(); - } -} diff --git a/Assets/Scripts/Road.cs.meta b/Assets/Scripts/Road.cs.meta deleted file mode 100644 index 24dc94b..0000000 --- a/Assets/Scripts/Road.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0ab512a631c0cc4468e77b6a2dfeb31f -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/RoadData.cs b/Assets/Scripts/RoadData.cs deleted file mode 100644 index eb620ba..0000000 --- a/Assets/Scripts/RoadData.cs +++ /dev/null @@ -1,7 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class RoadData : MonoBehaviour { - public Road road; -} diff --git a/Assets/Scripts/RoadData.cs.meta b/Assets/Scripts/RoadData.cs.meta deleted file mode 100644 index 2948b97..0000000 --- a/Assets/Scripts/RoadData.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 6bfac654eff68754c88b877a9b88ae42 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/RoadEdit.cs b/Assets/Scripts/RoadEdit.cs deleted file mode 100644 index a6cc73e..0000000 --- a/Assets/Scripts/RoadEdit.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class RoadEdit : MonoBehaviour { - public GameObject start; - public GameObject startDirection; - public GameObject end; - public GameObject endDirection; - public Camera mainCamera; - - private GameObject currentlyPulling = null; - public Bezier bezier; - private FlatBezierRenderer pathRenderer; - public Material highlight, noHighlight; - - void Start() { - } - - void updateRoad(Vector3 position) { - } - - void Update() { - Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); - if (Input.GetAxis("Fire1") != 0.0f) { - if (currentlyPulling == null && Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, 1 << 7)) { - currentlyPulling = hit.transform.gameObject; - currentlyPulling.GetComponent().material = highlight; - } - } else { - if (currentlyPulling != null) { - currentlyPulling.GetComponent().material = noHighlight; - } - currentlyPulling = null; - } - if (currentlyPulling != null) { - Physics.Raycast(ray, out RaycastHit hit_, Mathf.Infinity, 1 << 6); - Vector3 position = new Vector3(hit_.point.x, 0.0f, hit_.point.z); - currentlyPulling.transform.position = position; - updateRoad(position); - } - } -} diff --git a/Assets/Scripts/RoadEdit.cs.meta b/Assets/Scripts/RoadEdit.cs.meta deleted file mode 100644 index 4e09d01..0000000 --- a/Assets/Scripts/RoadEdit.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 68e9412fa432f2e468d5d7447e99fd89 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Roads.cs b/Assets/Scripts/Roads.cs deleted file mode 100644 index acd376a..0000000 --- a/Assets/Scripts/Roads.cs +++ /dev/null @@ -1,102 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Roads : MonoBehaviour { - public List nodes = new List(); - public List roads = new List(); - public FlatBezierRenderer roadRenderer; - public Camera mainCamera; - private bool drawing = false; - public Material material; - public Config config; - - void Start() { - } - - private Node startNode; - - private void startRoad() { - Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out RaycastHit hit, Mathf.Infinity, 1 << 6); - foreach (Node node in nodes) { - if ((hit.point - node.position).magnitude < 1.0f) { - startNode = node; - drawing = true; - return; - } - } - Vector3 position = new Vector3(hit.point.x, 0.0f, hit.point.z); - startNode = new Node(position, transform, config); - nodes.Add(startNode); - drawing = true; - } - - private void endRoad() { - Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out RaycastHit hit, Mathf.Infinity, 1 << 6); - Node endNode = null; - foreach (Node node in nodes) { - if ((hit.point - node.position).magnitude < 1.0f) { - endNode = node; - break; - } - } - if (endNode == null) { - Vector3 position = new Vector3(hit.point.x, 0.0f, hit.point.z); - endNode = new Node(position, transform, config); - nodes.Add(endNode); - } - Road road = new Road(startNode, endNode, config); - if (!roads.Contains(road) && startNode != endNode) { - startNode.roads.Add(road); - endNode.roads.Add(road); - roads.Add(road); - road.initialize(transform); - road.update(true); - } - startNode = null; - drawing = false; - } - - Node pulling = null; - - void Update() { - Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); - RaycastHit hit; - if (config.mode == Mode.DrawRoad) { - if (Input.GetAxis("Fire1") != 0.0f) { - if (!drawing) { - startRoad(); - } - } else if (drawing) { - endRoad(); - } - } else if (config.mode == Mode.DeleteRoad) { - if (!drawing && Input.GetAxis("Fire1") != 0.0f) { - if (Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 8)) { - drawing = true; - Road road = hit.transform.gameObject.GetComponent().road; - roads.Remove(road); - foreach (Node node in road.nodes) { - node.roads.Remove(road); - node.update(); - } - Destroy(hit.transform.parent.gameObject); - } - } else if (drawing && Input.GetAxis("Fire1") == 0.0f) { - drawing = false; - } - } - if (Input.GetAxis("Fire2") != 0.0f) { - if (pulling == null && Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 7)) { - pulling = nodes.Find(node => node.gameObject == hit.transform.gameObject); - } - } else { - pulling = null; - } - if (pulling != null) { - Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 6); - Vector3 position = new Vector3(hit.point.x, 0.0f, hit.point.z); - pulling.pull(position); - } - } -} diff --git a/Assets/Scripts/Roads.cs.meta b/Assets/Scripts/Roads.cs.meta deleted file mode 100644 index f26a401..0000000 --- a/Assets/Scripts/Roads.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 7635c530aa8ef31498f100f4e7465bea -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Roads.meta b/Assets/Scripts/Roads.meta new file mode 100644 index 0000000..defbd46 --- /dev/null +++ b/Assets/Scripts/Roads.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: e6a3f10ad4786e44d9faf825f11a8e1d +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Bezier.cs b/Assets/Scripts/Bezier.cs deleted file mode 100644 index ea19006..0000000 --- a/Assets/Scripts/Bezier.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Bezier { - public Vector3 A, B, C, D; - - public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { - this.A = A; - this.B = B; - this.C = C; - this.D = D; - } - - public Bezier() { - this.A = Vector3.zero; - this.B = Vector3.zero; - this.C = Vector3.zero; - this.D = Vector3.zero; - } - - public Vector3 getPosition(float t) { - float T = 1.0f - t; - return - A * ( T*T*T) + - B * (3*T*T*t) + - C * (3*T*t*t) + - D * ( t*t*t); - } - - public Vector3 getDirection(float t) { - float T = 1.0f - t; - return - A * (-3*T*T ) + - B * ( 3*T*T - 6*T*t) + - C * (-3*t*t + 6*T*t) + - D * ( 3*t*t ); - } -} diff --git a/Assets/Scripts/Bezier.cs.meta b/Assets/Scripts/Bezier.cs.meta deleted file mode 100644 index 14630e2..0000000 --- a/Assets/Scripts/Bezier.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 284cf9d52e385aa4ebba97eb8b43b8a6 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatBezierRenderer.cs b/Assets/Scripts/FlatBezierRenderer.cs deleted file mode 100644 index b8dad15..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatBezierRenderer { - private int percision; - private float width; - public Bezier bezier; - public Mesh mesh = new Mesh(); - public FlatBezierRenderer(Bezier bezier, int percision, float width) { - this.percision = percision; - this.width = width; - this.bezier = bezier; - update(); - } - - public void update() { - float realWidth = width / 2; - int vertexCount = 6 * (percision+1); - Vector3[] vertices = new Vector3[vertexCount / 3]; - int[] indices = new int[vertexCount]; - Vector3 position = bezier.getPosition(0.0f); - Vector3 tangent = getTangent(0.0f); - vertices[0] = position + tangent * realWidth; - vertices[1] = position - tangent * realWidth; - for (int i = 1; i <= percision; i++) { - float t = (float) i / percision; - indices[6*i ] = 2*i ; - indices[6*i+1] = 2*i-1; - indices[6*i+2] = 2*i-2; - indices[6*i+3] = 2*i+1; - indices[6*i+4] = 2*i-1; - indices[6*i+5] = 2*i ; - - position = bezier.getPosition(t); - tangent = getTangent(t); - vertices[2*i ] = position + tangent * realWidth; - vertices[2*i+1] = position - tangent * realWidth; - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - mesh.Optimize(); - } - - private Vector3 getTangent(float t) { - Vector3 direction = bezier.getDirection(t); - direction.Normalize(); - return new Vector3(-direction.z, 0, direction.x); - } -} diff --git a/Assets/Scripts/FlatBezierRenderer.cs.meta b/Assets/Scripts/FlatBezierRenderer.cs.meta deleted file mode 100644 index 1fe6547..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 9bfb303c44d15ef468dc0d7334241e94 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatCircleRenderer.cs b/Assets/Scripts/FlatCircleRenderer.cs deleted file mode 100644 index 363b663..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatCircleRenderer { - public Mesh mesh = new Mesh(); - public float radius, width; - private int segments; - - public FlatCircleRenderer(float radius, float width, int segments) { - this.radius = radius; - this.width = width; - this.segments = segments; - update(); - } - - public void update() { - Vector3[] vertices = new Vector3[2*segments+6]; - int[] indices = new int[6*segments]; - for (int i = 0; i < segments; i++) { - float alpha = (float) i / segments * 2 * Mathf.PI; - indices[6*i ] = 2*i ; - indices[6*i+3] = 2*i ; - indices[6*i+5] = 2*i+1; - if (i == 0) { - indices[6*i+1] = 2*segments-2; - indices[6*i+2] = 2*segments-1; - indices[6*i+4] = 2*segments-1; - } else { - indices[6*i+1] = 2*i-2; - indices[6*i+2] = 2*i-1; - indices[6*i+4] = 2*i-1; - } - Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); - vertices[2*i ] = direction * radius; - vertices[2*i+1] = direction * (radius + width); - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - } -} diff --git a/Assets/Scripts/FlatCircleRenderer.cs.meta b/Assets/Scripts/FlatCircleRenderer.cs.meta deleted file mode 100644 index d10c238..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0fe81c7924a85b142a4a86201b08c241 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Node.cs b/Assets/Scripts/Node.cs deleted file mode 100644 index e3c4cbd..0000000 --- a/Assets/Scripts/Node.cs +++ /dev/null @@ -1,81 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Node { - public FlatCircleRenderer circle = new FlatCircleRenderer(0.2f, 0.05f, 32); - public FlatCircleRenderer fullCircle = new FlatCircleRenderer(0f, 1f, 32); - - public Vector3 position; - public List roads = new List(); - public GameObject gameObject; - - public Node(Vector3 position, Transform parent, Config config) { - gameObject = new GameObject(); - gameObject.transform.position = position; - gameObject.transform.parent = parent; - gameObject.AddComponent().radius = 0.5f; - gameObject.layer = 7; - - GameObject nodeCircle = new GameObject(); - nodeCircle.AddComponent().material = config.roadEditMaterial; - nodeCircle.AddComponent().mesh = circle.mesh; - nodeCircle.transform.parent = gameObject.transform; - nodeCircle.transform.localPosition = Vector3.zero; - - GameObject nodeRoad = new GameObject(); - nodeRoad.AddComponent().material = config.roadMaterial; - nodeRoad.AddComponent().mesh = fullCircle.mesh; - nodeRoad.transform.parent = gameObject.transform; - nodeRoad.transform.localPosition = Vector3.zero; - 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(); - } - - override public bool Equals(object other) { - if ((other == null) || ! this.GetType().Equals(other.GetType())) { - return false; - } - if (other == this) { - return true; - } - return ((Node)other).position.Equals(position); - } - - public void pull(Vector3 position) { - this.position = position; - gameObject.transform.position = position; - foreach (Road road in roads) { - 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/Node.cs.meta b/Assets/Scripts/Node.cs.meta deleted file mode 100644 index 8bbedf1..0000000 --- a/Assets/Scripts/Node.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 3cbed1dad7ec67d4dbe8602aea1e2979 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Primitives.meta b/Assets/Scripts/Primitives.meta new file mode 100644 index 0000000..0cf8895 --- /dev/null +++ b/Assets/Scripts/Primitives.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 6d1a8c7f33189a240b4d749356f53399 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/Bezier.cs b/Assets/Scripts/Primitives/Bezier.cs new file mode 100644 index 0000000..ea19006 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs @@ -0,0 +1,39 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Bezier { + public Vector3 A, B, C, D; + + public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { + this.A = A; + this.B = B; + this.C = C; + this.D = D; + } + + public Bezier() { + this.A = Vector3.zero; + this.B = Vector3.zero; + this.C = Vector3.zero; + this.D = Vector3.zero; + } + + public Vector3 getPosition(float t) { + float T = 1.0f - t; + return + A * ( T*T*T) + + B * (3*T*T*t) + + C * (3*T*t*t) + + D * ( t*t*t); + } + + public Vector3 getDirection(float t) { + float T = 1.0f - t; + return + A * (-3*T*T ) + + B * ( 3*T*T - 6*T*t) + + C * (-3*t*t + 6*T*t) + + D * ( 3*t*t ); + } +} diff --git a/Assets/Scripts/Primitives/Bezier.cs.meta b/Assets/Scripts/Primitives/Bezier.cs.meta new file mode 100644 index 0000000..14630e2 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 284cf9d52e385aa4ebba97eb8b43b8a6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs b/Assets/Scripts/Primitives/FlatBezierRenderer.cs new file mode 100644 index 0000000..b8dad15 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs @@ -0,0 +1,51 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FlatBezierRenderer { + private int percision; + private float width; + public Bezier bezier; + public Mesh mesh = new Mesh(); + public FlatBezierRenderer(Bezier bezier, int percision, float width) { + this.percision = percision; + this.width = width; + this.bezier = bezier; + update(); + } + + public void update() { + float realWidth = width / 2; + int vertexCount = 6 * (percision+1); + Vector3[] vertices = new Vector3[vertexCount / 3]; + int[] indices = new int[vertexCount]; + Vector3 position = bezier.getPosition(0.0f); + Vector3 tangent = getTangent(0.0f); + vertices[0] = position + tangent * realWidth; + vertices[1] = position - tangent * realWidth; + for (int i = 1; i <= percision; i++) { + float t = (float) i / percision; + indices[6*i ] = 2*i ; + indices[6*i+1] = 2*i-1; + indices[6*i+2] = 2*i-2; + indices[6*i+3] = 2*i+1; + indices[6*i+4] = 2*i-1; + indices[6*i+5] = 2*i ; + + position = bezier.getPosition(t); + tangent = getTangent(t); + vertices[2*i ] = position + tangent * realWidth; + vertices[2*i+1] = position - tangent * realWidth; + } + mesh.Clear(); + mesh.vertices = vertices; + mesh.triangles = indices; + mesh.Optimize(); + } + + private Vector3 getTangent(float t) { + Vector3 direction = bezier.getDirection(t); + direction.Normalize(); + return new Vector3(-direction.z, 0, direction.x); + } +} diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta b/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta new file mode 100644 index 0000000..1fe6547 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9bfb303c44d15ef468dc0d7334241e94 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/FlatCircleRenderer.cs b/Assets/Scripts/Primitives/FlatCircleRenderer.cs new file mode 100644 index 0000000..363b663 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatCircleRenderer.cs @@ -0,0 +1,42 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FlatCircleRenderer { + public Mesh mesh = new Mesh(); + public float radius, width; + private int segments; + + public FlatCircleRenderer(float radius, float width, int segments) { + this.radius = radius; + this.width = width; + this.segments = segments; + update(); + } + + public void update() { + Vector3[] vertices = new Vector3[2*segments+6]; + int[] indices = new int[6*segments]; + for (int i = 0; i < segments; i++) { + float alpha = (float) i / segments * 2 * Mathf.PI; + indices[6*i ] = 2*i ; + indices[6*i+3] = 2*i ; + indices[6*i+5] = 2*i+1; + if (i == 0) { + indices[6*i+1] = 2*segments-2; + indices[6*i+2] = 2*segments-1; + indices[6*i+4] = 2*segments-1; + } else { + indices[6*i+1] = 2*i-2; + indices[6*i+2] = 2*i-1; + indices[6*i+4] = 2*i-1; + } + Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); + vertices[2*i ] = direction * radius; + vertices[2*i+1] = direction * (radius + width); + } + mesh.Clear(); + mesh.vertices = vertices; + mesh.triangles = indices; + } +} diff --git a/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta b/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta new file mode 100644 index 0000000..d10c238 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0fe81c7924a85b142a4a86201b08c241 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Road.cs b/Assets/Scripts/Road.cs deleted file mode 100644 index 0af2218..0000000 --- a/Assets/Scripts/Road.cs +++ /dev/null @@ -1,85 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Road { - public List nodes = new List(); - Bezier path; - FlatBezierRenderer pathLine, roadBody; - Config config; - MeshCollider collider; - - public Road(Node start, Node end, Config config) { - nodes.Add(start); - nodes.Add(end); - path = new Bezier(); - pathLine = new FlatBezierRenderer(path, 50, 0.05f); - roadBody = new FlatBezierRenderer(path, 50, 2f); - this.config = config; - } - - - public void initialize(Transform parent) { - GameObject child = new GameObject(); - child.transform.parent = parent; - GameObject lineChild = new GameObject(); - lineChild.transform.position = Vector3.zero; - lineChild.AddComponent().material = config.roadEditMaterial; - lineChild.AddComponent().mesh = pathLine.mesh; - lineChild.transform.parent = child.transform; - - GameObject roadChild = new GameObject(); - collider = roadChild.AddComponent(); - roadChild.transform.position = Vector3.zero; - roadChild.AddComponent().material = config.roadMaterial; - roadChild.transform.parent = child.transform; - roadChild.layer = 8; - roadChild.AddComponent().mesh = roadBody.mesh; - roadChild.AddComponent().road = this; - } - - public void update(bool updateOthers) { - path.A = nodes[0].position; - path.D = nodes[1].position; - if (nodes[0].roads.Count == 2) { - path.B = nodes[0].position + - 0.25f * (nodes[1].position - nodes[0].getOther(nodes[1]).position); - } else { - path.B = 0.5f * (nodes[0].position + nodes[1].position); - } - if (nodes[1].roads.Count == 2) { - path.C = nodes[1].position + - 0.25f * (nodes[0].position - nodes[1].getOther(nodes[0]).position); - } else { - path.C = 0.5f * (nodes[0].position + nodes[1].position); - } - if (updateOthers) { - foreach (Node node in nodes) { - node.lateUpdate(this); - } - } - pathLine.update(); - roadBody.update(); - collider.sharedMesh = roadBody.mesh; - } - - override public bool Equals(object other) { - if (other == null || !(other is Road)) { - return false; - } - if (other == this) { - return true; - } - Road otherRoad = (Road) other; - foreach (Node node in nodes) { - if (! otherRoad.nodes.Contains(node)) { - return false; - } - } - return true; - } - - public override int GetHashCode() { - return nodes[0].GetHashCode() << 16 | nodes[1].GetHashCode(); - } -} diff --git a/Assets/Scripts/Road.cs.meta b/Assets/Scripts/Road.cs.meta deleted file mode 100644 index 24dc94b..0000000 --- a/Assets/Scripts/Road.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0ab512a631c0cc4468e77b6a2dfeb31f -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/RoadData.cs b/Assets/Scripts/RoadData.cs deleted file mode 100644 index eb620ba..0000000 --- a/Assets/Scripts/RoadData.cs +++ /dev/null @@ -1,7 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class RoadData : MonoBehaviour { - public Road road; -} diff --git a/Assets/Scripts/RoadData.cs.meta b/Assets/Scripts/RoadData.cs.meta deleted file mode 100644 index 2948b97..0000000 --- a/Assets/Scripts/RoadData.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 6bfac654eff68754c88b877a9b88ae42 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/RoadEdit.cs b/Assets/Scripts/RoadEdit.cs deleted file mode 100644 index a6cc73e..0000000 --- a/Assets/Scripts/RoadEdit.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class RoadEdit : MonoBehaviour { - public GameObject start; - public GameObject startDirection; - public GameObject end; - public GameObject endDirection; - public Camera mainCamera; - - private GameObject currentlyPulling = null; - public Bezier bezier; - private FlatBezierRenderer pathRenderer; - public Material highlight, noHighlight; - - void Start() { - } - - void updateRoad(Vector3 position) { - } - - void Update() { - Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); - if (Input.GetAxis("Fire1") != 0.0f) { - if (currentlyPulling == null && Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, 1 << 7)) { - currentlyPulling = hit.transform.gameObject; - currentlyPulling.GetComponent().material = highlight; - } - } else { - if (currentlyPulling != null) { - currentlyPulling.GetComponent().material = noHighlight; - } - currentlyPulling = null; - } - if (currentlyPulling != null) { - Physics.Raycast(ray, out RaycastHit hit_, Mathf.Infinity, 1 << 6); - Vector3 position = new Vector3(hit_.point.x, 0.0f, hit_.point.z); - currentlyPulling.transform.position = position; - updateRoad(position); - } - } -} diff --git a/Assets/Scripts/RoadEdit.cs.meta b/Assets/Scripts/RoadEdit.cs.meta deleted file mode 100644 index 4e09d01..0000000 --- a/Assets/Scripts/RoadEdit.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 68e9412fa432f2e468d5d7447e99fd89 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Roads.cs b/Assets/Scripts/Roads.cs deleted file mode 100644 index acd376a..0000000 --- a/Assets/Scripts/Roads.cs +++ /dev/null @@ -1,102 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Roads : MonoBehaviour { - public List nodes = new List(); - public List roads = new List(); - public FlatBezierRenderer roadRenderer; - public Camera mainCamera; - private bool drawing = false; - public Material material; - public Config config; - - void Start() { - } - - private Node startNode; - - private void startRoad() { - Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out RaycastHit hit, Mathf.Infinity, 1 << 6); - foreach (Node node in nodes) { - if ((hit.point - node.position).magnitude < 1.0f) { - startNode = node; - drawing = true; - return; - } - } - Vector3 position = new Vector3(hit.point.x, 0.0f, hit.point.z); - startNode = new Node(position, transform, config); - nodes.Add(startNode); - drawing = true; - } - - private void endRoad() { - Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out RaycastHit hit, Mathf.Infinity, 1 << 6); - Node endNode = null; - foreach (Node node in nodes) { - if ((hit.point - node.position).magnitude < 1.0f) { - endNode = node; - break; - } - } - if (endNode == null) { - Vector3 position = new Vector3(hit.point.x, 0.0f, hit.point.z); - endNode = new Node(position, transform, config); - nodes.Add(endNode); - } - Road road = new Road(startNode, endNode, config); - if (!roads.Contains(road) && startNode != endNode) { - startNode.roads.Add(road); - endNode.roads.Add(road); - roads.Add(road); - road.initialize(transform); - road.update(true); - } - startNode = null; - drawing = false; - } - - Node pulling = null; - - void Update() { - Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); - RaycastHit hit; - if (config.mode == Mode.DrawRoad) { - if (Input.GetAxis("Fire1") != 0.0f) { - if (!drawing) { - startRoad(); - } - } else if (drawing) { - endRoad(); - } - } else if (config.mode == Mode.DeleteRoad) { - if (!drawing && Input.GetAxis("Fire1") != 0.0f) { - if (Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 8)) { - drawing = true; - Road road = hit.transform.gameObject.GetComponent().road; - roads.Remove(road); - foreach (Node node in road.nodes) { - node.roads.Remove(road); - node.update(); - } - Destroy(hit.transform.parent.gameObject); - } - } else if (drawing && Input.GetAxis("Fire1") == 0.0f) { - drawing = false; - } - } - if (Input.GetAxis("Fire2") != 0.0f) { - if (pulling == null && Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 7)) { - pulling = nodes.Find(node => node.gameObject == hit.transform.gameObject); - } - } else { - pulling = null; - } - if (pulling != null) { - Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 6); - Vector3 position = new Vector3(hit.point.x, 0.0f, hit.point.z); - pulling.pull(position); - } - } -} diff --git a/Assets/Scripts/Roads.cs.meta b/Assets/Scripts/Roads.cs.meta deleted file mode 100644 index f26a401..0000000 --- a/Assets/Scripts/Roads.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 7635c530aa8ef31498f100f4e7465bea -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Roads.meta b/Assets/Scripts/Roads.meta new file mode 100644 index 0000000..defbd46 --- /dev/null +++ b/Assets/Scripts/Roads.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: e6a3f10ad4786e44d9faf825f11a8e1d +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Roads/Node.cs b/Assets/Scripts/Roads/Node.cs new file mode 100644 index 0000000..3b2f31a --- /dev/null +++ b/Assets/Scripts/Roads/Node.cs @@ -0,0 +1,82 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Node { + private FlatCircleRenderer circle = new FlatCircleRenderer(0.2f, 0.05f, 32); + private FlatCircleRenderer fullCircle = new FlatCircleRenderer(0f, 1f, 32); + + public Vector3 position; + public List roads = new List(); + public GameObject gameObject; + + public Node(Vector3 position, Transform parent, Config config) { + gameObject = new GameObject(); + gameObject.transform.position = position; + gameObject.transform.parent = parent; + gameObject.AddComponent().radius = 2f; + gameObject.layer = 7; + gameObject.AddComponent().node = this; + + GameObject nodeCircle = new GameObject(); + nodeCircle.AddComponent().material = config.roadEditMaterial; + nodeCircle.AddComponent().mesh = circle.mesh; + nodeCircle.transform.parent = gameObject.transform; + nodeCircle.transform.localPosition = Vector3.zero; + + GameObject nodeRoad = new GameObject(); + nodeRoad.AddComponent().material = config.roadMaterial; + nodeRoad.AddComponent().mesh = fullCircle.mesh; + nodeRoad.transform.parent = gameObject.transform; + nodeRoad.transform.localPosition = Vector3.zero; + 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(); + } + + override public bool Equals(object other) { + if ((other == null) || ! this.GetType().Equals(other.GetType())) { + return false; + } + if (other == this) { + return true; + } + return ((Node)other).position.Equals(position); + } + + public void pull(Vector3 position) { + this.position = position; + gameObject.transform.position = position; + foreach (Road road in roads) { + 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/Bezier.cs b/Assets/Scripts/Bezier.cs deleted file mode 100644 index ea19006..0000000 --- a/Assets/Scripts/Bezier.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Bezier { - public Vector3 A, B, C, D; - - public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { - this.A = A; - this.B = B; - this.C = C; - this.D = D; - } - - public Bezier() { - this.A = Vector3.zero; - this.B = Vector3.zero; - this.C = Vector3.zero; - this.D = Vector3.zero; - } - - public Vector3 getPosition(float t) { - float T = 1.0f - t; - return - A * ( T*T*T) + - B * (3*T*T*t) + - C * (3*T*t*t) + - D * ( t*t*t); - } - - public Vector3 getDirection(float t) { - float T = 1.0f - t; - return - A * (-3*T*T ) + - B * ( 3*T*T - 6*T*t) + - C * (-3*t*t + 6*T*t) + - D * ( 3*t*t ); - } -} diff --git a/Assets/Scripts/Bezier.cs.meta b/Assets/Scripts/Bezier.cs.meta deleted file mode 100644 index 14630e2..0000000 --- a/Assets/Scripts/Bezier.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 284cf9d52e385aa4ebba97eb8b43b8a6 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatBezierRenderer.cs b/Assets/Scripts/FlatBezierRenderer.cs deleted file mode 100644 index b8dad15..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatBezierRenderer { - private int percision; - private float width; - public Bezier bezier; - public Mesh mesh = new Mesh(); - public FlatBezierRenderer(Bezier bezier, int percision, float width) { - this.percision = percision; - this.width = width; - this.bezier = bezier; - update(); - } - - public void update() { - float realWidth = width / 2; - int vertexCount = 6 * (percision+1); - Vector3[] vertices = new Vector3[vertexCount / 3]; - int[] indices = new int[vertexCount]; - Vector3 position = bezier.getPosition(0.0f); - Vector3 tangent = getTangent(0.0f); - vertices[0] = position + tangent * realWidth; - vertices[1] = position - tangent * realWidth; - for (int i = 1; i <= percision; i++) { - float t = (float) i / percision; - indices[6*i ] = 2*i ; - indices[6*i+1] = 2*i-1; - indices[6*i+2] = 2*i-2; - indices[6*i+3] = 2*i+1; - indices[6*i+4] = 2*i-1; - indices[6*i+5] = 2*i ; - - position = bezier.getPosition(t); - tangent = getTangent(t); - vertices[2*i ] = position + tangent * realWidth; - vertices[2*i+1] = position - tangent * realWidth; - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - mesh.Optimize(); - } - - private Vector3 getTangent(float t) { - Vector3 direction = bezier.getDirection(t); - direction.Normalize(); - return new Vector3(-direction.z, 0, direction.x); - } -} diff --git a/Assets/Scripts/FlatBezierRenderer.cs.meta b/Assets/Scripts/FlatBezierRenderer.cs.meta deleted file mode 100644 index 1fe6547..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 9bfb303c44d15ef468dc0d7334241e94 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatCircleRenderer.cs b/Assets/Scripts/FlatCircleRenderer.cs deleted file mode 100644 index 363b663..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatCircleRenderer { - public Mesh mesh = new Mesh(); - public float radius, width; - private int segments; - - public FlatCircleRenderer(float radius, float width, int segments) { - this.radius = radius; - this.width = width; - this.segments = segments; - update(); - } - - public void update() { - Vector3[] vertices = new Vector3[2*segments+6]; - int[] indices = new int[6*segments]; - for (int i = 0; i < segments; i++) { - float alpha = (float) i / segments * 2 * Mathf.PI; - indices[6*i ] = 2*i ; - indices[6*i+3] = 2*i ; - indices[6*i+5] = 2*i+1; - if (i == 0) { - indices[6*i+1] = 2*segments-2; - indices[6*i+2] = 2*segments-1; - indices[6*i+4] = 2*segments-1; - } else { - indices[6*i+1] = 2*i-2; - indices[6*i+2] = 2*i-1; - indices[6*i+4] = 2*i-1; - } - Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); - vertices[2*i ] = direction * radius; - vertices[2*i+1] = direction * (radius + width); - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - } -} diff --git a/Assets/Scripts/FlatCircleRenderer.cs.meta b/Assets/Scripts/FlatCircleRenderer.cs.meta deleted file mode 100644 index d10c238..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0fe81c7924a85b142a4a86201b08c241 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Node.cs b/Assets/Scripts/Node.cs deleted file mode 100644 index e3c4cbd..0000000 --- a/Assets/Scripts/Node.cs +++ /dev/null @@ -1,81 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Node { - public FlatCircleRenderer circle = new FlatCircleRenderer(0.2f, 0.05f, 32); - public FlatCircleRenderer fullCircle = new FlatCircleRenderer(0f, 1f, 32); - - public Vector3 position; - public List roads = new List(); - public GameObject gameObject; - - public Node(Vector3 position, Transform parent, Config config) { - gameObject = new GameObject(); - gameObject.transform.position = position; - gameObject.transform.parent = parent; - gameObject.AddComponent().radius = 0.5f; - gameObject.layer = 7; - - GameObject nodeCircle = new GameObject(); - nodeCircle.AddComponent().material = config.roadEditMaterial; - nodeCircle.AddComponent().mesh = circle.mesh; - nodeCircle.transform.parent = gameObject.transform; - nodeCircle.transform.localPosition = Vector3.zero; - - GameObject nodeRoad = new GameObject(); - nodeRoad.AddComponent().material = config.roadMaterial; - nodeRoad.AddComponent().mesh = fullCircle.mesh; - nodeRoad.transform.parent = gameObject.transform; - nodeRoad.transform.localPosition = Vector3.zero; - 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(); - } - - override public bool Equals(object other) { - if ((other == null) || ! this.GetType().Equals(other.GetType())) { - return false; - } - if (other == this) { - return true; - } - return ((Node)other).position.Equals(position); - } - - public void pull(Vector3 position) { - this.position = position; - gameObject.transform.position = position; - foreach (Road road in roads) { - 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/Node.cs.meta b/Assets/Scripts/Node.cs.meta deleted file mode 100644 index 8bbedf1..0000000 --- a/Assets/Scripts/Node.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 3cbed1dad7ec67d4dbe8602aea1e2979 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Primitives.meta b/Assets/Scripts/Primitives.meta new file mode 100644 index 0000000..0cf8895 --- /dev/null +++ b/Assets/Scripts/Primitives.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 6d1a8c7f33189a240b4d749356f53399 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/Bezier.cs b/Assets/Scripts/Primitives/Bezier.cs new file mode 100644 index 0000000..ea19006 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs @@ -0,0 +1,39 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Bezier { + public Vector3 A, B, C, D; + + public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { + this.A = A; + this.B = B; + this.C = C; + this.D = D; + } + + public Bezier() { + this.A = Vector3.zero; + this.B = Vector3.zero; + this.C = Vector3.zero; + this.D = Vector3.zero; + } + + public Vector3 getPosition(float t) { + float T = 1.0f - t; + return + A * ( T*T*T) + + B * (3*T*T*t) + + C * (3*T*t*t) + + D * ( t*t*t); + } + + public Vector3 getDirection(float t) { + float T = 1.0f - t; + return + A * (-3*T*T ) + + B * ( 3*T*T - 6*T*t) + + C * (-3*t*t + 6*T*t) + + D * ( 3*t*t ); + } +} diff --git a/Assets/Scripts/Primitives/Bezier.cs.meta b/Assets/Scripts/Primitives/Bezier.cs.meta new file mode 100644 index 0000000..14630e2 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 284cf9d52e385aa4ebba97eb8b43b8a6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs b/Assets/Scripts/Primitives/FlatBezierRenderer.cs new file mode 100644 index 0000000..b8dad15 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs @@ -0,0 +1,51 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FlatBezierRenderer { + private int percision; + private float width; + public Bezier bezier; + public Mesh mesh = new Mesh(); + public FlatBezierRenderer(Bezier bezier, int percision, float width) { + this.percision = percision; + this.width = width; + this.bezier = bezier; + update(); + } + + public void update() { + float realWidth = width / 2; + int vertexCount = 6 * (percision+1); + Vector3[] vertices = new Vector3[vertexCount / 3]; + int[] indices = new int[vertexCount]; + Vector3 position = bezier.getPosition(0.0f); + Vector3 tangent = getTangent(0.0f); + vertices[0] = position + tangent * realWidth; + vertices[1] = position - tangent * realWidth; + for (int i = 1; i <= percision; i++) { + float t = (float) i / percision; + indices[6*i ] = 2*i ; + indices[6*i+1] = 2*i-1; + indices[6*i+2] = 2*i-2; + indices[6*i+3] = 2*i+1; + indices[6*i+4] = 2*i-1; + indices[6*i+5] = 2*i ; + + position = bezier.getPosition(t); + tangent = getTangent(t); + vertices[2*i ] = position + tangent * realWidth; + vertices[2*i+1] = position - tangent * realWidth; + } + mesh.Clear(); + mesh.vertices = vertices; + mesh.triangles = indices; + mesh.Optimize(); + } + + private Vector3 getTangent(float t) { + Vector3 direction = bezier.getDirection(t); + direction.Normalize(); + return new Vector3(-direction.z, 0, direction.x); + } +} diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta b/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta new file mode 100644 index 0000000..1fe6547 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9bfb303c44d15ef468dc0d7334241e94 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/FlatCircleRenderer.cs b/Assets/Scripts/Primitives/FlatCircleRenderer.cs new file mode 100644 index 0000000..363b663 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatCircleRenderer.cs @@ -0,0 +1,42 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FlatCircleRenderer { + public Mesh mesh = new Mesh(); + public float radius, width; + private int segments; + + public FlatCircleRenderer(float radius, float width, int segments) { + this.radius = radius; + this.width = width; + this.segments = segments; + update(); + } + + public void update() { + Vector3[] vertices = new Vector3[2*segments+6]; + int[] indices = new int[6*segments]; + for (int i = 0; i < segments; i++) { + float alpha = (float) i / segments * 2 * Mathf.PI; + indices[6*i ] = 2*i ; + indices[6*i+3] = 2*i ; + indices[6*i+5] = 2*i+1; + if (i == 0) { + indices[6*i+1] = 2*segments-2; + indices[6*i+2] = 2*segments-1; + indices[6*i+4] = 2*segments-1; + } else { + indices[6*i+1] = 2*i-2; + indices[6*i+2] = 2*i-1; + indices[6*i+4] = 2*i-1; + } + Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); + vertices[2*i ] = direction * radius; + vertices[2*i+1] = direction * (radius + width); + } + mesh.Clear(); + mesh.vertices = vertices; + mesh.triangles = indices; + } +} diff --git a/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta b/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta new file mode 100644 index 0000000..d10c238 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0fe81c7924a85b142a4a86201b08c241 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Road.cs b/Assets/Scripts/Road.cs deleted file mode 100644 index 0af2218..0000000 --- a/Assets/Scripts/Road.cs +++ /dev/null @@ -1,85 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Road { - public List nodes = new List(); - Bezier path; - FlatBezierRenderer pathLine, roadBody; - Config config; - MeshCollider collider; - - public Road(Node start, Node end, Config config) { - nodes.Add(start); - nodes.Add(end); - path = new Bezier(); - pathLine = new FlatBezierRenderer(path, 50, 0.05f); - roadBody = new FlatBezierRenderer(path, 50, 2f); - this.config = config; - } - - - public void initialize(Transform parent) { - GameObject child = new GameObject(); - child.transform.parent = parent; - GameObject lineChild = new GameObject(); - lineChild.transform.position = Vector3.zero; - lineChild.AddComponent().material = config.roadEditMaterial; - lineChild.AddComponent().mesh = pathLine.mesh; - lineChild.transform.parent = child.transform; - - GameObject roadChild = new GameObject(); - collider = roadChild.AddComponent(); - roadChild.transform.position = Vector3.zero; - roadChild.AddComponent().material = config.roadMaterial; - roadChild.transform.parent = child.transform; - roadChild.layer = 8; - roadChild.AddComponent().mesh = roadBody.mesh; - roadChild.AddComponent().road = this; - } - - public void update(bool updateOthers) { - path.A = nodes[0].position; - path.D = nodes[1].position; - if (nodes[0].roads.Count == 2) { - path.B = nodes[0].position + - 0.25f * (nodes[1].position - nodes[0].getOther(nodes[1]).position); - } else { - path.B = 0.5f * (nodes[0].position + nodes[1].position); - } - if (nodes[1].roads.Count == 2) { - path.C = nodes[1].position + - 0.25f * (nodes[0].position - nodes[1].getOther(nodes[0]).position); - } else { - path.C = 0.5f * (nodes[0].position + nodes[1].position); - } - if (updateOthers) { - foreach (Node node in nodes) { - node.lateUpdate(this); - } - } - pathLine.update(); - roadBody.update(); - collider.sharedMesh = roadBody.mesh; - } - - override public bool Equals(object other) { - if (other == null || !(other is Road)) { - return false; - } - if (other == this) { - return true; - } - Road otherRoad = (Road) other; - foreach (Node node in nodes) { - if (! otherRoad.nodes.Contains(node)) { - return false; - } - } - return true; - } - - public override int GetHashCode() { - return nodes[0].GetHashCode() << 16 | nodes[1].GetHashCode(); - } -} diff --git a/Assets/Scripts/Road.cs.meta b/Assets/Scripts/Road.cs.meta deleted file mode 100644 index 24dc94b..0000000 --- a/Assets/Scripts/Road.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0ab512a631c0cc4468e77b6a2dfeb31f -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/RoadData.cs b/Assets/Scripts/RoadData.cs deleted file mode 100644 index eb620ba..0000000 --- a/Assets/Scripts/RoadData.cs +++ /dev/null @@ -1,7 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class RoadData : MonoBehaviour { - public Road road; -} diff --git a/Assets/Scripts/RoadData.cs.meta b/Assets/Scripts/RoadData.cs.meta deleted file mode 100644 index 2948b97..0000000 --- a/Assets/Scripts/RoadData.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 6bfac654eff68754c88b877a9b88ae42 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/RoadEdit.cs b/Assets/Scripts/RoadEdit.cs deleted file mode 100644 index a6cc73e..0000000 --- a/Assets/Scripts/RoadEdit.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class RoadEdit : MonoBehaviour { - public GameObject start; - public GameObject startDirection; - public GameObject end; - public GameObject endDirection; - public Camera mainCamera; - - private GameObject currentlyPulling = null; - public Bezier bezier; - private FlatBezierRenderer pathRenderer; - public Material highlight, noHighlight; - - void Start() { - } - - void updateRoad(Vector3 position) { - } - - void Update() { - Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); - if (Input.GetAxis("Fire1") != 0.0f) { - if (currentlyPulling == null && Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, 1 << 7)) { - currentlyPulling = hit.transform.gameObject; - currentlyPulling.GetComponent().material = highlight; - } - } else { - if (currentlyPulling != null) { - currentlyPulling.GetComponent().material = noHighlight; - } - currentlyPulling = null; - } - if (currentlyPulling != null) { - Physics.Raycast(ray, out RaycastHit hit_, Mathf.Infinity, 1 << 6); - Vector3 position = new Vector3(hit_.point.x, 0.0f, hit_.point.z); - currentlyPulling.transform.position = position; - updateRoad(position); - } - } -} diff --git a/Assets/Scripts/RoadEdit.cs.meta b/Assets/Scripts/RoadEdit.cs.meta deleted file mode 100644 index 4e09d01..0000000 --- a/Assets/Scripts/RoadEdit.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 68e9412fa432f2e468d5d7447e99fd89 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Roads.cs b/Assets/Scripts/Roads.cs deleted file mode 100644 index acd376a..0000000 --- a/Assets/Scripts/Roads.cs +++ /dev/null @@ -1,102 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Roads : MonoBehaviour { - public List nodes = new List(); - public List roads = new List(); - public FlatBezierRenderer roadRenderer; - public Camera mainCamera; - private bool drawing = false; - public Material material; - public Config config; - - void Start() { - } - - private Node startNode; - - private void startRoad() { - Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out RaycastHit hit, Mathf.Infinity, 1 << 6); - foreach (Node node in nodes) { - if ((hit.point - node.position).magnitude < 1.0f) { - startNode = node; - drawing = true; - return; - } - } - Vector3 position = new Vector3(hit.point.x, 0.0f, hit.point.z); - startNode = new Node(position, transform, config); - nodes.Add(startNode); - drawing = true; - } - - private void endRoad() { - Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out RaycastHit hit, Mathf.Infinity, 1 << 6); - Node endNode = null; - foreach (Node node in nodes) { - if ((hit.point - node.position).magnitude < 1.0f) { - endNode = node; - break; - } - } - if (endNode == null) { - Vector3 position = new Vector3(hit.point.x, 0.0f, hit.point.z); - endNode = new Node(position, transform, config); - nodes.Add(endNode); - } - Road road = new Road(startNode, endNode, config); - if (!roads.Contains(road) && startNode != endNode) { - startNode.roads.Add(road); - endNode.roads.Add(road); - roads.Add(road); - road.initialize(transform); - road.update(true); - } - startNode = null; - drawing = false; - } - - Node pulling = null; - - void Update() { - Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); - RaycastHit hit; - if (config.mode == Mode.DrawRoad) { - if (Input.GetAxis("Fire1") != 0.0f) { - if (!drawing) { - startRoad(); - } - } else if (drawing) { - endRoad(); - } - } else if (config.mode == Mode.DeleteRoad) { - if (!drawing && Input.GetAxis("Fire1") != 0.0f) { - if (Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 8)) { - drawing = true; - Road road = hit.transform.gameObject.GetComponent().road; - roads.Remove(road); - foreach (Node node in road.nodes) { - node.roads.Remove(road); - node.update(); - } - Destroy(hit.transform.parent.gameObject); - } - } else if (drawing && Input.GetAxis("Fire1") == 0.0f) { - drawing = false; - } - } - if (Input.GetAxis("Fire2") != 0.0f) { - if (pulling == null && Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 7)) { - pulling = nodes.Find(node => node.gameObject == hit.transform.gameObject); - } - } else { - pulling = null; - } - if (pulling != null) { - Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 6); - Vector3 position = new Vector3(hit.point.x, 0.0f, hit.point.z); - pulling.pull(position); - } - } -} diff --git a/Assets/Scripts/Roads.cs.meta b/Assets/Scripts/Roads.cs.meta deleted file mode 100644 index f26a401..0000000 --- a/Assets/Scripts/Roads.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 7635c530aa8ef31498f100f4e7465bea -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Roads.meta b/Assets/Scripts/Roads.meta new file mode 100644 index 0000000..defbd46 --- /dev/null +++ b/Assets/Scripts/Roads.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: e6a3f10ad4786e44d9faf825f11a8e1d +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Roads/Node.cs b/Assets/Scripts/Roads/Node.cs new file mode 100644 index 0000000..3b2f31a --- /dev/null +++ b/Assets/Scripts/Roads/Node.cs @@ -0,0 +1,82 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Node { + private FlatCircleRenderer circle = new FlatCircleRenderer(0.2f, 0.05f, 32); + private FlatCircleRenderer fullCircle = new FlatCircleRenderer(0f, 1f, 32); + + public Vector3 position; + public List roads = new List(); + public GameObject gameObject; + + public Node(Vector3 position, Transform parent, Config config) { + gameObject = new GameObject(); + gameObject.transform.position = position; + gameObject.transform.parent = parent; + gameObject.AddComponent().radius = 2f; + gameObject.layer = 7; + gameObject.AddComponent().node = this; + + GameObject nodeCircle = new GameObject(); + nodeCircle.AddComponent().material = config.roadEditMaterial; + nodeCircle.AddComponent().mesh = circle.mesh; + nodeCircle.transform.parent = gameObject.transform; + nodeCircle.transform.localPosition = Vector3.zero; + + GameObject nodeRoad = new GameObject(); + nodeRoad.AddComponent().material = config.roadMaterial; + nodeRoad.AddComponent().mesh = fullCircle.mesh; + nodeRoad.transform.parent = gameObject.transform; + nodeRoad.transform.localPosition = Vector3.zero; + 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(); + } + + override public bool Equals(object other) { + if ((other == null) || ! this.GetType().Equals(other.GetType())) { + return false; + } + if (other == this) { + return true; + } + return ((Node)other).position.Equals(position); + } + + public void pull(Vector3 position) { + this.position = position; + gameObject.transform.position = position; + foreach (Road road in roads) { + 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/Roads/Node.cs.meta b/Assets/Scripts/Roads/Node.cs.meta new file mode 100644 index 0000000..8bbedf1 --- /dev/null +++ b/Assets/Scripts/Roads/Node.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 3cbed1dad7ec67d4dbe8602aea1e2979 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Bezier.cs b/Assets/Scripts/Bezier.cs deleted file mode 100644 index ea19006..0000000 --- a/Assets/Scripts/Bezier.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Bezier { - public Vector3 A, B, C, D; - - public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { - this.A = A; - this.B = B; - this.C = C; - this.D = D; - } - - public Bezier() { - this.A = Vector3.zero; - this.B = Vector3.zero; - this.C = Vector3.zero; - this.D = Vector3.zero; - } - - public Vector3 getPosition(float t) { - float T = 1.0f - t; - return - A * ( T*T*T) + - B * (3*T*T*t) + - C * (3*T*t*t) + - D * ( t*t*t); - } - - public Vector3 getDirection(float t) { - float T = 1.0f - t; - return - A * (-3*T*T ) + - B * ( 3*T*T - 6*T*t) + - C * (-3*t*t + 6*T*t) + - D * ( 3*t*t ); - } -} diff --git a/Assets/Scripts/Bezier.cs.meta b/Assets/Scripts/Bezier.cs.meta deleted file mode 100644 index 14630e2..0000000 --- a/Assets/Scripts/Bezier.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 284cf9d52e385aa4ebba97eb8b43b8a6 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatBezierRenderer.cs b/Assets/Scripts/FlatBezierRenderer.cs deleted file mode 100644 index b8dad15..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatBezierRenderer { - private int percision; - private float width; - public Bezier bezier; - public Mesh mesh = new Mesh(); - public FlatBezierRenderer(Bezier bezier, int percision, float width) { - this.percision = percision; - this.width = width; - this.bezier = bezier; - update(); - } - - public void update() { - float realWidth = width / 2; - int vertexCount = 6 * (percision+1); - Vector3[] vertices = new Vector3[vertexCount / 3]; - int[] indices = new int[vertexCount]; - Vector3 position = bezier.getPosition(0.0f); - Vector3 tangent = getTangent(0.0f); - vertices[0] = position + tangent * realWidth; - vertices[1] = position - tangent * realWidth; - for (int i = 1; i <= percision; i++) { - float t = (float) i / percision; - indices[6*i ] = 2*i ; - indices[6*i+1] = 2*i-1; - indices[6*i+2] = 2*i-2; - indices[6*i+3] = 2*i+1; - indices[6*i+4] = 2*i-1; - indices[6*i+5] = 2*i ; - - position = bezier.getPosition(t); - tangent = getTangent(t); - vertices[2*i ] = position + tangent * realWidth; - vertices[2*i+1] = position - tangent * realWidth; - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - mesh.Optimize(); - } - - private Vector3 getTangent(float t) { - Vector3 direction = bezier.getDirection(t); - direction.Normalize(); - return new Vector3(-direction.z, 0, direction.x); - } -} diff --git a/Assets/Scripts/FlatBezierRenderer.cs.meta b/Assets/Scripts/FlatBezierRenderer.cs.meta deleted file mode 100644 index 1fe6547..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 9bfb303c44d15ef468dc0d7334241e94 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatCircleRenderer.cs b/Assets/Scripts/FlatCircleRenderer.cs deleted file mode 100644 index 363b663..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatCircleRenderer { - public Mesh mesh = new Mesh(); - public float radius, width; - private int segments; - - public FlatCircleRenderer(float radius, float width, int segments) { - this.radius = radius; - this.width = width; - this.segments = segments; - update(); - } - - public void update() { - Vector3[] vertices = new Vector3[2*segments+6]; - int[] indices = new int[6*segments]; - for (int i = 0; i < segments; i++) { - float alpha = (float) i / segments * 2 * Mathf.PI; - indices[6*i ] = 2*i ; - indices[6*i+3] = 2*i ; - indices[6*i+5] = 2*i+1; - if (i == 0) { - indices[6*i+1] = 2*segments-2; - indices[6*i+2] = 2*segments-1; - indices[6*i+4] = 2*segments-1; - } else { - indices[6*i+1] = 2*i-2; - indices[6*i+2] = 2*i-1; - indices[6*i+4] = 2*i-1; - } - Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); - vertices[2*i ] = direction * radius; - vertices[2*i+1] = direction * (radius + width); - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - } -} diff --git a/Assets/Scripts/FlatCircleRenderer.cs.meta b/Assets/Scripts/FlatCircleRenderer.cs.meta deleted file mode 100644 index d10c238..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0fe81c7924a85b142a4a86201b08c241 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Node.cs b/Assets/Scripts/Node.cs deleted file mode 100644 index e3c4cbd..0000000 --- a/Assets/Scripts/Node.cs +++ /dev/null @@ -1,81 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Node { - public FlatCircleRenderer circle = new FlatCircleRenderer(0.2f, 0.05f, 32); - public FlatCircleRenderer fullCircle = new FlatCircleRenderer(0f, 1f, 32); - - public Vector3 position; - public List roads = new List(); - public GameObject gameObject; - - public Node(Vector3 position, Transform parent, Config config) { - gameObject = new GameObject(); - gameObject.transform.position = position; - gameObject.transform.parent = parent; - gameObject.AddComponent().radius = 0.5f; - gameObject.layer = 7; - - GameObject nodeCircle = new GameObject(); - nodeCircle.AddComponent().material = config.roadEditMaterial; - nodeCircle.AddComponent().mesh = circle.mesh; - nodeCircle.transform.parent = gameObject.transform; - nodeCircle.transform.localPosition = Vector3.zero; - - GameObject nodeRoad = new GameObject(); - nodeRoad.AddComponent().material = config.roadMaterial; - nodeRoad.AddComponent().mesh = fullCircle.mesh; - nodeRoad.transform.parent = gameObject.transform; - nodeRoad.transform.localPosition = Vector3.zero; - 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(); - } - - override public bool Equals(object other) { - if ((other == null) || ! this.GetType().Equals(other.GetType())) { - return false; - } - if (other == this) { - return true; - } - return ((Node)other).position.Equals(position); - } - - public void pull(Vector3 position) { - this.position = position; - gameObject.transform.position = position; - foreach (Road road in roads) { - 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/Node.cs.meta b/Assets/Scripts/Node.cs.meta deleted file mode 100644 index 8bbedf1..0000000 --- a/Assets/Scripts/Node.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 3cbed1dad7ec67d4dbe8602aea1e2979 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Primitives.meta b/Assets/Scripts/Primitives.meta new file mode 100644 index 0000000..0cf8895 --- /dev/null +++ b/Assets/Scripts/Primitives.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 6d1a8c7f33189a240b4d749356f53399 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/Bezier.cs b/Assets/Scripts/Primitives/Bezier.cs new file mode 100644 index 0000000..ea19006 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs @@ -0,0 +1,39 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Bezier { + public Vector3 A, B, C, D; + + public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { + this.A = A; + this.B = B; + this.C = C; + this.D = D; + } + + public Bezier() { + this.A = Vector3.zero; + this.B = Vector3.zero; + this.C = Vector3.zero; + this.D = Vector3.zero; + } + + public Vector3 getPosition(float t) { + float T = 1.0f - t; + return + A * ( T*T*T) + + B * (3*T*T*t) + + C * (3*T*t*t) + + D * ( t*t*t); + } + + public Vector3 getDirection(float t) { + float T = 1.0f - t; + return + A * (-3*T*T ) + + B * ( 3*T*T - 6*T*t) + + C * (-3*t*t + 6*T*t) + + D * ( 3*t*t ); + } +} diff --git a/Assets/Scripts/Primitives/Bezier.cs.meta b/Assets/Scripts/Primitives/Bezier.cs.meta new file mode 100644 index 0000000..14630e2 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 284cf9d52e385aa4ebba97eb8b43b8a6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs b/Assets/Scripts/Primitives/FlatBezierRenderer.cs new file mode 100644 index 0000000..b8dad15 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs @@ -0,0 +1,51 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FlatBezierRenderer { + private int percision; + private float width; + public Bezier bezier; + public Mesh mesh = new Mesh(); + public FlatBezierRenderer(Bezier bezier, int percision, float width) { + this.percision = percision; + this.width = width; + this.bezier = bezier; + update(); + } + + public void update() { + float realWidth = width / 2; + int vertexCount = 6 * (percision+1); + Vector3[] vertices = new Vector3[vertexCount / 3]; + int[] indices = new int[vertexCount]; + Vector3 position = bezier.getPosition(0.0f); + Vector3 tangent = getTangent(0.0f); + vertices[0] = position + tangent * realWidth; + vertices[1] = position - tangent * realWidth; + for (int i = 1; i <= percision; i++) { + float t = (float) i / percision; + indices[6*i ] = 2*i ; + indices[6*i+1] = 2*i-1; + indices[6*i+2] = 2*i-2; + indices[6*i+3] = 2*i+1; + indices[6*i+4] = 2*i-1; + indices[6*i+5] = 2*i ; + + position = bezier.getPosition(t); + tangent = getTangent(t); + vertices[2*i ] = position + tangent * realWidth; + vertices[2*i+1] = position - tangent * realWidth; + } + mesh.Clear(); + mesh.vertices = vertices; + mesh.triangles = indices; + mesh.Optimize(); + } + + private Vector3 getTangent(float t) { + Vector3 direction = bezier.getDirection(t); + direction.Normalize(); + return new Vector3(-direction.z, 0, direction.x); + } +} diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta b/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta new file mode 100644 index 0000000..1fe6547 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9bfb303c44d15ef468dc0d7334241e94 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/FlatCircleRenderer.cs b/Assets/Scripts/Primitives/FlatCircleRenderer.cs new file mode 100644 index 0000000..363b663 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatCircleRenderer.cs @@ -0,0 +1,42 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FlatCircleRenderer { + public Mesh mesh = new Mesh(); + public float radius, width; + private int segments; + + public FlatCircleRenderer(float radius, float width, int segments) { + this.radius = radius; + this.width = width; + this.segments = segments; + update(); + } + + public void update() { + Vector3[] vertices = new Vector3[2*segments+6]; + int[] indices = new int[6*segments]; + for (int i = 0; i < segments; i++) { + float alpha = (float) i / segments * 2 * Mathf.PI; + indices[6*i ] = 2*i ; + indices[6*i+3] = 2*i ; + indices[6*i+5] = 2*i+1; + if (i == 0) { + indices[6*i+1] = 2*segments-2; + indices[6*i+2] = 2*segments-1; + indices[6*i+4] = 2*segments-1; + } else { + indices[6*i+1] = 2*i-2; + indices[6*i+2] = 2*i-1; + indices[6*i+4] = 2*i-1; + } + Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); + vertices[2*i ] = direction * radius; + vertices[2*i+1] = direction * (radius + width); + } + mesh.Clear(); + mesh.vertices = vertices; + mesh.triangles = indices; + } +} diff --git a/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta b/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta new file mode 100644 index 0000000..d10c238 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0fe81c7924a85b142a4a86201b08c241 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Road.cs b/Assets/Scripts/Road.cs deleted file mode 100644 index 0af2218..0000000 --- a/Assets/Scripts/Road.cs +++ /dev/null @@ -1,85 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Road { - public List nodes = new List(); - Bezier path; - FlatBezierRenderer pathLine, roadBody; - Config config; - MeshCollider collider; - - public Road(Node start, Node end, Config config) { - nodes.Add(start); - nodes.Add(end); - path = new Bezier(); - pathLine = new FlatBezierRenderer(path, 50, 0.05f); - roadBody = new FlatBezierRenderer(path, 50, 2f); - this.config = config; - } - - - public void initialize(Transform parent) { - GameObject child = new GameObject(); - child.transform.parent = parent; - GameObject lineChild = new GameObject(); - lineChild.transform.position = Vector3.zero; - lineChild.AddComponent().material = config.roadEditMaterial; - lineChild.AddComponent().mesh = pathLine.mesh; - lineChild.transform.parent = child.transform; - - GameObject roadChild = new GameObject(); - collider = roadChild.AddComponent(); - roadChild.transform.position = Vector3.zero; - roadChild.AddComponent().material = config.roadMaterial; - roadChild.transform.parent = child.transform; - roadChild.layer = 8; - roadChild.AddComponent().mesh = roadBody.mesh; - roadChild.AddComponent().road = this; - } - - public void update(bool updateOthers) { - path.A = nodes[0].position; - path.D = nodes[1].position; - if (nodes[0].roads.Count == 2) { - path.B = nodes[0].position + - 0.25f * (nodes[1].position - nodes[0].getOther(nodes[1]).position); - } else { - path.B = 0.5f * (nodes[0].position + nodes[1].position); - } - if (nodes[1].roads.Count == 2) { - path.C = nodes[1].position + - 0.25f * (nodes[0].position - nodes[1].getOther(nodes[0]).position); - } else { - path.C = 0.5f * (nodes[0].position + nodes[1].position); - } - if (updateOthers) { - foreach (Node node in nodes) { - node.lateUpdate(this); - } - } - pathLine.update(); - roadBody.update(); - collider.sharedMesh = roadBody.mesh; - } - - override public bool Equals(object other) { - if (other == null || !(other is Road)) { - return false; - } - if (other == this) { - return true; - } - Road otherRoad = (Road) other; - foreach (Node node in nodes) { - if (! otherRoad.nodes.Contains(node)) { - return false; - } - } - return true; - } - - public override int GetHashCode() { - return nodes[0].GetHashCode() << 16 | nodes[1].GetHashCode(); - } -} diff --git a/Assets/Scripts/Road.cs.meta b/Assets/Scripts/Road.cs.meta deleted file mode 100644 index 24dc94b..0000000 --- a/Assets/Scripts/Road.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0ab512a631c0cc4468e77b6a2dfeb31f -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/RoadData.cs b/Assets/Scripts/RoadData.cs deleted file mode 100644 index eb620ba..0000000 --- a/Assets/Scripts/RoadData.cs +++ /dev/null @@ -1,7 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class RoadData : MonoBehaviour { - public Road road; -} diff --git a/Assets/Scripts/RoadData.cs.meta b/Assets/Scripts/RoadData.cs.meta deleted file mode 100644 index 2948b97..0000000 --- a/Assets/Scripts/RoadData.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 6bfac654eff68754c88b877a9b88ae42 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/RoadEdit.cs b/Assets/Scripts/RoadEdit.cs deleted file mode 100644 index a6cc73e..0000000 --- a/Assets/Scripts/RoadEdit.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class RoadEdit : MonoBehaviour { - public GameObject start; - public GameObject startDirection; - public GameObject end; - public GameObject endDirection; - public Camera mainCamera; - - private GameObject currentlyPulling = null; - public Bezier bezier; - private FlatBezierRenderer pathRenderer; - public Material highlight, noHighlight; - - void Start() { - } - - void updateRoad(Vector3 position) { - } - - void Update() { - Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); - if (Input.GetAxis("Fire1") != 0.0f) { - if (currentlyPulling == null && Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, 1 << 7)) { - currentlyPulling = hit.transform.gameObject; - currentlyPulling.GetComponent().material = highlight; - } - } else { - if (currentlyPulling != null) { - currentlyPulling.GetComponent().material = noHighlight; - } - currentlyPulling = null; - } - if (currentlyPulling != null) { - Physics.Raycast(ray, out RaycastHit hit_, Mathf.Infinity, 1 << 6); - Vector3 position = new Vector3(hit_.point.x, 0.0f, hit_.point.z); - currentlyPulling.transform.position = position; - updateRoad(position); - } - } -} diff --git a/Assets/Scripts/RoadEdit.cs.meta b/Assets/Scripts/RoadEdit.cs.meta deleted file mode 100644 index 4e09d01..0000000 --- a/Assets/Scripts/RoadEdit.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 68e9412fa432f2e468d5d7447e99fd89 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Roads.cs b/Assets/Scripts/Roads.cs deleted file mode 100644 index acd376a..0000000 --- a/Assets/Scripts/Roads.cs +++ /dev/null @@ -1,102 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Roads : MonoBehaviour { - public List nodes = new List(); - public List roads = new List(); - public FlatBezierRenderer roadRenderer; - public Camera mainCamera; - private bool drawing = false; - public Material material; - public Config config; - - void Start() { - } - - private Node startNode; - - private void startRoad() { - Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out RaycastHit hit, Mathf.Infinity, 1 << 6); - foreach (Node node in nodes) { - if ((hit.point - node.position).magnitude < 1.0f) { - startNode = node; - drawing = true; - return; - } - } - Vector3 position = new Vector3(hit.point.x, 0.0f, hit.point.z); - startNode = new Node(position, transform, config); - nodes.Add(startNode); - drawing = true; - } - - private void endRoad() { - Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out RaycastHit hit, Mathf.Infinity, 1 << 6); - Node endNode = null; - foreach (Node node in nodes) { - if ((hit.point - node.position).magnitude < 1.0f) { - endNode = node; - break; - } - } - if (endNode == null) { - Vector3 position = new Vector3(hit.point.x, 0.0f, hit.point.z); - endNode = new Node(position, transform, config); - nodes.Add(endNode); - } - Road road = new Road(startNode, endNode, config); - if (!roads.Contains(road) && startNode != endNode) { - startNode.roads.Add(road); - endNode.roads.Add(road); - roads.Add(road); - road.initialize(transform); - road.update(true); - } - startNode = null; - drawing = false; - } - - Node pulling = null; - - void Update() { - Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); - RaycastHit hit; - if (config.mode == Mode.DrawRoad) { - if (Input.GetAxis("Fire1") != 0.0f) { - if (!drawing) { - startRoad(); - } - } else if (drawing) { - endRoad(); - } - } else if (config.mode == Mode.DeleteRoad) { - if (!drawing && Input.GetAxis("Fire1") != 0.0f) { - if (Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 8)) { - drawing = true; - Road road = hit.transform.gameObject.GetComponent().road; - roads.Remove(road); - foreach (Node node in road.nodes) { - node.roads.Remove(road); - node.update(); - } - Destroy(hit.transform.parent.gameObject); - } - } else if (drawing && Input.GetAxis("Fire1") == 0.0f) { - drawing = false; - } - } - if (Input.GetAxis("Fire2") != 0.0f) { - if (pulling == null && Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 7)) { - pulling = nodes.Find(node => node.gameObject == hit.transform.gameObject); - } - } else { - pulling = null; - } - if (pulling != null) { - Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 6); - Vector3 position = new Vector3(hit.point.x, 0.0f, hit.point.z); - pulling.pull(position); - } - } -} diff --git a/Assets/Scripts/Roads.cs.meta b/Assets/Scripts/Roads.cs.meta deleted file mode 100644 index f26a401..0000000 --- a/Assets/Scripts/Roads.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 7635c530aa8ef31498f100f4e7465bea -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Roads.meta b/Assets/Scripts/Roads.meta new file mode 100644 index 0000000..defbd46 --- /dev/null +++ b/Assets/Scripts/Roads.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: e6a3f10ad4786e44d9faf825f11a8e1d +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Roads/Node.cs b/Assets/Scripts/Roads/Node.cs new file mode 100644 index 0000000..3b2f31a --- /dev/null +++ b/Assets/Scripts/Roads/Node.cs @@ -0,0 +1,82 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Node { + private FlatCircleRenderer circle = new FlatCircleRenderer(0.2f, 0.05f, 32); + private FlatCircleRenderer fullCircle = new FlatCircleRenderer(0f, 1f, 32); + + public Vector3 position; + public List roads = new List(); + public GameObject gameObject; + + public Node(Vector3 position, Transform parent, Config config) { + gameObject = new GameObject(); + gameObject.transform.position = position; + gameObject.transform.parent = parent; + gameObject.AddComponent().radius = 2f; + gameObject.layer = 7; + gameObject.AddComponent().node = this; + + GameObject nodeCircle = new GameObject(); + nodeCircle.AddComponent().material = config.roadEditMaterial; + nodeCircle.AddComponent().mesh = circle.mesh; + nodeCircle.transform.parent = gameObject.transform; + nodeCircle.transform.localPosition = Vector3.zero; + + GameObject nodeRoad = new GameObject(); + nodeRoad.AddComponent().material = config.roadMaterial; + nodeRoad.AddComponent().mesh = fullCircle.mesh; + nodeRoad.transform.parent = gameObject.transform; + nodeRoad.transform.localPosition = Vector3.zero; + 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(); + } + + override public bool Equals(object other) { + if ((other == null) || ! this.GetType().Equals(other.GetType())) { + return false; + } + if (other == this) { + return true; + } + return ((Node)other).position.Equals(position); + } + + public void pull(Vector3 position) { + this.position = position; + gameObject.transform.position = position; + foreach (Road road in roads) { + 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/Roads/Node.cs.meta b/Assets/Scripts/Roads/Node.cs.meta new file mode 100644 index 0000000..8bbedf1 --- /dev/null +++ b/Assets/Scripts/Roads/Node.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 3cbed1dad7ec67d4dbe8602aea1e2979 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Roads/NodeData.cs b/Assets/Scripts/Roads/NodeData.cs new file mode 100644 index 0000000..9ee8ace --- /dev/null +++ b/Assets/Scripts/Roads/NodeData.cs @@ -0,0 +1,7 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class NodeData : MonoBehaviour { + public Node node = null; +} diff --git a/Assets/Scripts/Bezier.cs b/Assets/Scripts/Bezier.cs deleted file mode 100644 index ea19006..0000000 --- a/Assets/Scripts/Bezier.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Bezier { - public Vector3 A, B, C, D; - - public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { - this.A = A; - this.B = B; - this.C = C; - this.D = D; - } - - public Bezier() { - this.A = Vector3.zero; - this.B = Vector3.zero; - this.C = Vector3.zero; - this.D = Vector3.zero; - } - - public Vector3 getPosition(float t) { - float T = 1.0f - t; - return - A * ( T*T*T) + - B * (3*T*T*t) + - C * (3*T*t*t) + - D * ( t*t*t); - } - - public Vector3 getDirection(float t) { - float T = 1.0f - t; - return - A * (-3*T*T ) + - B * ( 3*T*T - 6*T*t) + - C * (-3*t*t + 6*T*t) + - D * ( 3*t*t ); - } -} diff --git a/Assets/Scripts/Bezier.cs.meta b/Assets/Scripts/Bezier.cs.meta deleted file mode 100644 index 14630e2..0000000 --- a/Assets/Scripts/Bezier.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 284cf9d52e385aa4ebba97eb8b43b8a6 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatBezierRenderer.cs b/Assets/Scripts/FlatBezierRenderer.cs deleted file mode 100644 index b8dad15..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatBezierRenderer { - private int percision; - private float width; - public Bezier bezier; - public Mesh mesh = new Mesh(); - public FlatBezierRenderer(Bezier bezier, int percision, float width) { - this.percision = percision; - this.width = width; - this.bezier = bezier; - update(); - } - - public void update() { - float realWidth = width / 2; - int vertexCount = 6 * (percision+1); - Vector3[] vertices = new Vector3[vertexCount / 3]; - int[] indices = new int[vertexCount]; - Vector3 position = bezier.getPosition(0.0f); - Vector3 tangent = getTangent(0.0f); - vertices[0] = position + tangent * realWidth; - vertices[1] = position - tangent * realWidth; - for (int i = 1; i <= percision; i++) { - float t = (float) i / percision; - indices[6*i ] = 2*i ; - indices[6*i+1] = 2*i-1; - indices[6*i+2] = 2*i-2; - indices[6*i+3] = 2*i+1; - indices[6*i+4] = 2*i-1; - indices[6*i+5] = 2*i ; - - position = bezier.getPosition(t); - tangent = getTangent(t); - vertices[2*i ] = position + tangent * realWidth; - vertices[2*i+1] = position - tangent * realWidth; - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - mesh.Optimize(); - } - - private Vector3 getTangent(float t) { - Vector3 direction = bezier.getDirection(t); - direction.Normalize(); - return new Vector3(-direction.z, 0, direction.x); - } -} diff --git a/Assets/Scripts/FlatBezierRenderer.cs.meta b/Assets/Scripts/FlatBezierRenderer.cs.meta deleted file mode 100644 index 1fe6547..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 9bfb303c44d15ef468dc0d7334241e94 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatCircleRenderer.cs b/Assets/Scripts/FlatCircleRenderer.cs deleted file mode 100644 index 363b663..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatCircleRenderer { - public Mesh mesh = new Mesh(); - public float radius, width; - private int segments; - - public FlatCircleRenderer(float radius, float width, int segments) { - this.radius = radius; - this.width = width; - this.segments = segments; - update(); - } - - public void update() { - Vector3[] vertices = new Vector3[2*segments+6]; - int[] indices = new int[6*segments]; - for (int i = 0; i < segments; i++) { - float alpha = (float) i / segments * 2 * Mathf.PI; - indices[6*i ] = 2*i ; - indices[6*i+3] = 2*i ; - indices[6*i+5] = 2*i+1; - if (i == 0) { - indices[6*i+1] = 2*segments-2; - indices[6*i+2] = 2*segments-1; - indices[6*i+4] = 2*segments-1; - } else { - indices[6*i+1] = 2*i-2; - indices[6*i+2] = 2*i-1; - indices[6*i+4] = 2*i-1; - } - Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); - vertices[2*i ] = direction * radius; - vertices[2*i+1] = direction * (radius + width); - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - } -} diff --git a/Assets/Scripts/FlatCircleRenderer.cs.meta b/Assets/Scripts/FlatCircleRenderer.cs.meta deleted file mode 100644 index d10c238..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0fe81c7924a85b142a4a86201b08c241 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Node.cs b/Assets/Scripts/Node.cs deleted file mode 100644 index e3c4cbd..0000000 --- a/Assets/Scripts/Node.cs +++ /dev/null @@ -1,81 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Node { - public FlatCircleRenderer circle = new FlatCircleRenderer(0.2f, 0.05f, 32); - public FlatCircleRenderer fullCircle = new FlatCircleRenderer(0f, 1f, 32); - - public Vector3 position; - public List roads = new List(); - public GameObject gameObject; - - public Node(Vector3 position, Transform parent, Config config) { - gameObject = new GameObject(); - gameObject.transform.position = position; - gameObject.transform.parent = parent; - gameObject.AddComponent().radius = 0.5f; - gameObject.layer = 7; - - GameObject nodeCircle = new GameObject(); - nodeCircle.AddComponent().material = config.roadEditMaterial; - nodeCircle.AddComponent().mesh = circle.mesh; - nodeCircle.transform.parent = gameObject.transform; - nodeCircle.transform.localPosition = Vector3.zero; - - GameObject nodeRoad = new GameObject(); - nodeRoad.AddComponent().material = config.roadMaterial; - nodeRoad.AddComponent().mesh = fullCircle.mesh; - nodeRoad.transform.parent = gameObject.transform; - nodeRoad.transform.localPosition = Vector3.zero; - 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(); - } - - override public bool Equals(object other) { - if ((other == null) || ! this.GetType().Equals(other.GetType())) { - return false; - } - if (other == this) { - return true; - } - return ((Node)other).position.Equals(position); - } - - public void pull(Vector3 position) { - this.position = position; - gameObject.transform.position = position; - foreach (Road road in roads) { - 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/Node.cs.meta b/Assets/Scripts/Node.cs.meta deleted file mode 100644 index 8bbedf1..0000000 --- a/Assets/Scripts/Node.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 3cbed1dad7ec67d4dbe8602aea1e2979 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Primitives.meta b/Assets/Scripts/Primitives.meta new file mode 100644 index 0000000..0cf8895 --- /dev/null +++ b/Assets/Scripts/Primitives.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 6d1a8c7f33189a240b4d749356f53399 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/Bezier.cs b/Assets/Scripts/Primitives/Bezier.cs new file mode 100644 index 0000000..ea19006 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs @@ -0,0 +1,39 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Bezier { + public Vector3 A, B, C, D; + + public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { + this.A = A; + this.B = B; + this.C = C; + this.D = D; + } + + public Bezier() { + this.A = Vector3.zero; + this.B = Vector3.zero; + this.C = Vector3.zero; + this.D = Vector3.zero; + } + + public Vector3 getPosition(float t) { + float T = 1.0f - t; + return + A * ( T*T*T) + + B * (3*T*T*t) + + C * (3*T*t*t) + + D * ( t*t*t); + } + + public Vector3 getDirection(float t) { + float T = 1.0f - t; + return + A * (-3*T*T ) + + B * ( 3*T*T - 6*T*t) + + C * (-3*t*t + 6*T*t) + + D * ( 3*t*t ); + } +} diff --git a/Assets/Scripts/Primitives/Bezier.cs.meta b/Assets/Scripts/Primitives/Bezier.cs.meta new file mode 100644 index 0000000..14630e2 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 284cf9d52e385aa4ebba97eb8b43b8a6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs b/Assets/Scripts/Primitives/FlatBezierRenderer.cs new file mode 100644 index 0000000..b8dad15 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs @@ -0,0 +1,51 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FlatBezierRenderer { + private int percision; + private float width; + public Bezier bezier; + public Mesh mesh = new Mesh(); + public FlatBezierRenderer(Bezier bezier, int percision, float width) { + this.percision = percision; + this.width = width; + this.bezier = bezier; + update(); + } + + public void update() { + float realWidth = width / 2; + int vertexCount = 6 * (percision+1); + Vector3[] vertices = new Vector3[vertexCount / 3]; + int[] indices = new int[vertexCount]; + Vector3 position = bezier.getPosition(0.0f); + Vector3 tangent = getTangent(0.0f); + vertices[0] = position + tangent * realWidth; + vertices[1] = position - tangent * realWidth; + for (int i = 1; i <= percision; i++) { + float t = (float) i / percision; + indices[6*i ] = 2*i ; + indices[6*i+1] = 2*i-1; + indices[6*i+2] = 2*i-2; + indices[6*i+3] = 2*i+1; + indices[6*i+4] = 2*i-1; + indices[6*i+5] = 2*i ; + + position = bezier.getPosition(t); + tangent = getTangent(t); + vertices[2*i ] = position + tangent * realWidth; + vertices[2*i+1] = position - tangent * realWidth; + } + mesh.Clear(); + mesh.vertices = vertices; + mesh.triangles = indices; + mesh.Optimize(); + } + + private Vector3 getTangent(float t) { + Vector3 direction = bezier.getDirection(t); + direction.Normalize(); + return new Vector3(-direction.z, 0, direction.x); + } +} diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta b/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta new file mode 100644 index 0000000..1fe6547 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9bfb303c44d15ef468dc0d7334241e94 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/FlatCircleRenderer.cs b/Assets/Scripts/Primitives/FlatCircleRenderer.cs new file mode 100644 index 0000000..363b663 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatCircleRenderer.cs @@ -0,0 +1,42 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FlatCircleRenderer { + public Mesh mesh = new Mesh(); + public float radius, width; + private int segments; + + public FlatCircleRenderer(float radius, float width, int segments) { + this.radius = radius; + this.width = width; + this.segments = segments; + update(); + } + + public void update() { + Vector3[] vertices = new Vector3[2*segments+6]; + int[] indices = new int[6*segments]; + for (int i = 0; i < segments; i++) { + float alpha = (float) i / segments * 2 * Mathf.PI; + indices[6*i ] = 2*i ; + indices[6*i+3] = 2*i ; + indices[6*i+5] = 2*i+1; + if (i == 0) { + indices[6*i+1] = 2*segments-2; + indices[6*i+2] = 2*segments-1; + indices[6*i+4] = 2*segments-1; + } else { + indices[6*i+1] = 2*i-2; + indices[6*i+2] = 2*i-1; + indices[6*i+4] = 2*i-1; + } + Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); + vertices[2*i ] = direction * radius; + vertices[2*i+1] = direction * (radius + width); + } + mesh.Clear(); + mesh.vertices = vertices; + mesh.triangles = indices; + } +} diff --git a/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta b/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta new file mode 100644 index 0000000..d10c238 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0fe81c7924a85b142a4a86201b08c241 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Road.cs b/Assets/Scripts/Road.cs deleted file mode 100644 index 0af2218..0000000 --- a/Assets/Scripts/Road.cs +++ /dev/null @@ -1,85 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Road { - public List nodes = new List(); - Bezier path; - FlatBezierRenderer pathLine, roadBody; - Config config; - MeshCollider collider; - - public Road(Node start, Node end, Config config) { - nodes.Add(start); - nodes.Add(end); - path = new Bezier(); - pathLine = new FlatBezierRenderer(path, 50, 0.05f); - roadBody = new FlatBezierRenderer(path, 50, 2f); - this.config = config; - } - - - public void initialize(Transform parent) { - GameObject child = new GameObject(); - child.transform.parent = parent; - GameObject lineChild = new GameObject(); - lineChild.transform.position = Vector3.zero; - lineChild.AddComponent().material = config.roadEditMaterial; - lineChild.AddComponent().mesh = pathLine.mesh; - lineChild.transform.parent = child.transform; - - GameObject roadChild = new GameObject(); - collider = roadChild.AddComponent(); - roadChild.transform.position = Vector3.zero; - roadChild.AddComponent().material = config.roadMaterial; - roadChild.transform.parent = child.transform; - roadChild.layer = 8; - roadChild.AddComponent().mesh = roadBody.mesh; - roadChild.AddComponent().road = this; - } - - public void update(bool updateOthers) { - path.A = nodes[0].position; - path.D = nodes[1].position; - if (nodes[0].roads.Count == 2) { - path.B = nodes[0].position + - 0.25f * (nodes[1].position - nodes[0].getOther(nodes[1]).position); - } else { - path.B = 0.5f * (nodes[0].position + nodes[1].position); - } - if (nodes[1].roads.Count == 2) { - path.C = nodes[1].position + - 0.25f * (nodes[0].position - nodes[1].getOther(nodes[0]).position); - } else { - path.C = 0.5f * (nodes[0].position + nodes[1].position); - } - if (updateOthers) { - foreach (Node node in nodes) { - node.lateUpdate(this); - } - } - pathLine.update(); - roadBody.update(); - collider.sharedMesh = roadBody.mesh; - } - - override public bool Equals(object other) { - if (other == null || !(other is Road)) { - return false; - } - if (other == this) { - return true; - } - Road otherRoad = (Road) other; - foreach (Node node in nodes) { - if (! otherRoad.nodes.Contains(node)) { - return false; - } - } - return true; - } - - public override int GetHashCode() { - return nodes[0].GetHashCode() << 16 | nodes[1].GetHashCode(); - } -} diff --git a/Assets/Scripts/Road.cs.meta b/Assets/Scripts/Road.cs.meta deleted file mode 100644 index 24dc94b..0000000 --- a/Assets/Scripts/Road.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0ab512a631c0cc4468e77b6a2dfeb31f -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/RoadData.cs b/Assets/Scripts/RoadData.cs deleted file mode 100644 index eb620ba..0000000 --- a/Assets/Scripts/RoadData.cs +++ /dev/null @@ -1,7 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class RoadData : MonoBehaviour { - public Road road; -} diff --git a/Assets/Scripts/RoadData.cs.meta b/Assets/Scripts/RoadData.cs.meta deleted file mode 100644 index 2948b97..0000000 --- a/Assets/Scripts/RoadData.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 6bfac654eff68754c88b877a9b88ae42 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/RoadEdit.cs b/Assets/Scripts/RoadEdit.cs deleted file mode 100644 index a6cc73e..0000000 --- a/Assets/Scripts/RoadEdit.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class RoadEdit : MonoBehaviour { - public GameObject start; - public GameObject startDirection; - public GameObject end; - public GameObject endDirection; - public Camera mainCamera; - - private GameObject currentlyPulling = null; - public Bezier bezier; - private FlatBezierRenderer pathRenderer; - public Material highlight, noHighlight; - - void Start() { - } - - void updateRoad(Vector3 position) { - } - - void Update() { - Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); - if (Input.GetAxis("Fire1") != 0.0f) { - if (currentlyPulling == null && Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, 1 << 7)) { - currentlyPulling = hit.transform.gameObject; - currentlyPulling.GetComponent().material = highlight; - } - } else { - if (currentlyPulling != null) { - currentlyPulling.GetComponent().material = noHighlight; - } - currentlyPulling = null; - } - if (currentlyPulling != null) { - Physics.Raycast(ray, out RaycastHit hit_, Mathf.Infinity, 1 << 6); - Vector3 position = new Vector3(hit_.point.x, 0.0f, hit_.point.z); - currentlyPulling.transform.position = position; - updateRoad(position); - } - } -} diff --git a/Assets/Scripts/RoadEdit.cs.meta b/Assets/Scripts/RoadEdit.cs.meta deleted file mode 100644 index 4e09d01..0000000 --- a/Assets/Scripts/RoadEdit.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 68e9412fa432f2e468d5d7447e99fd89 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Roads.cs b/Assets/Scripts/Roads.cs deleted file mode 100644 index acd376a..0000000 --- a/Assets/Scripts/Roads.cs +++ /dev/null @@ -1,102 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Roads : MonoBehaviour { - public List nodes = new List(); - public List roads = new List(); - public FlatBezierRenderer roadRenderer; - public Camera mainCamera; - private bool drawing = false; - public Material material; - public Config config; - - void Start() { - } - - private Node startNode; - - private void startRoad() { - Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out RaycastHit hit, Mathf.Infinity, 1 << 6); - foreach (Node node in nodes) { - if ((hit.point - node.position).magnitude < 1.0f) { - startNode = node; - drawing = true; - return; - } - } - Vector3 position = new Vector3(hit.point.x, 0.0f, hit.point.z); - startNode = new Node(position, transform, config); - nodes.Add(startNode); - drawing = true; - } - - private void endRoad() { - Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out RaycastHit hit, Mathf.Infinity, 1 << 6); - Node endNode = null; - foreach (Node node in nodes) { - if ((hit.point - node.position).magnitude < 1.0f) { - endNode = node; - break; - } - } - if (endNode == null) { - Vector3 position = new Vector3(hit.point.x, 0.0f, hit.point.z); - endNode = new Node(position, transform, config); - nodes.Add(endNode); - } - Road road = new Road(startNode, endNode, config); - if (!roads.Contains(road) && startNode != endNode) { - startNode.roads.Add(road); - endNode.roads.Add(road); - roads.Add(road); - road.initialize(transform); - road.update(true); - } - startNode = null; - drawing = false; - } - - Node pulling = null; - - void Update() { - Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); - RaycastHit hit; - if (config.mode == Mode.DrawRoad) { - if (Input.GetAxis("Fire1") != 0.0f) { - if (!drawing) { - startRoad(); - } - } else if (drawing) { - endRoad(); - } - } else if (config.mode == Mode.DeleteRoad) { - if (!drawing && Input.GetAxis("Fire1") != 0.0f) { - if (Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 8)) { - drawing = true; - Road road = hit.transform.gameObject.GetComponent().road; - roads.Remove(road); - foreach (Node node in road.nodes) { - node.roads.Remove(road); - node.update(); - } - Destroy(hit.transform.parent.gameObject); - } - } else if (drawing && Input.GetAxis("Fire1") == 0.0f) { - drawing = false; - } - } - if (Input.GetAxis("Fire2") != 0.0f) { - if (pulling == null && Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 7)) { - pulling = nodes.Find(node => node.gameObject == hit.transform.gameObject); - } - } else { - pulling = null; - } - if (pulling != null) { - Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 6); - Vector3 position = new Vector3(hit.point.x, 0.0f, hit.point.z); - pulling.pull(position); - } - } -} diff --git a/Assets/Scripts/Roads.cs.meta b/Assets/Scripts/Roads.cs.meta deleted file mode 100644 index f26a401..0000000 --- a/Assets/Scripts/Roads.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 7635c530aa8ef31498f100f4e7465bea -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Roads.meta b/Assets/Scripts/Roads.meta new file mode 100644 index 0000000..defbd46 --- /dev/null +++ b/Assets/Scripts/Roads.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: e6a3f10ad4786e44d9faf825f11a8e1d +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Roads/Node.cs b/Assets/Scripts/Roads/Node.cs new file mode 100644 index 0000000..3b2f31a --- /dev/null +++ b/Assets/Scripts/Roads/Node.cs @@ -0,0 +1,82 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Node { + private FlatCircleRenderer circle = new FlatCircleRenderer(0.2f, 0.05f, 32); + private FlatCircleRenderer fullCircle = new FlatCircleRenderer(0f, 1f, 32); + + public Vector3 position; + public List roads = new List(); + public GameObject gameObject; + + public Node(Vector3 position, Transform parent, Config config) { + gameObject = new GameObject(); + gameObject.transform.position = position; + gameObject.transform.parent = parent; + gameObject.AddComponent().radius = 2f; + gameObject.layer = 7; + gameObject.AddComponent().node = this; + + GameObject nodeCircle = new GameObject(); + nodeCircle.AddComponent().material = config.roadEditMaterial; + nodeCircle.AddComponent().mesh = circle.mesh; + nodeCircle.transform.parent = gameObject.transform; + nodeCircle.transform.localPosition = Vector3.zero; + + GameObject nodeRoad = new GameObject(); + nodeRoad.AddComponent().material = config.roadMaterial; + nodeRoad.AddComponent().mesh = fullCircle.mesh; + nodeRoad.transform.parent = gameObject.transform; + nodeRoad.transform.localPosition = Vector3.zero; + 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(); + } + + override public bool Equals(object other) { + if ((other == null) || ! this.GetType().Equals(other.GetType())) { + return false; + } + if (other == this) { + return true; + } + return ((Node)other).position.Equals(position); + } + + public void pull(Vector3 position) { + this.position = position; + gameObject.transform.position = position; + foreach (Road road in roads) { + 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/Roads/Node.cs.meta b/Assets/Scripts/Roads/Node.cs.meta new file mode 100644 index 0000000..8bbedf1 --- /dev/null +++ b/Assets/Scripts/Roads/Node.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 3cbed1dad7ec67d4dbe8602aea1e2979 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Roads/NodeData.cs b/Assets/Scripts/Roads/NodeData.cs new file mode 100644 index 0000000..9ee8ace --- /dev/null +++ b/Assets/Scripts/Roads/NodeData.cs @@ -0,0 +1,7 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class NodeData : MonoBehaviour { + public Node node = null; +} diff --git a/Assets/Scripts/Roads/NodeData.cs.meta b/Assets/Scripts/Roads/NodeData.cs.meta new file mode 100644 index 0000000..73dcf56 --- /dev/null +++ b/Assets/Scripts/Roads/NodeData.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4cf9bcd8c4889de4985e5d6a776acc5b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Bezier.cs b/Assets/Scripts/Bezier.cs deleted file mode 100644 index ea19006..0000000 --- a/Assets/Scripts/Bezier.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Bezier { - public Vector3 A, B, C, D; - - public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { - this.A = A; - this.B = B; - this.C = C; - this.D = D; - } - - public Bezier() { - this.A = Vector3.zero; - this.B = Vector3.zero; - this.C = Vector3.zero; - this.D = Vector3.zero; - } - - public Vector3 getPosition(float t) { - float T = 1.0f - t; - return - A * ( T*T*T) + - B * (3*T*T*t) + - C * (3*T*t*t) + - D * ( t*t*t); - } - - public Vector3 getDirection(float t) { - float T = 1.0f - t; - return - A * (-3*T*T ) + - B * ( 3*T*T - 6*T*t) + - C * (-3*t*t + 6*T*t) + - D * ( 3*t*t ); - } -} diff --git a/Assets/Scripts/Bezier.cs.meta b/Assets/Scripts/Bezier.cs.meta deleted file mode 100644 index 14630e2..0000000 --- a/Assets/Scripts/Bezier.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 284cf9d52e385aa4ebba97eb8b43b8a6 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatBezierRenderer.cs b/Assets/Scripts/FlatBezierRenderer.cs deleted file mode 100644 index b8dad15..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatBezierRenderer { - private int percision; - private float width; - public Bezier bezier; - public Mesh mesh = new Mesh(); - public FlatBezierRenderer(Bezier bezier, int percision, float width) { - this.percision = percision; - this.width = width; - this.bezier = bezier; - update(); - } - - public void update() { - float realWidth = width / 2; - int vertexCount = 6 * (percision+1); - Vector3[] vertices = new Vector3[vertexCount / 3]; - int[] indices = new int[vertexCount]; - Vector3 position = bezier.getPosition(0.0f); - Vector3 tangent = getTangent(0.0f); - vertices[0] = position + tangent * realWidth; - vertices[1] = position - tangent * realWidth; - for (int i = 1; i <= percision; i++) { - float t = (float) i / percision; - indices[6*i ] = 2*i ; - indices[6*i+1] = 2*i-1; - indices[6*i+2] = 2*i-2; - indices[6*i+3] = 2*i+1; - indices[6*i+4] = 2*i-1; - indices[6*i+5] = 2*i ; - - position = bezier.getPosition(t); - tangent = getTangent(t); - vertices[2*i ] = position + tangent * realWidth; - vertices[2*i+1] = position - tangent * realWidth; - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - mesh.Optimize(); - } - - private Vector3 getTangent(float t) { - Vector3 direction = bezier.getDirection(t); - direction.Normalize(); - return new Vector3(-direction.z, 0, direction.x); - } -} diff --git a/Assets/Scripts/FlatBezierRenderer.cs.meta b/Assets/Scripts/FlatBezierRenderer.cs.meta deleted file mode 100644 index 1fe6547..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 9bfb303c44d15ef468dc0d7334241e94 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatCircleRenderer.cs b/Assets/Scripts/FlatCircleRenderer.cs deleted file mode 100644 index 363b663..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatCircleRenderer { - public Mesh mesh = new Mesh(); - public float radius, width; - private int segments; - - public FlatCircleRenderer(float radius, float width, int segments) { - this.radius = radius; - this.width = width; - this.segments = segments; - update(); - } - - public void update() { - Vector3[] vertices = new Vector3[2*segments+6]; - int[] indices = new int[6*segments]; - for (int i = 0; i < segments; i++) { - float alpha = (float) i / segments * 2 * Mathf.PI; - indices[6*i ] = 2*i ; - indices[6*i+3] = 2*i ; - indices[6*i+5] = 2*i+1; - if (i == 0) { - indices[6*i+1] = 2*segments-2; - indices[6*i+2] = 2*segments-1; - indices[6*i+4] = 2*segments-1; - } else { - indices[6*i+1] = 2*i-2; - indices[6*i+2] = 2*i-1; - indices[6*i+4] = 2*i-1; - } - Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); - vertices[2*i ] = direction * radius; - vertices[2*i+1] = direction * (radius + width); - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - } -} diff --git a/Assets/Scripts/FlatCircleRenderer.cs.meta b/Assets/Scripts/FlatCircleRenderer.cs.meta deleted file mode 100644 index d10c238..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0fe81c7924a85b142a4a86201b08c241 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Node.cs b/Assets/Scripts/Node.cs deleted file mode 100644 index e3c4cbd..0000000 --- a/Assets/Scripts/Node.cs +++ /dev/null @@ -1,81 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Node { - public FlatCircleRenderer circle = new FlatCircleRenderer(0.2f, 0.05f, 32); - public FlatCircleRenderer fullCircle = new FlatCircleRenderer(0f, 1f, 32); - - public Vector3 position; - public List roads = new List(); - public GameObject gameObject; - - public Node(Vector3 position, Transform parent, Config config) { - gameObject = new GameObject(); - gameObject.transform.position = position; - gameObject.transform.parent = parent; - gameObject.AddComponent().radius = 0.5f; - gameObject.layer = 7; - - GameObject nodeCircle = new GameObject(); - nodeCircle.AddComponent().material = config.roadEditMaterial; - nodeCircle.AddComponent().mesh = circle.mesh; - nodeCircle.transform.parent = gameObject.transform; - nodeCircle.transform.localPosition = Vector3.zero; - - GameObject nodeRoad = new GameObject(); - nodeRoad.AddComponent().material = config.roadMaterial; - nodeRoad.AddComponent().mesh = fullCircle.mesh; - nodeRoad.transform.parent = gameObject.transform; - nodeRoad.transform.localPosition = Vector3.zero; - 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(); - } - - override public bool Equals(object other) { - if ((other == null) || ! this.GetType().Equals(other.GetType())) { - return false; - } - if (other == this) { - return true; - } - return ((Node)other).position.Equals(position); - } - - public void pull(Vector3 position) { - this.position = position; - gameObject.transform.position = position; - foreach (Road road in roads) { - 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/Node.cs.meta b/Assets/Scripts/Node.cs.meta deleted file mode 100644 index 8bbedf1..0000000 --- a/Assets/Scripts/Node.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 3cbed1dad7ec67d4dbe8602aea1e2979 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Primitives.meta b/Assets/Scripts/Primitives.meta new file mode 100644 index 0000000..0cf8895 --- /dev/null +++ b/Assets/Scripts/Primitives.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 6d1a8c7f33189a240b4d749356f53399 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/Bezier.cs b/Assets/Scripts/Primitives/Bezier.cs new file mode 100644 index 0000000..ea19006 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs @@ -0,0 +1,39 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Bezier { + public Vector3 A, B, C, D; + + public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { + this.A = A; + this.B = B; + this.C = C; + this.D = D; + } + + public Bezier() { + this.A = Vector3.zero; + this.B = Vector3.zero; + this.C = Vector3.zero; + this.D = Vector3.zero; + } + + public Vector3 getPosition(float t) { + float T = 1.0f - t; + return + A * ( T*T*T) + + B * (3*T*T*t) + + C * (3*T*t*t) + + D * ( t*t*t); + } + + public Vector3 getDirection(float t) { + float T = 1.0f - t; + return + A * (-3*T*T ) + + B * ( 3*T*T - 6*T*t) + + C * (-3*t*t + 6*T*t) + + D * ( 3*t*t ); + } +} diff --git a/Assets/Scripts/Primitives/Bezier.cs.meta b/Assets/Scripts/Primitives/Bezier.cs.meta new file mode 100644 index 0000000..14630e2 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 284cf9d52e385aa4ebba97eb8b43b8a6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs b/Assets/Scripts/Primitives/FlatBezierRenderer.cs new file mode 100644 index 0000000..b8dad15 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs @@ -0,0 +1,51 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FlatBezierRenderer { + private int percision; + private float width; + public Bezier bezier; + public Mesh mesh = new Mesh(); + public FlatBezierRenderer(Bezier bezier, int percision, float width) { + this.percision = percision; + this.width = width; + this.bezier = bezier; + update(); + } + + public void update() { + float realWidth = width / 2; + int vertexCount = 6 * (percision+1); + Vector3[] vertices = new Vector3[vertexCount / 3]; + int[] indices = new int[vertexCount]; + Vector3 position = bezier.getPosition(0.0f); + Vector3 tangent = getTangent(0.0f); + vertices[0] = position + tangent * realWidth; + vertices[1] = position - tangent * realWidth; + for (int i = 1; i <= percision; i++) { + float t = (float) i / percision; + indices[6*i ] = 2*i ; + indices[6*i+1] = 2*i-1; + indices[6*i+2] = 2*i-2; + indices[6*i+3] = 2*i+1; + indices[6*i+4] = 2*i-1; + indices[6*i+5] = 2*i ; + + position = bezier.getPosition(t); + tangent = getTangent(t); + vertices[2*i ] = position + tangent * realWidth; + vertices[2*i+1] = position - tangent * realWidth; + } + mesh.Clear(); + mesh.vertices = vertices; + mesh.triangles = indices; + mesh.Optimize(); + } + + private Vector3 getTangent(float t) { + Vector3 direction = bezier.getDirection(t); + direction.Normalize(); + return new Vector3(-direction.z, 0, direction.x); + } +} diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta b/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta new file mode 100644 index 0000000..1fe6547 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9bfb303c44d15ef468dc0d7334241e94 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/FlatCircleRenderer.cs b/Assets/Scripts/Primitives/FlatCircleRenderer.cs new file mode 100644 index 0000000..363b663 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatCircleRenderer.cs @@ -0,0 +1,42 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FlatCircleRenderer { + public Mesh mesh = new Mesh(); + public float radius, width; + private int segments; + + public FlatCircleRenderer(float radius, float width, int segments) { + this.radius = radius; + this.width = width; + this.segments = segments; + update(); + } + + public void update() { + Vector3[] vertices = new Vector3[2*segments+6]; + int[] indices = new int[6*segments]; + for (int i = 0; i < segments; i++) { + float alpha = (float) i / segments * 2 * Mathf.PI; + indices[6*i ] = 2*i ; + indices[6*i+3] = 2*i ; + indices[6*i+5] = 2*i+1; + if (i == 0) { + indices[6*i+1] = 2*segments-2; + indices[6*i+2] = 2*segments-1; + indices[6*i+4] = 2*segments-1; + } else { + indices[6*i+1] = 2*i-2; + indices[6*i+2] = 2*i-1; + indices[6*i+4] = 2*i-1; + } + Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); + vertices[2*i ] = direction * radius; + vertices[2*i+1] = direction * (radius + width); + } + mesh.Clear(); + mesh.vertices = vertices; + mesh.triangles = indices; + } +} diff --git a/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta b/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta new file mode 100644 index 0000000..d10c238 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0fe81c7924a85b142a4a86201b08c241 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Road.cs b/Assets/Scripts/Road.cs deleted file mode 100644 index 0af2218..0000000 --- a/Assets/Scripts/Road.cs +++ /dev/null @@ -1,85 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Road { - public List nodes = new List(); - Bezier path; - FlatBezierRenderer pathLine, roadBody; - Config config; - MeshCollider collider; - - public Road(Node start, Node end, Config config) { - nodes.Add(start); - nodes.Add(end); - path = new Bezier(); - pathLine = new FlatBezierRenderer(path, 50, 0.05f); - roadBody = new FlatBezierRenderer(path, 50, 2f); - this.config = config; - } - - - public void initialize(Transform parent) { - GameObject child = new GameObject(); - child.transform.parent = parent; - GameObject lineChild = new GameObject(); - lineChild.transform.position = Vector3.zero; - lineChild.AddComponent().material = config.roadEditMaterial; - lineChild.AddComponent().mesh = pathLine.mesh; - lineChild.transform.parent = child.transform; - - GameObject roadChild = new GameObject(); - collider = roadChild.AddComponent(); - roadChild.transform.position = Vector3.zero; - roadChild.AddComponent().material = config.roadMaterial; - roadChild.transform.parent = child.transform; - roadChild.layer = 8; - roadChild.AddComponent().mesh = roadBody.mesh; - roadChild.AddComponent().road = this; - } - - public void update(bool updateOthers) { - path.A = nodes[0].position; - path.D = nodes[1].position; - if (nodes[0].roads.Count == 2) { - path.B = nodes[0].position + - 0.25f * (nodes[1].position - nodes[0].getOther(nodes[1]).position); - } else { - path.B = 0.5f * (nodes[0].position + nodes[1].position); - } - if (nodes[1].roads.Count == 2) { - path.C = nodes[1].position + - 0.25f * (nodes[0].position - nodes[1].getOther(nodes[0]).position); - } else { - path.C = 0.5f * (nodes[0].position + nodes[1].position); - } - if (updateOthers) { - foreach (Node node in nodes) { - node.lateUpdate(this); - } - } - pathLine.update(); - roadBody.update(); - collider.sharedMesh = roadBody.mesh; - } - - override public bool Equals(object other) { - if (other == null || !(other is Road)) { - return false; - } - if (other == this) { - return true; - } - Road otherRoad = (Road) other; - foreach (Node node in nodes) { - if (! otherRoad.nodes.Contains(node)) { - return false; - } - } - return true; - } - - public override int GetHashCode() { - return nodes[0].GetHashCode() << 16 | nodes[1].GetHashCode(); - } -} diff --git a/Assets/Scripts/Road.cs.meta b/Assets/Scripts/Road.cs.meta deleted file mode 100644 index 24dc94b..0000000 --- a/Assets/Scripts/Road.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0ab512a631c0cc4468e77b6a2dfeb31f -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/RoadData.cs b/Assets/Scripts/RoadData.cs deleted file mode 100644 index eb620ba..0000000 --- a/Assets/Scripts/RoadData.cs +++ /dev/null @@ -1,7 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class RoadData : MonoBehaviour { - public Road road; -} diff --git a/Assets/Scripts/RoadData.cs.meta b/Assets/Scripts/RoadData.cs.meta deleted file mode 100644 index 2948b97..0000000 --- a/Assets/Scripts/RoadData.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 6bfac654eff68754c88b877a9b88ae42 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/RoadEdit.cs b/Assets/Scripts/RoadEdit.cs deleted file mode 100644 index a6cc73e..0000000 --- a/Assets/Scripts/RoadEdit.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class RoadEdit : MonoBehaviour { - public GameObject start; - public GameObject startDirection; - public GameObject end; - public GameObject endDirection; - public Camera mainCamera; - - private GameObject currentlyPulling = null; - public Bezier bezier; - private FlatBezierRenderer pathRenderer; - public Material highlight, noHighlight; - - void Start() { - } - - void updateRoad(Vector3 position) { - } - - void Update() { - Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); - if (Input.GetAxis("Fire1") != 0.0f) { - if (currentlyPulling == null && Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, 1 << 7)) { - currentlyPulling = hit.transform.gameObject; - currentlyPulling.GetComponent().material = highlight; - } - } else { - if (currentlyPulling != null) { - currentlyPulling.GetComponent().material = noHighlight; - } - currentlyPulling = null; - } - if (currentlyPulling != null) { - Physics.Raycast(ray, out RaycastHit hit_, Mathf.Infinity, 1 << 6); - Vector3 position = new Vector3(hit_.point.x, 0.0f, hit_.point.z); - currentlyPulling.transform.position = position; - updateRoad(position); - } - } -} diff --git a/Assets/Scripts/RoadEdit.cs.meta b/Assets/Scripts/RoadEdit.cs.meta deleted file mode 100644 index 4e09d01..0000000 --- a/Assets/Scripts/RoadEdit.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 68e9412fa432f2e468d5d7447e99fd89 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Roads.cs b/Assets/Scripts/Roads.cs deleted file mode 100644 index acd376a..0000000 --- a/Assets/Scripts/Roads.cs +++ /dev/null @@ -1,102 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Roads : MonoBehaviour { - public List nodes = new List(); - public List roads = new List(); - public FlatBezierRenderer roadRenderer; - public Camera mainCamera; - private bool drawing = false; - public Material material; - public Config config; - - void Start() { - } - - private Node startNode; - - private void startRoad() { - Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out RaycastHit hit, Mathf.Infinity, 1 << 6); - foreach (Node node in nodes) { - if ((hit.point - node.position).magnitude < 1.0f) { - startNode = node; - drawing = true; - return; - } - } - Vector3 position = new Vector3(hit.point.x, 0.0f, hit.point.z); - startNode = new Node(position, transform, config); - nodes.Add(startNode); - drawing = true; - } - - private void endRoad() { - Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out RaycastHit hit, Mathf.Infinity, 1 << 6); - Node endNode = null; - foreach (Node node in nodes) { - if ((hit.point - node.position).magnitude < 1.0f) { - endNode = node; - break; - } - } - if (endNode == null) { - Vector3 position = new Vector3(hit.point.x, 0.0f, hit.point.z); - endNode = new Node(position, transform, config); - nodes.Add(endNode); - } - Road road = new Road(startNode, endNode, config); - if (!roads.Contains(road) && startNode != endNode) { - startNode.roads.Add(road); - endNode.roads.Add(road); - roads.Add(road); - road.initialize(transform); - road.update(true); - } - startNode = null; - drawing = false; - } - - Node pulling = null; - - void Update() { - Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); - RaycastHit hit; - if (config.mode == Mode.DrawRoad) { - if (Input.GetAxis("Fire1") != 0.0f) { - if (!drawing) { - startRoad(); - } - } else if (drawing) { - endRoad(); - } - } else if (config.mode == Mode.DeleteRoad) { - if (!drawing && Input.GetAxis("Fire1") != 0.0f) { - if (Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 8)) { - drawing = true; - Road road = hit.transform.gameObject.GetComponent().road; - roads.Remove(road); - foreach (Node node in road.nodes) { - node.roads.Remove(road); - node.update(); - } - Destroy(hit.transform.parent.gameObject); - } - } else if (drawing && Input.GetAxis("Fire1") == 0.0f) { - drawing = false; - } - } - if (Input.GetAxis("Fire2") != 0.0f) { - if (pulling == null && Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 7)) { - pulling = nodes.Find(node => node.gameObject == hit.transform.gameObject); - } - } else { - pulling = null; - } - if (pulling != null) { - Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 6); - Vector3 position = new Vector3(hit.point.x, 0.0f, hit.point.z); - pulling.pull(position); - } - } -} diff --git a/Assets/Scripts/Roads.cs.meta b/Assets/Scripts/Roads.cs.meta deleted file mode 100644 index f26a401..0000000 --- a/Assets/Scripts/Roads.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 7635c530aa8ef31498f100f4e7465bea -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Roads.meta b/Assets/Scripts/Roads.meta new file mode 100644 index 0000000..defbd46 --- /dev/null +++ b/Assets/Scripts/Roads.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: e6a3f10ad4786e44d9faf825f11a8e1d +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Roads/Node.cs b/Assets/Scripts/Roads/Node.cs new file mode 100644 index 0000000..3b2f31a --- /dev/null +++ b/Assets/Scripts/Roads/Node.cs @@ -0,0 +1,82 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Node { + private FlatCircleRenderer circle = new FlatCircleRenderer(0.2f, 0.05f, 32); + private FlatCircleRenderer fullCircle = new FlatCircleRenderer(0f, 1f, 32); + + public Vector3 position; + public List roads = new List(); + public GameObject gameObject; + + public Node(Vector3 position, Transform parent, Config config) { + gameObject = new GameObject(); + gameObject.transform.position = position; + gameObject.transform.parent = parent; + gameObject.AddComponent().radius = 2f; + gameObject.layer = 7; + gameObject.AddComponent().node = this; + + GameObject nodeCircle = new GameObject(); + nodeCircle.AddComponent().material = config.roadEditMaterial; + nodeCircle.AddComponent().mesh = circle.mesh; + nodeCircle.transform.parent = gameObject.transform; + nodeCircle.transform.localPosition = Vector3.zero; + + GameObject nodeRoad = new GameObject(); + nodeRoad.AddComponent().material = config.roadMaterial; + nodeRoad.AddComponent().mesh = fullCircle.mesh; + nodeRoad.transform.parent = gameObject.transform; + nodeRoad.transform.localPosition = Vector3.zero; + 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(); + } + + override public bool Equals(object other) { + if ((other == null) || ! this.GetType().Equals(other.GetType())) { + return false; + } + if (other == this) { + return true; + } + return ((Node)other).position.Equals(position); + } + + public void pull(Vector3 position) { + this.position = position; + gameObject.transform.position = position; + foreach (Road road in roads) { + 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/Roads/Node.cs.meta b/Assets/Scripts/Roads/Node.cs.meta new file mode 100644 index 0000000..8bbedf1 --- /dev/null +++ b/Assets/Scripts/Roads/Node.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 3cbed1dad7ec67d4dbe8602aea1e2979 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Roads/NodeData.cs b/Assets/Scripts/Roads/NodeData.cs new file mode 100644 index 0000000..9ee8ace --- /dev/null +++ b/Assets/Scripts/Roads/NodeData.cs @@ -0,0 +1,7 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class NodeData : MonoBehaviour { + public Node node = null; +} diff --git a/Assets/Scripts/Roads/NodeData.cs.meta b/Assets/Scripts/Roads/NodeData.cs.meta new file mode 100644 index 0000000..73dcf56 --- /dev/null +++ b/Assets/Scripts/Roads/NodeData.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4cf9bcd8c4889de4985e5d6a776acc5b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Roads/Road.cs b/Assets/Scripts/Roads/Road.cs new file mode 100644 index 0000000..1ad1a74 --- /dev/null +++ b/Assets/Scripts/Roads/Road.cs @@ -0,0 +1,88 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Road { + public List nodes = new List(); + Bezier path; + private FlatBezierRenderer pathLine, roadBody; + private Config config; + private MeshCollider collider; + public GameObject gameObject; + + public Road(Node start, Node end, Config config) { + nodes.Add(start); + nodes.Add(end); + path = new Bezier(); + pathLine = new FlatBezierRenderer(path, 50, 0.05f); + roadBody = new FlatBezierRenderer(path, 50, 2f); + this.config = config; + } + + + public void initialize(Transform parent) { + gameObject = new GameObject(); + gameObject.transform.parent = parent; + gameObject.transform.position = Vector3.zero; + + GameObject lineChild = new GameObject(); + lineChild.transform.position = Vector3.zero; + lineChild.AddComponent().material = config.roadEditMaterial; + lineChild.AddComponent().mesh = pathLine.mesh; + lineChild.transform.parent = gameObject.transform; + + GameObject bodyChild = new GameObject(); + bodyChild.AddComponent().material = config.roadMaterial; + bodyChild.AddComponent().mesh = roadBody.mesh; + bodyChild.AddComponent().road = this; + collider = bodyChild.AddComponent(); + bodyChild.transform.parent = gameObject.transform; + bodyChild.layer = 8; + update(true); + } + + public void update(bool updateOthers) { + path.A = nodes[0].position; + path.D = nodes[1].position; + if (nodes[0].roads.Count == 2) { + path.B = nodes[0].position + + 0.25f * (nodes[1].position - nodes[0].getOther(nodes[1]).position); + } else { + path.B = 0.5f * (nodes[0].position + nodes[1].position); + } + if (nodes[1].roads.Count == 2) { + path.C = nodes[1].position + + 0.25f * (nodes[0].position - nodes[1].getOther(nodes[0]).position); + } else { + path.C = 0.5f * (nodes[0].position + nodes[1].position); + } + if (updateOthers) { + foreach (Node node in nodes) { + node.lateUpdate(this); + } + } + pathLine.update(); + roadBody.update(); + collider.sharedMesh = roadBody.mesh; + } + + override public bool Equals(object other) { + if (other == null || !(other is Road)) { + return false; + } + if (other == this) { + return true; + } + Road otherRoad = (Road) other; + foreach (Node node in nodes) { + if (! otherRoad.nodes.Contains(node)) { + return false; + } + } + return true; + } + + public override int GetHashCode() { + return nodes[0].GetHashCode() << 16 | nodes[1].GetHashCode(); + } +} diff --git a/Assets/Scripts/Bezier.cs b/Assets/Scripts/Bezier.cs deleted file mode 100644 index ea19006..0000000 --- a/Assets/Scripts/Bezier.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Bezier { - public Vector3 A, B, C, D; - - public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { - this.A = A; - this.B = B; - this.C = C; - this.D = D; - } - - public Bezier() { - this.A = Vector3.zero; - this.B = Vector3.zero; - this.C = Vector3.zero; - this.D = Vector3.zero; - } - - public Vector3 getPosition(float t) { - float T = 1.0f - t; - return - A * ( T*T*T) + - B * (3*T*T*t) + - C * (3*T*t*t) + - D * ( t*t*t); - } - - public Vector3 getDirection(float t) { - float T = 1.0f - t; - return - A * (-3*T*T ) + - B * ( 3*T*T - 6*T*t) + - C * (-3*t*t + 6*T*t) + - D * ( 3*t*t ); - } -} diff --git a/Assets/Scripts/Bezier.cs.meta b/Assets/Scripts/Bezier.cs.meta deleted file mode 100644 index 14630e2..0000000 --- a/Assets/Scripts/Bezier.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 284cf9d52e385aa4ebba97eb8b43b8a6 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatBezierRenderer.cs b/Assets/Scripts/FlatBezierRenderer.cs deleted file mode 100644 index b8dad15..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatBezierRenderer { - private int percision; - private float width; - public Bezier bezier; - public Mesh mesh = new Mesh(); - public FlatBezierRenderer(Bezier bezier, int percision, float width) { - this.percision = percision; - this.width = width; - this.bezier = bezier; - update(); - } - - public void update() { - float realWidth = width / 2; - int vertexCount = 6 * (percision+1); - Vector3[] vertices = new Vector3[vertexCount / 3]; - int[] indices = new int[vertexCount]; - Vector3 position = bezier.getPosition(0.0f); - Vector3 tangent = getTangent(0.0f); - vertices[0] = position + tangent * realWidth; - vertices[1] = position - tangent * realWidth; - for (int i = 1; i <= percision; i++) { - float t = (float) i / percision; - indices[6*i ] = 2*i ; - indices[6*i+1] = 2*i-1; - indices[6*i+2] = 2*i-2; - indices[6*i+3] = 2*i+1; - indices[6*i+4] = 2*i-1; - indices[6*i+5] = 2*i ; - - position = bezier.getPosition(t); - tangent = getTangent(t); - vertices[2*i ] = position + tangent * realWidth; - vertices[2*i+1] = position - tangent * realWidth; - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - mesh.Optimize(); - } - - private Vector3 getTangent(float t) { - Vector3 direction = bezier.getDirection(t); - direction.Normalize(); - return new Vector3(-direction.z, 0, direction.x); - } -} diff --git a/Assets/Scripts/FlatBezierRenderer.cs.meta b/Assets/Scripts/FlatBezierRenderer.cs.meta deleted file mode 100644 index 1fe6547..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 9bfb303c44d15ef468dc0d7334241e94 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatCircleRenderer.cs b/Assets/Scripts/FlatCircleRenderer.cs deleted file mode 100644 index 363b663..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatCircleRenderer { - public Mesh mesh = new Mesh(); - public float radius, width; - private int segments; - - public FlatCircleRenderer(float radius, float width, int segments) { - this.radius = radius; - this.width = width; - this.segments = segments; - update(); - } - - public void update() { - Vector3[] vertices = new Vector3[2*segments+6]; - int[] indices = new int[6*segments]; - for (int i = 0; i < segments; i++) { - float alpha = (float) i / segments * 2 * Mathf.PI; - indices[6*i ] = 2*i ; - indices[6*i+3] = 2*i ; - indices[6*i+5] = 2*i+1; - if (i == 0) { - indices[6*i+1] = 2*segments-2; - indices[6*i+2] = 2*segments-1; - indices[6*i+4] = 2*segments-1; - } else { - indices[6*i+1] = 2*i-2; - indices[6*i+2] = 2*i-1; - indices[6*i+4] = 2*i-1; - } - Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); - vertices[2*i ] = direction * radius; - vertices[2*i+1] = direction * (radius + width); - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - } -} diff --git a/Assets/Scripts/FlatCircleRenderer.cs.meta b/Assets/Scripts/FlatCircleRenderer.cs.meta deleted file mode 100644 index d10c238..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0fe81c7924a85b142a4a86201b08c241 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Node.cs b/Assets/Scripts/Node.cs deleted file mode 100644 index e3c4cbd..0000000 --- a/Assets/Scripts/Node.cs +++ /dev/null @@ -1,81 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Node { - public FlatCircleRenderer circle = new FlatCircleRenderer(0.2f, 0.05f, 32); - public FlatCircleRenderer fullCircle = new FlatCircleRenderer(0f, 1f, 32); - - public Vector3 position; - public List roads = new List(); - public GameObject gameObject; - - public Node(Vector3 position, Transform parent, Config config) { - gameObject = new GameObject(); - gameObject.transform.position = position; - gameObject.transform.parent = parent; - gameObject.AddComponent().radius = 0.5f; - gameObject.layer = 7; - - GameObject nodeCircle = new GameObject(); - nodeCircle.AddComponent().material = config.roadEditMaterial; - nodeCircle.AddComponent().mesh = circle.mesh; - nodeCircle.transform.parent = gameObject.transform; - nodeCircle.transform.localPosition = Vector3.zero; - - GameObject nodeRoad = new GameObject(); - nodeRoad.AddComponent().material = config.roadMaterial; - nodeRoad.AddComponent().mesh = fullCircle.mesh; - nodeRoad.transform.parent = gameObject.transform; - nodeRoad.transform.localPosition = Vector3.zero; - 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(); - } - - override public bool Equals(object other) { - if ((other == null) || ! this.GetType().Equals(other.GetType())) { - return false; - } - if (other == this) { - return true; - } - return ((Node)other).position.Equals(position); - } - - public void pull(Vector3 position) { - this.position = position; - gameObject.transform.position = position; - foreach (Road road in roads) { - 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/Node.cs.meta b/Assets/Scripts/Node.cs.meta deleted file mode 100644 index 8bbedf1..0000000 --- a/Assets/Scripts/Node.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 3cbed1dad7ec67d4dbe8602aea1e2979 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Primitives.meta b/Assets/Scripts/Primitives.meta new file mode 100644 index 0000000..0cf8895 --- /dev/null +++ b/Assets/Scripts/Primitives.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 6d1a8c7f33189a240b4d749356f53399 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/Bezier.cs b/Assets/Scripts/Primitives/Bezier.cs new file mode 100644 index 0000000..ea19006 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs @@ -0,0 +1,39 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Bezier { + public Vector3 A, B, C, D; + + public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { + this.A = A; + this.B = B; + this.C = C; + this.D = D; + } + + public Bezier() { + this.A = Vector3.zero; + this.B = Vector3.zero; + this.C = Vector3.zero; + this.D = Vector3.zero; + } + + public Vector3 getPosition(float t) { + float T = 1.0f - t; + return + A * ( T*T*T) + + B * (3*T*T*t) + + C * (3*T*t*t) + + D * ( t*t*t); + } + + public Vector3 getDirection(float t) { + float T = 1.0f - t; + return + A * (-3*T*T ) + + B * ( 3*T*T - 6*T*t) + + C * (-3*t*t + 6*T*t) + + D * ( 3*t*t ); + } +} diff --git a/Assets/Scripts/Primitives/Bezier.cs.meta b/Assets/Scripts/Primitives/Bezier.cs.meta new file mode 100644 index 0000000..14630e2 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 284cf9d52e385aa4ebba97eb8b43b8a6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs b/Assets/Scripts/Primitives/FlatBezierRenderer.cs new file mode 100644 index 0000000..b8dad15 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs @@ -0,0 +1,51 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FlatBezierRenderer { + private int percision; + private float width; + public Bezier bezier; + public Mesh mesh = new Mesh(); + public FlatBezierRenderer(Bezier bezier, int percision, float width) { + this.percision = percision; + this.width = width; + this.bezier = bezier; + update(); + } + + public void update() { + float realWidth = width / 2; + int vertexCount = 6 * (percision+1); + Vector3[] vertices = new Vector3[vertexCount / 3]; + int[] indices = new int[vertexCount]; + Vector3 position = bezier.getPosition(0.0f); + Vector3 tangent = getTangent(0.0f); + vertices[0] = position + tangent * realWidth; + vertices[1] = position - tangent * realWidth; + for (int i = 1; i <= percision; i++) { + float t = (float) i / percision; + indices[6*i ] = 2*i ; + indices[6*i+1] = 2*i-1; + indices[6*i+2] = 2*i-2; + indices[6*i+3] = 2*i+1; + indices[6*i+4] = 2*i-1; + indices[6*i+5] = 2*i ; + + position = bezier.getPosition(t); + tangent = getTangent(t); + vertices[2*i ] = position + tangent * realWidth; + vertices[2*i+1] = position - tangent * realWidth; + } + mesh.Clear(); + mesh.vertices = vertices; + mesh.triangles = indices; + mesh.Optimize(); + } + + private Vector3 getTangent(float t) { + Vector3 direction = bezier.getDirection(t); + direction.Normalize(); + return new Vector3(-direction.z, 0, direction.x); + } +} diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta b/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta new file mode 100644 index 0000000..1fe6547 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9bfb303c44d15ef468dc0d7334241e94 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/FlatCircleRenderer.cs b/Assets/Scripts/Primitives/FlatCircleRenderer.cs new file mode 100644 index 0000000..363b663 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatCircleRenderer.cs @@ -0,0 +1,42 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FlatCircleRenderer { + public Mesh mesh = new Mesh(); + public float radius, width; + private int segments; + + public FlatCircleRenderer(float radius, float width, int segments) { + this.radius = radius; + this.width = width; + this.segments = segments; + update(); + } + + public void update() { + Vector3[] vertices = new Vector3[2*segments+6]; + int[] indices = new int[6*segments]; + for (int i = 0; i < segments; i++) { + float alpha = (float) i / segments * 2 * Mathf.PI; + indices[6*i ] = 2*i ; + indices[6*i+3] = 2*i ; + indices[6*i+5] = 2*i+1; + if (i == 0) { + indices[6*i+1] = 2*segments-2; + indices[6*i+2] = 2*segments-1; + indices[6*i+4] = 2*segments-1; + } else { + indices[6*i+1] = 2*i-2; + indices[6*i+2] = 2*i-1; + indices[6*i+4] = 2*i-1; + } + Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); + vertices[2*i ] = direction * radius; + vertices[2*i+1] = direction * (radius + width); + } + mesh.Clear(); + mesh.vertices = vertices; + mesh.triangles = indices; + } +} diff --git a/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta b/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta new file mode 100644 index 0000000..d10c238 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0fe81c7924a85b142a4a86201b08c241 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Road.cs b/Assets/Scripts/Road.cs deleted file mode 100644 index 0af2218..0000000 --- a/Assets/Scripts/Road.cs +++ /dev/null @@ -1,85 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Road { - public List nodes = new List(); - Bezier path; - FlatBezierRenderer pathLine, roadBody; - Config config; - MeshCollider collider; - - public Road(Node start, Node end, Config config) { - nodes.Add(start); - nodes.Add(end); - path = new Bezier(); - pathLine = new FlatBezierRenderer(path, 50, 0.05f); - roadBody = new FlatBezierRenderer(path, 50, 2f); - this.config = config; - } - - - public void initialize(Transform parent) { - GameObject child = new GameObject(); - child.transform.parent = parent; - GameObject lineChild = new GameObject(); - lineChild.transform.position = Vector3.zero; - lineChild.AddComponent().material = config.roadEditMaterial; - lineChild.AddComponent().mesh = pathLine.mesh; - lineChild.transform.parent = child.transform; - - GameObject roadChild = new GameObject(); - collider = roadChild.AddComponent(); - roadChild.transform.position = Vector3.zero; - roadChild.AddComponent().material = config.roadMaterial; - roadChild.transform.parent = child.transform; - roadChild.layer = 8; - roadChild.AddComponent().mesh = roadBody.mesh; - roadChild.AddComponent().road = this; - } - - public void update(bool updateOthers) { - path.A = nodes[0].position; - path.D = nodes[1].position; - if (nodes[0].roads.Count == 2) { - path.B = nodes[0].position + - 0.25f * (nodes[1].position - nodes[0].getOther(nodes[1]).position); - } else { - path.B = 0.5f * (nodes[0].position + nodes[1].position); - } - if (nodes[1].roads.Count == 2) { - path.C = nodes[1].position + - 0.25f * (nodes[0].position - nodes[1].getOther(nodes[0]).position); - } else { - path.C = 0.5f * (nodes[0].position + nodes[1].position); - } - if (updateOthers) { - foreach (Node node in nodes) { - node.lateUpdate(this); - } - } - pathLine.update(); - roadBody.update(); - collider.sharedMesh = roadBody.mesh; - } - - override public bool Equals(object other) { - if (other == null || !(other is Road)) { - return false; - } - if (other == this) { - return true; - } - Road otherRoad = (Road) other; - foreach (Node node in nodes) { - if (! otherRoad.nodes.Contains(node)) { - return false; - } - } - return true; - } - - public override int GetHashCode() { - return nodes[0].GetHashCode() << 16 | nodes[1].GetHashCode(); - } -} diff --git a/Assets/Scripts/Road.cs.meta b/Assets/Scripts/Road.cs.meta deleted file mode 100644 index 24dc94b..0000000 --- a/Assets/Scripts/Road.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0ab512a631c0cc4468e77b6a2dfeb31f -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/RoadData.cs b/Assets/Scripts/RoadData.cs deleted file mode 100644 index eb620ba..0000000 --- a/Assets/Scripts/RoadData.cs +++ /dev/null @@ -1,7 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class RoadData : MonoBehaviour { - public Road road; -} diff --git a/Assets/Scripts/RoadData.cs.meta b/Assets/Scripts/RoadData.cs.meta deleted file mode 100644 index 2948b97..0000000 --- a/Assets/Scripts/RoadData.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 6bfac654eff68754c88b877a9b88ae42 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/RoadEdit.cs b/Assets/Scripts/RoadEdit.cs deleted file mode 100644 index a6cc73e..0000000 --- a/Assets/Scripts/RoadEdit.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class RoadEdit : MonoBehaviour { - public GameObject start; - public GameObject startDirection; - public GameObject end; - public GameObject endDirection; - public Camera mainCamera; - - private GameObject currentlyPulling = null; - public Bezier bezier; - private FlatBezierRenderer pathRenderer; - public Material highlight, noHighlight; - - void Start() { - } - - void updateRoad(Vector3 position) { - } - - void Update() { - Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); - if (Input.GetAxis("Fire1") != 0.0f) { - if (currentlyPulling == null && Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, 1 << 7)) { - currentlyPulling = hit.transform.gameObject; - currentlyPulling.GetComponent().material = highlight; - } - } else { - if (currentlyPulling != null) { - currentlyPulling.GetComponent().material = noHighlight; - } - currentlyPulling = null; - } - if (currentlyPulling != null) { - Physics.Raycast(ray, out RaycastHit hit_, Mathf.Infinity, 1 << 6); - Vector3 position = new Vector3(hit_.point.x, 0.0f, hit_.point.z); - currentlyPulling.transform.position = position; - updateRoad(position); - } - } -} diff --git a/Assets/Scripts/RoadEdit.cs.meta b/Assets/Scripts/RoadEdit.cs.meta deleted file mode 100644 index 4e09d01..0000000 --- a/Assets/Scripts/RoadEdit.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 68e9412fa432f2e468d5d7447e99fd89 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Roads.cs b/Assets/Scripts/Roads.cs deleted file mode 100644 index acd376a..0000000 --- a/Assets/Scripts/Roads.cs +++ /dev/null @@ -1,102 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Roads : MonoBehaviour { - public List nodes = new List(); - public List roads = new List(); - public FlatBezierRenderer roadRenderer; - public Camera mainCamera; - private bool drawing = false; - public Material material; - public Config config; - - void Start() { - } - - private Node startNode; - - private void startRoad() { - Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out RaycastHit hit, Mathf.Infinity, 1 << 6); - foreach (Node node in nodes) { - if ((hit.point - node.position).magnitude < 1.0f) { - startNode = node; - drawing = true; - return; - } - } - Vector3 position = new Vector3(hit.point.x, 0.0f, hit.point.z); - startNode = new Node(position, transform, config); - nodes.Add(startNode); - drawing = true; - } - - private void endRoad() { - Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out RaycastHit hit, Mathf.Infinity, 1 << 6); - Node endNode = null; - foreach (Node node in nodes) { - if ((hit.point - node.position).magnitude < 1.0f) { - endNode = node; - break; - } - } - if (endNode == null) { - Vector3 position = new Vector3(hit.point.x, 0.0f, hit.point.z); - endNode = new Node(position, transform, config); - nodes.Add(endNode); - } - Road road = new Road(startNode, endNode, config); - if (!roads.Contains(road) && startNode != endNode) { - startNode.roads.Add(road); - endNode.roads.Add(road); - roads.Add(road); - road.initialize(transform); - road.update(true); - } - startNode = null; - drawing = false; - } - - Node pulling = null; - - void Update() { - Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); - RaycastHit hit; - if (config.mode == Mode.DrawRoad) { - if (Input.GetAxis("Fire1") != 0.0f) { - if (!drawing) { - startRoad(); - } - } else if (drawing) { - endRoad(); - } - } else if (config.mode == Mode.DeleteRoad) { - if (!drawing && Input.GetAxis("Fire1") != 0.0f) { - if (Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 8)) { - drawing = true; - Road road = hit.transform.gameObject.GetComponent().road; - roads.Remove(road); - foreach (Node node in road.nodes) { - node.roads.Remove(road); - node.update(); - } - Destroy(hit.transform.parent.gameObject); - } - } else if (drawing && Input.GetAxis("Fire1") == 0.0f) { - drawing = false; - } - } - if (Input.GetAxis("Fire2") != 0.0f) { - if (pulling == null && Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 7)) { - pulling = nodes.Find(node => node.gameObject == hit.transform.gameObject); - } - } else { - pulling = null; - } - if (pulling != null) { - Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 6); - Vector3 position = new Vector3(hit.point.x, 0.0f, hit.point.z); - pulling.pull(position); - } - } -} diff --git a/Assets/Scripts/Roads.cs.meta b/Assets/Scripts/Roads.cs.meta deleted file mode 100644 index f26a401..0000000 --- a/Assets/Scripts/Roads.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 7635c530aa8ef31498f100f4e7465bea -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Roads.meta b/Assets/Scripts/Roads.meta new file mode 100644 index 0000000..defbd46 --- /dev/null +++ b/Assets/Scripts/Roads.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: e6a3f10ad4786e44d9faf825f11a8e1d +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Roads/Node.cs b/Assets/Scripts/Roads/Node.cs new file mode 100644 index 0000000..3b2f31a --- /dev/null +++ b/Assets/Scripts/Roads/Node.cs @@ -0,0 +1,82 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Node { + private FlatCircleRenderer circle = new FlatCircleRenderer(0.2f, 0.05f, 32); + private FlatCircleRenderer fullCircle = new FlatCircleRenderer(0f, 1f, 32); + + public Vector3 position; + public List roads = new List(); + public GameObject gameObject; + + public Node(Vector3 position, Transform parent, Config config) { + gameObject = new GameObject(); + gameObject.transform.position = position; + gameObject.transform.parent = parent; + gameObject.AddComponent().radius = 2f; + gameObject.layer = 7; + gameObject.AddComponent().node = this; + + GameObject nodeCircle = new GameObject(); + nodeCircle.AddComponent().material = config.roadEditMaterial; + nodeCircle.AddComponent().mesh = circle.mesh; + nodeCircle.transform.parent = gameObject.transform; + nodeCircle.transform.localPosition = Vector3.zero; + + GameObject nodeRoad = new GameObject(); + nodeRoad.AddComponent().material = config.roadMaterial; + nodeRoad.AddComponent().mesh = fullCircle.mesh; + nodeRoad.transform.parent = gameObject.transform; + nodeRoad.transform.localPosition = Vector3.zero; + 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(); + } + + override public bool Equals(object other) { + if ((other == null) || ! this.GetType().Equals(other.GetType())) { + return false; + } + if (other == this) { + return true; + } + return ((Node)other).position.Equals(position); + } + + public void pull(Vector3 position) { + this.position = position; + gameObject.transform.position = position; + foreach (Road road in roads) { + 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/Roads/Node.cs.meta b/Assets/Scripts/Roads/Node.cs.meta new file mode 100644 index 0000000..8bbedf1 --- /dev/null +++ b/Assets/Scripts/Roads/Node.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 3cbed1dad7ec67d4dbe8602aea1e2979 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Roads/NodeData.cs b/Assets/Scripts/Roads/NodeData.cs new file mode 100644 index 0000000..9ee8ace --- /dev/null +++ b/Assets/Scripts/Roads/NodeData.cs @@ -0,0 +1,7 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class NodeData : MonoBehaviour { + public Node node = null; +} diff --git a/Assets/Scripts/Roads/NodeData.cs.meta b/Assets/Scripts/Roads/NodeData.cs.meta new file mode 100644 index 0000000..73dcf56 --- /dev/null +++ b/Assets/Scripts/Roads/NodeData.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4cf9bcd8c4889de4985e5d6a776acc5b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Roads/Road.cs b/Assets/Scripts/Roads/Road.cs new file mode 100644 index 0000000..1ad1a74 --- /dev/null +++ b/Assets/Scripts/Roads/Road.cs @@ -0,0 +1,88 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Road { + public List nodes = new List(); + Bezier path; + private FlatBezierRenderer pathLine, roadBody; + private Config config; + private MeshCollider collider; + public GameObject gameObject; + + public Road(Node start, Node end, Config config) { + nodes.Add(start); + nodes.Add(end); + path = new Bezier(); + pathLine = new FlatBezierRenderer(path, 50, 0.05f); + roadBody = new FlatBezierRenderer(path, 50, 2f); + this.config = config; + } + + + public void initialize(Transform parent) { + gameObject = new GameObject(); + gameObject.transform.parent = parent; + gameObject.transform.position = Vector3.zero; + + GameObject lineChild = new GameObject(); + lineChild.transform.position = Vector3.zero; + lineChild.AddComponent().material = config.roadEditMaterial; + lineChild.AddComponent().mesh = pathLine.mesh; + lineChild.transform.parent = gameObject.transform; + + GameObject bodyChild = new GameObject(); + bodyChild.AddComponent().material = config.roadMaterial; + bodyChild.AddComponent().mesh = roadBody.mesh; + bodyChild.AddComponent().road = this; + collider = bodyChild.AddComponent(); + bodyChild.transform.parent = gameObject.transform; + bodyChild.layer = 8; + update(true); + } + + public void update(bool updateOthers) { + path.A = nodes[0].position; + path.D = nodes[1].position; + if (nodes[0].roads.Count == 2) { + path.B = nodes[0].position + + 0.25f * (nodes[1].position - nodes[0].getOther(nodes[1]).position); + } else { + path.B = 0.5f * (nodes[0].position + nodes[1].position); + } + if (nodes[1].roads.Count == 2) { + path.C = nodes[1].position + + 0.25f * (nodes[0].position - nodes[1].getOther(nodes[0]).position); + } else { + path.C = 0.5f * (nodes[0].position + nodes[1].position); + } + if (updateOthers) { + foreach (Node node in nodes) { + node.lateUpdate(this); + } + } + pathLine.update(); + roadBody.update(); + collider.sharedMesh = roadBody.mesh; + } + + override public bool Equals(object other) { + if (other == null || !(other is Road)) { + return false; + } + if (other == this) { + return true; + } + Road otherRoad = (Road) other; + foreach (Node node in nodes) { + if (! otherRoad.nodes.Contains(node)) { + return false; + } + } + return true; + } + + public override int GetHashCode() { + return nodes[0].GetHashCode() << 16 | nodes[1].GetHashCode(); + } +} diff --git a/Assets/Scripts/Roads/Road.cs.meta b/Assets/Scripts/Roads/Road.cs.meta new file mode 100644 index 0000000..24dc94b --- /dev/null +++ b/Assets/Scripts/Roads/Road.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0ab512a631c0cc4468e77b6a2dfeb31f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Bezier.cs b/Assets/Scripts/Bezier.cs deleted file mode 100644 index ea19006..0000000 --- a/Assets/Scripts/Bezier.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Bezier { - public Vector3 A, B, C, D; - - public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { - this.A = A; - this.B = B; - this.C = C; - this.D = D; - } - - public Bezier() { - this.A = Vector3.zero; - this.B = Vector3.zero; - this.C = Vector3.zero; - this.D = Vector3.zero; - } - - public Vector3 getPosition(float t) { - float T = 1.0f - t; - return - A * ( T*T*T) + - B * (3*T*T*t) + - C * (3*T*t*t) + - D * ( t*t*t); - } - - public Vector3 getDirection(float t) { - float T = 1.0f - t; - return - A * (-3*T*T ) + - B * ( 3*T*T - 6*T*t) + - C * (-3*t*t + 6*T*t) + - D * ( 3*t*t ); - } -} diff --git a/Assets/Scripts/Bezier.cs.meta b/Assets/Scripts/Bezier.cs.meta deleted file mode 100644 index 14630e2..0000000 --- a/Assets/Scripts/Bezier.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 284cf9d52e385aa4ebba97eb8b43b8a6 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatBezierRenderer.cs b/Assets/Scripts/FlatBezierRenderer.cs deleted file mode 100644 index b8dad15..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatBezierRenderer { - private int percision; - private float width; - public Bezier bezier; - public Mesh mesh = new Mesh(); - public FlatBezierRenderer(Bezier bezier, int percision, float width) { - this.percision = percision; - this.width = width; - this.bezier = bezier; - update(); - } - - public void update() { - float realWidth = width / 2; - int vertexCount = 6 * (percision+1); - Vector3[] vertices = new Vector3[vertexCount / 3]; - int[] indices = new int[vertexCount]; - Vector3 position = bezier.getPosition(0.0f); - Vector3 tangent = getTangent(0.0f); - vertices[0] = position + tangent * realWidth; - vertices[1] = position - tangent * realWidth; - for (int i = 1; i <= percision; i++) { - float t = (float) i / percision; - indices[6*i ] = 2*i ; - indices[6*i+1] = 2*i-1; - indices[6*i+2] = 2*i-2; - indices[6*i+3] = 2*i+1; - indices[6*i+4] = 2*i-1; - indices[6*i+5] = 2*i ; - - position = bezier.getPosition(t); - tangent = getTangent(t); - vertices[2*i ] = position + tangent * realWidth; - vertices[2*i+1] = position - tangent * realWidth; - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - mesh.Optimize(); - } - - private Vector3 getTangent(float t) { - Vector3 direction = bezier.getDirection(t); - direction.Normalize(); - return new Vector3(-direction.z, 0, direction.x); - } -} diff --git a/Assets/Scripts/FlatBezierRenderer.cs.meta b/Assets/Scripts/FlatBezierRenderer.cs.meta deleted file mode 100644 index 1fe6547..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 9bfb303c44d15ef468dc0d7334241e94 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatCircleRenderer.cs b/Assets/Scripts/FlatCircleRenderer.cs deleted file mode 100644 index 363b663..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatCircleRenderer { - public Mesh mesh = new Mesh(); - public float radius, width; - private int segments; - - public FlatCircleRenderer(float radius, float width, int segments) { - this.radius = radius; - this.width = width; - this.segments = segments; - update(); - } - - public void update() { - Vector3[] vertices = new Vector3[2*segments+6]; - int[] indices = new int[6*segments]; - for (int i = 0; i < segments; i++) { - float alpha = (float) i / segments * 2 * Mathf.PI; - indices[6*i ] = 2*i ; - indices[6*i+3] = 2*i ; - indices[6*i+5] = 2*i+1; - if (i == 0) { - indices[6*i+1] = 2*segments-2; - indices[6*i+2] = 2*segments-1; - indices[6*i+4] = 2*segments-1; - } else { - indices[6*i+1] = 2*i-2; - indices[6*i+2] = 2*i-1; - indices[6*i+4] = 2*i-1; - } - Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); - vertices[2*i ] = direction * radius; - vertices[2*i+1] = direction * (radius + width); - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - } -} diff --git a/Assets/Scripts/FlatCircleRenderer.cs.meta b/Assets/Scripts/FlatCircleRenderer.cs.meta deleted file mode 100644 index d10c238..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0fe81c7924a85b142a4a86201b08c241 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Node.cs b/Assets/Scripts/Node.cs deleted file mode 100644 index e3c4cbd..0000000 --- a/Assets/Scripts/Node.cs +++ /dev/null @@ -1,81 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Node { - public FlatCircleRenderer circle = new FlatCircleRenderer(0.2f, 0.05f, 32); - public FlatCircleRenderer fullCircle = new FlatCircleRenderer(0f, 1f, 32); - - public Vector3 position; - public List roads = new List(); - public GameObject gameObject; - - public Node(Vector3 position, Transform parent, Config config) { - gameObject = new GameObject(); - gameObject.transform.position = position; - gameObject.transform.parent = parent; - gameObject.AddComponent().radius = 0.5f; - gameObject.layer = 7; - - GameObject nodeCircle = new GameObject(); - nodeCircle.AddComponent().material = config.roadEditMaterial; - nodeCircle.AddComponent().mesh = circle.mesh; - nodeCircle.transform.parent = gameObject.transform; - nodeCircle.transform.localPosition = Vector3.zero; - - GameObject nodeRoad = new GameObject(); - nodeRoad.AddComponent().material = config.roadMaterial; - nodeRoad.AddComponent().mesh = fullCircle.mesh; - nodeRoad.transform.parent = gameObject.transform; - nodeRoad.transform.localPosition = Vector3.zero; - 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(); - } - - override public bool Equals(object other) { - if ((other == null) || ! this.GetType().Equals(other.GetType())) { - return false; - } - if (other == this) { - return true; - } - return ((Node)other).position.Equals(position); - } - - public void pull(Vector3 position) { - this.position = position; - gameObject.transform.position = position; - foreach (Road road in roads) { - 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/Node.cs.meta b/Assets/Scripts/Node.cs.meta deleted file mode 100644 index 8bbedf1..0000000 --- a/Assets/Scripts/Node.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 3cbed1dad7ec67d4dbe8602aea1e2979 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Primitives.meta b/Assets/Scripts/Primitives.meta new file mode 100644 index 0000000..0cf8895 --- /dev/null +++ b/Assets/Scripts/Primitives.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 6d1a8c7f33189a240b4d749356f53399 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/Bezier.cs b/Assets/Scripts/Primitives/Bezier.cs new file mode 100644 index 0000000..ea19006 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs @@ -0,0 +1,39 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Bezier { + public Vector3 A, B, C, D; + + public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { + this.A = A; + this.B = B; + this.C = C; + this.D = D; + } + + public Bezier() { + this.A = Vector3.zero; + this.B = Vector3.zero; + this.C = Vector3.zero; + this.D = Vector3.zero; + } + + public Vector3 getPosition(float t) { + float T = 1.0f - t; + return + A * ( T*T*T) + + B * (3*T*T*t) + + C * (3*T*t*t) + + D * ( t*t*t); + } + + public Vector3 getDirection(float t) { + float T = 1.0f - t; + return + A * (-3*T*T ) + + B * ( 3*T*T - 6*T*t) + + C * (-3*t*t + 6*T*t) + + D * ( 3*t*t ); + } +} diff --git a/Assets/Scripts/Primitives/Bezier.cs.meta b/Assets/Scripts/Primitives/Bezier.cs.meta new file mode 100644 index 0000000..14630e2 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 284cf9d52e385aa4ebba97eb8b43b8a6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs b/Assets/Scripts/Primitives/FlatBezierRenderer.cs new file mode 100644 index 0000000..b8dad15 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs @@ -0,0 +1,51 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FlatBezierRenderer { + private int percision; + private float width; + public Bezier bezier; + public Mesh mesh = new Mesh(); + public FlatBezierRenderer(Bezier bezier, int percision, float width) { + this.percision = percision; + this.width = width; + this.bezier = bezier; + update(); + } + + public void update() { + float realWidth = width / 2; + int vertexCount = 6 * (percision+1); + Vector3[] vertices = new Vector3[vertexCount / 3]; + int[] indices = new int[vertexCount]; + Vector3 position = bezier.getPosition(0.0f); + Vector3 tangent = getTangent(0.0f); + vertices[0] = position + tangent * realWidth; + vertices[1] = position - tangent * realWidth; + for (int i = 1; i <= percision; i++) { + float t = (float) i / percision; + indices[6*i ] = 2*i ; + indices[6*i+1] = 2*i-1; + indices[6*i+2] = 2*i-2; + indices[6*i+3] = 2*i+1; + indices[6*i+4] = 2*i-1; + indices[6*i+5] = 2*i ; + + position = bezier.getPosition(t); + tangent = getTangent(t); + vertices[2*i ] = position + tangent * realWidth; + vertices[2*i+1] = position - tangent * realWidth; + } + mesh.Clear(); + mesh.vertices = vertices; + mesh.triangles = indices; + mesh.Optimize(); + } + + private Vector3 getTangent(float t) { + Vector3 direction = bezier.getDirection(t); + direction.Normalize(); + return new Vector3(-direction.z, 0, direction.x); + } +} diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta b/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta new file mode 100644 index 0000000..1fe6547 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9bfb303c44d15ef468dc0d7334241e94 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/FlatCircleRenderer.cs b/Assets/Scripts/Primitives/FlatCircleRenderer.cs new file mode 100644 index 0000000..363b663 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatCircleRenderer.cs @@ -0,0 +1,42 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FlatCircleRenderer { + public Mesh mesh = new Mesh(); + public float radius, width; + private int segments; + + public FlatCircleRenderer(float radius, float width, int segments) { + this.radius = radius; + this.width = width; + this.segments = segments; + update(); + } + + public void update() { + Vector3[] vertices = new Vector3[2*segments+6]; + int[] indices = new int[6*segments]; + for (int i = 0; i < segments; i++) { + float alpha = (float) i / segments * 2 * Mathf.PI; + indices[6*i ] = 2*i ; + indices[6*i+3] = 2*i ; + indices[6*i+5] = 2*i+1; + if (i == 0) { + indices[6*i+1] = 2*segments-2; + indices[6*i+2] = 2*segments-1; + indices[6*i+4] = 2*segments-1; + } else { + indices[6*i+1] = 2*i-2; + indices[6*i+2] = 2*i-1; + indices[6*i+4] = 2*i-1; + } + Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); + vertices[2*i ] = direction * radius; + vertices[2*i+1] = direction * (radius + width); + } + mesh.Clear(); + mesh.vertices = vertices; + mesh.triangles = indices; + } +} diff --git a/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta b/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta new file mode 100644 index 0000000..d10c238 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0fe81c7924a85b142a4a86201b08c241 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Road.cs b/Assets/Scripts/Road.cs deleted file mode 100644 index 0af2218..0000000 --- a/Assets/Scripts/Road.cs +++ /dev/null @@ -1,85 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Road { - public List nodes = new List(); - Bezier path; - FlatBezierRenderer pathLine, roadBody; - Config config; - MeshCollider collider; - - public Road(Node start, Node end, Config config) { - nodes.Add(start); - nodes.Add(end); - path = new Bezier(); - pathLine = new FlatBezierRenderer(path, 50, 0.05f); - roadBody = new FlatBezierRenderer(path, 50, 2f); - this.config = config; - } - - - public void initialize(Transform parent) { - GameObject child = new GameObject(); - child.transform.parent = parent; - GameObject lineChild = new GameObject(); - lineChild.transform.position = Vector3.zero; - lineChild.AddComponent().material = config.roadEditMaterial; - lineChild.AddComponent().mesh = pathLine.mesh; - lineChild.transform.parent = child.transform; - - GameObject roadChild = new GameObject(); - collider = roadChild.AddComponent(); - roadChild.transform.position = Vector3.zero; - roadChild.AddComponent().material = config.roadMaterial; - roadChild.transform.parent = child.transform; - roadChild.layer = 8; - roadChild.AddComponent().mesh = roadBody.mesh; - roadChild.AddComponent().road = this; - } - - public void update(bool updateOthers) { - path.A = nodes[0].position; - path.D = nodes[1].position; - if (nodes[0].roads.Count == 2) { - path.B = nodes[0].position + - 0.25f * (nodes[1].position - nodes[0].getOther(nodes[1]).position); - } else { - path.B = 0.5f * (nodes[0].position + nodes[1].position); - } - if (nodes[1].roads.Count == 2) { - path.C = nodes[1].position + - 0.25f * (nodes[0].position - nodes[1].getOther(nodes[0]).position); - } else { - path.C = 0.5f * (nodes[0].position + nodes[1].position); - } - if (updateOthers) { - foreach (Node node in nodes) { - node.lateUpdate(this); - } - } - pathLine.update(); - roadBody.update(); - collider.sharedMesh = roadBody.mesh; - } - - override public bool Equals(object other) { - if (other == null || !(other is Road)) { - return false; - } - if (other == this) { - return true; - } - Road otherRoad = (Road) other; - foreach (Node node in nodes) { - if (! otherRoad.nodes.Contains(node)) { - return false; - } - } - return true; - } - - public override int GetHashCode() { - return nodes[0].GetHashCode() << 16 | nodes[1].GetHashCode(); - } -} diff --git a/Assets/Scripts/Road.cs.meta b/Assets/Scripts/Road.cs.meta deleted file mode 100644 index 24dc94b..0000000 --- a/Assets/Scripts/Road.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0ab512a631c0cc4468e77b6a2dfeb31f -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/RoadData.cs b/Assets/Scripts/RoadData.cs deleted file mode 100644 index eb620ba..0000000 --- a/Assets/Scripts/RoadData.cs +++ /dev/null @@ -1,7 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class RoadData : MonoBehaviour { - public Road road; -} diff --git a/Assets/Scripts/RoadData.cs.meta b/Assets/Scripts/RoadData.cs.meta deleted file mode 100644 index 2948b97..0000000 --- a/Assets/Scripts/RoadData.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 6bfac654eff68754c88b877a9b88ae42 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/RoadEdit.cs b/Assets/Scripts/RoadEdit.cs deleted file mode 100644 index a6cc73e..0000000 --- a/Assets/Scripts/RoadEdit.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class RoadEdit : MonoBehaviour { - public GameObject start; - public GameObject startDirection; - public GameObject end; - public GameObject endDirection; - public Camera mainCamera; - - private GameObject currentlyPulling = null; - public Bezier bezier; - private FlatBezierRenderer pathRenderer; - public Material highlight, noHighlight; - - void Start() { - } - - void updateRoad(Vector3 position) { - } - - void Update() { - Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); - if (Input.GetAxis("Fire1") != 0.0f) { - if (currentlyPulling == null && Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, 1 << 7)) { - currentlyPulling = hit.transform.gameObject; - currentlyPulling.GetComponent().material = highlight; - } - } else { - if (currentlyPulling != null) { - currentlyPulling.GetComponent().material = noHighlight; - } - currentlyPulling = null; - } - if (currentlyPulling != null) { - Physics.Raycast(ray, out RaycastHit hit_, Mathf.Infinity, 1 << 6); - Vector3 position = new Vector3(hit_.point.x, 0.0f, hit_.point.z); - currentlyPulling.transform.position = position; - updateRoad(position); - } - } -} diff --git a/Assets/Scripts/RoadEdit.cs.meta b/Assets/Scripts/RoadEdit.cs.meta deleted file mode 100644 index 4e09d01..0000000 --- a/Assets/Scripts/RoadEdit.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 68e9412fa432f2e468d5d7447e99fd89 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Roads.cs b/Assets/Scripts/Roads.cs deleted file mode 100644 index acd376a..0000000 --- a/Assets/Scripts/Roads.cs +++ /dev/null @@ -1,102 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Roads : MonoBehaviour { - public List nodes = new List(); - public List roads = new List(); - public FlatBezierRenderer roadRenderer; - public Camera mainCamera; - private bool drawing = false; - public Material material; - public Config config; - - void Start() { - } - - private Node startNode; - - private void startRoad() { - Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out RaycastHit hit, Mathf.Infinity, 1 << 6); - foreach (Node node in nodes) { - if ((hit.point - node.position).magnitude < 1.0f) { - startNode = node; - drawing = true; - return; - } - } - Vector3 position = new Vector3(hit.point.x, 0.0f, hit.point.z); - startNode = new Node(position, transform, config); - nodes.Add(startNode); - drawing = true; - } - - private void endRoad() { - Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out RaycastHit hit, Mathf.Infinity, 1 << 6); - Node endNode = null; - foreach (Node node in nodes) { - if ((hit.point - node.position).magnitude < 1.0f) { - endNode = node; - break; - } - } - if (endNode == null) { - Vector3 position = new Vector3(hit.point.x, 0.0f, hit.point.z); - endNode = new Node(position, transform, config); - nodes.Add(endNode); - } - Road road = new Road(startNode, endNode, config); - if (!roads.Contains(road) && startNode != endNode) { - startNode.roads.Add(road); - endNode.roads.Add(road); - roads.Add(road); - road.initialize(transform); - road.update(true); - } - startNode = null; - drawing = false; - } - - Node pulling = null; - - void Update() { - Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); - RaycastHit hit; - if (config.mode == Mode.DrawRoad) { - if (Input.GetAxis("Fire1") != 0.0f) { - if (!drawing) { - startRoad(); - } - } else if (drawing) { - endRoad(); - } - } else if (config.mode == Mode.DeleteRoad) { - if (!drawing && Input.GetAxis("Fire1") != 0.0f) { - if (Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 8)) { - drawing = true; - Road road = hit.transform.gameObject.GetComponent().road; - roads.Remove(road); - foreach (Node node in road.nodes) { - node.roads.Remove(road); - node.update(); - } - Destroy(hit.transform.parent.gameObject); - } - } else if (drawing && Input.GetAxis("Fire1") == 0.0f) { - drawing = false; - } - } - if (Input.GetAxis("Fire2") != 0.0f) { - if (pulling == null && Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 7)) { - pulling = nodes.Find(node => node.gameObject == hit.transform.gameObject); - } - } else { - pulling = null; - } - if (pulling != null) { - Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 6); - Vector3 position = new Vector3(hit.point.x, 0.0f, hit.point.z); - pulling.pull(position); - } - } -} diff --git a/Assets/Scripts/Roads.cs.meta b/Assets/Scripts/Roads.cs.meta deleted file mode 100644 index f26a401..0000000 --- a/Assets/Scripts/Roads.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 7635c530aa8ef31498f100f4e7465bea -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Roads.meta b/Assets/Scripts/Roads.meta new file mode 100644 index 0000000..defbd46 --- /dev/null +++ b/Assets/Scripts/Roads.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: e6a3f10ad4786e44d9faf825f11a8e1d +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Roads/Node.cs b/Assets/Scripts/Roads/Node.cs new file mode 100644 index 0000000..3b2f31a --- /dev/null +++ b/Assets/Scripts/Roads/Node.cs @@ -0,0 +1,82 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Node { + private FlatCircleRenderer circle = new FlatCircleRenderer(0.2f, 0.05f, 32); + private FlatCircleRenderer fullCircle = new FlatCircleRenderer(0f, 1f, 32); + + public Vector3 position; + public List roads = new List(); + public GameObject gameObject; + + public Node(Vector3 position, Transform parent, Config config) { + gameObject = new GameObject(); + gameObject.transform.position = position; + gameObject.transform.parent = parent; + gameObject.AddComponent().radius = 2f; + gameObject.layer = 7; + gameObject.AddComponent().node = this; + + GameObject nodeCircle = new GameObject(); + nodeCircle.AddComponent().material = config.roadEditMaterial; + nodeCircle.AddComponent().mesh = circle.mesh; + nodeCircle.transform.parent = gameObject.transform; + nodeCircle.transform.localPosition = Vector3.zero; + + GameObject nodeRoad = new GameObject(); + nodeRoad.AddComponent().material = config.roadMaterial; + nodeRoad.AddComponent().mesh = fullCircle.mesh; + nodeRoad.transform.parent = gameObject.transform; + nodeRoad.transform.localPosition = Vector3.zero; + 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(); + } + + override public bool Equals(object other) { + if ((other == null) || ! this.GetType().Equals(other.GetType())) { + return false; + } + if (other == this) { + return true; + } + return ((Node)other).position.Equals(position); + } + + public void pull(Vector3 position) { + this.position = position; + gameObject.transform.position = position; + foreach (Road road in roads) { + 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/Roads/Node.cs.meta b/Assets/Scripts/Roads/Node.cs.meta new file mode 100644 index 0000000..8bbedf1 --- /dev/null +++ b/Assets/Scripts/Roads/Node.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 3cbed1dad7ec67d4dbe8602aea1e2979 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Roads/NodeData.cs b/Assets/Scripts/Roads/NodeData.cs new file mode 100644 index 0000000..9ee8ace --- /dev/null +++ b/Assets/Scripts/Roads/NodeData.cs @@ -0,0 +1,7 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class NodeData : MonoBehaviour { + public Node node = null; +} diff --git a/Assets/Scripts/Roads/NodeData.cs.meta b/Assets/Scripts/Roads/NodeData.cs.meta new file mode 100644 index 0000000..73dcf56 --- /dev/null +++ b/Assets/Scripts/Roads/NodeData.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4cf9bcd8c4889de4985e5d6a776acc5b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Roads/Road.cs b/Assets/Scripts/Roads/Road.cs new file mode 100644 index 0000000..1ad1a74 --- /dev/null +++ b/Assets/Scripts/Roads/Road.cs @@ -0,0 +1,88 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Road { + public List nodes = new List(); + Bezier path; + private FlatBezierRenderer pathLine, roadBody; + private Config config; + private MeshCollider collider; + public GameObject gameObject; + + public Road(Node start, Node end, Config config) { + nodes.Add(start); + nodes.Add(end); + path = new Bezier(); + pathLine = new FlatBezierRenderer(path, 50, 0.05f); + roadBody = new FlatBezierRenderer(path, 50, 2f); + this.config = config; + } + + + public void initialize(Transform parent) { + gameObject = new GameObject(); + gameObject.transform.parent = parent; + gameObject.transform.position = Vector3.zero; + + GameObject lineChild = new GameObject(); + lineChild.transform.position = Vector3.zero; + lineChild.AddComponent().material = config.roadEditMaterial; + lineChild.AddComponent().mesh = pathLine.mesh; + lineChild.transform.parent = gameObject.transform; + + GameObject bodyChild = new GameObject(); + bodyChild.AddComponent().material = config.roadMaterial; + bodyChild.AddComponent().mesh = roadBody.mesh; + bodyChild.AddComponent().road = this; + collider = bodyChild.AddComponent(); + bodyChild.transform.parent = gameObject.transform; + bodyChild.layer = 8; + update(true); + } + + public void update(bool updateOthers) { + path.A = nodes[0].position; + path.D = nodes[1].position; + if (nodes[0].roads.Count == 2) { + path.B = nodes[0].position + + 0.25f * (nodes[1].position - nodes[0].getOther(nodes[1]).position); + } else { + path.B = 0.5f * (nodes[0].position + nodes[1].position); + } + if (nodes[1].roads.Count == 2) { + path.C = nodes[1].position + + 0.25f * (nodes[0].position - nodes[1].getOther(nodes[0]).position); + } else { + path.C = 0.5f * (nodes[0].position + nodes[1].position); + } + if (updateOthers) { + foreach (Node node in nodes) { + node.lateUpdate(this); + } + } + pathLine.update(); + roadBody.update(); + collider.sharedMesh = roadBody.mesh; + } + + override public bool Equals(object other) { + if (other == null || !(other is Road)) { + return false; + } + if (other == this) { + return true; + } + Road otherRoad = (Road) other; + foreach (Node node in nodes) { + if (! otherRoad.nodes.Contains(node)) { + return false; + } + } + return true; + } + + public override int GetHashCode() { + return nodes[0].GetHashCode() << 16 | nodes[1].GetHashCode(); + } +} diff --git a/Assets/Scripts/Roads/Road.cs.meta b/Assets/Scripts/Roads/Road.cs.meta new file mode 100644 index 0000000..24dc94b --- /dev/null +++ b/Assets/Scripts/Roads/Road.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0ab512a631c0cc4468e77b6a2dfeb31f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Roads/RoadData.cs b/Assets/Scripts/Roads/RoadData.cs new file mode 100644 index 0000000..eb620ba --- /dev/null +++ b/Assets/Scripts/Roads/RoadData.cs @@ -0,0 +1,7 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class RoadData : MonoBehaviour { + public Road road; +} diff --git a/Assets/Scripts/Bezier.cs b/Assets/Scripts/Bezier.cs deleted file mode 100644 index ea19006..0000000 --- a/Assets/Scripts/Bezier.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Bezier { - public Vector3 A, B, C, D; - - public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { - this.A = A; - this.B = B; - this.C = C; - this.D = D; - } - - public Bezier() { - this.A = Vector3.zero; - this.B = Vector3.zero; - this.C = Vector3.zero; - this.D = Vector3.zero; - } - - public Vector3 getPosition(float t) { - float T = 1.0f - t; - return - A * ( T*T*T) + - B * (3*T*T*t) + - C * (3*T*t*t) + - D * ( t*t*t); - } - - public Vector3 getDirection(float t) { - float T = 1.0f - t; - return - A * (-3*T*T ) + - B * ( 3*T*T - 6*T*t) + - C * (-3*t*t + 6*T*t) + - D * ( 3*t*t ); - } -} diff --git a/Assets/Scripts/Bezier.cs.meta b/Assets/Scripts/Bezier.cs.meta deleted file mode 100644 index 14630e2..0000000 --- a/Assets/Scripts/Bezier.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 284cf9d52e385aa4ebba97eb8b43b8a6 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatBezierRenderer.cs b/Assets/Scripts/FlatBezierRenderer.cs deleted file mode 100644 index b8dad15..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatBezierRenderer { - private int percision; - private float width; - public Bezier bezier; - public Mesh mesh = new Mesh(); - public FlatBezierRenderer(Bezier bezier, int percision, float width) { - this.percision = percision; - this.width = width; - this.bezier = bezier; - update(); - } - - public void update() { - float realWidth = width / 2; - int vertexCount = 6 * (percision+1); - Vector3[] vertices = new Vector3[vertexCount / 3]; - int[] indices = new int[vertexCount]; - Vector3 position = bezier.getPosition(0.0f); - Vector3 tangent = getTangent(0.0f); - vertices[0] = position + tangent * realWidth; - vertices[1] = position - tangent * realWidth; - for (int i = 1; i <= percision; i++) { - float t = (float) i / percision; - indices[6*i ] = 2*i ; - indices[6*i+1] = 2*i-1; - indices[6*i+2] = 2*i-2; - indices[6*i+3] = 2*i+1; - indices[6*i+4] = 2*i-1; - indices[6*i+5] = 2*i ; - - position = bezier.getPosition(t); - tangent = getTangent(t); - vertices[2*i ] = position + tangent * realWidth; - vertices[2*i+1] = position - tangent * realWidth; - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - mesh.Optimize(); - } - - private Vector3 getTangent(float t) { - Vector3 direction = bezier.getDirection(t); - direction.Normalize(); - return new Vector3(-direction.z, 0, direction.x); - } -} diff --git a/Assets/Scripts/FlatBezierRenderer.cs.meta b/Assets/Scripts/FlatBezierRenderer.cs.meta deleted file mode 100644 index 1fe6547..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 9bfb303c44d15ef468dc0d7334241e94 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatCircleRenderer.cs b/Assets/Scripts/FlatCircleRenderer.cs deleted file mode 100644 index 363b663..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatCircleRenderer { - public Mesh mesh = new Mesh(); - public float radius, width; - private int segments; - - public FlatCircleRenderer(float radius, float width, int segments) { - this.radius = radius; - this.width = width; - this.segments = segments; - update(); - } - - public void update() { - Vector3[] vertices = new Vector3[2*segments+6]; - int[] indices = new int[6*segments]; - for (int i = 0; i < segments; i++) { - float alpha = (float) i / segments * 2 * Mathf.PI; - indices[6*i ] = 2*i ; - indices[6*i+3] = 2*i ; - indices[6*i+5] = 2*i+1; - if (i == 0) { - indices[6*i+1] = 2*segments-2; - indices[6*i+2] = 2*segments-1; - indices[6*i+4] = 2*segments-1; - } else { - indices[6*i+1] = 2*i-2; - indices[6*i+2] = 2*i-1; - indices[6*i+4] = 2*i-1; - } - Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); - vertices[2*i ] = direction * radius; - vertices[2*i+1] = direction * (radius + width); - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - } -} diff --git a/Assets/Scripts/FlatCircleRenderer.cs.meta b/Assets/Scripts/FlatCircleRenderer.cs.meta deleted file mode 100644 index d10c238..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0fe81c7924a85b142a4a86201b08c241 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Node.cs b/Assets/Scripts/Node.cs deleted file mode 100644 index e3c4cbd..0000000 --- a/Assets/Scripts/Node.cs +++ /dev/null @@ -1,81 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Node { - public FlatCircleRenderer circle = new FlatCircleRenderer(0.2f, 0.05f, 32); - public FlatCircleRenderer fullCircle = new FlatCircleRenderer(0f, 1f, 32); - - public Vector3 position; - public List roads = new List(); - public GameObject gameObject; - - public Node(Vector3 position, Transform parent, Config config) { - gameObject = new GameObject(); - gameObject.transform.position = position; - gameObject.transform.parent = parent; - gameObject.AddComponent().radius = 0.5f; - gameObject.layer = 7; - - GameObject nodeCircle = new GameObject(); - nodeCircle.AddComponent().material = config.roadEditMaterial; - nodeCircle.AddComponent().mesh = circle.mesh; - nodeCircle.transform.parent = gameObject.transform; - nodeCircle.transform.localPosition = Vector3.zero; - - GameObject nodeRoad = new GameObject(); - nodeRoad.AddComponent().material = config.roadMaterial; - nodeRoad.AddComponent().mesh = fullCircle.mesh; - nodeRoad.transform.parent = gameObject.transform; - nodeRoad.transform.localPosition = Vector3.zero; - 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(); - } - - override public bool Equals(object other) { - if ((other == null) || ! this.GetType().Equals(other.GetType())) { - return false; - } - if (other == this) { - return true; - } - return ((Node)other).position.Equals(position); - } - - public void pull(Vector3 position) { - this.position = position; - gameObject.transform.position = position; - foreach (Road road in roads) { - 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/Node.cs.meta b/Assets/Scripts/Node.cs.meta deleted file mode 100644 index 8bbedf1..0000000 --- a/Assets/Scripts/Node.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 3cbed1dad7ec67d4dbe8602aea1e2979 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Primitives.meta b/Assets/Scripts/Primitives.meta new file mode 100644 index 0000000..0cf8895 --- /dev/null +++ b/Assets/Scripts/Primitives.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 6d1a8c7f33189a240b4d749356f53399 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/Bezier.cs b/Assets/Scripts/Primitives/Bezier.cs new file mode 100644 index 0000000..ea19006 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs @@ -0,0 +1,39 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Bezier { + public Vector3 A, B, C, D; + + public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { + this.A = A; + this.B = B; + this.C = C; + this.D = D; + } + + public Bezier() { + this.A = Vector3.zero; + this.B = Vector3.zero; + this.C = Vector3.zero; + this.D = Vector3.zero; + } + + public Vector3 getPosition(float t) { + float T = 1.0f - t; + return + A * ( T*T*T) + + B * (3*T*T*t) + + C * (3*T*t*t) + + D * ( t*t*t); + } + + public Vector3 getDirection(float t) { + float T = 1.0f - t; + return + A * (-3*T*T ) + + B * ( 3*T*T - 6*T*t) + + C * (-3*t*t + 6*T*t) + + D * ( 3*t*t ); + } +} diff --git a/Assets/Scripts/Primitives/Bezier.cs.meta b/Assets/Scripts/Primitives/Bezier.cs.meta new file mode 100644 index 0000000..14630e2 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 284cf9d52e385aa4ebba97eb8b43b8a6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs b/Assets/Scripts/Primitives/FlatBezierRenderer.cs new file mode 100644 index 0000000..b8dad15 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs @@ -0,0 +1,51 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FlatBezierRenderer { + private int percision; + private float width; + public Bezier bezier; + public Mesh mesh = new Mesh(); + public FlatBezierRenderer(Bezier bezier, int percision, float width) { + this.percision = percision; + this.width = width; + this.bezier = bezier; + update(); + } + + public void update() { + float realWidth = width / 2; + int vertexCount = 6 * (percision+1); + Vector3[] vertices = new Vector3[vertexCount / 3]; + int[] indices = new int[vertexCount]; + Vector3 position = bezier.getPosition(0.0f); + Vector3 tangent = getTangent(0.0f); + vertices[0] = position + tangent * realWidth; + vertices[1] = position - tangent * realWidth; + for (int i = 1; i <= percision; i++) { + float t = (float) i / percision; + indices[6*i ] = 2*i ; + indices[6*i+1] = 2*i-1; + indices[6*i+2] = 2*i-2; + indices[6*i+3] = 2*i+1; + indices[6*i+4] = 2*i-1; + indices[6*i+5] = 2*i ; + + position = bezier.getPosition(t); + tangent = getTangent(t); + vertices[2*i ] = position + tangent * realWidth; + vertices[2*i+1] = position - tangent * realWidth; + } + mesh.Clear(); + mesh.vertices = vertices; + mesh.triangles = indices; + mesh.Optimize(); + } + + private Vector3 getTangent(float t) { + Vector3 direction = bezier.getDirection(t); + direction.Normalize(); + return new Vector3(-direction.z, 0, direction.x); + } +} diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta b/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta new file mode 100644 index 0000000..1fe6547 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9bfb303c44d15ef468dc0d7334241e94 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/FlatCircleRenderer.cs b/Assets/Scripts/Primitives/FlatCircleRenderer.cs new file mode 100644 index 0000000..363b663 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatCircleRenderer.cs @@ -0,0 +1,42 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FlatCircleRenderer { + public Mesh mesh = new Mesh(); + public float radius, width; + private int segments; + + public FlatCircleRenderer(float radius, float width, int segments) { + this.radius = radius; + this.width = width; + this.segments = segments; + update(); + } + + public void update() { + Vector3[] vertices = new Vector3[2*segments+6]; + int[] indices = new int[6*segments]; + for (int i = 0; i < segments; i++) { + float alpha = (float) i / segments * 2 * Mathf.PI; + indices[6*i ] = 2*i ; + indices[6*i+3] = 2*i ; + indices[6*i+5] = 2*i+1; + if (i == 0) { + indices[6*i+1] = 2*segments-2; + indices[6*i+2] = 2*segments-1; + indices[6*i+4] = 2*segments-1; + } else { + indices[6*i+1] = 2*i-2; + indices[6*i+2] = 2*i-1; + indices[6*i+4] = 2*i-1; + } + Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); + vertices[2*i ] = direction * radius; + vertices[2*i+1] = direction * (radius + width); + } + mesh.Clear(); + mesh.vertices = vertices; + mesh.triangles = indices; + } +} diff --git a/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta b/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta new file mode 100644 index 0000000..d10c238 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0fe81c7924a85b142a4a86201b08c241 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Road.cs b/Assets/Scripts/Road.cs deleted file mode 100644 index 0af2218..0000000 --- a/Assets/Scripts/Road.cs +++ /dev/null @@ -1,85 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Road { - public List nodes = new List(); - Bezier path; - FlatBezierRenderer pathLine, roadBody; - Config config; - MeshCollider collider; - - public Road(Node start, Node end, Config config) { - nodes.Add(start); - nodes.Add(end); - path = new Bezier(); - pathLine = new FlatBezierRenderer(path, 50, 0.05f); - roadBody = new FlatBezierRenderer(path, 50, 2f); - this.config = config; - } - - - public void initialize(Transform parent) { - GameObject child = new GameObject(); - child.transform.parent = parent; - GameObject lineChild = new GameObject(); - lineChild.transform.position = Vector3.zero; - lineChild.AddComponent().material = config.roadEditMaterial; - lineChild.AddComponent().mesh = pathLine.mesh; - lineChild.transform.parent = child.transform; - - GameObject roadChild = new GameObject(); - collider = roadChild.AddComponent(); - roadChild.transform.position = Vector3.zero; - roadChild.AddComponent().material = config.roadMaterial; - roadChild.transform.parent = child.transform; - roadChild.layer = 8; - roadChild.AddComponent().mesh = roadBody.mesh; - roadChild.AddComponent().road = this; - } - - public void update(bool updateOthers) { - path.A = nodes[0].position; - path.D = nodes[1].position; - if (nodes[0].roads.Count == 2) { - path.B = nodes[0].position + - 0.25f * (nodes[1].position - nodes[0].getOther(nodes[1]).position); - } else { - path.B = 0.5f * (nodes[0].position + nodes[1].position); - } - if (nodes[1].roads.Count == 2) { - path.C = nodes[1].position + - 0.25f * (nodes[0].position - nodes[1].getOther(nodes[0]).position); - } else { - path.C = 0.5f * (nodes[0].position + nodes[1].position); - } - if (updateOthers) { - foreach (Node node in nodes) { - node.lateUpdate(this); - } - } - pathLine.update(); - roadBody.update(); - collider.sharedMesh = roadBody.mesh; - } - - override public bool Equals(object other) { - if (other == null || !(other is Road)) { - return false; - } - if (other == this) { - return true; - } - Road otherRoad = (Road) other; - foreach (Node node in nodes) { - if (! otherRoad.nodes.Contains(node)) { - return false; - } - } - return true; - } - - public override int GetHashCode() { - return nodes[0].GetHashCode() << 16 | nodes[1].GetHashCode(); - } -} diff --git a/Assets/Scripts/Road.cs.meta b/Assets/Scripts/Road.cs.meta deleted file mode 100644 index 24dc94b..0000000 --- a/Assets/Scripts/Road.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0ab512a631c0cc4468e77b6a2dfeb31f -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/RoadData.cs b/Assets/Scripts/RoadData.cs deleted file mode 100644 index eb620ba..0000000 --- a/Assets/Scripts/RoadData.cs +++ /dev/null @@ -1,7 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class RoadData : MonoBehaviour { - public Road road; -} diff --git a/Assets/Scripts/RoadData.cs.meta b/Assets/Scripts/RoadData.cs.meta deleted file mode 100644 index 2948b97..0000000 --- a/Assets/Scripts/RoadData.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 6bfac654eff68754c88b877a9b88ae42 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/RoadEdit.cs b/Assets/Scripts/RoadEdit.cs deleted file mode 100644 index a6cc73e..0000000 --- a/Assets/Scripts/RoadEdit.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class RoadEdit : MonoBehaviour { - public GameObject start; - public GameObject startDirection; - public GameObject end; - public GameObject endDirection; - public Camera mainCamera; - - private GameObject currentlyPulling = null; - public Bezier bezier; - private FlatBezierRenderer pathRenderer; - public Material highlight, noHighlight; - - void Start() { - } - - void updateRoad(Vector3 position) { - } - - void Update() { - Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); - if (Input.GetAxis("Fire1") != 0.0f) { - if (currentlyPulling == null && Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, 1 << 7)) { - currentlyPulling = hit.transform.gameObject; - currentlyPulling.GetComponent().material = highlight; - } - } else { - if (currentlyPulling != null) { - currentlyPulling.GetComponent().material = noHighlight; - } - currentlyPulling = null; - } - if (currentlyPulling != null) { - Physics.Raycast(ray, out RaycastHit hit_, Mathf.Infinity, 1 << 6); - Vector3 position = new Vector3(hit_.point.x, 0.0f, hit_.point.z); - currentlyPulling.transform.position = position; - updateRoad(position); - } - } -} diff --git a/Assets/Scripts/RoadEdit.cs.meta b/Assets/Scripts/RoadEdit.cs.meta deleted file mode 100644 index 4e09d01..0000000 --- a/Assets/Scripts/RoadEdit.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 68e9412fa432f2e468d5d7447e99fd89 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Roads.cs b/Assets/Scripts/Roads.cs deleted file mode 100644 index acd376a..0000000 --- a/Assets/Scripts/Roads.cs +++ /dev/null @@ -1,102 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Roads : MonoBehaviour { - public List nodes = new List(); - public List roads = new List(); - public FlatBezierRenderer roadRenderer; - public Camera mainCamera; - private bool drawing = false; - public Material material; - public Config config; - - void Start() { - } - - private Node startNode; - - private void startRoad() { - Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out RaycastHit hit, Mathf.Infinity, 1 << 6); - foreach (Node node in nodes) { - if ((hit.point - node.position).magnitude < 1.0f) { - startNode = node; - drawing = true; - return; - } - } - Vector3 position = new Vector3(hit.point.x, 0.0f, hit.point.z); - startNode = new Node(position, transform, config); - nodes.Add(startNode); - drawing = true; - } - - private void endRoad() { - Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out RaycastHit hit, Mathf.Infinity, 1 << 6); - Node endNode = null; - foreach (Node node in nodes) { - if ((hit.point - node.position).magnitude < 1.0f) { - endNode = node; - break; - } - } - if (endNode == null) { - Vector3 position = new Vector3(hit.point.x, 0.0f, hit.point.z); - endNode = new Node(position, transform, config); - nodes.Add(endNode); - } - Road road = new Road(startNode, endNode, config); - if (!roads.Contains(road) && startNode != endNode) { - startNode.roads.Add(road); - endNode.roads.Add(road); - roads.Add(road); - road.initialize(transform); - road.update(true); - } - startNode = null; - drawing = false; - } - - Node pulling = null; - - void Update() { - Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); - RaycastHit hit; - if (config.mode == Mode.DrawRoad) { - if (Input.GetAxis("Fire1") != 0.0f) { - if (!drawing) { - startRoad(); - } - } else if (drawing) { - endRoad(); - } - } else if (config.mode == Mode.DeleteRoad) { - if (!drawing && Input.GetAxis("Fire1") != 0.0f) { - if (Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 8)) { - drawing = true; - Road road = hit.transform.gameObject.GetComponent().road; - roads.Remove(road); - foreach (Node node in road.nodes) { - node.roads.Remove(road); - node.update(); - } - Destroy(hit.transform.parent.gameObject); - } - } else if (drawing && Input.GetAxis("Fire1") == 0.0f) { - drawing = false; - } - } - if (Input.GetAxis("Fire2") != 0.0f) { - if (pulling == null && Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 7)) { - pulling = nodes.Find(node => node.gameObject == hit.transform.gameObject); - } - } else { - pulling = null; - } - if (pulling != null) { - Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 6); - Vector3 position = new Vector3(hit.point.x, 0.0f, hit.point.z); - pulling.pull(position); - } - } -} diff --git a/Assets/Scripts/Roads.cs.meta b/Assets/Scripts/Roads.cs.meta deleted file mode 100644 index f26a401..0000000 --- a/Assets/Scripts/Roads.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 7635c530aa8ef31498f100f4e7465bea -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Roads.meta b/Assets/Scripts/Roads.meta new file mode 100644 index 0000000..defbd46 --- /dev/null +++ b/Assets/Scripts/Roads.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: e6a3f10ad4786e44d9faf825f11a8e1d +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Roads/Node.cs b/Assets/Scripts/Roads/Node.cs new file mode 100644 index 0000000..3b2f31a --- /dev/null +++ b/Assets/Scripts/Roads/Node.cs @@ -0,0 +1,82 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Node { + private FlatCircleRenderer circle = new FlatCircleRenderer(0.2f, 0.05f, 32); + private FlatCircleRenderer fullCircle = new FlatCircleRenderer(0f, 1f, 32); + + public Vector3 position; + public List roads = new List(); + public GameObject gameObject; + + public Node(Vector3 position, Transform parent, Config config) { + gameObject = new GameObject(); + gameObject.transform.position = position; + gameObject.transform.parent = parent; + gameObject.AddComponent().radius = 2f; + gameObject.layer = 7; + gameObject.AddComponent().node = this; + + GameObject nodeCircle = new GameObject(); + nodeCircle.AddComponent().material = config.roadEditMaterial; + nodeCircle.AddComponent().mesh = circle.mesh; + nodeCircle.transform.parent = gameObject.transform; + nodeCircle.transform.localPosition = Vector3.zero; + + GameObject nodeRoad = new GameObject(); + nodeRoad.AddComponent().material = config.roadMaterial; + nodeRoad.AddComponent().mesh = fullCircle.mesh; + nodeRoad.transform.parent = gameObject.transform; + nodeRoad.transform.localPosition = Vector3.zero; + 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(); + } + + override public bool Equals(object other) { + if ((other == null) || ! this.GetType().Equals(other.GetType())) { + return false; + } + if (other == this) { + return true; + } + return ((Node)other).position.Equals(position); + } + + public void pull(Vector3 position) { + this.position = position; + gameObject.transform.position = position; + foreach (Road road in roads) { + 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/Roads/Node.cs.meta b/Assets/Scripts/Roads/Node.cs.meta new file mode 100644 index 0000000..8bbedf1 --- /dev/null +++ b/Assets/Scripts/Roads/Node.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 3cbed1dad7ec67d4dbe8602aea1e2979 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Roads/NodeData.cs b/Assets/Scripts/Roads/NodeData.cs new file mode 100644 index 0000000..9ee8ace --- /dev/null +++ b/Assets/Scripts/Roads/NodeData.cs @@ -0,0 +1,7 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class NodeData : MonoBehaviour { + public Node node = null; +} diff --git a/Assets/Scripts/Roads/NodeData.cs.meta b/Assets/Scripts/Roads/NodeData.cs.meta new file mode 100644 index 0000000..73dcf56 --- /dev/null +++ b/Assets/Scripts/Roads/NodeData.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4cf9bcd8c4889de4985e5d6a776acc5b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Roads/Road.cs b/Assets/Scripts/Roads/Road.cs new file mode 100644 index 0000000..1ad1a74 --- /dev/null +++ b/Assets/Scripts/Roads/Road.cs @@ -0,0 +1,88 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Road { + public List nodes = new List(); + Bezier path; + private FlatBezierRenderer pathLine, roadBody; + private Config config; + private MeshCollider collider; + public GameObject gameObject; + + public Road(Node start, Node end, Config config) { + nodes.Add(start); + nodes.Add(end); + path = new Bezier(); + pathLine = new FlatBezierRenderer(path, 50, 0.05f); + roadBody = new FlatBezierRenderer(path, 50, 2f); + this.config = config; + } + + + public void initialize(Transform parent) { + gameObject = new GameObject(); + gameObject.transform.parent = parent; + gameObject.transform.position = Vector3.zero; + + GameObject lineChild = new GameObject(); + lineChild.transform.position = Vector3.zero; + lineChild.AddComponent().material = config.roadEditMaterial; + lineChild.AddComponent().mesh = pathLine.mesh; + lineChild.transform.parent = gameObject.transform; + + GameObject bodyChild = new GameObject(); + bodyChild.AddComponent().material = config.roadMaterial; + bodyChild.AddComponent().mesh = roadBody.mesh; + bodyChild.AddComponent().road = this; + collider = bodyChild.AddComponent(); + bodyChild.transform.parent = gameObject.transform; + bodyChild.layer = 8; + update(true); + } + + public void update(bool updateOthers) { + path.A = nodes[0].position; + path.D = nodes[1].position; + if (nodes[0].roads.Count == 2) { + path.B = nodes[0].position + + 0.25f * (nodes[1].position - nodes[0].getOther(nodes[1]).position); + } else { + path.B = 0.5f * (nodes[0].position + nodes[1].position); + } + if (nodes[1].roads.Count == 2) { + path.C = nodes[1].position + + 0.25f * (nodes[0].position - nodes[1].getOther(nodes[0]).position); + } else { + path.C = 0.5f * (nodes[0].position + nodes[1].position); + } + if (updateOthers) { + foreach (Node node in nodes) { + node.lateUpdate(this); + } + } + pathLine.update(); + roadBody.update(); + collider.sharedMesh = roadBody.mesh; + } + + override public bool Equals(object other) { + if (other == null || !(other is Road)) { + return false; + } + if (other == this) { + return true; + } + Road otherRoad = (Road) other; + foreach (Node node in nodes) { + if (! otherRoad.nodes.Contains(node)) { + return false; + } + } + return true; + } + + public override int GetHashCode() { + return nodes[0].GetHashCode() << 16 | nodes[1].GetHashCode(); + } +} diff --git a/Assets/Scripts/Roads/Road.cs.meta b/Assets/Scripts/Roads/Road.cs.meta new file mode 100644 index 0000000..24dc94b --- /dev/null +++ b/Assets/Scripts/Roads/Road.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0ab512a631c0cc4468e77b6a2dfeb31f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Roads/RoadData.cs b/Assets/Scripts/Roads/RoadData.cs new file mode 100644 index 0000000..eb620ba --- /dev/null +++ b/Assets/Scripts/Roads/RoadData.cs @@ -0,0 +1,7 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class RoadData : MonoBehaviour { + public Road road; +} diff --git a/Assets/Scripts/Roads/RoadData.cs.meta b/Assets/Scripts/Roads/RoadData.cs.meta new file mode 100644 index 0000000..2948b97 --- /dev/null +++ b/Assets/Scripts/Roads/RoadData.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 6bfac654eff68754c88b877a9b88ae42 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Bezier.cs b/Assets/Scripts/Bezier.cs deleted file mode 100644 index ea19006..0000000 --- a/Assets/Scripts/Bezier.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Bezier { - public Vector3 A, B, C, D; - - public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { - this.A = A; - this.B = B; - this.C = C; - this.D = D; - } - - public Bezier() { - this.A = Vector3.zero; - this.B = Vector3.zero; - this.C = Vector3.zero; - this.D = Vector3.zero; - } - - public Vector3 getPosition(float t) { - float T = 1.0f - t; - return - A * ( T*T*T) + - B * (3*T*T*t) + - C * (3*T*t*t) + - D * ( t*t*t); - } - - public Vector3 getDirection(float t) { - float T = 1.0f - t; - return - A * (-3*T*T ) + - B * ( 3*T*T - 6*T*t) + - C * (-3*t*t + 6*T*t) + - D * ( 3*t*t ); - } -} diff --git a/Assets/Scripts/Bezier.cs.meta b/Assets/Scripts/Bezier.cs.meta deleted file mode 100644 index 14630e2..0000000 --- a/Assets/Scripts/Bezier.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 284cf9d52e385aa4ebba97eb8b43b8a6 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatBezierRenderer.cs b/Assets/Scripts/FlatBezierRenderer.cs deleted file mode 100644 index b8dad15..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatBezierRenderer { - private int percision; - private float width; - public Bezier bezier; - public Mesh mesh = new Mesh(); - public FlatBezierRenderer(Bezier bezier, int percision, float width) { - this.percision = percision; - this.width = width; - this.bezier = bezier; - update(); - } - - public void update() { - float realWidth = width / 2; - int vertexCount = 6 * (percision+1); - Vector3[] vertices = new Vector3[vertexCount / 3]; - int[] indices = new int[vertexCount]; - Vector3 position = bezier.getPosition(0.0f); - Vector3 tangent = getTangent(0.0f); - vertices[0] = position + tangent * realWidth; - vertices[1] = position - tangent * realWidth; - for (int i = 1; i <= percision; i++) { - float t = (float) i / percision; - indices[6*i ] = 2*i ; - indices[6*i+1] = 2*i-1; - indices[6*i+2] = 2*i-2; - indices[6*i+3] = 2*i+1; - indices[6*i+4] = 2*i-1; - indices[6*i+5] = 2*i ; - - position = bezier.getPosition(t); - tangent = getTangent(t); - vertices[2*i ] = position + tangent * realWidth; - vertices[2*i+1] = position - tangent * realWidth; - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - mesh.Optimize(); - } - - private Vector3 getTangent(float t) { - Vector3 direction = bezier.getDirection(t); - direction.Normalize(); - return new Vector3(-direction.z, 0, direction.x); - } -} diff --git a/Assets/Scripts/FlatBezierRenderer.cs.meta b/Assets/Scripts/FlatBezierRenderer.cs.meta deleted file mode 100644 index 1fe6547..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 9bfb303c44d15ef468dc0d7334241e94 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatCircleRenderer.cs b/Assets/Scripts/FlatCircleRenderer.cs deleted file mode 100644 index 363b663..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatCircleRenderer { - public Mesh mesh = new Mesh(); - public float radius, width; - private int segments; - - public FlatCircleRenderer(float radius, float width, int segments) { - this.radius = radius; - this.width = width; - this.segments = segments; - update(); - } - - public void update() { - Vector3[] vertices = new Vector3[2*segments+6]; - int[] indices = new int[6*segments]; - for (int i = 0; i < segments; i++) { - float alpha = (float) i / segments * 2 * Mathf.PI; - indices[6*i ] = 2*i ; - indices[6*i+3] = 2*i ; - indices[6*i+5] = 2*i+1; - if (i == 0) { - indices[6*i+1] = 2*segments-2; - indices[6*i+2] = 2*segments-1; - indices[6*i+4] = 2*segments-1; - } else { - indices[6*i+1] = 2*i-2; - indices[6*i+2] = 2*i-1; - indices[6*i+4] = 2*i-1; - } - Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); - vertices[2*i ] = direction * radius; - vertices[2*i+1] = direction * (radius + width); - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - } -} diff --git a/Assets/Scripts/FlatCircleRenderer.cs.meta b/Assets/Scripts/FlatCircleRenderer.cs.meta deleted file mode 100644 index d10c238..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0fe81c7924a85b142a4a86201b08c241 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Node.cs b/Assets/Scripts/Node.cs deleted file mode 100644 index e3c4cbd..0000000 --- a/Assets/Scripts/Node.cs +++ /dev/null @@ -1,81 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Node { - public FlatCircleRenderer circle = new FlatCircleRenderer(0.2f, 0.05f, 32); - public FlatCircleRenderer fullCircle = new FlatCircleRenderer(0f, 1f, 32); - - public Vector3 position; - public List roads = new List(); - public GameObject gameObject; - - public Node(Vector3 position, Transform parent, Config config) { - gameObject = new GameObject(); - gameObject.transform.position = position; - gameObject.transform.parent = parent; - gameObject.AddComponent().radius = 0.5f; - gameObject.layer = 7; - - GameObject nodeCircle = new GameObject(); - nodeCircle.AddComponent().material = config.roadEditMaterial; - nodeCircle.AddComponent().mesh = circle.mesh; - nodeCircle.transform.parent = gameObject.transform; - nodeCircle.transform.localPosition = Vector3.zero; - - GameObject nodeRoad = new GameObject(); - nodeRoad.AddComponent().material = config.roadMaterial; - nodeRoad.AddComponent().mesh = fullCircle.mesh; - nodeRoad.transform.parent = gameObject.transform; - nodeRoad.transform.localPosition = Vector3.zero; - 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(); - } - - override public bool Equals(object other) { - if ((other == null) || ! this.GetType().Equals(other.GetType())) { - return false; - } - if (other == this) { - return true; - } - return ((Node)other).position.Equals(position); - } - - public void pull(Vector3 position) { - this.position = position; - gameObject.transform.position = position; - foreach (Road road in roads) { - 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/Node.cs.meta b/Assets/Scripts/Node.cs.meta deleted file mode 100644 index 8bbedf1..0000000 --- a/Assets/Scripts/Node.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 3cbed1dad7ec67d4dbe8602aea1e2979 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Primitives.meta b/Assets/Scripts/Primitives.meta new file mode 100644 index 0000000..0cf8895 --- /dev/null +++ b/Assets/Scripts/Primitives.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 6d1a8c7f33189a240b4d749356f53399 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/Bezier.cs b/Assets/Scripts/Primitives/Bezier.cs new file mode 100644 index 0000000..ea19006 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs @@ -0,0 +1,39 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Bezier { + public Vector3 A, B, C, D; + + public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { + this.A = A; + this.B = B; + this.C = C; + this.D = D; + } + + public Bezier() { + this.A = Vector3.zero; + this.B = Vector3.zero; + this.C = Vector3.zero; + this.D = Vector3.zero; + } + + public Vector3 getPosition(float t) { + float T = 1.0f - t; + return + A * ( T*T*T) + + B * (3*T*T*t) + + C * (3*T*t*t) + + D * ( t*t*t); + } + + public Vector3 getDirection(float t) { + float T = 1.0f - t; + return + A * (-3*T*T ) + + B * ( 3*T*T - 6*T*t) + + C * (-3*t*t + 6*T*t) + + D * ( 3*t*t ); + } +} diff --git a/Assets/Scripts/Primitives/Bezier.cs.meta b/Assets/Scripts/Primitives/Bezier.cs.meta new file mode 100644 index 0000000..14630e2 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 284cf9d52e385aa4ebba97eb8b43b8a6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs b/Assets/Scripts/Primitives/FlatBezierRenderer.cs new file mode 100644 index 0000000..b8dad15 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs @@ -0,0 +1,51 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FlatBezierRenderer { + private int percision; + private float width; + public Bezier bezier; + public Mesh mesh = new Mesh(); + public FlatBezierRenderer(Bezier bezier, int percision, float width) { + this.percision = percision; + this.width = width; + this.bezier = bezier; + update(); + } + + public void update() { + float realWidth = width / 2; + int vertexCount = 6 * (percision+1); + Vector3[] vertices = new Vector3[vertexCount / 3]; + int[] indices = new int[vertexCount]; + Vector3 position = bezier.getPosition(0.0f); + Vector3 tangent = getTangent(0.0f); + vertices[0] = position + tangent * realWidth; + vertices[1] = position - tangent * realWidth; + for (int i = 1; i <= percision; i++) { + float t = (float) i / percision; + indices[6*i ] = 2*i ; + indices[6*i+1] = 2*i-1; + indices[6*i+2] = 2*i-2; + indices[6*i+3] = 2*i+1; + indices[6*i+4] = 2*i-1; + indices[6*i+5] = 2*i ; + + position = bezier.getPosition(t); + tangent = getTangent(t); + vertices[2*i ] = position + tangent * realWidth; + vertices[2*i+1] = position - tangent * realWidth; + } + mesh.Clear(); + mesh.vertices = vertices; + mesh.triangles = indices; + mesh.Optimize(); + } + + private Vector3 getTangent(float t) { + Vector3 direction = bezier.getDirection(t); + direction.Normalize(); + return new Vector3(-direction.z, 0, direction.x); + } +} diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta b/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta new file mode 100644 index 0000000..1fe6547 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9bfb303c44d15ef468dc0d7334241e94 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/FlatCircleRenderer.cs b/Assets/Scripts/Primitives/FlatCircleRenderer.cs new file mode 100644 index 0000000..363b663 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatCircleRenderer.cs @@ -0,0 +1,42 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FlatCircleRenderer { + public Mesh mesh = new Mesh(); + public float radius, width; + private int segments; + + public FlatCircleRenderer(float radius, float width, int segments) { + this.radius = radius; + this.width = width; + this.segments = segments; + update(); + } + + public void update() { + Vector3[] vertices = new Vector3[2*segments+6]; + int[] indices = new int[6*segments]; + for (int i = 0; i < segments; i++) { + float alpha = (float) i / segments * 2 * Mathf.PI; + indices[6*i ] = 2*i ; + indices[6*i+3] = 2*i ; + indices[6*i+5] = 2*i+1; + if (i == 0) { + indices[6*i+1] = 2*segments-2; + indices[6*i+2] = 2*segments-1; + indices[6*i+4] = 2*segments-1; + } else { + indices[6*i+1] = 2*i-2; + indices[6*i+2] = 2*i-1; + indices[6*i+4] = 2*i-1; + } + Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); + vertices[2*i ] = direction * radius; + vertices[2*i+1] = direction * (radius + width); + } + mesh.Clear(); + mesh.vertices = vertices; + mesh.triangles = indices; + } +} diff --git a/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta b/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta new file mode 100644 index 0000000..d10c238 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0fe81c7924a85b142a4a86201b08c241 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Road.cs b/Assets/Scripts/Road.cs deleted file mode 100644 index 0af2218..0000000 --- a/Assets/Scripts/Road.cs +++ /dev/null @@ -1,85 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Road { - public List nodes = new List(); - Bezier path; - FlatBezierRenderer pathLine, roadBody; - Config config; - MeshCollider collider; - - public Road(Node start, Node end, Config config) { - nodes.Add(start); - nodes.Add(end); - path = new Bezier(); - pathLine = new FlatBezierRenderer(path, 50, 0.05f); - roadBody = new FlatBezierRenderer(path, 50, 2f); - this.config = config; - } - - - public void initialize(Transform parent) { - GameObject child = new GameObject(); - child.transform.parent = parent; - GameObject lineChild = new GameObject(); - lineChild.transform.position = Vector3.zero; - lineChild.AddComponent().material = config.roadEditMaterial; - lineChild.AddComponent().mesh = pathLine.mesh; - lineChild.transform.parent = child.transform; - - GameObject roadChild = new GameObject(); - collider = roadChild.AddComponent(); - roadChild.transform.position = Vector3.zero; - roadChild.AddComponent().material = config.roadMaterial; - roadChild.transform.parent = child.transform; - roadChild.layer = 8; - roadChild.AddComponent().mesh = roadBody.mesh; - roadChild.AddComponent().road = this; - } - - public void update(bool updateOthers) { - path.A = nodes[0].position; - path.D = nodes[1].position; - if (nodes[0].roads.Count == 2) { - path.B = nodes[0].position + - 0.25f * (nodes[1].position - nodes[0].getOther(nodes[1]).position); - } else { - path.B = 0.5f * (nodes[0].position + nodes[1].position); - } - if (nodes[1].roads.Count == 2) { - path.C = nodes[1].position + - 0.25f * (nodes[0].position - nodes[1].getOther(nodes[0]).position); - } else { - path.C = 0.5f * (nodes[0].position + nodes[1].position); - } - if (updateOthers) { - foreach (Node node in nodes) { - node.lateUpdate(this); - } - } - pathLine.update(); - roadBody.update(); - collider.sharedMesh = roadBody.mesh; - } - - override public bool Equals(object other) { - if (other == null || !(other is Road)) { - return false; - } - if (other == this) { - return true; - } - Road otherRoad = (Road) other; - foreach (Node node in nodes) { - if (! otherRoad.nodes.Contains(node)) { - return false; - } - } - return true; - } - - public override int GetHashCode() { - return nodes[0].GetHashCode() << 16 | nodes[1].GetHashCode(); - } -} diff --git a/Assets/Scripts/Road.cs.meta b/Assets/Scripts/Road.cs.meta deleted file mode 100644 index 24dc94b..0000000 --- a/Assets/Scripts/Road.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0ab512a631c0cc4468e77b6a2dfeb31f -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/RoadData.cs b/Assets/Scripts/RoadData.cs deleted file mode 100644 index eb620ba..0000000 --- a/Assets/Scripts/RoadData.cs +++ /dev/null @@ -1,7 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class RoadData : MonoBehaviour { - public Road road; -} diff --git a/Assets/Scripts/RoadData.cs.meta b/Assets/Scripts/RoadData.cs.meta deleted file mode 100644 index 2948b97..0000000 --- a/Assets/Scripts/RoadData.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 6bfac654eff68754c88b877a9b88ae42 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/RoadEdit.cs b/Assets/Scripts/RoadEdit.cs deleted file mode 100644 index a6cc73e..0000000 --- a/Assets/Scripts/RoadEdit.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class RoadEdit : MonoBehaviour { - public GameObject start; - public GameObject startDirection; - public GameObject end; - public GameObject endDirection; - public Camera mainCamera; - - private GameObject currentlyPulling = null; - public Bezier bezier; - private FlatBezierRenderer pathRenderer; - public Material highlight, noHighlight; - - void Start() { - } - - void updateRoad(Vector3 position) { - } - - void Update() { - Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); - if (Input.GetAxis("Fire1") != 0.0f) { - if (currentlyPulling == null && Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, 1 << 7)) { - currentlyPulling = hit.transform.gameObject; - currentlyPulling.GetComponent().material = highlight; - } - } else { - if (currentlyPulling != null) { - currentlyPulling.GetComponent().material = noHighlight; - } - currentlyPulling = null; - } - if (currentlyPulling != null) { - Physics.Raycast(ray, out RaycastHit hit_, Mathf.Infinity, 1 << 6); - Vector3 position = new Vector3(hit_.point.x, 0.0f, hit_.point.z); - currentlyPulling.transform.position = position; - updateRoad(position); - } - } -} diff --git a/Assets/Scripts/RoadEdit.cs.meta b/Assets/Scripts/RoadEdit.cs.meta deleted file mode 100644 index 4e09d01..0000000 --- a/Assets/Scripts/RoadEdit.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 68e9412fa432f2e468d5d7447e99fd89 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Roads.cs b/Assets/Scripts/Roads.cs deleted file mode 100644 index acd376a..0000000 --- a/Assets/Scripts/Roads.cs +++ /dev/null @@ -1,102 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Roads : MonoBehaviour { - public List nodes = new List(); - public List roads = new List(); - public FlatBezierRenderer roadRenderer; - public Camera mainCamera; - private bool drawing = false; - public Material material; - public Config config; - - void Start() { - } - - private Node startNode; - - private void startRoad() { - Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out RaycastHit hit, Mathf.Infinity, 1 << 6); - foreach (Node node in nodes) { - if ((hit.point - node.position).magnitude < 1.0f) { - startNode = node; - drawing = true; - return; - } - } - Vector3 position = new Vector3(hit.point.x, 0.0f, hit.point.z); - startNode = new Node(position, transform, config); - nodes.Add(startNode); - drawing = true; - } - - private void endRoad() { - Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out RaycastHit hit, Mathf.Infinity, 1 << 6); - Node endNode = null; - foreach (Node node in nodes) { - if ((hit.point - node.position).magnitude < 1.0f) { - endNode = node; - break; - } - } - if (endNode == null) { - Vector3 position = new Vector3(hit.point.x, 0.0f, hit.point.z); - endNode = new Node(position, transform, config); - nodes.Add(endNode); - } - Road road = new Road(startNode, endNode, config); - if (!roads.Contains(road) && startNode != endNode) { - startNode.roads.Add(road); - endNode.roads.Add(road); - roads.Add(road); - road.initialize(transform); - road.update(true); - } - startNode = null; - drawing = false; - } - - Node pulling = null; - - void Update() { - Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); - RaycastHit hit; - if (config.mode == Mode.DrawRoad) { - if (Input.GetAxis("Fire1") != 0.0f) { - if (!drawing) { - startRoad(); - } - } else if (drawing) { - endRoad(); - } - } else if (config.mode == Mode.DeleteRoad) { - if (!drawing && Input.GetAxis("Fire1") != 0.0f) { - if (Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 8)) { - drawing = true; - Road road = hit.transform.gameObject.GetComponent().road; - roads.Remove(road); - foreach (Node node in road.nodes) { - node.roads.Remove(road); - node.update(); - } - Destroy(hit.transform.parent.gameObject); - } - } else if (drawing && Input.GetAxis("Fire1") == 0.0f) { - drawing = false; - } - } - if (Input.GetAxis("Fire2") != 0.0f) { - if (pulling == null && Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 7)) { - pulling = nodes.Find(node => node.gameObject == hit.transform.gameObject); - } - } else { - pulling = null; - } - if (pulling != null) { - Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 6); - Vector3 position = new Vector3(hit.point.x, 0.0f, hit.point.z); - pulling.pull(position); - } - } -} diff --git a/Assets/Scripts/Roads.cs.meta b/Assets/Scripts/Roads.cs.meta deleted file mode 100644 index f26a401..0000000 --- a/Assets/Scripts/Roads.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 7635c530aa8ef31498f100f4e7465bea -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Roads.meta b/Assets/Scripts/Roads.meta new file mode 100644 index 0000000..defbd46 --- /dev/null +++ b/Assets/Scripts/Roads.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: e6a3f10ad4786e44d9faf825f11a8e1d +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Roads/Node.cs b/Assets/Scripts/Roads/Node.cs new file mode 100644 index 0000000..3b2f31a --- /dev/null +++ b/Assets/Scripts/Roads/Node.cs @@ -0,0 +1,82 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Node { + private FlatCircleRenderer circle = new FlatCircleRenderer(0.2f, 0.05f, 32); + private FlatCircleRenderer fullCircle = new FlatCircleRenderer(0f, 1f, 32); + + public Vector3 position; + public List roads = new List(); + public GameObject gameObject; + + public Node(Vector3 position, Transform parent, Config config) { + gameObject = new GameObject(); + gameObject.transform.position = position; + gameObject.transform.parent = parent; + gameObject.AddComponent().radius = 2f; + gameObject.layer = 7; + gameObject.AddComponent().node = this; + + GameObject nodeCircle = new GameObject(); + nodeCircle.AddComponent().material = config.roadEditMaterial; + nodeCircle.AddComponent().mesh = circle.mesh; + nodeCircle.transform.parent = gameObject.transform; + nodeCircle.transform.localPosition = Vector3.zero; + + GameObject nodeRoad = new GameObject(); + nodeRoad.AddComponent().material = config.roadMaterial; + nodeRoad.AddComponent().mesh = fullCircle.mesh; + nodeRoad.transform.parent = gameObject.transform; + nodeRoad.transform.localPosition = Vector3.zero; + 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(); + } + + override public bool Equals(object other) { + if ((other == null) || ! this.GetType().Equals(other.GetType())) { + return false; + } + if (other == this) { + return true; + } + return ((Node)other).position.Equals(position); + } + + public void pull(Vector3 position) { + this.position = position; + gameObject.transform.position = position; + foreach (Road road in roads) { + 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/Roads/Node.cs.meta b/Assets/Scripts/Roads/Node.cs.meta new file mode 100644 index 0000000..8bbedf1 --- /dev/null +++ b/Assets/Scripts/Roads/Node.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 3cbed1dad7ec67d4dbe8602aea1e2979 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Roads/NodeData.cs b/Assets/Scripts/Roads/NodeData.cs new file mode 100644 index 0000000..9ee8ace --- /dev/null +++ b/Assets/Scripts/Roads/NodeData.cs @@ -0,0 +1,7 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class NodeData : MonoBehaviour { + public Node node = null; +} diff --git a/Assets/Scripts/Roads/NodeData.cs.meta b/Assets/Scripts/Roads/NodeData.cs.meta new file mode 100644 index 0000000..73dcf56 --- /dev/null +++ b/Assets/Scripts/Roads/NodeData.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4cf9bcd8c4889de4985e5d6a776acc5b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Roads/Road.cs b/Assets/Scripts/Roads/Road.cs new file mode 100644 index 0000000..1ad1a74 --- /dev/null +++ b/Assets/Scripts/Roads/Road.cs @@ -0,0 +1,88 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Road { + public List nodes = new List(); + Bezier path; + private FlatBezierRenderer pathLine, roadBody; + private Config config; + private MeshCollider collider; + public GameObject gameObject; + + public Road(Node start, Node end, Config config) { + nodes.Add(start); + nodes.Add(end); + path = new Bezier(); + pathLine = new FlatBezierRenderer(path, 50, 0.05f); + roadBody = new FlatBezierRenderer(path, 50, 2f); + this.config = config; + } + + + public void initialize(Transform parent) { + gameObject = new GameObject(); + gameObject.transform.parent = parent; + gameObject.transform.position = Vector3.zero; + + GameObject lineChild = new GameObject(); + lineChild.transform.position = Vector3.zero; + lineChild.AddComponent().material = config.roadEditMaterial; + lineChild.AddComponent().mesh = pathLine.mesh; + lineChild.transform.parent = gameObject.transform; + + GameObject bodyChild = new GameObject(); + bodyChild.AddComponent().material = config.roadMaterial; + bodyChild.AddComponent().mesh = roadBody.mesh; + bodyChild.AddComponent().road = this; + collider = bodyChild.AddComponent(); + bodyChild.transform.parent = gameObject.transform; + bodyChild.layer = 8; + update(true); + } + + public void update(bool updateOthers) { + path.A = nodes[0].position; + path.D = nodes[1].position; + if (nodes[0].roads.Count == 2) { + path.B = nodes[0].position + + 0.25f * (nodes[1].position - nodes[0].getOther(nodes[1]).position); + } else { + path.B = 0.5f * (nodes[0].position + nodes[1].position); + } + if (nodes[1].roads.Count == 2) { + path.C = nodes[1].position + + 0.25f * (nodes[0].position - nodes[1].getOther(nodes[0]).position); + } else { + path.C = 0.5f * (nodes[0].position + nodes[1].position); + } + if (updateOthers) { + foreach (Node node in nodes) { + node.lateUpdate(this); + } + } + pathLine.update(); + roadBody.update(); + collider.sharedMesh = roadBody.mesh; + } + + override public bool Equals(object other) { + if (other == null || !(other is Road)) { + return false; + } + if (other == this) { + return true; + } + Road otherRoad = (Road) other; + foreach (Node node in nodes) { + if (! otherRoad.nodes.Contains(node)) { + return false; + } + } + return true; + } + + public override int GetHashCode() { + return nodes[0].GetHashCode() << 16 | nodes[1].GetHashCode(); + } +} diff --git a/Assets/Scripts/Roads/Road.cs.meta b/Assets/Scripts/Roads/Road.cs.meta new file mode 100644 index 0000000..24dc94b --- /dev/null +++ b/Assets/Scripts/Roads/Road.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0ab512a631c0cc4468e77b6a2dfeb31f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Roads/RoadData.cs b/Assets/Scripts/Roads/RoadData.cs new file mode 100644 index 0000000..eb620ba --- /dev/null +++ b/Assets/Scripts/Roads/RoadData.cs @@ -0,0 +1,7 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class RoadData : MonoBehaviour { + public Road road; +} diff --git a/Assets/Scripts/Roads/RoadData.cs.meta b/Assets/Scripts/Roads/RoadData.cs.meta new file mode 100644 index 0000000..2948b97 --- /dev/null +++ b/Assets/Scripts/Roads/RoadData.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 6bfac654eff68754c88b877a9b88ae42 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Roads/RoadEdit.cs b/Assets/Scripts/Roads/RoadEdit.cs new file mode 100644 index 0000000..a6cc73e --- /dev/null +++ b/Assets/Scripts/Roads/RoadEdit.cs @@ -0,0 +1,43 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class RoadEdit : MonoBehaviour { + public GameObject start; + public GameObject startDirection; + public GameObject end; + public GameObject endDirection; + public Camera mainCamera; + + private GameObject currentlyPulling = null; + public Bezier bezier; + private FlatBezierRenderer pathRenderer; + public Material highlight, noHighlight; + + void Start() { + } + + void updateRoad(Vector3 position) { + } + + void Update() { + Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); + if (Input.GetAxis("Fire1") != 0.0f) { + if (currentlyPulling == null && Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, 1 << 7)) { + currentlyPulling = hit.transform.gameObject; + currentlyPulling.GetComponent().material = highlight; + } + } else { + if (currentlyPulling != null) { + currentlyPulling.GetComponent().material = noHighlight; + } + currentlyPulling = null; + } + if (currentlyPulling != null) { + Physics.Raycast(ray, out RaycastHit hit_, Mathf.Infinity, 1 << 6); + Vector3 position = new Vector3(hit_.point.x, 0.0f, hit_.point.z); + currentlyPulling.transform.position = position; + updateRoad(position); + } + } +} diff --git a/Assets/Scripts/Bezier.cs b/Assets/Scripts/Bezier.cs deleted file mode 100644 index ea19006..0000000 --- a/Assets/Scripts/Bezier.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Bezier { - public Vector3 A, B, C, D; - - public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { - this.A = A; - this.B = B; - this.C = C; - this.D = D; - } - - public Bezier() { - this.A = Vector3.zero; - this.B = Vector3.zero; - this.C = Vector3.zero; - this.D = Vector3.zero; - } - - public Vector3 getPosition(float t) { - float T = 1.0f - t; - return - A * ( T*T*T) + - B * (3*T*T*t) + - C * (3*T*t*t) + - D * ( t*t*t); - } - - public Vector3 getDirection(float t) { - float T = 1.0f - t; - return - A * (-3*T*T ) + - B * ( 3*T*T - 6*T*t) + - C * (-3*t*t + 6*T*t) + - D * ( 3*t*t ); - } -} diff --git a/Assets/Scripts/Bezier.cs.meta b/Assets/Scripts/Bezier.cs.meta deleted file mode 100644 index 14630e2..0000000 --- a/Assets/Scripts/Bezier.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 284cf9d52e385aa4ebba97eb8b43b8a6 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatBezierRenderer.cs b/Assets/Scripts/FlatBezierRenderer.cs deleted file mode 100644 index b8dad15..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatBezierRenderer { - private int percision; - private float width; - public Bezier bezier; - public Mesh mesh = new Mesh(); - public FlatBezierRenderer(Bezier bezier, int percision, float width) { - this.percision = percision; - this.width = width; - this.bezier = bezier; - update(); - } - - public void update() { - float realWidth = width / 2; - int vertexCount = 6 * (percision+1); - Vector3[] vertices = new Vector3[vertexCount / 3]; - int[] indices = new int[vertexCount]; - Vector3 position = bezier.getPosition(0.0f); - Vector3 tangent = getTangent(0.0f); - vertices[0] = position + tangent * realWidth; - vertices[1] = position - tangent * realWidth; - for (int i = 1; i <= percision; i++) { - float t = (float) i / percision; - indices[6*i ] = 2*i ; - indices[6*i+1] = 2*i-1; - indices[6*i+2] = 2*i-2; - indices[6*i+3] = 2*i+1; - indices[6*i+4] = 2*i-1; - indices[6*i+5] = 2*i ; - - position = bezier.getPosition(t); - tangent = getTangent(t); - vertices[2*i ] = position + tangent * realWidth; - vertices[2*i+1] = position - tangent * realWidth; - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - mesh.Optimize(); - } - - private Vector3 getTangent(float t) { - Vector3 direction = bezier.getDirection(t); - direction.Normalize(); - return new Vector3(-direction.z, 0, direction.x); - } -} diff --git a/Assets/Scripts/FlatBezierRenderer.cs.meta b/Assets/Scripts/FlatBezierRenderer.cs.meta deleted file mode 100644 index 1fe6547..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 9bfb303c44d15ef468dc0d7334241e94 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatCircleRenderer.cs b/Assets/Scripts/FlatCircleRenderer.cs deleted file mode 100644 index 363b663..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatCircleRenderer { - public Mesh mesh = new Mesh(); - public float radius, width; - private int segments; - - public FlatCircleRenderer(float radius, float width, int segments) { - this.radius = radius; - this.width = width; - this.segments = segments; - update(); - } - - public void update() { - Vector3[] vertices = new Vector3[2*segments+6]; - int[] indices = new int[6*segments]; - for (int i = 0; i < segments; i++) { - float alpha = (float) i / segments * 2 * Mathf.PI; - indices[6*i ] = 2*i ; - indices[6*i+3] = 2*i ; - indices[6*i+5] = 2*i+1; - if (i == 0) { - indices[6*i+1] = 2*segments-2; - indices[6*i+2] = 2*segments-1; - indices[6*i+4] = 2*segments-1; - } else { - indices[6*i+1] = 2*i-2; - indices[6*i+2] = 2*i-1; - indices[6*i+4] = 2*i-1; - } - Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); - vertices[2*i ] = direction * radius; - vertices[2*i+1] = direction * (radius + width); - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - } -} diff --git a/Assets/Scripts/FlatCircleRenderer.cs.meta b/Assets/Scripts/FlatCircleRenderer.cs.meta deleted file mode 100644 index d10c238..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0fe81c7924a85b142a4a86201b08c241 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Node.cs b/Assets/Scripts/Node.cs deleted file mode 100644 index e3c4cbd..0000000 --- a/Assets/Scripts/Node.cs +++ /dev/null @@ -1,81 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Node { - public FlatCircleRenderer circle = new FlatCircleRenderer(0.2f, 0.05f, 32); - public FlatCircleRenderer fullCircle = new FlatCircleRenderer(0f, 1f, 32); - - public Vector3 position; - public List roads = new List(); - public GameObject gameObject; - - public Node(Vector3 position, Transform parent, Config config) { - gameObject = new GameObject(); - gameObject.transform.position = position; - gameObject.transform.parent = parent; - gameObject.AddComponent().radius = 0.5f; - gameObject.layer = 7; - - GameObject nodeCircle = new GameObject(); - nodeCircle.AddComponent().material = config.roadEditMaterial; - nodeCircle.AddComponent().mesh = circle.mesh; - nodeCircle.transform.parent = gameObject.transform; - nodeCircle.transform.localPosition = Vector3.zero; - - GameObject nodeRoad = new GameObject(); - nodeRoad.AddComponent().material = config.roadMaterial; - nodeRoad.AddComponent().mesh = fullCircle.mesh; - nodeRoad.transform.parent = gameObject.transform; - nodeRoad.transform.localPosition = Vector3.zero; - 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(); - } - - override public bool Equals(object other) { - if ((other == null) || ! this.GetType().Equals(other.GetType())) { - return false; - } - if (other == this) { - return true; - } - return ((Node)other).position.Equals(position); - } - - public void pull(Vector3 position) { - this.position = position; - gameObject.transform.position = position; - foreach (Road road in roads) { - 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/Node.cs.meta b/Assets/Scripts/Node.cs.meta deleted file mode 100644 index 8bbedf1..0000000 --- a/Assets/Scripts/Node.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 3cbed1dad7ec67d4dbe8602aea1e2979 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Primitives.meta b/Assets/Scripts/Primitives.meta new file mode 100644 index 0000000..0cf8895 --- /dev/null +++ b/Assets/Scripts/Primitives.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 6d1a8c7f33189a240b4d749356f53399 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/Bezier.cs b/Assets/Scripts/Primitives/Bezier.cs new file mode 100644 index 0000000..ea19006 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs @@ -0,0 +1,39 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Bezier { + public Vector3 A, B, C, D; + + public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { + this.A = A; + this.B = B; + this.C = C; + this.D = D; + } + + public Bezier() { + this.A = Vector3.zero; + this.B = Vector3.zero; + this.C = Vector3.zero; + this.D = Vector3.zero; + } + + public Vector3 getPosition(float t) { + float T = 1.0f - t; + return + A * ( T*T*T) + + B * (3*T*T*t) + + C * (3*T*t*t) + + D * ( t*t*t); + } + + public Vector3 getDirection(float t) { + float T = 1.0f - t; + return + A * (-3*T*T ) + + B * ( 3*T*T - 6*T*t) + + C * (-3*t*t + 6*T*t) + + D * ( 3*t*t ); + } +} diff --git a/Assets/Scripts/Primitives/Bezier.cs.meta b/Assets/Scripts/Primitives/Bezier.cs.meta new file mode 100644 index 0000000..14630e2 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 284cf9d52e385aa4ebba97eb8b43b8a6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs b/Assets/Scripts/Primitives/FlatBezierRenderer.cs new file mode 100644 index 0000000..b8dad15 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs @@ -0,0 +1,51 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FlatBezierRenderer { + private int percision; + private float width; + public Bezier bezier; + public Mesh mesh = new Mesh(); + public FlatBezierRenderer(Bezier bezier, int percision, float width) { + this.percision = percision; + this.width = width; + this.bezier = bezier; + update(); + } + + public void update() { + float realWidth = width / 2; + int vertexCount = 6 * (percision+1); + Vector3[] vertices = new Vector3[vertexCount / 3]; + int[] indices = new int[vertexCount]; + Vector3 position = bezier.getPosition(0.0f); + Vector3 tangent = getTangent(0.0f); + vertices[0] = position + tangent * realWidth; + vertices[1] = position - tangent * realWidth; + for (int i = 1; i <= percision; i++) { + float t = (float) i / percision; + indices[6*i ] = 2*i ; + indices[6*i+1] = 2*i-1; + indices[6*i+2] = 2*i-2; + indices[6*i+3] = 2*i+1; + indices[6*i+4] = 2*i-1; + indices[6*i+5] = 2*i ; + + position = bezier.getPosition(t); + tangent = getTangent(t); + vertices[2*i ] = position + tangent * realWidth; + vertices[2*i+1] = position - tangent * realWidth; + } + mesh.Clear(); + mesh.vertices = vertices; + mesh.triangles = indices; + mesh.Optimize(); + } + + private Vector3 getTangent(float t) { + Vector3 direction = bezier.getDirection(t); + direction.Normalize(); + return new Vector3(-direction.z, 0, direction.x); + } +} diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta b/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta new file mode 100644 index 0000000..1fe6547 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9bfb303c44d15ef468dc0d7334241e94 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/FlatCircleRenderer.cs b/Assets/Scripts/Primitives/FlatCircleRenderer.cs new file mode 100644 index 0000000..363b663 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatCircleRenderer.cs @@ -0,0 +1,42 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FlatCircleRenderer { + public Mesh mesh = new Mesh(); + public float radius, width; + private int segments; + + public FlatCircleRenderer(float radius, float width, int segments) { + this.radius = radius; + this.width = width; + this.segments = segments; + update(); + } + + public void update() { + Vector3[] vertices = new Vector3[2*segments+6]; + int[] indices = new int[6*segments]; + for (int i = 0; i < segments; i++) { + float alpha = (float) i / segments * 2 * Mathf.PI; + indices[6*i ] = 2*i ; + indices[6*i+3] = 2*i ; + indices[6*i+5] = 2*i+1; + if (i == 0) { + indices[6*i+1] = 2*segments-2; + indices[6*i+2] = 2*segments-1; + indices[6*i+4] = 2*segments-1; + } else { + indices[6*i+1] = 2*i-2; + indices[6*i+2] = 2*i-1; + indices[6*i+4] = 2*i-1; + } + Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); + vertices[2*i ] = direction * radius; + vertices[2*i+1] = direction * (radius + width); + } + mesh.Clear(); + mesh.vertices = vertices; + mesh.triangles = indices; + } +} diff --git a/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta b/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta new file mode 100644 index 0000000..d10c238 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0fe81c7924a85b142a4a86201b08c241 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Road.cs b/Assets/Scripts/Road.cs deleted file mode 100644 index 0af2218..0000000 --- a/Assets/Scripts/Road.cs +++ /dev/null @@ -1,85 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Road { - public List nodes = new List(); - Bezier path; - FlatBezierRenderer pathLine, roadBody; - Config config; - MeshCollider collider; - - public Road(Node start, Node end, Config config) { - nodes.Add(start); - nodes.Add(end); - path = new Bezier(); - pathLine = new FlatBezierRenderer(path, 50, 0.05f); - roadBody = new FlatBezierRenderer(path, 50, 2f); - this.config = config; - } - - - public void initialize(Transform parent) { - GameObject child = new GameObject(); - child.transform.parent = parent; - GameObject lineChild = new GameObject(); - lineChild.transform.position = Vector3.zero; - lineChild.AddComponent().material = config.roadEditMaterial; - lineChild.AddComponent().mesh = pathLine.mesh; - lineChild.transform.parent = child.transform; - - GameObject roadChild = new GameObject(); - collider = roadChild.AddComponent(); - roadChild.transform.position = Vector3.zero; - roadChild.AddComponent().material = config.roadMaterial; - roadChild.transform.parent = child.transform; - roadChild.layer = 8; - roadChild.AddComponent().mesh = roadBody.mesh; - roadChild.AddComponent().road = this; - } - - public void update(bool updateOthers) { - path.A = nodes[0].position; - path.D = nodes[1].position; - if (nodes[0].roads.Count == 2) { - path.B = nodes[0].position + - 0.25f * (nodes[1].position - nodes[0].getOther(nodes[1]).position); - } else { - path.B = 0.5f * (nodes[0].position + nodes[1].position); - } - if (nodes[1].roads.Count == 2) { - path.C = nodes[1].position + - 0.25f * (nodes[0].position - nodes[1].getOther(nodes[0]).position); - } else { - path.C = 0.5f * (nodes[0].position + nodes[1].position); - } - if (updateOthers) { - foreach (Node node in nodes) { - node.lateUpdate(this); - } - } - pathLine.update(); - roadBody.update(); - collider.sharedMesh = roadBody.mesh; - } - - override public bool Equals(object other) { - if (other == null || !(other is Road)) { - return false; - } - if (other == this) { - return true; - } - Road otherRoad = (Road) other; - foreach (Node node in nodes) { - if (! otherRoad.nodes.Contains(node)) { - return false; - } - } - return true; - } - - public override int GetHashCode() { - return nodes[0].GetHashCode() << 16 | nodes[1].GetHashCode(); - } -} diff --git a/Assets/Scripts/Road.cs.meta b/Assets/Scripts/Road.cs.meta deleted file mode 100644 index 24dc94b..0000000 --- a/Assets/Scripts/Road.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0ab512a631c0cc4468e77b6a2dfeb31f -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/RoadData.cs b/Assets/Scripts/RoadData.cs deleted file mode 100644 index eb620ba..0000000 --- a/Assets/Scripts/RoadData.cs +++ /dev/null @@ -1,7 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class RoadData : MonoBehaviour { - public Road road; -} diff --git a/Assets/Scripts/RoadData.cs.meta b/Assets/Scripts/RoadData.cs.meta deleted file mode 100644 index 2948b97..0000000 --- a/Assets/Scripts/RoadData.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 6bfac654eff68754c88b877a9b88ae42 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/RoadEdit.cs b/Assets/Scripts/RoadEdit.cs deleted file mode 100644 index a6cc73e..0000000 --- a/Assets/Scripts/RoadEdit.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class RoadEdit : MonoBehaviour { - public GameObject start; - public GameObject startDirection; - public GameObject end; - public GameObject endDirection; - public Camera mainCamera; - - private GameObject currentlyPulling = null; - public Bezier bezier; - private FlatBezierRenderer pathRenderer; - public Material highlight, noHighlight; - - void Start() { - } - - void updateRoad(Vector3 position) { - } - - void Update() { - Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); - if (Input.GetAxis("Fire1") != 0.0f) { - if (currentlyPulling == null && Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, 1 << 7)) { - currentlyPulling = hit.transform.gameObject; - currentlyPulling.GetComponent().material = highlight; - } - } else { - if (currentlyPulling != null) { - currentlyPulling.GetComponent().material = noHighlight; - } - currentlyPulling = null; - } - if (currentlyPulling != null) { - Physics.Raycast(ray, out RaycastHit hit_, Mathf.Infinity, 1 << 6); - Vector3 position = new Vector3(hit_.point.x, 0.0f, hit_.point.z); - currentlyPulling.transform.position = position; - updateRoad(position); - } - } -} diff --git a/Assets/Scripts/RoadEdit.cs.meta b/Assets/Scripts/RoadEdit.cs.meta deleted file mode 100644 index 4e09d01..0000000 --- a/Assets/Scripts/RoadEdit.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 68e9412fa432f2e468d5d7447e99fd89 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Roads.cs b/Assets/Scripts/Roads.cs deleted file mode 100644 index acd376a..0000000 --- a/Assets/Scripts/Roads.cs +++ /dev/null @@ -1,102 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Roads : MonoBehaviour { - public List nodes = new List(); - public List roads = new List(); - public FlatBezierRenderer roadRenderer; - public Camera mainCamera; - private bool drawing = false; - public Material material; - public Config config; - - void Start() { - } - - private Node startNode; - - private void startRoad() { - Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out RaycastHit hit, Mathf.Infinity, 1 << 6); - foreach (Node node in nodes) { - if ((hit.point - node.position).magnitude < 1.0f) { - startNode = node; - drawing = true; - return; - } - } - Vector3 position = new Vector3(hit.point.x, 0.0f, hit.point.z); - startNode = new Node(position, transform, config); - nodes.Add(startNode); - drawing = true; - } - - private void endRoad() { - Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out RaycastHit hit, Mathf.Infinity, 1 << 6); - Node endNode = null; - foreach (Node node in nodes) { - if ((hit.point - node.position).magnitude < 1.0f) { - endNode = node; - break; - } - } - if (endNode == null) { - Vector3 position = new Vector3(hit.point.x, 0.0f, hit.point.z); - endNode = new Node(position, transform, config); - nodes.Add(endNode); - } - Road road = new Road(startNode, endNode, config); - if (!roads.Contains(road) && startNode != endNode) { - startNode.roads.Add(road); - endNode.roads.Add(road); - roads.Add(road); - road.initialize(transform); - road.update(true); - } - startNode = null; - drawing = false; - } - - Node pulling = null; - - void Update() { - Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); - RaycastHit hit; - if (config.mode == Mode.DrawRoad) { - if (Input.GetAxis("Fire1") != 0.0f) { - if (!drawing) { - startRoad(); - } - } else if (drawing) { - endRoad(); - } - } else if (config.mode == Mode.DeleteRoad) { - if (!drawing && Input.GetAxis("Fire1") != 0.0f) { - if (Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 8)) { - drawing = true; - Road road = hit.transform.gameObject.GetComponent().road; - roads.Remove(road); - foreach (Node node in road.nodes) { - node.roads.Remove(road); - node.update(); - } - Destroy(hit.transform.parent.gameObject); - } - } else if (drawing && Input.GetAxis("Fire1") == 0.0f) { - drawing = false; - } - } - if (Input.GetAxis("Fire2") != 0.0f) { - if (pulling == null && Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 7)) { - pulling = nodes.Find(node => node.gameObject == hit.transform.gameObject); - } - } else { - pulling = null; - } - if (pulling != null) { - Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 6); - Vector3 position = new Vector3(hit.point.x, 0.0f, hit.point.z); - pulling.pull(position); - } - } -} diff --git a/Assets/Scripts/Roads.cs.meta b/Assets/Scripts/Roads.cs.meta deleted file mode 100644 index f26a401..0000000 --- a/Assets/Scripts/Roads.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 7635c530aa8ef31498f100f4e7465bea -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Roads.meta b/Assets/Scripts/Roads.meta new file mode 100644 index 0000000..defbd46 --- /dev/null +++ b/Assets/Scripts/Roads.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: e6a3f10ad4786e44d9faf825f11a8e1d +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Roads/Node.cs b/Assets/Scripts/Roads/Node.cs new file mode 100644 index 0000000..3b2f31a --- /dev/null +++ b/Assets/Scripts/Roads/Node.cs @@ -0,0 +1,82 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Node { + private FlatCircleRenderer circle = new FlatCircleRenderer(0.2f, 0.05f, 32); + private FlatCircleRenderer fullCircle = new FlatCircleRenderer(0f, 1f, 32); + + public Vector3 position; + public List roads = new List(); + public GameObject gameObject; + + public Node(Vector3 position, Transform parent, Config config) { + gameObject = new GameObject(); + gameObject.transform.position = position; + gameObject.transform.parent = parent; + gameObject.AddComponent().radius = 2f; + gameObject.layer = 7; + gameObject.AddComponent().node = this; + + GameObject nodeCircle = new GameObject(); + nodeCircle.AddComponent().material = config.roadEditMaterial; + nodeCircle.AddComponent().mesh = circle.mesh; + nodeCircle.transform.parent = gameObject.transform; + nodeCircle.transform.localPosition = Vector3.zero; + + GameObject nodeRoad = new GameObject(); + nodeRoad.AddComponent().material = config.roadMaterial; + nodeRoad.AddComponent().mesh = fullCircle.mesh; + nodeRoad.transform.parent = gameObject.transform; + nodeRoad.transform.localPosition = Vector3.zero; + 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(); + } + + override public bool Equals(object other) { + if ((other == null) || ! this.GetType().Equals(other.GetType())) { + return false; + } + if (other == this) { + return true; + } + return ((Node)other).position.Equals(position); + } + + public void pull(Vector3 position) { + this.position = position; + gameObject.transform.position = position; + foreach (Road road in roads) { + 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/Roads/Node.cs.meta b/Assets/Scripts/Roads/Node.cs.meta new file mode 100644 index 0000000..8bbedf1 --- /dev/null +++ b/Assets/Scripts/Roads/Node.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 3cbed1dad7ec67d4dbe8602aea1e2979 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Roads/NodeData.cs b/Assets/Scripts/Roads/NodeData.cs new file mode 100644 index 0000000..9ee8ace --- /dev/null +++ b/Assets/Scripts/Roads/NodeData.cs @@ -0,0 +1,7 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class NodeData : MonoBehaviour { + public Node node = null; +} diff --git a/Assets/Scripts/Roads/NodeData.cs.meta b/Assets/Scripts/Roads/NodeData.cs.meta new file mode 100644 index 0000000..73dcf56 --- /dev/null +++ b/Assets/Scripts/Roads/NodeData.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4cf9bcd8c4889de4985e5d6a776acc5b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Roads/Road.cs b/Assets/Scripts/Roads/Road.cs new file mode 100644 index 0000000..1ad1a74 --- /dev/null +++ b/Assets/Scripts/Roads/Road.cs @@ -0,0 +1,88 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Road { + public List nodes = new List(); + Bezier path; + private FlatBezierRenderer pathLine, roadBody; + private Config config; + private MeshCollider collider; + public GameObject gameObject; + + public Road(Node start, Node end, Config config) { + nodes.Add(start); + nodes.Add(end); + path = new Bezier(); + pathLine = new FlatBezierRenderer(path, 50, 0.05f); + roadBody = new FlatBezierRenderer(path, 50, 2f); + this.config = config; + } + + + public void initialize(Transform parent) { + gameObject = new GameObject(); + gameObject.transform.parent = parent; + gameObject.transform.position = Vector3.zero; + + GameObject lineChild = new GameObject(); + lineChild.transform.position = Vector3.zero; + lineChild.AddComponent().material = config.roadEditMaterial; + lineChild.AddComponent().mesh = pathLine.mesh; + lineChild.transform.parent = gameObject.transform; + + GameObject bodyChild = new GameObject(); + bodyChild.AddComponent().material = config.roadMaterial; + bodyChild.AddComponent().mesh = roadBody.mesh; + bodyChild.AddComponent().road = this; + collider = bodyChild.AddComponent(); + bodyChild.transform.parent = gameObject.transform; + bodyChild.layer = 8; + update(true); + } + + public void update(bool updateOthers) { + path.A = nodes[0].position; + path.D = nodes[1].position; + if (nodes[0].roads.Count == 2) { + path.B = nodes[0].position + + 0.25f * (nodes[1].position - nodes[0].getOther(nodes[1]).position); + } else { + path.B = 0.5f * (nodes[0].position + nodes[1].position); + } + if (nodes[1].roads.Count == 2) { + path.C = nodes[1].position + + 0.25f * (nodes[0].position - nodes[1].getOther(nodes[0]).position); + } else { + path.C = 0.5f * (nodes[0].position + nodes[1].position); + } + if (updateOthers) { + foreach (Node node in nodes) { + node.lateUpdate(this); + } + } + pathLine.update(); + roadBody.update(); + collider.sharedMesh = roadBody.mesh; + } + + override public bool Equals(object other) { + if (other == null || !(other is Road)) { + return false; + } + if (other == this) { + return true; + } + Road otherRoad = (Road) other; + foreach (Node node in nodes) { + if (! otherRoad.nodes.Contains(node)) { + return false; + } + } + return true; + } + + public override int GetHashCode() { + return nodes[0].GetHashCode() << 16 | nodes[1].GetHashCode(); + } +} diff --git a/Assets/Scripts/Roads/Road.cs.meta b/Assets/Scripts/Roads/Road.cs.meta new file mode 100644 index 0000000..24dc94b --- /dev/null +++ b/Assets/Scripts/Roads/Road.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0ab512a631c0cc4468e77b6a2dfeb31f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Roads/RoadData.cs b/Assets/Scripts/Roads/RoadData.cs new file mode 100644 index 0000000..eb620ba --- /dev/null +++ b/Assets/Scripts/Roads/RoadData.cs @@ -0,0 +1,7 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class RoadData : MonoBehaviour { + public Road road; +} diff --git a/Assets/Scripts/Roads/RoadData.cs.meta b/Assets/Scripts/Roads/RoadData.cs.meta new file mode 100644 index 0000000..2948b97 --- /dev/null +++ b/Assets/Scripts/Roads/RoadData.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 6bfac654eff68754c88b877a9b88ae42 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Roads/RoadEdit.cs b/Assets/Scripts/Roads/RoadEdit.cs new file mode 100644 index 0000000..a6cc73e --- /dev/null +++ b/Assets/Scripts/Roads/RoadEdit.cs @@ -0,0 +1,43 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class RoadEdit : MonoBehaviour { + public GameObject start; + public GameObject startDirection; + public GameObject end; + public GameObject endDirection; + public Camera mainCamera; + + private GameObject currentlyPulling = null; + public Bezier bezier; + private FlatBezierRenderer pathRenderer; + public Material highlight, noHighlight; + + void Start() { + } + + void updateRoad(Vector3 position) { + } + + void Update() { + Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); + if (Input.GetAxis("Fire1") != 0.0f) { + if (currentlyPulling == null && Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, 1 << 7)) { + currentlyPulling = hit.transform.gameObject; + currentlyPulling.GetComponent().material = highlight; + } + } else { + if (currentlyPulling != null) { + currentlyPulling.GetComponent().material = noHighlight; + } + currentlyPulling = null; + } + if (currentlyPulling != null) { + Physics.Raycast(ray, out RaycastHit hit_, Mathf.Infinity, 1 << 6); + Vector3 position = new Vector3(hit_.point.x, 0.0f, hit_.point.z); + currentlyPulling.transform.position = position; + updateRoad(position); + } + } +} diff --git a/Assets/Scripts/Roads/RoadEdit.cs.meta b/Assets/Scripts/Roads/RoadEdit.cs.meta new file mode 100644 index 0000000..4e09d01 --- /dev/null +++ b/Assets/Scripts/Roads/RoadEdit.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 68e9412fa432f2e468d5d7447e99fd89 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Bezier.cs b/Assets/Scripts/Bezier.cs deleted file mode 100644 index ea19006..0000000 --- a/Assets/Scripts/Bezier.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Bezier { - public Vector3 A, B, C, D; - - public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { - this.A = A; - this.B = B; - this.C = C; - this.D = D; - } - - public Bezier() { - this.A = Vector3.zero; - this.B = Vector3.zero; - this.C = Vector3.zero; - this.D = Vector3.zero; - } - - public Vector3 getPosition(float t) { - float T = 1.0f - t; - return - A * ( T*T*T) + - B * (3*T*T*t) + - C * (3*T*t*t) + - D * ( t*t*t); - } - - public Vector3 getDirection(float t) { - float T = 1.0f - t; - return - A * (-3*T*T ) + - B * ( 3*T*T - 6*T*t) + - C * (-3*t*t + 6*T*t) + - D * ( 3*t*t ); - } -} diff --git a/Assets/Scripts/Bezier.cs.meta b/Assets/Scripts/Bezier.cs.meta deleted file mode 100644 index 14630e2..0000000 --- a/Assets/Scripts/Bezier.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 284cf9d52e385aa4ebba97eb8b43b8a6 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatBezierRenderer.cs b/Assets/Scripts/FlatBezierRenderer.cs deleted file mode 100644 index b8dad15..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatBezierRenderer { - private int percision; - private float width; - public Bezier bezier; - public Mesh mesh = new Mesh(); - public FlatBezierRenderer(Bezier bezier, int percision, float width) { - this.percision = percision; - this.width = width; - this.bezier = bezier; - update(); - } - - public void update() { - float realWidth = width / 2; - int vertexCount = 6 * (percision+1); - Vector3[] vertices = new Vector3[vertexCount / 3]; - int[] indices = new int[vertexCount]; - Vector3 position = bezier.getPosition(0.0f); - Vector3 tangent = getTangent(0.0f); - vertices[0] = position + tangent * realWidth; - vertices[1] = position - tangent * realWidth; - for (int i = 1; i <= percision; i++) { - float t = (float) i / percision; - indices[6*i ] = 2*i ; - indices[6*i+1] = 2*i-1; - indices[6*i+2] = 2*i-2; - indices[6*i+3] = 2*i+1; - indices[6*i+4] = 2*i-1; - indices[6*i+5] = 2*i ; - - position = bezier.getPosition(t); - tangent = getTangent(t); - vertices[2*i ] = position + tangent * realWidth; - vertices[2*i+1] = position - tangent * realWidth; - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - mesh.Optimize(); - } - - private Vector3 getTangent(float t) { - Vector3 direction = bezier.getDirection(t); - direction.Normalize(); - return new Vector3(-direction.z, 0, direction.x); - } -} diff --git a/Assets/Scripts/FlatBezierRenderer.cs.meta b/Assets/Scripts/FlatBezierRenderer.cs.meta deleted file mode 100644 index 1fe6547..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 9bfb303c44d15ef468dc0d7334241e94 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatCircleRenderer.cs b/Assets/Scripts/FlatCircleRenderer.cs deleted file mode 100644 index 363b663..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatCircleRenderer { - public Mesh mesh = new Mesh(); - public float radius, width; - private int segments; - - public FlatCircleRenderer(float radius, float width, int segments) { - this.radius = radius; - this.width = width; - this.segments = segments; - update(); - } - - public void update() { - Vector3[] vertices = new Vector3[2*segments+6]; - int[] indices = new int[6*segments]; - for (int i = 0; i < segments; i++) { - float alpha = (float) i / segments * 2 * Mathf.PI; - indices[6*i ] = 2*i ; - indices[6*i+3] = 2*i ; - indices[6*i+5] = 2*i+1; - if (i == 0) { - indices[6*i+1] = 2*segments-2; - indices[6*i+2] = 2*segments-1; - indices[6*i+4] = 2*segments-1; - } else { - indices[6*i+1] = 2*i-2; - indices[6*i+2] = 2*i-1; - indices[6*i+4] = 2*i-1; - } - Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); - vertices[2*i ] = direction * radius; - vertices[2*i+1] = direction * (radius + width); - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - } -} diff --git a/Assets/Scripts/FlatCircleRenderer.cs.meta b/Assets/Scripts/FlatCircleRenderer.cs.meta deleted file mode 100644 index d10c238..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0fe81c7924a85b142a4a86201b08c241 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Node.cs b/Assets/Scripts/Node.cs deleted file mode 100644 index e3c4cbd..0000000 --- a/Assets/Scripts/Node.cs +++ /dev/null @@ -1,81 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Node { - public FlatCircleRenderer circle = new FlatCircleRenderer(0.2f, 0.05f, 32); - public FlatCircleRenderer fullCircle = new FlatCircleRenderer(0f, 1f, 32); - - public Vector3 position; - public List roads = new List(); - public GameObject gameObject; - - public Node(Vector3 position, Transform parent, Config config) { - gameObject = new GameObject(); - gameObject.transform.position = position; - gameObject.transform.parent = parent; - gameObject.AddComponent().radius = 0.5f; - gameObject.layer = 7; - - GameObject nodeCircle = new GameObject(); - nodeCircle.AddComponent().material = config.roadEditMaterial; - nodeCircle.AddComponent().mesh = circle.mesh; - nodeCircle.transform.parent = gameObject.transform; - nodeCircle.transform.localPosition = Vector3.zero; - - GameObject nodeRoad = new GameObject(); - nodeRoad.AddComponent().material = config.roadMaterial; - nodeRoad.AddComponent().mesh = fullCircle.mesh; - nodeRoad.transform.parent = gameObject.transform; - nodeRoad.transform.localPosition = Vector3.zero; - 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(); - } - - override public bool Equals(object other) { - if ((other == null) || ! this.GetType().Equals(other.GetType())) { - return false; - } - if (other == this) { - return true; - } - return ((Node)other).position.Equals(position); - } - - public void pull(Vector3 position) { - this.position = position; - gameObject.transform.position = position; - foreach (Road road in roads) { - 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/Node.cs.meta b/Assets/Scripts/Node.cs.meta deleted file mode 100644 index 8bbedf1..0000000 --- a/Assets/Scripts/Node.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 3cbed1dad7ec67d4dbe8602aea1e2979 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Primitives.meta b/Assets/Scripts/Primitives.meta new file mode 100644 index 0000000..0cf8895 --- /dev/null +++ b/Assets/Scripts/Primitives.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 6d1a8c7f33189a240b4d749356f53399 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/Bezier.cs b/Assets/Scripts/Primitives/Bezier.cs new file mode 100644 index 0000000..ea19006 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs @@ -0,0 +1,39 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Bezier { + public Vector3 A, B, C, D; + + public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { + this.A = A; + this.B = B; + this.C = C; + this.D = D; + } + + public Bezier() { + this.A = Vector3.zero; + this.B = Vector3.zero; + this.C = Vector3.zero; + this.D = Vector3.zero; + } + + public Vector3 getPosition(float t) { + float T = 1.0f - t; + return + A * ( T*T*T) + + B * (3*T*T*t) + + C * (3*T*t*t) + + D * ( t*t*t); + } + + public Vector3 getDirection(float t) { + float T = 1.0f - t; + return + A * (-3*T*T ) + + B * ( 3*T*T - 6*T*t) + + C * (-3*t*t + 6*T*t) + + D * ( 3*t*t ); + } +} diff --git a/Assets/Scripts/Primitives/Bezier.cs.meta b/Assets/Scripts/Primitives/Bezier.cs.meta new file mode 100644 index 0000000..14630e2 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 284cf9d52e385aa4ebba97eb8b43b8a6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs b/Assets/Scripts/Primitives/FlatBezierRenderer.cs new file mode 100644 index 0000000..b8dad15 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs @@ -0,0 +1,51 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FlatBezierRenderer { + private int percision; + private float width; + public Bezier bezier; + public Mesh mesh = new Mesh(); + public FlatBezierRenderer(Bezier bezier, int percision, float width) { + this.percision = percision; + this.width = width; + this.bezier = bezier; + update(); + } + + public void update() { + float realWidth = width / 2; + int vertexCount = 6 * (percision+1); + Vector3[] vertices = new Vector3[vertexCount / 3]; + int[] indices = new int[vertexCount]; + Vector3 position = bezier.getPosition(0.0f); + Vector3 tangent = getTangent(0.0f); + vertices[0] = position + tangent * realWidth; + vertices[1] = position - tangent * realWidth; + for (int i = 1; i <= percision; i++) { + float t = (float) i / percision; + indices[6*i ] = 2*i ; + indices[6*i+1] = 2*i-1; + indices[6*i+2] = 2*i-2; + indices[6*i+3] = 2*i+1; + indices[6*i+4] = 2*i-1; + indices[6*i+5] = 2*i ; + + position = bezier.getPosition(t); + tangent = getTangent(t); + vertices[2*i ] = position + tangent * realWidth; + vertices[2*i+1] = position - tangent * realWidth; + } + mesh.Clear(); + mesh.vertices = vertices; + mesh.triangles = indices; + mesh.Optimize(); + } + + private Vector3 getTangent(float t) { + Vector3 direction = bezier.getDirection(t); + direction.Normalize(); + return new Vector3(-direction.z, 0, direction.x); + } +} diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta b/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta new file mode 100644 index 0000000..1fe6547 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9bfb303c44d15ef468dc0d7334241e94 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/FlatCircleRenderer.cs b/Assets/Scripts/Primitives/FlatCircleRenderer.cs new file mode 100644 index 0000000..363b663 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatCircleRenderer.cs @@ -0,0 +1,42 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FlatCircleRenderer { + public Mesh mesh = new Mesh(); + public float radius, width; + private int segments; + + public FlatCircleRenderer(float radius, float width, int segments) { + this.radius = radius; + this.width = width; + this.segments = segments; + update(); + } + + public void update() { + Vector3[] vertices = new Vector3[2*segments+6]; + int[] indices = new int[6*segments]; + for (int i = 0; i < segments; i++) { + float alpha = (float) i / segments * 2 * Mathf.PI; + indices[6*i ] = 2*i ; + indices[6*i+3] = 2*i ; + indices[6*i+5] = 2*i+1; + if (i == 0) { + indices[6*i+1] = 2*segments-2; + indices[6*i+2] = 2*segments-1; + indices[6*i+4] = 2*segments-1; + } else { + indices[6*i+1] = 2*i-2; + indices[6*i+2] = 2*i-1; + indices[6*i+4] = 2*i-1; + } + Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); + vertices[2*i ] = direction * radius; + vertices[2*i+1] = direction * (radius + width); + } + mesh.Clear(); + mesh.vertices = vertices; + mesh.triangles = indices; + } +} diff --git a/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta b/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta new file mode 100644 index 0000000..d10c238 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0fe81c7924a85b142a4a86201b08c241 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Road.cs b/Assets/Scripts/Road.cs deleted file mode 100644 index 0af2218..0000000 --- a/Assets/Scripts/Road.cs +++ /dev/null @@ -1,85 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Road { - public List nodes = new List(); - Bezier path; - FlatBezierRenderer pathLine, roadBody; - Config config; - MeshCollider collider; - - public Road(Node start, Node end, Config config) { - nodes.Add(start); - nodes.Add(end); - path = new Bezier(); - pathLine = new FlatBezierRenderer(path, 50, 0.05f); - roadBody = new FlatBezierRenderer(path, 50, 2f); - this.config = config; - } - - - public void initialize(Transform parent) { - GameObject child = new GameObject(); - child.transform.parent = parent; - GameObject lineChild = new GameObject(); - lineChild.transform.position = Vector3.zero; - lineChild.AddComponent().material = config.roadEditMaterial; - lineChild.AddComponent().mesh = pathLine.mesh; - lineChild.transform.parent = child.transform; - - GameObject roadChild = new GameObject(); - collider = roadChild.AddComponent(); - roadChild.transform.position = Vector3.zero; - roadChild.AddComponent().material = config.roadMaterial; - roadChild.transform.parent = child.transform; - roadChild.layer = 8; - roadChild.AddComponent().mesh = roadBody.mesh; - roadChild.AddComponent().road = this; - } - - public void update(bool updateOthers) { - path.A = nodes[0].position; - path.D = nodes[1].position; - if (nodes[0].roads.Count == 2) { - path.B = nodes[0].position + - 0.25f * (nodes[1].position - nodes[0].getOther(nodes[1]).position); - } else { - path.B = 0.5f * (nodes[0].position + nodes[1].position); - } - if (nodes[1].roads.Count == 2) { - path.C = nodes[1].position + - 0.25f * (nodes[0].position - nodes[1].getOther(nodes[0]).position); - } else { - path.C = 0.5f * (nodes[0].position + nodes[1].position); - } - if (updateOthers) { - foreach (Node node in nodes) { - node.lateUpdate(this); - } - } - pathLine.update(); - roadBody.update(); - collider.sharedMesh = roadBody.mesh; - } - - override public bool Equals(object other) { - if (other == null || !(other is Road)) { - return false; - } - if (other == this) { - return true; - } - Road otherRoad = (Road) other; - foreach (Node node in nodes) { - if (! otherRoad.nodes.Contains(node)) { - return false; - } - } - return true; - } - - public override int GetHashCode() { - return nodes[0].GetHashCode() << 16 | nodes[1].GetHashCode(); - } -} diff --git a/Assets/Scripts/Road.cs.meta b/Assets/Scripts/Road.cs.meta deleted file mode 100644 index 24dc94b..0000000 --- a/Assets/Scripts/Road.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0ab512a631c0cc4468e77b6a2dfeb31f -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/RoadData.cs b/Assets/Scripts/RoadData.cs deleted file mode 100644 index eb620ba..0000000 --- a/Assets/Scripts/RoadData.cs +++ /dev/null @@ -1,7 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class RoadData : MonoBehaviour { - public Road road; -} diff --git a/Assets/Scripts/RoadData.cs.meta b/Assets/Scripts/RoadData.cs.meta deleted file mode 100644 index 2948b97..0000000 --- a/Assets/Scripts/RoadData.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 6bfac654eff68754c88b877a9b88ae42 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/RoadEdit.cs b/Assets/Scripts/RoadEdit.cs deleted file mode 100644 index a6cc73e..0000000 --- a/Assets/Scripts/RoadEdit.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class RoadEdit : MonoBehaviour { - public GameObject start; - public GameObject startDirection; - public GameObject end; - public GameObject endDirection; - public Camera mainCamera; - - private GameObject currentlyPulling = null; - public Bezier bezier; - private FlatBezierRenderer pathRenderer; - public Material highlight, noHighlight; - - void Start() { - } - - void updateRoad(Vector3 position) { - } - - void Update() { - Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); - if (Input.GetAxis("Fire1") != 0.0f) { - if (currentlyPulling == null && Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, 1 << 7)) { - currentlyPulling = hit.transform.gameObject; - currentlyPulling.GetComponent().material = highlight; - } - } else { - if (currentlyPulling != null) { - currentlyPulling.GetComponent().material = noHighlight; - } - currentlyPulling = null; - } - if (currentlyPulling != null) { - Physics.Raycast(ray, out RaycastHit hit_, Mathf.Infinity, 1 << 6); - Vector3 position = new Vector3(hit_.point.x, 0.0f, hit_.point.z); - currentlyPulling.transform.position = position; - updateRoad(position); - } - } -} diff --git a/Assets/Scripts/RoadEdit.cs.meta b/Assets/Scripts/RoadEdit.cs.meta deleted file mode 100644 index 4e09d01..0000000 --- a/Assets/Scripts/RoadEdit.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 68e9412fa432f2e468d5d7447e99fd89 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Roads.cs b/Assets/Scripts/Roads.cs deleted file mode 100644 index acd376a..0000000 --- a/Assets/Scripts/Roads.cs +++ /dev/null @@ -1,102 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Roads : MonoBehaviour { - public List nodes = new List(); - public List roads = new List(); - public FlatBezierRenderer roadRenderer; - public Camera mainCamera; - private bool drawing = false; - public Material material; - public Config config; - - void Start() { - } - - private Node startNode; - - private void startRoad() { - Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out RaycastHit hit, Mathf.Infinity, 1 << 6); - foreach (Node node in nodes) { - if ((hit.point - node.position).magnitude < 1.0f) { - startNode = node; - drawing = true; - return; - } - } - Vector3 position = new Vector3(hit.point.x, 0.0f, hit.point.z); - startNode = new Node(position, transform, config); - nodes.Add(startNode); - drawing = true; - } - - private void endRoad() { - Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out RaycastHit hit, Mathf.Infinity, 1 << 6); - Node endNode = null; - foreach (Node node in nodes) { - if ((hit.point - node.position).magnitude < 1.0f) { - endNode = node; - break; - } - } - if (endNode == null) { - Vector3 position = new Vector3(hit.point.x, 0.0f, hit.point.z); - endNode = new Node(position, transform, config); - nodes.Add(endNode); - } - Road road = new Road(startNode, endNode, config); - if (!roads.Contains(road) && startNode != endNode) { - startNode.roads.Add(road); - endNode.roads.Add(road); - roads.Add(road); - road.initialize(transform); - road.update(true); - } - startNode = null; - drawing = false; - } - - Node pulling = null; - - void Update() { - Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); - RaycastHit hit; - if (config.mode == Mode.DrawRoad) { - if (Input.GetAxis("Fire1") != 0.0f) { - if (!drawing) { - startRoad(); - } - } else if (drawing) { - endRoad(); - } - } else if (config.mode == Mode.DeleteRoad) { - if (!drawing && Input.GetAxis("Fire1") != 0.0f) { - if (Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 8)) { - drawing = true; - Road road = hit.transform.gameObject.GetComponent().road; - roads.Remove(road); - foreach (Node node in road.nodes) { - node.roads.Remove(road); - node.update(); - } - Destroy(hit.transform.parent.gameObject); - } - } else if (drawing && Input.GetAxis("Fire1") == 0.0f) { - drawing = false; - } - } - if (Input.GetAxis("Fire2") != 0.0f) { - if (pulling == null && Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 7)) { - pulling = nodes.Find(node => node.gameObject == hit.transform.gameObject); - } - } else { - pulling = null; - } - if (pulling != null) { - Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 6); - Vector3 position = new Vector3(hit.point.x, 0.0f, hit.point.z); - pulling.pull(position); - } - } -} diff --git a/Assets/Scripts/Roads.cs.meta b/Assets/Scripts/Roads.cs.meta deleted file mode 100644 index f26a401..0000000 --- a/Assets/Scripts/Roads.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 7635c530aa8ef31498f100f4e7465bea -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Roads.meta b/Assets/Scripts/Roads.meta new file mode 100644 index 0000000..defbd46 --- /dev/null +++ b/Assets/Scripts/Roads.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: e6a3f10ad4786e44d9faf825f11a8e1d +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Roads/Node.cs b/Assets/Scripts/Roads/Node.cs new file mode 100644 index 0000000..3b2f31a --- /dev/null +++ b/Assets/Scripts/Roads/Node.cs @@ -0,0 +1,82 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Node { + private FlatCircleRenderer circle = new FlatCircleRenderer(0.2f, 0.05f, 32); + private FlatCircleRenderer fullCircle = new FlatCircleRenderer(0f, 1f, 32); + + public Vector3 position; + public List roads = new List(); + public GameObject gameObject; + + public Node(Vector3 position, Transform parent, Config config) { + gameObject = new GameObject(); + gameObject.transform.position = position; + gameObject.transform.parent = parent; + gameObject.AddComponent().radius = 2f; + gameObject.layer = 7; + gameObject.AddComponent().node = this; + + GameObject nodeCircle = new GameObject(); + nodeCircle.AddComponent().material = config.roadEditMaterial; + nodeCircle.AddComponent().mesh = circle.mesh; + nodeCircle.transform.parent = gameObject.transform; + nodeCircle.transform.localPosition = Vector3.zero; + + GameObject nodeRoad = new GameObject(); + nodeRoad.AddComponent().material = config.roadMaterial; + nodeRoad.AddComponent().mesh = fullCircle.mesh; + nodeRoad.transform.parent = gameObject.transform; + nodeRoad.transform.localPosition = Vector3.zero; + 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(); + } + + override public bool Equals(object other) { + if ((other == null) || ! this.GetType().Equals(other.GetType())) { + return false; + } + if (other == this) { + return true; + } + return ((Node)other).position.Equals(position); + } + + public void pull(Vector3 position) { + this.position = position; + gameObject.transform.position = position; + foreach (Road road in roads) { + 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/Roads/Node.cs.meta b/Assets/Scripts/Roads/Node.cs.meta new file mode 100644 index 0000000..8bbedf1 --- /dev/null +++ b/Assets/Scripts/Roads/Node.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 3cbed1dad7ec67d4dbe8602aea1e2979 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Roads/NodeData.cs b/Assets/Scripts/Roads/NodeData.cs new file mode 100644 index 0000000..9ee8ace --- /dev/null +++ b/Assets/Scripts/Roads/NodeData.cs @@ -0,0 +1,7 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class NodeData : MonoBehaviour { + public Node node = null; +} diff --git a/Assets/Scripts/Roads/NodeData.cs.meta b/Assets/Scripts/Roads/NodeData.cs.meta new file mode 100644 index 0000000..73dcf56 --- /dev/null +++ b/Assets/Scripts/Roads/NodeData.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4cf9bcd8c4889de4985e5d6a776acc5b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Roads/Road.cs b/Assets/Scripts/Roads/Road.cs new file mode 100644 index 0000000..1ad1a74 --- /dev/null +++ b/Assets/Scripts/Roads/Road.cs @@ -0,0 +1,88 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Road { + public List nodes = new List(); + Bezier path; + private FlatBezierRenderer pathLine, roadBody; + private Config config; + private MeshCollider collider; + public GameObject gameObject; + + public Road(Node start, Node end, Config config) { + nodes.Add(start); + nodes.Add(end); + path = new Bezier(); + pathLine = new FlatBezierRenderer(path, 50, 0.05f); + roadBody = new FlatBezierRenderer(path, 50, 2f); + this.config = config; + } + + + public void initialize(Transform parent) { + gameObject = new GameObject(); + gameObject.transform.parent = parent; + gameObject.transform.position = Vector3.zero; + + GameObject lineChild = new GameObject(); + lineChild.transform.position = Vector3.zero; + lineChild.AddComponent().material = config.roadEditMaterial; + lineChild.AddComponent().mesh = pathLine.mesh; + lineChild.transform.parent = gameObject.transform; + + GameObject bodyChild = new GameObject(); + bodyChild.AddComponent().material = config.roadMaterial; + bodyChild.AddComponent().mesh = roadBody.mesh; + bodyChild.AddComponent().road = this; + collider = bodyChild.AddComponent(); + bodyChild.transform.parent = gameObject.transform; + bodyChild.layer = 8; + update(true); + } + + public void update(bool updateOthers) { + path.A = nodes[0].position; + path.D = nodes[1].position; + if (nodes[0].roads.Count == 2) { + path.B = nodes[0].position + + 0.25f * (nodes[1].position - nodes[0].getOther(nodes[1]).position); + } else { + path.B = 0.5f * (nodes[0].position + nodes[1].position); + } + if (nodes[1].roads.Count == 2) { + path.C = nodes[1].position + + 0.25f * (nodes[0].position - nodes[1].getOther(nodes[0]).position); + } else { + path.C = 0.5f * (nodes[0].position + nodes[1].position); + } + if (updateOthers) { + foreach (Node node in nodes) { + node.lateUpdate(this); + } + } + pathLine.update(); + roadBody.update(); + collider.sharedMesh = roadBody.mesh; + } + + override public bool Equals(object other) { + if (other == null || !(other is Road)) { + return false; + } + if (other == this) { + return true; + } + Road otherRoad = (Road) other; + foreach (Node node in nodes) { + if (! otherRoad.nodes.Contains(node)) { + return false; + } + } + return true; + } + + public override int GetHashCode() { + return nodes[0].GetHashCode() << 16 | nodes[1].GetHashCode(); + } +} diff --git a/Assets/Scripts/Roads/Road.cs.meta b/Assets/Scripts/Roads/Road.cs.meta new file mode 100644 index 0000000..24dc94b --- /dev/null +++ b/Assets/Scripts/Roads/Road.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0ab512a631c0cc4468e77b6a2dfeb31f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Roads/RoadData.cs b/Assets/Scripts/Roads/RoadData.cs new file mode 100644 index 0000000..eb620ba --- /dev/null +++ b/Assets/Scripts/Roads/RoadData.cs @@ -0,0 +1,7 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class RoadData : MonoBehaviour { + public Road road; +} diff --git a/Assets/Scripts/Roads/RoadData.cs.meta b/Assets/Scripts/Roads/RoadData.cs.meta new file mode 100644 index 0000000..2948b97 --- /dev/null +++ b/Assets/Scripts/Roads/RoadData.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 6bfac654eff68754c88b877a9b88ae42 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Roads/RoadEdit.cs b/Assets/Scripts/Roads/RoadEdit.cs new file mode 100644 index 0000000..a6cc73e --- /dev/null +++ b/Assets/Scripts/Roads/RoadEdit.cs @@ -0,0 +1,43 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class RoadEdit : MonoBehaviour { + public GameObject start; + public GameObject startDirection; + public GameObject end; + public GameObject endDirection; + public Camera mainCamera; + + private GameObject currentlyPulling = null; + public Bezier bezier; + private FlatBezierRenderer pathRenderer; + public Material highlight, noHighlight; + + void Start() { + } + + void updateRoad(Vector3 position) { + } + + void Update() { + Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); + if (Input.GetAxis("Fire1") != 0.0f) { + if (currentlyPulling == null && Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, 1 << 7)) { + currentlyPulling = hit.transform.gameObject; + currentlyPulling.GetComponent().material = highlight; + } + } else { + if (currentlyPulling != null) { + currentlyPulling.GetComponent().material = noHighlight; + } + currentlyPulling = null; + } + if (currentlyPulling != null) { + Physics.Raycast(ray, out RaycastHit hit_, Mathf.Infinity, 1 << 6); + Vector3 position = new Vector3(hit_.point.x, 0.0f, hit_.point.z); + currentlyPulling.transform.position = position; + updateRoad(position); + } + } +} diff --git a/Assets/Scripts/Roads/RoadEdit.cs.meta b/Assets/Scripts/Roads/RoadEdit.cs.meta new file mode 100644 index 0000000..4e09d01 --- /dev/null +++ b/Assets/Scripts/Roads/RoadEdit.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 68e9412fa432f2e468d5d7447e99fd89 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Roads/Roads.cs b/Assets/Scripts/Roads/Roads.cs new file mode 100644 index 0000000..02923e2 --- /dev/null +++ b/Assets/Scripts/Roads/Roads.cs @@ -0,0 +1,120 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public enum DrawMode { + None, + DrawRoad, + DragRoad, + DeleteRoad +} + +public class Roads : MonoBehaviour { + public Camera mainCamera; + public Material material; + public Config config; + public DrawMode drawMode = DrawMode.None; + + void Start() { + } + + private Node pullingNode = null; + + private void startRoad(Ray ray, Vector3 groundPosition) { + drawMode = DrawMode.DrawRoad; + if (Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, 1 << 7)) { + pullingNode = hit.transform.gameObject.GetComponent().node; + return; + } + pullingNode = new Node(groundPosition, transform, config); + } + + private void endRoad(Ray ray, Vector3 groundPosition) { + drawMode = DrawMode.None; + Node endNode = null; + if (Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, 1 << 7)) { + endNode = hit.transform.gameObject.GetComponent().node; + } else { + endNode = new Node(groundPosition, transform, config); + } + Road road = new Road(pullingNode, endNode, config); + if (! pullingNode.roads.Contains(road) && pullingNode != endNode) { + pullingNode.roads.Add(road); + endNode.roads.Add(road); + road.initialize(transform); + } + pullingNode = null; + } + + private void handleRoadDrawing(Ray ray, Vector3 groundPosition) { + if (Input.GetAxis("Fire1") == 1.0f) { + if (drawMode == DrawMode.None) { + startRoad(ray, groundPosition); + } + } else if (drawMode == DrawMode.DrawRoad) { + endRoad(ray, groundPosition); + } + } + + private void removeRoad(Road road) { + foreach (Node node in road.nodes) { + node.roads.Remove(road); + if (node.roads.Count == 0) { + removeNode(node); + } else { + node.update(); + } + } + Destroy(road.gameObject); + } + + private void removeNode(Node node) { + foreach (Road road in node.roads) { + road.nodes.Remove(node); + removeRoad(road); + } + Destroy(node.gameObject); + } + + private void handleRoadRemoving(Ray ray) { + if (drawMode == DrawMode.None && Input.GetAxis("Fire1") != 0.0f) { + RaycastHit hit; + if (Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 7)) { + Node node = hit.transform.gameObject.GetComponent().node; + removeNode(node); + drawMode = DrawMode.DeleteRoad; + } else if (Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 8)) { + Road road = hit.transform.gameObject.GetComponent().road; + print("road"); + removeRoad(road); + drawMode = DrawMode.DeleteRoad; + } + } else if (drawMode == DrawMode.DeleteRoad && Input.GetAxis("Fire1") == 0.0f) { + drawMode = DrawMode.None; + } + } + + void Update() { + Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); + RaycastHit hit; + Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 6); + Vector3 groundPosition = new Vector3(hit.point.x, 0.0f, hit.point.z); + + if (config.mode == Mode.DrawRoad) { + handleRoadDrawing(ray, groundPosition); + } else if (config.mode == Mode.DeleteRoad) { + handleRoadRemoving(ray); + } + if (Input.GetAxis("Fire2") != 0.0f) { + if (drawMode == DrawMode.None && Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 7)) { + drawMode = DrawMode.DragRoad; + pullingNode = hit.transform.gameObject.GetComponent().node; + } + } else if (drawMode == DrawMode.DragRoad) { + drawMode = DrawMode.None; + } + if (drawMode == DrawMode.DragRoad) { + pullingNode.pull(groundPosition); + } + } +} diff --git a/Assets/Scripts/Bezier.cs b/Assets/Scripts/Bezier.cs deleted file mode 100644 index ea19006..0000000 --- a/Assets/Scripts/Bezier.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Bezier { - public Vector3 A, B, C, D; - - public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { - this.A = A; - this.B = B; - this.C = C; - this.D = D; - } - - public Bezier() { - this.A = Vector3.zero; - this.B = Vector3.zero; - this.C = Vector3.zero; - this.D = Vector3.zero; - } - - public Vector3 getPosition(float t) { - float T = 1.0f - t; - return - A * ( T*T*T) + - B * (3*T*T*t) + - C * (3*T*t*t) + - D * ( t*t*t); - } - - public Vector3 getDirection(float t) { - float T = 1.0f - t; - return - A * (-3*T*T ) + - B * ( 3*T*T - 6*T*t) + - C * (-3*t*t + 6*T*t) + - D * ( 3*t*t ); - } -} diff --git a/Assets/Scripts/Bezier.cs.meta b/Assets/Scripts/Bezier.cs.meta deleted file mode 100644 index 14630e2..0000000 --- a/Assets/Scripts/Bezier.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 284cf9d52e385aa4ebba97eb8b43b8a6 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatBezierRenderer.cs b/Assets/Scripts/FlatBezierRenderer.cs deleted file mode 100644 index b8dad15..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatBezierRenderer { - private int percision; - private float width; - public Bezier bezier; - public Mesh mesh = new Mesh(); - public FlatBezierRenderer(Bezier bezier, int percision, float width) { - this.percision = percision; - this.width = width; - this.bezier = bezier; - update(); - } - - public void update() { - float realWidth = width / 2; - int vertexCount = 6 * (percision+1); - Vector3[] vertices = new Vector3[vertexCount / 3]; - int[] indices = new int[vertexCount]; - Vector3 position = bezier.getPosition(0.0f); - Vector3 tangent = getTangent(0.0f); - vertices[0] = position + tangent * realWidth; - vertices[1] = position - tangent * realWidth; - for (int i = 1; i <= percision; i++) { - float t = (float) i / percision; - indices[6*i ] = 2*i ; - indices[6*i+1] = 2*i-1; - indices[6*i+2] = 2*i-2; - indices[6*i+3] = 2*i+1; - indices[6*i+4] = 2*i-1; - indices[6*i+5] = 2*i ; - - position = bezier.getPosition(t); - tangent = getTangent(t); - vertices[2*i ] = position + tangent * realWidth; - vertices[2*i+1] = position - tangent * realWidth; - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - mesh.Optimize(); - } - - private Vector3 getTangent(float t) { - Vector3 direction = bezier.getDirection(t); - direction.Normalize(); - return new Vector3(-direction.z, 0, direction.x); - } -} diff --git a/Assets/Scripts/FlatBezierRenderer.cs.meta b/Assets/Scripts/FlatBezierRenderer.cs.meta deleted file mode 100644 index 1fe6547..0000000 --- a/Assets/Scripts/FlatBezierRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 9bfb303c44d15ef468dc0d7334241e94 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/FlatCircleRenderer.cs b/Assets/Scripts/FlatCircleRenderer.cs deleted file mode 100644 index 363b663..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class FlatCircleRenderer { - public Mesh mesh = new Mesh(); - public float radius, width; - private int segments; - - public FlatCircleRenderer(float radius, float width, int segments) { - this.radius = radius; - this.width = width; - this.segments = segments; - update(); - } - - public void update() { - Vector3[] vertices = new Vector3[2*segments+6]; - int[] indices = new int[6*segments]; - for (int i = 0; i < segments; i++) { - float alpha = (float) i / segments * 2 * Mathf.PI; - indices[6*i ] = 2*i ; - indices[6*i+3] = 2*i ; - indices[6*i+5] = 2*i+1; - if (i == 0) { - indices[6*i+1] = 2*segments-2; - indices[6*i+2] = 2*segments-1; - indices[6*i+4] = 2*segments-1; - } else { - indices[6*i+1] = 2*i-2; - indices[6*i+2] = 2*i-1; - indices[6*i+4] = 2*i-1; - } - Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); - vertices[2*i ] = direction * radius; - vertices[2*i+1] = direction * (radius + width); - } - mesh.Clear(); - mesh.vertices = vertices; - mesh.triangles = indices; - } -} diff --git a/Assets/Scripts/FlatCircleRenderer.cs.meta b/Assets/Scripts/FlatCircleRenderer.cs.meta deleted file mode 100644 index d10c238..0000000 --- a/Assets/Scripts/FlatCircleRenderer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0fe81c7924a85b142a4a86201b08c241 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Node.cs b/Assets/Scripts/Node.cs deleted file mode 100644 index e3c4cbd..0000000 --- a/Assets/Scripts/Node.cs +++ /dev/null @@ -1,81 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Node { - public FlatCircleRenderer circle = new FlatCircleRenderer(0.2f, 0.05f, 32); - public FlatCircleRenderer fullCircle = new FlatCircleRenderer(0f, 1f, 32); - - public Vector3 position; - public List roads = new List(); - public GameObject gameObject; - - public Node(Vector3 position, Transform parent, Config config) { - gameObject = new GameObject(); - gameObject.transform.position = position; - gameObject.transform.parent = parent; - gameObject.AddComponent().radius = 0.5f; - gameObject.layer = 7; - - GameObject nodeCircle = new GameObject(); - nodeCircle.AddComponent().material = config.roadEditMaterial; - nodeCircle.AddComponent().mesh = circle.mesh; - nodeCircle.transform.parent = gameObject.transform; - nodeCircle.transform.localPosition = Vector3.zero; - - GameObject nodeRoad = new GameObject(); - nodeRoad.AddComponent().material = config.roadMaterial; - nodeRoad.AddComponent().mesh = fullCircle.mesh; - nodeRoad.transform.parent = gameObject.transform; - nodeRoad.transform.localPosition = Vector3.zero; - 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(); - } - - override public bool Equals(object other) { - if ((other == null) || ! this.GetType().Equals(other.GetType())) { - return false; - } - if (other == this) { - return true; - } - return ((Node)other).position.Equals(position); - } - - public void pull(Vector3 position) { - this.position = position; - gameObject.transform.position = position; - foreach (Road road in roads) { - 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/Node.cs.meta b/Assets/Scripts/Node.cs.meta deleted file mode 100644 index 8bbedf1..0000000 --- a/Assets/Scripts/Node.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 3cbed1dad7ec67d4dbe8602aea1e2979 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Primitives.meta b/Assets/Scripts/Primitives.meta new file mode 100644 index 0000000..0cf8895 --- /dev/null +++ b/Assets/Scripts/Primitives.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 6d1a8c7f33189a240b4d749356f53399 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/Bezier.cs b/Assets/Scripts/Primitives/Bezier.cs new file mode 100644 index 0000000..ea19006 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs @@ -0,0 +1,39 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Bezier { + public Vector3 A, B, C, D; + + public Bezier(Vector3 A, Vector3 B, Vector3 C, Vector3 D) { + this.A = A; + this.B = B; + this.C = C; + this.D = D; + } + + public Bezier() { + this.A = Vector3.zero; + this.B = Vector3.zero; + this.C = Vector3.zero; + this.D = Vector3.zero; + } + + public Vector3 getPosition(float t) { + float T = 1.0f - t; + return + A * ( T*T*T) + + B * (3*T*T*t) + + C * (3*T*t*t) + + D * ( t*t*t); + } + + public Vector3 getDirection(float t) { + float T = 1.0f - t; + return + A * (-3*T*T ) + + B * ( 3*T*T - 6*T*t) + + C * (-3*t*t + 6*T*t) + + D * ( 3*t*t ); + } +} diff --git a/Assets/Scripts/Primitives/Bezier.cs.meta b/Assets/Scripts/Primitives/Bezier.cs.meta new file mode 100644 index 0000000..14630e2 --- /dev/null +++ b/Assets/Scripts/Primitives/Bezier.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 284cf9d52e385aa4ebba97eb8b43b8a6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs b/Assets/Scripts/Primitives/FlatBezierRenderer.cs new file mode 100644 index 0000000..b8dad15 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs @@ -0,0 +1,51 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FlatBezierRenderer { + private int percision; + private float width; + public Bezier bezier; + public Mesh mesh = new Mesh(); + public FlatBezierRenderer(Bezier bezier, int percision, float width) { + this.percision = percision; + this.width = width; + this.bezier = bezier; + update(); + } + + public void update() { + float realWidth = width / 2; + int vertexCount = 6 * (percision+1); + Vector3[] vertices = new Vector3[vertexCount / 3]; + int[] indices = new int[vertexCount]; + Vector3 position = bezier.getPosition(0.0f); + Vector3 tangent = getTangent(0.0f); + vertices[0] = position + tangent * realWidth; + vertices[1] = position - tangent * realWidth; + for (int i = 1; i <= percision; i++) { + float t = (float) i / percision; + indices[6*i ] = 2*i ; + indices[6*i+1] = 2*i-1; + indices[6*i+2] = 2*i-2; + indices[6*i+3] = 2*i+1; + indices[6*i+4] = 2*i-1; + indices[6*i+5] = 2*i ; + + position = bezier.getPosition(t); + tangent = getTangent(t); + vertices[2*i ] = position + tangent * realWidth; + vertices[2*i+1] = position - tangent * realWidth; + } + mesh.Clear(); + mesh.vertices = vertices; + mesh.triangles = indices; + mesh.Optimize(); + } + + private Vector3 getTangent(float t) { + Vector3 direction = bezier.getDirection(t); + direction.Normalize(); + return new Vector3(-direction.z, 0, direction.x); + } +} diff --git a/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta b/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta new file mode 100644 index 0000000..1fe6547 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatBezierRenderer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9bfb303c44d15ef468dc0d7334241e94 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Primitives/FlatCircleRenderer.cs b/Assets/Scripts/Primitives/FlatCircleRenderer.cs new file mode 100644 index 0000000..363b663 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatCircleRenderer.cs @@ -0,0 +1,42 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class FlatCircleRenderer { + public Mesh mesh = new Mesh(); + public float radius, width; + private int segments; + + public FlatCircleRenderer(float radius, float width, int segments) { + this.radius = radius; + this.width = width; + this.segments = segments; + update(); + } + + public void update() { + Vector3[] vertices = new Vector3[2*segments+6]; + int[] indices = new int[6*segments]; + for (int i = 0; i < segments; i++) { + float alpha = (float) i / segments * 2 * Mathf.PI; + indices[6*i ] = 2*i ; + indices[6*i+3] = 2*i ; + indices[6*i+5] = 2*i+1; + if (i == 0) { + indices[6*i+1] = 2*segments-2; + indices[6*i+2] = 2*segments-1; + indices[6*i+4] = 2*segments-1; + } else { + indices[6*i+1] = 2*i-2; + indices[6*i+2] = 2*i-1; + indices[6*i+4] = 2*i-1; + } + Vector3 direction = new Vector3(Mathf.Sin(alpha), 0.0f, Mathf.Cos(alpha)); + vertices[2*i ] = direction * radius; + vertices[2*i+1] = direction * (radius + width); + } + mesh.Clear(); + mesh.vertices = vertices; + mesh.triangles = indices; + } +} diff --git a/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta b/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta new file mode 100644 index 0000000..d10c238 --- /dev/null +++ b/Assets/Scripts/Primitives/FlatCircleRenderer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0fe81c7924a85b142a4a86201b08c241 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Road.cs b/Assets/Scripts/Road.cs deleted file mode 100644 index 0af2218..0000000 --- a/Assets/Scripts/Road.cs +++ /dev/null @@ -1,85 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Road { - public List nodes = new List(); - Bezier path; - FlatBezierRenderer pathLine, roadBody; - Config config; - MeshCollider collider; - - public Road(Node start, Node end, Config config) { - nodes.Add(start); - nodes.Add(end); - path = new Bezier(); - pathLine = new FlatBezierRenderer(path, 50, 0.05f); - roadBody = new FlatBezierRenderer(path, 50, 2f); - this.config = config; - } - - - public void initialize(Transform parent) { - GameObject child = new GameObject(); - child.transform.parent = parent; - GameObject lineChild = new GameObject(); - lineChild.transform.position = Vector3.zero; - lineChild.AddComponent().material = config.roadEditMaterial; - lineChild.AddComponent().mesh = pathLine.mesh; - lineChild.transform.parent = child.transform; - - GameObject roadChild = new GameObject(); - collider = roadChild.AddComponent(); - roadChild.transform.position = Vector3.zero; - roadChild.AddComponent().material = config.roadMaterial; - roadChild.transform.parent = child.transform; - roadChild.layer = 8; - roadChild.AddComponent().mesh = roadBody.mesh; - roadChild.AddComponent().road = this; - } - - public void update(bool updateOthers) { - path.A = nodes[0].position; - path.D = nodes[1].position; - if (nodes[0].roads.Count == 2) { - path.B = nodes[0].position + - 0.25f * (nodes[1].position - nodes[0].getOther(nodes[1]).position); - } else { - path.B = 0.5f * (nodes[0].position + nodes[1].position); - } - if (nodes[1].roads.Count == 2) { - path.C = nodes[1].position + - 0.25f * (nodes[0].position - nodes[1].getOther(nodes[0]).position); - } else { - path.C = 0.5f * (nodes[0].position + nodes[1].position); - } - if (updateOthers) { - foreach (Node node in nodes) { - node.lateUpdate(this); - } - } - pathLine.update(); - roadBody.update(); - collider.sharedMesh = roadBody.mesh; - } - - override public bool Equals(object other) { - if (other == null || !(other is Road)) { - return false; - } - if (other == this) { - return true; - } - Road otherRoad = (Road) other; - foreach (Node node in nodes) { - if (! otherRoad.nodes.Contains(node)) { - return false; - } - } - return true; - } - - public override int GetHashCode() { - return nodes[0].GetHashCode() << 16 | nodes[1].GetHashCode(); - } -} diff --git a/Assets/Scripts/Road.cs.meta b/Assets/Scripts/Road.cs.meta deleted file mode 100644 index 24dc94b..0000000 --- a/Assets/Scripts/Road.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0ab512a631c0cc4468e77b6a2dfeb31f -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/RoadData.cs b/Assets/Scripts/RoadData.cs deleted file mode 100644 index eb620ba..0000000 --- a/Assets/Scripts/RoadData.cs +++ /dev/null @@ -1,7 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class RoadData : MonoBehaviour { - public Road road; -} diff --git a/Assets/Scripts/RoadData.cs.meta b/Assets/Scripts/RoadData.cs.meta deleted file mode 100644 index 2948b97..0000000 --- a/Assets/Scripts/RoadData.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 6bfac654eff68754c88b877a9b88ae42 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/RoadEdit.cs b/Assets/Scripts/RoadEdit.cs deleted file mode 100644 index a6cc73e..0000000 --- a/Assets/Scripts/RoadEdit.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class RoadEdit : MonoBehaviour { - public GameObject start; - public GameObject startDirection; - public GameObject end; - public GameObject endDirection; - public Camera mainCamera; - - private GameObject currentlyPulling = null; - public Bezier bezier; - private FlatBezierRenderer pathRenderer; - public Material highlight, noHighlight; - - void Start() { - } - - void updateRoad(Vector3 position) { - } - - void Update() { - Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); - if (Input.GetAxis("Fire1") != 0.0f) { - if (currentlyPulling == null && Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, 1 << 7)) { - currentlyPulling = hit.transform.gameObject; - currentlyPulling.GetComponent().material = highlight; - } - } else { - if (currentlyPulling != null) { - currentlyPulling.GetComponent().material = noHighlight; - } - currentlyPulling = null; - } - if (currentlyPulling != null) { - Physics.Raycast(ray, out RaycastHit hit_, Mathf.Infinity, 1 << 6); - Vector3 position = new Vector3(hit_.point.x, 0.0f, hit_.point.z); - currentlyPulling.transform.position = position; - updateRoad(position); - } - } -} diff --git a/Assets/Scripts/RoadEdit.cs.meta b/Assets/Scripts/RoadEdit.cs.meta deleted file mode 100644 index 4e09d01..0000000 --- a/Assets/Scripts/RoadEdit.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 68e9412fa432f2e468d5d7447e99fd89 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Roads.cs b/Assets/Scripts/Roads.cs deleted file mode 100644 index acd376a..0000000 --- a/Assets/Scripts/Roads.cs +++ /dev/null @@ -1,102 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class Roads : MonoBehaviour { - public List nodes = new List(); - public List roads = new List(); - public FlatBezierRenderer roadRenderer; - public Camera mainCamera; - private bool drawing = false; - public Material material; - public Config config; - - void Start() { - } - - private Node startNode; - - private void startRoad() { - Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out RaycastHit hit, Mathf.Infinity, 1 << 6); - foreach (Node node in nodes) { - if ((hit.point - node.position).magnitude < 1.0f) { - startNode = node; - drawing = true; - return; - } - } - Vector3 position = new Vector3(hit.point.x, 0.0f, hit.point.z); - startNode = new Node(position, transform, config); - nodes.Add(startNode); - drawing = true; - } - - private void endRoad() { - Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out RaycastHit hit, Mathf.Infinity, 1 << 6); - Node endNode = null; - foreach (Node node in nodes) { - if ((hit.point - node.position).magnitude < 1.0f) { - endNode = node; - break; - } - } - if (endNode == null) { - Vector3 position = new Vector3(hit.point.x, 0.0f, hit.point.z); - endNode = new Node(position, transform, config); - nodes.Add(endNode); - } - Road road = new Road(startNode, endNode, config); - if (!roads.Contains(road) && startNode != endNode) { - startNode.roads.Add(road); - endNode.roads.Add(road); - roads.Add(road); - road.initialize(transform); - road.update(true); - } - startNode = null; - drawing = false; - } - - Node pulling = null; - - void Update() { - Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); - RaycastHit hit; - if (config.mode == Mode.DrawRoad) { - if (Input.GetAxis("Fire1") != 0.0f) { - if (!drawing) { - startRoad(); - } - } else if (drawing) { - endRoad(); - } - } else if (config.mode == Mode.DeleteRoad) { - if (!drawing && Input.GetAxis("Fire1") != 0.0f) { - if (Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 8)) { - drawing = true; - Road road = hit.transform.gameObject.GetComponent().road; - roads.Remove(road); - foreach (Node node in road.nodes) { - node.roads.Remove(road); - node.update(); - } - Destroy(hit.transform.parent.gameObject); - } - } else if (drawing && Input.GetAxis("Fire1") == 0.0f) { - drawing = false; - } - } - if (Input.GetAxis("Fire2") != 0.0f) { - if (pulling == null && Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 7)) { - pulling = nodes.Find(node => node.gameObject == hit.transform.gameObject); - } - } else { - pulling = null; - } - if (pulling != null) { - Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 6); - Vector3 position = new Vector3(hit.point.x, 0.0f, hit.point.z); - pulling.pull(position); - } - } -} diff --git a/Assets/Scripts/Roads.cs.meta b/Assets/Scripts/Roads.cs.meta deleted file mode 100644 index f26a401..0000000 --- a/Assets/Scripts/Roads.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 7635c530aa8ef31498f100f4e7465bea -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Roads.meta b/Assets/Scripts/Roads.meta new file mode 100644 index 0000000..defbd46 --- /dev/null +++ b/Assets/Scripts/Roads.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: e6a3f10ad4786e44d9faf825f11a8e1d +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Roads/Node.cs b/Assets/Scripts/Roads/Node.cs new file mode 100644 index 0000000..3b2f31a --- /dev/null +++ b/Assets/Scripts/Roads/Node.cs @@ -0,0 +1,82 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Node { + private FlatCircleRenderer circle = new FlatCircleRenderer(0.2f, 0.05f, 32); + private FlatCircleRenderer fullCircle = new FlatCircleRenderer(0f, 1f, 32); + + public Vector3 position; + public List roads = new List(); + public GameObject gameObject; + + public Node(Vector3 position, Transform parent, Config config) { + gameObject = new GameObject(); + gameObject.transform.position = position; + gameObject.transform.parent = parent; + gameObject.AddComponent().radius = 2f; + gameObject.layer = 7; + gameObject.AddComponent().node = this; + + GameObject nodeCircle = new GameObject(); + nodeCircle.AddComponent().material = config.roadEditMaterial; + nodeCircle.AddComponent().mesh = circle.mesh; + nodeCircle.transform.parent = gameObject.transform; + nodeCircle.transform.localPosition = Vector3.zero; + + GameObject nodeRoad = new GameObject(); + nodeRoad.AddComponent().material = config.roadMaterial; + nodeRoad.AddComponent().mesh = fullCircle.mesh; + nodeRoad.transform.parent = gameObject.transform; + nodeRoad.transform.localPosition = Vector3.zero; + 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(); + } + + override public bool Equals(object other) { + if ((other == null) || ! this.GetType().Equals(other.GetType())) { + return false; + } + if (other == this) { + return true; + } + return ((Node)other).position.Equals(position); + } + + public void pull(Vector3 position) { + this.position = position; + gameObject.transform.position = position; + foreach (Road road in roads) { + 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/Roads/Node.cs.meta b/Assets/Scripts/Roads/Node.cs.meta new file mode 100644 index 0000000..8bbedf1 --- /dev/null +++ b/Assets/Scripts/Roads/Node.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 3cbed1dad7ec67d4dbe8602aea1e2979 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Roads/NodeData.cs b/Assets/Scripts/Roads/NodeData.cs new file mode 100644 index 0000000..9ee8ace --- /dev/null +++ b/Assets/Scripts/Roads/NodeData.cs @@ -0,0 +1,7 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class NodeData : MonoBehaviour { + public Node node = null; +} diff --git a/Assets/Scripts/Roads/NodeData.cs.meta b/Assets/Scripts/Roads/NodeData.cs.meta new file mode 100644 index 0000000..73dcf56 --- /dev/null +++ b/Assets/Scripts/Roads/NodeData.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4cf9bcd8c4889de4985e5d6a776acc5b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Roads/Road.cs b/Assets/Scripts/Roads/Road.cs new file mode 100644 index 0000000..1ad1a74 --- /dev/null +++ b/Assets/Scripts/Roads/Road.cs @@ -0,0 +1,88 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Road { + public List nodes = new List(); + Bezier path; + private FlatBezierRenderer pathLine, roadBody; + private Config config; + private MeshCollider collider; + public GameObject gameObject; + + public Road(Node start, Node end, Config config) { + nodes.Add(start); + nodes.Add(end); + path = new Bezier(); + pathLine = new FlatBezierRenderer(path, 50, 0.05f); + roadBody = new FlatBezierRenderer(path, 50, 2f); + this.config = config; + } + + + public void initialize(Transform parent) { + gameObject = new GameObject(); + gameObject.transform.parent = parent; + gameObject.transform.position = Vector3.zero; + + GameObject lineChild = new GameObject(); + lineChild.transform.position = Vector3.zero; + lineChild.AddComponent().material = config.roadEditMaterial; + lineChild.AddComponent().mesh = pathLine.mesh; + lineChild.transform.parent = gameObject.transform; + + GameObject bodyChild = new GameObject(); + bodyChild.AddComponent().material = config.roadMaterial; + bodyChild.AddComponent().mesh = roadBody.mesh; + bodyChild.AddComponent().road = this; + collider = bodyChild.AddComponent(); + bodyChild.transform.parent = gameObject.transform; + bodyChild.layer = 8; + update(true); + } + + public void update(bool updateOthers) { + path.A = nodes[0].position; + path.D = nodes[1].position; + if (nodes[0].roads.Count == 2) { + path.B = nodes[0].position + + 0.25f * (nodes[1].position - nodes[0].getOther(nodes[1]).position); + } else { + path.B = 0.5f * (nodes[0].position + nodes[1].position); + } + if (nodes[1].roads.Count == 2) { + path.C = nodes[1].position + + 0.25f * (nodes[0].position - nodes[1].getOther(nodes[0]).position); + } else { + path.C = 0.5f * (nodes[0].position + nodes[1].position); + } + if (updateOthers) { + foreach (Node node in nodes) { + node.lateUpdate(this); + } + } + pathLine.update(); + roadBody.update(); + collider.sharedMesh = roadBody.mesh; + } + + override public bool Equals(object other) { + if (other == null || !(other is Road)) { + return false; + } + if (other == this) { + return true; + } + Road otherRoad = (Road) other; + foreach (Node node in nodes) { + if (! otherRoad.nodes.Contains(node)) { + return false; + } + } + return true; + } + + public override int GetHashCode() { + return nodes[0].GetHashCode() << 16 | nodes[1].GetHashCode(); + } +} diff --git a/Assets/Scripts/Roads/Road.cs.meta b/Assets/Scripts/Roads/Road.cs.meta new file mode 100644 index 0000000..24dc94b --- /dev/null +++ b/Assets/Scripts/Roads/Road.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0ab512a631c0cc4468e77b6a2dfeb31f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Roads/RoadData.cs b/Assets/Scripts/Roads/RoadData.cs new file mode 100644 index 0000000..eb620ba --- /dev/null +++ b/Assets/Scripts/Roads/RoadData.cs @@ -0,0 +1,7 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class RoadData : MonoBehaviour { + public Road road; +} diff --git a/Assets/Scripts/Roads/RoadData.cs.meta b/Assets/Scripts/Roads/RoadData.cs.meta new file mode 100644 index 0000000..2948b97 --- /dev/null +++ b/Assets/Scripts/Roads/RoadData.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 6bfac654eff68754c88b877a9b88ae42 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Roads/RoadEdit.cs b/Assets/Scripts/Roads/RoadEdit.cs new file mode 100644 index 0000000..a6cc73e --- /dev/null +++ b/Assets/Scripts/Roads/RoadEdit.cs @@ -0,0 +1,43 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class RoadEdit : MonoBehaviour { + public GameObject start; + public GameObject startDirection; + public GameObject end; + public GameObject endDirection; + public Camera mainCamera; + + private GameObject currentlyPulling = null; + public Bezier bezier; + private FlatBezierRenderer pathRenderer; + public Material highlight, noHighlight; + + void Start() { + } + + void updateRoad(Vector3 position) { + } + + void Update() { + Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); + if (Input.GetAxis("Fire1") != 0.0f) { + if (currentlyPulling == null && Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, 1 << 7)) { + currentlyPulling = hit.transform.gameObject; + currentlyPulling.GetComponent().material = highlight; + } + } else { + if (currentlyPulling != null) { + currentlyPulling.GetComponent().material = noHighlight; + } + currentlyPulling = null; + } + if (currentlyPulling != null) { + Physics.Raycast(ray, out RaycastHit hit_, Mathf.Infinity, 1 << 6); + Vector3 position = new Vector3(hit_.point.x, 0.0f, hit_.point.z); + currentlyPulling.transform.position = position; + updateRoad(position); + } + } +} diff --git a/Assets/Scripts/Roads/RoadEdit.cs.meta b/Assets/Scripts/Roads/RoadEdit.cs.meta new file mode 100644 index 0000000..4e09d01 --- /dev/null +++ b/Assets/Scripts/Roads/RoadEdit.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 68e9412fa432f2e468d5d7447e99fd89 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Roads/Roads.cs b/Assets/Scripts/Roads/Roads.cs new file mode 100644 index 0000000..02923e2 --- /dev/null +++ b/Assets/Scripts/Roads/Roads.cs @@ -0,0 +1,120 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public enum DrawMode { + None, + DrawRoad, + DragRoad, + DeleteRoad +} + +public class Roads : MonoBehaviour { + public Camera mainCamera; + public Material material; + public Config config; + public DrawMode drawMode = DrawMode.None; + + void Start() { + } + + private Node pullingNode = null; + + private void startRoad(Ray ray, Vector3 groundPosition) { + drawMode = DrawMode.DrawRoad; + if (Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, 1 << 7)) { + pullingNode = hit.transform.gameObject.GetComponent().node; + return; + } + pullingNode = new Node(groundPosition, transform, config); + } + + private void endRoad(Ray ray, Vector3 groundPosition) { + drawMode = DrawMode.None; + Node endNode = null; + if (Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, 1 << 7)) { + endNode = hit.transform.gameObject.GetComponent().node; + } else { + endNode = new Node(groundPosition, transform, config); + } + Road road = new Road(pullingNode, endNode, config); + if (! pullingNode.roads.Contains(road) && pullingNode != endNode) { + pullingNode.roads.Add(road); + endNode.roads.Add(road); + road.initialize(transform); + } + pullingNode = null; + } + + private void handleRoadDrawing(Ray ray, Vector3 groundPosition) { + if (Input.GetAxis("Fire1") == 1.0f) { + if (drawMode == DrawMode.None) { + startRoad(ray, groundPosition); + } + } else if (drawMode == DrawMode.DrawRoad) { + endRoad(ray, groundPosition); + } + } + + private void removeRoad(Road road) { + foreach (Node node in road.nodes) { + node.roads.Remove(road); + if (node.roads.Count == 0) { + removeNode(node); + } else { + node.update(); + } + } + Destroy(road.gameObject); + } + + private void removeNode(Node node) { + foreach (Road road in node.roads) { + road.nodes.Remove(node); + removeRoad(road); + } + Destroy(node.gameObject); + } + + private void handleRoadRemoving(Ray ray) { + if (drawMode == DrawMode.None && Input.GetAxis("Fire1") != 0.0f) { + RaycastHit hit; + if (Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 7)) { + Node node = hit.transform.gameObject.GetComponent().node; + removeNode(node); + drawMode = DrawMode.DeleteRoad; + } else if (Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 8)) { + Road road = hit.transform.gameObject.GetComponent().road; + print("road"); + removeRoad(road); + drawMode = DrawMode.DeleteRoad; + } + } else if (drawMode == DrawMode.DeleteRoad && Input.GetAxis("Fire1") == 0.0f) { + drawMode = DrawMode.None; + } + } + + void Update() { + Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); + RaycastHit hit; + Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 6); + Vector3 groundPosition = new Vector3(hit.point.x, 0.0f, hit.point.z); + + if (config.mode == Mode.DrawRoad) { + handleRoadDrawing(ray, groundPosition); + } else if (config.mode == Mode.DeleteRoad) { + handleRoadRemoving(ray); + } + if (Input.GetAxis("Fire2") != 0.0f) { + if (drawMode == DrawMode.None && Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 7)) { + drawMode = DrawMode.DragRoad; + pullingNode = hit.transform.gameObject.GetComponent().node; + } + } else if (drawMode == DrawMode.DragRoad) { + drawMode = DrawMode.None; + } + if (drawMode == DrawMode.DragRoad) { + pullingNode.pull(groundPosition); + } + } +} diff --git a/Assets/Scripts/Roads/Roads.cs.meta b/Assets/Scripts/Roads/Roads.cs.meta new file mode 100644 index 0000000..f26a401 --- /dev/null +++ b/Assets/Scripts/Roads/Roads.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 7635c530aa8ef31498f100f4e7465bea +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: