Skip to content

Commit

Permalink
Fixed grid enabled/disabled (playcanvas#7374)
Browse files Browse the repository at this point in the history
* Fixed grid enabled/disabled

* Added in cast shadows option when creating render entity

* Removes render component on destroy if script added it
  • Loading branch information
kpal81xd authored Feb 24, 2025
1 parent 79dda04 commit 1a317d9
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions scripts/esm/grid.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import {
Vec2
} from 'playcanvas';

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

const tmpVa = new Vec2();

const EPISILON = 1e-3;
Expand Down Expand Up @@ -139,6 +137,12 @@ class Grid extends Script {
*/
static RESOLUTION_HIGH = 2;

/**
* @type {boolean}
* @private
*/
_addedRender = false;

/**
* @type {ShaderMaterial}
* @private
Expand Down Expand Up @@ -179,7 +183,10 @@ class Grid extends Script {

// ensure the entity has a render component
if (!this.entity.render) {
this.entity.addComponent('render');
this.entity.addComponent('render', {
castShadows: false
});
this._addedRender = true;
}

// create shader material
Expand Down Expand Up @@ -222,11 +229,13 @@ class Grid extends Script {

// enable/disable the mesh instance
this.on('enable', () => {
this.entity.render.meshInstances = [this._meshInstance];
this._meshInstance.visible = true;
});
this.on('disable', () => {
this.entity.render.meshInstances = [];
this._meshInstance.visible = false;
});

this.on('destroy', this.destroy, this);
}

/**
Expand Down Expand Up @@ -320,6 +329,14 @@ class Grid extends Script {
get resolution() {
return this._resolution;
}

destroy() {
this.entity.render.meshInstances = [];

if (this._addedRender) {
this.entity.removeComponent('render');
}
}
}

export { Grid };

0 comments on commit 1a317d9

Please sign in to comment.