Skip to content

Instantly share code, notes, and snippets.

@andreiagmu
Last active November 24, 2020 03:37
Show Gist options
  • Save andreiagmu/cfbc947357044ff1aa3b2689b92d1cbe to your computer and use it in GitHub Desktop.
Save andreiagmu/cfbc947357044ff1aa3b2689b92d1cbe to your computer and use it in GitHub Desktop.
RealToon Properties Overrider for Unity
// RealToon Properties Overrider for Unity
// by Andy Miira (Andrei Müller), October 2019
//
//
// [IMPORTANT]
// You must add the file RenderPipelineHelper.cs to your project, linked below:
// https://gist.github.com/andreiagmu/b862ae47ef91be05f61ae2da26627a01
//
//
// [INSTRUCTIONS]
// 0) Add this script inside an "Editor" folder in your project.
// You must create this folder if it doesn't already exist.
//
// 1) Open Unity, then click on
// "Tools -> Mirror Mirai -> RealToon Helpers -> RealToon Properties Overrider" at the menu bar.
//
// 2) Drag and drop your model's Prefab/Game Object with RealToon materials in the "Model Prefab" field.
//
// 3) You can override some of the RealToon shader properties from your model,
// by enabling the "Set <Property>" option and setting the respective override value in the field below.
//
// 4) Finally, click on the "Override" button.
// In ALL of the model's materials that use RealToon shader, the enabled property overrides will be applied.
//
// Usage example:
// https://twitter.com/andymiira/status/1189736787863838720
//
//
// Check out more Unity scripts and utilities at:
// https://gist.github.com/andreiagmu
using MirrorMirai.Helpers;
using UnityEditor;
using UnityEngine;
namespace MirrorMirai
{
public class RealToonPropertiesOverrider : ScriptableWizard
{
public GameObject modelPrefab;
// Shader properties overrides
// If enabled, these overrides will be set in ALL of the prefab's materials!
[Space] [Tooltip("If enabled, can set the RealToon shader to Default or Lite, using the value below.")]
public bool setShaderCategory;
public enum ShaderCategory
{
Default,
Lite
}
public ShaderCategory shaderCategory;
[Space] [Tooltip("If enabled, can override \"Main Color\" property with the value below.")]
public bool setMainColor;
[Tooltip("The value to override \"Main Color\" property.")]
public Color mainColorOverride = new Color(0.6886792f, 0.6886792f, 0.6886792f, 1);
[Space] [Tooltip("If enabled, can override \"Opacity\" property with the value below.")]
public bool setOpacity;
[Range(0, 1)] [Tooltip("The value to override \"Opacity\" property.")]
public float opacityOverride = 1f;
[Space] [Tooltip("If enabled, can override \"Transparent Threshold\" property with the value below.")]
public bool setTransparentThreshold;
[Tooltip("The value to override \"Transparent Threshold\" property.")]
public float transparentThresholdOverride;
[Space] [Tooltip("If enabled, can override \"Outline Width\" property with the value below.")]
public bool setOutlineWidth;
[Tooltip("The value to override \"Outline Width\" property.")]
public float outlineWidthOverride = 0.5f;
[Space] [Tooltip("If enabled, can override \"Outline Color\" property with the value below.")]
public bool setOutlineColor;
[Tooltip("The value to override \"Outline Color\" property.")]
public Color outlineColorOverride = Color.black;
[Space] [Tooltip("If enabled, can override \"Gloss Color\" property with the value below.")]
public bool setGlossColor;
[Tooltip("The value to override \"Gloss Color\" property.")]
public Color glossColorOverride = Color.white;
[Space] [Tooltip("If enabled, can override \"Gloss Texture Softness\" property with the value below.")]
public bool setGlossTexSoftness;
[Tooltip("The value to override \"Gloss Texture Softness\" property.")]
public float glossTexSoftnessOverride;
[Space] [Tooltip("If enabled, can override \"Overall Shadow Color\" property with the value below.")]
public bool setOverallShadowColor;
[Tooltip("The value to override \"Overall Shadow Color\" property.")]
public Color overallShadowColorOverride = Color.black;
[Space] [Tooltip("If enabled, can override \"Shadow Threshold\" property with the value below.")]
public bool setSelfShadowThreshold;
[Range(0, 1)] [Tooltip("The value to override \"Shadow Threshold\" property.")]
public float sSThresholdOverride = 0.85f;
[Space] [Tooltip("If enabled, can override \"Shadow Hardness\" property with the value below.")]
public bool setSelfShadowHardness;
[Range(0, 1)] [Tooltip("The value to override \"Shadow Hardness\" property.")]
public float sSHardnessOverride = 1f;
[Space]
[Tooltip("If enabled, can override \"Environmental Lighting Intensity\" property with the value below.")]
public bool setEnvLightIntensity;
[Tooltip("The value to override \"Environmental Lighting Intensity\" property.")]
public float envLightIntensityOverride = 0.65f;
[Space] [Tooltip("If enabled, can override \"Directional Light Intensity\" property with the value below.")]
public bool setDirLightIntensity;
[Tooltip("The value to override \"Directional Light Intensity\" property.")]
public float dirLightIntensityOverride;
[Space] [Tooltip("If enabled, can override \"Point and Spot Light Intensity\" property with the value below.")]
public bool setPointSpotLightIntensity;
[Tooltip("The value to override \"Point and Spot Light Intensity\" property.")]
public float pointSpotLightIntensityOverride = 0.3f;
[Space] [Tooltip("If enabled, can override \"Rim Light Unfill\" property with the value below.")]
public bool setRimLightUnfill;
[Tooltip("The value to override \"Rim Light Unfill\" property.")]
public float rimLightUnfillOverride = 1.5f;
[Space] [Tooltip("If enabled, can override \"Rim Light Color\" property with the value below.")]
public bool setRimLightColor;
[Tooltip("The value to override \"Rim Light Color\" property.")]
public Color rimLightColorOverride = Color.white;
private const string ShaderCategoryDefault = "Default";
private const string ShaderCategoryLite = "Lite";
private const string ShaderTypeDefault = "Default";
private const string ShaderTypeFadeTransparency = "Fade Transparency";
// RealToon cached property indexes
private static readonly int MainColor = Shader.PropertyToID("_MainColor");
private static readonly int Opacity = Shader.PropertyToID("_Opacity");
private static readonly int TransparentThreshold = Shader.PropertyToID("_TransparentThreshold");
private static readonly int OutlineWidth = Shader.PropertyToID("_OutlineWidth");
private static readonly int OutlineColor = Shader.PropertyToID("_OutlineColor");
private static readonly int GlossColor = Shader.PropertyToID("_GlossColor");
private static readonly int GlossTextureSoftness = Shader.PropertyToID("_GlossTextureSoftness");
private static readonly int OverallShadowColor = Shader.PropertyToID("_OverallShadowColor");
private static readonly int SelfShadowThreshold = Shader.PropertyToID("_SelfShadowThreshold");
private static readonly int SelfShadowHardness = Shader.PropertyToID("_SelfShadowHardness");
private static readonly int EnvironmentalLightingIntensity =
Shader.PropertyToID("_EnvironmentalLightingIntensity");
private static readonly int DirectionalLightIntensity = Shader.PropertyToID("_DirectionalLightIntensity");
private static readonly int PointSpotlightIntensity = Shader.PropertyToID("_PointSpotlightIntensity");
private static readonly int RimLightUnfill = Shader.PropertyToID("_RimLightUnfill");
private static readonly int RimLightColor = Shader.PropertyToID("_RimLightColor");
[MenuItem("Tools/Mirror Mirai/RealToon Helpers/RealToon Properties Overrider")]
static void CreateWizard()
{
DisplayWizard<RealToonPropertiesOverrider>("RealToon Properties Overrider", "Override");
}
void OnWizardCreate()
{
var renderers = modelPrefab.GetComponentsInChildren<Renderer>();
var currentRenderPipeline = RenderPipelineHelper.CheckRenderPipeline();
foreach (var renderer in renderers)
{
var sharedMaterials = renderer.sharedMaterials;
foreach (var mat in sharedMaterials)
{
// Set property overrides if enabled
if (setShaderCategory && currentRenderPipeline == RenderPipelines.BuiltIn)
{
var shCategory = "";
if (shaderCategory == ShaderCategory.Default)
shCategory = ShaderCategoryDefault;
else if (shaderCategory == ShaderCategory.Lite)
shCategory = ShaderCategoryLite;
var shType = "";
if (mat.shader.name.EndsWith(ShaderTypeDefault))
shType = ShaderTypeDefault;
else if (mat.shader.name.EndsWith(ShaderTypeFadeTransparency))
shType = ShaderTypeFadeTransparency;
mat.shader = Shader.Find($"RealToon/Version 5/{shCategory}/{shType}");
}
if (setMainColor)
mat.SetColor(MainColor, mainColorOverride);
if (setOpacity)
mat.SetFloat(Opacity, opacityOverride);
if (setTransparentThreshold)
mat.SetFloat(TransparentThreshold, transparentThresholdOverride);
if (setOutlineWidth)
mat.SetFloat(OutlineWidth, outlineWidthOverride);
if (setOutlineColor)
mat.SetColor(OutlineColor, outlineColorOverride);
if (setGlossColor)
mat.SetColor(GlossColor, glossColorOverride);
if (setGlossTexSoftness)
mat.SetFloat(GlossTextureSoftness, glossTexSoftnessOverride);
if (setOverallShadowColor)
mat.SetColor(OverallShadowColor, overallShadowColorOverride);
if (setSelfShadowThreshold)
mat.SetFloat(SelfShadowThreshold, sSThresholdOverride);
if (setSelfShadowHardness)
mat.SetFloat(SelfShadowHardness, sSHardnessOverride);
if (setEnvLightIntensity)
mat.SetFloat(EnvironmentalLightingIntensity, envLightIntensityOverride);
if (setDirLightIntensity)
mat.SetFloat(DirectionalLightIntensity, dirLightIntensityOverride);
if (setPointSpotLightIntensity)
mat.SetFloat(PointSpotlightIntensity, pointSpotLightIntensityOverride);
if (setRimLightUnfill)
mat.SetFloat(RimLightUnfill, rimLightUnfillOverride);
if (setRimLightColor)
mat.SetColor(RimLightColor, rimLightColorOverride);
}
}
}
}
}
@andreiagmu
Copy link
Author

@andreiagmu
Copy link
Author

Added compatibility to all main render pipelines (Built-in, URP, HDRP).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment