Skip to content

Commit 8dbaffb

Browse files
committed
bug fixes relating opening and saving files + function node input/output node registration fix to register listeners correctly + plane primitive in test scene + change order of unserialize to parent first
1 parent 1b37f8e commit 8dbaffb

20 files changed

+186
-67
lines changed

src/Editor.ts

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { LayoutElement } from "./layout/LayoutElement";
1313
import { onDoubleClick } from "./util/onDoubleClick";
1414
import { createNewNode } from "./ui/NodeSelectionModal";
1515
import { ConnectionsArray, OuletCandidate } from "./core/Connection";
16-
import { NodeGroupType, NodeTypes } from "./EditorNodes";
16+
import { newNodeById, NodeGroupType, NodeTypes } from "./EditorNodes";
1717
import { ScenePreviewNode } from "./nodes/preview/ScenePreview";
1818
import { Script } from "./export/Script";
1919
import { FunctionCallNode } from "./nodes/logic/FunctionCallNode";
@@ -543,18 +543,12 @@ export class Editor {
543543
}
544544

545545
protected bingToTop(node: Node) {
546-
const nodeIndexZ = this.zSortedObjs.indexOf(node);
547-
const nodeIndex = this.objs.indexOf(node);
546+
const nodeIndexZ = this.zSortedObjs.indexOf(node);
548547

549548
if (nodeIndexZ !== -1) {
550549
this.zSortedObjs.splice(nodeIndexZ, 1);
551550
this.zSortedObjs.push(node);
552-
}
553-
554-
// if (nodeIndex !== -1) {
555-
// this.objs.splice(nodeIndex, 1);
556-
// this.objs.push(node);
557-
// }
551+
}
558552
}
559553

560554
/**
@@ -618,13 +612,7 @@ export class Editor {
618612

619613
add(node: Node | string, nodeSetup?: (node: Node) => void) {
620614
if (typeof node == "string") {
621-
const referencedNode = NodeTypes.flatMap((group) => group.nodes).find(
622-
(n) => n.id == node,
623-
);
624-
if (!referencedNode) {
625-
throw new Error(`Trying to create a node with id:${node} that doesn't exist.`);
626-
}
627-
node = new referencedNode.TypeClass();
615+
node = newNodeById( node );
628616
}
629617

630618
node.editor = this;

src/EditorNodes.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { ColorNode } from './nodes/input/ColorNode';
44
import { tslInputNodes } from './nodes/input/TslInputNode';
55
import { UniformValueNode } from './nodes/input/UniformValueNode';
66
import { ValueNode } from './nodes/input/ValueNode';
7+
import { ForNode } from './nodes/logic/ForNode';
78
import { FunctionCallNode } from './nodes/logic/FunctionCallNode';
89
import { FunctionInputNode } from './nodes/logic/FunctionInputNode';
910
import { FunctionNode } from './nodes/logic/FunctionNode';
@@ -77,6 +78,7 @@ export const NodeTypes: NodeGroupType[] = [
7778
hidden: true,
7879
},
7980
{ TypeClass: IfNode, name: 'If / Conditional', id: 'if' },
81+
{ TypeClass:ForNode, name:"For loop", id:"foor-loop"},
8082
{
8183
TypeClass: VariableDeclarationNode,
8284
name: 'Variable declaration',
@@ -138,3 +140,24 @@ export const NodeTypes: NodeGroupType[] = [
138140
export function getNodeTypeById(id: string) {
139141
return NodeTypes.flatMap((g) => g.nodes).find((n) => n.id == id);
140142
}
143+
144+
export function newNodeById( id:string ) {
145+
const referencedNode = NodeTypes.flatMap((group) => group.nodes).find(
146+
(n) => n.id == id,
147+
);
148+
if (!referencedNode) {
149+
throw new Error(`Trying to create a node with id:${id} that doesn't exist.`);
150+
}
151+
152+
const args = referencedNode.constructorArgs;
153+
154+
if (args) {
155+
return new referencedNode.TypeClass(
156+
...(Array.isArray(args)
157+
? args
158+
: [args]),
159+
);
160+
} else {
161+
return new referencedNode.TypeClass();
162+
}
163+
}

src/ThreeScene.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ export class ThreeScene extends THREE.EventDispatcher<SceneEvents> {
1818
private objs: THREE.Mesh[] = [];
1919
readonly pointLight: THREE.PointLight;
2020
private mouse: THREE.Vector2 = new THREE.Vector2();
21-
rotationSpeed = 1;
21+
rotationSpeed = 0;
22+
rotation = 0;
2223
protected _currentObjectIndex = 1;
2324

2425
private materialNotSet = new THREE.MeshStandardMaterial({
@@ -66,8 +67,7 @@ export class ThreeScene extends THREE.EventDispatcher<SceneEvents> {
6667
}
6768

6869
protected buildScene() {
69-
const material = this.materialNotSet;
70-
70+
const material = this.materialNotSet;
7171
//
7272
// dummy objects
7373
//
@@ -102,6 +102,11 @@ export class ThreeScene extends THREE.EventDispatcher<SceneEvents> {
102102
},
103103
);
104104

105+
const plane = new THREE.Mesh(new THREE.PlaneGeometry(5,5), material)
106+
plane.name="Plane";
107+
this.objs.push(plane);
108+
this.objHolder.add(plane);
109+
105110
this.scene.add(this.objHolder);
106111

107112
this.camera.position.z = 4;
@@ -155,7 +160,12 @@ export class ThreeScene extends THREE.EventDispatcher<SceneEvents> {
155160
render() {
156161
const delta = this.clock.getDelta();
157162
this.updateLightPosition();
158-
this.objHolder.rotateY(delta * this.rotationSpeed);
163+
//this.objHolder.rotateY(delta * this.rotationSpeed);
164+
if( this.rotationSpeed )
165+
{
166+
this.rotation += delta * this.rotationSpeed;
167+
}
168+
this.objHolder.rotation.y = this.rotation;
159169
//this.objHolder.rotateX(-delta*this.rotationSpeed)
160170
this.renderer.render(this.scene, this.camera);
161171
}

src/components/ChangeNodeCustomNameButton.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FunctionNode } from '../nodes/logic/FunctionNode';
1+
import { Node } from '../nodes/Node';
22
import { WinNode } from '../nodes/WinNode';
33
import { Button } from './Button';
44

@@ -21,13 +21,8 @@ export class ChangeNodeCustomNameButton extends Button {
2121
this.onNewGame(newName);
2222
}
2323
else {
24-
newName = (
25-
(this.root as WinNode).childOf as FunctionNode
26-
)?.getValidParameterName(newName);
2724

28-
if (!newName) {
29-
return;
30-
}
25+
newName = (this.root as WinNode).editor.getSafeName( this.root as Node, newName);
3126

3227
node.customNodeName = newName;
3328
node.update();

src/nodes/WinNode.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ export class WinNode<T extends WinNodeEvents = WinNodeEvents> extends Node<
143143
}
144144

145145
override unserialize(data: Record<string, any>): void {
146-
this.customNodeName = data.customName;
147-
this._nodeName = data.name;
148146
super.unserialize(data);
147+
this.customNodeName = data.customName;
148+
this._nodeName = data.name;
149149
}
150150
}

src/nodes/attribute/UVNode.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ export class UVNode extends BaseAttributeNode {
3838
}
3939

4040
override unserialize(data: Record<string, any>): void {
41-
this.uvChannel.value = data.channel;
4241
super.unserialize(data);
42+
this.uvChannel.value = data.channel;
43+
4344
}
4445
}

src/nodes/input/ColorNode.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ export class ColorNode extends InputBaseNode {
4141
}
4242

4343
override unserialize(data: Record<string, any>): void {
44-
this.color.color = new Color(data.color);
4544
super.unserialize(data);
45+
this.color.color = new Color(data.color);
46+
4647
}
4748
}

src/nodes/input/TslInputNode.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { string } from 'three/tsl';
21
import { PropertiesNode } from './PropertiesNode';
32

43
const tslInputs: [string, any][] = [
@@ -285,6 +284,11 @@ const tslInputs: [string, any][] = [
285284
desc: 'Returns the viewport size in physical pixel units.',
286285
type: 'vec2',
287286
},
287+
{
288+
name:"viewportResolution",
289+
desc: 'The resolution node is a vec2 containing the render target’s width and height in pixels (e.g., vec2(window.innerWidth, window.innerHeight) for the default canvas).',
290+
type: 'vec2',
291+
}
288292
],
289293
],
290294

src/nodes/input/ValueNode.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class ValueNode extends InputBaseNode {
1818
);
1919

2020
super('Value', [
21-
new DataTypeComboBox((val) => (this.size = val)),
21+
new DataTypeComboBox((val) => (this.size = val+1 )),
2222
...inputs,
2323
new Output('value', DataType.wildcard),
2424
]);
@@ -33,7 +33,7 @@ export class ValueNode extends InputBaseNode {
3333
}
3434
set size(n: number) {
3535
this.inputs.forEach((input, i) => {
36-
input.enabled = i <= n;
36+
input.enabled = i < n;
3737
});
3838

3939
this.output.updateType();
@@ -45,9 +45,9 @@ export class ValueNode extends InputBaseNode {
4545

4646
override get nodeDataType(): IDataType | undefined {
4747
const size = this.size;
48-
return size == 1
48+
return size <= 1
4949
? DataType.float
50-
: DataTypes.find((dt) => dt.name == 'vec' + size)!.type;
50+
: DataTypes.find((dt) => dt.name == 'vec' + size )!.type;
5151
}
5252

5353
protected override writeNodeScript(script: Script): string {
@@ -75,10 +75,12 @@ export class ValueNode extends InputBaseNode {
7575
}
7676

7777
override unserialize(data: Record<string, any>): void {
78-
this.size = data.size ?? 1;
78+
super.unserialize(data);
7979
data.values?.forEach((value: number, i: number) => {
8080
this.inputs[i].value = value;
8181
});
82-
super.unserialize(data);
82+
83+
this.size = data.size ?? 1;
84+
8385
}
8486
}

src/nodes/logic/ForNode.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { DataType, IDataType } from '../../core/IOutlet';
2+
import { Script } from '../../export/Script';
3+
import { InputOrValue } from '../../properties/InputOrValue';
4+
import { Output } from '../../properties/Output';
5+
import { ExecutableLogicNode } from './ExecutableLogicNode';
6+
7+
/**
8+
* A node that behaves like an IF statement
9+
*/
10+
export class ForNode extends ExecutableLogicNode {
11+
12+
private count: InputOrValue;
13+
private loopIndex: Output;
14+
private execute: Output;
15+
16+
constructor() {
17+
const count = new InputOrValue("Count");
18+
const loopIndex = new LoopIndexOutput();
19+
const execute = new Output('Execute', DataType.script);
20+
21+
super('Loop', [count, execute, loopIndex ]);
22+
23+
this.count = count;
24+
this.execute = execute;
25+
this.loopIndex = loopIndex;
26+
}
27+
28+
override get nodeDataType(): IDataType | undefined {
29+
return;
30+
}
31+
32+
protected override writeNodeScript(script: Script): string {
33+
script.importModule('Loop');
34+
35+
const count = this.count.writeScript(script);
36+
const blockcope = script.newScope(true);
37+
38+
//execute
39+
(this.execute.connectedTo?.owner as ExecutableLogicNode).writeBlockScript( script );
40+
41+
blockcope.exit();
42+
43+
//
44+
// the actual TSL if...
45+
//
46+
script.writeLine( `Loop(${ count }, ({ i:${ this.nodeName+"_i" } })=>{ ${ blockcope.toString() } })`);
47+
48+
return '';
49+
}
50+
51+
}
52+
53+
class LoopIndexOutput extends Output {
54+
constructor() {
55+
super("i", DataType.uint);
56+
}
57+
58+
override writeScript(): string {
59+
return this.owner.nodeName+"_i";
60+
}
61+
}

0 commit comments

Comments
 (0)