Skip to content
Snippets Groups Projects
Commit a74b3604 authored by El Ghoul Khalil's avatar El Ghoul Khalil
Browse files

terrain generation and terrain map switcher

have to imrove some parts but i work thanks god :ok_hand::blush:
parent bfe5c009
No related branches found
No related tags found
No related merge requests found
Showing
with 358 additions and 5179 deletions
......@@ -216,12 +216,9 @@ GameObject:
serializedVersion: 6
m_Component:
- component: {fileID: 1390321174}
- component: {fileID: 1390321173}
- component: {fileID: 1390321172}
- component: {fileID: 1390321171}
- component: {fileID: 1390321170}
m_Layer: 0
m_Name: perlin
m_Name: mapsController
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
......@@ -265,76 +262,57 @@ MonoBehaviour:
- {fileID: 3252406161960408255, guid: 0374c381b4c86d3428a2296c447aa847, type: 3}
- {fileID: 7829466307646916961, guid: 5c5454eaed843a44ab4e5565ffa0d3e0, type: 3}
- {fileID: 5965391858534874730, guid: 718497b3bc2d78d469803161dce68ae6, type: 3}
width: 64
height: 64
scale: 1
chunckvalue:
- 0.04
- 0.08
- 0.12
- 0.16
- 0.2
- 0.24
- 0.28
- 0.32
- 0.36
- 0.4
- 0.44
- 0.48
- 0.52
- 0.56
- 0.6
- 0.64
- 0.68
- 0.72
- 0.76
- 0.8
- 0.84
- 0.88
- 0.92
- 0.96
- 1
width: 10
chunckW: 20
chunckH: 20
scale: 10
xOrigin: 0
yOrigin: 0
texture: {fileID: 2800000, guid: 551cd4cfe330cda40a5b0206a2a143a2, type: 3}
--- !u!64 &1390321171
MeshCollider:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1390321169}
m_Material: {fileID: 0}
m_IsTrigger: 0
m_Enabled: 1
serializedVersion: 4
m_Convex: 0
m_CookingOptions: 30
m_Mesh: {fileID: 10209, guid: 0000000000000000e000000000000000, type: 0}
--- !u!23 &1390321172
MeshRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1390321169}
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_RenderingLayerMask: 1
m_RendererPriority: 0
m_Materials:
- {fileID: 2100000, guid: d2a27993a2f1ecb48a215b7faa567e15, 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 &1390321173
MeshFilter:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1390321169}
m_Mesh: {fileID: 10209, guid: 0000000000000000e000000000000000, type: 0}
texture: {fileID: 0}
maps:
- id: 0
name: egypt
prefab: {fileID: 4674599255680578714, guid: ef6f9e2b653dbc649865abace28c4647, type: 3}
Instantiate: 1
RandomTerrainData: 0
- id: 1
name: psudoRandom
prefab: {fileID: 2691398543946263012, guid: 66cd31752a10caf4c8dcc1eadb916d55, type: 3}
Instantiate: 0
RandomTerrainData: 0
- id: 2
name: random
prefab: {fileID: 4194594803482203017, guid: 424f23b9eb8222b45b4bc86b0d88b005, type: 3}
Instantiate: 0
RandomTerrainData: 1
mapId: 3
mapChoice:
--- !u!4 &1390321174
Transform:
m_ObjectHideFlags: 0
......
This diff is collapsed.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[Serializable]
public class Maps
{
public int id;
public string name;
public GameObject prefab;
public bool Instantiate;
public bool RandomTerrainData;
public Maps(int id, string name,GameObject prefab)
{
this.id = id;
this.name = name;
this.prefab = prefab;
}
}
fileFormatVersion: 2
guid: 993b96ce22fa17c4db07ac6e3cbfc414
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TerrainGenerator : MonoBehaviour
{
public GameObject[] chuncks;
[SerializeField] int width, height;
[SerializeField] float scale,xOrigin, yOrigin;
[SerializeField] private Texture2D texture;
private PerlinNoiseGen perlin;
private Renderer rend;
void Start()
{
rend = transform.GetComponent<Renderer>();
texture = new Texture2D(width, height);
rend.material.mainTexture = texture;
perlin = new PerlinNoiseGen(width,height,scale);
int i = 0, offset = 20;
foreach(GameObject terrain in chuncks)
{
Instantiate(terrain, new Vector3(i, 0f, 0f), Quaternion.identity);
i += offset;
}
}
// Update is called once per frame
void Update()
{
perlin.updateTexture(xOrigin, yOrigin, scale);
texture = perlin.generate2DTexture();
rend.material.mainTexture = texture;
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TerrainGeneratorPaintBall : MonoBehaviour
{
public GameObject[] chuncks;
public float[] chunckvalue;
[SerializeField] int width,chunckW,chunckH;
[SerializeField] float scale,xOrigin, yOrigin;
[SerializeField] private Texture2D texture;
[SerializeField] Maps[] maps;
[SerializeField] int mapId = 0;
[SerializeField] string mapChoice = "None";
private GameObject mapInUse;
private PerlinNoiseGen perlin;
void Start()
{
xOrigin = UnityEngine.Random.Range(0f, 25f);
yOrigin = UnityEngine.Random.Range(0f, 25f);
scale = UnityEngine.Random.Range(8f, 15f);
texture = new Texture2D(width, width);
perlin = new PerlinNoiseGen(width, width, scale);
perlin.updateTexture(xOrigin, yOrigin, scale);
texture = perlin.generate2DTexture();
}
int oldID = 0;
void Update()
{
switchMaps();
}
void generateTerrain(GameObject parent)
{
chunckvalue = new float[chuncks.Length];
int index = 0;
foreach(GameObject chunck in chuncks)
{
chunckvalue[index] = int.Parse(chunck.name) / ((float)chuncks.Length);
index++;
}
perlin.updateTexture(xOrigin, yOrigin, scale);
for (int x = 0; x< width; x++)
{
for (int y = 0; y < width; y++)
{
texture = perlin.generate2DTexture();
double grayScale = texture.GetPixel(x, y).grayscale;
for(int i = 0; i< chunckvalue.Length; i++)
{
if(grayScale< chunckvalue[i])
{
Instantiate(chuncks[i], new Vector3(x * chunckW, 0, y * chunckH), Quaternion.identity, parent.transform);
break;
}
}
}
}
}
void switchMaps()
{
if (mapId > maps.Length)
{
mapId = maps.Length;
}
else if (mapId < 1)
{
mapId = 1;
}
if (mapId != oldID)
{
if (mapInUse != null)
{
Destroy(mapInUse);
}
oldID = mapId;
Maps map = maps[mapId - 1];
mapChoice = map.name;
if (map.Instantiate)
{
mapInUse = Instantiate(map.prefab, new Vector3(0, 0, 0), Quaternion.identity,transform);
}
else
{
if (!map.RandomTerrainData && !map.Instantiate)
{
xOrigin = UnityEngine.Random.Range(0f, 25f);
yOrigin = UnityEngine.Random.Range(0f, 25f);
scale = UnityEngine.Random.Range(8f, 15f);
GameObject parent = Instantiate(map.prefab, new Vector3(0, 0, 0), Quaternion.identity,transform);
generateTerrain(parent);
mapInUse = parent;
}
else if (map.RandomTerrainData)
{
mapInUse = Instantiate(map.prefab, new Vector3(0, 0, 0), Quaternion.identity, transform);
Terrain teraain = mapInUse.GetComponent<Terrain>();
teraain = genTerrainRandom(map);
}
}
}
}
Terrain genTerrainRandom(Maps map)
{
Terrain terrain = map.prefab.GetComponent<Terrain>();
if(terrain == null)
{
terrain = new Terrain();
}
terrain.terrainData.size = new Vector3(chunckW * width, 20, chunckW * width);
terrain.terrainData.heightmapResolution = (int)terrain.terrainData.size.x + 1;
terrain.terrainData.SetHeights(0, 0, generateHeight());
return terrain;
}
float[,] generateHeight()
{
int size = chunckW * width;
float[,] heights = new float[size, size];
for(int x = 0; x < size; x++)
{
for (int y = 0; y< size; y++)
{
heights[x, y] = calculateHeight(x, y);
}
}
return heights;
}
float calculateHeight(int x, int y)
{
int size = chunckW * width;
float xCoord = (float)x / size * scale;
float yCoord = (float)y / size * scale;
return Mathf.PerlinNoise(xCoord, yCoord);
}
}
fileFormatVersion: 2
guid: 15da2ea272da8c74a82811c74f0bd0fc
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &4194594803482203017
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 4194594803482203014}
- component: {fileID: 4194594803482203015}
- component: {fileID: 4194594803482203016}
m_Layer: 0
m_Name: random
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 2147483647
m_IsActive: 1
--- !u!4 &4194594803482203014
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4194594803482203017}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!218 &4194594803482203015
Terrain:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4194594803482203017}
m_Enabled: 1
serializedVersion: 6
m_TerrainData: {fileID: 15600000, guid: 633d16acb17e6c44ea8a43b0cef66fc2, type: 2}
m_TreeDistance: 5000
m_TreeBillboardDistance: 50
m_TreeCrossFadeLength: 5
m_TreeMaximumFullLODCount: 50
m_DetailObjectDistance: 80
m_DetailObjectDensity: 1
m_HeightmapPixelError: 5
m_SplatMapDistance: 1000
m_HeightmapMaximumLOD: 0
m_ShadowCastingMode: 2
m_DrawHeightmap: 1
m_DrawInstanced: 0
m_DrawTreesAndFoliage: 1
m_StaticShadowCaster: 0
m_ReflectionProbeUsage: 1
m_MaterialTemplate: {fileID: 2100000, guid: 594ea882c5a793440b60ff72d896021e, type: 2}
m_BakeLightProbesForTrees: 1
m_PreserveTreePrototypeLayers: 0
m_DeringLightProbesForTrees: 1
m_ScaleInLightmap: 0.0256
m_LightmapParameters: {fileID: 15203, guid: 0000000000000000f000000000000000, type: 0}
m_GroupingID: 0
m_RenderingLayerMask: 1
m_AllowAutoConnect: 1
--- !u!154 &4194594803482203016
TerrainCollider:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4194594803482203017}
m_Material: {fileID: 0}
m_Enabled: 1
m_TerrainData: {fileID: 15600000, guid: 633d16acb17e6c44ea8a43b0cef66fc2, type: 2}
m_EnableTreeColliders: 1
fileFormatVersion: 2
guid: 424f23b9eb8222b45b4bc86b0d88b005
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
No preview for this file type
File added
fileFormatVersion: 2
guid: 633d16acb17e6c44ea8a43b0cef66fc2
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 15600000
userData:
assetBundleName:
assetBundleVariant:
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment