粗暴一点,直接上代码:
第一步:
通过指令下载three.js
?
1
|
npm install three -S
|
第二步:
在组件中引用
?
1
|
import * as THREE from 'three'
|
第三步:
html部分
?
1
|
< div id = "container" ></ div >
|
js部分
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
<script>
import * as THREE from 'three' ;
var camera;
var renderer;
var scene;
export default {
name: 'panorama' ,
data() {
return {
bigImg: require( "../../../../../images/项目案例/001.jpg" ), //全景图图片路径
}
},
mounted() {
// 调用全景图函数
this .$nextTick(() => {
this .init();
this .animate();
})
},
methods: {
// 全景图配置函数---------------
init() {
var container = document.getElementById( 'container' );
// 创建渲染器
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio(window.devicePixelRatio);
// 设置画布的宽高
renderer.setSize(window.innerWidth, window.innerHeight);
// 判断容器中子元素的长度
let childs = container.childNodes;
if (container.childNodes.length > 0) {
container.removeChild(childs[0]);
container.appendChild(renderer.domElement);
} else {
container.appendChild(renderer.domElement);
}
// container.appendChild(renderer.domElement);
// 创建场景
scene = new THREE.Scene();
// 创建相机
camera = new THREE.PerspectiveCamera(90, window.innerWidth / window.innerHeight, 0.1, 100);
camera.position.set(0, 0, 0);
var material = new THREE.MeshBasicMaterial(); //材质
var texture = new THREE.TextureLoader().load( this .bigImg);
material.map = texture;
var skyBox = new THREE.Mesh(
new THREE.SphereBufferGeometry(100, 100, 100),
material
);
skyBox.geometry.scale(1, 1, -1);
scene.add(skyBox);
window.addEventListener( 'resize' , this .onWindowResize, false );
var bMouseDown = false ;
var x = -1;
var y = -1;
// 添加鼠标事件
container.onmousedown = function (event) {
event.preventDefault(); //取消默认事件
x = event.clientX;
y = event.clientY;
bMouseDown = true ;
}
container.onmouseup = function (event) {
event.preventDefault();
bMouseDown = false ;
}
container.onmousemove = function (event) {
event.preventDefault();
if (bMouseDown) {
skyBox.rotation.y += -0.005 * (event.clientX - x);
skyBox.rotation.x += -0.005 * (event.clientY - y);
if (skyBox.rotation.x > Math.PI / 2) {
skyBox.rotation.x = Math.PI / 2
}
if (skyBox.rotation.x < -Math.PI / 2) {
skyBox.rotation.x = -Math.PI / 2
}
x = event.clientX;
y = event.clientY;
}
}
container.onmousewheel = function (event) {
event.preventDefault();
if (event.wheelDelta != 0) {
camera.fov += event.wheelDelta > 0 ? 1 : -1;
if (camera.fov > 150) {
camera.fov = 150;
}
else if (camera.fov < 30) {
camera.fov = 30;
}
camera.updateProjectionMatrix();
}
}
},
onWindowResize() {
// 窗口缩放的时候,保证场景也跟着一起缩放
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
},
animate() {
requestAnimationFrame( this .animate);
renderer.render(scene, camera);
}
},
}
</script>
|
相关文章
猜你喜欢
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-29 92
-
2025-05-27 88
-
2025-05-27 56
-
2025-05-25 59
-
2025-05-29 57
热门评论