ModifyImageRightNode.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using PixiEditor.ChangeableDocument.Changeables.Graph.Context;
  2. using PixiEditor.ChangeableDocument.Changeables.Graph.Interfaces;
  3. using PixiEditor.ChangeableDocument.Rendering;
  4. using PixiEditor.DrawingApi.Core;
  5. using PixiEditor.DrawingApi.Core.Shaders.Generation;
  6. using PixiEditor.DrawingApi.Core.Shaders.Generation.Expressions;
  7. using PixiEditor.DrawingApi.Core.Surfaces;
  8. using PixiEditor.DrawingApi.Core.Surfaces.PaintImpl;
  9. using PixiEditor.Numerics;
  10. namespace PixiEditor.ChangeableDocument.Changeables.Graph.Nodes;
  11. [NodeInfo("ModifyImageRight")]
  12. [PairNode(typeof(ModifyImageLeftNode), "ModifyImageZone")]
  13. public class ModifyImageRightNode : RenderNode, IPairNode, ICustomShaderNode
  14. {
  15. public Guid OtherNode { get; set; }
  16. private Paint drawingPaint = new Paint() { BlendMode = BlendMode.Src };
  17. public FuncInputProperty<Float2> Coordinate { get; }
  18. public FuncInputProperty<Half4> Color { get; }
  19. private string _lastSksl;
  20. public ModifyImageRightNode()
  21. {
  22. Coordinate = CreateFuncInput(nameof(Coordinate), "UV", new Float2("coords"));
  23. Color = CreateFuncInput(nameof(Color), "COLOR", new Half4(""));
  24. }
  25. protected override void OnPaint(RenderContext renderContext, DrawingSurface targetSurface)
  26. {
  27. if (OtherNode == null)
  28. {
  29. FindStartNode();
  30. if (OtherNode == null)
  31. {
  32. return;
  33. }
  34. }
  35. var startNode = FindStartNode();
  36. if (startNode == null)
  37. {
  38. return;
  39. }
  40. if (startNode.Image.Value is not { Size: var size })
  41. {
  42. return;
  43. }
  44. Texture surface = RequestTexture(0, size);
  45. ShaderBuilder builder = new(size);
  46. FuncContext context = new(renderContext, builder);
  47. if (Coordinate.Connection != null)
  48. {
  49. var coordinate = Coordinate.Value(context);
  50. if (string.IsNullOrEmpty(coordinate.VariableName))
  51. {
  52. builder.SetConstant(context.SamplePosition, coordinate);
  53. }
  54. else
  55. {
  56. builder.Set(context.SamplePosition, coordinate);
  57. }
  58. }
  59. else
  60. {
  61. var constCoords = Coordinate.NonOverridenValue(FuncContext.NoContext);
  62. constCoords.VariableName = "constCords";
  63. builder.AddUniform(constCoords.VariableName, constCoords.ConstantValue);
  64. builder.Set(context.SamplePosition, constCoords);
  65. }
  66. if (Color.Connection != null)
  67. {
  68. builder.ReturnVar(Color.Value(context));
  69. }
  70. else
  71. {
  72. Half4 color = Color.NonOverridenValue(FuncContext.NoContext);
  73. color.VariableName = "color";
  74. builder.AddUniform(color.VariableName, color.ConstantValue);
  75. builder.ReturnVar(color);
  76. }
  77. string sksl = builder.ToSkSl();
  78. if (sksl != _lastSksl)
  79. {
  80. _lastSksl = sksl;
  81. drawingPaint?.Shader?.Dispose();
  82. drawingPaint.Shader = builder.BuildShader();
  83. }
  84. else
  85. {
  86. drawingPaint.Shader = drawingPaint.Shader.WithUpdatedUniforms(builder.Uniforms);
  87. }
  88. surface.DrawingSurface.Canvas.DrawPaint(drawingPaint);
  89. targetSurface.Canvas.DrawSurface(surface.DrawingSurface, 0, 0);
  90. builder.Dispose();
  91. }
  92. public override RectD? GetPreviewBounds(int frame, string elementToRenderName = "")
  93. {
  94. //TODO: Implement
  95. return null;
  96. }
  97. public override bool RenderPreview(DrawingSurface renderOn, ChunkResolution resolution, int frame, string elementToRenderName)
  98. {
  99. //TODO: Implement
  100. return false;
  101. }
  102. public override void Dispose()
  103. {
  104. base.Dispose();
  105. drawingPaint?.Dispose();
  106. }
  107. private ModifyImageLeftNode FindStartNode()
  108. {
  109. ModifyImageLeftNode startNode = null;
  110. TraverseBackwards(node =>
  111. {
  112. if (node is ModifyImageLeftNode leftNode)
  113. {
  114. startNode = leftNode;
  115. OtherNode = leftNode.Id;
  116. return false;
  117. }
  118. return true;
  119. });
  120. return startNode;
  121. }
  122. public override Node CreateCopy() => new ModifyImageRightNode();
  123. }