SW-Wiktor-Bombola/proj1/p1/Assets/Scripts/main.cs
2021-10-24 22:26:58 +02:00

155 lines
5.3 KiB
C#
Raw Blame History

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
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;
Vector3 lightDir = new Vector3(10.0f, 10.0f, 10.0f);
//components
GameObject mainCamera;
GameObject directionalLight;
GameObject ground;
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;
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>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>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.transform.LookAt(new Vector3(m_camLookAtX, m_camLookAtY, m_camLookAtZ), new Vector3(0.0f, 1.0f, 0.0f));
}
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);
}
}