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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 |
<button id="button">全屏</button> <br/> <div style="width:800px; height:350px;"> <canvas id="canvas" style="position:absolute; border:1px solid #3399ff;"></canvas> </div> <script> let original_width = 800; let original_height = 350; let canvas = document.getElementById("canvas"); let button = document.getElementById("button"); button.onclick = function () { CanvasResizeManager.addFullScreenListener(); }; let head = document.getElementsByTagName("head")[0]; let cannon_script = document.createElement("script"); cannon_script.src = "https://github.com/schteppe/cannon.js/releases/download/v0.6.2/cannon.min.js"; head.appendChild(cannon_script); let three_script = document.createElement("script"); three_script.src = "https://github.com/mrdoob/three.js/blob/dev/build/three_r132_min.js"; head.appendChild(three_script); cannon_script.onload = function () { three_script.onload = function () { new Cinema().render(); }; }; class Cinema { constructor() { canvas.width = original_width; canvas.height = original_height; this.cannonWorld = new CANNON.World(); this.cannonWorld.gravity.set(0, -9, 0); this.cannonWorld.solver.iterations = 5; this.cannonWorld.broadphase = new CANNON.NaiveBroadphase(); this.pickPosition = {x: -100000, y: -100000}; // 鼠标的实时位置 let ambientLight = LightFactory.getAmbientLight("#90d7ec"); let directionalLight = LightFactory.getDirectionalLight("#ffffff"); let aspect = canvas.width / canvas.height; this.camera = new THREE.PerspectiveCamera(60, aspect, 0.5, 1000); this.camera.position.x = 50; this.camera.position.y = 60; this.camera.position.z = 70; this.camera.lookAt(0, 5, 0); let _controls = new THREE.OrbitControls(this.camera, canvas); this.scene = new THREE.Scene(); this.scene.background = new THREE.Color('#393939'); this.scene.add(this.camera); this.scene.add(ambientLight); this.scene.add(directionalLight); this.renderer = new THREE.WebGLRenderer({canvas: canvas, antialias: true}); this.renderer.shadowMap.enabled = true; this.renderer.shadowMap.type = THREE.PCFSoftShadowMap; this.renderer.setSize(canvas.width, canvas.height); this.pickHelper = new PickHelper(this.cannonWorld, this.scene, this.camera); canvas.addEventListener('click', this.clickCallback.bind(this)); // 鼠标点击 document.addEventListener('mousewheel', this.resetCallback.bind(this)); document.addEventListener('mousemove', this.resetCallback.bind(this)); window.addEventListener("resize", this.onCanvasResize.bind(this)); let bound = 500; let ground = new CannonGround(this.scene, this.cannonWorld); ground.intoSceneAndWorld(bound, bound); this.intervalTime = 1000; setInterval(() => { let cannonBox = new CannonBox(this.scene, this.cannonWorld); cannonBox.intoSceneAndWorld(bound); }, this.intervalTime); } resetCallback(event) { this.pickPosition = {x: -100000, y: -100000}; } clickCallback(event) { let rect = canvas.getBoundingClientRect(); let pos = { x: (event.clientX - rect.left) * canvas.width / rect.width, y: (event.clientY - rect.top) * canvas.height / rect.height, }; this.pickPosition.x = (pos.x / canvas.width) * 2 - 1; this.pickPosition.y = (pos.y / canvas.height) * -2 + 1; // note we flip Y this.pickHelper.pick(this.pickPosition); this.resetCallback(); } onCanvasResize() { // 浏览器全屏与退出响应 let fullscreenEnabled = CanvasResizeManager.isFullscreenEnabled(); if (fullscreenEnabled) { let fullscreen = CanvasResizeManager.isFullscreen(); if (fullscreen) { canvas.width = window.innerWidth; canvas.height = window.innerHeight; } else { canvas.width = original_width; canvas.height = original_height; } this.resizeRendererToDisplaySize(); } } resizeRendererToDisplaySize() { this.camera.aspect = canvas.width / canvas.height; this.camera.updateProjectionMatrix(); this.renderer.setSize(canvas.width, canvas.height, false); } updatePhysics() { this.cannonWorld.step(0.2); // 降落速度 this.scene.children.forEach(obj => { // 遍历场景中的对象 if (obj.isMesh == true) { obj.position.copy(obj.userData.position); obj.quaternion.copy(obj.userData.quaternion); } }); } render() { this.updatePhysics(); this.renderer.render(this.scene, this.camera); requestAnimationFrame(this.render.bind(this)); } } class CanvasResizeManager { static addFullScreenListener() { let fullscreenEnabled = CanvasResizeManager.isFullscreenEnabled(); if (fullscreenEnabled) { let fullscreen = CanvasResizeManager.isFullscreen(); if (!fullscreen) { if (canvas.requestFullscreen) { canvas.requestFullscreen(); } else if (canvas.msRequestFullscreen) { canvas.msRequestFullscreen(); } else if (canvas.mozRequestFullScreen) { canvas.mozRequestFullScreen(); } else if (canvas.webkitRequestFullscreen) { canvas.webkitRequestFullscreen(); } } } } static exitScreen() { // 手动退出全屏 if (document.exitFullscreen) { document.exitFullscreen(); } else if (document.mozCancelFullScreen) { document.mozCancelFullScreen(); } else if (document.webkitCancelFullScreen) { document.webkitCancelFullScreen(); } else if (document.msExitFullscreen) { document.msExitFullscreen(); } } static isFullscreenEnabled() { // 浏览器是否支持全屏 return document.fullscreenEnabled || document.mozFullScreenEnabled || document.webkitFullscreenEnabled || document.msFullscreenEnabled || false; } static isFullscreen() { // 判断是否全屏状态 return Boolean(document.fullscreenElement || document.msFullscreenElement || document.mozFullScreenElement || document.webkitFullscreenElement || false); } } class PickHelper { constructor(cannonWorld, scene, camera) { this.raycaster = new THREE.Raycaster(); this.cannonWorld = cannonWorld; this.scene = scene; this.camera = camera; this.count = 0; this.textSprite = new TextLegendSprite(0); this.camera.add(this.textSprite); } pick(pickedPosition) { this.raycaster.setFromCamera(pickedPosition, this.camera); let objs = this.raycaster.intersectObjects(this.scene.children); if (objs.length > 0) { let obj = objs[0].object; if (null !== obj) { if (typeof(obj) !== "undefined") { if ("ground" !== obj.name) { this.cannonWorld.removeBody(obj.userData); this.scene.remove(obj); // 删除物体 this.count++; this.camera.remove(this.textSprite); this.textSprite = new TextLegendSprite(this.count); this.camera.add(this.textSprite); } } } } } } class LightFactory { static getDirectionalLight(color, x = 80, y = 200, z = -150) { let light = new THREE.DirectionalLight(color, 1); light.position.set(x, y, z); light.target.position.set(300, 5, 150); light.castShadow = true; light.shadow.camera.visible = true; let d = 250; light.shadow.camera.left = -d; light.shadow.camera.right = d; light.shadow.camera.top = d; light.shadow.camera.bottom = -d; light.shadow.camera.far = 500; light.shadow.bias = -0.01; light.shadow.mapSize.height = 2048; light.shadow.mapSize.width = 2048; return light; } static getAmbientLight(color) { let light = new THREE.AmbientLight(); light.color.set(color); return light; } } class CannonGround { constructor(scene, cannonWorld) { this.scene = scene; this.cannonWorld = cannonWorld; } intoSceneAndWorld(width, depth) { let height = 0.05; let x = 0; let y = 0.05; let z = 0; let shape = new CANNON.Box(new CANNON.Vec3(width / 2, height / 2, depth / 2)); // let cannonBody = new CANNON.Body({ mass: 0, position: new CANNON.Vec3(x, y, z), shape: shape, material: new CANNON.Material({ friction: 0.5, restitution: 0.5, }) }); this.cannonWorld.addBody(cannonBody); let color = "#cececf"; let geometry = new THREE.BoxGeometry(width, height, depth); let material = new THREE.MeshPhongMaterial({ color: color, shininess: 5, }); let mesh = new THREE.Mesh(geometry, material); mesh.name = "ground"; mesh.position.x = x; mesh.position.y = y; mesh.position.z = z; mesh.castShdow = true; mesh.receiveShadow = true; mesh.userData = cannonBody; this.scene.add(mesh); let grid = new THREE.GridHelper(width, depth / 10, "#993333", "#993333"); grid.name = "ground"; grid.material.opacity = 0.2; grid.material.transparent = true; grid.position.x = x; grid.position.y = y * 2; // y是负数就除以,是正数就乘以 grid.position.z = z; this.scene.add(grid); } } class CannonBox { constructor(scene, cannonWorld) { this.scene = scene; this.cannonWorld = cannonWorld; } intoSceneAndWorld(bound) { let width = 10; let depth = 10; let height = 10; let x = (0.5 - Math.random()) * bound / 5; let y = 100; let z = (0.5 - Math.random()) * bound / 5; let cannonBody = new CANNON.Body({ mass: 1, position: new CANNON.Vec3(x, y, z), shape: new CANNON.Box(new CANNON.Vec3(width / 2, height / 2, depth / 2)), // material: new CANNON.Material({ friction: 0.4, restitution: 1.2 }) }); this.cannonWorld.addBody(cannonBody); var color = "#"; for (let k = 0; k < 6; k++) color += parseInt(Math.random() * 16).toString(16); let material = new THREE.MeshPhongMaterial({ color: color, side: THREE.DoubleSide, }); let geometry = new THREE.BoxGeometry(width, height, depth); let mesh = new THREE.Mesh(geometry, material); mesh.name = new Date().getTime() + ""; mesh.position.set(x, y, z); mesh.castShadow = true; mesh.receiveShadow = true; mesh.userData = cannonBody; this.scene.add(mesh); setTimeout(() => { this.cannonWorld.removeBody(cannonBody); this.scene.remove(mesh); mesh.material.dispose(); mesh.geometry.dispose(); }, 8000); } } class TextLegendSprite { constructor(text) { let canvas = document.createElement("canvas"); canvas.width = 64; canvas.height = 64; let paint = canvas.getContext("2d"); paint.fillStyle = "#f03B30"; paint.font = 28 + "px bold"; paint.fillText(text + "", 10, 35); let spriteMaterial = new THREE.SpriteMaterial({ map: new THREE.CanvasTexture(canvas), transparent: true, }); let _sprite = new THREE.Sprite(spriteMaterial); _sprite.position.set(-1.2, 0.6, -1.4); _sprite.scale.set(0.2, 0.2, 1); return _sprite; } } </script> |
- end
声明
本文由崔维友 威格灵 cuiweiyou vigiles cuiweiyou 原创,转载请注明出处:http://www.gaohaiyan.com/3140.html
承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设