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

相关推荐
斯维赤19 分钟前
三级缓存为什么是三级?二级缓存不够吗
前端·后端·面试
锋行天下1 小时前
LangGraph 对接 MCP 实战指南:从本地开发到线上部署与模型防坑
前端·人工智能·后端
Highcharts.js1 小时前
Highcharts与TypeScript集成指南
前端·javascript·typescript·前端框架·数据可视化·highcharts
跳跳的小古风2 小时前
前端转全栈:一条接口从前端请求到 SQL 完整流程
java·前端
亿元程序员2 小时前
微信小游戏备案很久?这样操作可以直接发布!
前端
福狼2 小时前
The Expanse:让风沙成为世界的一部分
前端·three.js
程序员鱼皮2 小时前
这 3 年 AI Agent 到底进化了啥?万字大白话,一次讲透!
前端·后端·aigc
用户900305093622 小时前
Claude Code平替评测:换掉之前先理清楚这些选型条件
前端
bai2732 小时前
同一个 JSON,Node 读出了三种坏法:Windows 上四种写法的编码实测
前端
ZANE2 小时前
纯前端实现的 .gitignore 生成器技术实现
前端