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

相关推荐
不爱吃糖的程序媛23 分钟前
浅谈前端架构设计与工程化
前端·前端架构设计
BillKu2 小时前
Vue3 Element Plus 对话框加载实现
javascript·vue.js·elementui
郝YH是人间理想2 小时前
系统架构设计师案例分析题——web篇
前端·软件工程
Evaporator Core2 小时前
深入探索:Core Web Vitals 进阶优化与新兴指标
前端·windows
初遇你时动了情3 小时前
html js 原生实现web组件、web公共组件、template模版插槽
前端·javascript·html
QQ2740287563 小时前
Soundness Gitpod 部署教程
linux·运维·服务器·前端·chrome·web3
前端小崔3 小时前
从零开始学习three.js(18):一文详解three.js中的着色器Shader
前端·javascript·学习·3d·webgl·数据可视化·着色器
哎呦你好3 小时前
HTML 表格与div深度解析区别及常见误区
前端·html
运维@小兵3 小时前
vue配置子路由,实现点击左侧菜单,内容区域显示不同的内容
前端·javascript·vue.js
koiy.cc4 小时前
记录:echarts实现tooltip的某个数据常显和恢复
前端·echarts