Skip to content

Instantly share code, notes, and snippets.

@shivaduke28
Last active March 20, 2023 01:02
Show Gist options
  • Save shivaduke28/932982a9bc81deef82b8d184e96a4e18 to your computer and use it in GitHub Desktop.
Save shivaduke28/932982a9bc81deef82b8d184e96a4e18 to your computer and use it in GitHub Desktop.
Simple Udon scripts for changing ligthmaps at runtime
using UdonSharp;
using UnityEngine;
namespace TKMNY
{
[UdonBehaviourSyncMode(BehaviourSyncMode.NoVariableSync)]
public sealed class LightMapHolder : UdonSharpBehaviour
{
[SerializeField] Texture[] lightmaps;
Renderer[] renderers;
MaterialPropertyBlock block;
void Start()
{
block = new MaterialPropertyBlock();
}
public void Initialize(LightMapManager lightMapManager)
{
renderers = lightMapManager.GetRenderers();
}
public void Apply()
{
foreach (var renderer in renderers)
{
var index = renderer.lightmapIndex;
if (index < 0) continue;
renderer.GetPropertyBlock(block);
block.SetTexture("unity_Lightmap", lightmaps[index]);
renderer.SetPropertyBlock(block);
}
}
}
}
using UdonSharp;
using UnityEngine;
using VRC.SDKBase;
namespace TKMNY
{
[UdonBehaviourSyncMode(BehaviourSyncMode.Manual)]
public sealed class LightMapManager : UdonSharpBehaviour
{
[SerializeField] LightMapHolder[] lightMapHolders;
[SerializeField] LightMapTrigger[] lightMapTriggers;
[SerializeField] Renderer[] renderers;
[SerializeField, UdonSynced, FieldChangeCallback(nameof(SyncedIndex))]
int syncedIndex;
public Renderer[] GetRenderers()
{
return renderers;
}
public int SyncedIndex
{
get => syncedIndex;
set
{
UpdateLightMaps(value);
syncedIndex = value;
}
}
void Start()
{
foreach (var lightMapHolder in lightMapHolders)
{
lightMapHolder.Initialize(this);
}
for (var i = 0; i < lightMapTriggers.Length; i++)
{
lightMapTriggers[i].Initialize(this, i);
}
UpdateLightMaps(syncedIndex);
}
public void SetIndex(int i)
{
Networking.SetOwner(Networking.LocalPlayer, gameObject);
SyncedIndex = i;
RequestSerialization();
}
void UpdateLightMaps(int index)
{
for (var i = 0; i < lightMapHolders.Length; i++)
{
if (i == index)
{
lightMapHolders[i].Apply();
break;
}
}
}
}
}
using UdonSharp;
namespace TKMNY
{
[UdonBehaviourSyncMode(BehaviourSyncMode.NoVariableSync)]
public sealed class LightMapTrigger : UdonSharpBehaviour
{
LightMapManager lightMapManager;
int index;
public void Initialize(LightMapManager lightMapManager, int index)
{
this.lightMapManager = lightMapManager;
this.index = index;
}
public override void Interact()
{
lightMapManager.SetIndex(index);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment