SampleImageNode.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using PixiEditor.ChangeableDocument.Changeables.Graph.Context;
  2. using PixiEditor.ChangeableDocument.Rendering;
  3. using PixiEditor.DrawingApi.Core;
  4. using PixiEditor.DrawingApi.Core.ColorsImpl;
  5. using PixiEditor.DrawingApi.Core.Shaders.Generation.Expressions;
  6. using PixiEditor.DrawingApi.Core.Surfaces;
  7. using PixiEditor.Numerics;
  8. namespace PixiEditor.ChangeableDocument.Changeables.Graph.Nodes;
  9. [NodeInfo("SampleImage")]
  10. public class SampleImageNode : Node
  11. {
  12. public InputProperty<Texture?> Image { get; }
  13. public FuncInputProperty<Float2> Coordinate { get; }
  14. public FuncOutputProperty<Half4> Color { get; }
  15. public SampleImageNode()
  16. {
  17. Image = CreateInput<Texture>(nameof(Texture), "IMAGE", null);
  18. Coordinate = CreateFuncInput<Float2>(nameof(Coordinate), "UV", VecD.Zero);
  19. Color = CreateFuncOutput(nameof(Color), "COLOR", GetColor);
  20. }
  21. private Half4 GetColor(FuncContext context)
  22. {
  23. context.ThrowOnMissingContext();
  24. if (Image.Value is null)
  25. {
  26. return new Half4("");
  27. }
  28. Float2 uv = context.GetValue(Coordinate);
  29. return context.SampleTexture(Image.Value, uv);
  30. }
  31. protected override Texture? OnExecute(RenderingContext context)
  32. {
  33. return Image.Value;
  34. }
  35. public override Node CreateCopy() => new SampleImageNode();
  36. }