Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Runtime.InteropServices;
- using UnityEngine.Rendering;
- public class SplineMaker : MonoBehaviour
- {
- private Vector3 _position = new Vector3(0,0,0);
- private int _angle = 0;
- private int _length = 0;
- public bool ShowSpheres = false;
- public bool ShowLine = true;
- public bool ShowEditorLine = true;
- public bool CustomLight = true;
- private int _steps = 0;
- public GameObject Slice;
- public float UvMultiplier = 1.0f;
- public List<Vector3> TransitionPoints = new List<Vector3>();
- public List<Vector3> TransitionVectors = new List<Vector3>();
- // Use this for initialization
- void Start()
- {
- }
- // Update is called once per frame
- void Update()
- {
- }
- public void BuildSpline(int length, int angle)
- {
- float angleRad = _angle / 180.0f * Mathf.PI;
- GameObject spline = new GameObject();
- spline.name = "Spline";
- spline.transform.position = _position;
- SimpleSpline splineLine = spline.AddComponent<SimpleSpline>();
- splineLine.DrawSpheres = ShowSpheres;
- splineLine.Parameters.StartPoint = _position;
- splineLine.DrawLines = ShowEditorLine;
- if (TransitionPoints.Count == 0)
- {
- TransitionPoints.Add(_position);
- TransitionVectors.Add(new Vector3(Mathf.Cos((float)Mathf.PI / 2), 0, Mathf.Sin((float)Mathf.PI / 2)));
- }
- if (angle == 0)
- {
- spline.GetComponent<SimpleSpline>().Parameters.ControlPoint = _position + new Vector3(length / 2.0f * Mathf.Cos(angleRad + (float)Mathf.PI / 2), 0, length / 2.0f * Mathf.Sin(angleRad + (float)Mathf.PI / 2));
- spline.GetComponent<SimpleSpline>().Parameters.EndPoint = _position + new Vector3(length * Mathf.Cos(angleRad + (float)Mathf.PI / 2), 0, length * Mathf.Sin(angleRad + (float)Mathf.PI / 2));
- TransitionPoints.Add(spline.GetComponent<SimpleSpline>().Parameters.EndPoint);
- TransitionVectors.Add(new Vector3(Mathf.Cos(angleRad + (float)Mathf.PI / 2), 0,
- Mathf.Sin(angleRad + (float)Mathf.PI / 2)));
- }
- else
- {
- float radius = Mathf.Abs((float)length / (angle / 180.0f * Mathf.PI));
- if (angle > 0)
- {
- spline.GetComponent<SimpleSpline>().Parameters.EndPoint =
- _position
- - new Vector3(radius * Mathf.Cos(angleRad), 0, radius * Mathf.Sin(angleRad))
- + new Vector3(radius * Mathf.Cos(angleRad + angle / 180.0f * Mathf.PI), 0, radius * Mathf.Sin(angleRad + angle / 180.0f * Mathf.PI));
- TransitionPoints.Add(spline.GetComponent<SimpleSpline>().Parameters.EndPoint);
- TransitionVectors.Add(new Vector3(Mathf.Cos(angleRad + (float)Mathf.PI / 2), 0,
- Mathf.Sin(angleRad + (float)Mathf.PI / 2)));
- spline.GetComponent<SimpleSpline>().Parameters.ControlPoint =
- ClosestPointOnTwoLines(spline.GetComponent<SimpleSpline>().Parameters.StartPoint,
- new Vector3(Mathf.Cos(angleRad + (float)Mathf.PI / 2), 0, Mathf.Sin(angleRad + (float)Mathf.PI / 2)),
- spline.GetComponent<SimpleSpline>().Parameters.EndPoint,
- new Vector3(Mathf.Cos((_angle + angle + 90) / 180.0f * Mathf.PI), 0, Mathf.Sin((_angle + angle + 90) / 180.0f * Mathf.PI)));
- }
- else
- {
- spline.GetComponent<SimpleSpline>().Parameters.EndPoint =
- _position
- + new Vector3(radius * Mathf.Cos(angleRad), 0, radius * Mathf.Sin(angleRad))
- - new Vector3(radius * Mathf.Cos(angleRad + angle / 180.0f * Mathf.PI), 0, radius * Mathf.Sin(angleRad + angle / 180.0f * Mathf.PI));
- TransitionPoints.Add(spline.GetComponent<SimpleSpline>().Parameters.EndPoint);
- TransitionVectors.Add(new Vector3(Mathf.Cos(angleRad + (float)Mathf.PI / 2), 0,
- Mathf.Sin(angleRad + (float)Mathf.PI / 2)));
- spline.GetComponent<SimpleSpline>().Parameters.ControlPoint =
- ClosestPointOnTwoLines(spline.GetComponent<SimpleSpline>().Parameters.StartPoint,
- new Vector3(Mathf.Cos(angleRad + (float)Mathf.PI / 2), 0, Mathf.Sin(angleRad + (float)Mathf.PI / 2)),
- spline.GetComponent<SimpleSpline>().Parameters.EndPoint,
- new Vector3(Mathf.Cos((_angle + angle + 90) / 180.0f * Mathf.PI), 0, Mathf.Sin((_angle + angle + 90) / 180.0f * Mathf.PI)));
- }
- }
- _angle += angle;
- _length += length;
- _steps = angle == 0 ? 2 : Mathf.Abs(angle) / 2;
- spline.GetComponent<SimpleSpline>().InterpolationSteps = _steps;
- List<Vector3> pointslist = spline.GetComponent<SimpleSpline>().CalculateSplinePoints();
- _position = spline.GetComponent<SimpleSpline>().Parameters.EndPoint;
- if (ShowLine)
- {
- LineRenderer lineRenderer = spline.AddComponent<LineRenderer>();
- lineRenderer.SetVertexCount(_steps);
- for (int i = 0; i < _steps; i++)
- {
- lineRenderer.SetPosition(i, spline.GetComponent<SimpleSpline>().SplinePoints[i]);
- }
- lineRenderer.material = new Material(Shader.Find("Particles/Alpha Blended"));
- lineRenderer.useLightProbes = false;
- lineRenderer.receiveShadows = false;
- }
- else
- {
- spline.AddComponent<MeshRenderer>();
- spline.GetComponent<MeshRenderer>().material = Slice.GetComponent<MeshRenderer>().sharedMaterial; //Visualisation
- //spline.GetComponent<MeshRenderer>().receiveShadows = false;
- //spline.GetComponent<MeshRenderer>().shadowCastingMode = ShadowCastingMode.Off;
- spline.AddComponent<MeshFilter>(); //Mesh itself
- MeshBuilder build = spline.AddComponent<MeshBuilder>(); //Custom script which makes the Mesh
- build.Slice = Slice;
- build.UvMultiplier = UvMultiplier;
- build.Points = pointslist;
- build.Angle = angle;
- build.TotalAngle = _angle;
- build.Length = length;
- build.TotalLength = _length;
- spline.AddComponent<MeshCollider>(); //Collision so you can drive on it
- if (CustomLight)
- {
- Vector3[] vertices = Slice.GetComponent<MeshFilter>().sharedMesh.vertices;
- float highestY = 0;
- float lowestY = 0;
- float highestX = 0;
- float lowestX = 0;
- for (int i = 0; i < vertices.Length; i++)
- {
- if (lowestY > vertices[i].y)
- lowestY = vertices[i].y;
- if (highestY < vertices[i].y)
- highestY = vertices[i].y;
- if (highestX < vertices[i].x)
- highestX = vertices[i].x;
- if (lowestX > vertices[i].x)
- lowestX = vertices[i].x;
- }
- float width = highestX - lowestX;
- float height = highestY - lowestY;
- var lightComp = spline.AddComponent<MakeLight>();
- lightComp.Length = length;
- lightComp.Height = height;
- }
- }
- }
- private Vector3 ClosestPointOnTwoLines(Vector3 linePoint1, Vector3 lineVec1, Vector3 linePoint2, Vector3 lineVec2)
- {
- float a = Vector3.Dot(lineVec1, lineVec1);
- float b = Vector3.Dot(lineVec1, lineVec2);
- float e = Vector3.Dot(lineVec2, lineVec2);
- float d = a * e - b * b;
- //lines are not parallel
- if (d != 0.0f)
- {
- Vector3 r = linePoint1 - linePoint2;
- float c = Vector3.Dot(lineVec1, r);
- float f = Vector3.Dot(lineVec2, r);
- float s = (b * f - c * e) / d;
- return linePoint1 + lineVec1 * s;
- }
- else
- {
- Debug.Log("Failed to find a point");
- return new Vector3(0, 0, 0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement