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.
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?
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.
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
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?