createBox(device, [opts]) in this line of code device parameter is graphicsDevice. What is mean by graphicsDevice
I tried with pc.createBox(“graphicsDevice”) and pc.createBox(graphicsDevice) but it is not working
Assuming you mean this function and you are using it in a gamescript: pc | PlayCanvas API Reference
The first param takes an instead of the graphicsDevice object which is created on the application object: Application | PlayCanvas API Reference
const boxMesh = pc.createBox(this.app.graphicsDevice, {
// Options for the size of the box and other properties
});
However, if you are doing this in the Editor it is recommended to use the renderComponent on an Entity instead
// Assuming there isn't a render component on the entity already
this.entity.addComponent('render', {
type: 'box'
});
It’s useful to call directly only if you want to pass some other creation parameters. For example see PlayCanvas Examples - the createHighQualitySphere function creates the sphere directly, to be able to create higher subdivision for smoother sphere
const mesh = pc.createSphere(app.graphicsDevice, {
latitudeBands: 200,
longitudeBands: 200,
});
In order to render it, you can either go the route of creating a mesh instance + graph node etc. or simply use app.renderMesh
:
app = pc.app;
mesh = pc.createBox(app.graphicsDevice);
material = new pc.StandardMaterial();
mat4 = new pc.Mat4();
if (typeof meshUpdate !== 'undefined') {
app.off('update', meshUpdate)
}
function meshUpdate() {
mat4.setTranslate(
Math.sin(Date.now() * 0.005),
Math.cos(Date.now() * 0.005),
Math.tan(Date.now() * 0.005)
);
app.renderMesh(mesh, material, mat4);
}
app.on('update', meshUpdate);
(you can just open f12/devtools and execute it on any PlayCanvas code, as long as pc
or app
is accessible)