初识three

three.js 的基本应用

安装THREE.js 官网

	npm install three -S
	//方便测试(非必要)
	npm install gsap

导入要使用的插件

	import * as THREE from 'three'
	import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
	import gsap from 'gsap';

使用three.js

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

    const camera = new THREE.PerspectiveCamera(75, innerWidth / innerHeight, 0.1, 1000);// 创建相机

    camera.position.set(0, 0, 5);//设置相机位置

    const boxView = new THREE.BoxGeometry(1, 1, 1);//创建一个正方体
    const mesh = new THREE.MeshBasicMaterial({ color: '#fff000' });//创建一个素材

    const cube = new THREE.Mesh(boxView, mesh);//讲物体和素材放入一个盒子

    scene.add(cube);
    scene.add(camera);
    const renderer = new THREE.WebGL1Renderer({ antialias: true });
    renderer.setSize(innerWidth, innerHeight);
    //添加坐标轴
    scene.add(new THREE.AxesHelper(5))
    document.body.appendChild(renderer.domElement);

    //使用Clock 跟踪时间 解决requestAnimationFrame运行间隔不一样的问题
    // const clock = new THREE.Clock();
    


    //创建轨道控制器  
    const orbit = new OrbitControls(camera, renderer.domElement);
    //设置控制器阻尼
    orbit.enableDamping = true;//类似惯性
    render(scene, camera);



    //使用gsap 设置动画
    gsap.to(cube.position, {
        x: 5, duration: 5, onComplete: () => {
            console.log('动画完成回调')
        },
        repeat:6,//重复次数   -1 就是无限循环
        yoyo:true,//往返操作
        delay:2,//延迟几秒移动
    })//cube x轴 5秒 移动到5
    gsap.to(cube.rotation, { x: 2 * Math.PI, duration: 5, ease: 'powerl.inOut' })//ease:'powerl.inOut   速度入 匀速 加速度等

    function render() {
        // //两次间隔的时间
        // let deltaTIme =clock.getDelta();
        // //移动
        // cube.position.x +=number * deltaTIme *10;
        // //缩放
        // cube.scale.set(cube.position.x ,cube.position.x ,cube.position.x );
        // //旋转
        // cube.rotation.set(Math.PI/cube.position.x,0,0);
        // if(cube.position.x >= 3){
        //     number = -0.01 ;
        // }
        // if(cube.position.x <=0){
        //     number =  0.01
        // }
        orbit.update();
        renderer.render(scene, camera);
        requestAnimationFrame(render);
    }
    //适应屏幕
    window.addEventListener('resize',()=>{
        camera.aspect = window.innerWidth/window.innerHeight;
        camera.updateProjectionMatrix();//更新矩阵
        renderer.setSize(window.innerWidth,window.innerHeight);
        renderer.setPixelRatio(window.devicePixelRatio);//更新设备的像素比例(移动端会有区别)
    })
    //双击全屏
    window.addEventListener('dblclick',()=>{
        if(!document.fullscreenElement){
            renderer.domElement.requestFullscreen();//请求全屏
        }else{
            document.exitFullscreen();
        }
        
    })

})()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值