Hello everyone,
after my post from yesterday (Dynamic Cubemaps / Reflection Probes / Render To Texture in 2020) I decided to bite the bullet and render my cubemaps in Blender instead of, dynamically, at runtime. So far so good.
I looked at the ‘Orange Room’ example (https://playcanvas.com/project/345310/overview/orange-room) and felt inspired to bascially assign the pre-rendered Cubemaps at runtime.
(Explanation on what I want to achieve: Let’s say I have 5 rooms with 6 or so materials each [walls, ceiling, floor, etc.]. I could technically duplicate each material 5 times and assign 5 different sets of Lightmaps and Cubemaps. But then, if I wanted to change, say, the color on one of my materials, I would have to change it on the other 4, too -> NOT ideal)
So I wrote a script:
var CustomCubemap = pc.createScript('customCubemap');
CustomCubemap.attributes.add('customCubemap', {type: 'asset', assetType: 'cubemap', description: 'Custom cubemap'});
CustomCubemap.attributes.add('cubeMapCenter', {type: 'vec3', default: [0,0,0], description: 'Cubemap projection offset'});
CustomCubemap.attributes.add('cubeMapHalfExtends', {type: 'vec3', default: [0.5,0.5,0.5], description: 'Cubemap half extends'});
CustomCubemap.prototype.initialize = function () {
var meshes = this.entity.model.meshInstances; // get all materials assigned
for (var i = 0; i < meshes.length; i++) {
var material = meshes[i].material;
material.clone();
material.useSkybox = false; // make sure the material doesn't use the global skybox
material.cubeMap = this.customCubemap.resources[1]; // use mipmap level 1 of prefiltered cubemaps
// set cubemap to projection box; set center and half extends
material.cubeMapProjection = pc.CUBEPROJ_BOX;
material.cubeMapProjectionBox = new pc.BoundingBox (this.cubeMapCenter, this.cubeMapHalfExtends);
material.update(); // update material
}
};
It should go through each material of an object (room) and clone each material. Then assign a custom cubemap to each material.
This is what it should look like (I manually assigned the Cubemap in the Editor) - notice the material’s glossiness is 50 -> somewhat diffuse reflection
This is what it does look like when the project is launched -> reflection is extremely sharp, as if the glossiness were 100
(Yes, I checked, the glossiness doesn’t change in the material before and after assigning the cubemap)
Here’s the link to a test project with the script: https://playcanvas.com/project/739534/overview/cubemap-test
What am I doint wrong? Someone please enlighten me