internal static bool ResetMaterialKeywords(Material material, ShaderID shaderId) { MaterialResetter resetter; // If we send a non HDRP material we don't throw an exception, the return type already handles errors. try { k_MaterialResetters.TryGetValue(shaderId, out resetter); } catch { return(false); } if (resetter != null) { CoreEditorUtils.RemoveMaterialKeywords(material); // We need to reapply ToggleOff/Toggle keyword after reset via ApplyMaterialPropertyDrawers MaterialEditor.ApplyMaterialPropertyDrawers(material); resetter(material); EditorUtility.SetDirty(material); return(true); } return(false); }
static void UpgradeV3(Material material, ShaderID shaderID) { if (shaderID.IsShaderGraph()) { return; } switch (shaderID) { case ShaderID.Lit: case ShaderID.SimpleLit: case ShaderID.ParticlesLit: case ShaderID.ParticlesSimpleLit: case ShaderID.ParticlesUnlit: var propertyID = Shader.PropertyToID("_EmissionColor"); if (material.HasProperty(propertyID)) { // In older version there was a bug that these shaders did not had HDR attribute on emission property. // This caused emission color to be converted from gamma to linear space. // In order to avoid visual regression on older projects we will do gamma to linear conversion here. var emissionGamma = material.GetColor(propertyID); var emissionLinear = emissionGamma.linear; material.SetColor(propertyID, emissionLinear); } break; } }
//Loads data public override void Load(Item item, TagCompound tag) { itemName = tag.GetString("Name"); shaderID = ShaderID.Load(tag.GetCompound("Shader")); //Set the item's name correctly item.SafeNameOverride(itemName); }
public void SendAmmoShaderPacket() { if (Main.netMode == NetmodeID.MultiplayerClient) { ModPacket pak = GetPacket(); pak.Write((byte)PacketType.AMMO); ShaderID.Write(pak, ammoShaders[Main.myPlayer]); pak.Send(); } }
//Saves data public override TagCompound Save(Item item) { //Save custom item info TagCompound data = new TagCompound(); data.Set("Name", itemName); data.Set("Shader", ShaderID.Save(shaderID)); return(data); }
public void SendNPCShaderPacket(NPC npc, CustomizerNPCInfo npcInfo) { if (Main.netMode == NetmodeID.MultiplayerClient) { ModPacket pak = GetPacket(); pak.Write((byte)PacketType.NPC); ShaderID.Write(pak, new ShaderID(npcInfo.shaderID)); pak.Write(npc.whoAmI); pak.Send(); } }
internal static string GetShaderPath(ShaderID id) { int index = (int)id; if (index < 0 && index >= (int)ShaderID.Count_Standard) { Debug.LogError("Trying to access HDRP shader path out of bounds"); return(""); } return(s_ShaderPaths[index]); }
internal static Type GetShaderMasterNodeType(ShaderID id) { int index = (int)id - (int)ShaderID.Count_Standard; if (index < 0 && index >= (int)ShaderID.Count_ShaderGraph) { Debug.LogError("Trying to access HDRP shader path out of bounds"); return(null); } return(s_MasterNodes[index]); }
static void UpgradeV2(Material material, ShaderID shaderID) { if (shaderID.IsShaderGraph()) { return; } // fix 50 offset on shaders if (material.HasProperty("_QueueOffset")) { BaseShaderGUI.SetupMaterialBlendMode(material); } }
static void UpgradeV1(Material material, ShaderID shaderID) { if (shaderID.IsShaderGraph()) { return; } var shaderPath = ShaderUtils.GetShaderPath((ShaderPathID)shaderID); var upgradeFlag = MaterialUpgrader.UpgradeFlags.LogMessageWhenNoUpgraderFound; switch (shaderID) { case ShaderID.Unlit: MaterialUpgrader.Upgrade(material, new UnlitUpdaterV1(shaderPath), upgradeFlag); UnlitShader.SetMaterialKeywords(material); break; case ShaderID.SimpleLit: MaterialUpgrader.Upgrade(material, new SimpleLitUpdaterV1(shaderPath), upgradeFlag); SimpleLitShader.SetMaterialKeywords(material, SimpleLitGUI.SetMaterialKeywords); break; case ShaderID.Lit: MaterialUpgrader.Upgrade(material, new LitUpdaterV1(shaderPath), upgradeFlag); LitShader.SetMaterialKeywords(material, LitGUI.SetMaterialKeywords); break; case ShaderID.ParticlesLit: MaterialUpgrader.Upgrade(material, new ParticleUpdaterV1(shaderPath), upgradeFlag); ParticlesLitShader.SetMaterialKeywords(material, LitGUI.SetMaterialKeywords, ParticleGUI.SetMaterialKeywords); break; case ShaderID.ParticlesSimpleLit: MaterialUpgrader.Upgrade(material, new ParticleUpdaterV1(shaderPath), upgradeFlag); ParticlesSimpleLitShader.SetMaterialKeywords(material, SimpleLitGUI.SetMaterialKeywords, ParticleGUI.SetMaterialKeywords); break; case ShaderID.ParticlesUnlit: MaterialUpgrader.Upgrade(material, new ParticleUpdaterV1(shaderPath), upgradeFlag); ParticlesUnlitShader.SetMaterialKeywords(material, null, ParticleGUI.SetMaterialKeywords); break; } }
internal static void ResetMaterialKeywords(Material material, ShaderID shaderID = ShaderID.Unknown) { // if unknown, look it up from the material's shader // NOTE: this will only work for asset-based shaders.. if (shaderID == ShaderID.Unknown) { shaderID = GetShaderID(material.shader); } switch (shaderID) { case ShaderID.SG_Lit: BuiltInLitGUI.UpdateMaterial(material); break; case ShaderID.SG_Unlit: BuiltInUnlitGUI.UpdateMaterial(material); break; } }
// Separate Preserve Specular Lighting from Premultiplied blend mode. // Update materials params for backwards compatibility. (Keep the same end result). // - Previous (incorrect) premultiplied blend mode --> Alpha blend mode + Preserve Specular Lighting // - Otherwise keep the blend mode and disable Preserve Specular Lighting // - Correct premultiply mode is not possible in V5. // // This is run both hand-written and shadergraph materials. // // Hand-written and overridable shadergraphs always have blendModePreserveSpecular property, which // is assumed to be new since we only run this for V5 -> V6 upgrade. // // Fixed shadergraphs do not have this keyword and are filtered out. // The blend mode is baked in the generated shader, so there's no material properties to be upgraded. // The shadergraph upgrade on re-import will handle the fixed shadergraphs. static void UpgradeV6(Material material, ShaderID shaderID) { var surfaceTypePID = Shader.PropertyToID(Property.SurfaceType); bool isTransparent = material.HasProperty(surfaceTypePID) && material.GetFloat(surfaceTypePID) >= 1.0f; if (isTransparent) { if (shaderID == ShaderID.Unlit) { var blendModePID = Shader.PropertyToID(Property.BlendMode); var blendMode = (BaseShaderGUI.BlendMode)material.GetFloat(blendModePID); // Premultiply used to be "Premultiply (* alpha in shader)" aka Alpha blend if (blendMode == BaseShaderGUI.BlendMode.Premultiply) { material.SetFloat(blendModePID, (float)BaseShaderGUI.BlendMode.Alpha); } } else { var blendModePreserveSpecularPID = Shader.PropertyToID(Property.BlendModePreserveSpecular); if (material.HasProperty(blendModePreserveSpecularPID)) { var blendModePID = Shader.PropertyToID(Property.BlendMode); var blendMode = (BaseShaderGUI.BlendMode)material.GetFloat(blendModePID); if (blendMode == BaseShaderGUI.BlendMode.Premultiply) { material.SetFloat(blendModePID, (float)BaseShaderGUI.BlendMode.Alpha); material.SetFloat(blendModePreserveSpecularPID, 1.0f); } else { material.SetFloat(blendModePreserveSpecularPID, 0.0f); } BaseShaderGUI.SetMaterialKeywords(material); } } } }
static void UpgradeV5(Material material, ShaderID shaderID) { if (shaderID.IsShaderGraph()) { return; } var propertyID = Shader.PropertyToID("_Surface"); if (material.HasProperty(propertyID)) { float surfaceType = material.GetFloat(propertyID); if (surfaceType >= 1.0f) { material.EnableKeyword("_SURFACE_TYPE_TRANSPARENT"); } else { material.DisableKeyword("_SURFACE_TYPE_TRANSPARENT"); } } }
public override void HandlePacket(BinaryReader reader, int whoAmI) { var type = (PacketType)reader.ReadByte(); if (Main.netMode == NetmodeID.Server) { ModPacket pak = GetPacket(); pak.Write((byte)type); ShaderID.Write(pak, ShaderID.Read(reader)); if (type == PacketType.NPC) { pak.Write(reader.ReadInt32()); } else if (type == PacketType.PROJ) { pak.Write(reader.ReadInt32()); pak.Write(reader.ReadByte()); } else { pak.Write(whoAmI); } pak.Send(ignoreClient: whoAmI); } else { ShaderID shader = ShaderID.Read(reader); switch (type) { case PacketType.AMMO: ammoShaders[reader.ReadInt32()] = shader; break; case PacketType.NPC: Main.npc[reader.ReadInt32()].Customizer().shaderID = shader.ID; break; case PacketType.PROJ: // read in reverse order because why ??? // none of this actually works anyway // if GlobalItem had a net sync function that'd be great int identity = reader.ReadInt32(); byte owner = reader.ReadByte(); //int idx = Projectile.GetByUUID(owner, identity); int num84 = 1000; for (int num85 = 0; num85 < 1000; num85++) { if (Main.projectile[num85].owner == owner && Main.projectile[num85].identity == identity && Main.projectile[num85].active) { num84 = num85; break; } } if (num84 == 1000) { for (int num86 = 0; num86 < 1000; num86++) { if (!Main.projectile[num86].active) { num84 = num86; break; } } } Main.NewText("Received projectile packet: " + owner + " " + identity + " " + num84 + " " + shader.ID); if (num84 != -1 && num84 != Main.maxProjectiles) { var proj = Main.projectile[num84]; //if(proj.Customizer() is CustomizerProjInfo customizer) //customizer.shaderID = shader.ID; proj.Customizer().shaderID = shader.ID; } else { Main.NewText("Invalid projectile identity"); } break; } } }
static void InitializeLatest(Material material, ShaderID shaderID) { // newly created shadergraph materials should reset their keywords immediately (in case inspector doesn't get invoked) ShaderUtils.ResetMaterialKeywords(material); }
// this is used to update a material's keywords, applying any shader-associated logic to update dependent properties and keywords // this is also invoked when a material is created, modified, or the material's shader is modified or reassigned internal static void UpdateMaterial(Material material, MaterialUpdateType updateType, ShaderID shaderID = ShaderID.Unknown) { // if unknown, look it up from the material's shader // NOTE: this will only work for asset-based shaders.. if (shaderID == ShaderID.Unknown) { shaderID = GetShaderID(material.shader); } switch (shaderID) { case ShaderID.Lit: LitShader.SetMaterialKeywords(material, LitGUI.SetMaterialKeywords); break; case ShaderID.SimpleLit: SimpleLitShader.SetMaterialKeywords(material, SimpleLitGUI.SetMaterialKeywords); break; case ShaderID.Unlit: UnlitShader.SetMaterialKeywords(material); break; case ShaderID.ParticlesLit: ParticlesLitShader.SetMaterialKeywords(material, LitGUI.SetMaterialKeywords, ParticleGUI.SetMaterialKeywords); break; case ShaderID.ParticlesSimpleLit: ParticlesSimpleLitShader.SetMaterialKeywords(material, SimpleLitGUI.SetMaterialKeywords, ParticleGUI.SetMaterialKeywords); break; case ShaderID.ParticlesUnlit: ParticlesUnlitShader.SetMaterialKeywords(material, null, ParticleGUI.SetMaterialKeywords); break; case ShaderID.SG_Lit: ShaderGraphLitGUI.UpdateMaterial(material, updateType); break; case ShaderID.SG_Unlit: ShaderGraphUnlitGUI.UpdateMaterial(material, updateType); break; default: break; } }
internal static bool IsShaderGraph(this ShaderID id) { return((int)id >= 1000); }
internal static bool IsShaderGraph(this ShaderID id) { return(id >= ShaderID.SG_Start); }
//Stops items from losing their noice data when dropped in multiplayer public override void NetSend(Item item, BinaryWriter writer) { writer.Write(itemName); ShaderID.Write(writer, shaderID); }
public override void NetReceive(Item item, BinaryReader reader) { itemName = reader.ReadString(); shaderID = ShaderID.Read(reader); item.SafeNameOverride(itemName); }
static void InitializeLatest(Material material, ShaderID id) { // newly created materials should reset their keywords immediately (in case inspector doesn't get invoked) Unity.Rendering.Universal.ShaderUtils.UpdateMaterial(material, MaterialUpdateType.CreatedNewMaterial, id); }
static void UpgradeV4(Material material, ShaderID shaderID) { }