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

学习 three.js (Part 3: Load model) #325

Open
nonocast opened this issue Sep 8, 2024 · 0 comments
Open

学习 three.js (Part 3: Load model) #325

nonocast opened this issue Sep 8, 2024 · 0 comments

Comments

@nonocast
Copy link
Owner

nonocast commented Sep 8, 2024

在SketchUp中建个小房子,随便拉个长方体,画个中线,然后用move来一下中线,导出glB

Screenshot 2024-09-08 at 15 04 28

main.js

import * as THREE from "three";
import { GLTFLoader } from "three/addons/loaders/GLTFLoader.js";

const scene = new THREE.Scene();

const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(3, 2, 5);
camera.lookAt(0, 0, 0);

const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

const directionalLight = new THREE.DirectionalLight(0xffffff, 6); // 方向光
directionalLight.position.set(3, 3, 1);
scene.add(directionalLight);

const loader = new GLTFLoader();
loader.load('/house.glb', function (gltf) {
  const model = gltf.scene;
  scene.add(model);

  // 可选:调整模型的位置和缩放
  model.position.set(0, 0, 0);
  model.scale.set(1, 1, 1);

  renderer.render(scene, camera);
}, undefined, function (error) {
  console.error('An error happened while loading the model:', error);
});
Screenshot 2024-09-08 at 15 05 57

最后附上模型: house.glb.zip
代码在: https://github.com/nonocast/hello-map

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

1 participant