Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class ObjectPlacer : MonoBehaviour
- {
- private float maxWeight = 0;
- public float radius = 32;
- public Vector2 regionSize = new Vector2(500, 500);
- public int rejectionSamples = 30;
- public float displayRadius = 8;
- public bool generate = false;
- List<Vector2> points;
- public float defaultheight = 80;
- public float maxRayCastDistance = 100;
- public GameObject prefab;
- public Transform parant;
- [SerializeField]
- private List<GOArray> myPrefabs;
- [Header("Prefab Instances")]
- [Space(20)]
- public GameObject GameAsset_Environment_Bush_Azalea_Low;
- public GameObject GameAsset_Environment_Bush_AzaleaFlowers_Pink;
- public GameObject GameAsset_Environment_Bush_AzaleaFlowers_White;
- public GameObject GameAsset_Environment_Bush_AzaleaPatch_Flowers_Low;
- public GameObject GameAsset_Environment_Rock_Ice3;
- public GameObject GameAsset_Environment_Rock_Sandstone17;
- public GameObject GameAsset_Environment_Rock_Snowy2;
- public GameObject GameAsset_Environment_Tree_AlaskaCedar_Young1;
- public GameObject GameAsset_Environment_Tree_Blue_Spruce_Broken1;
- public GameObject GameAsset_Environment_Tree_Broadleaf6;
- public GameObject NodePosition;
- public void Update()
- {
- if (generate)
- {
- DestroyChildren();
- points = ObjectPlacement.GeneratePoints(radius, regionSize, rejectionSamples);
- PlaceObjects(points);
- generate = false;
- }
- }
- void OnDrawGizmos()
- {
- Gizmos.DrawWireCube(regionSize / 2, regionSize);
- if (points != null)
- {
- foreach (Vector2 point in points)
- {
- Gizmos.DrawSphere(point, displayRadius);
- }
- }
- }
- private void PlaceObjects(List<Vector2> points)
- {
- Vector3 testDirection = new Vector3(0, -1, 0);
- foreach (Vector2 point in points)
- {
- Vector3 testPosition = new Vector3(point.x, defaultheight, point.y);
- RaycastHit hit;
- Ray raycast = new Ray(testPosition, testDirection);
- if (Physics.Raycast(raycast, out hit, maxRayCastDistance))
- {
- Debug.Log("HIT");
- float distance = hit.distance;
- float angle = Vector3.Angle(hit.normal, testDirection);
- //Debug.Log(angle);
- testPosition.y = testPosition.y - distance;
- GameObject prefabForInstantiate = getPrefabObject();
- Instantiate(prefabForInstantiate, testPosition, Quaternion.identity, parant);
- }
- else
- {
- Debug.DrawRay(raycast.origin, raycast.direction * maxRayCastDistance, Color.white, 20);
- Debug.Log("Hit fail");
- }
- }
- }
- private GameObject getPrefabObject()
- {
- maxWeight = 0;
- foreach (GOArray tmp in myPrefabs)
- {
- maxWeight += tmp.prefabs.relativeWeight;
- }
- float currentWeight = maxWeight;
- float ranValue = UnityEngine.Random.Range(0, maxWeight);
- foreach (GOArray tmp in myPrefabs)
- {
- if (ranValue <= tmp.prefabs.relativeWeight)
- {
- return EnumToPrefab(tmp.prefabs.Prefab);
- }
- else
- {
- ranValue -= tmp.prefabs.relativeWeight;
- }
- }
- Debug.LogError("No valid ENUM/PREFAB selected");
- return EnumToPrefab(PrefabEnums.SpawnNode);
- }
- private void DestroyChildren()
- {
- foreach (Transform child in parant)
- {
- GameObject.Destroy(child.gameObject);
- }
- }
- [System.Serializable]
- public struct weightedPrefab
- {
- [SerializeField]
- public PrefabEnums Prefab;
- [SerializeField]
- [Range(0,1)]
- public float relativeWeight;
- }
- [System.Serializable]
- public class GOArray
- {
- [SerializeField]
- public weightedPrefab prefabs;
- }
- public enum PrefabEnums {
- GameAsset_Environment_Bush_Azalea_Low,
- GameAsset_Environment_Bush_AzaleaFlowers_Pink,
- GameAsset_Environment_Bush_AzaleaFlowers_White,
- GameAsset_Environment_Bush_AzaleaPatch_Flowers_Low,
- GameAsset_Environment_Rock_Ice3,
- GameAsset_Environment_Rock_Sandstone17,
- GameAsset_Environment_Rock_Snowy2,
- GameAsset_Environment_Tree_AlaskaCedar_Young1,
- GameAsset_Environment_Tree_Blue_Spruce_Broken1,
- GameAsset_Environment_Tree_Broadleaf6,
- SpawnNode }
- public void Start()
- {
- var tmp = Enum.GetValues(typeof(PrefabEnums));
- foreach(PrefabEnums tempEnum in tmp)
- {
- GOArray temp = new GOArray();
- temp.prefabs = new weightedPrefab();
- temp.prefabs.Prefab = tempEnum;
- temp.prefabs.relativeWeight = 1;
- myPrefabs.Add(temp);
- }
- Debug.Log("Finished");
- }
- private GameObject EnumToPrefab(PrefabEnums inputEnum)
- {
- switch (inputEnum)
- {
- case PrefabEnums.GameAsset_Environment_Bush_Azalea_Low:
- return GameAsset_Environment_Bush_Azalea_Low;
- case PrefabEnums.GameAsset_Environment_Bush_AzaleaFlowers_Pink:
- return GameAsset_Environment_Bush_AzaleaFlowers_Pink;
- case PrefabEnums.GameAsset_Environment_Bush_AzaleaFlowers_White:
- return GameAsset_Environment_Bush_AzaleaFlowers_White;
- case PrefabEnums.GameAsset_Environment_Bush_AzaleaPatch_Flowers_Low:
- return GameAsset_Environment_Bush_AzaleaPatch_Flowers_Low;
- case PrefabEnums.GameAsset_Environment_Rock_Ice3:
- return GameAsset_Environment_Rock_Ice3;
- case PrefabEnums.GameAsset_Environment_Rock_Sandstone17:
- return GameAsset_Environment_Rock_Sandstone17;
- case PrefabEnums.GameAsset_Environment_Rock_Snowy2:
- return GameAsset_Environment_Rock_Snowy2;
- case PrefabEnums.GameAsset_Environment_Tree_AlaskaCedar_Young1:
- return GameAsset_Environment_Tree_AlaskaCedar_Young1;
- case PrefabEnums.GameAsset_Environment_Tree_Blue_Spruce_Broken1:
- return GameAsset_Environment_Tree_Blue_Spruce_Broken1;
- case PrefabEnums.GameAsset_Environment_Tree_Broadleaf6:
- return GameAsset_Environment_Tree_Broadleaf6;
- case PrefabEnums.SpawnNode:
- return NodePosition;
- default:
- Debug.LogError("Invalid ENUM has been entered");
- return NodePosition;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement