当前位置:网站首页>three. Basic framework created by JS
three. Basic framework created by JS
2022-06-24 19:19:00 【Simple bird learning code··】
three.js Basic framework created

First, two tutorial documents are given
Download package address
file 1
file 2
1. First let's download three library
npm i [email protected]0.139 -S
perhaps
npm i [email protected]0.133 -S
This article is based on these two versions 
This is mainly about 3 Introductions
1.three.jsIt's a direct introductionTHREEobject
2.three.module.jsIs in html Kind of script Oftype=moduleIntroduced
3.jsmIt is the reference of some plug-ins

The main difference is module It is different from the ordinary introduction, or an error will be reported
2. Build the basic framework
// Create a scene
var scene = new THREE.Scene();
// Create objects 100*100*100
var geometry = new THREE.BoxGeometry(100, 100, 100);
var material = new THREE.MeshLambertMaterial({
color: 0x4d90fe,// The color of the object
opacity: 0.4,// Object transparency
transparent: true// To enable transparency, you must have , Otherwise, even if there is transparency, it will not work
});
var cube = new THREE.Mesh(geometry, material);// Add objects to the mesh body
scene.add(cube); // Objects are added to the scene
cube.position.set(50, 50, 50)// Set the position of the object
// Add light source
const ambient = new THREE.AmbientLight(0xffffff, 0.5)// Add ambient light
scene.add(ambient)
//const pointLight = new THREE.PointLight(0xffffff, 0.6)// Add electro-optic
//pointLight.position.set(200, 200, 210)// Set the spot light position
//scene.add(pointLight)
// Add axis red x green y blue z The color can be set later The parameter is the length of the coordinate axis Too small will be in the object
const axesHelper = new THREE.AxesHelper(150)
scene.add(axesHelper)
// Visualize point lights
const pointLightHelper = new THREE.PointLightHelper(pointLight, 10);// Similar to the light source above
scene.add(pointLightHelper);
// Add a camera
const width = 1000
const height = 1000
var camera = new THREE.PerspectiveCamera(450, width / height, 10, 1000); // The camera angle Near point Stay away
camera.position.set(100, 200, 200) // Camera position
camera.lookAt(300, 0, 300)// The camera looks at that point
// Add renderer
var renderer = new THREE.WebGLRenderer();
renderer.setSize(width, height);// Render size
document.querySelector('#app').appendChild(renderer.domElement); // Rendered dom
// Animation
const clock = new THREE.Clock()// Default 1s60 frame So you can cycle through the animation
function render() {
const spt = clock.getDelta()
// console.log(' Render interval ',spt);
renderer.render(scene, camera) // Camera rendering
cube.rotateY(0.001)
requestAnimationFrame(render)
}
render()
// The camera control can be dragged by viewing angle
const controls = new THREE.OrbitControls(camera, renderer.domElement)// Camera controls
controls.target.set(300, 0, 300)// With the camera lookAt The coordinates are the same
controls.update()// Triggering event
Here is a pit ,
controls.target.set(300, 0, 300)// With the camera lookAt The coordinates are the same
controls.update()// Triggering event
If this string of code is not added, it will affect the coordinate origin , Find that even if you setcamera.lookAt(300, 0, 300)// The camera looks at that pointCamera point but it doesn't work , Add these two lines of code updated It will be executed internallycamera.lookAt.targetHow to change So if you find that you have set the coordinate origin but have not changed the origin , You can add these two lines of code
This expands a knowledge point
requestAnimationFrameThis is a window One way to do this is ES6 Of , Its function is to execute every second 60 Always execute , amount tosetInterval
边栏推荐
- Buddha bless you that there will never be a bug
- 论文解读(SR-GNN)《Shift-Robust GNNs: Overcoming the Limitations of Localized Graph Training Data》
- R语言 4.1.0软件安装包和安装教程
- Use ado Net call stored procedure
- 【计算讲谈社】第三讲:如何提出关键问题?
- Dataworks development ODPs SQL development production environment automatic completion of ProjectName
- Starring V6 platform development take out point process
- Generate the last login user account report of the computer through SCCM SQL
- Does finkcdc support sqlserver2008?
- 优维低代码:构件渲染子构件
猜你喜欢

电源噪声分析

Freeswitch使用originate转dialplan

Experience of MDM master data project implementation for manufacturing projects

Sr-gnn shift robot gnns: overlapping the limitations of localized graph training data

Introduction, download and use of global meteorological data CRU ts from 1901 to 2020

电源效率测试

Sr-gnn shift robot gnns: overlapping the limitations of localized graph training data

为什么 useEvent 不够好

西北工业大学遭黑客攻击?双因素认证改变局面!

How to customize cursor position in wechat applet rotation chart
随机推荐
【Leetcode】旋转系列(数组、矩阵、链表、函数、字符串)
Preliminary study nuxt3
starring开发HttpJson接入点+数据库
西北工业大学遭黑客攻击?双因素认证改变局面!
香港服务器租用错误可能导致严重后果
How to perform power regression in R
我链接mysql 报这个错 是啥意思呀?
Volcano becomes spark default batch scheduler
Sentry series satellite introduction and download tutorial
NOKOV动作捕捉系统使多场协同无人机自主建造成为可能
Introduction to smart contract security audit delegatecall (2)
假如,程序员面试的时候说真话
Nokov motion capture system makes it possible for multi field cooperative UAV to build independently
电源噪声分析
論文解讀(SR-GNN)《Shift-Robust GNNs: Overcoming the Limitations of Localized Graph Training Data》
Interpreting harmonyos application and service ecology
Intel and Microsoft give full play to the potential energy of edge cloud collaboration to promote the large-scale deployment of AI
subject may not be empty [subject-empty]
Sr-gnn shift robot gnns: overlapping the limitations of localized graph training data
Generate the last login user account report of the computer through SCCM SQL