three3D的vite+vue版本基础代码

自己稍微处理一下目录结构

javascript 复制代码
<script setup>
  // 导入three.js 
import  * as THREE from 'three'

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

// 创建相机
const camera =new THREE.PerspectiveCamera(
    45, //视角
    window.innerWidth / window.innerHeight, //宽高比
    0.1, // 近平面
    1000 // 远平面
);

// 创建渲染器
const renderer= new THREE.WebGL1Renderer();
renderer.setSize(window.innerWidth,window.innerHeight);
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);


// 将网格添加到场景中
scene.add(cube);

// 设置相机位置
camera.position.z=5;
// 相机默认看向原点
camera.lookAt(0,0,0);




// 渲染函数
function animate(){
    requestAnimationFrame(animate);
    //旋转
    cube.rotation.x +=0.01;
    cube.rotation.y +=0.02;

    // 渲染
    renderer.render(scene,camera);
}


animate()
</script>

<template>

</template>

<style>
*{
  margin: 0;
  padding: 0;
}

canvas{
  display: block;
  position: fixed;
  left: 0;
  top: 0;
  width: 100vw;
  height: 100vh;
}

</style>

效果:

three3D

相关推荐
牧羊狼的狼3 小时前
React 中的 HOC 和 Hooks
前端·javascript·react.js·hooks·高阶组件·hoc
知识分享小能手4 小时前
React学习教程,从入门到精通, React 属性(Props)语法知识点与案例详解(14)
前端·javascript·vue.js·学习·react.js·vue·react
魔云连洲4 小时前
深入解析:Vue与React的异步批处理更新机制
前端·vue.js·react.js
mCell5 小时前
JavaScript 的多线程能力:Worker
前端·javascript·浏览器
超级无敌攻城狮6 小时前
3 分钟学会!波浪文字动画超详细教程,从 0 到 1 实现「思考中 / 加载中」高级效果
前端
excel7 小时前
用 TensorFlow.js Node 实现猫图像识别(教学版逐步分解)
前端
前端工作日常7 小时前
我学习到的Vue2.6的prop修饰符
vue.js
gnip8 小时前
JavaScript事件流
前端·javascript
小菜全8 小时前
基于若依框架Vue+TS导出PDF文件的方法
javascript·vue.js·前端框架·json
赵得C8 小时前
【前端技巧】Element Table 列标题如何优雅添加 Tooltip 提示?
前端·elementui·vue·table组件