通过对three官网案例标注学习three2

<!DOCTYPE html>
<html lang="en">
	<head>
		<title>three.js webgl - effects - parallax barrier</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<link type="text/css" rel="stylesheet" href="main.css">
	</head>

	<body>
		<div id="info">
			<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - effects - parallax barrier<br/>
			skybox by <a href="https://www.pauldebevec.com/" target="_blank" rel="noopener">Paul Debevec</a>
		</div>

		<!-- Import maps polyfill -->
		<!-- Remove this when import maps will be widely supported -->
		<script async src="https://unpkg.com/es-module-shims@1.6.3/dist/es-module-shims.js"></script>

		<script type="importmap">
			{
				"imports": {
					"three": "../js/three.module.js",
					"three/addons/": "./js/"
				}
			}
		</script>

		<script type="module">
			import * as THREE from 'three';
			import { ParallaxBarrierEffect } from './js/ParallaxBarrierEffect.js';

			let container, camera, scene, renderer, effect;

			const spheres = [];

			let mouseX = 0;
			let mouseY = 0;
			let windowHalfX = window.innerWidth / 2;
			let windowHalfY = window.innerHeight / 2;
			// 绑定鼠标移动事件
			document.addEventListener( 'mousemove', onDocumentMouseMove );

			init();
			animate();

			function init() {

				container = document.createElement( 'div' );
				document.body.appendChild( container );
				// 摄像机
				camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.01, 100 );
				camera.position.z = 3;
				camera.focalLength = 3;//焦距
				// 导入房屋场景贴图 nx.png等
				const path = 'textures/cube/pisa/';
				const format = '.png';
				const urls = [
					path + 'px' + format, path + 'nx' + format,
					path + 'py' + format, path + 'ny' + format,
					path + 'pz' + format, path + 'nz' + format
				];
				const textureCube = new THREE.CubeTextureLoader().load( urls );
				// 场景
				scene = new THREE.Scene();
				scene.background = textureCube;// 给场景添加背景
				// 小球
				const geometry = new THREE.SphereGeometry( 0.1, 32, 16 );
				const material = new THREE.MeshBasicMaterial( { color: 0xffffff, envMap: textureCube } );
				for ( let i = 0; i < 500; i ++ ) {
					// 批量创造小球
					const mesh = new THREE.Mesh( geometry, material );
					// 小球随机初始位置
					mesh.position.x = Math.random() * 10 - 5;
					mesh.position.y = Math.random() * 10 - 5;
					mesh.position.z = Math.random() * 10 - 5;
					// 小球随机大小
					mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 3 + 1;
					// 场景添加小球
					scene.add( mesh );
					// 记录小球信息,后续使用
					spheres.push( mesh );

				}

				//渲染器
				renderer = new THREE.WebGLRenderer();
				renderer.setPixelRatio( window.devicePixelRatio );//设备像素比
				renderer.useLegacyLights = false;//渲染管线
				container.appendChild( renderer.domElement );

				const width = window.innerWidth || 2;
				const height = window.innerHeight || 2;
				// 视差屏障效果
				// 视差屏障式 3D 立体显示(Parallax Barriers) 视差屏障式 3D 立体显示屏幕,
				// 是在屏幕表面设置称为「视差屏障」的纵向栅栏状光学屏障来控制光线行进方向,让左右两眼接受不同影像产生视差达成立体显示效果。
				effect = new ParallaxBarrierEffect( renderer );
				effect.setSize( width, height );

				//窗口自适应
				window.addEventListener( 'resize', onWindowResize );

			}
			//窗口自适应
			function onWindowResize() {
				// aspect属性:设置摄像机视口比例
				camera.aspect = window.innerWidth / window.innerHeight;
				//更新摄像机投影矩阵。在任何参数被改变以后必须被调用。
				camera.updateProjectionMatrix();
				// 视差屏障效果更新
				effect.setSize( window.innerWidth, window.innerHeight );
			}
			// 鼠标移动事件
			function onDocumentMouseMove( event ) {
				
				windowHalfX = window.innerWidth / 2;
				windowHalfY = window.innerHeight / 2;
				mouseX = ( event.clientX - windowHalfX ) / 100;
				mouseY = ( event.clientY - windowHalfY ) / 100;

			}

			//渲染动画
			function animate() {
				requestAnimationFrame( animate );
				render();
			}

			function render() {

				const timer = 0.0001 * Date.now();

				camera.position.x += ( mouseX - camera.position.x ) * .05;
				camera.position.y += ( - mouseY - camera.position.y ) * .05;

				camera.lookAt( scene.position );
				// 小球飘动
				for ( let i = 0, il = spheres.length; i < il; i ++ ) {

					const sphere = spheres[ i ];

					sphere.position.x = 5 * Math.cos( timer + i );
					sphere.position.y = 5 * Math.sin( timer + i * 1.1 );

				}
				// renderer.render(scene,camera)
				effect.render( scene, camera );// 视差屏障效果渲染

			}

		</script>

	</body>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值