# Dynamic Cubemap Reflection

**URL:** <https://forum.playcanvas.com/t/dynamic-cubemap-reflection/32435>\
**Category:** Help & Support\
**Tags:** graphics\
**Created:** [July 31, 2023, 11:07am UTC](https://forum.playcanvas.com/t/dynamic-cubemap-reflection/32435 "2023-07-31T11:07:16Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![cw\_bc](https://forum.playcanvas.com/letter_avatar_proxy/v4/letter/c/51bf81/32.png) [@cw\_bc](https://forum.playcanvas.com/u/cw_bc)\
**Post date:** [July 31, 2023, 11:07am UTC](https://forum.playcanvas.com/t/dynamic-cubemap-reflection/32435/1 "2023-07-31T11:07:16Z")

</div>

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:

```javascript
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?

```javascript
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;
};

```

---

<div class="post-metadata">

**Author:** ![mvaligursky](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/mvaligursky/32/11681_2.png) [@mvaligursky](https://forum.playcanvas.com/u/mvaligursky)\
**Post date:** [July 31, 2023, 11:52am UTC](https://forum.playcanvas.com/t/dynamic-cubemap-reflection/32435/2 "2023-07-31T11:52:15Z")

</div>

Here is up to date example:  
[https://playcanvas.github.io/#/graphics/reflection-cubemap](https://playcanvas.github.io/#/graphics/reflection-cubemap)

source code: [https://github.com/playcanvas/engine/blob/main/examples/src/examples/graphics/reflection-cubemap.tsx](https://github.com/playcanvas/engine/blob/main/examples/src/examples/graphics/reflection-cubemap.tsx)

and it uses this script, which can be used in the Editor project as well: [https://github.com/playcanvas/engine/blob/main/scripts/utils/cubemap-renderer.js](https://github.com/playcanvas/engine/blob/main/scripts/utils/cubemap-renderer.js)

It’s possible there is some Editor example demonstrating its usage, but I’m not sure.

---

<div class="post-metadata">

**Author:** ![mvaligursky](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/mvaligursky/32/11681_2.png) [@mvaligursky](https://forum.playcanvas.com/u/mvaligursky)\
**Post date:** [July 31, 2023, 11:53am UTC](https://forum.playcanvas.com/t/dynamic-cubemap-reflection/32435/3 "2023-07-31T11:53:21Z")

</div>

Actually, we have one more example, using a box projected cubemap too  
[https://playcanvas.github.io/#/graphics/reflection-box](https://playcanvas.github.io/#/graphics/reflection-box)

---

<div class="post-metadata">

**Author:** ![cw\_bc](https://forum.playcanvas.com/letter_avatar_proxy/v4/letter/c/51bf81/32.png) [@cw\_bc](https://forum.playcanvas.com/u/cw_bc)\
**Post date:** [July 31, 2023, 12:28pm UTC](https://forum.playcanvas.com/t/dynamic-cubemap-reflection/32435/4 "2023-07-31T12:28:59Z")

</div>

I try using the script for the Editor Project, but it is not Capturing the Objects in the Scene. When i manually add a Environment Cubemap it reflects it. but the Captured one is not working.  
Am i doing something wrong?

[https://playcanvas.com/project/1115995/overview/reflect](https://playcanvas.com/project/1115995/overview/reflect)

---

<div class="post-metadata">

**Author:** ![mvaligursky](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/mvaligursky/32/11681_2.png) [@mvaligursky](https://forum.playcanvas.com/u/mvaligursky)\
**Post date:** [July 31, 2023, 1:09pm UTC](https://forum.playcanvas.com/t/dynamic-cubemap-reflection/32435/5 "2023-07-31T13:09:12Z")

</div>

It’s capturing - I can see it when I capture the frame using SpectorJS.  
But you need to do other things the example does as well … specifically at least assign the captured cubemap to other materials to use.

You also need to set up layers (you might have already) to make sure the meshes that use the captured cubemap are not rendered to a cubemap.

Study the examples provided, including comments in them.

---

<div class="post-metadata">

**Author:** ![cw\_bc](https://forum.playcanvas.com/letter_avatar_proxy/v4/letter/c/51bf81/32.png) [@cw\_bc](https://forum.playcanvas.com/u/cw_bc)\
**Post date:** [August 1, 2023, 8:55am UTC](https://forum.playcanvas.com/t/dynamic-cubemap-reflection/32435/6 "2023-08-01T08:55:30Z")

</div>

Okay sorry for my missunderstanding, im a newbie to PlayCanvas.

It is reflecting now, but states the Error:

```javascript
Trying to bind current color buffer as a texture

```

on every Frame. Is there a way to fix this issue? (i used the Editor Project as Reference)

---

<div class="post-metadata">

**Author:** ![mvaligursky](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/mvaligursky/32/11681_2.png) [@mvaligursky](https://forum.playcanvas.com/u/mvaligursky)\
**Post date:** [August 1, 2023, 8:56am UTC](https://forum.playcanvas.com/t/dynamic-cubemap-reflection/32435/7 "2023-08-01T08:56:41Z")

</div>

> [@mvaligursky](#):
>
> You also need to set up layers (you might have already) to make sure the meshes that use the captured cubemap are not rendered to a cubemap.

yes, this part of what I mentioned:

_You also need to set up layers (you might have already) to make sure the meshes that use the captured cubemap are not rendered to a cubemap._

---

<div class="post-metadata">

**Author:** ![cw\_bc](https://forum.playcanvas.com/letter_avatar_proxy/v4/letter/c/51bf81/32.png) [@cw\_bc](https://forum.playcanvas.com/u/cw_bc)\
**Post date:** [August 1, 2023, 9:10am UTC](https://forum.playcanvas.com/t/dynamic-cubemap-reflection/32435/8 "2023-08-01T09:10:39Z")

</div>

Oh my god, thank you ^^  
It finally worked. I forgot to add the layers to the render order, after adding them in the settings.

Thank you for your patients!!

---

<div class="post-metadata">

**Author:** ![Guohao6666](https://forum.playcanvas.com/letter_avatar_proxy/v4/letter/g/dfb087/32.png) [@Guohao6666](https://forum.playcanvas.com/u/Guohao6666)\
**Post date:** [June 20, 2024, 5:36am UTC](https://forum.playcanvas.com/t/dynamic-cubemap-reflection/32435/9 "2024-06-20T05:36:55Z")

</div>

Hello, could you share your project with me? I would greatly appreciate it
