Created
December 23, 2015 17:22
-
-
Save karl-/e13ff3a6e74a3a69ba49 to your computer and use it in GitHub Desktop.
Hack to access UV3 attribute as vec4 in ShaderForge
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 UnityEngine; | |
using UnityEditor; | |
using System.Collections; | |
using System; | |
using System.IO; | |
using System.Linq; | |
using System.Text.RegularExpressions; | |
/** | |
* Provides a quick and dirty path to access a mesh.uv3 channel as Vector4 in ShaderForge | |
* To use in ShaderForge: | |
* - create a UV input set to channel 2, but do not connect to anything. | |
* - create a new vector4 property, and name it "TEMP_UV3_CHANNEL" | |
* - build your shader as normal | |
* - select the compiled shader in unity and run "Tools/ShaderForge/Replace UV3 Channel Property" | |
*/ | |
public class ReplaceUV3Property : Editor | |
{ | |
[MenuItem("Tools/ShaderForge/Replace UV3 Channel Property")] | |
static void Init() | |
{ | |
Shader shader = Selection.objects.FirstOrDefault(x => x != null && x is Shader) as Shader; | |
if(shader == null) | |
{ | |
Debug.LogWarning("No Shader Selected"); | |
return; | |
} | |
string path = AssetDatabase.GetAssetPath(shader); | |
string source = File.ReadAllText( path ); | |
// remove sf metadata | |
string[] split = source.Split(Environment.NewLine.ToCharArray()); | |
int index = Array.FindIndex(split, x => x.StartsWith("/*SF_DATA")); | |
source = string.Join(Environment.NewLine, split.Skip(index + 1).ToArray()); | |
source = Regex.Replace(source, "uniform float4 _TEMP_UV3_CHANNEL;", ""); | |
source = Regex.Replace(source, Regex.Escape("_TEMP_UV3_CHANNEL (\"TEMP_UV3_CHANNEL\", Vector) = (0,0,0,0)"), ""); | |
source = Regex.Replace(source, "_TEMP_UV3_CHANNEL", "i.uv2"); | |
source = Regex.Replace(source, "float2 uv2", "float4 uv2"); | |
source = Regex.Replace(source, "float2 texcoord2", "float4 texcoord2"); | |
string name = Regex.Match(source, "Shader\\s\".*\"\\s{").Value.Replace("Shader \"", "").Replace("\" {", ""); | |
source = Regex.Replace(source, "Shader\\s\".*\"\\s{", "Shader \"" + name + "_UV3_Vec4_Enabled\" {"); | |
string regexed = AssetDatabase.GenerateUniqueAssetPath(path); | |
File.WriteAllText(regexed, source); | |
AssetDatabase.Refresh(); | |
EditorGUIUtility.PingObject( AssetDatabase.LoadAssetAtPath<Shader>(regexed) ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment