Seven is the number.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.6 KiB

4 years ago
  1. using UnityEngine;
  2. using System.Collections;
  3. public class PlanetInit : MonoBehaviour
  4. {
  5. public GameObject m_sun;
  6. public Material m_groundMaterial;
  7. public Material m_skyMaterial;
  8. public float m_hdrExposure = 0.8f;
  9. public Vector3 m_waveLength = new Vector3(0.65f,0.57f,0.475f); // Wave length of sun light
  10. public float m_ESun = 20.0f; // Sun brightness constant
  11. public float m_kr = 0.0025f; // Rayleigh scattering constant
  12. public float m_km = 0.0010f; // Mie scattering constant
  13. public float m_g = -0.990f; // The Mie phase asymmetry factor, must be between 0.999 to -0.999
  14. //Dont change these
  15. private const float m_outerScaleFactor = 1.025f; // Difference between inner and ounter radius. Must be 2.5%
  16. private float m_innerRadius; // Radius of the ground sphere
  17. private float m_outerRadius; // Radius of the sky sphere
  18. private float m_scaleDepth = 0.25f; // The scale depth (i.e. the altitude at which the atmosphere's average density is found)
  19. void Start ()
  20. {
  21. //Get the radius of the sphere. This presumes that the sphere mesh is a unit sphere (radius of 1)
  22. //that has been scaled uniformly on the x, y and z axis
  23. float radius = transform.lossyScale.x;
  24. m_innerRadius = radius;
  25. //The outer sphere must be 2.5% larger that the inner sphere
  26. m_outerRadius = m_outerScaleFactor * radius;
  27. InitMaterial(m_groundMaterial);
  28. InitMaterial(m_skyMaterial);
  29. }
  30. void Update ()
  31. {
  32. InitMaterial(m_groundMaterial);
  33. InitMaterial(m_skyMaterial);
  34. }
  35. void InitMaterial(Material mat)
  36. {
  37. Vector3 invWaveLength4 = new Vector3(1.0f / Mathf.Pow(m_waveLength.x, 4.0f), 1.0f / Mathf.Pow(m_waveLength.y, 4.0f), 1.0f / Mathf.Pow(m_waveLength.z, 4.0f));
  38. float scale = 1.0f / (m_outerRadius - m_innerRadius);
  39. mat.SetVector("v3LightPos", m_sun.transform.forward*-1.0f);
  40. mat.SetVector("v3InvWavelength", invWaveLength4);
  41. mat.SetFloat("fOuterRadius", m_outerRadius);
  42. mat.SetFloat("fOuterRadius2", m_outerRadius*m_outerRadius);
  43. mat.SetFloat("fInnerRadius", m_innerRadius);
  44. mat.SetFloat("fInnerRadius2", m_innerRadius*m_innerRadius);
  45. mat.SetFloat("fKrESun", m_kr*m_ESun);
  46. mat.SetFloat("fKmESun", m_km*m_ESun);
  47. mat.SetFloat("fKr4PI", m_kr*4.0f*Mathf.PI);
  48. mat.SetFloat("fKm4PI", m_km*4.0f*Mathf.PI);
  49. mat.SetFloat("fScale", scale);
  50. mat.SetFloat("fScaleDepth", m_scaleDepth);
  51. mat.SetFloat("fScaleOverScaleDepth", scale/m_scaleDepth);
  52. mat.SetFloat("fHdrExposure", m_hdrExposure);
  53. mat.SetFloat("g", m_g);
  54. mat.SetFloat("g2", m_g*m_g);
  55. mat.SetVector("v3LightPos", m_sun.transform.forward*-1.0f);
  56. mat.SetVector("v3Translate", transform.position);
  57. }
  58. }