当前位置:网站首页>three.js创建的基础框架
three.js创建的基础框架
2022-06-24 18:33:00 【小菜鸟学代码··】
three.js创建的基础框架

1. 首先我们先下载three库
npm i [email protected]0.139 -S
或者
npm i [email protected]0.133 -S
这篇文章就是基于这两个版本写的
这里主要讲3个引入
1.three.js是直接引入THREE对象
2.three.module.js是在html种script的type=module种引入的
3.jsm是一些插件的引用在里面

主要区分module跟普通引入的区别不然会报错
2.构建基本框架
//创建场景
var scene = new THREE.Scene();
//创建物体 100*100*100
var geometry = new THREE.BoxGeometry(100, 100, 100);
var material = new THREE.MeshLambertMaterial({
color: 0x4d90fe,//物体颜色
opacity: 0.4,//物体透明度
transparent: true//开启透明这个必须要有,不然就算有透明度也没效果
});
var cube = new THREE.Mesh(geometry, material);//把物体添加到网格体中
scene.add(cube); //物体添加到场景中
cube.position.set(50, 50, 50)//设置物体的位置
// 添加光源
const ambient = new THREE.AmbientLight(0xffffff, 0.5)//添加环境光
scene.add(ambient)
//const pointLight = new THREE.PointLight(0xffffff, 0.6)//添加电光
//pointLight.position.set(200, 200, 210)//设置点光位置
//scene.add(pointLight)
//添加坐标轴 红x 绿y 蓝z 颜色可以后期自己设置 参数是坐标轴长短 太小就会在物体里面
const axesHelper = new THREE.AxesHelper(150)
scene.add(axesHelper)
// 可视化点光源
const pointLightHelper = new THREE.PointLightHelper(pointLight, 10);//跟上面的光源差不多
scene.add(pointLightHelper);
// 添加摄像机
const width = 1000
const height = 1000
var camera = new THREE.PerspectiveCamera(450, width / height, 10, 1000); //摄像机 角度 近点 远点
camera.position.set(100, 200, 200) //摄像机位置
camera.lookAt(300, 0, 300)//相机看向那个点
// 添加渲染器
var renderer = new THREE.WebGLRenderer();
renderer.setSize(width, height);//渲染的大小
document.querySelector('#app').appendChild(renderer.domElement); //渲染的dom
//动画
const clock = new THREE.Clock()//默认1s60帧 这样就可以进行循环动画
function render() {
const spt = clock.getDelta()
// console.log('渲染间隔时间',spt);
renderer.render(scene, camera) //相机渲染
cube.rotateY(0.001)
requestAnimationFrame(render)
}
render()
//相机控件是可以进行视角拖动的
const controls = new THREE.OrbitControls(camera, renderer.domElement)//相机控件
controls.target.set(300, 0, 300)//跟相机的lookAt坐标一致
controls.update()//触发事件
这里延申一个坑点,
controls.target.set(300, 0, 300)//跟相机的lookAt坐标一致
controls.update()//触发事件
这串代码如果不加的话就会影响坐标原点,发现就算你设置了camera.lookAt(300, 0, 300)//相机看向那个点相机的点但是并没有生效,加了这两行代码updated内部就会自己执行camera.lookAt.target的改变方法 所以如果发现你设置了坐标原点但是并没有改变原点,你可以加上这两行代码
这里面扩展一个知识点
requestAnimationFrame这是window的一个方法也是ES6的,其作用就是可以每秒执行60次一直执行,相当于setInterval
边栏推荐
- Flex box flex attribute
- About pyqt5 to realize paging function (one window implements different interfaces)
- 25.sql statement differentiation
- Application service access configuration parameters
- 2022 network security C module of the secondary vocational group scans the script of the surviving target aircraft (municipal, provincial and national)
- 360 digital released information security trends in January: 120000 fraud risks were captured and users were reminded 2.68 million times
- 13 skills necessary for a competent QA Manager
- How does the video platform import the old database into the new database?
- Sudoku (easy to understand)
- Five advantages and disadvantages of Bi
猜你喜欢

Microservice system design - sub service project construction

Mcu-08 interrupt system and external interrupt application
![[NLP] 3 papers on how Stanford team builds a better chat AI](/img/f1/1c2ff31a728152395618800600df45.jpg)
[NLP] 3 papers on how Stanford team builds a better chat AI

It is often blocked by R & D and operation? You need to master the 8 steps before realizing the requirements

Eight digit

Recommend a distributed JVM monitoring tool, which is very practical!

Microservice system design -- interface document management design

微服务系统设计——数据模型与系统架构设计

Nine practical guidelines for improving responsive design testing

Leetcode weekly buckle 281
随机推荐
SAP license: what is ERP supply chain
Industry Cloud video editing software
next_ Permutation full permutation function
Digital transformation informatization data planning and technology planning
Restcloud ETL extracting dynamic library table data
Online sequence flow chart making tool
2022 network security C module of the secondary vocational group scans the script of the surviving target aircraft (municipal, provincial and national)
Is it safe to open an account online? What should I do?
Leetcode daily question solution: 717 1-bit and 2-bit characters - reverse order
How does the chief information security officer discuss network security with the enterprise board of directors
Selection (033) - what is the output of the following code?
Data driven decision making: Decision intelligence and design thinking
What makes data analysts good- Cassie Kozyrkov
History object
股票网上开户安全吗?应该怎么办理?
110. balanced binary tree
Eight digit
Does the wave of layoffs in Chinese enterprises in 2021 need to be "judged" by morality?
Analysis on the issue of raising the right of MSSQL in 2021 secondary vocational group network security competition in Shandong Province
360 digital released information security trends in January: 120000 fraud risks were captured and users were reminded 2.68 million times