当前位置:网站首页>threejs的控制器 立方体空间 基本控制器+惯性控制+飞行控制
threejs的控制器 立方体空间 基本控制器+惯性控制+飞行控制
2022-07-02 11:21:00 【门前大桥下.】
threejs的控制器 立方体空间 基本控制器+惯性控制+飞行控制
import React, {
Component} from 'react';
import * as THREE from "three";
import {
OrbitControls} from "three/examples/jsm/controls/OrbitControls";
// import {reflect} from "three/examples/jsm/nodes/shadernode/ShaderNodeBaseElements";
// import {Refractor} from "three/examples/jsm/objects/Refractor";
// import {Reflector} from "three/examples/jsm/objects/Reflector";
//更加 有惯性的OrbitControls 的升级基本
import {
TrackballControls} from "three/examples/jsm/controls/TrackballControls";
//飞行控制 鼠标操作视角 的前进和方向
import {
FlyControls} from "three/examples/jsm/controls/FlyControls";
const clock=new THREE.Clock()
class Index extends Component {
ready:false
constructor(args) {
super(args);
this.state={
width:window.innerWidth,
height:window.innerHeight,
count:0,
}
this.ballpoi={
x:0,
y:100,
z:0,
vx:(Math.random()-0.5)*7,
vy:(Math.random()-0.5)*7,
vz:(Math.random()-0.5)*7
}
}
render() {
return (
<div className={
'three-canvas'}>
</div>
);
}
componentDidMount() {
if(this.state.count===0){
this.init()
// eslint-disable-next-line react/no-direct-mutation-state
this.state.count++
}
}
getRenderer(){
//设置
const renderer=new THREE.WebGLRenderer()
renderer.setSize(this.state.width,this.state.height)
document.querySelector('.three-canvas').appendChild(renderer.domElement)
return renderer
}
getCamera(){
const camera=new THREE.PerspectiveCamera(75,this.state.width/this.state.height,1,1000)
camera.position.z=400
camera.lookAt(0,0,0)
return camera
}
getScene(){
const scene=new THREE.Scene()
return scene
}
getControl(){
const control=new FlyControls(this.camera,this.renderer.domElement)
control.movementSpeed=50
control.autoForward =false
control.rollSpeed=0.5
return control
}
addLight(){
//环境光
const light = new THREE.AmbientLight( 0xffffff,0.5);
const pointLight = new THREE.PointLight( 0xffffff,1);
pointLight.position.set(0,0,0)
pointLight.castShadow=true
this.scene.add( light ,pointLight);
}
addPlane(){
const geometry=new THREE.BoxGeometry(Math.random()*10,Math.random()*10,Math.random()*10)
const material = new THREE.MeshNormalMaterial({
side: THREE.DoubleSide})
const box= new THREE.Mesh(
geometry,
material
)
box.rotation.set(Math.PI/(Math.random()*10),Math.PI/(Math.random()*10),Math.PI/(Math.random()*10))
box.position.set((Math.random()-0.5)*300,(Math.random()-0.5)*300,(Math.random()-0.5)*300)
this.scene.add(box)
return box
}
init(){
//创建rander --相机 ---场景----动画
this.renderer=this.getRenderer()
this.camera=this.getCamera()
this.scene=this.getScene()
this.scene.background=new THREE.Color('#d1b8b8')
for (let i=0;i<400;i++){
this.addPlane()
}
//添加灯光
this.addLight()
//添加控制器
this.control=this.getControl()
//执行动画
this.animation()
}
animation(){
requestAnimationFrame(this.animation.bind(this))
this.renderer.render(this.scene,this.camera)
this.control.update(clock.getDelta())
}
}
export default Index;
边栏推荐
猜你喜欢

Fabric. JS free draw circle

YOLOv3&YOLOv5输出结果说明

Dangbei projection 4K laser projection X3 Pro received unanimous praise: 10000 yuan projector preferred
![[development environment] Dell computer system reinstallation (download Dell OS recovery tool | use Dell OS recovery tool to make USB flash disk system | install system)](/img/e0/e9cb42f241a60995d4598ba6a1a2fe.jpg)
[development environment] Dell computer system reinstallation (download Dell OS recovery tool | use Dell OS recovery tool to make USB flash disk system | install system)

2022家用投影仪首选!当贝F5强悍音画效果带来极致视听体验

Borui data integrated intelligent observable platform was selected into the "Yunyuan production catalogue" of China Academy of communications in 2022

QT new project_ MyNotepad++

QT new project

联合搜索:搜索中的所有需求

Design of non main lamp: how to make intelligent lighting more "intelligent"?
随机推荐
Fabric.js 上划线、中划线(删除线)、下划线
Selenium element positioning method
每日学习2
Openharmony notes --------- (4)
Use of freemaker
Chinese science and technology from the Winter Olympics (III): the awakening and evolution of digital people
PyQt5_ Qscrollarea content is saved as a picture
docker mysql
默认插槽,具名插槽,作用域插槽
Go operation redis
Fabric. JS upper dash, middle dash (strikethrough), underline
给Android程序员的一些面试建议「建议收藏」
测试框架TestNG的使用(二):testNG xml的使用
无主灯设计:如何让智能照明更加「智能」?
PyQt5_QScrollArea内容保存成图片
Fabric.js 缩放画布
Golang quickly generates model and queryset of database tables
每天坚持20分钟go的基础二
Systemserver process
kaggle如何使用utility script