Skip to content

Commit

Permalink
Add centering to UV node
Browse files Browse the repository at this point in the history
  • Loading branch information
emilwidlund committed Sep 4, 2023
1 parent 239cc2c commit ae1770b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 10 deletions.
9 changes: 8 additions & 1 deletion packages/client/src/templates/layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,14 @@ export const DEFAULT_NEW_CIRCUIT_LAYER_CONTEXT = {
name: 'UV',
type: 'UV',
data: { position: { x: -802, y: 240 } },
inputs: {},
inputs: {
center: {
id: '4b1d7a5b-55b1-4e8f-8252-57d5a948gb6c',
name: 'Center',
type: 'bool',
defaultValue: { tag: 'lit', type: 'bool', val: false }
}
},
outputs: {
uv: {
id: '4eddd22e-f245-4b7a-b447-651b2f913876',
Expand Down
4 changes: 2 additions & 2 deletions packages/client/src/utils/nodes/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export const nodesHierarchy: (
) => ContextMenuContainerSection[] = createNodeCallback => [
{
items: [
extractItem(createNodeCallback)(GLSLNode),
{
icon: 'shapes',
label: 'Common',
Expand All @@ -89,8 +90,7 @@ export const nodesHierarchy: (
CeilNode,
ClampNode,
StepNode,
SmoothstepNode,
GLSLNode
SmoothstepNode
].map(extractItem(createNodeCallback))
}
]
Expand Down
20 changes: 16 additions & 4 deletions packages/webgl/src/nodes/accessor/UVNode/UVNode.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { $xy } from '@thi.ng/shader-ast';
import { $xy, bool, div } from '@thi.ng/shader-ast';
import { aspectCorrectedUV } from '@thi.ng/shader-ast-stdlib';
import { Node, IOutputProps, Output } from '@usealma/graph';
import { Node, IOutputProps, Output, IInputProps, Input } from '@usealma/graph';
import { defaults } from 'lodash';

import { IUVNodeInputs, IUVNodeOutputs, IUVNodeProps } from './UVNode.types';
Expand All @@ -18,7 +18,16 @@ export class UVNode extends Node {
constructor(context: WebGLContext, props: IUVNodeProps = {}) {
super(context, props);

this.inputs = {};
this.inputs = {
center: new Input(
this,
defaults<Partial<IInputProps<'bool'>> | undefined, IInputProps<'bool'>>(props.inputs?.center, {
name: 'Center',
type: 'bool',
defaultValue: bool(false)
})
)
};

this.outputs = {
uv: new Output(
Expand All @@ -27,7 +36,10 @@ export class UVNode extends Node {
name: 'UV',
type: 'vec2',
value: () => {
return aspectCorrectedUV($xy(context.target.gl_FragCoord), context.uniforms.uResolution);
// @ts-ignore
return this.resolveValue(this.inputs.center.value).val === true
? aspectCorrectedUV($xy(context.target.gl_FragCoord), context.uniforms.uResolution)
: div($xy(context.target.gl_FragCoord), context.uniforms.uResolution);
}
})
),
Expand Down
8 changes: 6 additions & 2 deletions packages/webgl/src/nodes/accessor/UVNode/UVNode.types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { INodeProps, IOutputProps, Output } from '@usealma/graph';
import { IInputProps, INodeProps, Input, IOutputProps, Output } from '@usealma/graph';

import { UVNode } from './UVNode';

export interface IUVNodeInputs {
[key: string]: never;
[key: string]: Input<'bool', UVNode>;
center: Input<'bool', UVNode>;
}

export interface IUVNodeOutputs {
Expand All @@ -13,6 +14,9 @@ export interface IUVNodeOutputs {
}

export interface IUVNodeProps extends INodeProps {
inputs?: {
center?: IInputProps<'bool'>;
};
outputs?: {
uv?: IOutputProps<'vec2'>;
fragCoord?: IOutputProps<'vec4'>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class TextureNode extends Node {

declare inputs: ITextureNodeInputs;
declare outputs: ITextureNodeOutputs;
data: ITextureNodeData;
declare data: ITextureNodeData;
textureId!: string;
texture!: Texture;

Expand Down

0 comments on commit ae1770b

Please sign in to comment.