Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix light shader data abnormal when multiple scenes are activated #1759

Merged
merged 3 commits into from
Sep 15, 2023

Conversation

cptbtptpbcptdtptp
Copy link
Collaborator

Demo:

/**
 * @title Multi Scene
 * @category Scene
 */

import {
  AssetType,
  BackgroundMode,
  BlinnPhongMaterial,
  Camera,
  Color,
  DirectLight,
  Engine,
  GLTFResource,
  Layer,
  MeshRenderer,
  PrimitiveMesh,
  Scene,
  Script,
  SkyBoxMaterial,
  TextureCube,
  Vector3,
  WebGLEngine,
} from "@galacean/engine";
import { OrbitControl } from "@galacean/engine-toolkit-controls";
import * as dat from "dat.gui";

WebGLEngine.create({ canvas: "canvas" }).then((engine) => {
  engine.canvas.resizeByClientSize();
  const { sceneManager } = engine;
  const firstScene = initFirstScene(engine);
  const secondScene = initSecondScene(engine);

  engine.run();

  const guiData = {
    showFirst: true,
    showSecond: true,
  };

  function addGUI() {
    const gui = new dat.GUI();
    const sceneFolder = gui.addFolder("multi scene");
    sceneFolder.open();

    gui
      .add(guiData, "showFirst")
      .onChange((value: boolean) => {
        if (value) {
          sceneManager.addScene(0, firstScene);
        } else {
          sceneManager.removeScene(firstScene);
        }
      })
      .listen();
    gui
      .add(guiData, "showSecond")
      .onChange((value: boolean) => {
        if (value) {
          sceneManager.addScene(1, secondScene);
        } else {
          sceneManager.removeScene(secondScene);
        }
      })
      .listen();
  }

  addGUI();
});

function initFirstScene(engine: Engine): Scene {
  const scene = engine.sceneManager.scenes[0];
  const rootEntity = scene.createRootEntity();

  // Add sky box background
  // engine.resourceManager
  //   .load<TextureCube>({
  //     urls: [
  //       "https://gw.alipayobjects.com/mdn/rms_7c464e/afts/img/A*5w6_Rr6ML6IAAAAAAAAAAAAAARQnAQ",
  //       "https://gw.alipayobjects.com/mdn/rms_7c464e/afts/img/A*TiT2TbN5cG4AAAAAAAAAAAAAARQnAQ",
  //       "https://gw.alipayobjects.com/mdn/rms_7c464e/afts/img/A*8GF6Q4LZefUAAAAAAAAAAAAAARQnAQ",
  //       "https://gw.alipayobjects.com/mdn/rms_7c464e/afts/img/A*D5pdRqUHC3IAAAAAAAAAAAAAARQnAQ",
  //       "https://gw.alipayobjects.com/mdn/rms_7c464e/afts/img/A*_FooTIp6pNIAAAAAAAAAAAAAARQnAQ",
  //       "https://gw.alipayobjects.com/mdn/rms_7c464e/afts/img/A*CYGZR7ogZfoAAAAAAAAAAAAAARQnAQ",
  //     ],
  //     type: AssetType.TextureCube,
  //   })
  //   .then((cubeMap1) => {
  //     const { background } = scene;
  //     background.mode = BackgroundMode.Sky;
  //     const skyMaterial = new SkyBoxMaterial(engine);
  //     skyMaterial.texture = cubeMap1;

  //     background.sky.material = skyMaterial;
  //     background.sky.mesh = PrimitiveMesh.createCuboid(engine, 2, 2, 2);
  //   });

  // Create full screen camera
  const cameraEntity = rootEntity.createChild("fullscreen-camera");
  const camera = cameraEntity.addComponent(Camera);
  camera.cullingMask = Layer.Layer0;
  cameraEntity.transform.setPosition(10, 10, 10);
  cameraEntity.transform.lookAt(new Vector3(0, 0, 0));
  cameraEntity.addComponent(OrbitControl);

  const lightEntity = rootEntity.createChild("Light");
  lightEntity.transform.setRotation(-30, 0, 0);
  lightEntity.addComponent(DirectLight);

  // Create cube
  const cubeEntity = rootEntity.createChild("cube");
  cubeEntity.transform.setPosition(-3, 0, 3);
  const renderer = cubeEntity.addComponent(MeshRenderer);
  renderer.mesh = PrimitiveMesh.createSphere(engine, 2, 24);
  const material = new BlinnPhongMaterial(engine);
  material.baseColor = new Color(1, 0.25, 0.25, 1);
  renderer.setMaterial(material);

  return scene;
}

function initSecondScene(engine: Engine): Scene {
  // Init window camera
  const scene = new Scene(engine);
  engine.sceneManager.addScene(scene);
  const rootEntity = scene.createRootEntity();

  const lightEntity = rootEntity.createChild("Light");
  lightEntity.transform.setRotation(-45, 0, 0);
  lightEntity.addComponent(DirectLight).color = new Color(1, 0, 0);

  const cameraEntity = rootEntity.createChild("window-camera");
  const camera = cameraEntity.addComponent(Camera);
  camera.viewport.set(0.6, 0.2, 0.25, 0.6);
  camera.farClipPlane = 200;
  cameraEntity.transform.setPosition(0, 3, 5);
  cameraEntity.transform.lookAt(new Vector3(0, 1, 0));

  engine.resourceManager
    .load<GLTFResource>(
      "https://gw.alipayobjects.com/os/OasisHub/267000040/9994/%25E5%25BD%2592%25E6%25A1%25A3.gltf"
    )
    .then((gltf) => {
      const defaultSceneRoot = gltf.defaultSceneRoot;
      rootEntity.addChild(defaultSceneRoot);
      defaultSceneRoot.addComponent(RotateScript);
    });

  return scene;
}

/**
 * Script for rotate.
 */
class RotateScript extends Script {
  /**
   * @override
   * The main loop, called frame by frame.
   * @param deltaTime - The deltaTime when the script update.
   */
  onUpdate(deltaTime: number): void {
    this.entity.transform.rotate(0.0, 50 * deltaTime, 0);
  }
}

@cptbtptpbcptdtptp cptbtptpbcptdtptp changed the base branch from main to dev/1.1 September 15, 2023 07:14
@cptbtptpbcptdtptp cptbtptpbcptdtptp self-assigned this Sep 15, 2023
@cptbtptpbcptdtptp cptbtptpbcptdtptp added the bug Something isn't working label Sep 15, 2023
@cptbtptpbcptdtptp cptbtptpbcptdtptp added this to the 1.1 milestone Sep 15, 2023
@codecov
Copy link

codecov bot commented Sep 15, 2023

Codecov Report

Patch coverage: 54.63% and project coverage change: +4.55% 🎉

Comparison is base (7ad53d1) 60.44% compared to head (7d69e50) 65.00%.
Report is 86 commits behind head on dev/1.1.

❗ Current head 7d69e50 differs from pull request most recent head 5f61060. Consider uploading reports for the commit 5f61060 to get more accurate results

Additional details and impacted files
@@             Coverage Diff             @@
##           dev/1.1    #1759      +/-   ##
===========================================
+ Coverage    60.44%   65.00%   +4.55%     
===========================================
  Files          386      470      +84     
  Lines        19993    23572    +3579     
  Branches      2856     3352     +496     
===========================================
+ Hits         12085    15323    +3238     
- Misses        6798     7046     +248     
- Partials      1110     1203      +93     
Files Changed Coverage Δ
packages/core/src/RenderPipeline/RenderContext.ts 100.00% <ø> (ø)
...kages/core/src/RenderPipeline/SpriteMaskBatcher.ts 13.55% <0.00%> (-0.24%) ⬇️
...ges/core/src/RenderPipeline/enums/PipelineStage.ts 100.00% <ø> (ø)
packages/core/src/Utils.ts 58.66% <0.00%> (+3.66%) ⬆️
packages/core/src/animation/AnimationClip.ts 42.85% <0.00%> (-8.37%) ⬇️
packages/core/src/animation/Keyframe.ts 100.00% <ø> (ø)
...ore/src/animation/animationCurve/AnimationCurve.ts 61.42% <0.00%> (+2.85%) ⬆️
packages/core/src/asset/AssetType.ts 100.00% <ø> (ø)
packages/core/src/asset/Loader.ts 83.33% <ø> (ø)
packages/core/src/base/Constant.ts 100.00% <ø> (ø)
... and 217 more

... and 36 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@cptbtptpbcptdtptp cptbtptpbcptdtptp changed the base branch from dev/1.1 to main September 15, 2023 07:52
@GuoLei1990 GuoLei1990 merged commit ab9214b into galacean:main Sep 15, 2023
4 of 5 checks passed
GuoLei1990 added a commit to HypnosNova/engine-1 that referenced this pull request Sep 19, 2023
* main: (23 commits)
  Support entity reference (galacean#1417)
  feat: log physics version (galacean#1769)
  Fix Script destroy bug (galacean#1770)
  Fix collider shape bug (galacean#1768)
  fix: physics unit test timeout (galacean#1766)
  Fix `viewportPointToRay` triggers precision issues when the depth value is 1 (galacean#1767)
  fix: project loader not exported (galacean#1758)
  "v1.1.0-beta.4"
  Opt SpriteBatch code (galacean#1760)
  Fix light shader data abnormal when multiple scenes are activated (galacean#1759)
  Fix BlendShape anim compatible with multiple SkinMeshRenderer in on… (galacean#1756)
  refactor: clear code and opt memory (galacean#1757)
  test: add physx unit timeout (galacean#1755)
  "v1.1.0-beta.3"
  Fix charactor controller error (galacean#1754)
  test: fix unit test timeout
  "v1.1.0-beta.2"
  Fix clone error (galacean#1753)
  Support main thread ktx2 (galacean#1745)
  "v1.1.0-beta.1"
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants