polygonjs-engine
Version:
node-based webgl 3D engine https://polygonjs.com
288 lines (269 loc) • 11.1 kB
text/typescript
import {ParamInitValuesTypeMap} from '../../../../params/types/ParamInitValuesTypeMap';
import {ParamType} from '../../../../poly/ParamType';
import {RampParam} from '../../../../params/Ramp';
// import {BaseTypedConnection} from './_Base';
// TODO: simplify GLDataType into one enum
// http://learnwebgl.brown37.net/12_shader_language/glsl_data_types.html
//
//
// GL Data types
//
//
export enum GlConnectionPointType {
BOOL = 'bool',
// BVEC2 = 'bvec2',
// BVEC3 = 'bvec3',
// BVEC4 = 'bvec4',
INT = 'int',
// IVEC2 = 'ivec2',
// IVEC3 = 'ivec3',
// IVEC4 = 'ivec4',
FLOAT = 'float',
VEC2 = 'vec2',
VEC3 = 'vec3',
VEC4 = 'vec4',
// matrices to be used later
// MAT2 = 'mat2',
// MAT3 = 'mat3',
// MAT4 = 'mat4',
SAMPLER_2D = 'sampler2D',
}
// interface IGlConnectionPointType {
// [EnumGlConnectionPointType.BOOL]: Readonly<'bool'>;
// // BVEC2 = 'bvec2',
// // BVEC3 = 'bvec3',
// // BVEC4 = 'bvec4',
// [EnumGlConnectionPointType.INT]: Readonly<'int'>;
// // IVEC2 = 'ivec2',
// // IVEC3 = 'ivec3',
// // IVEC4 = 'ivec4',
// [EnumGlConnectionPointType.FLOAT]: Readonly<'float'>;
// [EnumGlConnectionPointType.VEC2]: Readonly<'vec2'>;
// [EnumGlConnectionPointType.VEC3]: Readonly<'vec3'>;
// [EnumGlConnectionPointType.VEC4]: Readonly<'vec4'>;
// // matrices to be used later
// // MAT2 = 'mat2',
// // MAT3 = 'mat3',
// // MAT4 = 'mat4',
// [EnumGlConnectionPointType.SAMPLER_2D]: Readonly<'sampler2D'>;
// }
// export const GlConnectionPointType: IGlConnectionPointType = {
// [EnumGlConnectionPointType.BOOL]: 'bool',
// // BVEC2 = 'bvec2',
// // BVEC3 = 'bvec3',
// // BVEC4 = 'bvec4',
// [EnumGlConnectionPointType.INT]: 'int',
// // IVEC2 = 'ivec2',
// // IVEC3 = 'ivec3',
// // IVEC4 = 'ivec4',
// [EnumGlConnectionPointType.FLOAT]: 'float',
// [EnumGlConnectionPointType.VEC2]: 'vec2',
// [EnumGlConnectionPointType.VEC3]: 'vec3',
// [EnumGlConnectionPointType.VEC4]: 'vec4',
// // matrices to be used later
// // MAT2 = 'mat2',
// // MAT3 = 'mat3',
// // MAT4 = 'mat4',
// [EnumGlConnectionPointType.SAMPLER_2D]: 'sampler2D',
// };
//
//
// ALL GL Data types in an array
//
//
export const GL_CONNECTION_POINT_TYPES: Array<GlConnectionPointType> = [
GlConnectionPointType.BOOL,
// ConnectionPointType.BVEC2,
// ConnectionPointType.BVEC3,
// ConnectionPointType.BVEC4,
GlConnectionPointType.INT,
// ConnectionPointType.IVEC2,
// ConnectionPointType.IVEC3,
// ConnectionPointType.IVEC4,
GlConnectionPointType.FLOAT,
GlConnectionPointType.VEC2,
GlConnectionPointType.VEC3,
GlConnectionPointType.VEC4,
];
//
//
// Map to convert from a GL Data type to a ParamType
//
//
type ConnectionPointTypeToParamTypeMapGeneric = {[key in GlConnectionPointType]: ParamType};
export interface IConnectionPointTypeToParamTypeMap extends ConnectionPointTypeToParamTypeMapGeneric {
[]: ParamType.BOOLEAN;
// [ConnectionPointType.BVEC2]: [ParamType.BOOLEAN, ParamType.BOOLEAN]
// [ConnectionPointType.BVEC3]: [ParamType.BOOLEAN, ParamType.BOOLEAN, ParamType.BOOLEAN]
// [ConnectionPointType.BVEC4]: [ParamType.BOOLEAN, ParamType.BOOLEAN, ParamType.BOOLEAN, ParamType.BOOLEAN]
[]: ParamType.INTEGER;
// [ConnectionPointType.IVEC2]: [ParamType.INTEGER, ParamType.INTEGER];
// [ConnectionPointType.IVEC3]: [ParamType.INTEGER, ParamType.INTEGER, ParamType.INTEGER];
// [ConnectionPointType.IVEC4]: [ParamType.INTEGER, ParamType.INTEGER, ParamType.INTEGER, ParamType.INTEGER];
[]: ParamType.FLOAT;
[]: ParamType.VECTOR2;
[]: ParamType.VECTOR3;
[]: ParamType.VECTOR4;
}
export const GlConnectionPointTypeToParamTypeMap: IConnectionPointTypeToParamTypeMap = {
[]: ParamType.BOOLEAN,
// [ConnectionPointType.BVEC2]: [ParamType.BOOLEAN, ParamType.BOOLEAN]
// [ConnectionPointType.BVEC3]: [ParamType.BOOLEAN, ParamType.BOOLEAN, ParamType.BOOLEAN]
// [ConnectionPointType.BVEC4]: [ParamType.BOOLEAN, ParamType.BOOLEAN, ParamType.BOOLEAN, ParamType.BOOLEAN]
[]: ParamType.INTEGER,
// [ConnectionPointType.IVEC2]: [ParamType.INTEGER, ParamType.INTEGER];
// [ConnectionPointType.IVEC3]: [ParamType.INTEGER, ParamType.INTEGER, ParamType.INTEGER];
// [ConnectionPointType.IVEC4]: [ParamType.INTEGER, ParamType.INTEGER, ParamType.INTEGER, ParamType.INTEGER];
[]: ParamType.FLOAT,
[]: ParamType.VECTOR2,
[]: ParamType.VECTOR3,
[]: ParamType.VECTOR4,
[]: ParamType.RAMP,
};
//
//
// Map to convert from a ParamType to GL Data type
//
//
type GLParamTypeToConnectionPointTypeMapGeneric = {[key in ParamType]: GlConnectionPointType | undefined};
export interface IGLParamTypeToConnectionPointTypeMap extends GLParamTypeToConnectionPointTypeMapGeneric {
[]: GlConnectionPointType.BOOL;
[]: GlConnectionPointType.VEC3;
// [ConnectionPointType.BVEC2]: [ParamType.BOOLEAN, ParamType.BOOLEAN]
// [ConnectionPointType.BVEC3]: [ParamType.BOOLEAN, ParamType.BOOLEAN, ParamType.BOOLEAN]
// [ConnectionPointType.BVEC4]: [ParamType.BOOLEAN, ParamType.BOOLEAN, ParamType.BOOLEAN, ParamType.BOOLEAN]
[]: GlConnectionPointType.INT;
// [ConnectionPointType.IVEC2]: [ParamType.INTEGER, ParamType.INTEGER];
// [ConnectionPointType.IVEC3]: [ParamType.INTEGER, ParamType.INTEGER, ParamType.INTEGER];
// [ConnectionPointType.IVEC4]: [ParamType.INTEGER, ParamType.INTEGER, ParamType.INTEGER, ParamType.INTEGER];
[]: GlConnectionPointType.FLOAT;
[]: undefined;
[]: GlConnectionPointType.VEC2;
[]: GlConnectionPointType.VEC3;
[]: GlConnectionPointType.VEC4;
[]: undefined;
[]: undefined;
[]: undefined;
[]: undefined;
[]: undefined;
[]: undefined;
[]: undefined;
}
export const GLParamTypeToConnectionPointTypeMap: IGLParamTypeToConnectionPointTypeMap = {
[]: GlConnectionPointType.BOOL,
[]: GlConnectionPointType.VEC3,
// [ConnectionPointType.BVEC2]: [ParamType.BOOLEAN, ParamType.BOOLEAN]
// [ConnectionPointType.BVEC3]: [ParamType.BOOLEAN, ParamType.BOOLEAN, ParamType.BOOLEAN]
// [ConnectionPointType.BVEC4]: [ParamType.BOOLEAN, ParamType.BOOLEAN, ParamType.BOOLEAN, ParamType.BOOLEAN]
[]: GlConnectionPointType.INT,
// [ConnectionPointType.IVEC2]: [ParamType.INTEGER, ParamType.INTEGER];
// [ConnectionPointType.IVEC3]: [ParamType.INTEGER, ParamType.INTEGER, ParamType.INTEGER];
// [ConnectionPointType.IVEC4]: [ParamType.INTEGER, ParamType.INTEGER, ParamType.INTEGER, ParamType.INTEGER];
[]: GlConnectionPointType.FLOAT,
[]: undefined,
[]: GlConnectionPointType.VEC2,
[]: GlConnectionPointType.VEC3,
[]: GlConnectionPointType.VEC4,
[]: undefined,
[]: undefined,
[]: undefined,
[]: undefined,
[]: undefined,
[]: undefined,
[]: undefined,
};
//
//
// Map of GL Data type default values
//
//
export type ConnectionPointInitValueMapGeneric = {
[]: ParamInitValuesTypeMap[IConnectionPointTypeToParamTypeMap[key]];
};
export const GlConnectionPointInitValueMap: ConnectionPointInitValueMapGeneric = {
[]: false,
// [ConnectionPointType.BVEC2]: [ParamType.BOOLEAN, ParamType.BOOLEAN]
// [ConnectionPointType.BVEC3]: [ParamType.BOOLEAN, ParamType.BOOLEAN, ParamType.BOOLEAN]
// [ConnectionPointType.BVEC4]: [ParamType.BOOLEAN, ParamType.BOOLEAN, ParamType.BOOLEAN, ParamType.BOOLEAN]
[]: 0,
// [ConnectionPointType.IVEC2]: [ParamType.INTEGER, ParamType.INTEGER];
// [ConnectionPointType.IVEC3]: [ParamType.INTEGER, ParamType.INTEGER, ParamType.INTEGER];
// [ConnectionPointType.IVEC4]: [ParamType.INTEGER, ParamType.INTEGER, ParamType.INTEGER, ParamType.INTEGER];
[]: 0,
[]: [0, 0],
[]: [0, 0, 0],
[]: [0, 0, 0, 0],
[]: RampParam.DEFAULT_VALUE_JSON,
};
//
//
// Map of GL Data type component counts
//
//
export type ConnectionPointComponentsCountMapGeneric = {
[]: number;
};
export const GlConnectionPointComponentsCountMap: ConnectionPointComponentsCountMapGeneric = {
[]: 1,
// [ConnectionPointType.BVEC2]: [ParamType.BOOLEAN, ParamType.BOOLEAN]
// [ConnectionPointType.BVEC3]: [ParamType.BOOLEAN, ParamType.BOOLEAN, ParamType.BOOLEAN]
// [ConnectionPointType.BVEC4]: [ParamType.BOOLEAN, ParamType.BOOLEAN, ParamType.BOOLEAN, ParamType.BOOLEAN]
[]: 1,
// [ConnectionPointType.IVEC2]: [ParamType.INTEGER, ParamType.INTEGER];
// [ConnectionPointType.IVEC3]: [ParamType.INTEGER, ParamType.INTEGER, ParamType.INTEGER];
// [ConnectionPointType.IVEC4]: [ParamType.INTEGER, ParamType.INTEGER, ParamType.INTEGER, ParamType.INTEGER];
[]: 1,
[]: 2,
[]: 3,
[]: 4,
[]: 1,
};
// import {
// ConnectionPointType,
// ConnectionPointInitValueMapGeneric,
// ConnectionPointInitValueMap,
// ConnectionPointTypeToParamTypeMap,
// IConnectionPointTypeToParamTypeMap,
// } from '../ConnectionPointType';
// import {ParamInitValuesTypeMap} from '../params/ParamsController';
export interface GlConnectionPointData<T extends GlConnectionPointType> {
name: string;
type: T;
}
import {BaseConnectionPoint} from './_Base';
export class GlConnectionPoint<T extends GlConnectionPointType> extends BaseConnectionPoint {
protected _json: GlConnectionPointData<T> | undefined;
// protected _init_value: any;
constructor(
protected _name: string,
protected _type: T,
protected _init_value?: ConnectionPointInitValueMapGeneric[T]
) {
super(_name, _type);
// if (this._init_value === undefined) {
this._init_value = this._init_value || GlConnectionPointInitValueMap[this._type];
// }
}
type() {
return this._type;
}
are_types_matched(src_type: string, dest_type: string): boolean {
return src_type == dest_type;
}
get param_type(): IConnectionPointTypeToParamTypeMap[T] {
return GlConnectionPointTypeToParamTypeMap[this._type];
}
get init_value() {
return this._init_value;
}
toJSON(): GlConnectionPointData<T> {
return (this._json = this._json || this._create_json());
}
protected _create_json(): GlConnectionPointData<T> {
return {
name: this._name,
type: this._type,
};
}
}
export type BaseGlConnectionPoint = GlConnectionPoint<GlConnectionPointType>;