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 2: Light) #324

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

学习 three.js (Part 2: Light) #324

nonocast opened this issue Sep 7, 2024 · 0 comments

Comments

@nonocast
Copy link
Owner

nonocast commented Sep 7, 2024

import * as THREE from "three";

const scene = new THREE.Scene();

// const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
// fov (视角): 相机的视角(单位: 度)。控制视野的大小,值越大视野越宽。
// aspect (纵横比): 相机的纵横比。通常是窗口的宽度除以高度。
// near (近截面): 相机的近截面距离。控制从相机到近截面的距离。
// far (远截面): 相机的远截面距离。控制从相机到远截面的距离。
const camera = new THREE.PerspectiveCamera(
  75,
  window.innerWidth / window.innerHeight,
  0.1,
  1000
);

// three.js中,z轴的正方向是朝向屏幕外为正, x轴水平向右为正,y轴垂直向上为正
camera.position.set(10, 10, 10);
camera.lookAt(0, 0, 0);

const renderer = new THREE.WebGLRenderer();
renderer.shadowMap.enabled = true; 
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

const light = new THREE.PointLight(0xffffff, 300, 100);
light.position.set(10, 10, 10);
light.lookAt(0, 0, 0);
scene.add(light);

// 创建一个立方体
const geometry = new THREE.BoxGeometry(3, 3, 3);
const material = new THREE.MeshStandardMaterial({ color: 0xff0000 });
const cube = new THREE.Mesh(geometry, material);
cube.position.y = 1.5;
scene.add(cube);

// 创建一个地面
const planeGeometry = new THREE.PlaneGeometry(100, 100);
const planeMaterial = new THREE.MeshStandardMaterial({ color: 0xffffff });
const plane = new THREE.Mesh(planeGeometry, planeMaterial);
plane.receiveShadow = false;
plane.rotation.x = - Math.PI / 2;
plane.position.set(0, 0, 0);
scene.add(plane);

renderer.render(scene, camera);
Screenshot 2024-09-08 at 00 50 38
  • Scene: 场景, 管理和组织所有要许安然的对象
  • Camera: 相机,定义了观察视角,相机包括PerpectiveCamera和OrthographcCamera
  • Renderer: 将相机看到的内容渲染到屏幕上,默认是WebGLRenderer
  • Light: 灯光
  • Material: 材质。其中MeshBasicMaterial (不受light影响), MeshStandardMaterial和MeshLambertMaterial
  • Geometry: 几何体
  • Mesh: 网格。网格是由几何体和材质组成的3D对象。
  • Animation: 动画
  • Controls: 控制器

注意:

  1. threejs采用右手左边系,x轴水平向右为正,y轴垂直向上为正,z轴向屏幕外为正
  2. plane加进scene默认是yz面,如果要水平则需要旋转-1/2pi,要注意 normal (法线)
@nonocast nonocast changed the title 学习three.js (Part 2: Light) 学习 three.js (Part 2: Light) Sep 11, 2024
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