Hello,
for a while i am trying to accomplish a Script to render a Cubemap on Runtime and use it for Reflections on various Objects in the Scene.
All Scripts and Hints i can find are for older Versions of Cubemap Renderer or Reflections.
Below is the Code i currently try to use to render the Cubemap.
It throws the following error:
TypeError: Cannot read properties of null (reading 'original')
at TextureHandler.load (texture.js:290:29)
at ResourceLoader._loadNull (loader.js:191:17)
at ResourceLoader.load (loader.js:112:18)
at AssetRegistry.load (asset-registry.js:399:26)
at CubemapHandler.loadAssets (cubemap.js:328:26)
at CubemapHandler.load (cubemap.js:37:14)
at ResourceLoader._loadNull (loader.js:191:17)
at ResourceLoader.load (loader.js:112:18)
at AssetRegistry.load (asset-registry.js:399:26)
at AssetRegistry.add (asset-registry.js:257:18)
Does someone know how to fix this?
Or does someone has a more efficent way to do real time reflections?
var ReflectionCubemapScript = pc.createScript('reflectionCubemapScript');
ReflectionCubemapScript.attributes.add('cubemapResolution', {
type: 'number',
default: 512,
description: 'The resolution of the reflection cubemap.'
});
ReflectionCubemapScript.prototype.initialize = function () {
this.frameCount = 0;
this.reflectionCamera = null;
this.createReflectionCamera();
};
ReflectionCubemapScript.prototype.createReflectionCamera = function () {
this.reflectionCamera = new pc.Entity();
this.reflectionCamera.name = 'ReflectionCamera';
this.reflectionCamera.addComponent('camera', {
clearColor: new pc.Color(0, 0, 0), // Set the clear color to black (or any other color you prefer)
priority: -1 // Set the priority to a lower value than the main camera to render first
});
this.reflectionCamera.camera.nearClip = 0.1;
this.reflectionCamera.camera.farClip = 1000;
this.entity.addChild(this.reflectionCamera);
};
ReflectionCubemapScript.prototype.update = function (dt) {
this.frameCount++;
if (this.frameCount % 20 === 0) {
// Capture the cubemap every 20 frames
this.captureCubemap();
}
};
ReflectionCubemapScript.prototype.captureCubemap = function () {
var cubemapAsset = new pc.Asset('ReflectionCubemap', 'cubemap', {
url: null
});
cubemapAsset.on('load', function () {
this.entity.render.material.cubeMap = cubemapAsset.resource;
}, this);
cubemapAsset.on('error', function (err) {
console.error('Error loading cubemap asset: ', err);
});
this.app.assets.add(cubemapAsset);
this.app.assets.load(cubemapAsset);
this.reflectionCamera.camera.cubeMap = cubemapAsset.resource;
var targets = [
new pc.Vec3(1, 0, 0),
new pc.Vec3(-1, 0, 0),
new pc.Vec3(0, 1, 0),
new pc.Vec3(0, -1, 0),
new pc.Vec3(0, 0, 1),
new pc.Vec3(0, 0, -1)
];
var upVectors = [
new pc.Vec3(0, -1, 0),
new pc.Vec3(0, -1, 0),
new pc.Vec3(0, 0, 1),
new pc.Vec3(0, 0, -1),
new pc.Vec3(0, -1, 0),
new pc.Vec3(0, -1, 0)
];
for (var i = 0; i < 6; i++) {
var target = targets[i];
var up = upVectors[i];
this.reflectionCamera.setPosition(this.entity.getPosition());
this.reflectionCamera.lookAt(this.entity.getPosition().add(target), up);
this.reflectionCamera.camera.renderTarget = this.reflectionCamera.camera.cubeMap;
this.app.render();
}
this.entity.render.material.cubemap = this.reflectionCamera.camera.cubeMap;
};