using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO; public class main : MonoBehaviour { //ground float m_groundWidth = 150.0f; float m_groundHeight = 120.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 = 8.0f; float m_camPosZ = 10.0f; float m_camLookAtX = 0.0f; float m_camLookAtY = -15.0f; float m_camLookAtZ = 0.0f; Vector3 lightDir = new Vector3(10.0f, 10.0f, 10.0f); //SceneVariables float PlantSpacing = 10.0f; float PlantDist = 4.0f; float PlantOffset = 20.0f; static int rowCount = 8; static int colCount = 10; //components GameObject mainCamera; GameObject directionalLight; GameObject ground; GameObject[,] trees = new GameObject[colCount,rowCount]; void Start() { //camera mainCamera = new GameObject(); mainCamera.AddComponent(); 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(); directionalLight.name = "Directional Light"; directionalLight.GetComponent().type = LightType.Directional; directionalLight.GetComponent().shadows = LightShadows.Soft; directionalLight.GetComponent().color = Color.white; directionalLight.GetComponent().useColorTemperature = true; directionalLight.GetComponent().colorTemperature = 5900; directionalLight.transform.position = new Vector3(10, 10.0f, 10.0f); directionalLight.transform.Rotate(lightDir.x, lightDir.y, lightDir.z, Space.Self); //--------------------------------------------------------------------------------------------------------------------- //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(); groundMeshRenderer.material = new Material(m_groundShader); groundMeshRenderer.material.color = m_groundColor; MeshFilter groundMeshFilter = ground.AddComponent(); Mesh groundMesh = new Mesh(); //ground.transform.Translate(new Vector3(0, 0, -20)); //dane wierzcho�k�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�k�w dla groundMesh groundMesh.vertices = vertices; //dane indeks�w dla groundMesh int[] indices = new int[6] { 0, 3, 2, 0, 2, 1 }; //bufor indeks�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�rz�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) //}; //Material mat = Resources.Load("GroundMat") as Material; //groundMeshRenderer.material = mat; //Inna wersja współrzędnych dla tekstury(small tiles) Vector2[] uv = new Vector2[4]; for (int i = 0; i < uv.Length; i++) { uv[i] = new Vector2(vertices[i].x, vertices[i].z); } Material groundMaterial = Resources.Load("GroundMat") as Material; groundMaterial.SetTextureScale("_MainTex", new Vector2(0.03f, 0.03f)); groundMeshRenderer.material = groundMaterial; //bufor wsp�rz�dnych tekstury dla groundMesh groundMesh.uv = uv; for(int i = 0; i < groundMesh.triangles.Length / 3; i++) { //int rndTriStart = Random.Range(0, groundMesh.triangles.Length/3); //pick a triangle Vector2 rndNorm = new Vector2(Random.value, Random.value); //find random point in normalized square if (rndNorm.x + rndNorm.y >= 1f) { rndNorm = new Vector2(1f - rndNorm.x, 1f - rndNorm.y); //cut the square diagonally in half by reflecting points not in the normalized triangle } Vector3 position = groundMesh.vertices[i] + (rndNorm.x * groundMesh.vertices[i + 1]) + (rndNorm.y * groundMesh.vertices[i + 2]); position.y = 0f; Vector3 normal = groundMesh.normals[i] + (rndNorm.x * groundMesh.normals[i + 1]) + (rndNorm.y * groundMesh.normals[i + 2]); GameObject go = GameObject.CreatePrimitive(PrimitiveType.Sphere); go.transform.position = position; go.transform.rotation = Quaternion.FromToRotation(Vector3.up, normal); } //Create multiple tree objects based on Width/Length spacing for (int i = 0; i < colCount; i++) { for (int j = 0; j < rowCount; j++) { trees[i, j] = GameObject.CreatePrimitive(PrimitiveType.Sphere); //TODO randomization Vector3 position = new Vector3(groundMesh.vertices[2].x - i * PlantSpacing - PlantOffset, 0, groundMesh.vertices[2].z + j * PlantDist - 50); trees[i, j].transform.position = position; } } //mesh dla GameObject "Ground" groundMeshFilter.mesh = groundMesh; //GetComponent().mesh = groundMesh; } void Update() { mainCamera.GetComponent().fieldOfView = m_FieldOfView; mainCamera.GetComponent().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)); //Update location of objects based on Spacing and Dist variables for (int i = 0; i < colCount; i++) { for (int j = 0; j < rowCount; j++) { Vector3 position = new Vector3(ground.GetComponent().mesh.vertices[2].x - i * PlantSpacing - PlantOffset, 0, ground.GetComponent().mesh.vertices[2].z + j * PlantDist - 50); trees[i, j].transform.position = position; } } } 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, 50.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, 200, 60, 20), "PlantSpacing"); PlantSpacing = GUI.HorizontalSlider(new Rect(70, 205, 150, 20), PlantSpacing, 3.0f, 15.0f); GUI.Label(new Rect(10, 230, 60, 20), "PlantDist"); PlantDist = GUI.HorizontalSlider(new Rect(70, 240, 150, 20), PlantDist, 1.0f, 20.0f); GUI.Label(new Rect(10, 260, 60, 20), "PlantOffset"); PlantOffset = GUI.HorizontalSlider(new Rect(70, 270, 150, 20), PlantOffset, 10.0f, 60.0f); } }