three.js实现简单的跳一跳逻辑

无图不欢,先上静态图
在这里插入图片描述
图片资源下载
操作使用空格

<!DOCTYPE html>
<html>
<head>
    <meta name="keywords" content="风舞红枫,前端技术,canvas"/>
    <meta name="description" content="风舞红枫,前端技术,canvas,vue,react,node,个人博客"/>
    <title>three.js演示</title>
    <script type="text/javascript" src="three.js"></script>
    <script type="text/javascript" src="OrbitControls.js"></script>
    <style type="text/css">
        *{margin:0;padding:0;}
        canvas{display:block;}
    </style>
</head>
<body>
	<div id="fwhf"></div>
	<script type="text/javascript">
		window.onload = function(){
			class FwhfYZH{
				constructor(){
					this.speedStart = 0;
					this.speedMove = 0
					this.i = 0;
					this.init();	
				}
				init(){
					// 创建一个渲染器
					this.renderer = new THREE.WebGLRenderer({antialias: true});
					this.renderer.setSize(window.innerWidth,window.innerHeight);
					this.renderer.setClearColor('#222',1);
					this.renderer.shadowMap.enabled  = true;
					document.getElementById("fwhf").append(this.renderer.domElement);

					//创建一个场景
        			this.scene=new THREE.Scene();

        			//创建一个相机
					this.camera=new THREE.PerspectiveCamera(45,window.innerWidth/window.innerHeight,0.1,1000);
					this.camera.position.x=0;
			        this.camera.position.y=80;
			        this.camera.position.z=120;

			        this.help();
			        this.light();
			        this.cylinder();
			        this.character();
			        this.start();

					window.onkeydown = (e)=>{
						var e = e || event;
						if(e.keyCode == 32){
							if(this.cylind[this.i]['scaleIndex'] > 0.5){
								this.cylind[this.i]['scaleIndex'] -= 0.02;
								this.cylind[this.i].position.y -= 14*0.01;
								this.cylind[this.i].scale.set(1,this.cylind[this.i]['scaleIndex'],1);

								this.cube.position.y -= 14*0.02;
							}
						}
					}
					window.onkeyup = (e)=>{
						var e = e || event;
						if(e.keyCode == 32){
							this.speedStart = (1-this.cylind[this.i]['scaleIndex'])*10;
							this.speedMove = (1-this.cylind[this.i]['scaleIndex'])*10;

							this.cylind[this.i]['scaleIndex'] = 1;
							this.cylind[this.i].position.y = 7;
							this.cylind[this.i].scale.set(1,this.cylind[this.i]['scaleIndex'],1);

							this.cube.position.y = 17;
						}
					}
				}
				help(){
					//三维辅助线
				    var axes=new THREE.AxesHelper(40);
				    this.scene.add(axes);
				}
				light(){
				    // 创建光源
			        var light = new THREE.SpotLight(0xffffff,1,0);
			        light.position.set(40,60,100);
			        light.castShadow = true;
			        this.scene.add(light);
				}
				cylinder(){
					this.cylind = [];
					for(var i = 0 ; i < 50 ; i++){
						var texture = new THREE.TextureLoader().load( "tyt512.png" );
						var geometry = new THREE.CylinderGeometry( 14, 14, 14, 32 );
						var material = new THREE.MeshLambertMaterial( {map:texture,transparent:true,side:THREE.DoubleSide} );
						this.cylind[i] = new THREE.Mesh( geometry, material );
						this.cylind[i].position.y += 7;
						this.cylind[i].position.x -= 40*i;
						this.cylind[i]['scaleIndex'] = 1;
						this.cylind[i].receiveShadow = true;
						this.scene.add( this.cylind[i] );
					}
				}
				character(){
					var geometry = new THREE.BoxGeometry( 6, 6, 6 );
					var material = new THREE.MeshLambertMaterial();
					this.cube = new THREE.Mesh( geometry, material );
					this.cube.position.y = 17;
					this.cube.castShadow = true; 
					this.scene.add( this.cube );
					// this.camera.lookAt(this.cube.position);
				}
				characterAnimation(){
					if(this.speedStart != 0){
						this.cube.position.y += this.speedMove;
						this.cube.position.x -= (this.speedMove+this.speedStart)/2;
						this.camera.position.x -= (this.speedMove+this.speedStart)/2;
						// this.camera.lookAt(this.cube.position);
						// this.camera.position.z += (this.speedMove+this.speedStart)/4;
						this.speedMove -= 0.1;
						if(this.speedMove <= -this.speedStart){
							this.speedMove = this.speedStart = 0;
							this.cube.position.y = 17;
							this.i++;
							if(this.cube.position.x < this.i*(-40)-14 || this.cube.position.x > this.i*(-40)+14){
								alert('Game over');
								location.href = location.href;
							}
						}
					}
				}
				start(){
					this.controls = new THREE.OrbitControls(this.camera);
					this.controls.addEventListener('mousemove', this.renderer);
				    this.controls.update();
					this.animate();
				}
				animate(){
				    requestAnimationFrame( this.animate.bind(this) );
				    // this.controls.update();
				    this.characterAnimation();
				    this.renderer.render( this.scene, this.camera );
				}
				rand(n,m){
					var c = m - n + 1;
					return Math.floor(Math.random() * c + n);
				}
			}
			new FwhfYZH();
		}
	</script>
</body>
</html>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
要使用Three.js实现存样柜,需要以下步骤: 1. 引入Three.js库:下载并引入最新的Three.js库文件到你的项目中。 2. 创建场景(Scene)和相机(Camera):使用Three.js创建一个场景,并添加一个透视相机。 ```javascript var scene = new THREE.Scene(); var camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 ); ``` 3. 创建渲染器(Renderer):创建一个渲染器,并将其附加到HTML文档中的一个容器元素上。 ```javascript var renderer = new THREE.WebGLRenderer(); renderer.setSize( window.innerWidth, window.innerHeight ); document.body.appendChild( renderer.domElement ); ``` 4. 添加灯光(Lighting):在场景中添加光源,以便能够看到物体。 ```javascript var light = new THREE.PointLight( 0xffffff, 1, 100 ); light.position.set( 0, 0, 10 ); scene.add( light ); ``` 5. 创建存样柜模型(Model):使用Three.js加载存样柜模型,并将其添加到场景中。 ```javascript var loader = new THREE.GLTFLoader(); loader.load( 'model.gltf', function ( gltf ) { var model = gltf.scene; scene.add( model ); }, undefined, function ( error ) { console.error( error ); } ); ``` 在上面的代码中,'model.gltf'是存样柜模型的文件路径,你需要将其替换为实际的模型文件路径。 6. 渲染场景(Scene):使用渲染器将场景和相机渲染到屏幕上。 ```javascript function animate() { requestAnimationFrame( animate ); // 在这里可以添加存样柜的动画或交互逻辑 renderer.render( scene, camera ); } animate(); ``` 在上面的代码中,你可以在animate函数中添加存样柜的动画或交互逻辑。 这是一个基本的使用Three.js实现存样柜的示例代码,你可以根据实际需求进行扩展和修改。请确保在使用Three.js之前对其进行适当的学习和了解。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风舞红枫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值