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

相关推荐
Asort2 分钟前
JavaScript设计模式(十四)——命令模式:解耦请求发送者与接收者
前端·javascript·设计模式
小茴香35310 分钟前
Vue 脚手架(Vue CLI)
前端·javascript·vue.js
午安~婉15 分钟前
ESLint
前端·eslint·检查
“抚琴”的人16 分钟前
C#中获取程序执行时间
服务器·前端·c#
掘金一周27 分钟前
Flex 布局下文字省略不生效?原因其实很简单| 掘金一周 10.16
前端
Stringzhua32 分钟前
Vue的Axios介绍【9】
前端·javascript·vue.js
摸着石头过河的石头1 小时前
JavaScript 垃圾收集:内存管理的艺术
前端·javascript
前端小崽子1 小时前
🔥 踩坑实录:Fabric 在 Intel Iris Xe 显卡上 CPU 飙升 100%
前端
东华帝君1 小时前
React Suspense组件
前端
siaikin1 小时前
基于 Astro Starlight 的多框架文档
前端·vue.js·markdown