nonocast / me

记录和分享技术的博客
http://nonocast.cn
MIT License
20 stars 0 forks source link

学习 three.js (Part 1: hello world) #322

Open nonocast opened 2 weeks ago

nonocast commented 2 weeks ago

ref: Installation – three.js docs

操作步骤略有不同:

  1. yarn create vite, project: hello, framework选择Vanilla
  2. code打开hello, 在terminal中运行yarn安装依赖,然后yarn dev运行,打开可以看到vite默认页面
  3. yarn add three
  4. 然后copy three.js docs中index.html代码
    
    <!DOCTYPE html>
    <html lang="en">
My first three.js app

5. copy main.js代码
```js
import * as THREE from 'three';

const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );

const renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setAnimationLoop( animate );
document.body.appendChild( renderer.domElement );

const geometry = new THREE.BoxGeometry( 1, 1, 1 );
const material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
const cube = new THREE.Mesh( geometry, material );
scene.add( cube );
camera.position.z = 5;

function animate() {
  cube.rotation.x += 0.01;
  cube.rotation.y += 0.01;
  renderer.render( scene, camera );
}

打开浏览器看到green cube, 收工。

cube

注:

参考阅读: