Skip to content

Commit

Permalink
Improve the progress node. Add the random int node
Browse files Browse the repository at this point in the history
  • Loading branch information
grifdail committed Nov 6, 2024
1 parent 25aaaec commit fd6e6f4
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 4 deletions.
27 changes: 25 additions & 2 deletions src/Nodes/Inputs/Progress.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { IconClock } from "@tabler/icons-react";
import { NodeDefinition } from "../../Types/NodeDefinition";
import { NodeData } from "../../Types/NodeData";
import { ContextMenuData, NodeDefinition } from "../../Types/NodeDefinition";
import { convertToShaderValue } from "../../Utils/convertToShaderValue";
import { createPortConnection } from "../../Utils/createPortConnection";

export const Progress: NodeDefinition = {
id: "Progress",
Expand All @@ -14,10 +16,31 @@ export const Progress: NodeDefinition = {
settings: [{ id: "preview-duration", defaultValue: 1, type: "number", globalKey: "progress" }],
getData: (portId, nodeData, context) => {
var value = context.getGlobalSetting<number>("progress") || 1;
return (context.time / (value * 1000)) % 1;
let offset = 0;
if (nodeData.dataInputs["offset"]) {
offset = context.getInputValueNumber(nodeData, "offset");
}
return (context.time / (value * 1000) + offset) % 1;
},
getShaderCode(node, context) {
var value = context.getGlobalSetting<number>("progress") || 1;
if (node.dataInputs["offset"]) {
return `float ${context.getShaderVar(node, "progress", "number", true)} = mod(time / (${convertToShaderValue(value, "number")} * 1000.0) + ${context.getShaderVar(node, "offset", "number")}, 1.0);`;
}
return `float ${context.getShaderVar(node, "progress", "number", true)} = mod(time / (${convertToShaderValue(value, "number")} * 1000.0), 1.0);`;
},
contextMenu: (node) => {
if (node.dataInputs["offset"]) {
return {} as ContextMenuData;
}
return {
"Use advanced option": (node: NodeData) => {
node.dataInputs["offset"] = createPortConnection({
id: `offset`,
type: "number",
defaultValue: 0,
});
},
};
},
};
30 changes: 30 additions & 0 deletions src/Nodes/Inputs/RandomInt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { IconArrowsShuffle } from "@tabler/icons-react";
import { NodeDefinition } from "../../Types/NodeDefinition";

export const RandomInt: NodeDefinition = {
id: "RandomInt",
description: "A random value between to integer, consistant across frames",
icon: IconArrowsShuffle,
tags: ["Input"],
dataInputs: [
{
id: "min",
type: "number",
defaultValue: 0,
},
{
id: "max",
type: "number",
defaultValue: 6,
},
],
dataOutputs: [{ id: "value", type: "number", defaultValue: 0 }],
executeOutputs: [],
settings: [],
defaultType: "number",
getData: (portId, nodeData, context) => {
var min = context.getInputValueNumber(nodeData, "min");
var max = context.getInputValueNumber(nodeData, "max");
return Math.floor(context.p5.random() * (max - min) + min);
},
};
4 changes: 3 additions & 1 deletion src/Nodes/Nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ import { CustomSimulationStart } from "./CustomFunction/CustomSimulationStart";
import { DrawImagePart } from "./Images/DrawImagePart";
import { ImageDimension } from "./Images/ImageDimension";
import { PaintImage } from "./Images/PaintImage";
import { RandomInt } from "./Inputs/RandomInt";
import { RandomOnSphere } from "./Inputs/RandomOnSphere";
import { Select } from "./Logic/SelectNode";
import { Abs } from "./Math/Abs";
Expand Down Expand Up @@ -159,9 +160,9 @@ import { Sqrt } from "./Math/Sqrt";
import { Step } from "./Math/Step";
import { ShaderMaterial } from "./Shaders/ShaderMaterial";
import { Blackboard } from "./System/Blackboard";
import { ScaleAdd } from "./Vector/ScaleAdd";
import { CrossProduct } from "./Vector/CrossProduct";
import { FocalLength } from "./Vector/FocalLength";
import { ScaleAdd } from "./Vector/ScaleAdd";
import { SeededRandom } from "./Vector/SeededRandom";
import { Value } from "./Vector/Value";
import { Distance } from "./Vector/VectorDistance";
Expand Down Expand Up @@ -246,6 +247,7 @@ export const Nodes: Array<NodeDefinition> = [
Random,
SeededRandom,
RandomOnSphere,
RandomInt,

//Input Misc
Dimension,
Expand Down
2 changes: 1 addition & 1 deletion src/Types/NodeDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { PortType } from "./PortType";
import { SettingDefinition } from "./SettingDefinition";
import { TreeStore } from "./TreeStore";

type ContextMenuData = {
export type ContextMenuData = {
[key: string]: (node: NodeData, tree: TreeStore) => void;
};

Expand Down

0 comments on commit fd6e6f4

Please sign in to comment.