143 lines
No EOL
5.6 KiB
C#
143 lines
No EOL
5.6 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
namespace VRCLightVolumes
|
|
{
|
|
public class LightVolumeInstance : MonoBehaviour
|
|
{
|
|
[ColorUsage(false)]
|
|
[Tooltip("Changing the color is useful for animating Additive volumes. You can even control the R, G, B channels separately this way.")]
|
|
public UnityEngine.Color Color = UnityEngine.Color.get_white();
|
|
|
|
[Tooltip("Color multiplies by this value.")]
|
|
public float Intensity = 1f;
|
|
|
|
[Tooltip("Defines whether this volume can be moved in runtime. Disabling this option slightly improves performance. You can even change it in runtime.")]
|
|
public bool IsDynamic = false;
|
|
|
|
[Tooltip("Additive volumes apply their light on top of others as an overlay. Useful for movable lights like flashlights, projectors, disco balls, etc. They can also project light onto static lightmapped objects if the surface shader supports it.")]
|
|
public bool IsAdditive = false;
|
|
|
|
[Tooltip("Inverse rotation of the pose the volume was baked in. Automatically recalculated for dynamic volumes with auto-update, or manually via the UpdateRotation() method.")]
|
|
public Quaternion InvBakedRotation = Quaternion.get_identity();
|
|
|
|
[Space]
|
|
[Tooltip("Min bounds of Texture0 in 3D atlas space. W stores Scale X.)")]
|
|
public Vector4 BoundsUvwMin0 = new Vector4();
|
|
|
|
[Tooltip("Min bounds of Texture1 in 3D atlas space. W stores Scale Y.")]
|
|
public Vector4 BoundsUvwMin1 = new Vector4();
|
|
|
|
[Tooltip("Min bounds of Texture2 in 3D atlas space. W stores Scale Z.")]
|
|
public Vector4 BoundsUvwMin2 = new Vector4();
|
|
|
|
[Tooltip("Min bounds of occlusion texture in 3D atlas space.")]
|
|
public Vector4 BoundsUvwMinOcclusion = new Vector4();
|
|
|
|
[Space]
|
|
[Tooltip("Max bounds of Texture0 in 3D atlas space. (Legacy)")]
|
|
public Vector4 BoundsUvwMax0 = new Vector4();
|
|
|
|
[Tooltip("Max bounds of Texture1 in 3D atlas space. (Legacy)")]
|
|
public Vector4 BoundsUvwMax1 = new Vector4();
|
|
|
|
[Tooltip("Max bounds of Texture2 in 3D atlas space. (Legacy)")]
|
|
public Vector4 BoundsUvwMax2 = new Vector4();
|
|
|
|
[Space]
|
|
[Tooltip("Inversed edge smoothing in 3D atlas space. Recalculates via SetSmoothBlending(float radius) method.")]
|
|
public Vector4 InvLocalEdgeSmoothing = new Vector4();
|
|
|
|
[Tooltip("Inversed TRS matrix of this volume that transforms it into the 1x1x1 cube. Recalculates via the UpdateRotation() method.")]
|
|
public Matrix4x4 InvWorldMatrix = Matrix4x4.get_identity();
|
|
|
|
[Tooltip("Current volume's rotation relative to the rotation it was baked with. Mandatory for dynamic volumes. Recalculates via the UpdateRotation() method.")]
|
|
public Vector4 RelativeRotation = new Vector4(0f, 0f, 0f, 1f);
|
|
|
|
[Tooltip("Current volume's rotation matrix row 0 relative to the rotation it was baked with. Mandatory for dynamic volumes. Recalculates via the UpdateRotation() method. (Legacy)")]
|
|
public Vector3 RelativeRotationRow0 = Vector3.get_zero();
|
|
|
|
[Tooltip("Current volume's rotation matrix row 1 relative to the rotation it was baked with. Mandatory for dynamic volumes. Recalculates via the UpdateRotation() method. (Legacy)")]
|
|
public Vector3 RelativeRotationRow1 = Vector3.get_zero();
|
|
|
|
[Tooltip("True if there is any relative rotation. No relative rotation improves performance. Recalculated via the UpdateRotation() method.")]
|
|
public bool IsRotated = false;
|
|
|
|
[Tooltip("True if the volume has baked occlusion.")]
|
|
public bool BakeOcclusion = false;
|
|
|
|
[Tooltip("True if this Light Volume added to the Light Volumes array in LightVolumeManager. Should be always true for the Light Volumes placed in editor. Helps to initialize Light Volumes spawned in runtime.")]
|
|
public bool IsInitialized = false;
|
|
|
|
[Tooltip("Reference to the Light Volume Manager. Needed for runtime initialization.")]
|
|
public VRCLightVolumes.LightVolumeManager LightVolumeManager;
|
|
|
|
[HideInInspector]
|
|
public bool IsIterartedThrough = false;
|
|
|
|
private UnityEngine.Color _prevColor = UnityEngine.Color.get_white();
|
|
|
|
private float _prevIntensity = 1f;
|
|
|
|
public LightVolumeInstance()
|
|
{
|
|
}
|
|
|
|
public void DelayInitialize()
|
|
{
|
|
if ((this.IsInitialized ? false : this.LightVolumeManager != null))
|
|
{
|
|
this.LightVolumeManager.InitializeLightVolume(this);
|
|
}
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
if (this.LightVolumeManager != null)
|
|
{
|
|
this.LightVolumeManager.RequestUpdateVolumes();
|
|
}
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
if (this.LightVolumeManager != null)
|
|
{
|
|
this.LightVolumeManager.RequestUpdateVolumes();
|
|
}
|
|
}
|
|
|
|
public void SetSmoothBlending(float radius)
|
|
{
|
|
this.InvLocalEdgeSmoothing = base.get_transform().get_lossyScale() / Mathf.Max(radius, 1E-05f);
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
this.DelayInitialize();
|
|
if ((this._prevColor != this.Color ? true : this._prevIntensity != this.Intensity))
|
|
{
|
|
this._prevColor = this.Color;
|
|
this._prevIntensity = this.Intensity;
|
|
this.LightVolumeManager.RequestUpdateVolumes();
|
|
}
|
|
}
|
|
|
|
public void UpdateTransform()
|
|
{
|
|
Quaternion _rotation = base.get_transform().get_rotation();
|
|
Matrix4x4 matrix4x4 = Matrix4x4.TRS(base.get_transform().get_position(), _rotation, base.get_transform().get_lossyScale());
|
|
this.InvWorldMatrix = matrix4x4.get_inverse();
|
|
Quaternion invBakedRotation = _rotation * this.InvBakedRotation;
|
|
this.IsRotated = Quaternion.Dot(invBakedRotation, Quaternion.get_identity()) < 0.999999f;
|
|
Matrix4x4 matrix4x41 = Matrix4x4.Rotate(invBakedRotation);
|
|
Vector4 row = matrix4x41.GetRow(0);
|
|
row.w = 0f;
|
|
this.RelativeRotationRow0 = row;
|
|
Vector4 vector4 = matrix4x41.GetRow(1);
|
|
vector4.w = 0f;
|
|
this.RelativeRotationRow1 = vector4;
|
|
this.RelativeRotation = new Vector4(invBakedRotation.x, invBakedRotation.y, invBakedRotation.z, invBakedRotation.w);
|
|
}
|
|
}
|
|
} |