Last active
March 20, 2023 01:02
-
-
Save shivaduke28/932982a9bc81deef82b8d184e96a4e18 to your computer and use it in GitHub Desktop.
Simple Udon scripts for changing ligthmaps at runtime
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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