Skip to content

Commit

Permalink
Merge branch 'main' into extras-merge
Browse files Browse the repository at this point in the history
  • Loading branch information
kpal81xd committed Apr 15, 2024
2 parents 13aa703 + 3985cdc commit 566ab44
Show file tree
Hide file tree
Showing 28 changed files with 738 additions and 674 deletions.
11 changes: 11 additions & 0 deletions examples/src/examples/misc/gizmos/controls.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ export function controls({ observer, ReactPCUI, React, jsx, fragment }) {
binding: new BindingTwoWay(),
link: { observer, path: 'gizmo.zAxisColor' }
})
),
jsx(
LabelGroup,
{ text: 'Color Alpha' },
jsx(SliderInput, {
binding: new BindingTwoWay(),
link: { observer, path: 'gizmo.colorAlpha' },
min: 0,
max: 1,
precision: 2
})
)
),
jsx(
Expand Down
1 change: 1 addition & 0 deletions examples/src/examples/misc/gizmos/example.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class GizmoHandler {
xAxisColor: Object.values(gizmo.xAxisColor),
yAxisColor: Object.values(gizmo.yAxisColor),
zAxisColor: Object.values(gizmo.zAxisColor),
colorAlpha: gizmo.colorAlpha,
coordSpace: gizmo.coordSpace,
axisLineTolerance: gizmo.axisLineTolerance,
axisCenterTolerance: gizmo.axisCenterTolerance,
Expand Down
8 changes: 2 additions & 6 deletions src/core/preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,12 @@ class Preprocessor {
* Run c-like preprocessor on the source code, and resolves the code based on the defines and ifdefs
*
* @param {string} source - The source code to work on.
* @param {Map<string, string>} [defines] - A map containing key-value pairs of define names
* and their values. These are used for resolving #ifdef style of directives in the source.
* @param {Map<string, string>} [includes] - A map containing key-value pairs of include names
* and their content. These are used for resolving #include directives in the source.
* @param {boolean} [stripUnusedColorAttachments] - If true, strips unused color attachments.
* @returns {string|null} Returns preprocessed source code, or null in case of error.
*/
static run(source, defines, includes = new Map(), stripUnusedColorAttachments = false) {

// shallow clone as we will modify the map
defines = defines ? new Map(defines) : new Map();
static run(source, includes = new Map(), stripUnusedColorAttachments = false) {

// strips comments, handles // and many cases of /*
source = source.replace(/\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm, '$1');
Expand All @@ -65,6 +60,7 @@ class Preprocessor {
.join('\n');

// generate defines to remove unused color attachments
const defines = new Map();
if (stripUnusedColorAttachments) {

// find out how many times pcFragColorX is used (see gles3.js)
Expand Down
3 changes: 3 additions & 0 deletions src/deprecated/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ import { RigidBodyComponentSystem } from '../framework/components/rigid-body/sys
import { basisInitialize } from '../framework/handlers/basis.js';
import { LitShader } from '../scene/shader-lib/programs/lit-shader.js';

// ScriptType alias
export { Script as ScriptType } from '../framework/script/script.js';

// CORE
export const LINEBATCH_WORLD = 0;
export const LINEBATCH_OVERLAY = 1;
Expand Down
73 changes: 55 additions & 18 deletions src/extras/gizmo/transform-gizmo.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,6 @@ const FACING_EPSILON = 0.2;
const SPANLINE_SIZE = 1e3;
const ROTATE_SCALE = 900;

/**
* @param {Color} color - The color.
* @returns {Color} - The semi transparent color.
*/
const colorSemi = (color) => {
const clone = color.clone();
clone.a = 0.6;
return clone;
};

/**
* Shape axis for the line X.
*
Expand Down Expand Up @@ -93,6 +83,17 @@ export const SHAPEAXIS_XYZ = 'xyz';
*/
export const SHAPEAXIS_FACE = 'face';


/**
* Converts Color4 to Color3.
*
* @param {Color} color - Color4
* @returns {Color} - Color3
*/
function color3from4(color) {
return new Color(color.r, color.g, color.b);
}

/**
* The base class for all transform gizmos.
*
Expand Down Expand Up @@ -135,6 +136,14 @@ class TransformGizmo extends Gizmo {
*/
static EVENT_TRANSFORMEND = 'transform:end';

/**
* Internal color alpha value.
*
* @type {number}
* @private
*/
_colorAlpha = 0.6;

/**
* Internal color for meshs.
*
Expand All @@ -143,11 +152,11 @@ class TransformGizmo extends Gizmo {
*/
_meshColors = {
axis: {
x: colorSemi(COLOR_RED),
y: colorSemi(COLOR_GREEN),
z: colorSemi(COLOR_BLUE),
xyz: colorSemi(Color.WHITE),
face: colorSemi(Color.WHITE)
x: this._colorSemi(COLOR_RED),
y: this._colorSemi(COLOR_GREEN),
z: this._colorSemi(COLOR_BLUE),
xyz: this._colorSemi(Color.WHITE),
face: this._colorSemi(Color.WHITE)
},
hover: {
x: COLOR_RED.clone(),
Expand Down Expand Up @@ -428,10 +437,38 @@ class TransformGizmo extends Gizmo {
return this._meshColors.axis.z;
}

/**
* The color alpha for all axes.
*
* @type {number}
*/
set colorAlpha(value) {
this._colorAlpha = math.clamp(value, 0, 1);

this._meshColors.axis.x.copy(this._colorSemi(this._meshColors.axis.x));
this._meshColors.axis.y.copy(this._colorSemi(this._meshColors.axis.y));
this._meshColors.axis.z.copy(this._colorSemi(this._meshColors.axis.z));
this._meshColors.axis.xyz.copy(this._colorSemi(this._meshColors.axis.xyz));
this._meshColors.axis.face.copy(this._colorSemi(this._meshColors.axis.face));
}

get colorAlpha() {
return this._colorAlpha;
}

_colorSemi(color) {
const clone = color.clone();
clone.a = this._colorAlpha;
return clone;
}

_updateAxisColor(axis, value) {
this._guideColors[axis].copy(value);
this._meshColors.axis[axis].copy(colorSemi(value));
this._meshColors.hover[axis].copy(value);
const color3 = color3from4(value);
const color4 = this._colorSemi(value);

this._guideColors[axis].copy(color3);
this._meshColors.axis[axis].copy(color4);
this._meshColors.hover[axis].copy(color3);

for (const name in this._shapes) {
this._shapes[name].hover(!!this._hoverAxis);
Expand Down
44 changes: 22 additions & 22 deletions src/framework/components/script/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { Entity } from '../../entity.js';
*/
class ScriptComponent extends Component {
/**
* Fired when a {@link ScriptType} instance is created and attached to the script component.
* Fired when a {@link Script} instance is created and attached to the script component.
* This event is available in two forms. They are as follows:
*
* 1. `create` - Fired when a script instance is created. The name of the script type and the
Expand All @@ -40,7 +40,7 @@ class ScriptComponent extends Component {
static EVENT_CREATE = 'create';

/**
* Fired when a {@link ScriptType} instance is destroyed and removed from the script component.
* Fired when a {@link Script} instance is destroyed and removed from the script component.
* This event is available in two forms. They are as follows:
*
* 1. `destroy` - Fired when a script instance is destroyed. The name of the script type and
Expand Down Expand Up @@ -109,7 +109,7 @@ class ScriptComponent extends Component {
static EVENT_STATE = 'state';

/**
* Fired when the index of a {@link ScriptType} instance is changed in the script component.
* Fired when the index of a {@link Script} instance is changed in the script component.
* This event is available in two forms. They are as follows:
*
* 1. `move` - Fired when a script instance is moved. The name of the script type, the script
Expand All @@ -130,7 +130,7 @@ class ScriptComponent extends Component {
static EVENT_MOVE = 'move';

/**
* Fired when a {@link ScriptType} instance had an exception. The handler is passed the script
* Fired when a {@link Script} instance had an exception. The handler is passed the script
* instance, the exception and the method name that the exception originated from.
*
* @event
Expand All @@ -154,7 +154,7 @@ class ScriptComponent extends Component {
/**
* Holds all script instances for this component.
*
* @type {import('../../script/script-type.js').ScriptType[]}
* @type {import('../../script/script.js').Script[]}
* @private
*/
this._scripts = [];
Expand Down Expand Up @@ -191,7 +191,7 @@ class ScriptComponent extends Component {
* An array of all script instances attached to an entity. This array is read-only and should
* not be modified by developer.
*
* @type {import('../../script/script-type.js').ScriptType[]}
* @type {import('../../script/script.js').Script[]}
*/
set scripts(value) {
this._scriptsData = value;
Expand Down Expand Up @@ -546,8 +546,8 @@ class ScriptComponent extends Component {
/**
* Detect if script is attached to an entity.
*
* @param {string|typeof import('../../script/script-type.js').ScriptType} nameOrType - The
* name or type of {@link ScriptType}.
* @param {string|typeof import('../../script/script.js').Script} nameOrType - The
* name or type of {@link Script}.
* @returns {boolean} If script is attached to an entity.
* @example
* if (entity.script.has('playerController')) {
Expand All @@ -570,9 +570,9 @@ class ScriptComponent extends Component {
/**
* Get a script instance (if attached).
*
* @param {string|typeof import('../../script/script-type.js').ScriptType} nameOrType - The
* name or type of {@link ScriptType}.
* @returns {import('../../script/script-type.js').ScriptType|null} If script is attached, the
* @param {string|typeof import('../../script/script.js').Script} nameOrType - The
* name or type of {@link Script}.
* @returns {import('../../script/script.js').Script|null} If script is attached, the
* instance is returned. Otherwise null is returned.
* @example
* const controller = entity.script.get('playerController');
Expand All @@ -594,8 +594,8 @@ class ScriptComponent extends Component {
/**
* Create a script instance and attach to an entity script component.
*
* @param {string|typeof import('../../script/script-type.js').ScriptType} nameOrType - The
* name or type of {@link ScriptType}.
* @param {string|typeof import('../../script/script.js').Script} nameOrType - The
* name or type of {@link Script}.
* @param {object} [args] - Object with arguments for a script.
* @param {boolean} [args.enabled] - If script instance is enabled after creation. Defaults to
* true.
Expand All @@ -605,9 +605,9 @@ class ScriptComponent extends Component {
* script and attributes must be initialized manually. Defaults to false.
* @param {number} [args.ind] - The index where to insert the script instance at. Defaults to
* -1, which means append it at the end.
* @returns {import('../../script/script-type.js').ScriptType|null} Returns an instance of a
* {@link ScriptType} if successfully attached to an entity, or null if it failed because a
* script with a same name has already been added or if the {@link ScriptType} cannot be found
* @returns {import('../../script/script.js').Script|null} Returns an instance of a
* {@link Script} if successfully attached to an entity, or null if it failed because a
* script with a same name has already been added or if the {@link Script} cannot be found
* by name in the {@link ScriptRegistry}.
* @example
* entity.script.create('playerController', {
Expand Down Expand Up @@ -699,8 +699,8 @@ class ScriptComponent extends Component {
/**
* Destroy the script instance that is attached to an entity.
*
* @param {string|typeof import('../../script/script-type.js').ScriptType} nameOrType - The
* name or type of {@link ScriptType}.
* @param {string|typeof import('../../script/script.js').Script} nameOrType - The
* name or type of {@link Script}.
* @returns {boolean} If it was successfully destroyed.
* @example
* entity.script.destroy('playerController');
Expand Down Expand Up @@ -756,8 +756,8 @@ class ScriptComponent extends Component {
/**
* Swap the script instance.
*
* @param {string|typeof import('../../script/script-type.js').ScriptType} nameOrType - The
* name or type of {@link ScriptType}.
* @param {string|typeof import('../../script/script.js').Script} nameOrType - The
* name or type of {@link Script}.
* @returns {boolean} If it was successfully swapped.
* @private
*/
Expand Down Expand Up @@ -927,8 +927,8 @@ class ScriptComponent extends Component {
/**
* Move script instance to different position to alter update order of scripts within entity.
*
* @param {string|typeof import('../../script/script-type.js').ScriptType} nameOrType - The
* name or type of {@link ScriptType}.
* @param {string|typeof import('../../script/script.js').Script} nameOrType - The
* name or type of {@link Script}.
* @param {number} ind - New position index.
* @returns {boolean} If it was successfully moved.
* @example
Expand Down
8 changes: 4 additions & 4 deletions src/framework/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,8 @@ class Entity extends GraphNode {
/**
* Search the entity and all of its descendants for the first script instance of specified type.
*
* @param {string|typeof import('./script/script-type.js').ScriptType} nameOrType - The name or type of {@link ScriptType}.
* @returns {import('./script/script-type.js').ScriptType|undefined} A script instance of specified type, if the entity or any of its descendants
* @param {string|typeof import('./script/script.js').Script} nameOrType - The name or type of {@link Script}.
* @returns {import('./script/script.js').Script|undefined} A script instance of specified type, if the entity or any of its descendants
* has one. Returns undefined otherwise.
* @example
* // Get the first found "playerController" instance in the hierarchy tree that starts with this entity
Expand All @@ -415,8 +415,8 @@ class Entity extends GraphNode {
/**
* Search the entity and all of its descendants for all script instances of specified type.
*
* @param {string|typeof import('./script/script-type.js').ScriptType} nameOrType - The name or type of {@link ScriptType}.
* @returns {import('./script/script-type.js').ScriptType[]} All script instances of specified type in the entity or any of its
* @param {string|typeof import('./script/script.js').Script} nameOrType - The name or type of {@link Script}.
* @returns {import('./script/script.js').Script[]} All script instances of specified type in the entity or any of its
* descendants. Returns empty array if none found.
* @example
* // Get all "playerController" instances in the hierarchy tree that starts with this entity
Expand Down
6 changes: 3 additions & 3 deletions src/framework/handlers/script.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { platform } from '../../core/platform.js';
import { script } from '../script.js';
import { ScriptType } from '../script/script-type.js';
import { Script } from '../script/script.js';
import { ScriptTypes } from '../script/script-types.js';
import { registerScript } from '../script/script.js';
import { registerScript } from '../script/script-create.js';
import { ResourceLoader } from './loader.js';

import { ResourceHandler } from './handler.js';
Expand Down Expand Up @@ -154,7 +154,7 @@ class ScriptHandler extends ResourceHandler {

for (const key in module) {
const scriptClass = module[key];
const extendsScriptType = scriptClass.prototype instanceof ScriptType;
const extendsScriptType = scriptClass.prototype instanceof Script;

if (extendsScriptType) {

Expand Down
6 changes: 3 additions & 3 deletions src/framework/script/script-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,16 @@ function rawToValue(app, args, value, old) {

/**
* Container of Script Attribute definitions. Implements an interface to add/remove attributes and
* store their definition for a {@link ScriptType}. Note: An instance of ScriptAttributes is
* created automatically by each {@link ScriptType}.
* store their definition for a {@link Script}. Note: An instance of ScriptAttributes is
* created automatically by each {@link Script}.
*
* @category Script
*/
class ScriptAttributes {
/**
* Create a new ScriptAttributes instance.
*
* @param {typeof import('./script-type.js').ScriptType} scriptType - Script Type that attributes relate to.
* @param {typeof import('./script.js').Script} scriptType - Script Type that attributes relate to.
*/
constructor(scriptType) {
this.scriptType = scriptType;
Expand Down
Loading

0 comments on commit 566ab44

Please sign in to comment.