Archived
Private
Public Access
1
0

Initial commit

This commit is contained in:
2022-09-04 12:45:01 +02:00
commit f4a01d6a69
11601 changed files with 4206660 additions and 0 deletions

View File

@@ -0,0 +1,31 @@
import Node from '../core/Node.js';
class ArrayElementNode extends Node {
constructor( node, indexNode ) {
super();
this.node = node;
this.indexNode = indexNode;
}
getNodeType( builder ) {
return this.node.getNodeType( builder );
}
generate( builder ) {
const nodeSnippet = this.node.build( builder );
const indexSnippet = this.indexNode.build( builder, 'int' );
return `${nodeSnippet}[ ${indexSnippet} ]`;
}
}
export default ArrayElementNode;

View File

@@ -0,0 +1,33 @@
import Node from '../core/Node.js';
class ConvertNode extends Node {
constructor( node, convertTo ) {
super();
this.node = node;
this.convertTo = convertTo;
}
getNodeType( /*builder*/ ) {
return this.convertTo;
}
generate( builder ) {
const convertTo = this.convertTo;
const convertToSnippet = builder.getType( convertTo );
const nodeSnippet = this.node.build( builder, convertTo );
return `${ convertToSnippet }( ${ nodeSnippet } )`;
}
}
export default ConvertNode;

View File

@@ -0,0 +1,42 @@
import Node from '../core/Node.js';
class JoinNode extends Node {
constructor( nodes = [] ) {
super();
this.nodes = nodes;
}
getNodeType( builder ) {
return builder.getTypeFromLength( this.nodes.reduce( ( count, cur ) => count + builder.getTypeLength( cur.getNodeType( builder ) ), 0 ) );
}
generate( builder ) {
const type = this.getNodeType( builder );
const nodes = this.nodes;
const snippetValues = [];
for ( let i = 0; i < nodes.length; i ++ ) {
const input = nodes[ i ];
const inputSnippet = input.build( builder );
snippetValues.push( inputSnippet );
}
return `${ builder.getType( type ) }( ${ snippetValues.join( ', ' ) } )`;
}
}
export default JoinNode;

View File

@@ -0,0 +1,74 @@
import Node from '../core/Node.js';
import TimerNode from './TimerNode.js';
import { abs, fract, round, sin, add, sub, mul, PI2 } from '../ShaderNode.js';
class OscNode extends Node {
static SINE = 'sine';
static SQUARE = 'square';
static TRIANGLE = 'triangle';
static SAWTOOTH = 'sawtooth';
constructor( method = OscNode.SINE, timeNode = new TimerNode() ) {
super();
this.method = method;
this.timeNode = timeNode;
}
getNodeType( builder ) {
return this.timeNode.getNodeType( builder );
}
generate( builder ) {
const method = this.method;
const timeNode = this.timeNode;
let outputNode = null;
if ( method === OscNode.SINE ) {
outputNode = add( mul( sin( mul( add( timeNode, .75 ), PI2 ) ), .5 ), .5 );
} else if ( method === OscNode.SQUARE ) {
outputNode = round( fract( timeNode ) );
} else if ( method === OscNode.TRIANGLE ) {
outputNode = abs( sub( 1, mul( fract( add( timeNode, .5 ) ), 2 ) ) );
} else if ( method === OscNode.SAWTOOTH ) {
outputNode = fract( timeNode );
}
return outputNode.build( builder );
}
serialize( data ) {
super.serialize( data );
data.method = this.method;
}
deserialize( data ) {
super.deserialize( data );
this.method = data.method;
}
}
export default OscNode;

View File

@@ -0,0 +1,86 @@
import Node from '../core/Node.js';
import { vector } from '../core/NodeBuilder.js';
class SplitNode extends Node {
constructor( node, components = 'x' ) {
super();
this.node = node;
this.components = components;
}
getVectorLength() {
let vectorLength = this.components.length;
for ( const c of this.components ) {
vectorLength = Math.max( vector.indexOf( c ) + 1, vectorLength );
}
return vectorLength;
}
getNodeType( builder ) {
return builder.getTypeFromLength( this.components.length );
}
generate( builder ) {
const node = this.node;
const nodeTypeLength = builder.getTypeLength( node.getNodeType( builder ) );
if ( nodeTypeLength > 1 ) {
let type = null;
const componentsLength = this.getVectorLength();
if ( componentsLength >= nodeTypeLength ) {
// need expand the input node
type = builder.getTypeFromLength( this.getVectorLength() );
}
const nodeSnippet = node.build( builder, type );
return `${nodeSnippet}.${this.components}`;
} else {
// ignore components if node is a float
return node.build( builder );
}
}
serialize( data ) {
super.serialize( data );
data.components = this.components;
}
deserialize( data ) {
super.deserialize( data );
this.components = data.components;
}
}
export default SplitNode;

View File

@@ -0,0 +1,58 @@
import Node from '../core/Node.js';
import FloatNode from '../inputs/FloatNode.js';
import UVNode from '../accessors/UVNode.js';
import MathNode from '../math/MathNode.js';
import OperatorNode from '../math/OperatorNode.js';
import SplitNode from '../utils/SplitNode.js';
import JoinNode from '../utils/JoinNode.js';
class SpriteSheetUVNode extends Node {
constructor( countNode, uvNode = new UVNode(), frameNode = new FloatNode( 0 ).setConst( true ) ) {
super( 'vec2' );
this.countNode = countNode;
this.uvNode = uvNode;
this.frameNode = frameNode;
}
generate( builder ) {
const count = this.countNode;
const uv = this.uvNode;
const frame = this.frameNode;
const one = new FloatNode( 1 ).setConst( true );
const width = new SplitNode( count, 'x' );
const height = new SplitNode( count, 'y' );
const total = new OperatorNode( '*', width, height );
const roundFrame = new MathNode( MathNode.FLOOR, new MathNode( MathNode.MOD, frame, total ) );
const frameNum = new OperatorNode( '+', roundFrame, one );
const cell = new MathNode( MathNode.MOD, roundFrame, width );
const row = new MathNode( MathNode.CEIL, new OperatorNode( '/', frameNum, width ) );
const rowInv = new OperatorNode( '-', height, row );
const scale = new OperatorNode( '/', one, count );
const uvFrameOffset = new JoinNode( [
new OperatorNode( '*', cell, new SplitNode( scale, 'x' ) ),
new OperatorNode( '*', rowInv, new SplitNode( scale, 'y' ) )
] );
const uvScale = new OperatorNode( '*', uv, scale );
const uvFrame = new OperatorNode( '+', uvScale, uvFrameOffset );
return uvFrame.build( builder, this.getNodeType( builder ) );
}
}
export default SpriteSheetUVNode;

View File

@@ -0,0 +1,64 @@
import FloatNode from '../inputs/FloatNode.js';
import { NodeUpdateType } from '../core/constants.js';
class TimerNode extends FloatNode {
static LOCAL = 'local';
static GLOBAL = 'global';
static DELTA = 'delta';
constructor( scope = TimerNode.LOCAL ) {
super();
this.scope = scope;
this.scale = 1;
this.updateType = NodeUpdateType.Frame;
}
update( frame ) {
const scope = this.scope;
const scale = this.scale;
if ( scope === TimerNode.LOCAL ) {
this.value += frame.deltaTime * scale;
} else if ( scope === TimerNode.DELTA ) {
this.value = frame.deltaTime * scale;
} else {
// global
this.value = frame.time * scale;
}
}
serialize( data ) {
super.serialize( data );
data.scope = this.scope;
data.scale = this.scale;
}
deserialize( data ) {
super.deserialize( data );
this.scope = data.scope;
this.scale = data.scale;
}
}
export default TimerNode;