three模拟三维场景甩动钢球撞击墙面物理效果代码

代码语言:html

所属分类:三维

代码描述:three模拟三维场景甩动钢球撞击墙面物理效果代码

代码标签: three 模拟 三维 场景 甩动 钢球 撞击 墙面 物理

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<html lang="en">
	<head>

		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		
		<style>
			body {
				color: #333;
			}
		</style>
	</head>
	<body>
	<div id="info">
	按住键盘q与a键进行旋转杆子
		<div id="container"></div>

		<script src="//repo.bfw.wiki/bfwtool/examples/js/libs/ammo.wasm.js"></script>

		<!-- Import maps polyfill -->
		<!-- Remove this when import maps will be widely supported -->
		<script async type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/es-module-shims.1.3.6.js"></script>


		<script type="importmap">
			{
				"imports": {
					"three": "//repo.bfw.wiki/bfwtool/build/three.module.js"
				}
			}
		</script>

		<script type="module">

			import * as THREE from 'three';

			import Stats from '//repo.bfw.wiki/bfwtool/examples/jsm/libs/stats.module.js';

			import { OrbitControls } from '//repo.bfw.wiki/bfwtool/examples/jsm/controls/OrbitControls.js';

		// Graphics variables
		let container, stats;
		let camera, controls, scene, renderer;
		let textureLoader;
		const clock = new THREE.Clock();

		// Physics variables
		const gravityConstant = - 9.8;
		let collisionConfiguration;
		let dispatcher;
		let broadphase;
		let solver;
		let softBodySolver;
		let physicsWorld;
		const rigidBodies = [];
		const margin = 0.05;
		let hinge;
		let rope;
		let transformAux1;

		let armMovement = 0;

		Ammo().then( function ( AmmoLib ) {

			Ammo = AmmoLib;

			init();
			animate();

		} );

		function init() {

			initGraphics();

			initPhysics();

			createObjects();

			initInput();

		}

		function initGraphics() {

			container = document.getElementById( 'container' );

			camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.2, 2000 );

			scene = new THREE.Scene();
			scene.background = new THREE.Color( 0xbfd1e5 );

			camera.position.set( - 7, 5, 8 );

			renderer = new THREE.WebGLRenderer();
			renderer.setPixelRatio( window.devicePixelRatio );
			renderer.setSize( window.innerWidth, window.innerHeight );
			renderer.shadowMap.enabled = true;
			container.appendChild( renderer.domElement );

			controls = new OrbitControls( camera, renderer.domElement );
			controls.target.set( 0, 2, 0 );
			controls.update();

			textureLoader = new THREE.TextureLoader();

			const ambientLight = new THREE.AmbientLight( 0x404040 );
			scene.add( ambientLight );

			const light = new THREE.DirectionalLight( 0xffffff, 1 );
			light.position.set( - 10, 10, 5 );
			light.castShadow = true;
			const d = 10;
			light.shadow.camera.left = - d;
			light.shadow.camera.right = d;
			light.shadow.camera.top = d;
			light.shadow.camera.bottom = - d;

			light.shadow.camera.near = 2;
			light.shadow.camera.far = 50;

			light.shadow.mapSize.x = 1024;
			light.shadow.mapSize.y = 1024;

			scene.add( light );

			stats = new Stats();
			stats.domElement.style.position = 'absolute';
			stats.domElement.style.top = '0px';
			container.appendChild( stats.domElement );

			//

			window.addEventListener( 'resize', onWindowResize );

		}

		function initPhysics() {

			// Physics configuration

			collisionConfiguration = new Ammo.btSoftBodyRigidBodyCollisionConfiguration();
			dispatcher = new Ammo.btCollisionDispatcher( collisionConfiguration );
			broadphase = new Ammo.btDbvtBroadphase();
			solver = new Ammo.btSequentialImpulseConstraintSolver();
			softBodySolver = new Ammo.btDefaultSoftBodySolver();
			physicsWorld = new Ammo.btSoftRigidDynamicsWorld( dispatcher, broadphase, solver, collisionConfiguration, softBodySolver );
			physicsWorld.setGravity( new Ammo.btVector3( 0, gravityConstant, 0 ) );
			physicsWorld.getWorldInfo().set_m_gravity( new Ammo.btVector3( 0, gravityConstant, 0 ) );

			transformAux1 = new Ammo.btTransform();

		}

		function createObjects() {

			const pos = new THREE.Vector3();
			const quat = new THREE.Quaternion();

			// Ground
			pos.set( 0, - 0.5, 0 );
			quat.set( 0, 0, 0, 1 );
			const ground = createParalellepiped( 40, 1, 40, 0, pos, quat, new THREE.MeshPhongMaterial( { color: 0xFFFFFF } ) );
			ground.castShadow = true;
			ground.receiveShadow = true;
			textureLoader.load( 'textures/grid.png', function ( texture ) {

				texture.wrapS = THREE.RepeatWrapping;
				texture.wrapT = THREE.RepeatWrapping;
				texture.repeat.set( 40, 40 );
				ground.material.map = texture;
				ground.material.needsUpdate = true;

			} );


			// Ball
			const ballMass = 1.2;
			const ballRadius = 0.6;

			const ball = new THREE.Mesh( new THREE.SphereGeometry( ballRadius, 20, 20 ), new THREE.MeshPhongMaterial( { color: 0x202020 } ) );
			ball.castShadow = true;
			ball.receiveShadow = true;
			const ballShape = new Ammo.btSphereShape( ballRadius );
			ballShape.setMargin( margin );
			pos.set( - 3, 2, 0 );
			quat.set( 0, 0, 0, 1 );
			createRigidBody( ball, ballShape, ballMass, pos, quat );
			ball.userData.physicsBody.setFriction( 0.5 );

			// Wall
			const brickMass = 0.5;
			const brickLength = 1.2;
			const brickDepth = 0.6;
			const brickHeight = brickLength * 0.5;
			const numBricksLength = 6;
			const numBricksHeight = 8;
			const z0 = - numBricksLength * brickLength * 0.5;
			pos.set( 0, brickHeight * 0.5, z0 );
			quat.set( 0, 0, 0, 1 );

			for ( let j = 0; j < numBricksHeight; j ++ ) {

				const oddRow = ( j % 2 ) == 1;

				pos.z = z0;

				if ( oddRow ) {

					pos.z -= 0.25 * brickLength;

				}

				const nRow = oddRow ? numBricksLength + 1 : numBricksLength;

				for ( let i = 0; i < nRow; i ++ ) {

					let brickLengthCurrent = brickLength;
					let brickMassCurrent = brickMass;
					if ( oddRow && ( i == 0 || i == nRow - 1 ) ) {

						brickLengthCurrent *= 0.5;
						brickMassCurrent *= 0.5;

					}

					const brick = createParalellepiped( brickDepth, brickHeight, brickLengthCurrent, brickMassCurrent, pos, quat, createMaterial() );
					brick.castShadow = true;
					brick.receiveShadow = true;

					if ( oddRow && ( i == 0 || i == nRow - 2 ) ) {

						pos.z += 0.75 * brickLength;

					} else {

						pos.z += brickLength;

					}

				}

				pos.y += brickHeight;

			}

			// The rope
			// Rope graphic object
			const ropeNumSegments = 10;
			const ropeLength = 4;
			const ropeMass = 3;
			const ropePos = ball.position.clone();
			ropePos.y += ballRadius;

			const segmentLength = ropeLength / ropeNumSegments;
			const ropeGeometry = new THREE.BufferGeometry();.........完整代码请登录后点击上方下载按钮下载查看

网友评论0