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.

169 lines
5.3 KiB

using UnityEngine;
namespace UC
{
[ExecuteInEditMode]
[RequireComponent(typeof(Camera))]
[AddComponentMenu("Image Effects/Ray Cast/Cloud")]
public class RayCastCloud : MonoBehaviour
{
public Light sun;
[Header("Cloud Color")]
public Color sunGlareColor = new Color(1.0f, 0.4f, 0.2f);
public Color color1 = new Color(0.65f, 0.7f, 0.75f);
public Color color3 = new Color(1.0f, 0.95f, 0.8f);
public Color color4 = new Color(0.25f, 0.3f, 0.35f);
[Range(0.0f, 1.0f)]
public float sssAmount = 0.5f;
[Header("Cloud Shape")]
public bool useMultNoise = true;
[Range(-2.0f, 2.0f)]
public float cloudPosition = 0;
[Range(-2.0f, 4.0f)]
public float cloudThickness = 0;
[Range(-5f, 0.0f)]
public float cloudFreq1 = -1;
[Range(-5f, 0.0f)]
public float cloudFreq2 = -2;
[Range(-5f, 0.0f)]
public float cloudFreq3 = -3;
[Range(-5f, 0.0f)]
public float cloudFreq4 = -4;
[Range(-5f, 0.0f)]
public float cloudFreq5 = -5;
[Range(0f, 50.0f)]
public float near1 = 0;
[Range(0f, 50.0f)]
public float far1 = 50;
[Range(0f, 80.0f)]
public float near2 = 50;
public Vector3 windDirection = new Vector3(0, 0, 0);
[Header("Performance Factor")]
[Range(1, 8)]
public int textureDivideRate = 5;
[Range(60, 400)]
public int rayIteratorCount = 120;
public bool debug = false;
public bool useMipmap = true;
private enum Pass
{
//RenderCloud,
VRRenderCloud,
VRBlendBg
}
[SerializeField]
private Shader m_Shader;
public Shader shader
{
get
{
if (m_Shader == null)
m_Shader = Shader.Find("Hidden/RayCastCloud");
return m_Shader;
}
}
private Material m_Material;
public Material material
{
get
{
if (m_Material == null)
m_Material = ImageEffectHelper.CheckShaderAndCreateMaterial(shader);
return m_Material;
}
}
private void OnEnable()
{
if (!ImageEffectHelper.IsSupported(shader, true, false, this))
enabled = false;
GetComponent<Camera>().depthTextureMode = DepthTextureMode.DepthNormals;
}
private void OnDisable()
{
if (m_Material != null)
DestroyImmediate(m_Material);
m_Material = null;
}
private void OnRenderImage(RenderTexture source, RenderTexture destination)
{
float hue, s, v;
Color.RGBToHSV(sunGlareColor, out hue, out s, out v);
Color sunSSSColor = Color.HSVToRGB(hue + 0.025f, s * 0.87f, v);
sunSSSColor.a = sssAmount;
RayCastUtils.SetupMaterial(material);
material.SetColor("_SunGlareColor", sunGlareColor);
material.SetColor("_Color1", color1);
material.SetColor("_SunSSSColor", sunSSSColor);
material.SetColor("_Color3", color3);
material.SetColor("_Color4", color4);
material.SetFloat("cloud_thickness", cloudThickness);
material.SetFloat("cloud_position", cloudPosition);
material.SetFloat("cloud_freq1", Mathf.Pow(2, cloudFreq1));
material.SetFloat("cloud_freq2", Mathf.Pow(2, cloudFreq2));
material.SetFloat("cloud_freq3", Mathf.Pow(2, cloudFreq3));
material.SetFloat("cloud_freq4", Mathf.Pow(2, cloudFreq4));
material.SetFloat("cloud_freq5", Mathf.Pow(2, cloudFreq5));
material.SetFloat("near1", near1);
material.SetFloat("far1", far1);
material.SetFloat("near2", near2);
material.SetInt("rayIteratorCount", rayIteratorCount);
material.SetInt("debug", debug ? 1 : 0);
if (debug)
material.EnableKeyword ("DEBUG");
else
material.DisableKeyword ("DEBUG");
if (useMipmap)
material.EnableKeyword ("MIPMAP");
else
material.DisableKeyword ("MIPMAP");
if (useMultNoise)
material.EnableKeyword("MULTNOISE");
else
material.DisableKeyword("MULTNOISE");
if (sun != null)
{
Vector3 sun_dir = -sun.transform.forward;
material.SetVector("_SunDirection", new Vector4(sun_dir.x, sun_dir.y, sun_dir.z, 1.0f));
}
material.SetVector("winddir", windDirection);
//if(textureDivideRate != 1)
{
int w = source.width / textureDivideRate;
int h = source.height / textureDivideRate;
RenderTexture tmp1 = RenderTexture.GetTemporary(w, h, 0, source.format);
Graphics.Blit(source, tmp1, material, (int)Pass.VRRenderCloud);
material.SetTexture("_CloudTex", tmp1);
Graphics.Blit(source, destination, material, (int)Pass.VRBlendBg);
RenderTexture.ReleaseTemporary(tmp1);
}
//else
// Graphics.Blit(source, destination, material, (int)Pass.VRRenderCloud);
}
}
}