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

VRM or pmx loader? #2454

Open
625673575 opened this issue Dec 6, 2024 · 1 comment
Open

VRM or pmx loader? #2454

625673575 opened this issue Dec 6, 2024 · 1 comment

Comments

@625673575
Copy link

有无加载VRM或者PMX模型的库

@cptbtptpbcptdtptp
Copy link
Collaborator

cptbtptpbcptdtptp commented Dec 6, 2024

目前暂时没有,如果你对模型结构十分熟悉,可以通过自定义加载器加载:

// @galacean/[email protected]
// @galacean/[email protected]
import {
  AssetPromise,
  BlinnPhongMaterial,
  Camera,
  Color,
  DirectLight,
  Loader,
  LoadItem,
  MeshRenderer,
  MeshTopology,
  ModelMesh,
  request,
  resourceLoader,
  ResourceManager,
  Vector3,
  WebGLEngine,
} from "@galacean/engine";
import { OrbitControl } from "@galacean/engine-toolkit-controls";

@resourceLoader("OBJ", ["obj"])
class OBJLoader extends Loader<ModelMesh> {
  load(
    item: LoadItem,
    resourceManager: ResourceManager
  ): AssetPromise<ModelMesh> {
    return request<string>(item.url!, { ...item, type: "text" }).then(
      (text: string) => {
        const lines = text.split(/\n/);
        const positions: Vector3[] = [];
        const indices: number[] = [];
        lines
          .map((lineText) => lineText.split(" "))
          .forEach((parseTexts) => {
            if (parseTexts[0] === "v") {
              positions.push(
                new Vector3(
                  parseFloat(parseTexts[1]),
                  parseFloat(parseTexts[2]),
                  parseFloat(parseTexts[3])
                )
              );
            } else if (parseTexts[0] === "f") {
              indices.push(
                parseInt(parseTexts[1]) - 1,
                parseInt(parseTexts[2]) - 1,
                parseInt(parseTexts[3]) - 1
              );
            }
          });
        const mesh = new ModelMesh(resourceManager.engine);
        mesh.setPositions(positions);
        mesh.setIndices(Uint16Array.from(indices));
        mesh.addSubMesh(0, indices.length, MeshTopology.Triangles);
        mesh.uploadData(false);
        return mesh;
      }
    );
  }
}

WebGLEngine.create({ canvas: "canvas" }).then((engine) => {
  engine.canvas.resizeByClientSize();
  const scene = engine.sceneManager.activeScene;
  const rootEntity = scene.createRootEntity();

  // init camera
  const cameraEntity = rootEntity.createChild("camera");
  cameraEntity.addComponent(Camera);
  cameraEntity.addComponent(OrbitControl);
  cameraEntity.transform.setPosition(0.5, 0.5, 0.5);
  cameraEntity.transform.lookAt(new Vector3(0, 0, 0));

  // init light
  rootEntity.addComponent(DirectLight);

  engine.resourceManager
    .load<ModelMesh>(
      "https://gw.alipayobjects.com/os/bmw-prod/b885a803-5315-44f0-af54-6787ec47ed1b.obj"
    )
    .then((mesh) => {
      renderer.mesh = mesh;
    });
  // init cube
  const cubeEntity = rootEntity.createChild("cube");
  const renderer = cubeEntity.addComponent(MeshRenderer);
  const material = new BlinnPhongMaterial(engine);
  material.baseColor = new Color(1, 0.25, 0.25, 1);
  renderer.setMaterial(material);
  engine.run();
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants