mson-three/src/mson/mson.type.ts

278 lines
6.3 KiB
TypeScript

import { PartialBy } from '../util/deep-partial.type';
export interface MsonTexture {
w?: number;
h?: number;
u?: number;
v?: number;
}
export type MsonOperand =
| '+' /* ADD */
| '-' /* SUBTRACT */
| '*' /* MULTIPLY */
| '/' /* DIVIDE */
| '%' /* MODULUS */
| '^' /* EXPONENT */;
export type MsonVariable = string | number;
export type Vec3 = [number, number, number];
export type Vec2 = [number, number];
export type MsonVec3 = Vec3 | [MsonVariable, MsonVariable, MsonVariable];
export type MsonVec2 = Vec2 | [MsonVariable, MsonVariable];
/**
* Mson allows you to use variables and do basic calculations within the model file.
* Unless otherwise stated, it can be assumed that every place where a numerical value
* is expected can also accept a reference to a variable (see below).
*
* Expressions with calculations can only be done in the `local` blocks where veriables are defined.
*/
export type MsonExpression = [MsonVariable, MsonOperand, MsonVariable];
export type MsonLocals = Record<string, MsonVariable | MsonExpression>;
export type MsonEvaluatedLocals = Record<string, number>;
export type MsonComponentType =
| 'mson:compound'
| 'mson:box'
| 'mson:plane'
| 'mson:planar'
| 'mson:slot'
| 'mson:cone'
| 'mson:quads'
| string;
export interface MsonBaseComponent {
type?: MsonComponentType;
/**
* Whether or not this part is visible. You shouldn't have to use this in most circumstances.
* @default true
*/
visible?: boolean;
/**
* The XYZ center of rotation of this part.
*/
pivot?: MsonVec3;
/**
* The XYZ rotation angle of this part, given in degrees.
*/
rotate?: MsonVec3;
texture?: MsonTexture;
/**
* A map of child components to load as part of this one.
*/
children?: MsonData;
__comment?: any;
}
export interface MsonBox extends MsonBaseComponent {
type: 'mson:box';
/**
* The relative XYZ position of the box
*/
from: MsonVec3;
/**
* The width, height, and depth of the box
*/
size: MsonVec3;
/**
* Part-specific dilation applied to any cubes created by components defined by this part.
*/
dilate?: Vec3 | number;
}
export interface MsonCompound extends MsonBaseComponent {
type: 'mson:compound';
dilate?: MsonVec3;
/**
* Whether to flip this part's textures.
* @default false
*/
mirror?: boolean | MsonVec3;
/**
* default type for components (if omitted): mson:box
* allowed types: <mson:box|mson:cone|mson:link|mson:quads|mson:plane|mson:slot>
*/
cubes?: MsonCompoundComponent[];
}
export type MsonFace = 'up' | 'down' | 'east' | 'west' | 'south' | 'north';
export const MsonFaces: MsonFace[] = [
'up',
'down',
'east',
'west',
'south',
'north',
];
export type MsonPlanarXYZWH = [
MsonVariable,
MsonVariable,
MsonVariable,
MsonVariable,
MsonVariable,
];
export type MsonPlanarXYZWHUV = [
MsonVariable,
MsonVariable,
MsonVariable,
MsonVariable,
MsonVariable,
MsonVariable,
MsonVariable,
];
export type MsonPlanarXYZWHUVXY = [
MsonVariable,
MsonVariable,
MsonVariable,
MsonVariable,
MsonVariable,
MsonVariable,
MsonVariable,
boolean,
boolean,
];
/**
* First 3 are the position,
* Next two are the size,
* Last two are an optional texture coordinate. If not given, the parent's texture will be used.
*
* Two booleans indicate whether to flip the texture vertically and horizontally.
*/
export type MsonPlanarPlane =
| MsonPlanarXYZWH
| MsonPlanarXYZWHUV
| MsonPlanarXYZWHUVXY;
export interface MsonPlanar extends MsonBaseComponent {
type: 'mson:planar';
dilate?: MsonVec3;
/**
* Whether to flip this part's textures.
* @default false
*/
mirror?: boolean | MsonVec3;
up?: MsonPlanarPlane;
down?: MsonPlanarPlane;
east?: MsonPlanarPlane;
west?: MsonPlanarPlane;
south?: MsonPlanarPlane;
north?: MsonPlanarPlane;
}
export interface MsonPlane extends MsonBaseComponent {
type: 'mson:plane';
face?: MsonFace;
position?: MsonVec3;
size?: MsonVec2;
mirror?: [boolean, boolean];
dilate?: MsonVec3;
}
export interface MsonSlot extends MsonBaseComponent {
type: 'mson:slot';
implementation: string;
name: string;
data: MsonData | string;
locals?: MsonLocals;
}
export interface MsonCone extends MsonBaseComponent {
type: 'mson:cone';
size?: MsonVec3;
from?: MsonVec3;
dilate?: MsonVariable;
taper?: MsonVariable;
}
export interface MsonQuads extends MsonBaseComponent {
type: 'mson:quads';
// TODO: dunno
}
export type MsonComponent =
| MsonBox
| PartialBy<MsonCompound, 'type'>
| MsonPlanar
| MsonPlane
| MsonSlot
| MsonCone
| MsonQuads;
export type MsonCompoundComponent =
| PartialBy<MsonBox, 'type'>
| MsonCone
| MsonQuads
| MsonPlane
| MsonSlot;
export type MsonData = Record<MsonComponentType, MsonComponent>;
export interface MsonModel {
/**
* The parent file to extend from.
*/
parent?: MsonComponentType;
/**
* Texture definition specifying the default width and height for the file.
* This property is inherited by all components defined in this file
* and defining it will function similarly to defining variables in the locals block.
*/
texture?: MsonTexture;
/**
* The default growth (dilation) applied to all boxes defined by components in this file.
* Note that components may have their own dilation parameter, in which instance that value is *added*
* to this one
*
* ** DOES NOT SUPPORT VARIABLES **
*/
dilate?: Vec3;
/**
* A block of local variables that may be references by components within the data block.
* Values defined in here are typically applied over what is inherited from the parent file (if specified)
* and become available for use by the parent's components as well.
*/
locals?: MsonLocals;
/**
* default type for components (if omitted): mson:compound
* allowed types: <mson:compound|mson:slot|mson:link|mson:planar>
*/
data: MsonData;
}
export interface MsonEvaluatedModel extends MsonModel {
name: string;
/**
* Combined locals from whole dependency tree
*/
_locals?: MsonLocals;
/**
* Evaluated locals
*/
_localsEvaluated?: MsonEvaluatedLocals;
/**
* Combined data from whole dependency tree
*/
_data?: MsonData;
/**
* Evaluated data with evaluated locals
*/
_dataEvaluated?: MsonData;
/**
* Evaluated parent
*/
_parent?: MsonEvaluatedModel;
}