I’m trying to update the Render to Texture tutorial (PlayCanvas | HTML5 Game Engine) as it gives two deprecated warnings:
pc.RenderTarget constructor no longer accepts GraphicsDevice parameter.
and
pc.Layer#renderTarget is deprecated. Set the render target on the camera instead.
I’m using the more recent example code at PlayCanvas Examples as a guide but without success – the cylinder is just solid black. However, if I uncomment lines 28 and 29 in RenderLayerToTexture.js
below, it works (but still with a deprecated warning).
I have the following code:
RenderLayerToTexture.js
var RenderLayerTotexture = pc.createScript('renderLayerTotexture');
RenderLayerTotexture.attributes.add("cameraName", {type: "string"});
RenderLayerTotexture.prototype.initialize = function() {
const colorBuffer = new pc.Texture(this.app.graphicsDevice, {
width: 512,
height: 512,
format: pc.PIXELFORMAT_R8_G8_B8,
mipmaps: true,
minFilter: pc.FILTER_LINEAR,
magFilter: pc.FILTER_LINEAR,
addressU: pc.ADDRESS_CLAMP_TO_EDGE,
addressV: pc.ADDRESS_CLAMP_TO_EDGE,
});
const renderTarget = new pc.RenderTarget({
name: `RT`,
colorBuffer: colorBuffer,
depth: true,
flipY: true,
samples: 2,
});
let cam = this.app.root.findByName(this.cameraName);
cam.renderTarget = renderTarget;
// var layer = this.app.scene.layers.getLayerByName("RenderTexture");
// layer.renderTarget = renderTarget;
};
ApplyTexture.js
var ApplyTexture = pc.createScript('applyTexture');
ApplyTexture.attributes.add("cameraName", {type: "string"});
ApplyTexture.prototype.postInitialize = function() {
let cam = this.app.root.findByName(this.cameraName);
var material = this.entity.model.model.meshInstances[0].material;
material.diffuseMap = cam.renderTarget.colorBuffer;
// material.emissiveMap = cam.renderTarget.colorBuffer;
material.update();
};
Anyone have ideas what I’m doing wrong?
TIA