SionAraAra 1 неделя назад
Родитель
Сommit
f087b033c6

+ 239 - 0
Valley_Runner/Assets/Scenes/Game.unity

@@ -2755,6 +2755,115 @@ CanvasRenderer:
   m_PrefabAsset: {fileID: 0}
   m_GameObject: {fileID: 676987666}
   m_CullTransparentMesh: 1
+--- !u!1 &739003891
+GameObject:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  serializedVersion: 6
+  m_Component:
+  - component: {fileID: 739003895}
+  - component: {fileID: 739003894}
+  - component: {fileID: 739003893}
+  - component: {fileID: 739003892}
+  m_Layer: 0
+  m_Name: Plane
+  m_TagString: Untagged
+  m_Icon: {fileID: 0}
+  m_NavMeshLayer: 0
+  m_StaticEditorFlags: 0
+  m_IsActive: 0
+--- !u!64 &739003892
+MeshCollider:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 739003891}
+  m_Material: {fileID: 0}
+  m_IncludeLayers:
+    serializedVersion: 2
+    m_Bits: 0
+  m_ExcludeLayers:
+    serializedVersion: 2
+    m_Bits: 0
+  m_LayerOverridePriority: 0
+  m_IsTrigger: 0
+  m_ProvidesContacts: 0
+  m_Enabled: 1
+  serializedVersion: 5
+  m_Convex: 0
+  m_CookingOptions: 30
+  m_Mesh: {fileID: 10209, guid: 0000000000000000e000000000000000, type: 0}
+--- !u!23 &739003893
+MeshRenderer:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 739003891}
+  m_Enabled: 1
+  m_CastShadows: 1
+  m_ReceiveShadows: 1
+  m_DynamicOccludee: 1
+  m_StaticShadowCaster: 0
+  m_MotionVectors: 1
+  m_LightProbeUsage: 1
+  m_ReflectionProbeUsage: 1
+  m_RayTracingMode: 2
+  m_RayTraceProcedural: 0
+  m_RayTracingAccelStructBuildFlagsOverride: 0
+  m_RayTracingAccelStructBuildFlags: 1
+  m_SmallMeshCulling: 1
+  m_RenderingLayerMask: 1
+  m_RendererPriority: 0
+  m_Materials:
+  - {fileID: 2100000, guid: 31321ba15b8f8eb4c954353edc038b1d, type: 2}
+  m_StaticBatchInfo:
+    firstSubMesh: 0
+    subMeshCount: 0
+  m_StaticBatchRoot: {fileID: 0}
+  m_ProbeAnchor: {fileID: 0}
+  m_LightProbeVolumeOverride: {fileID: 0}
+  m_ScaleInLightmap: 1
+  m_ReceiveGI: 1
+  m_PreserveUVs: 0
+  m_IgnoreNormalsForChartDetection: 0
+  m_ImportantGI: 0
+  m_StitchLightmapSeams: 1
+  m_SelectedEditorRenderState: 3
+  m_MinimumChartSize: 4
+  m_AutoUVMaxDistance: 0.5
+  m_AutoUVMaxAngle: 89
+  m_LightmapParameters: {fileID: 0}
+  m_SortingLayerID: 0
+  m_SortingLayer: 0
+  m_SortingOrder: 0
+  m_AdditionalVertexStreams: {fileID: 0}
+--- !u!33 &739003894
+MeshFilter:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 739003891}
+  m_Mesh: {fileID: 10209, guid: 0000000000000000e000000000000000, type: 0}
+--- !u!4 &739003895
+Transform:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 739003891}
+  serializedVersion: 2
+  m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
+  m_LocalPosition: {x: 829.2313, y: 521.19904, z: -400.91602}
+  m_LocalScale: {x: 1, y: 1, z: 1}
+  m_ConstrainProportionsScale: 0
+  m_Children: []
+  m_Father: {fileID: 0}
+  m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
 --- !u!1 &836302148
 GameObject:
   m_ObjectHideFlags: 0
@@ -2891,6 +3000,134 @@ CanvasRenderer:
   m_PrefabAsset: {fileID: 0}
   m_GameObject: {fileID: 836302148}
   m_CullTransparentMesh: 1
+--- !u!1 &842308759
+GameObject:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  serializedVersion: 6
+  m_Component:
+  - component: {fileID: 842308762}
+  - component: {fileID: 842308761}
+  - component: {fileID: 842308760}
+  m_Layer: 0
+  m_Name: Map Generator
+  m_TagString: Untagged
+  m_Icon: {fileID: 0}
+  m_NavMeshLayer: 0
+  m_StaticEditorFlags: 0
+  m_IsActive: 1
+--- !u!114 &842308760
+MonoBehaviour:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 842308759}
+  m_Enabled: 1
+  m_EditorHideFlags: 0
+  m_Script: {fileID: 11500000, guid: 4aeb0f3a8e031433495f2ddd66acbef4, type: 3}
+  m_Name: 
+  m_EditorClassIdentifier: 
+  textureRender: {fileID: 0}
+  meshFilter: {fileID: 0}
+  meshRenderer: {fileID: 0}
+--- !u!114 &842308761
+MonoBehaviour:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 842308759}
+  m_Enabled: 1
+  m_EditorHideFlags: 0
+  m_Script: {fileID: 11500000, guid: 916b4f4143ed7456294aa19afa97bde8, type: 3}
+  m_Name: 
+  m_EditorClassIdentifier: 
+  drawMode: 2
+  levelOfDetail: 0
+  noiseScale: 25
+  octaves: 5
+  persistance: 0.5
+  lacunarity: 2
+  seed: 0
+  offset: {x: 51, y: 0}
+  meshHeightMultiplier: 13
+  meshHeightCurve:
+    serializedVersion: 2
+    m_Curve:
+    - serializedVersion: 3
+      time: 0
+      value: 0
+      inSlope: 0
+      outSlope: 0
+      tangentMode: 0
+      weightedMode: 0
+      inWeight: 0.33333334
+      outWeight: 0.33333334
+    - serializedVersion: 3
+      time: 0.42305937
+      value: 0.028682068
+      inSlope: 0.1906106
+      outSlope: 0.1906106
+      tangentMode: 0
+      weightedMode: 0
+      inWeight: 0.33333334
+      outWeight: 0.33333334
+    - serializedVersion: 3
+      time: 1
+      value: 1
+      inSlope: 4.1107154
+      outSlope: 4.1107154
+      tangentMode: 0
+      weightedMode: 0
+      inWeight: 0.33333334
+      outWeight: 0.33333334
+    m_PreInfinity: 2
+    m_PostInfinity: 2
+    m_RotationOrder: 0
+  autoUpdate: 1
+  regions:
+  - name: Water Deep
+    height: 0.3
+    colour: {r: 0.19679932, g: 0.38871264, b: 0.7647059, a: 0}
+  - name: Water Shallow
+    height: 0.4
+    colour: {r: 0.21204588, g: 0.4037765, b: 0.7794118, a: 0}
+  - name: Sand
+    height: 0.45
+    colour: {r: 0.8235294, g: 0.81434196, b: 0.49048445, a: 0}
+  - name: Grass
+    height: 0.55
+    colour: {r: 0.33856717, g: 0.5955882, b: 0.09196584, a: 0}
+  - name: Grass 2
+    height: 0.6
+    colour: {r: 0.24139647, g: 0.41911763, b: 0.07088018, a: 0}
+  - name: Rock
+    height: 0.7
+    colour: {r: 0.35294116, g: 0.27240184, b: 0.23615916, a: 0}
+  - name: Rock2
+    height: 0.9
+    colour: {r: 0.29411763, g: 0.23507784, b: 0.20977508, a: 0}
+  - name: Snow
+    height: 1
+    colour: {r: 1, g: 1, b: 1, a: 0}
+--- !u!4 &842308762
+Transform:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 842308759}
+  serializedVersion: 2
+  m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
+  m_LocalPosition: {x: 829.2313, y: 521.19904, z: -400.91602}
+  m_LocalScale: {x: 1, y: 1, z: 1}
+  m_ConstrainProportionsScale: 0
+  m_Children: []
+  m_Father: {fileID: 0}
+  m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
 --- !u!1 &850812445
 GameObject:
   m_ObjectHideFlags: 0
@@ -7167,3 +7404,5 @@ SceneRoots:
   - {fileID: 1795929184}
   - {fileID: 1252737535}
   - {fileID: 1825512302}
+  - {fileID: 842308762}
+  - {fileID: 739003895}

+ 20 - 0
Valley_Runner/Assets/Scripts/MapDisplay.cs

@@ -0,0 +1,20 @@
+using UnityEngine;
+using System.Collections;
+
+public class MapDisplay : MonoBehaviour {
+
+	public Renderer textureRender;
+	public MeshFilter meshFilter;
+	public MeshRenderer meshRenderer;
+
+	public void DrawTexture(Texture2D texture) {
+		textureRender.sharedMaterial.mainTexture = texture;
+		textureRender.transform.localScale = new Vector3 (texture.width, 1, texture.height);
+	}
+
+	public void DrawMesh(MeshData meshData, Texture2D texture) {
+		meshFilter.sharedMesh = meshData.CreateMesh ();
+		meshRenderer.sharedMaterial.mainTexture = texture;
+	}
+
+}

+ 12 - 0
Valley_Runner/Assets/Scripts/MapDisplay.cs.meta

@@ -0,0 +1,12 @@
+fileFormatVersion: 2
+guid: 4aeb0f3a8e031433495f2ddd66acbef4
+timeCreated: 1454754170
+licenseType: Pro
+MonoImporter:
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 70 - 0
Valley_Runner/Assets/Scripts/MapGenerator.cs

@@ -0,0 +1,70 @@
+using UnityEngine;
+using System.Collections;
+
+public class MapGenerator : MonoBehaviour {
+
+	public enum DrawMode {NoiseMap, ColourMap, Mesh};
+	public DrawMode drawMode;
+
+	const int mapChunkSize = 241;
+	[Range(0,6)]
+	public int levelOfDetail;
+	public float noiseScale;
+
+	public int octaves;
+	[Range(0,1)]
+	public float persistance;
+	public float lacunarity;
+
+	public int seed;
+	public Vector2 offset;
+
+	public float meshHeightMultiplier;
+	public AnimationCurve meshHeightCurve;
+
+	public bool autoUpdate;
+
+	public TerrainType[] regions;
+
+	public void GenerateMap() {
+		float[,] noiseMap = Noise.GenerateNoiseMap (mapChunkSize, mapChunkSize, seed, noiseScale, octaves, persistance, lacunarity, offset);
+
+		Color[] colourMap = new Color[mapChunkSize * mapChunkSize];
+		for (int y = 0; y < mapChunkSize; y++) {
+			for (int x = 0; x < mapChunkSize; x++) {
+				float currentHeight = noiseMap [x, y];
+				for (int i = 0; i < regions.Length; i++) {
+					if (currentHeight <= regions [i].height) {
+						colourMap [y * mapChunkSize + x] = regions [i].colour;
+						break;
+					}
+				}
+			}
+		}
+
+		MapDisplay display = FindObjectOfType<MapDisplay> ();
+		if (drawMode == DrawMode.NoiseMap) {
+			display.DrawTexture (TextureGenerator.TextureFromHeightMap (noiseMap));
+		} else if (drawMode == DrawMode.ColourMap) {
+			display.DrawTexture (TextureGenerator.TextureFromColourMap (colourMap, mapChunkSize, mapChunkSize));
+		} else if (drawMode == DrawMode.Mesh) {
+			display.DrawMesh (MeshGenerator.GenerateTerrainMesh (noiseMap, meshHeightMultiplier, meshHeightCurve, levelOfDetail), TextureGenerator.TextureFromColourMap (colourMap, mapChunkSize, mapChunkSize));
+		}
+	}
+
+	void OnValidate() {
+		if (lacunarity < 1) {
+			lacunarity = 1;
+		}
+		if (octaves < 0) {
+			octaves = 0;
+		}
+	}
+}
+
+[System.Serializable]
+public struct TerrainType {
+	public string name;
+	public float height;
+	public Color colour;
+}

+ 12 - 0
Valley_Runner/Assets/Scripts/MapGenerator.cs.meta

@@ -0,0 +1,12 @@
+fileFormatVersion: 2
+guid: 916b4f4143ed7456294aa19afa97bde8
+timeCreated: 1454754067
+licenseType: Pro
+MonoImporter:
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 66 - 0
Valley_Runner/Assets/Scripts/MeshGenerator.cs

@@ -0,0 +1,66 @@
+using UnityEngine;
+using System.Collections;
+
+public static class MeshGenerator {
+
+	public static MeshData GenerateTerrainMesh(float[,] heightMap, float heightMultiplier, AnimationCurve heightCurve, int levelOfDetail) {
+		int width = heightMap.GetLength (0);
+		int height = heightMap.GetLength (1);
+		float topLeftX = (width - 1) / -2f;
+		float topLeftZ = (height - 1) / 2f;
+
+		int meshSimplificationIncrement = (levelOfDetail == 0)?1:levelOfDetail * 2;
+		int verticesPerLine = (width - 1) / meshSimplificationIncrement + 1;
+
+		MeshData meshData = new MeshData (verticesPerLine, verticesPerLine);
+		int vertexIndex = 0;
+
+		for (int y = 0; y < height; y += meshSimplificationIncrement) {
+			for (int x = 0; x < width; x += meshSimplificationIncrement) {
+				meshData.vertices [vertexIndex] = new Vector3 (topLeftX + x, heightCurve.Evaluate(heightMap [x, y]) * heightMultiplier, topLeftZ - y);
+				meshData.uvs [vertexIndex] = new Vector2 (x / (float)width, y / (float)height);
+
+				if (x < width - 1 && y < height - 1) {
+					meshData.AddTriangle (vertexIndex, vertexIndex + verticesPerLine + 1, vertexIndex + verticesPerLine);
+					meshData.AddTriangle (vertexIndex + verticesPerLine + 1, vertexIndex, vertexIndex + 1);
+				}
+
+				vertexIndex++;
+			}
+		}
+
+		return meshData;
+
+	}
+}
+
+public class MeshData {
+	public Vector3[] vertices;
+	public int[] triangles;
+	public Vector2[] uvs;
+
+	int triangleIndex;
+
+	public MeshData(int meshWidth, int meshHeight) {
+		vertices = new Vector3[meshWidth * meshHeight];
+		uvs = new Vector2[meshWidth * meshHeight];
+		triangles = new int[(meshWidth-1)*(meshHeight-1)*6];
+	}
+
+	public void AddTriangle(int a, int b, int c) {
+		triangles [triangleIndex] = a;
+		triangles [triangleIndex + 1] = b;
+		triangles [triangleIndex + 2] = c;
+		triangleIndex += 3;
+	}
+
+	public Mesh CreateMesh() {
+		Mesh mesh = new Mesh ();
+		mesh.vertices = vertices;
+		mesh.triangles = triangles;
+		mesh.uv = uvs;
+		mesh.RecalculateNormals ();
+		return mesh;
+	}
+
+}

+ 12 - 0
Valley_Runner/Assets/Scripts/MeshGenerator.cs.meta

@@ -0,0 +1,12 @@
+fileFormatVersion: 2
+guid: 7939d15f4beb24a65bc949a0e3c02d5e
+timeCreated: 1455965849
+licenseType: Pro
+MonoImporter:
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 64 - 0
Valley_Runner/Assets/Scripts/Noise.cs

@@ -0,0 +1,64 @@
+using UnityEngine;
+using System.Collections;
+
+public static class Noise {
+
+	public static float[,] GenerateNoiseMap(int mapWidth, int mapHeight, int seed, float scale, int octaves, float persistance, float lacunarity, Vector2 offset) {
+		float[,] noiseMap = new float[mapWidth,mapHeight];
+
+		System.Random prng = new System.Random (seed);
+		Vector2[] octaveOffsets = new Vector2[octaves];
+		for (int i = 0; i < octaves; i++) {
+			float offsetX = prng.Next (-100000, 100000) + offset.x;
+			float offsetY = prng.Next (-100000, 100000) + offset.y;
+			octaveOffsets [i] = new Vector2 (offsetX, offsetY);
+		}
+
+		if (scale <= 0) {
+			scale = 0.0001f;
+		}
+
+		float maxNoiseHeight = float.MinValue;
+		float minNoiseHeight = float.MaxValue;
+
+		float halfWidth = mapWidth / 2f;
+		float halfHeight = mapHeight / 2f;
+
+
+		for (int y = 0; y < mapHeight; y++) {
+			for (int x = 0; x < mapWidth; x++) {
+		
+				float amplitude = 1;
+				float frequency = 1;
+				float noiseHeight = 0;
+
+				for (int i = 0; i < octaves; i++) {
+					float sampleX = (x-halfWidth) / scale * frequency + octaveOffsets[i].x;
+					float sampleY = (y-halfHeight) / scale * frequency + octaveOffsets[i].y;
+
+					float perlinValue = Mathf.PerlinNoise (sampleX, sampleY) * 2 - 1;
+					noiseHeight += perlinValue * amplitude;
+
+					amplitude *= persistance;
+					frequency *= lacunarity;
+				}
+
+				if (noiseHeight > maxNoiseHeight) {
+					maxNoiseHeight = noiseHeight;
+				} else if (noiseHeight < minNoiseHeight) {
+					minNoiseHeight = noiseHeight;
+				}
+				noiseMap [x, y] = noiseHeight;
+			}
+		}
+
+		for (int y = 0; y < mapHeight; y++) {
+			for (int x = 0; x < mapWidth; x++) {
+				noiseMap [x, y] = Mathf.InverseLerp (minNoiseHeight, maxNoiseHeight, noiseMap [x, y]);
+			}
+		}
+
+		return noiseMap;
+	}
+
+}

+ 12 - 0
Valley_Runner/Assets/Scripts/Noise.cs.meta

@@ -0,0 +1,12 @@
+fileFormatVersion: 2
+guid: d4ed0d0ddb42c4af290db1cea4b1264f
+timeCreated: 1454753270
+licenseType: Pro
+MonoImporter:
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 30 - 0
Valley_Runner/Assets/Scripts/TextureGenerator.cs

@@ -0,0 +1,30 @@
+using UnityEngine;
+using System.Collections;
+
+public static class TextureGenerator {
+
+	public static Texture2D TextureFromColourMap(Color[] colourMap, int width, int height) {
+		Texture2D texture = new Texture2D (width, height);
+		texture.filterMode = FilterMode.Point;
+		texture.wrapMode = TextureWrapMode.Clamp;
+		texture.SetPixels (colourMap);
+		texture.Apply ();
+		return texture;
+	}
+
+
+	public static Texture2D TextureFromHeightMap(float[,] heightMap) {
+		int width = heightMap.GetLength (0);
+		int height = heightMap.GetLength (1);
+
+		Color[] colourMap = new Color[width * height];
+		for (int y = 0; y < height; y++) {
+			for (int x = 0; x < width; x++) {
+				colourMap [y * width + x] = Color.Lerp (Color.black, Color.white, heightMap [x, y]);
+			}
+		}
+
+		return TextureFromColourMap (colourMap, width, height);
+	}
+
+}

+ 12 - 0
Valley_Runner/Assets/Scripts/TextureGenerator.cs.meta

@@ -0,0 +1,12 @@
+fileFormatVersion: 2
+guid: df7acc444474841759c3b95c7ef6d1c3
+timeCreated: 1455354562
+licenseType: Pro
+MonoImporter:
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: