Skip to content

Instantly share code, notes, and snippets.

@fight4dream
Created September 30, 2022 02:39
Show Gist options
  • Save fight4dream/97f4f03d8d55a1838e86d1e15ef23d20 to your computer and use it in GitHub Desktop.
Save fight4dream/97f4f03d8d55a1838e86d1e15ef23d20 to your computer and use it in GitHub Desktop.
/*
MIT License
Copyright (c) 2022 FIGHT4DREAM LIMITED
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System;
using System.Collections.Generic;
using UnityEngine;
public class MaterialPropertyBlockSetter : MonoBehaviour
{
[Serializable]
public class ColorProperty
{
public string Name;
public Color Value;
}
[Serializable]
public class FloatProperty
{
public string Name;
public float Value;
}
[Serializable]
public class VectorProperty
{
public string Name;
public Vector4 Value;
}
[Serializable]
public class TextureProperty
{
public string Name;
public Texture Value;
}
[Serializable]
public class Matrix4x4Property
{
public string Name;
public Matrix4x4 Value;
}
public List<ColorProperty> ColorOverrides = new List<ColorProperty>();
public List<FloatProperty> FloatOverrides = new List<FloatProperty>();
public List<VectorProperty> VectorOverrides = new List<VectorProperty>();
public List<TextureProperty> TextureOverrides = new List<TextureProperty>();
public List<Matrix4x4Property> Matrix4x4Overrides = new List<Matrix4x4Property>();
public bool UseMaterialInstance = false;
private static MaterialPropertyBlock _Empty;
private static MaterialPropertyBlock Empty
{
get
{
if (_Empty == null)
{
_Empty = new MaterialPropertyBlock();
}
return _Empty;
}
}
private MaterialPropertyBlock _MaterialPropertyBlock;
private MaterialPropertyBlock MaterialPropertyBlock
{
get
{
if (_MaterialPropertyBlock == null)
{
_MaterialPropertyBlock = new MaterialPropertyBlock();
}
return _MaterialPropertyBlock;
}
}
public void UpdateMaterialPropertyBlock()
{
MaterialPropertyBlock.Clear();
foreach (var colorOverride in ColorOverrides)
{
MaterialPropertyBlock.SetColor(colorOverride.Name, colorOverride.Value);
}
foreach (var vectorOverride in VectorOverrides)
{
MaterialPropertyBlock.SetVector(vectorOverride.Name, vectorOverride.Value);
}
foreach (var floatOverride in FloatOverrides)
{
MaterialPropertyBlock.SetFloat(floatOverride.Name, floatOverride.Value);
}
foreach (var textureOverride in TextureOverrides)
{
if (textureOverride.Value)
{
MaterialPropertyBlock.SetTexture(textureOverride.Name, textureOverride.Value);
}
}
}
public void UpdateMaterialPropertyBlockAndApply()
{
UpdateMaterialPropertyBlock();
Apply();
}
public List<Renderer> Renderers = new List<Renderer>();
public string MaterialName;
public void UpdateRendererList()
{
Renderers.Clear();
var renderers = GetComponentsInChildren<Renderer>();
foreach (var renderer in renderers)
{
if (!renderer.enabled || renderer.shadowCastingMode == UnityEngine.Rendering.ShadowCastingMode.ShadowsOnly)
{
continue;
}
foreach (var material in renderer.sharedMaterials)
{
if (string.IsNullOrEmpty(MaterialName) || material.name == MaterialName)
{
Renderers.Add(renderer);
break;
}
}
}
}
public void Apply()
{
foreach (var renderer in Renderers)
{
if (!UseMaterialInstance)
{
renderer.SetPropertyBlock(MaterialPropertyBlock);
}
else
{
foreach (ColorProperty property in ColorOverrides)
{
renderer.material.SetColor(property.Name, property.Value);
}
foreach (FloatProperty property in FloatOverrides)
{
renderer.material.SetFloat(property.Name, property.Value);
}
foreach (VectorProperty property in VectorOverrides)
{
renderer.material.SetVector(property.Name, property.Value);
}
foreach (TextureProperty property in TextureOverrides)
{
renderer.material.SetTexture(property.Name, property.Value);
}
}
}
}
public void Unapply()
{
foreach (var renderer in Renderers)
{
if (!UseMaterialInstance)
{
renderer.SetPropertyBlock(Empty);
}
}
}
public void UseAsMainTexture(Texture TextureToUse)
{
var property = TextureOverrides.Find(p => p.Name == "_MainTex");
if (property == null)
{
TextureOverrides.Add(property = new TextureProperty { Name = "_MainTex" });
}
property.Value = TextureToUse;
UpdateMaterialPropertyBlockAndApply();
}
public void FlipMainTextureHorizontally()
{
var property = VectorOverrides.Find(p => p.Name == "_MainTex_ST");
if (property == null)
{
VectorOverrides.Add(property = new VectorProperty { Name = "_MainTex_ST", Value = new Vector4(1, 1, 0, 0) });
}
property.Value[0] *= -1;
property.Value[2] = 1 - property.Value[2];
UpdateMaterialPropertyBlockAndApply();
}
public bool IsMainTextureFlippedHorizontally()
{
var property = VectorOverrides.Find(p => p.Name == "_MainTex_ST");
return property != null && property.Value[0] < 0;
}
public void FlipMainTextureVertically()
{
var property = VectorOverrides.Find(p => p.Name == "_MainTex_ST");
if (property == null)
{
VectorOverrides.Add(property = new VectorProperty { Name = "_MainTex_ST", Value = new Vector4(1, 1, 0, 0) });
}
property.Value[1] *= -1;
property.Value[3] = 1 - property.Value[3];
UpdateMaterialPropertyBlockAndApply();
}
public bool IsMainTextureFlippedVertically()
{
var property = VectorOverrides.Find(p => p.Name == "_MainTex_ST");
return property != null && property.Value[1] < 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment