Permalink
Browse files

initial commit

  • Loading branch information...
GandhiGames committed Nov 16, 2016
0 parents commit 6aba432cbc2bf97dac80a707a01b0e251dc5988a
Showing with 84,003 additions and 0 deletions.
  1. +9 −0 Assets/UG.meta
  2. +9 −0 Assets/UG/Prefabs.meta
  3. BIN Assets/UG/Prefabs/Line.prefab
  4. +8 −0 Assets/UG/Prefabs/Line.prefab.meta
  5. +9 −0 Assets/UG/Scenes.meta
  6. BIN Assets/UG/Scenes/Demo.unity
  7. +8 −0 Assets/UG/Scenes/Demo.unity.meta
  8. +9 −0 Assets/UG/Scripts.meta
  9. +21 −0 Assets/UG/Scripts/Demo_Grid.cs
  10. +12 −0 Assets/UG/Scripts/Demo_Grid.cs.meta
  11. +298 −0 Assets/UG/Scripts/Grid.cs
  12. +12 −0 Assets/UG/Scripts/Grid.cs.meta
  13. +683 −0 Assets/UG/Scripts/Interpolate.cs
  14. +12 −0 Assets/UG/Scripts/Interpolate.cs.meta
  15. +40 −0 Assets/UG/Scripts/PointMass.cs
  16. +12 −0 Assets/UG/Scripts/PointMass.cs.meta
  17. +40 −0 Assets/UG/Scripts/Spring.cs
  18. +12 −0 Assets/UG/Scripts/Spring.cs.meta
  19. +9 −0 Assets/UG/Sprites.meta
  20. BIN Assets/UG/Sprites/pixel.PNG
  21. +59 −0 Assets/UG/Sprites/pixel.PNG.meta
  22. BIN Library/AnnotationManager
  23. +1 −0 Library/AssetImportState
  24. BIN Library/AssetServerCacheV3
  25. BIN Library/AssetVersioning.db
  26. 0 Library/BuildPlayer.prefs
  27. BIN Library/BuildSettings.asset
  28. BIN Library/CurrentLayout.dwlt
  29. BIN Library/CurrentMaximizeLayout.dwlt
  30. BIN Library/EditorUserBuildSettings.asset
  31. BIN Library/EditorUserSettings.asset
  32. BIN Library/InspectorExpandedItems.asset
  33. +4 −0 Library/LastSceneManagerSetup.txt
  34. +2 −0 Library/LibraryFormatVersion.txt
  35. BIN Library/MonoManager.asset
  36. BIN Library/ProjectSettings.asset
  37. BIN Library/ScriptAssemblies/Assembly-CSharp.dll.mdb
  38. +2 −0 Library/ScriptAssemblies/BuiltinAssemblies.stamp
  39. BIN Library/ScriptMapper
  40. BIN Library/ShaderCache.db
  41. BIN Library/ShaderCache/3/3fe78fd97c40c4790689e08a39b231f3.bin
  42. BIN Library/ShaderCache/5/58756e6566804f935178a53a3cda76b2.bin
  43. BIN Library/ShaderCache/6/69fb7fb993c6553c588236487eac23dc.bin
  44. BIN Library/ShaderCache/9/90c4640ec622119b071939e8227285f8.bin
  45. BIN Library/ShaderCache/9/956cc8410b10c26b27aae967bab45ebb.bin
  46. BIN Library/ShaderCache/c/cf55de8b55d6066eeb9acf04283cb607.bin
  47. BIN Library/ShaderCache/d/d65793d2124bcb0186ea87e51c41a93f.bin
  48. BIN Library/ShaderCache/e/e34947141480be6a70ac0eeab68cff62.bin
  49. BIN Library/ShaderCache/e/e6042cf20e2d8b7e7ff73cadc8523b7a.bin
  50. BIN Library/ShaderCache/e/e9eb8980d44e6a8352b2272376158b61.bin
  51. BIN Library/ShaderCache/e/ec469ca656d785e256753ea8e3abea51.bin
  52. BIN Library/ShaderCache/f/f4f0b408a648323cfc1276f3cc6100b2.bin
  53. +22,949 −0 Library/UnityAssemblies/UnityEditor.xml
  54. +5,087 −0 Library/UnityAssemblies/UnityEngine.Networking.xml
  55. +5,951 −0 Library/UnityAssemblies/UnityEngine.UI.xml
  56. +48,644 −0 Library/UnityAssemblies/UnityEngine.xml
  57. +6 −0 Library/UnityAssemblies/version.txt
  58. BIN Library/assetDatabase3
  59. BIN Library/expandedItems
  60. BIN Library/metadata/00/00000000000000001000000000000000
  61. BIN Library/metadata/00/00000000000000001000000000000000.info
  62. BIN Library/metadata/00/00000000000000002000000000000000
  63. BIN Library/metadata/00/00000000000000002000000000000000.info
  64. BIN Library/metadata/00/00000000000000003000000000000000
  65. BIN Library/metadata/00/00000000000000003000000000000000.info
  66. BIN Library/metadata/00/00000000000000004000000000000000
  67. BIN Library/metadata/00/00000000000000004000000000000000.info
  68. BIN Library/metadata/00/00000000000000004100000000000000
  69. BIN Library/metadata/00/00000000000000004100000000000000.info
  70. BIN Library/metadata/00/00000000000000005000000000000000
  71. BIN Library/metadata/00/00000000000000005000000000000000.info
  72. BIN Library/metadata/00/00000000000000005100000000000000
  73. BIN Library/metadata/00/00000000000000005100000000000000.info
  74. BIN Library/metadata/00/00000000000000006000000000000000
  75. BIN Library/metadata/00/00000000000000006000000000000000.info
  76. BIN Library/metadata/00/00000000000000006100000000000000
  77. BIN Library/metadata/00/00000000000000006100000000000000.info
  78. BIN Library/metadata/00/00000000000000007000000000000000
  79. BIN Library/metadata/00/00000000000000007000000000000000.info
  80. BIN Library/metadata/00/00000000000000007100000000000000
  81. BIN Library/metadata/00/00000000000000007100000000000000.info
  82. BIN Library/metadata/00/00000000000000008000000000000000
  83. BIN Library/metadata/00/00000000000000008000000000000000.info
  84. BIN Library/metadata/00/00000000000000008100000000000000
  85. BIN Library/metadata/00/00000000000000008100000000000000.info
  86. BIN Library/metadata/00/00000000000000009000000000000000
  87. BIN Library/metadata/00/00000000000000009000000000000000.info
  88. BIN Library/metadata/00/0000000000000000a000000000000000
  89. BIN Library/metadata/00/0000000000000000a000000000000000.info
  90. BIN Library/metadata/00/0000000000000000a100000000000000
  91. BIN Library/metadata/00/0000000000000000a100000000000000.info
  92. BIN Library/metadata/00/0000000000000000b000000000000000
  93. BIN Library/metadata/00/0000000000000000b000000000000000.info
  94. BIN Library/metadata/00/0000000000000000c000000000000000
  95. BIN Library/metadata/00/0000000000000000c000000000000000.info
  96. BIN Library/metadata/01/01a57a3eef5852640a8f94ff99f7e1bf
  97. BIN Library/metadata/01/01a57a3eef5852640a8f94ff99f7e1bf.info
  98. BIN Library/metadata/26/2604a3ee64496c4408dd52f98f548cad
  99. BIN Library/metadata/26/2604a3ee64496c4408dd52f98f548cad.info
  100. BIN Library/metadata/27/27c4f7c497b956d4598f31006fe12df8
  101. BIN Library/metadata/27/27c4f7c497b956d4598f31006fe12df8.info
  102. BIN Library/metadata/3c/3c0ad459c1534645b5d603b7cc258f97
  103. BIN Library/metadata/3c/3c0ad459c1534645b5d603b7cc258f97.info
  104. BIN Library/metadata/3d/3d086da5a2439a9469193cf13bbc5fcb
  105. BIN Library/metadata/3d/3d086da5a2439a9469193cf13bbc5fcb.info
  106. BIN Library/metadata/48/482e82dd57d649d439ffb2eb234a4ae5
  107. BIN Library/metadata/48/482e82dd57d649d439ffb2eb234a4ae5.info
  108. BIN Library/metadata/56/5650f4ff69d984745bba9108d990fed6
  109. BIN Library/metadata/56/5650f4ff69d984745bba9108d990fed6.info
  110. BIN Library/metadata/5f/5f32cd94baa94578a686d4b9d6b660f7
  111. BIN Library/metadata/5f/5f32cd94baa94578a686d4b9d6b660f7.info
  112. BIN Library/metadata/67/67db316532143b248b19bb424019b871
  113. BIN Library/metadata/67/67db316532143b248b19bb424019b871.info
  114. BIN Library/metadata/73/739bbd9f364b4268874f9fd86ab3beef
  115. BIN Library/metadata/73/739bbd9f364b4268874f9fd86ab3beef.info
  116. BIN Library/metadata/7c/7cbab2be89b54486bbd23a6fe637d30e
  117. BIN Library/metadata/7c/7cbab2be89b54486bbd23a6fe637d30e.info
  118. BIN Library/metadata/80/80a3616ca19596e4da0f10f14d241e9f
  119. BIN Library/metadata/80/80a3616ca19596e4da0f10f14d241e9f.info
  120. BIN Library/metadata/85/852e56802eb941638acbb491814497b0
  121. BIN Library/metadata/85/852e56802eb941638acbb491814497b0.info
  122. BIN Library/metadata/87/870353891bb340e2b2a9c8707e7419ba
  123. BIN Library/metadata/87/870353891bb340e2b2a9c8707e7419ba.info
  124. BIN Library/metadata/8e/8e0cd8ed44d4412cbe0642067abc9e44
  125. BIN Library/metadata/8e/8e0cd8ed44d4412cbe0642067abc9e44.info
  126. BIN Library/metadata/94/94d34846f3d3d4944a3e014509e6a2cb
  127. BIN Library/metadata/94/94d34846f3d3d4944a3e014509e6a2cb.info
  128. BIN Library/metadata/97/97decbdab0634cdd991f8d23ddf0dead
  129. BIN Library/metadata/97/97decbdab0634cdd991f8d23ddf0dead.info
  130. BIN Library/metadata/99/99397ff1167bee7468d9086851d46576
  131. BIN Library/metadata/99/99397ff1167bee7468d9086851d46576.info
  132. BIN Library/metadata/99/99f985a59b4626d4190f1deec7321ede
  133. BIN Library/metadata/99/99f985a59b4626d4190f1deec7321ede.info
  134. BIN Library/metadata/9a/9a4913d2cbdee7a4eae858e155c6dc31
  135. BIN Library/metadata/9a/9a4913d2cbdee7a4eae858e155c6dc31.info
  136. BIN Library/metadata/ad/adebbd281f1a4ef3a30be7f21937e02f
  137. BIN Library/metadata/ad/adebbd281f1a4ef3a30be7f21937e02f.info
  138. BIN Library/metadata/b9/b96e871714f8e644489414e01c0c1f71
  139. BIN Library/metadata/b9/b96e871714f8e644489414e01c0c1f71.info
  140. BIN Library/metadata/d5/d504102d2c22b864e911bc08c653e742
  141. BIN Library/metadata/d5/d504102d2c22b864e911bc08c653e742.info
  142. BIN Library/metadata/f5/f5f67c52d1564df4a8936ccd202a3bd8
  143. BIN Library/metadata/f5/f5f67c52d1564df4a8936ccd202a3bd8.info
  144. BIN ProjectSettings/AudioManager.asset
  145. BIN ProjectSettings/ClusterInputManager.asset
  146. BIN ProjectSettings/DynamicsManager.asset
  147. BIN ProjectSettings/EditorBuildSettings.asset
  148. BIN ProjectSettings/EditorSettings.asset
  149. BIN ProjectSettings/GraphicsSettings.asset
  150. BIN ProjectSettings/InputManager.asset
  151. BIN ProjectSettings/NavMeshAreas.asset
  152. BIN ProjectSettings/NetworkManager.asset
  153. BIN ProjectSettings/Physics2DSettings.asset
  154. BIN ProjectSettings/ProjectSettings.asset
  155. +2 −0 ProjectSettings/ProjectVersion.txt
  156. BIN ProjectSettings/QualitySettings.asset
  157. BIN ProjectSettings/TagManager.asset
  158. BIN ProjectSettings/TimeManager.asset
  159. BIN ProjectSettings/UnityAdsSettings.asset
  160. BIN ProjectSettings/UnityConnectSettings.asset
  161. +73 −0 unity_grid.CSharp.csproj
  162. +20 −0 unity_grid.sln

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
Binary file not shown.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
Binary file not shown.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -0,0 +1,21 @@
using UnityEngine;
using System.Collections;
public class Demo_Grid : MonoBehaviour {
public Grid grid;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetMouseButtonUp(0))
{
grid.ApplyDirectedForce(Vector2.up * 10f, Camera.main.ScreenToWorldPoint(Input.mousePosition), 1f);
}
}
}

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -0,0 +1,298 @@
using UnityEngine;
using System.Collections.Generic;
public class Grid : MonoBehaviour
{
public Rect size;
public Vector2 spacing;
public float minLineWidth = 0.01f;
public float maxLineWidth = 0.03f;
public int maxInstantiatedLines = 1520;
public GameObject linePrefab;
private Spring[] springs;
private PointMass[,] points;
private List<SpriteRenderer> m_Renderers;
private int m_LineIndex = 0;
void Start()
{
m_Renderers = new List<SpriteRenderer>();
var springList = new List<Spring>();
int numColumns = (int)(size.width / spacing.x) + 1;
int numRows = (int)(size.height / spacing.y) + 1;
points = new PointMass[numColumns, numRows];
// these fixed points will be used to anchor the grid to fixed positions on the screen
PointMass[,] fixedPoints = new PointMass[numColumns, numRows];
// create the point masses
int column = 0, row = 0;
for (float y = size.yMin; y <= size.yMax; y += spacing.y)
{
for (float x = size.xMin; x <= size.xMax; x += spacing.x)
{
points[column, row] = new PointMass(new Vector3(x, y, 0), 1);
fixedPoints[column, row] = new PointMass(new Vector3(x, y, 0), 0);
column++;
}
row++;
column = 0;
}
int count = 0;
// link the point masses with springs
for (int y = 0; y < numRows; y++)
{
for (int x = 0; x < numColumns; x++)
{
if (x == 0 || y == 0 || x == numColumns - 1 || y == numRows - 1) // anchor the border of the grid
{
springList.Add(new Spring(fixedPoints[x, y], points[x, y], 0.1f, 0.1f));
}
else if (x % 3 == 0 && y % 3 == 0) // loosely anchor 1/9th of the point masses
{
springList.Add(new Spring(fixedPoints[x, y], points[x, y], 0.002f, 0.02f));
}
const float stiffness = 0.28f;
const float damping = 0.06f;
if (x > 0)
{
springList.Add(new Spring(points[x - 1, y], points[x, y], stiffness, damping));
}
if (y > 0)
{
springList.Add(new Spring(points[x, y - 1], points[x, y], stiffness, damping));
}
count++;
}
}
for(int i = 0; i < maxInstantiatedLines; i++)
{
m_Renderers.Add(GetNewLineRenderer());
}
springs = springList.ToArray();
}
private SpriteRenderer GetNewLineRenderer()
{
GameObject line = (GameObject)Instantiate(linePrefab);
line.transform.SetParent(transform);
return line.GetComponent<SpriteRenderer>();
}
void Update()
{
foreach (var spring in springs)
{
spring.Update();
}
foreach (var mass in points)
{
mass.Update();
}
Draw();
print("Drawn: " + numDrawn);
numDrawn = 0;
}
private void Draw()
{
int width = points.GetLength(0);
int height = points.GetLength(1);
Color color = Color.green;
for (int y = 1; y < height; y++)
{
for (int x = 1; x < width; x++)
{
Vector2 left = new Vector2(), up = new Vector2();
Vector2 p = ToVec2(points[x, y].Position);
if (x > 1)
{
left = ToVec2(points[x - 1, y].Position);
float thickness = y % 3 == 1 ? maxLineWidth : minLineWidth;
// use Catmull-Rom interpolation to help smooth bends in the grid
int clampedX = Mathf.Min(x + 1, width - 1);
Vector2 mid = Interpolate.CatmullRom(ToVec2(points[x - 2, y].Position), left, p, ToVec2(points[clampedX, y].Position), 0.5f, 05f);
// If the grid is very straight here, draw a single straight line. Otherwise, draw lines to our
// new interpolated midpoint
var dist = Vector2.Distance(mid, (left + p) / 2);
if (dist * dist > 1)
{
DrawLine(left, mid, color, thickness, m_Renderers[m_LineIndex]);
m_LineIndex = (m_LineIndex + 1) % m_Renderers.Count;
DrawLine(mid, p, color, thickness, m_Renderers[m_LineIndex]);
m_LineIndex = (m_LineIndex + 1) % m_Renderers.Count;
}
else
{
DrawLine(left, p, color, thickness, m_Renderers[m_LineIndex]);
m_LineIndex = (m_LineIndex + 1) % m_Renderers.Count;
}
}
if (y > 1)
{
up = ToVec2(points[x, y - 1].Position);
float thickness = x % 3 == 1 ? maxLineWidth : minLineWidth;
int clampedY = Mathf.Min(y + 1, height - 1);
Vector2 mid = Interpolate.CatmullRom(ToVec2(points[x, y - 2].Position), up, p, ToVec2(points[x, clampedY].Position), 0.5f, 0.5f);
var dist = Vector2.Distance(mid, (up + p) / 2);
if (dist * dist > 1)
{
DrawLine(up, mid, color, thickness, m_Renderers[m_LineIndex]);
m_LineIndex = (m_LineIndex + 1) % m_Renderers.Count;
DrawLine(mid, p, color, thickness, m_Renderers[m_LineIndex]);
m_LineIndex = (m_LineIndex + 1) % m_Renderers.Count;
}
else
{
DrawLine(up, p, color, thickness, m_Renderers[m_LineIndex]);
m_LineIndex = (m_LineIndex + 1) % m_Renderers.Count;
}
}
// Add interpolated lines halfway between our point masses. This makes the grid look
// denser without the cost of simulating more springs and point masses.
if (x > 1 && y > 1)
{
Vector2 upLeft = ToVec2(points[x - 1, y - 1].Position);
DrawLine(0.5f * (upLeft + up), 0.5f * (left + p), color, minLineWidth, m_Renderers[m_LineIndex]); // vertical line
m_LineIndex = (m_LineIndex + 1) % m_Renderers.Count;
DrawLine(0.5f * (upLeft + left), 0.5f * (up + p), color, minLineWidth, m_Renderers[m_LineIndex]); // horizontal line
m_LineIndex = (m_LineIndex + 1) % m_Renderers.Count;
}
/* Vector2 left = new Vector2(), up = new Vector2();
Vector2 p = ToVec2(points[x, y].Position);
if (x > 1)
{
left = ToVec2(points[x - 1, y].Position);
float thickness = y % 3 == 1 ? .1f : .03f;
DrawLine(left, p, color, thickness, m_Renderers[m_LineIndex]);
m_LineIndex = (m_LineIndex + 1) % m_Renderers.Count;
}
if (y > 1)
{
up = ToVec2(points[x, y - 1].Position);
float thickness = x % 3 == 1 ? .1f : .03f;
DrawLine(up, p, color, thickness, m_Renderers[m_LineIndex]);
m_LineIndex = (m_LineIndex + 1) % m_Renderers.Count;
}
if (x > 1 && y > 1)
{
Vector2 upLeft = ToVec2(points[x - 1, y - 1].Position);
DrawLine(0.5f * (upLeft + up), 0.5f * (left + p), color, 0.03f, m_Renderers[m_LineIndex]); // vertical line
m_LineIndex = (m_LineIndex + 1) % m_Renderers.Count;
DrawLine(0.5f * (upLeft + left), 0.5f * (up + p), color, 0.03f, m_Renderers[m_LineIndex]); // horizontal line
m_LineIndex = (m_LineIndex + 1) % m_Renderers.Count;
}*/
}
}
}
int numDrawn = 0;
private void DrawLine(Vector2 start, Vector2 end, Color color, float thickness, SpriteRenderer renderer)
{
numDrawn++;
var heading = end - start;
var distance = heading.magnitude;
var direction = heading / distance;
//Debug.Log("Start: " + start);
Vector3 centerPos = new Vector3(start.x + end.x, start.y + end.y) / 2;
renderer.transform.position = centerPos;
// angle
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
renderer.transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
//length
var objectWidthSize = 10f / 5f; // 10 = pixels of line sprite, 5f = pixels per units of line sprite.
renderer.transform.localScale = new Vector3(distance / objectWidthSize, thickness, renderer.transform.localScale.z);
//colour
renderer.color = color;
}
public Vector2 ToVec2(Vector3 v)
{
// do a perspective projection
float factor = (v.z + 2000) / 2000;
var screenSize = new Vector2(Screen.width, Screen.height);
return (new Vector2(v.x, v.y) - screenSize / 2f) * factor + screenSize / 2;
}
public void ApplyDirectedForce(Vector3 force, Vector3 position, float radius)
{
foreach (var mass in points)
{
var dist = Vector2.Distance(position, mass.Position);
if (dist < radius)
mass.ApplyForce(10 * force / (10 + Vector3.Distance(position, mass.Position)));
}
}
public void ApplyImplosiveForce(float force, Vector3 position, float radius)
{
foreach (var mass in points)
{
float dist2 = Vector2.Distance(position, mass.Position);
if (dist2 < radius)
{
mass.ApplyForce(10 * force * (position - mass.Position) / (100 + dist2));
mass.IncreaseDamping(0.6f);
}
}
}
public void ApplyExplosiveForce(float force, Vector3 position, float radius)
{
print(position);
foreach (var mass in points)
{
float dist2 = Vector2.Distance(position, mass.Position);
if (dist2 < radius)
{
mass.ApplyForce(100 * force * (mass.Position - position) / (10000 + dist2));
mass.IncreaseDamping(0.6f);
}
}
}
}
Oops, something went wrong.

0 comments on commit 6aba432

Please sign in to comment.