01-three_basic 使用three.js渲染第一个场景和物体

1.package.json

{
  "name": "01-three_basic",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "parcel src/index.html",
    "build": "parcel build src/index.html"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "dat.gui": "^0.7.9",
    "gsap": "^3.12.2",
    "parcel": "^2.10.3",
    "three": "^0.158.0"
  }
}

2.代码及注释 main.js

import * as THREE from 'three';

// console.log(THREE);

//目标:了解three。js最近的内容

//1. 创建场景
const scene = new THREE.Scene();

//2、创建相机
const camera = new THREE.PerspectiveCamera(
    75,
    window.innerWidth / window.innerHeight,
    0.1,
    1000
);
//设置相机位置
camera.position.set(0,0,10)
scene.add(camera);

//添加物体
//创建几何体
const cubeGeometry = new THREE.BoxGeometry(1, 1, 1);
const cubeMaterial = new THREE.MeshBasicMaterial({color:0xffff00});
//根据几何体和材质创建物体
const cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
//将几何体添加到场景中
scene.add(cube);

//初始化渲染器
const renderer = new THREE.WebGLRenderer();
//设置渲染的尺寸大小
renderer.setSize(window.innerWidth, window.innerHeight);
//console.log(renderer);
// 将webgl渲染的canvas 内容添加到body
document.body.appendChild(renderer.domElement);

//使用渲染器,通过相机将场景渲染进来
renderer.render(scene, camera);

效果

01-three_basic 使用three.js渲染第一个场景和物体_第1张图片

你可能感兴趣的:(three,javascript,开发语言,js)