// -------------------------------------------------------------------------------------------------------------------- // // Copyright (c) VRMADA, All rights reserved. // // -------------------------------------------------------------------------------------------------------------------- using UltimateXR.Core; using UltimateXR.Core.Components; using UltimateXR.Manipulation; using UnityEngine; namespace UltimateXR.Examples.FullScene.Lab { /// /// Allows to model light bulbs that will affect the light attached to the they are placed on. /// public class LightBulb : UxrComponent { #region Inspector Properties/Serialized Fields [SerializeField] private UxrGrabbableObject _grabbableObject; [SerializeField] private float _lightIntensity; [SerializeField] private bool _isFaulty; [SerializeField] private Color _emissiveDisabled = Color.black; [SerializeField] private Color _emissiveEnabled = Color.white; #endregion #region Public Types & Data /// /// Gets the light intensity contributed by the light bulb, which may flicker if it's faulty or be zero if it's not /// connected to the lamp. /// public float Intensity { get { if (_grabbableObject.CurrentAnchor == null) { // Not attached to anything return 0.0f; } if (_grabbableObject.CurrentAnchor.GetComponentInParent()) { // Attached to a lamp. See if it is faulty or works correctly. if (_isFaulty) { float noise = Mathf.PerlinNoise(_randX + Time.time * 20.0f, _randY * 10.0f); if (noise > 0.66f) { return _lightIntensity; } if (noise > 0.16f) { return 0.0f; } return _lightIntensity * 0.5f; } return _lightIntensity; } // Not attached to a lamp return 0.0f; } } #endregion #region Unity /// /// Initializes the component. /// protected override void Awake() { base.Awake(); Renderer renderer = GetComponent(); if (renderer) { _material = renderer.material; } _randX = Random.value; _randY = Random.value; } /// /// Updates the emissive based on the light intensity. /// private void Update() { if (_material && _grabbableObject && _lightIntensity > 0.0f) { Color emissiveColor = Color.Lerp(_emissiveDisabled, _emissiveEnabled, Intensity / _lightIntensity); _material.SetColor(UxrConstants.Shaders.EmissionColorVarName, emissiveColor); } } #endregion #region Private Types & Data private Material _material; private float _randX; private float _randY; #endregion } }