2021-10-24 22:26:58 +02:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
|
|
public class main : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
//ground
|
2021-11-07 17:49:43 +01:00
|
|
|
|
float m_groundWidth = 150.0f;
|
|
|
|
|
float m_groundHeight = 120.0f;
|
2021-10-24 22:26:58 +02:00
|
|
|
|
Shader m_groundShader;
|
|
|
|
|
Texture m_groundTexture;
|
|
|
|
|
Color m_groundColor;
|
|
|
|
|
|
|
|
|
|
//camera GUI
|
|
|
|
|
float m_FieldOfView = 60.0f;
|
|
|
|
|
float m_camPosX = 0.0f;
|
2021-11-07 17:49:43 +01:00
|
|
|
|
float m_camPosY = 8.0f;
|
|
|
|
|
float m_camPosZ = 10.0f;
|
2021-10-24 22:26:58 +02:00
|
|
|
|
float m_camLookAtX = 0.0f;
|
2021-11-07 17:49:43 +01:00
|
|
|
|
float m_camLookAtY = -15.0f;
|
2021-10-24 22:26:58 +02:00
|
|
|
|
float m_camLookAtZ = 0.0f;
|
|
|
|
|
|
|
|
|
|
Vector3 lightDir = new Vector3(10.0f, 10.0f, 10.0f);
|
|
|
|
|
|
2021-11-07 17:49:43 +01:00
|
|
|
|
//SceneVariables
|
|
|
|
|
float PlantSpacing = 10.0f;
|
|
|
|
|
float PlantDist = 4.0f;
|
|
|
|
|
float PlantOffset = 20.0f;
|
|
|
|
|
static int rowCount = 8;
|
|
|
|
|
static int colCount = 10;
|
|
|
|
|
|
2021-10-24 22:26:58 +02:00
|
|
|
|
//components
|
|
|
|
|
GameObject mainCamera;
|
|
|
|
|
GameObject directionalLight;
|
|
|
|
|
GameObject ground;
|
2021-11-07 17:49:43 +01:00
|
|
|
|
GameObject[,] trees = new GameObject[colCount,rowCount];
|
|
|
|
|
|
2021-10-24 22:26:58 +02:00
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
//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;
|
2021-11-07 17:49:43 +01:00
|
|
|
|
|
2021-10-24 22:26:58 +02:00
|
|
|
|
MeshFilter groundMeshFilter = ground.AddComponent<MeshFilter>();
|
|
|
|
|
|
|
|
|
|
Mesh groundMesh = new Mesh();
|
2021-11-07 17:49:43 +01:00
|
|
|
|
//ground.transform.Translate(new Vector3(0, 0, -20));
|
|
|
|
|
|
2021-10-24 22:26:58 +02:00
|
|
|
|
//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>rz<72>dnych tekstury dla groundMesh
|
2021-11-07 17:49:43 +01:00
|
|
|
|
//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++)
|
2021-10-24 22:26:58 +02:00
|
|
|
|
{
|
2021-11-07 17:49:43 +01:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-10-24 22:26:58 +02:00
|
|
|
|
|
|
|
|
|
//bufor wsp<73>rz<72>dnych tekstury dla groundMesh
|
|
|
|
|
groundMesh.uv = uv;
|
|
|
|
|
|
2021-10-24 23:42:03 +02:00
|
|
|
|
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);
|
2021-11-07 17:49:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-24 23:42:03 +02:00
|
|
|
|
|
2021-10-24 22:26:58 +02:00
|
|
|
|
//mesh dla GameObject "Ground"
|
|
|
|
|
groundMeshFilter.mesh = groundMesh;
|
2021-11-07 17:49:43 +01:00
|
|
|
|
//GetComponent<MeshFilter>().mesh = groundMesh;
|
2021-10-24 22:26:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Update()
|
|
|
|
|
{
|
|
|
|
|
mainCamera.GetComponent<Camera>().fieldOfView = m_FieldOfView;
|
|
|
|
|
mainCamera.GetComponent<Camera>().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));
|
2021-11-07 17:49:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//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<MeshFilter>().mesh.vertices[2].x - i * PlantSpacing - PlantOffset, 0, ground.GetComponent<MeshFilter>().mesh.vertices[2].z + j * PlantDist - 50);
|
|
|
|
|
trees[i, j].transform.position = position;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-24 22:26:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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");
|
2021-11-07 17:49:43 +01:00
|
|
|
|
m_camPosZ = GUI.HorizontalSlider(new Rect(70, 95, 150, 20), m_camPosZ, -30.0f, 50.0f);
|
2021-10-24 22:26:58 +02:00
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
2021-11-07 17:49:43 +01:00
|
|
|
|
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);
|
2021-10-24 22:26:58 +02:00
|
|
|
|
|
2021-11-07 17:49:43 +01:00
|
|
|
|
GUI.Label(new Rect(10, 260, 60, 20), "PlantOffset");
|
|
|
|
|
PlantOffset = GUI.HorizontalSlider(new Rect(70, 270, 150, 20), PlantOffset, 10.0f, 60.0f);
|
2021-10-24 22:26:58 +02:00
|
|
|
|
}
|
2021-11-07 17:49:43 +01:00
|
|
|
|
|
2021-10-24 22:26:58 +02:00
|
|
|
|
}
|