Skip to content

Commit

Permalink
Merge pull request #3077 from t3du/PolygonArray
Browse files Browse the repository at this point in the history
Draw Polygon From Array Node
  • Loading branch information
luboslenco authored Nov 22, 2024
2 parents ec2feb9 + c4d5302 commit 63c21e0
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
57 changes: 57 additions & 0 deletions Sources/armory/logicnode/DrawPolygonFromArrayNode.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package armory.logicnode;

import kha.Color;
import kha.math.Vector2;
import armory.renderpath.RenderToTexture;

#if arm_ui
using zui.GraphicsExtension;
#end

class DrawPolygonFromArrayNode extends LogicNode {

var vertices: Array<Vector2>;

public function new(tree: LogicTree) {
super(tree);
}

override function run(from: Int) {
#if arm_ui
RenderToTexture.ensure2DContext("DrawPolygonNode");

var vertArr: Dynamic = inputs[6].get();

if (vertices == null) {
// Preallocate
vertices = [];
vertices.resize(vertArr.length);
for (i in 0...vertices.length) {
vertices[i] = new Vector2();
}
}

for (i in 0...vertArr.length) {
if (Std.isOfType(vertArr[i], iron.math.Vec4) || Std.isOfType(vertArr[i], iron.math.Vec3)){
vertices[i].x = vertArr[i].x;
vertices[i].y = vertArr[i].y;
} else {
assert(Error, vertArr[i].length >= 2, "Array positions must be an array of two dimensions (X, Y)");
vertices[i].x = vertArr[i][0];
vertices[i].y = vertArr[i][1];
}
}

final colorVec = inputs[1].get();
RenderToTexture.g.color = Color.fromFloats(colorVec.x, colorVec.y, colorVec.z, colorVec.w);

if (inputs[2].get()) {
RenderToTexture.g.fillPolygon(inputs[4].get(), inputs[5].get(), vertices);
} else {
RenderToTexture.g.drawPolygon(inputs[4].get(), inputs[5].get(), vertices, inputs[3].get());
}
#end

runOutput(0);
}
}
35 changes: 35 additions & 0 deletions blender/arm/logicnode/draw/LN_draw_polygon_from_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from arm.logicnode.arm_nodes import *

class DrawPolygonFromArrayNode(ArmLogicTreeNode):
"""Draws a polygon.
@input Draw: Activate to draw the polygon on this frame. The input must
be (indirectly) called from an `On Render2D` node.
@input Color: The color of the polygon.
@input Filled: Whether the polygon is filled or only the outline is drawn.
@input Strength: The line strength if the polygon is not filled.
@input Origin X/Origin Y: The origin position of the polygon, in pixels
from the top left corner. This position is added to all other
points, so they are defined relative to this position.
@input Xn/Yn: The position of polygon's points, in pixels from `Origin X`/`Origin Y`.
@output Out: Activated after the polygon has been drawn.
@see [`kha.graphics2.GraphicsExtension.drawPolygon()`](http://kha.tech/api/kha/graphics2/GraphicsExtension.html#drawPolygon).
@see [`kha.graphics2.GraphicsExtension.fillPolygon()`](http://kha.tech/api/kha/graphics2/GraphicsExtension.html#fillPolygon).
"""
bl_idname = 'LNDrawPolygonFromArrayNode'
bl_label = 'Draw Polygon From Array'
arm_section = 'draw'
arm_version = 1

def arm_init(self, context):
self.add_input('ArmNodeSocketAction', 'Draw')
self.add_input('ArmColorSocket', 'Color', default_value=[1.0, 1.0, 1.0, 1.0])
self.add_input('ArmBoolSocket', 'Filled', default_value=False)
self.add_input('ArmFloatSocket', 'Strength', default_value=1.0)
self.add_input('ArmFloatSocket', 'Origin X')
self.add_input('ArmFloatSocket', 'Origin Y')
self.add_input('ArmNodeSocketArray', 'Array (X, Y)')

self.add_output('ArmNodeSocketAction', 'Out')

0 comments on commit 63c21e0

Please sign in to comment.