Modify original Fire_Shader graph with custom toon function for VFX Graph
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Has been cancelled

- Изменен исходный файл Fire_Shader.shadergraph для поддержки в VFX Graph.
- Внедрена Custom Function нода с кодом трехступенчатого мультяшного закрашивания на основе цвета частиц.
- Удалены временные шейдеры и скрипты.
This commit is contained in:
Данил Ярош 2026-08-06 14:02:54 +03:00
parent 150f84decd
commit 8b41d48b7f
8 changed files with 4600 additions and 4342 deletions

View File

@ -1,42 +0,0 @@
using UnityEngine;
using UnityEditor;
public static class ApplyCartoonFireShader
{
[MenuItem("Tools/Apply Cartoon Fire Shader")]
public static void Apply()
{
string materialPath = "Assets/Materials/Fire_Material.mat";
Material mat = AssetDatabase.LoadAssetAtPath<Material>(materialPath);
if (mat == null)
{
Debug.LogError($"Material not found at {materialPath}");
return;
}
Shader cartoonShader = Shader.Find("Custom/CartoonFire");
if (cartoonShader == null)
{
Debug.LogError("Shader Custom/CartoonFire not found! Make sure it compiled successfully.");
return;
}
Undo.RecordObject(mat, "Apply Cartoon Fire Shader");
mat.shader = cartoonShader;
// Set properties
mat.SetColor("_ColorOuter", new Color(1.5f, 0.2f, 0.1f, 1.0f));
mat.SetColor("_ColorMid", new Color(1.8f, 0.55f, 0.0f, 1.0f));
mat.SetColor("_ColorInner", new Color(2.2f, 0.95f, 0.15f, 1.0f));
mat.SetVector("_Speed", new Vector4(0.0f, -1.8f, 0.0f, 0.0f));
mat.SetFloat("_DistortStrength", 0.08f);
mat.SetFloat("_ThresholdOuter", 0.15f);
mat.SetFloat("_ThresholdMid", 0.38f);
mat.SetFloat("_ThresholdInner", 0.58f);
mat.SetFloat("_NoiseScale", 15.0f);
EditorUtility.SetDirty(mat);
AssetDatabase.SaveAssets();
Debug.Log("Successfully applied CartoonFire shader to Fire_Material.mat and set default values!");
}
}

View File

@ -1,2 +0,0 @@
fileFormatVersion: 2
guid: ccc774ec6a36cbd41af560b342b39471

View File

@ -0,0 +1,326 @@
using UnityEngine;
using UnityEditor;
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public static class InjectToonFireToShaderGraph
{
[MenuItem("Tools/Inject Toon Fire to Shader Graph")]
public static void Inject()
{
string path = "Assets/Shaders/Fire_Shader.shadergraph";
if (!File.Exists(path))
{
Debug.LogError($"ShaderGraph not found at {path}");
return;
}
string jsonText = File.ReadAllText(path);
List<JObject> jObjects = new List<JObject>();
// Parse Multi-JSON using SupportMultipleContent = true
using (var stringReader = new StringReader(jsonText))
using (var jsonReader = new JsonTextReader(stringReader))
{
jsonReader.SupportMultipleContent = true;
while (jsonReader.Read())
{
if (jsonReader.TokenType == JsonToken.StartObject)
{
JObject obj = JObject.Load(jsonReader);
jObjects.Add(obj);
}
}
}
// Find the main GraphData object (usually the first one, but let's be safe)
JObject graphData = null;
foreach (var obj in jObjects)
{
if ((string)obj["m_Type"] == "UnityEditor.ShaderGraph.GraphData")
{
graphData = obj;
break;
}
}
if (graphData == null)
{
Debug.LogError("Could not find GraphData object in ShaderGraph file!");
return;
}
// 1. Remove connections to BaseColor and Alpha block nodes in m_Edges
// BaseColor block id: "5795a95f005b46bbbbf4c4e22a0ae9c3"
// Alpha block id: "6c0a4ae63e3c48ad9e08ffbc4cf86bba"
JArray edges = (JArray)graphData["m_Edges"];
List<JToken> edgesToRemove = new List<JToken>();
foreach (var edge in edges)
{
string inputNodeId = (string)edge["m_InputSlot"]["m_Node"]["m_Id"];
if (inputNodeId == "5795a95f005b46bbbbf4c4e22a0ae9c3" || inputNodeId == "6c0a4ae63e3c48ad9e08ffbc4cf86bba")
{
edgesToRemove.Add(edge);
}
}
foreach (var edge in edgesToRemove)
{
edges.Remove(edge);
}
// 2. Define our Custom Function node and Slot JObjects
JObject slotInRawValue = JObject.Parse(@"{
""m_SGVersion"": 0,
""m_Type"": ""UnityEditor.ShaderGraph.Vector1MaterialSlot"",
""m_ObjectId"": ""slot_in_raw_value"",
""m_Id"": 0,
""m_DisplayName"": ""rawValue"",
""m_SlotType"": 0,
""m_Hidden"": false,
""m_ShaderOutputName"": ""rawValue"",
""m_StageCapability"": 3,
""m_Value"": 0.0,
""m_DefaultValue"": 0.0,
""m_Labels"": []
}");
JObject slotInBaseColor = JObject.Parse(@"{
""m_SGVersion"": 0,
""m_Type"": ""UnityEditor.ShaderGraph.Vector4MaterialSlot"",
""m_ObjectId"": ""slot_in_base_color"",
""m_Id"": 1,
""m_DisplayName"": ""baseColor"",
""m_SlotType"": 0,
""m_Hidden"": false,
""m_ShaderOutputName"": ""baseColor"",
""m_StageCapability"": 3,
""m_Value"": {
""x"": 1.0,
""y"": 1.0,
""z"": 1.0,
""w"": 1.0
},
""m_DefaultValue"": {
""x"": 0.0,
""y"": 0.0,
""z"": 0.0,
""w"": 0.0
},
""m_Labels"": []
}");
JObject slotOutColor = JObject.Parse(@"{
""m_SGVersion"": 0,
""m_Type"": ""UnityEditor.ShaderGraph.Vector3MaterialSlot"",
""m_ObjectId"": ""slot_out_color"",
""m_Id"": 2,
""m_DisplayName"": ""outColor"",
""m_SlotType"": 1,
""m_Hidden"": false,
""m_ShaderOutputName"": ""outColor"",
""m_StageCapability"": 3,
""m_Value"": {
""x"": 0.0,
""y"": 0.0,
""z"": 0.0
},
""m_DefaultValue"": {
""x"": 0.0,
""y"": 0.0,
""z"": 0.0
},
""m_Labels"": []
}");
JObject slotOutAlpha = JObject.Parse(@"{
""m_SGVersion"": 0,
""m_Type"": ""UnityEditor.ShaderGraph.Vector1MaterialSlot"",
""m_ObjectId"": ""slot_out_alpha"",
""m_Id"": 3,
""m_DisplayName"": ""outAlpha"",
""m_SlotType"": 1,
""m_Hidden"": false,
""m_ShaderOutputName"": ""outAlpha"",
""m_StageCapability"": 3,
""m_Value"": 0.0,
""m_DefaultValue"": 0.0,
""m_Labels"": []
}");
JObject customFunctionNode = JObject.Parse(@"{
""m_SGVersion"": 1,
""m_Type"": ""UnityEditor.ShaderGraph.CustomFunctionNode"",
""m_ObjectId"": ""node_cartoon_fire_custom"",
""m_Group"": {
""m_Id"": """"
},
""m_Name"": ""ToonFireColor (Custom Function)"",
""m_DrawState"": {
""m_Expanded"": true,
""m_Position"": {
""serializedVersion"": ""2"",
""x"": -400.0,
""y"": 56.0,
""width"": 220.0,
""height"": 180.0
}
},
""m_Slots"": [
{ ""m_Id"": ""slot_in_raw_value"" },
{ ""m_Id"": ""slot_in_base_color"" },
{ ""m_Id"": ""slot_out_color"" },
{ ""m_Id"": ""slot_out_alpha"" }
],
""synonyms"": [],
""m_Precision"": 0,
""m_PreviewExpanded"": false,
""m_DismissedVersion"": 0,
""m_PreviewMode"": 0,
""m_CustomColors"": {
""m_SerializableColors"": []
},
""m_SourceType"": 1,
""m_FunctionName"": ""ToonFireColor"",
""m_FunctionSource"": """",
""m_FunctionBody"": ""float3 colInner = float3(1.0, 0.95, 0.4) * baseColor.rgb * 2.2;\nfloat3 colMid = float3(1.0, 0.55, 0.0) * baseColor.rgb * 1.8;\nfloat3 colOuter = float3(1.0, 0.15, 0.1) * baseColor.rgb * 1.4;\n\nfloat stepOuter = step(0.15, rawValue);\nfloat stepMid = step(0.38, rawValue);\nfloat stepInner = step(0.58, rawValue);\n\nfloat3 finalColor = float3(0,0,0);\nif (stepInner > 0.5)\n{\n finalColor = colInner;\n}\nelse if (stepMid > 0.5)\n{\n finalColor = colMid;\n}\nelse if (stepOuter > 0.5)\n{\n finalColor = colOuter;\n}\n\noutColor = finalColor;\noutAlpha = stepOuter * baseColor.a;""
}");
// Remove previous instances of our nodes from list (if re-running script)
jObjects.RemoveAll(x => (string)x["m_ObjectId"] == "slot_in_raw_value");
jObjects.RemoveAll(x => (string)x["m_ObjectId"] == "slot_in_base_color");
jObjects.RemoveAll(x => (string)x["m_ObjectId"] == "slot_out_color");
jObjects.RemoveAll(x => (string)x["m_ObjectId"] == "slot_out_alpha");
jObjects.RemoveAll(x => (string)x["m_ObjectId"] == "node_cartoon_fire_custom");
// Add them to the list of objects in the file
jObjects.Add(slotInRawValue);
jObjects.Add(slotInBaseColor);
jObjects.Add(slotOutColor);
jObjects.Add(slotOutAlpha);
jObjects.Add(customFunctionNode);
// Add the node reference ID to "m_Nodes" array in GraphData
JArray nodes = (JArray)graphData["m_Nodes"];
bool hasNode = false;
foreach (var node in nodes)
{
if ((string)node["m_Id"] == "node_cartoon_fire_custom")
{
hasNode = true;
break;
}
}
if (!hasNode)
{
JObject nodeRef = new JObject();
nodeRef["m_Id"] = "node_cartoon_fire_custom";
nodes.Add(nodeRef);
}
// 3. Connect the new edges in GraphData
// Edge 1: raw fire shape (MultiplyNode 0d74e8c23ef1428eaf8c38a4c033c563 output slot 2) -> slot_in_raw_value (CustomFunctionNode input slot 0)
JObject edge1 = JObject.Parse(@"{
""m_OutputSlot"": {
""m_Node"": {
""m_Id"": ""0d74e8c23ef1428eaf8c38a4c033c563""
},
""m_SlotId"": 2
},
""m_InputSlot"": {
""m_Node"": {
""m_Id"": ""node_cartoon_fire_custom""
},
""m_SlotId"": 0
}
}");
// Edge 2: base color representation (MultiplyNode e48170ea86fb4291b3e4198160139b3e output slot 2) -> slot_in_base_color (CustomFunctionNode input slot 1)
JObject edge2 = JObject.Parse(@"{
""m_OutputSlot"": {
""m_Node"": {
""m_Id"": ""e48170ea86fb4291b3e4198160139b3e""
},
""m_SlotId"": 2
},
""m_InputSlot"": {
""m_Node"": {
""m_Id"": ""node_cartoon_fire_custom""
},
""m_SlotId"": 1
}
}");
// Edge 3: CustomFunctionNode outColor (output slot 2) -> BaseColor block node 5795a95f005b46bbbbf4c4e22a0ae9c3 (slot 0)
JObject edge3 = JObject.Parse(@"{
""m_OutputSlot"": {
""m_Node"": {
""m_Id"": ""node_cartoon_fire_custom""
},
""m_SlotId"": 2
},
""m_InputSlot"": {
""m_Node"": {
""m_Id"": ""5795a95f005b46bbbbf4c4e22a0ae9c3""
},
""m_SlotId"": 0
}
}");
// Edge 4: CustomFunctionNode outAlpha (output slot 3) -> Alpha block node 6c0a4ae63e3c48ad9e08ffbc4cf86bba (slot 0)
JObject edge4 = JObject.Parse(@"{
""m_OutputSlot"": {
""m_Node"": {
""m_Id"": ""node_cartoon_fire_custom""
},
""m_SlotId"": 3
},
""m_InputSlot"": {
""m_Node"": {
""m_Id"": ""6c0a4ae63e3c48ad9e08ffbc4cf86bba""
},
""m_SlotId"": 0
}
}");
edges.Add(edge1);
edges.Add(edge2);
edges.Add(edge3);
edges.Add(edge4);
// Write all JObjects back to file separated by two newlines
StringBuilder sb = new StringBuilder();
for (int i = 0; i < jObjects.Count; i++)
{
sb.Append(jObjects[i].ToString(Newtonsoft.Json.Formatting.Indented));
if (i < jObjects.Count - 1)
{
sb.Append("\n\n");
}
}
File.WriteAllText(path, sb.ToString());
// Restore material shader to original Fire_Shader.shadergraph
string materialPath = "Assets/Materials/Fire_Material.mat";
Material mat = AssetDatabase.LoadAssetAtPath<Material>(materialPath);
if (mat != null)
{
Shader origShader = Shader.Find("Shader Graphs/Fire_Shader");
if (origShader != null)
{
Undo.RecordObject(mat, "Restore original Fire ShaderGraph");
mat.shader = origShader;
EditorUtility.SetDirty(mat);
}
}
AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
AssetDatabase.SaveAssets();
Debug.Log("Successfully injected Toon Fire Custom Function into Fire_Shader.shadergraph, updated edges, and restored material shader!");
}
}

View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 99fc5f3bc18a15f40b96011883199d7d

View File

@ -8,7 +8,7 @@ Material:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Fire_Material
m_Shader: {fileID: 4800000, guid: 8b5db5774fae5da4e8d1096b76b05ba6, type: 3}
m_Shader: {fileID: -6465566751694194690, guid: ec072c5cefc89e045946ddcbed457f8a, type: 3}
m_Parent: {fileID: 0}
m_ModifiedSerializedProperties: 0
m_ValidKeywords: []
@ -53,6 +53,7 @@ Material:
- _ThresholdMid: 0.38
- _ThresholdOuter: 0.15
m_Colors:
- White: {r: 1, g: 1, b: 1, a: 1}
- _Color: {r: 2, g: 2, b: 2, a: 1}
- _ColorInner: {r: 2.2, g: 0.95, b: 0.15, a: 1}
- _ColorMid: {r: 1.8, g: 0.55, b: 0, a: 1}

View File

@ -1,172 +0,0 @@
Shader "Custom/CartoonFire"
{
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
_Color ("Main Color (Multiplier)", Color) = (1,1,1,1)
[HDR] _ColorOuter ("Outer Color (Red)", Color) = (1.5, 0.2, 0.1, 1)
[HDR] _ColorMid ("Mid Color (Orange)", Color) = (1.8, 0.55, 0.0, 1)
[HDR] _ColorInner ("Inner Color (Yellow)", Color) = (2.2, 0.95, 0.15, 1)
_Speed ("Scroll Speed (XY)", Vector) = (0.0, -1.8, 0.0, 0.0)
_DistortStrength ("Distort Strength", Range(0, 0.5)) = 0.08
_ThresholdOuter ("Threshold Outer", Range(0, 1)) = 0.15
_ThresholdMid ("Threshold Mid", Range(0, 1)) = 0.38
_ThresholdInner ("Threshold Inner", Range(0, 1)) = 0.58
_NoiseScale ("Noise Scale", Range(1, 50)) = 15.0
}
SubShader
{
Tags
{
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
"PreviewType" = "Plane"
"CanUseSpriteAtlas" = "True"
"RenderPipeline" = "UniversalPipeline"
}
Cull Off
Lighting Off
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
Name "SpriteUnlit"
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
struct Attributes
{
float4 positionOS : POSITION;
float4 color : COLOR;
float2 uv : TEXCOORD0;
};
struct Varyings
{
float4 positionCS : SV_POSITION;
float4 color : COLOR;
float2 uv : TEXCOORD0;
};
TEXTURE2D(_MainTex);
SAMPLER(sampler_MainTex);
CBUFFER_START(UnityPerMaterial)
float4 _MainTex_ST;
float4 _Color;
float4 _ColorOuter;
float4 _ColorMid;
float4 _ColorInner;
float4 _Speed;
float _DistortStrength;
float _ThresholdOuter;
float _ThresholdMid;
float _ThresholdInner;
float _NoiseScale;
CBUFFER_END
// Procedural noise functions
float hash(float2 p)
{
p = frac(p * float2(123.34, 456.21));
p += dot(p, p + 45.32);
return frac(p.x * p.y);
}
float noise(float2 p)
{
float2 i = floor(p);
float2 f = frac(p);
float2 u = f * f * (3.0 - 2.0 * f);
return lerp(lerp(hash(i + float2(0.0,0.0)), hash(i + float2(1.0,0.0)), u.x),
lerp(hash(i + float2(0.0,1.0)), hash(i + float2(1.0,1.0)), u.x), u.y);
}
float fbm(float2 p)
{
float v = 0.0;
float a = 0.5;
float2 shift = float2(100.0, 100.0);
float2x2 rot = float2x2(0.8, 0.6, -0.6, 0.8);
for (int i = 0; i < 4; ++i) {
v += a * noise(p);
p = mul(rot, p) * 2.0 + shift;
a *= 0.5;
}
return v;
}
Varyings vert(Attributes input)
{
Varyings output;
output.positionCS = TransformObjectToHClip(input.positionOS.xyz);
output.uv = input.uv;
output.color = input.color;
return output;
}
float4 frag(Varyings input) : SV_Target
{
// Calculate moving noise coordinates
float2 noiseUV = input.uv * _NoiseScale + _Speed.xy * _Time.y;
float n = fbm(noiseUV);
// Distort the UV coordinates
float2 distortedUV = input.uv + float2(n - 0.5, n - 0.5) * _DistortStrength;
// Sample the main sprite texture
float4 mainTex = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, distortedUV);
// Base value is derived from texture transparency and color
float value = mainTex.a;
// Fade out at the top of the sprite (vertical gradient)
value *= saturate(1.15 - input.uv.y);
// Combine with noise value
value = saturate(value * 0.3 + value * n * 0.7);
// Toon stepping (thresholds)
float stepOuter = step(_ThresholdOuter, value);
float stepMid = step(_ThresholdMid, value);
float stepInner = step(_ThresholdInner, value);
// Select color based on steps
float4 fireColor = float4(0, 0, 0, 0);
if (stepInner > 0.5)
{
fireColor = _ColorInner;
}
else if (stepMid > 0.5)
{
fireColor = _ColorMid;
}
else if (stepOuter > 0.5)
{
fireColor = _ColorOuter;
}
// Final alpha is gated by the outer step
fireColor.a *= stepOuter;
// Modulate by sprite / particle vertex color
fireColor *= input.color * _Color;
return fireColor;
}
ENDHLSL
}
}
}

View File

@ -1,9 +0,0 @@
fileFormatVersion: 2
guid: 8b5db5774fae5da4e8d1096b76b05ba6
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:

File diff suppressed because it is too large Load Diff