使用vue3+ts实现three.js官方案例(第一期)

前言

因为想学习下vue3、ts以及对three.js感兴趣,所以突发奇想准备把官方的案例使用vue来实现。虽然复刻这些案例对ts以及vue3相关的语法几乎没啥使用,但是聊胜于无嘛哈哈。后面有空就会复刻一个相对好看点的案例。希望对大家有所帮助

效果展示

在这里插入图片描述

视频

three_demo_webgpu_mirror

源码
<template>
    <div>
        <div id="info"></div>
    </div>
</template>
<script lang="ts" setup>
import * as THREE from 'three';
import { MeshPhongNodeMaterial, reflector, uv, texture, color } from 'three/examples/jsm/nodes/Nodes';
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
import WebGPURenderer from 'three/examples/jsm/renderers/webgpu/WebGPURenderer';
import { onMounted } from "vue";


let camera,scene,renderer;
let cameraControls;
let sphereGroup,smallSphere;
onMounted(() => {
  init();
})
function init(){
    //场景
    scene = new THREE.Scene();
    //相机
    camera = new THREE.PerspectiveCamera( 45, window.innerHeight / window.innerWidth, 1, 500);
    camera.position.set(0,75,160)
    
    let geometry,material;

    sphereGroup = new THREE.Object3D();
    scene.add(sphereGroup);

    geometry = new THREE.CylinderGeometry( 0.1, 15 * Math.cos( Math.PI / 180 * 30), 0.1, 24, 1);
    material = new THREE.MeshPhongMaterial( { color: 0xffffff, emissive: 0x8d8d8d});
    const sphereCap = new THREE.Mesh( geometry, material );
	sphereCap.position.y = - 15 * Math.sin( Math.PI / 180 * 30 ) - 0.05;
	sphereCap.rotateX( - Math.PI );

    geometry = new THREE.SphereGeometry( 15, 24, 24, Math.PI / 2, Math.PI * 2, 0, Math.PI / 180 * 120 );
				const halfSphere = new THREE.Mesh( geometry, material );
				halfSphere.add( sphereCap );
				halfSphere.rotateX( - Math.PI / 180 * 135 );
				halfSphere.rotateZ( - Math.PI / 180 * 20 );
				halfSphere.position.y = 7.5 + 15 * Math.sin( Math.PI / 180 * 30 );

				sphereGroup.add( halfSphere );

				geometry = new THREE.IcosahedronGeometry( 5, 0 );
				material = new THREE.MeshPhongMaterial( { color: 0xffffff, emissive: 0x7b7b7b, flatShading: true } );
				smallSphere = new THREE.Mesh( geometry, material );
				scene.add( smallSphere );

				// textures

				const textureLoader = new THREE.TextureLoader();

				const floorNormal = textureLoader.load( 'textures/floors/FloorsCheckerboard_S_Normal.jpg' );
				floorNormal.wrapS = THREE.RepeatWrapping;
				floorNormal.wrapT = THREE.RepeatWrapping;

				const decalDiffuse = textureLoader.load( 'textures/decal/decal-diffuse.png' );
				decalDiffuse.colorSpace = THREE.SRGBColorSpace;

				const decalNormal = textureLoader.load( 'textures/decal/decal-normal.jpg' );

				// reflectors / mirrors

				const groundReflector = reflector();
				const verticalReflector = reflector();

				const groundNormalScale = - 0.08;
				const verticalNormalScale = 0.1;

				const groundUVOffset = texture( decalNormal ).xy.mul( 2 ).sub( 1 ).mul( groundNormalScale );
				const verticalUVOffset = texture( floorNormal, uv().mul( 5 ) ).xy.mul( 2 ).sub( 1 ).mul( verticalNormalScale );

				groundReflector.uvNode = groundReflector.uvNode.add( groundUVOffset );
				verticalReflector.uvNode = verticalReflector.uvNode.add( verticalUVOffset );

				const groundNode = texture( decalDiffuse ).a.mix( color( 0xffffff ), groundReflector );
				const verticalNode = color( 0x0000ff ).mul( .1 ).add( verticalReflector );

				// walls

				const planeGeo = new THREE.PlaneGeometry( 100.1, 100.1 );

				//

				const planeBottom = new THREE.Mesh( planeGeo, new MeshPhongNodeMaterial( {
					colorNode: groundNode
				} ) );
				planeBottom.rotateX( - Math.PI / 2 );
				planeBottom.add( groundReflector.target );
				scene.add( planeBottom );

				const planeBack = new THREE.Mesh( planeGeo, new MeshPhongNodeMaterial( {
					colorNode: verticalNode
				} ) );
				planeBack.position.z = - 50;
				planeBack.position.y = 50;
				planeBack.add( verticalReflector.target );
				scene.add( planeBack );

				//

				const planeTop = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xffffff } ) );
				planeTop.position.y = 100;
				planeTop.rotateX( Math.PI / 2 );
				scene.add( planeTop );

				const planeFront = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0x7f7fff } ) );
				planeFront.position.z = 50;
				planeFront.position.y = 50;
				planeFront.rotateY( Math.PI );
				scene.add( planeFront );

				const planeRight = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0x00ff00 } ) );
				planeRight.position.x = 50;
				planeRight.position.y = 50;
				planeRight.rotateY( - Math.PI / 2 );
				scene.add( planeRight );

				const planeLeft = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xff0000 } ) );
				planeLeft.position.x = - 50;
				planeLeft.position.y = 50;
				planeLeft.rotateY( Math.PI / 2 );
				scene.add( planeLeft );

				// lights

				const mainLight = new THREE.PointLight( 0xe7e7e7, 2.5, 250, 0 );
				mainLight.position.y = 60;
				scene.add( mainLight );

				const greenLight = new THREE.PointLight( 0x00ff00, 0.5, 1000, 0 );
				greenLight.position.set( 550, 50, 0 );
				scene.add( greenLight );

				const redLight = new THREE.PointLight( 0xff0000, 0.5, 1000, 0 );
				redLight.position.set( - 550, 50, 0 );
				scene.add( redLight );

				const blueLight = new THREE.PointLight( 0xbbbbfe, 0.5, 1000, 0 );
				blueLight.position.set( 0, 50, 550 );
				scene.add( blueLight );

				// renderer

				renderer = new WebGPURenderer( { antialias: true } );
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );
				renderer.setAnimationLoop( animate );
				document.body.appendChild( renderer.domElement );

				// controls

				cameraControls = new OrbitControls( camera, renderer.domElement );
				cameraControls.target.set( 0, 40, 0 );
				cameraControls.maxDistance = 400;
				cameraControls.minDistance = 10;
				cameraControls.update();

				window.addEventListener( 'resize', onWindowResize );


}
function onWindowResize() {

camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();

renderer.setSize( window.innerWidth, window.innerHeight );

}

function animate() {

const timer = Date.now() * 0.01;

sphereGroup.rotation.y -= 0.002;

smallSphere.position.set(
    Math.cos( timer * 0.1 ) * 30,
    Math.abs( Math.cos( timer * 0.2 ) ) * 20 + 5,
    Math.sin( timer * 0.1 ) * 30
);
smallSphere.rotation.y = ( Math.PI / 2 ) - timer * 0.1;
smallSphere.rotation.z = timer * 0.8;

renderer.render( scene, camera );

}

</script>


注意事项

相关的资源文件一定要放在public下,否则会加载不出来。以下是我目前的目录结构:
在这里插入图片描述

项目地址:

https://gitee.com/chenfangqi/three.js

three.js官方gitbub地址:

https://github.com/mrdoob/three.js/tree/master

第一期到此结束,希望大家喜欢~

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!对于使用Vue 3、TypeScript和Vite来实现一个看房自由的应用,可以结合Three.js这个D图形库来实现。下面是一个简单的步骤指南: 1. 首先,确保你已经安装了Node.js和npm。 2. 创建一个新的Vue项目,可以使用Vue CLI来快速搭建一个基本的项目结构。 ```bash npm install -g @vue/cli vue create my-project ``` 3. 在Vue项目中安装Vite作为开发服务器。 ```bash cd my-project npm install -D create-vite npx create-vite ``` 4. 安装Three.js库和相关依赖。 ```bash npm install three ``` 5. 在Vue组件中引入Three.js库,并开始编写代码来实现看房自由功能。 ```typescript <template> <div ref="container"></div> </template> <script lang="ts"> import { ref, onMounted } from 'vue'; import * as THREE from 'three'; export default { setup() { const container = ref(null); onMounted(() => { // 创建场景、相机和渲染器 const scene = new THREE.Scene(); const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000); const renderer = new THREE.WebGLRenderer(); // 设置渲染器的大小并将其添加到DOM中 renderer.setSize(window.innerWidth, window.innerHeight); container.value.appendChild(renderer.domElement); // 创建一个立方体并将其添加到场景中 const geometry = new THREE.BoxGeometry(); const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 }); const cube = new THREE.Mesh(geometry, material); scene.add(cube); // 设置相机的位置 camera.position.z = 5; // 动画循环 const animate = function () { requestAnimationFrame(animate); // 使立方体旋转起来 cube.rotation.x += 0.01; cube.rotation.y += 0.01; // 渲染场景和相机 renderer.render(scene, camera); }; animate(); }); return { container, }; }, }; </script> <style> #container { width: 100%; height: 100%; } </style> ``` 这只是一个简单的示例,你可以根据自己的需求来构建更复杂的场景和交互逻辑。希望对你有所帮助!如有任何疑问,欢迎继续提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值