Rendering from camera to target material

Hi,
I am trying to render the output of a camera to a material. I have tried reviewing some old examples(https://playcanvas.com/project/335444/overview/tutorial-render-to-texture). However nothing seems to work. The object using the material still appears empty.

Here is my implementation of the script, i am not sure why it doesnt work:

var CameraRenderTarget = pc.createScript('cameraRenderTarget');

CameraRenderTarget.attributes.add("RenderTargetMaterial",{type:"asset",assetType:"material"});

// initialize code called once per entity
CameraRenderTarget.prototype.initialize = function() {
     var colorBuffer = new pc.Texture(this.app.graphicsDevice, {
                width: 512,
                height: 512,
                format: pc.PIXELFORMAT_R8_G8_B8,
                autoMipmap: true
            });
            colorBuffer.minFilter = pc.FILTER_LINEAR;
            colorBuffer.magFilter = pc.FILTER_LINEAR;
            var renderTarget = new pc.RenderTarget(this.app.graphicsDevice, colorBuffer, {
                depth: true
            });

            this.renderTarget = renderTarget;
            this.entity.camera.renderTarget = renderTarget;
            this.RenderTargetMaterial.diffuseMap = renderTarget;
};

// update code called every frame
CameraRenderTarget.prototype.update = function(dt) {
   
};

thanks

Ok, so i found out some of what i was missing.
The render target’s color buffer is now written to the material on the targeted entity and the material is then updated, the material now updates(so i am at least writing to the correct place) but the it remains completely black. Any examples or help would be appreciated.

var CameraRenderTarget = pc.createScript('cameraRenderTarget');

CameraRenderTarget.attributes.add("RenderTargetEntity",{type:"entity"});

// initialize code called once per entity
CameraRenderTarget.prototype.initialize = function() {
     var colorBuffer = new pc.Texture(this.app.graphicsDevice, {
                width: 512,
                height: 512,
                format: pc.PIXELFORMAT_R8_G8_B8,
                autoMipmap: true
            });
           
    colorBuffer.minFilter = pc.FILTER_LINEAR;
    colorBuffer.magFilter = pc.FILTER_LINEAR;
            
    var renderTarget = new pc.RenderTarget(this.app.graphicsDevice, colorBuffer, {
                depth: false
            });

    this.entity.camera.renderTarget = renderTarget;
    this.targetMaterial = this.RenderTargetEntity.model.model.meshInstances[0].material;
    
    this.targetMaterial.diffuseMap = this.entity.camera.renderTarget.colorBuffer;
    this.targetMaterial.update();
};

// update code called every frame
CameraRenderTarget.prototype.update = function(dt) {
   
};

This thread may help: Problem with layers

Thanks, i will check this out