Shader BakeShader(Shader orig) { string path = AssetDatabase.GetAssetPath(orig); System.IO.StreamReader reader = new System.IO.StreamReader(path); string shaderSource = reader.ReadToEnd(); reader.Close(); string outfilename = AssetDatabase.GenerateUniqueAssetPath(generatedDir.stringValue + "/" + orig.name.Replace("/", "-") + ".shader"); int shaderNamePos = findAndConsumeComments("Shader", 0, shaderSource); int firstQuote = findAndConsumeComments("\"", shaderNamePos, shaderSource); int secondQuote = shaderSource.IndexOf("\"", firstQuote + 1); int brace = findAndConsumeComments("{", secondQuote + 1, shaderSource); BoxClipOrigin boxClipObj = (BoxClipOrigin)target; // (generatedDir.stringValue.GetHashCode() & 0x7fffffff) string outSource = shaderSource.Substring(0, firstQuote) + "\"" + "Hidden/BoxClipBaked/" + AssetDatabase.AssetPathToGUID(generatedDir.stringValue) + "Inst/" + orig.name.Substring(8) + "\" {\n"; outSource += "CGINCLUDE\n" + " #define BOXCLIP_CONFIGURED 1\n" + " #define BOXCLIP_SCALE " + scale.floatValue + "\n" + " #define BOXCLIP_ALLOW_IN_FRONT " + allowInFront.floatValue + "\n" + " #define BOXCLIP_ALLOW_IN_FRONT " + allowInFront.floatValue + "\n\n"; outSource += generateBoxQuadsMacro(boxClipObj, "ClipShow", BoxClipOrigin.BoxClipArray.ClipShow); outSource += generateBoxQuadsMacro(boxClipObj, "ClipHide", BoxClipOrigin.BoxClipArray.ClipHide); outSource += generateBoxQuadsMacro(boxClipObj, "ShowVolume", BoxClipOrigin.BoxClipArray.ShowVolume); outSource += generateBoxQuadsMacro(boxClipObj, "HideVolume", BoxClipOrigin.BoxClipArray.HideVolume); outSource += generateBoxQuadsMacro(boxClipObj, "ShowCameraWithin", BoxClipOrigin.BoxClipArray.ShowCameraWithin); outSource += generateBoxQuadsMacro(boxClipObj, "HideCameraWithin", BoxClipOrigin.BoxClipArray.HideCameraWithin); outSource += generateBoxQuadsMacro(boxClipObj, "ZCompress", BoxClipOrigin.BoxClipArray.ZCompress); outSource += "\nENDCG\n"; outSource += shaderSource.Substring(brace + 1); System.IO.StreamWriter writer = new System.IO.StreamWriter(outfilename, true); writer.Write(outSource); writer.Close(); string cgincpath = System.IO.Path.GetDirectoryName(path) + "/BoxClipTemplate.cginc"; string targetcgincpath = generatedDir.stringValue + "/BoxClipTemplate.cginc"; reader = new System.IO.StreamReader(cgincpath); string cgincSource = reader.ReadToEnd(); reader.Close(); writer = new System.IO.StreamWriter(targetcgincpath); writer.Write(cgincSource); writer.Close(); cgincpath = System.IO.Path.GetDirectoryName(path) + "/BoxClipStandardShadow.cginc"; targetcgincpath = generatedDir.stringValue + "/BoxClipStandardShadow.cginc"; reader = new System.IO.StreamReader(cgincpath); cgincSource = reader.ReadToEnd(); reader.Close(); writer = new System.IO.StreamWriter(targetcgincpath); writer.Write(cgincSource); writer.Close(); AssetDatabase.ImportAsset(outfilename); return(AssetDatabase.LoadAssetAtPath <Shader>(outfilename)); }
string generateBoxQuadsMacro(BoxClipOrigin boxClipObj, string arrName, BoxClipOrigin.BoxClipArray arrType) { int count; Vector4[] boxQuads = boxClipObj.createBoxQuads((Transform)origin.objectReferenceValue, arrType, out count); string outSource = " #define boxQuad_" + arrName + "_Count " + count + "\n" + " #define DECLARE_BOXCLIP_" + arrName + "_ARRAY static internalBoxQuad boxQuad_" + arrName + "_Quads[" + (count == 0 ? 1 : count) + "] = {\\\n"; for (int i = 0; i < count || i == 0; i++) { outSource += " {" + printFloat3(boxQuads[i * 4]) + "," + printFloat4(boxQuads[i * 4 + 1]) + "," + printFloat4(boxQuads[i * 4 + 2]) + "," + printFloat4(boxQuads[i * 4 + 3]) + "}" + (i == count - 1 ? "" : ",") + "\\\n"; } outSource += "};\n\n"; return(outSource); }