Make lorax

This commit is contained in:
Sofia 2020-04-20 02:19:22 +03:00
parent 2f4ac6fd62
commit 1d6af4c6cd
4 changed files with 127 additions and 0 deletions

View File

@ -9,6 +9,7 @@ GameObject:
serializedVersion: 6
m_Component:
- component: {fileID: 6166170817692975782}
- component: {fileID: 5133887194504435357}
m_Layer: 0
m_Name: Lorax
m_TagString: Untagged
@ -30,3 +31,31 @@ Transform:
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &5133887194504435357
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6166170817692975783}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 8df7bcbf7138e33448643b71967f880c, type: 3}
m_Name:
m_EditorClassIdentifier:
Terrain: {fileID: 0}
Seed: 2
Regen: 0
SpawnableTrees:
- {fileID: 3828898392337730748, guid: b6ff003e05b42fb4e9319ea8bb82d882, type: 3}
- {fileID: 6700814859666117554, guid: 410d196d969ed404a883c67749ad3170, type: 3}
- {fileID: 2469457951508719009, guid: 46e8ab3179fc53841920eb9ab6986b24, type: 3}
Chances: 02000000070000000e000000
HeightMin: 7.5
HeightMax: 9
TreeChance: 0.9
CampSize: 8
CampMiddle: {x: -1, y: 0, z: -1}
RandomNudge: 3
Denseness: 3
MinimumAdjacency: 3

87
Assets/Scripts/Lorax.cs Normal file
View File

@ -0,0 +1,87 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteAlways]
public class Lorax : MonoBehaviour {
public Terrain Terrain;
[Header("Generic Gen")]
public int Seed;
public bool Regen = false;
public List<GameObject> SpawnableTrees;
public List<int> Chances;
[Header("Generation Details")]
public float HeightMin = 7.5f;
public float HeightMax = 9;
public float TreeChance = 0.9f;
public float CampSize = 10f;
public Vector3 CampMiddle;
public float RandomNudge = 2f;
public float Denseness = 4;
public float MinimumAdjacency = 1.5f;
private int LastSeed = -1;
private Vector3[][] TreePositions;
void Start() {
}
void Update() {
if (Application.isEditor) {
if (LastSeed == Seed && !Regen) {
return;
}
Regen = false;
LastSeed = Seed;
Random.InitState(Seed);
for (int x = transform.childCount - 1; x >= 0; x--) {
DestroyImmediate(transform.GetChild(0).gameObject);
}
int cap = (int)Mathf.Floor(500 / Denseness);
int[] TotalChances = new int[SpawnableTrees.Count + 1];
int Counter = 0;
for (int x = 0; x < TotalChances.Length - 1; x++) {
TotalChances[x + 1] = Counter + Chances[x];
Counter = TotalChances[x + 1];
}
TreePositions = new Vector3[cap][];
for (int y = 0; y < cap; y++) {
TreePositions[y] = new Vector3[cap];
for (int x = 0; x < cap; x++) {
if (Random.value > TreeChance) {
continue;
}
var pos = new Vector3(x * Denseness - 250 + Random.value * RandomNudge - RandomNudge / 2, 0, y * Denseness - 250 + Random.value * RandomNudge - RandomNudge / 2);
TreePositions[y][x] = pos;
if (x > 0 && (TreePositions[y][x - 1] - pos).magnitude < MinimumAdjacency) {
continue;
}
if (y > 0 && (TreePositions[y - 1][x] - pos).magnitude < MinimumAdjacency) {
continue;
}
if ((pos - CampMiddle).magnitude < CampSize) {
continue;
}
var height = Terrain.SampleHeight(pos);
if (height >= HeightMin && height <= HeightMax) {
pos.y = Terrain.transform.position.y + height;
var rand = Random.Range(0, TotalChances[TotalChances.Length - 1]);
var Chosen = SpawnableTrees[0];
for (int curr = TotalChances.Length - 2; curr > 0; curr--) {
if (rand > TotalChances[curr]) {
Chosen = SpawnableTrees[curr];
break;
}
}
var obj = GameObject.Instantiate(Chosen, pos, new Quaternion(), gameObject.transform);
}
}
}
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8df7bcbf7138e33448643b71967f880c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

Binary file not shown.