Lightmapper engine only Error

Hello, I have issue with lightmapper, I’m getting an error when I try to bake with lightmapper using engine only but works fine in editor. The code for the editor and engine is almost same. I have no idea what is causing the issue. The engine version I’m using is 1.68.0.

This is my code inside editor:

Lightmapper.prototype.initialize = function () {
    let omni = []
    let walls = []

  const property = this.app.root.findByName('missipi');
    const lights = property.find('lights')[0]

    const renders = property.findComponents('render');
    renders.forEach((render) => {
        if (render.entity.name.includes('walls')) {
            render.castShadows = true;
            render.castShadowsLightmap = true;
            render.lightmapped = true;
            walls.push(render.entity);
        }
    });

    lights.forEach((light) => {
        const lightOmni = new pc.Entity('Omni');
        lightOmni.addComponent('render', {
            type: 'box'
        })
        lightOmni.setLocalScale(0.1, 0.1, 0.1);

        lightOmni.addComponent('light', {
            type: 'omni',
            affectDynamic: false,
            affectLightmapped: true,
            bake: true,
            castShadows: true,
            normalOffsetBias: 0.05,
            shadowBias: 0.2,
            shadowDistance: 25,
            shadowResolution: 512,
            shadowType: pc.SHADOW_PCF3,
            color: pc.Color.WHITE,
            range: 20,
            intensity: 0.15
        });

        this.app.root.addChild(lightOmni);
        omni.push(lightOmni);

        lightOmni.setPosition(light.getPosition());
        lightOmni.setRotation(light.getRotation());
    });


    this.app.lightmapper.bake(walls, pc.BAKE_COLORDIR);
    console.log(this.app.lightmapper.stats.totalRenderTime.toFixed(2));
}

and result:

Engine Only:

App setup:

 const app = new pc.Application(canvas, {
  graphicsDeviceOptions: {
    alpha: true,
    antialias: true,
    powerPreference: "high-performance",
  },
});

  app.setCanvasResolution("AUTO");
  app.setCanvasFillMode("FILL_WINDOW");
  app.start();

Ligntmapping:


  private onload = async (asset) => {
    const renderEntity = asset.resource.instantiateRenderEntity();
    this.setPosition(0, -0.35, 0);
    this.app.root.addChild(renderEntity);
    this.setupOrbitCamera();

    const renders = renderEntity.findComponents('render') as RenderComponent[];
    const walls = [];
    renders.forEach((render) => {
      if (render.entity.name.includes('walls')) {
        render.castShadows = true;
        render.castShadowsLightmap = true;
        render.lightmapped = true;
        walls.push(render.entity);
      }
    });

   const roomLights = renderEntity.children.find((entity) => entity.name === lightId);
roomLights?.forEach((roomLight) => {
      const position = roomLight.getPosition();
      const euler = roomLight.getEulerAngles();

      const entity = new Entity(roomLight.name);
      entity.addComponent('light', {
        type: 'omni',
        affectDynamic: false,
        affectLightmapped: true,
        bake: true,
        castShadows: true,
        normalOffsetBias: 0.05,
        shadowBias: 0.2,
        shadowDistance: 25,
        shadowResolution: 512,
        shadowType:pc.SHADOW_PCF3,
        color: pc.Color.WHITE,
        range: 20,
        intensity: 0.15,
      });

      this.app.root.addChild(entity);
      this._roomLights.push(entity);

      entity.setPosition(position);
      entity.setEulerAngles(euler);
    });
  };

    this.app.scene.lightmapMode = pc.BAKE_COLORDIR;
    this.app.scene.lightmapMaxResolution = 1024;
    this.app.scene.lightmapSizeMultiplier = 512;

     this.app.lightmapper.bake(walls, pc.BAKE_COLORDIR);
     console.log(this.app.lightmapper.stats.totalRenderTime.toFixed(2));
  };

I get these errors:



Screenshot 2024-05-22 at 11.39.36 AM

Any idea how to fix this? Thanks

See a functioning example here:
https://playcanvas.github.io/#/graphics/lights-baked-a-o

click on that SetupScene error to see what it’s doing on that line.

Yes, I did follow that example for lightmapping.

this is the where the error occurs:

this.scene.ambientLight.copy(this.ambientLight);

Actually I missed one important setup code with ambient light:

  this.app.scene.ambientLight = new Color(1, 1, 1);

now baking works!

2 Likes