three.js初时基础

第一步:找到Three.js – JavaScript 3D Library (threejs.org)

第二步

 第三步:

第四步:

安装依赖 

 第五步:新建一个项目文件,在文件中npm init 进行初始化出现一个package.json

 第六步:配置安装🚀 快速开始 | Parcel中文网 (parceljs.cn)

 在package.json中配置

配置结果: 

 

package.json文件中的配置如下:

{

  "name": "threejs",

  "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.11.3",

    "parcel-bundler": "^1.12.5",

    "three": "^0.146.0"

  },

  "devDependencies": {

    "parcel": "^2.8.0"

  }

}

文件创建:

 第七步:

 第八步:

dist是生产的源文件 

第九步:

main中的配置及详解:

import * as THREE from "three"

//导入轨道控制器

import{OrbitControls} from "three/examples/jsm/controls/OrbitControls"

//目标:3D物体的移动

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

//创建相机    PerspectiveCamera 透视相机

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

// 设置相机位置

// 将几何体添加到场景中

//初始化渲染器

const renderer = new THREE.WebGLRenderer();

//设置渲染的尺寸大小

renderer.setSize( window.innerWidth, window.innerHeight );

// 将webgl渲染的canvas内容添加到body

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 );

console.log("物体几何的位置",cube)

//修改物体的位置

// cube.position.set(5,0,0)//这个是方法

cube.position.x=3//这个是属性

scene.add( cube );

//创建轨道控制器 第一个参数是相机  第二个是渲染器

const controls = new OrbitControls(camera, renderer.domElement )

//添加坐标轴辅助器

const axesHelper = new THREE.AxesHelper( 5 );

// 红色代表 X 轴. 绿色代表 Y 轴. 蓝色代表 Z 轴.

scene.add( axesHelper );

function animate() {

  //判断x轴的属性等于5时,从0开始

  cube.position.x+=0.01

  if(cube.position.x>=5){//当x轴的属性等于5时从头开始

    cube.position.x=0

  }

  //下一帧再重新的渲染这个函数

  requestAnimationFrame( animate );

  // 开启局部旋转模式 旋转的距离x与y轴  鼠标的变化而变化

  //  cube.rotation.x += 0.01;

  //  cube.rotation.y += 0.01;

//使用渲染器,通过相机将场景渲染进来

  renderer.render( scene, camera );

}

//开始渲染一次 第一次

animate();

camera.position.z = 5;

入口文件index.html引入

<!DOCTYPE html>

<html lang="en">

<head>

  <meta charset="UTF-8">

  <meta http-equiv="X-UA-Compatible" content="IE=edge">

  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>Document</title>

  <link rel="stylesheet" href="../src/assets/css/style.css">

</head>

<body>

   <script src="../src/main/main.js" type="module"></script>

</body>

</html>

所有的文档均从官网下载后,运行找到

本地的loclhost:8080....地址,点击文档即可打开。

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

共创splendid--与您携手

鼓励会让我为您创造出更优质作品

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值