Skip to content
Snippets Groups Projects
Commit 059af3a0 authored by Clément's avatar Clément
Browse files

Ajout des fichiers qui génèrent la map

parent feb7ce37
Branches
No related tags found
No related merge requests found
Showing
with 712 additions and 0 deletions
fileFormatVersion: 2
guid: 055f92a72c33e0c4bacc70d3fda0de4f
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[CustomEditor (typeof(MapGenerator))]
public class MapGeneratorEditor : Editor
{
public override void OnInspectorGUI()
{
MapGenerator mapGen = (MapGenerator)target;
if (DrawDefaultInspector())
{
if (mapGen.autoUpdate)
{
mapGen.DrawMapInEditor();
}
}
if (GUILayout.Button("Generate"))
{
mapGen.DrawMapInEditor();
}
}
}
fileFormatVersion: 2
guid: 0dd09e1002d102f4ba69ec7b8f0fa41d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
This diff is collapsed.
fileFormatVersion: 2
guid: 96cc04971e775d946b39cd60ced9dd6f
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EndlessTerrain : MonoBehaviour
{
public const float maxViewDst = 500;
public Transform viewer;
public Material mapMaterial;
public static Vector2 viewerPosition;
static MapGenerator mapGenerator;
int chunkSize;
int chunksVisibleInViewDst;
Dictionary<Vector2, TerrainChunk> terrainChunkDictionnary = new Dictionary<Vector2, TerrainChunk>();
List<TerrainChunk> terrainChunkVisibleLastUpdate = new List<TerrainChunk>();
private void Start()
{
mapGenerator = FindObjectOfType<MapGenerator>();
chunkSize = MapGenerator.mapChunkSize - 1;
chunksVisibleInViewDst = Mathf.RoundToInt(maxViewDst / chunkSize);
}
private void Update()
{
viewerPosition = new Vector2(viewer.position.x, viewer.position.z);
UpdateVisibleChunks();
}
void UpdateVisibleChunks()
{
for (int i = 0; i < terrainChunkVisibleLastUpdate.Count; i++)
{
terrainChunkVisibleLastUpdate[i].SetVisible(false);
}
terrainChunkVisibleLastUpdate.Clear();
int currentChunkCoordX = Mathf.RoundToInt(viewerPosition.x / chunkSize);
int currentChunkCoordY = Mathf.RoundToInt(viewerPosition.y / chunkSize);
for (int yOffset = -chunksVisibleInViewDst; yOffset <= chunksVisibleInViewDst; yOffset++)
{
for (int xOffset = -chunksVisibleInViewDst; xOffset <= chunksVisibleInViewDst; xOffset++)
{
Vector2 viewedChunkCoord = new Vector2(currentChunkCoordX + xOffset, currentChunkCoordY + yOffset);
if (terrainChunkDictionnary.ContainsKey(viewedChunkCoord))
{
terrainChunkDictionnary[viewedChunkCoord].UpdateTerrainChunk();
if (terrainChunkDictionnary[viewedChunkCoord].IsVisible())
{
terrainChunkVisibleLastUpdate.Add(terrainChunkDictionnary[viewedChunkCoord]);
}
}
else
{
terrainChunkDictionnary.Add(viewedChunkCoord, new TerrainChunk(viewedChunkCoord, chunkSize, transform, mapMaterial));
}
}
}
}
public class TerrainChunk
{
GameObject meshObject;
Vector2 position;
Bounds bounds;
MapData mapData;
MeshRenderer meshRenderer;
MeshFilter meshFilter;
public TerrainChunk (Vector2 coord, int size, Transform parent, Material material)
{
position = coord * size;
bounds = new Bounds(position, Vector2.one * size);
Vector3 positionV3 = new Vector3(position.x, 0, position.y);
meshObject = new GameObject("Terrain Chunk");
meshRenderer = meshObject.AddComponent<MeshRenderer>();
meshFilter = meshObject.AddComponent<MeshFilter>();
meshRenderer.material = material;
meshObject.transform.position = positionV3;
meshObject.transform.parent = parent;
SetVisible(false);
mapGenerator.RequestMapData(OnMapDataReceived);
}
void OnMapDataReceived(MapData mapData)
{
Debug.Log("map data recived");
mapGenerator.RequestMeshData(mapData, OnMeshDataReceived);
}
void OnMeshDataReceived(MeshData meshData)
{
meshFilter.mesh = meshData.CreateMesh();
}
public void UpdateTerrainChunk()
{
float viewerDstFromNearestEdge = Mathf.Sqrt(bounds.SqrDistance(viewerPosition));
bool visible = viewerDstFromNearestEdge <= maxViewDst;
SetVisible(visible);
}
public void SetVisible(bool visible)
{
meshObject.SetActive(visible);
}
public bool IsVisible()
{
return meshObject.activeSelf;
}
}
}
fileFormatVersion: 2
guid: 6e81dde687394084e9f84cfd4da4bcbc
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MapDisplay : MonoBehaviour
{
public Renderer textureRenderer;
public MeshFilter meshFilter;
public MeshRenderer meshRenderer;
public void DrawTexture(Texture2D texture)
{
textureRenderer.sharedMaterial.mainTexture = texture;
textureRenderer.transform.localScale = new Vector3(texture.width, 1, texture.height);
}
public void DrawMesh(MeshData meshData, Texture2D texture)
{
meshFilter.sharedMesh = meshData.CreateMesh();
meshRenderer.sharedMaterial.mainTexture = texture;
}
}
fileFormatVersion: 2
guid: 0e0fefe2b81432c439b50b74dfa9cd0a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using System.Collections;
using System.Collections.Generic;
using System;
using System.Threading;
using UnityEngine;
public class MapGenerator : MonoBehaviour
{
public enum DrawMode { NoiseMap, ColorMap, Mesh }
public DrawMode drawMode;
public 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;
Queue<MapThreadInfo<MapData>> mapDataThreadInfoQueue = new Queue<MapThreadInfo<MapData>>();
Queue<MapThreadInfo<MeshData>> meshDataThreadInfoQueue = new Queue<MapThreadInfo<MeshData>>();
public void DrawMapInEditor()
{
MapData mapData = GenerateMapData();
MapDisplay display = FindObjectOfType<MapDisplay>();
if (drawMode == DrawMode.NoiseMap)
{
display.DrawTexture(TextureGenerator.TextureFromHeightMap(mapData.heightMap));
}
else if (drawMode == DrawMode.ColorMap)
{
display.DrawTexture(TextureGenerator.TextureFromColorMap(mapData.colorMap, mapChunkSize, mapChunkSize));
}
else if (drawMode == DrawMode.Mesh)
{
display.DrawMesh(MeshGenerator.GenerateTerrainMesh(mapData.heightMap, meshHeightMultiplier, meshHeightCurve, levelOfDetail), TextureGenerator.TextureFromColorMap(mapData.colorMap, mapChunkSize, mapChunkSize));
}
}
public void RequestMapData(Action<MapData> callback)
{
ThreadStart threadStart = delegate
{
MapDataThread(callback);
};
new Thread(threadStart).Start();
}
public void MapDataThread(Action<MapData> callback)
{
MapData mapData = GenerateMapData();
lock (mapDataThreadInfoQueue)
{
mapDataThreadInfoQueue.Enqueue(new MapThreadInfo<MapData>(callback, mapData));
}
}
public void RequestMeshData(MapData mapData, Action<MeshData> callback)
{
ThreadStart threadStart = delegate
{
MeshDataThread(mapData, callback);
};
new Thread(threadStart).Start();
}
void MeshDataThread(MapData mapData, Action<MeshData> callback)
{
MeshData meshData = MeshGenerator.GenerateTerrainMesh(mapData.heightMap, meshHeightMultiplier, meshHeightCurve, levelOfDetail);
lock (meshDataThreadInfoQueue)
{
meshDataThreadInfoQueue.Enqueue(new MapThreadInfo<MeshData>(callback, meshData));
}
}
private void Update()
{
if (mapDataThreadInfoQueue.Count > 0)
{
for (int i = 0; i < mapDataThreadInfoQueue.Count; i++)
{
MapThreadInfo<MapData> threadInfo = mapDataThreadInfoQueue.Dequeue();
threadInfo.callback(threadInfo.parameter);
}
}
if (meshDataThreadInfoQueue.Count > 0)
{
for (int i = 0; i < meshDataThreadInfoQueue.Count; i++)
{
MapThreadInfo<MeshData> threadInfo = meshDataThreadInfoQueue.Dequeue();
threadInfo.callback(threadInfo.parameter);
}
}
}
MapData GenerateMapData()
{
float[,] noiseMap = noise.GenerateNoiseMap(mapChunkSize, mapChunkSize, seed, noiseScale, octaves, persistance, lacunarity, offset);
Color[] colorMap = 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)
{
colorMap[y * mapChunkSize + x] = regions[i].color;
break;
}
}
}
}
return new MapData(noiseMap, colorMap);
}
private void OnValidate()
{
if (lacunarity < 1)
{
lacunarity = 1;
}
if (octaves < 0)
{
octaves = 0;
}
}
struct MapThreadInfo<T>
{
public readonly Action<T> callback;
public readonly T parameter;
public MapThreadInfo(Action<T> callback, T parameter)
{
this.callback = callback;
this.parameter = parameter;
}
}
}
[System.Serializable]
public struct TerrainType
{
public string name;
public float height;
public Color color;
}
public struct MapData
{
public readonly float[,] heightMap;
public readonly Color[] colorMap;
public MapData(float[,] heightMap, Color[] colorMap)
{
this.heightMap = heightMap;
this.colorMap = colorMap;
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: bbb1c1467c6d331428f51b13cf715c08
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: 116bc253b2461704c891b837f6293754
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
%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: Mapmat
m_Shader: {fileID: 10752, 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: 1}
m_Offset: {x: 0, y: 0}
- _MainTex:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
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.5
- _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: 1, g: 1, b: 1, a: 1}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
m_BuildTextureStacks: []
fileFormatVersion: 2
guid: 8f14d3ec8fe83f74d9fa331924f413c0
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 2100000
userData:
assetBundleName:
assetBundleVariant:
%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: MeshMat
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: 1}
m_Offset: {x: 0, y: 0}
- _MainTex:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
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: 1, g: 1, b: 1, a: 1}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
m_BuildTextureStacks: []
fileFormatVersion: 2
guid: 789d2faff8173fe4d87d5a6e96e677db
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 2100000
userData:
assetBundleName:
assetBundleVariant:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
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;
int triangleIndex;
public Vector2[] uvs;
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;
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: eb22827e217e5254caea2cd61c70bfb8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TextureGenerator
{
public static Texture2D TextureFromColorMap(Color[] colorMap, int width, int height)
{
Texture2D texture = new Texture2D(width, height);
texture.filterMode = FilterMode.Point;
texture.wrapMode = TextureWrapMode.Clamp;
texture.SetPixels(colorMap);
texture.Apply();
return texture;
}
public static Texture2D TextureFromHeightMap(float[,] heightMap)
{
int width = heightMap.GetLength(0);
int height = heightMap.GetLength(1);
Color[] colorMap = new Color[width * height];
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
colorMap[y * width + x] = Color.Lerp(Color.black, Color.white, heightMap[x, y]);
}
}
return TextureFromColorMap(colorMap, width, height);
}
}
fileFormatVersion: 2
guid: f985f46aa93d39b46a1d8db5ed3d3fa2
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment