I’m trying to work with node.js and play canvas engine. Got very confused today because when I was trying to pass in an asset into an ES6 class, it reported bugs (actually the value passed in is null). However, when I define the type of variable into a string, it can be passed in normally. WTF??
Here is my code.
import * as pc from 'playcanvas';
export default class MfGrid extends pc.ScriptType{
initialize() {
let material = this.entity.model.meshInstances[0].material;
let shader = [
"#ifdef MAPFLOAT\n" +
"uniform float material_opacity;\n" +
"#endif\n" +
"\n" +
"#ifdef MAPTEXTURE\n" +
"uniform float time;\n" +
"uniform float scale;\n" +
"uniform sampler2D texture_opacityMap;\n" +
"uniform sampler2D mask1;\n" +
"uniform sampler2D mask2;\n" +
"uniform sampler2D mask3;\n" +
"uniform sampler2D mask4;\n" +
"uniform float map_scale;\n" +
"uniform float panner_x;\n" +
"uniform float panner_y;\n" +
"uniform float intensity;\n" +
"#endif\n" +
"\n" +
"void getOpacity() {\n" +
" dAlpha = 1.0;\n" +
"\n" +
" #ifdef MAPFLOAT\n" +
" dAlpha *= material_opacity;\n" +
" #endif\n" +
"\n" +
" #ifdef MAPTEXTURE\n" +
" vec2 scale_offset = vec2(0.5,0.5);\n" +
" vec2 time_offset = vec2(time, 0);\n" +
" vec2 scaleUV = $UV*scale + scale_offset - scale_offset * scale;\n" +
" vec2 mapUV = $UV*map_scale + scale_offset - scale_offset * map_scale;\n" +
" vec2 panner = vec2(panner_x, panner_y);\n" +
" vec4 main_panner = texture2D(texture_opacityMap, mapUV + time_offset);\n" +
" vec4 mask1_fixed = texture2D(mask1, scaleUV);\n" +
" vec4 mask2_fixed = texture2D(mask2, scaleUV);\n" +
" vec4 mask2_teleport = texture2D(mask2, scaleUV + panner);\n" +
" vec4 mask3_fixed = texture2D(mask3, mapUV);\n" +
" vec4 mask4_fixed = texture2D(mask4, mapUV);\n" +
"\n" +
" vec4 grid_fixed = intensity * main_panner * mask1_fixed * 10.0 * mask2_fixed * mask2_fixed;\n" +
" vec4 grid_teleport = (intensity * (1.0 - main_panner) * mask2_teleport * 0.1);// * mask2_teleport;\n" +
" \n" +
" vec4 opacity = (grid_fixed + grid_teleport) * mask3_fixed * mask4_fixed;\n" +
" dAlpha *= opacity.$CH;\n" +
" //dAlpha *= texture2D(texture_opacityMap, $UV).$CH;\n" +
" #endif\n" +
"\n" +
" #ifdef MAPVERTEX\n" +
" dAlpha *= clamp(vVertexColor.$VC, 0.0, 1.0);\n" +
" #endif\n" +
"}"
].join("\n");
material.chunks[this.chunkName + 'PS'] = shader;
material.setParameter('mask1', this.mask1.resource);
material.setParameter('mask2', this.mask2.resource);
material.setParameter('mask3', this.mask3.resource);
material.setParameter('mask4', this.mask4.resource);
material.update();
this.timer = 0;
this.second = -1;
this.second2 = -1;
this.isFadeOut = false;
this.intensity = 0;
}
update(dt) {
this.timer += dt;
let material = this.entity.model.meshInstances[0].material;
material.setParameter('scale', this.scale);
material.setParameter('map_scale', this.map_scale);
material.setParameter('time', this.timer * this.speed);
if (parseInt(this.timer*this.teleport_freq) !== this.second & this.isFadeOut === false){
let panner = new pc.Vec2(Math.floor(Math.random() * 16)*0.0625,Math.floor(Math.random() * 16)*0.0625);
material.setParameter('panner_x',panner.x);
material.setParameter('panner_y',panner.y);
this.isFadeOut = false;
++this.second;
}
else if (parseInt(this.timer*this.fadeout_freq) !== this.second2){
let panner = new pc.Vec2(Math.floor(Math.random() * 16)*0.0625,Math.floor(Math.random() * 16)*0.0625);
material.setParameter('panner_x',panner.x);
material.setParameter('panner_y',panner.y);
this.isFadeOut = !this.isFadeOut;
++this.second2;
}
if (this.isFadeOut === true){
this.intensity = this.fadeout_curve.value(this.timer*this.fadeout_freq-Math.floor(this.timer*this.fadeout_freq));
material.setParameter('intensity', this.intensity);
}
else{
this.intensity = this.teleport_curve.value(this.timer*this.teleport_freq-Math.floor(this.timer*this.teleport_freq));
material.setParameter('intensity', this.intensity);
}
material.update();
}
}
And I tried to pass in assets after I defined the variables, like this:
MfGrid.attributes.add('chunkName' , { type: 'string' });
MfGrid.attributes.add('mask1', { type: 'string'});
MfGrid.attributes.add('mask2', { type: 'string'});
MfGrid.attributes.add('mask3', { type: 'string'});
MfGrid.attributes.add('mask4', { type: 'string'});
MfGrid.attributes.add('speed' , { type: 'number' , default : 0.1});
MfGrid.attributes.add('scale' , { type: 'number' , default : 2});
MfGrid.attributes.add('map_scale' , { type: 'number' , default : 1});
MfGrid.attributes.add('teleport_freq' , {type: 'number' , default: 2});
MfGrid.attributes.add('fadeout_freq' , {type: 'number' , default: 0.5});
MfGrid.attributes.add('teleport_curve', { type: 'curve'});
MfGrid.attributes.add('fadeout_curve', { type: 'curve'});
app.assets.loadFromUrl('../public/assets/textures/mask001.jpg','texture',function(err,asset){
let mask01 = asset;
app.assets.loadFromUrl('../public/assets/textures/mask002.jpg','texture',function(err,asset){
let mask02 = asset;
app.assets.loadFromUrl('../public/assets/textures/gridmask.jpg','texture',function(err,asset){
let mask03 = asset;
grid.addComponent('script');
grid.script.create('mfGrid',{
attributes: {
chunkName : 'opacity',
mask1: mask01,
mask2: mask02,
mask3: mask03,
mask4: mask02,
speed: 0.1,
scale: 3,
map_scale: 1,
teleport_freq: 2,
fadeout_freq: 2,
teleport_curve: {
type:pc.CURVE_LINEAR,
keys:[0,1]
},
fadeout_curve: {
type:pc.CURVE_LINEAR,
keys:[0,1]
},
}
});
console.log(grid.script.mfGrid);
});
});
});
I can make sure that the same code (when defined as an asset) can run normally in the editor version, so I am sure that the problem is when using ES6 syntax. Any suggestions?