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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
| import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
import gsap from "gsap";
import * as dat from "dat.gui";
import * as CANNON from "cannon-es";
console.log(CANNON);
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 300 );
camera.position.set(0, 0, 18); scene.add(camera);
const cubeArr = [];
const cubeWorldMaterial = new CANNON.Material("cube");
function createCube() { const cubeGeometry = new THREE.BoxBufferGeometry(1, 1, 1); const cubeMaterial = new THREE.MeshStandardMaterial(); const cube = new THREE.Mesh(cubeGeometry, cubeMaterial); cube.castShadow = true; scene.add(cube); const cubeShape = new CANNON.Box(new CANNON.Vec3(0.5, 0.5, 0.5));
const cubeBody = new CANNON.Body({ shape: cubeShape, position: new CANNON.Vec3(0, 0, 0), mass: 1, material: cubeWorldMaterial, }); cubeBody.applyLocalForce( new CANNON.Vec3(300, 0, 0), new CANNON.Vec3(0, 0, 0) );
world.addBody(cubeBody); function HitEvent(e) { const impactStrength = e.contact.getImpactVelocityAlongNormal(); console.log(impactStrength); if (impactStrength > 2) { hitSound.currentTime = 0; hitSound.volume = impactStrength / 12; hitSound.play(); } } cubeBody.addEventListener("collide", HitEvent); cubeArr.push({ mesh: cube, body: cubeBody, }); }
window.addEventListener("click", createCube);
const floor = new THREE.Mesh( new THREE.PlaneBufferGeometry(20, 20), new THREE.MeshStandardMaterial() );
floor.position.set(0, -5, 0); floor.rotation.x = -Math.PI / 2; floor.receiveShadow = true; scene.add(floor);
const world = new CANNON.World(); world.gravity.set(0, -9.8, 0);
const hitSound = new Audio("assets/metalHit.mp3");
const floorShape = new CANNON.Plane(); const floorBody = new CANNON.Body(); const floorMaterial = new CANNON.Material("floor"); floorBody.material = floorMaterial;
floorBody.mass = 0; floorBody.addShape(floorShape);
floorBody.position.set(0, -5, 0);
floorBody.quaternion.setFromAxisAngle(new CANNON.Vec3(1, 0, 0), -Math.PI / 2); world.addBody(floorBody);
const defaultContactMaterial = new CANNON.ContactMaterial( cubeWorldMaterial, floorMaterial, { friction: 0.1, restitution: 0.7, } );
world.addContactMaterial(defaultContactMaterial);
world.defaultContactMaterial = defaultContactMaterial;
const ambientLight = new THREE.AmbientLight(0xffffff, 0.5); scene.add(ambientLight); const dirLight = new THREE.DirectionalLight(0xffffff, 0.5); dirLight.castShadow = true; scene.add(dirLight);
const renderer = new THREE.WebGLRenderer({ alpha: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
document.body.appendChild(renderer.domElement);
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
const axesHelper = new THREE.AxesHelper(5); scene.add(axesHelper);
const clock = new THREE.Clock();
function render() { let deltaTime = clock.getDelta(); world.step(1 / 120, deltaTime);
cubeArr.forEach((item) => { item.mesh.position.copy(item.body.position); item.mesh.quaternion.copy(item.body.quaternion); });
renderer.render(scene, camera); requestAnimationFrame(render); }
render();
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight; camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight); renderer.setPixelRatio(window.devicePixelRatio); });
|