[SOLVED] How to render to multiple textures

This is my bootleg frankenstein code for render to texture

var Camtoscreen = pc.createScript('camtoscreen');
Camtoscreen.attributes.add('applyTextureToEntity', {
type: 'entity', 
title: 'Entity to Apply Texture to',
});
Camtoscreen.prototype.initialize = function() {
var colorBuffer = new pc.Texture(this.app.graphicsDevice, {
width: 240,
height: 240,
format: pc.PIXELFORMAT_R1_G1_B1
});  
var renderTarget = new pc.RenderTarget(this.app.graphicsDevice, colorBuffer, {
depth: true
});
this.entity.camera.renderTarget = renderTarget;
var meshInstances = this.applyTextureToEntity.model.meshInstances;
for (var i = 0; i < meshInstances.length; ++i) {
var mesh = meshInstances[i];
mesh.material.emissiveMap = renderTarget.colorBuffer;
mesh.material.diffuseMap = renderTarget.colorBuffer;
mesh.material.update();
}
};

Despite the fact that I only assign one entity to it, the camera casts to multiple entities. In fact, it seems to render to every single model without a material. No idea why that is, but I do not like that.

What I am looking for is the ability to render separate cameras to separate models.

Hi @ALUCARD ,

Do all of the entities you describe lack a material? It sounds like you’re updating the default material, and since all of the entities have that default material they all display the same texture that’s being rendered. Have you considered creating a material specifically for the entity that you want to render a texture to and seeing what that does?

I hope this is helpful.

1 Like

And how would one go about doing that? Again this is frankenstein code from a tutorial so not entirely sure how it works.

All you would really have to do is just create a material in the editor and apply it to your model component that you’re assigning to applyTextureToEntity. Then when you update that material, only the models that have that material assigned will have the texture rendered to them.

yes this worked

although now I get an error saying “trying to bind color buffer as a texture”. I am trying to render a camera to texture then use a camera to render that texture to another texture, like a periscope. I only get the error when a texture render captures another rendered texture

Hi @ALUCARD,

Without seeing the project, and understanding exactly the outcome you’re looking for, it’s hard to say. Could you post some screenshots of the problem and a link to the project?

Hi @eproasim
Could you help me out with a game I’m working on?

I actually got it working, despite the error. Its one of those “gives an error but still works somehow” type of things.

What is the problem with your game?

there are no problems
i just need someone who can make a breaking animation and them or someone else who can make sounds for placing and breaking

What does that have to do with the topic you posted this on?

Oh sorry I thought the topic was collaboration