Commit be658bcb authored by kgingras's avatar kgingras
Browse files

circle arena. pillars. mobius cube. twist ring. maze prefabs

parent 5cd00951
using UnityEngine;
using System.Collections;
public class Level {
public Mesh mesh;
private bool drawDebugMesh;
private Path path;
private Face[] faces;
private int[] triangles;
private Vector3[] vertices;
private Vector2[] uvs;
public Level (int len, float startRadius, float maxRadius, float partLength, int sides, bool draw) {
float startTime = Time.realtimeSinceStartup;
drawDebugMesh = draw;
mesh = new Mesh ();
uvs = new Vector2[len * sides];
path = new Path (len, startRadius, maxRadius, partLength, drawDebugMesh);
faces = GenerateFaces (path, sides, drawDebugMesh);
vertices = GenerateVertices (len, sides, faces);
triangles = GenerateTriangles (len, sides, vertices);
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.uv = uvs;
mesh.RecalculateNormals ();
mesh.Optimize ();
Debug.Log ((Time.realtimeSinceStartup - startTime) * 1000 + "ms for generating level");
}
private Face[] GenerateFaces (Path p, int sides, bool drawDebugMesh) {
Face[] f = new Face[p.length];
for (int n = 0; n < f.Length; n++) {
f[n] = new Face (p.radius[n], sides, p.centerPoints[n], p.centerPointNormals[n], p.faceRotation[n], drawDebugMesh);
if (n != 0 && drawDebugMesh) {
for (int i = 1; i < f[n].vertices.Length; i++) {
Debug.DrawLine (f[n].vertices[i], f[n - 1].vertices[i], Color.red, 600);
}
}
}
return f;
}
private Vector3[] GenerateVertices (int len, int sides, Face[] face) {
Vector3[] v = new Vector3[len * sides];
for (int n = 0; n < face.Length; n++) {
for (int i = 0; i < sides; i++) {
v[n * (sides) + i] = face[n].vertices[i];
}
}
return v;
}
private int[] GenerateTriangles (int len, int sides, Vector3[] verts) {
int[] tris = new int[verts.Length * 6];
for (int n = 0; n < len - 1; n++) {
for (int i = 0; i < sides; i++) {
if (i != sides - 1) {
tris[n * (sides) * 6 + i * 6] = (n * sides) + i;
tris[n * (sides) * 6 + i * 6 + 1] = (n + 1) * sides + i;
tris[n * (sides) * 6 + i * 6 + 2] = (n + 1) * sides + 1 + i;
tris[n * (sides) * 6 + i * 6 + 3] = (n * sides) + i;
tris[n * (sides) * 6 + i * 6 + 4] = (n + 1) * sides + 1 + i;
tris[n * (sides) * 6 + i * 6 + 5] = (n * sides) + i + 1;
} else {
tris[n * (sides) * 6 + i * 6] = (n + 1) * sides - 1;
tris[n * (sides) * 6 + i * 6 + 1] = sides * (n + 1) + i;
tris[n * (sides) * 6 + i * 6 + 2] = sides * (n + 1);
tris[n * (sides) * 6 + i * 6 + 3] = (n + 1) * sides - 1;
tris[n * (sides) * 6 + i * 6 + 4] = sides * (n + 1);
tris[n * (sides) * 6 + i * 6 + 5] = n * sides;
}
}
}
return tris;
}
public struct Path {
public int length; // Length (in circles) of the tunnel
public float[] radius; // The radius of each circle
public Vector3[] centerPoints; // The center point of each circle
public Vector3[] centerPointNormals; // The normal of each circle
public Vector3[] faceRotation;
public Path (int len, float startRadius, float maxRadius, float partLength, bool drawDebugMesh) {
length = len;
radius = new float[length];
centerPoints = new Vector3[length];
centerPointNormals = new Vector3[length];
faceRotation = new Vector3[length];
radius[0] = startRadius;
centerPoints[0] = Vector3.zero;
faceRotation[0] = Vector3.zero;
for (int n = 1; n < length; n++) {
radius[n] = startRadius;
//radius[n] = radius[n - 1] + Random.Range (-0.5f, 0.5f);
faceRotation[n] = new Vector3 (faceRotation[n - 1].x + Random.Range (-0.2f, 0.2f), faceRotation[n - 1].y + Random.Range (-0.2f, 0.2f), 0);
centerPoints[n].x = partLength * Mathf.Sin (faceRotation[n].y) * Mathf.Cos (faceRotation[n].x) + centerPoints[n - 1].x;
centerPoints[n].y = partLength * Mathf.Cos (faceRotation[n].y) * Mathf.Sin (faceRotation[n].x) + centerPoints[n - 1].y;
centerPoints[n].z = partLength * Mathf.Cos (faceRotation[n].x) * Mathf.Cos (faceRotation[n].y) + centerPoints[n - 1].z;
if (drawDebugMesh) {
Debug.DrawLine (centerPoints[n - 1], centerPoints[n], Color.yellow, 600);
}
}
for (int n = 0; n < length - 1; n++) {
centerPointNormals[n] = centerPoints[n] - centerPoints[n + 1];
}
}
}
public struct Face {
public Vector3[] vertices;
public Face (float r, int sides, Vector3 centerPoint, Vector3 centerPointNormal, Vector3 rotation, bool drawDebugMesh) {
vertices = new Vector3[sides];
float angle = 2 * Mathf.PI / sides;
Vector3 normalized = centerPointNormal;
Vector3 ortho1 = Vector3.up;
Vector3 ortho2 = Vector3.right;
Vector3.OrthoNormalize(ref normalized, ref ortho1, ref ortho2);
for (int n = 0; n < sides; n++) {
//vertices[n].x = centerPoint.x + r * Mathf.Sin(n * angle);
//vertices[n].y = centerPoint.y + r * Mathf.Cos(n * angle);
//vertices[n].z = centerPoint.z;
vertices[n] = centerPoint + r * Mathf.Sin(n * angle) * ortho1 + r * Mathf.Cos(n * angle) * ortho2;
//vertices[n] = vertices[n] - Vector3.Dot (vertices[n] - centerPoint, normalized) * normalized;
if (n != 0 && drawDebugMesh) {
Debug.DrawLine (vertices[n], vertices[n - 1], Color.red, 600);
}
if (n == sides - 1 && drawDebugMesh) {
Debug.DrawLine (vertices[n], vertices[0], Color.red, 600);
}
}
}
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: 0f0f4be8d4f95364298925847611e558
timeCreated: 1454654769
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: b7560e49b16858c4c816dee8967acc21
timeCreated: 1454467866
guid: 50dbb0ad0f1084c45a5d1886ff386746
timeCreated: 1454648013
licenseType: Free
NativeFormatImporter:
userData:
......
fileFormatVersion: 2
guid: 26d0071bb7160f146b9814343e5b42ae
timeCreated: 1454467847
guid: 24101ace713e3f245a0b4dc047bd733b
timeCreated: 1454662027
licenseType: Free
NativeFormatImporter:
userData:
......
Shader "Custom/HeightDependentTint"
Shader "Custom/HeightDependentTintSF"
{
Properties
{
......@@ -6,8 +6,11 @@
_HeightMin ("Height Min", Float) = -1
_HeightMax ("Height Max", Float) = 1
_ColorMin ("Tint Color At Min", Color) = (0,0,0,1)
_ColorMax ("Tint Color At Max", Color) = (1,1,1,1)
_ColorLowQuart ("Tint Color At Low quarter", Color) = (1,1,1,1)
_ColorMid ("Tint Color At Mid", Color) = (1,1,1,1)
_ColorHighQuart ("Tint Color At High quarter", Color) = (1,1,1,1)
_ColorMax ("Tint Color At Max", Color) = (1,1,1,1)
}
......@@ -22,7 +25,9 @@
fixed4 _ColorMin;
fixed4 _ColorMax;
fixed4 _ColorMid;
fixed4 _ColorLowQuart;
fixed4 _ColorHighQuart;
float _HeightMin;
float _HeightMax;
fixed4 tintColor;
......@@ -36,12 +41,18 @@
void surf (Input IN, inout SurfaceOutput o)
{
half4 c = tex2D (_MainTex, IN.uv_MainTex);
float h = (_HeightMax-IN.worldPos.y) / (_HeightMax-_HeightMin);
if(h<_HeightMax/2){
tintColor = lerp(_ColorMid.rgba, _ColorMin.rgba, h);
// float h = (_HeightMax-IN.worldPos.y) / (_HeightMax-_HeightMin);
if(IN.worldPos.y<_HeightMax/4){
tintColor = lerp(_ColorLowQuart.rgba, _ColorMin.rgba, ((_HeightMax/4)-IN.worldPos.y) / ((_HeightMax/4)-_HeightMin));
}
else if(IN.worldPos.y<_HeightMax/2){
tintColor = lerp(_ColorMid.rgba, _ColorLowQuart.rgba, ((_HeightMax/2)-IN.worldPos.y) / ((_HeightMax/2)-_HeightMax/4));
}
else if(IN.worldPos.y<_HeightMax*3/4){
tintColor = lerp(_ColorHighQuart.rgba, _ColorMid.rgba, ((_HeightMax*3/4)-IN.worldPos.y) / ((_HeightMax*3/4)-(_HeightMax/2)));
}
else{
tintColor = lerp(_ColorMax.rgba, _ColorMid.rgba, h);
tintColor = lerp(_ColorMax.rgba, _ColorHighQuart.rgba, (_HeightMax-IN.worldPos.y) / (_HeightMax-(_HeightMax*3/4)));
}
......
fileFormatVersion: 2
guid: 03eb77c556cce484ebd76cdaa4deeb97
guid: 9b551fb41c245fd4b92d34ff7dcdc13b
folderAsset: yes
timeCreated: 1454465229
timeCreated: 1454642286
licenseType: Free
DefaultImporter:
userData:
......
fileFormatVersion: 2
guid: ff56c9b9b6a4585449587f204f58767d
timeCreated: 1454463864
guid: 8a0d5ae1054c56e4d892a40211f0661c
timeCreated: 1454642286
licenseType: Free
NativeFormatImporter:
userData:
......
fileFormatVersion: 2
guid: 0ee462befb90b0943bf6630e0f7ab682
timeCreated: 1454642753
licenseType: Free
NativeFormatImporter:
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: 0dc45ad7d2400484dbb86f337066df7a
timeCreated: 1454643339
licenseType: Free
NativeFormatImporter:
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: d31bdc3d9a09efc4baa7f1de950203a1
timeCreated: 1454648062
licenseType: Free
NativeFormatImporter:
userData:
assetBundleName:
assetBundleVariant:
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment