Skip to content

Commit

Permalink
Refactor Grid class initialization and set initial values (#7287)
Browse files Browse the repository at this point in the history
* Refactor Grid class initialization and set initial values

* add missing type to _set
  • Loading branch information
marklundin authored Jan 16, 2025
1 parent 6143472 commit 55a557d
Showing 1 changed file with 10 additions and 22 deletions.
32 changes: 10 additions & 22 deletions scripts/esm/grid.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@ import {

/** @import { AppBase, Entity } from 'playcanvas' */

/**
* @typedef {object} ScriptArgs
* @property {AppBase} app - The app.
* @property {Entity} entity - The entity.
* @property {boolean} [enabled] - The enabled state.
* @property {object} [attributes] - The attributes.
*/

const tmpVa = new Vec2();

const EPISILON = 1e-3;
Expand Down Expand Up @@ -183,16 +175,7 @@ class Grid extends Script {
*/
_resolution = Grid.RESOLUTION_HIGH;

/**
* @param {ScriptArgs} args - The arguments.
*/
constructor(args) {
super(args);
const {
colorX,
colorZ,
resolution
} = args.attributes;
initialize() {

// ensure the entity has a render component
if (!this.entity.render) {
Expand All @@ -219,9 +202,9 @@ class Grid extends Script {
this.entity.render.meshInstances = [this._meshInstance];

// set the initial values
this.colorX = colorX ?? this.colorX;
this.colorZ = colorZ ?? this.colorZ;
this.resolution = resolution ?? this.resolution;
this.colorX = this._colorX;
this.colorZ = this._colorZ;
this.resolution = this._resolution;

// calculate half extents
this._set('uHalfExtents', this._calcHalfExtents(tmpVa));
Expand Down Expand Up @@ -258,10 +241,15 @@ class Grid extends Script {

/**
* @param {string} name - The name of the parameter.
* @param {Color|Vec2} value - The value of the parameter.
* @param {Color|Vec2|number} value - The value of the parameter.
* @private
*/
_set(name, value) {

if (!this._material) {
return;
}

if (value instanceof Color) {
this._material.setParameter(name, [value.r, value.g, value.b]);
}
Expand Down

0 comments on commit 55a557d

Please sign in to comment.