Skip to content

Commit

Permalink
Some optimizations, added obj placement example scene, moved everythi…
Browse files Browse the repository at this point in the history
…ng into assets folder.
  • Loading branch information
SebLague committed May 25, 2019
1 parent fe26670 commit 87dd56e
Show file tree
Hide file tree
Showing 93 changed files with 3,015 additions and 421 deletions.
56 changes: 56 additions & 0 deletions PathCreator/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"files.exclude":
{
"**/.DS_Store":true,
"**/.git":true,
"**/.gitignore":true,
"**/.gitmodules":true,
"**/*.booproj":true,
"**/*.pidb":true,
"**/*.suo":true,
"**/*.user":true,
"**/*.userprefs":true,
"**/*.unityproj":true,
"**/*.dll":true,
"**/*.exe":true,
"**/*.pdf":true,
"**/*.mid":true,
"**/*.midi":true,
"**/*.wav":true,
"**/*.gif":true,
"**/*.ico":true,
"**/*.jpg":true,
"**/*.jpeg":true,
"**/*.png":true,
"**/*.psd":true,
"**/*.tga":true,
"**/*.tif":true,
"**/*.tiff":true,
"**/*.3ds":true,
"**/*.3DS":true,
"**/*.fbx":true,
"**/*.FBX":true,
"**/*.lxo":true,
"**/*.LXO":true,
"**/*.ma":true,
"**/*.MA":true,
"**/*.obj":true,
"**/*.OBJ":true,
"**/*.asset":true,
"**/*.cubemap":true,
"**/*.flare":true,
"**/*.mat":true,
"**/*.meta":true,
"**/*.prefab":true,
"**/*.unity":true,
"build/":true,
"Build/":true,
"Library/":true,
"library/":true,
"obj/":true,
"Obj/":true,
"ProjectSettings/":true,
"temp/":true,
"Temp/":true
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

Large diffs are not rendered by default.

File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ public class GlobalDisplaySettings : ScriptableObject
public enum HandleType { Sphere, Circle, Square };

[Header("Appearance")]

public float anchorSize = 10;
public float controlSize = 7f;

[Tooltip("Should the path still be drawn when behind objects in the scene?")]
public bool visibleBehindObjects = true;
[Tooltip("Should the path be drawn even when the path object is not selected?")]
public bool alwaysDrawPath = true;
public bool visibleWhenNotSelected = true;
[Tooltip("If true, control points will be hidden when the control point mode is set to automatic. Otherwise they will inactive, but still visible.")]
public bool hideAutoControls = true;
public HandleType anchorShape;
Expand All @@ -43,14 +44,11 @@ public enum HandleType { Sphere, Circle, Square };

[Header("Vertex Path Colours")]
public Color vertexPath = Color.white;
public Color vertex = Color.black;

[Header("Normals")]
public Color normals = Color.yellow;
[Range(0,1)]
public float normalsLength = .1f;
[Range(0.005f, 0.2f)]
public float normalsWidth = 0.01f;

#if UNITY_EDITOR
public static GlobalDisplaySettings Load() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,19 @@ void OnPathUpdated () {
// Draw the path when path objected is not selected (if enabled in settings)
void OnDrawGizmos () {

if (path != null) {
// Only draw path gizmo if the path object is not selected
// (editor script is resposible for drawing when selected)
GameObject selectedObj = UnityEditor.Selection.activeGameObject;
if (selectedObj != gameObject) {

if (globalEditorDisplaySettings == null) {
globalEditorDisplaySettings = GlobalDisplaySettings.Load ();
}
if (path != null) {

if (globalEditorDisplaySettings == null) {
globalEditorDisplaySettings = GlobalDisplaySettings.Load ();
}

if (globalEditorDisplaySettings.alwaysDrawPath) {
if (globalEditorDisplaySettings.visibleWhenNotSelected) {

// Only draw path gizmo if the path object is not selected
// (editor script is resposible for drawing when selected)
GameObject selectedObj = UnityEditor.Selection.activeGameObject;
if (selectedObj != gameObject) {
Gizmos.color = globalEditorDisplaySettings.bezierPath;

for (int i = 0; i < path.NumVertices; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class PathCreatorData
public float vertexPathMinVertexSpacing = 0.01f;

// bezier display settings
public bool pathTransformationEnabled;
public bool pathTransformationEnabled = true;
public bool showPathBounds;
public bool showPerSegmentBounds;
public bool displayAnchorPoints = true;
Expand All @@ -33,8 +33,8 @@ public class PathCreatorData
public bool keepConstantHandleSize;

// vertex display settings
public float vertexHandleSize = .2f;
public bool showNormalsInVertexMode;
public bool showBezierPathInVertexMode;

// Editor display states
public bool showDisplayOptions;
Expand Down
File renamed without changes.
File renamed without changes.
136 changes: 136 additions & 0 deletions PathCreator/Assets/Core/Runtime/Utility/CubicBezierUtility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace PathCreation.Utility {

/// Collection of functions related to cubic bezier curves
/// (a curve with a start and end 'anchor' point, and two 'control' points to define the shape of the curve between the anchors)
public static class CubicBezierUtility {

/// Returns point at time 't' (between 0 and 1) along bezier curve defined by 4 points (anchor_1, control_1, control_2, anchor_2)
public static Vector3 EvaluateCurve (Vector3[] points, float t) {
return EvaluateCurve (points[0], points[1], points[2], points[3], t);
}

/// Returns point at time 't' (between 0 and 1) along bezier curve defined by 4 points (anchor_1, control_1, control_2, anchor_2)
public static Vector3 EvaluateCurve (Vector3 a1, Vector3 c1, Vector3 c2, Vector3 a2, float t) {
t = Mathf.Clamp01 (t);
return (1 - t) * (1 - t) * (1 - t) * a1 + 3 * (1 - t) * (1 - t) * t * c1 + 3 * (1 - t) * t * t * c2 + t * t * t * a2;
}

/// Returns a vector tangent to the point at time 't'
/// This is the vector tangent to the curve at that point
public static Vector3 EvaluateCurveDerivative (Vector3[] points, float t) {
return EvaluateCurveDerivative (points[0], points[1], points[2], points[3], t);
}

/// Calculates the derivative of the curve at time 't'
/// This is the vector tangent to the curve at that point
public static Vector3 EvaluateCurveDerivative (Vector3 a1, Vector3 c1, Vector3 c2, Vector3 a2, float t) {
t = Mathf.Clamp01 (t);
return 3 * (1 - t) * (1 - t) * (c1 - a1) + 6 * (1 - t) * t * (c2 - c1) + 3 * t * t * (a2 - c2);
}

/// Returns the second derivative of the curve at time 't'
public static Vector3 EvaluateCurveSecondDerivative (Vector3[] points, float t) {
return EvaluateCurveSecondDerivative (points[0], points[1], points[2], points[3], t);
}

///Returns the second derivative of the curve at time 't'
public static Vector3 EvaluateCurveSecondDerivative (Vector3 a1, Vector3 c1, Vector3 c2, Vector3 a2, float t) {
t = Mathf.Clamp01 (t);
return 6 * (1 - t) * (c2 - 2 * c1 + a1) + 6 * t * (a2 - 2 * c2 + c1);
}

/// Calculates the normal vector (vector perpendicular to the curve) at specified time
public static Vector3 Normal (Vector3[] points, float t) {
return Normal (points[0], points[1], points[2], points[3], t);
}

/// Calculates the normal vector (vector perpendicular to the curve) at specified time
public static Vector3 Normal (Vector3 a1, Vector3 c1, Vector3 c2, Vector3 a2, float t) {
Vector3 tangent = EvaluateCurveDerivative (a1, c1, c2, a2, t);
Vector3 nextTangent = EvaluateCurveSecondDerivative (a1, c1, c2, a2, t);
Vector3 c = Vector3.Cross (nextTangent, tangent);
return Vector3.Cross (c, tangent).normalized;
}

public static Bounds CalculateBounds (Vector3[] points) {
MinMax3D minMax = new MinMax3D ();
minMax.AddValue (points[0]);
minMax.AddValue (points[3]);

List<float> extremePointTimes = ExtremePointTimes (points[0], points[1], points[2], points[3]);
foreach (float t in extremePointTimes) {
minMax.AddValue (CubicBezierUtility.EvaluateCurve (points, t));
}

return new Bounds ((minMax.Min + minMax.Max) / 2, minMax.Max - minMax.Min);
}

/// Splits curve into two curves at time t. Returns 2 arrays of 4 points.
public static Vector3[][] SplitCurve (Vector3[] points, float t) {
Vector3 a1 = Vector3.Lerp (points[0], points[1], t);
Vector3 a2 = Vector3.Lerp (points[1], points[2], t);
Vector3 a3 = Vector3.Lerp (points[2], points[3], t);
Vector3 b1 = Vector3.Lerp (a1, a2, t);
Vector3 b2 = Vector3.Lerp (a2, a3, t);
Vector3 pointOnCurve = Vector3.Lerp (b1, b2, t);

return new Vector3[][] {
new Vector3[] { points[0], a1, b1, pointOnCurve },
new Vector3[] { pointOnCurve, b2, a3, points[3] }
};
}

// Crude, but fast estimation of curve length.
public static float EstimateCurveLength (Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3) {
float controlNetLength = (p0 - p1).magnitude + (p1 - p2).magnitude + (p2 - p3).magnitude;
float estimatedCurveLength = (p0 - p3).magnitude + controlNetLength / 2f;
return estimatedCurveLength;
}

/// Times of stationary points on curve (points where derivative is zero on any axis)
public static List<float> ExtremePointTimes (Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3) {
// coefficients of derivative function
Vector3 a = 3 * (-p0 + 3 * p1 - 3 * p2 + p3);
Vector3 b = 6 * (p0 - 2 * p1 + p2);
Vector3 c = 3 * (p1 - p0);

List<float> times = new List<float> ();
times.AddRange (StationaryPointTimes (a.x, b.x, c.x));
times.AddRange (StationaryPointTimes (a.y, b.y, c.y));
times.AddRange (StationaryPointTimes (a.z, b.z, c.z));
return times;
}

// Finds times of stationary points on curve defined by ax^2 + bx + c.
// Only times between 0 and 1 are considered as Bezier only uses values in that range
static IEnumerable<float> StationaryPointTimes (float a, float b, float c) {
List<float> times = new List<float> ();

// from quadratic equation: y = [-b +- sqrt(b^2 - 4ac)]/2a
if (a != 0) {
float discriminant = b * b - 4 * a * c;
if (discriminant >= 0) {
float s = Mathf.Sqrt (discriminant);
float t1 = (-b + s) / (2 * a);
if (t1 >= 0 && t1 <= 1) {
times.Add (t1);
}

if (discriminant != 0) {
float t2 = (-b - s) / (2 * a);

if (t2 >= 0 && t2 <= 1) {
times.Add (t2);
}
}
}
}
return times;
}

}
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@ MonoBehaviour:
m_EditorClassIdentifier:
anchorSize: 10
controlSize: 7
alwaysDrawPath: 1
visibleBehindObjects: 1
visibleWhenNotSelected: 1
hideAutoControls: 1
anchorShape: 0
controlShape: 0
anchor: {r: 0.95, g: 0.25, b: 0.25, a: 0.85}
anchorHighlighted: {r: 1, g: 0.4, b: 0.4, a: 1}
anchor: {r: 1, g: 0.3726415, b: 0.3726415, a: 1}
anchorHighlighted: {r: 0.735849, g: 0.03818085, b: 0.03818085, a: 1}
anchorSelected: {r: 1, g: 1, b: 1, a: 1}
control: {r: 0.35, g: 0.6, b: 1, a: 0.85}
controlHighlighted: {r: 0.6, g: 0.6, b: 1, a: 1}
control: {r: 0.35, g: 0.6, b: 1, a: 1}
controlHighlighted: {r: 0.19579923, g: 0.19579923, b: 0.754717, a: 1}
controlSelected: {r: 1, g: 1, b: 1, a: 1}
handleDisabled: {r: 1, g: 1, b: 1, a: 0.2}
controlLine: {r: 0, g: 0, b: 0, a: 0.7254902}
Expand All @@ -31,6 +32,5 @@ MonoBehaviour:
bounds: {r: 1, g: 1, b: 1, a: 0.4}
segmentBounds: {r: 1, g: 1, b: 1, a: 0.4}
vertexPath: {r: 1, g: 1, b: 1, a: 1}
vertex: {r: 0, g: 0, b: 0, a: 1}
normals: {r: 1, g: 0.92156863, b: 0.015686275, a: 1}
normalsLength: 0.1
normalsLength: 0.198
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
77 changes: 77 additions & 0 deletions PathCreator/Assets/Examples/Materials/Brown.mat
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!21 &2100000
Material:
serializedVersion: 6
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Brown
m_Shader: {fileID: 46, guid: 0000000000000000f000000000000000, type: 0}
m_ShaderKeywords:
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: -1
stringTagMap: {}
disabledShaderPasses: []
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _BumpMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailAlbedoMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailMask:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailNormalMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _EmissionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 10.66}
m_Offset: {x: 0, y: 0}
- _MainTex:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 10.66}
m_Offset: {x: 0, y: 0}
- _MetallicGlossMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _OcclusionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _ParallaxMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Floats:
- _BumpScale: 1
- _Cutoff: 0.5
- _DetailNormalMapScale: 1
- _DstBlend: 0
- _GlossMapScale: 1
- _Glossiness: 0
- _GlossyReflections: 1
- _Metallic: 0
- _Mode: 0
- _OcclusionStrength: 1
- _Parallax: 0.02
- _SmoothnessTextureChannel: 0
- _SpecularHighlights: 1
- _SrcBlend: 1
- _UVSec: 0
- _ZWrite: 1
m_Colors:
- _Color: {r: 0.4528302, g: 0.19663213, b: 0.10466359, a: 1}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
10 changes: 10 additions & 0 deletions PathCreator/Assets/Examples/Materials/Brown.mat.meta

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

Loading

0 comments on commit 87dd56e

Please sign in to comment.