SW-ramon-dyzman/Assets/Scripts/main.cs

241 lines
8.3 KiB
C#
Raw Normal View History

2021-11-04 20:57:24 +01:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System;
using UnityEngine.SceneManagement;
public class main : MonoBehaviour
{
//ground
float m_groundWidth = 10.0f;
float m_groundHeight = 10.0f;
Shader m_groundShader;
Texture m_groundTexture;
Color m_groundColor;
//camera GUI
float m_FieldOfView = 60.0f;
float m_camPosX = 0.0f;
float m_camPosY = 10.0f;
float m_camPosZ = 20.0f;
float m_camLookAtX = 0.0f;
float m_camLookAtY = 0.0f;
float m_camLookAtZ = 0.0f;
float camera_rotation = 0.1f;
float camera_r = 20f;
Vector3 lightDir = new Vector3(10.0f, 10.0f, 10.0f);
//components
GameObject mainCamera;
GameObject directionalLight;
GameObject ground;
int numOfTrees;
int numOfRows;
//prefabs
public GameObject tree1;
public GameObject tree2;
public GameObject tree3;
public GameObject tree4;
public GameObject tree5;
public System.Random random = new System.Random();
public List<GameObject> renderedTrees = new List<GameObject>();
float treesZSlider = 0.0f;
float treesXSlider = 0.0f;
GameObject selectRandomTree()
{
var list = new List<GameObject> { tree1, tree2, tree3, tree4, tree5 };
var index = UnityEngine.Random.Range(0, list.Count - 1);
return list[index];
}
void Start()
{
//camera
mainCamera = new GameObject();
mainCamera.AddComponent<Camera>();
mainCamera.transform.position = new Vector3(m_camPosX, m_camPosY, m_camPosZ);
mainCamera.transform.LookAt(new Vector3(m_camLookAtX, m_camLookAtY, m_camLookAtZ), new Vector3(0.0f, 1.0f, 0.0f));
mainCamera.name = "MainCamera";
//--------------------------------------------------------------------------------------------------------------------
//directional light
directionalLight = new GameObject();
directionalLight.AddComponent<Light>();
directionalLight.name = "Directional Light";
directionalLight.GetComponent<Light>().type = LightType.Directional;
directionalLight.GetComponent<Light>().shadows = LightShadows.Soft;
directionalLight.GetComponent<Light>().color = Color.white;
directionalLight.GetComponent<Light>().useColorTemperature = true;
directionalLight.GetComponent<Light>().colorTemperature = 5900;
directionalLight.transform.position = new Vector3(10, 10.0f, 10.0f);
directionalLight.transform.Rotate(lightDir.x, lightDir.y, lightDir.z, Space.Self);
//---------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------
// create prefabs
int positionShift_x = 500;
int positionShift_z = 500;
int treesNumber = 5;
int rowsNumber = 3;
int gridShiftX = 5;
int gridShiftZ = 5;
int randomShiftX;
int randomShiftZ;
numOfTrees = treesNumber;
numOfRows = rowsNumber;
int maxShiftValue = 5;
for (int i=0; i<treesNumber * gridShiftX; i+=gridShiftX)
{
for(int k=0; k<rowsNumber * gridShiftZ; k+=gridShiftZ)
{
randomShiftX = random.Next(0, maxShiftValue);
randomShiftZ = random.Next(0, maxShiftValue);
renderedTrees.Add(Instantiate(selectRandomTree(), new Vector3(i + positionShift_x + randomShiftX, 1, k + positionShift_z + randomShiftZ), Quaternion.identity));
}
}
//---------------------------------------------------------------------------------------------------------------------
//ground
/*
m_groundColor = new Color(1.0f, 0.4f, 0.6f, 1.0f);
m_groundShader = Shader.Find("Standard");
ground = new GameObject("Ground");
MeshRenderer groundMeshRenderer = ground.AddComponent<MeshRenderer>();
groundMeshRenderer.material = new Material(m_groundShader);
groundMeshRenderer.material.color = m_groundColor;
MeshFilter groundMeshFilter = ground.AddComponent<MeshFilter>();
Mesh groundMesh = new Mesh();
//dane wierzcho<68>k<EFBFBD>w
Vector3[] vertices = new Vector3[4]
{
new Vector3(-m_groundWidth/2.0f, -1.0f,-m_groundHeight/2.0f ),
new Vector3(m_groundWidth/2.0f, -1.0f, -m_groundHeight/2.0f),
new Vector3(m_groundWidth/2.0f,-1.0f, m_groundHeight/2.0f),
new Vector3(-m_groundWidth/2.0f, -1.0f, m_groundHeight/2.0f),
};
//bufor wierzcho<68>k<EFBFBD>w dla groundMesh
groundMesh.vertices = vertices;
//dane indeks<6B>w dla groundMesh
int[] indices = new int[6]
{
0, 3, 2,
0, 2, 1
};
//bufor indeks<6B>w
groundMesh.triangles = indices;
//dane normalnych dla groundMesh
Vector3[] normals = new Vector3[4]
{
new Vector3(0,1,0),
new Vector3(0,1,0),
new Vector3(0,1,0),
new Vector3(0,1,0),
};
//bufor normalnych dla groundMesh
groundMesh.normals = normals;
//dane wsp<73><70>rz<72>dnych tekstury dla groundMesh
Vector2[] uv = new Vector2[4]
{
new Vector2(0, 0),
new Vector2(1, 0),
new Vector2(0, 1),
new Vector2(1, 1)
};
//bufor wsp<73><70>rz<72>dnych tekstury dla groundMesh
groundMesh.uv = uv;
//mesh dla GameObject "Ground"
groundMeshFilter.mesh = groundMesh;
**/
}
void Update()
{
mainCamera.GetComponent<Camera>().fieldOfView = m_FieldOfView;
// mainCamera.GetComponent<Camera>().transform.position = new Vector3(m_camPosX,m_camPosY,m_camPosZ);
mainCamera.GetComponent<Camera>().transform.position = new Vector3(camera_r * Mathf.Cos(camera_rotation) + 500f, 10f, camera_r * Mathf.Sin(camera_rotation) + 500f);
mainCamera.GetComponent<Camera>().transform.LookAt(new Vector3(500f, 1f, 500f));
camera_rotation += 0.01f;
int index = 0;
renderedTrees.ForEach(elem => {
var currentPosition = elem.transform.position;
float shiftValueZ = treesZSlider * ((index % numOfRows) - numOfRows/2);
float shiftValueX = treesXSlider * ( index / numOfRows - numOfTrees/2);
elem.transform.position = new Vector3(currentPosition.x + shiftValueX, currentPosition.y, currentPosition.z + shiftValueZ);
index++;
});
treesZSlider = 0;
treesXSlider = 0;
//SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
void OnGUI()
{
GUI.Label(new Rect(10, 20, 60, 20), "FOV");
m_FieldOfView = GUI.HorizontalSlider(new Rect(70, 20, 150, 20), m_FieldOfView, 20.0f, 150.0f);
GUI.Label(new Rect(10, 45, 60, 20), "camPosX");
m_camPosX = GUI.HorizontalSlider(new Rect(70, 45, 150, 20), m_camPosX, -30.0f, 30.0f);
GUI.Label(new Rect(10, 70, 60, 20), "camPosY");
m_camPosY = GUI.HorizontalSlider(new Rect(70, 70, 150, 20), m_camPosY, -30.0f, 30.0f);
GUI.Label(new Rect(10, 95, 60, 20), "camPosZ");
m_camPosZ = GUI.HorizontalSlider(new Rect(70, 95, 150, 20), m_camPosZ, -30.0f, 30.0f);
GUI.Label(new Rect(10, 120, 60, 20), "LookAtX");
m_camLookAtX = GUI.HorizontalSlider(new Rect(70, 120, 150, 20), m_camLookAtX, -30.0f, 30.0f);
GUI.Label(new Rect(10, 150, 60, 20), "LookAtY");
m_camLookAtY = GUI.HorizontalSlider(new Rect(70, 145, 150, 20), m_camLookAtY, -30.0f, 30.0f);
GUI.Label(new Rect(10, 170, 60, 20), "LookAtZ");
m_camLookAtZ = GUI.HorizontalSlider(new Rect(70, 170, 150, 20), m_camLookAtZ, -30.0f, 30.0f);
GUI.Label(new Rect(10, 190, 60, 20), "CameraR");
camera_r = GUI.HorizontalSlider(new Rect(70, 190, 150, 20), camera_r, -150.0f, 150.0f);
GUI.Label(new Rect(10, 210, 60, 20), "TreesZSlider");
treesZSlider = GUI.HorizontalSlider(new Rect(70, 210, 150, 20), treesZSlider, -30.0f, 30.0f);
GUI.Label(new Rect(10, 230, 60, 20), "TreesXSlider");
treesXSlider = GUI.HorizontalSlider(new Rect(70, 230, 150, 20), treesXSlider, -30.0f, 30.0f);
}
}