WIP : Props placement, lighting and optimisation test
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace MonoLimboStudio
|
||||
@@ -10,47 +9,88 @@ namespace MonoLimboStudio
|
||||
public Light targetLight;
|
||||
public float minIntensity = 0.5f;
|
||||
public float maxIntensity = 1.5f;
|
||||
|
||||
[Header("Perlin Flicker (smooth)")]
|
||||
public float flickerSpeed = 5f;
|
||||
|
||||
[Header("Glitch Flicker (bursts)")]
|
||||
[Range(0f, 1f)] public float glitchChance = 0.15f; // probability of a glitch burst each frame
|
||||
public float glitchMinDuration = 0.02f;
|
||||
public float glitchMaxDuration = 0.12f;
|
||||
|
||||
[Header("Material Settings")]
|
||||
public Renderer targetRenderer;
|
||||
public string opacityProperty = "_Opacity"; // Must be a float property in your shader
|
||||
public string opacityProperty = "_Opacity";
|
||||
[Range(0f, 1f)] public float minOpacity = 0.2f;
|
||||
[Range(0f, 1f)] public float maxOpacity = 1.0f;
|
||||
|
||||
private Material materialInstance;
|
||||
private Material _materialInstance;
|
||||
private float _randomOffset;
|
||||
|
||||
void Start()
|
||||
{
|
||||
// unique offset per instance so lamps don't flicker in sync
|
||||
_randomOffset = Random.Range(0f, 1000f);
|
||||
|
||||
if (targetRenderer != null)
|
||||
{
|
||||
materialInstance = targetRenderer.material;
|
||||
_materialInstance = targetRenderer.material;
|
||||
|
||||
// Validate that the property exists
|
||||
if (!materialInstance.HasProperty(opacityProperty))
|
||||
if (!_materialInstance.HasProperty(opacityProperty))
|
||||
Debug.LogWarning($"Property '{opacityProperty}' not found on material.");
|
||||
}
|
||||
|
||||
StartCoroutine(FlickerRoutine());
|
||||
}
|
||||
|
||||
private IEnumerator FlickerRoutine()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
// --- Smooth Perlin phase ---
|
||||
float perlinValue = Mathf.PerlinNoise(Time.time * flickerSpeed + _randomOffset, 0f);
|
||||
ApplyFlicker(perlinValue);
|
||||
|
||||
// --- Random glitch burst (occasional) ---
|
||||
if (Random.value < glitchChance)
|
||||
{
|
||||
Debug.LogWarning($"Material does not have a float property named '{opacityProperty}'.");
|
||||
yield return StartCoroutine(GlitchBurst());
|
||||
}
|
||||
|
||||
yield return null; // wait for next frame
|
||||
}
|
||||
}
|
||||
|
||||
void Update()
|
||||
private IEnumerator GlitchBurst()
|
||||
{
|
||||
float flickerValue = Mathf.PerlinNoise(Time.time * flickerSpeed, 0.0f);
|
||||
float duration = Random.Range(glitchMinDuration, glitchMaxDuration);
|
||||
float elapsed = 0f;
|
||||
|
||||
// Flicker the light
|
||||
while (elapsed < duration)
|
||||
{
|
||||
// fully random intensity during the burst
|
||||
float glitchValue = Random.value;
|
||||
ApplyFlicker(glitchValue);
|
||||
|
||||
elapsed += Time.deltaTime;
|
||||
yield return null;
|
||||
}
|
||||
}
|
||||
|
||||
private void ApplyFlicker(float t)
|
||||
{
|
||||
if (targetLight != null)
|
||||
{
|
||||
float intensity = Mathf.Lerp(minIntensity, maxIntensity, flickerValue);
|
||||
targetLight.intensity = intensity;
|
||||
}
|
||||
targetLight.intensity = Mathf.Lerp(minIntensity, maxIntensity, t);
|
||||
|
||||
// Adjust material opacity float
|
||||
if (materialInstance != null && materialInstance.HasProperty(opacityProperty))
|
||||
{
|
||||
float opacity = Mathf.Lerp(minOpacity, maxOpacity, flickerValue);
|
||||
materialInstance.SetFloat(opacityProperty, opacity);
|
||||
}
|
||||
if (_materialInstance != null && _materialInstance.HasProperty(opacityProperty))
|
||||
_materialInstance.SetFloat(opacityProperty, Mathf.Lerp(minOpacity, maxOpacity, t));
|
||||
}
|
||||
|
||||
void OnDestroy()
|
||||
{
|
||||
// .material creates a new instance — destroy it to avoid memory leaks
|
||||
if (_materialInstance != null)
|
||||
Destroy(_materialInstance);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user