Skip to content

Instantly share code, notes, and snippets.

@andybak
Created March 6, 2019 23:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andybak/117c6f1a891bf4503093a2e35d73a593 to your computer and use it in GitHub Desktop.
Save andybak/117c6f1a891bf4503093a2e35d73a593 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace UnityEditor.VFX.Block
{
[VFXInfo(category = "Color")]
class ColorBlend : VFXBlock
{
public enum BlendMode
{
Darken,
Multiply,
ColorBurn,
LinearBurn,
Lighten,
Screen,
ColorDodge,
LinearDodge,
Overlay,
SoftLight,
HardLight,
VividLight,
LinearLight,
PinLight,
Difference,
Exclusion
}
[SerializeField, VFXSetting, Tooltip("The blend mode to use")]
private BlendMode blendMode = BlendMode.Screen;
public override string name {get{return "Color Blend";}}
public override VFXContextType compatibleContexts {get{return VFXContextType.kInitAndUpdateAndOutput;}}
public override VFXDataType compatibleData {get{return VFXDataType.kParticle;}}
public class InputProperties
{
public Color blendColor;
}
public override IEnumerable<VFXAttributeInfo> attributes
{
get
{
yield return new VFXAttributeInfo(VFXAttribute.Color, VFXAttributeMode.ReadWrite);
}
}
public override string source
{
get
{
switch (blendMode)
{
case BlendMode.Darken:
return @"color = min(blendColor,color);";
case BlendMode.Multiply:
return @"color = blendColor * color;";
case BlendMode.ColorBurn:
return @"color = 1.0 - (1.0-blendColor) / color;";
case BlendMode.LinearBurn:
return @"color = blendColor + color - 1.0;";
case BlendMode.Lighten:
return @"color = max(blendColor,color);";
case BlendMode.Screen:
return @"color = 1.0 - (1.0-blendColor) * (1.0-color);";
case BlendMode.ColorDodge:
return @"color = blendColor / (1.0-color);";
case BlendMode.LinearDodge:
return @"color = blendColor + color;";
case BlendMode.Overlay:
return @"color = (blendColor > 0.5) * (1.0 - (1.0-2.0*(blendColor-0.5)) * (1.0-color)) + (blendColor <= 0.5) * ((2.0*blendColor) * color);";
case BlendMode.SoftLight:
return @"color = (color > 0.5) * (1.0 - (1.0-blendColor) * (1.0-(color-0.5))) + (color <= 0.5) * (blendColor * (color+0.5));";
case BlendMode.HardLight:
return @"color = (color > 0.5) * (1.0 - (1.0-blendColor) * (1.0-2.0*(color-0.5))) + (color <= 0.5) * (blendColor * (2.0*color));";
case BlendMode.VividLight:
return @"color = (color > 0.5) * (1.0 - (1.0-blendColor) / (2.0*(color-0.5))) + (color <= 0.5) * (blendColor / (1.0-2.0*color));";
case BlendMode.LinearLight:
return @"color = (color > 0.5) * (blendColor + 2.0*(color-0.5)) + (color <= 0.5) * (blendColor + 2.0*color - 1.0);";
case BlendMode.PinLight:
return @"color = (color > 0.5) * (max(blendColor,2.0*(color-0.5))) + (color <= 0.5) * (min(blendColor,2.0*color));";
case BlendMode.Difference:
return @"color = abs(blendColor - color);";
case BlendMode.Exclusion:
return @"color = 0.5 - 2.0*(blendColor-0.5)*(color-0.5);";
default:
return "";
}
}
}
}
}
@MostHated
Copy link

MostHated commented Jan 12, 2020

Where would this end up needing to go? I was looking for some example code of trying to override/modify some of the default VFX Graph settings such as color and minimum size a stickynote can be and change some keymappings when I came across this and thought it looked pretty interesting and wanted to try it out, but when I made a script and put this in it I kept getting "inaccessible due to protection level" errors. Does it need to manually be placed into the VFX Graph folders as opposed to being able to extend it from within the project folders?

Thanks,
-MH

@andybak
Copy link
Author

andybak commented Jan 12, 2020

Off the top of my head I think you need to use an asmdef to fool Unity into thinking the code is part of the VFX Graph assembly.

I got that trick from peeweek. See if you can work it out based on this repo: https://github.com/peeweek/net.peeweek.vfxgraph-extras

@MostHated
Copy link

Awesome, thanks a bunch. I appreciate it.

@andybak
Copy link
Author

andybak commented Jan 13, 2020

No worries. Let me know how you get on. I'd love to know more about hacking the internals of VFX graph myself.

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