|
| 1 | +window.angular |
| 2 | + .module('babylonSharkConcept', []) |
| 3 | + .model('sharkModel', () => ({ |
| 4 | + swimming: true, |
| 5 | + speed: 0.03, |
| 6 | + heading: 0, |
| 7 | + depth: 0, |
| 8 | + })) |
| 9 | + .controller( |
| 10 | + 'DemoCtrl', |
| 11 | + class { |
| 12 | + static $inject = ['sharkModel']; |
| 13 | + |
| 14 | + constructor(sharkModel) { |
| 15 | + this.shark = sharkModel; |
| 16 | + requestAnimationFrame(() => this.mount()); |
| 17 | + } |
| 18 | + |
| 19 | + toggleSwim() { |
| 20 | + this.shark.swimming = !this.shark.swimming; |
| 21 | + } |
| 22 | + |
| 23 | + turn(amount) { |
| 24 | + this.shark.heading += amount; |
| 25 | + } |
| 26 | + |
| 27 | + dive(amount) { |
| 28 | + this.shark.depth = Math.max(-1.4, Math.min(1.2, this.shark.depth + amount)); |
| 29 | + } |
| 30 | + |
| 31 | + faster() { |
| 32 | + this.shark.speed = Math.min(0.12, this.shark.speed + 0.01); |
| 33 | + } |
| 34 | + |
| 35 | + slower() { |
| 36 | + this.shark.speed = Math.max(0.0, this.shark.speed - 0.01); |
| 37 | + } |
| 38 | + |
| 39 | + reset() { |
| 40 | + this.shark.heading = 0; |
| 41 | + this.shark.depth = 0; |
| 42 | + } |
| 43 | + |
| 44 | + async mount() { |
| 45 | + const canvas = document.getElementById('babylon-view'); |
| 46 | + const engine = new BABYLON.Engine(canvas, true); |
| 47 | + |
| 48 | + engine.setHardwareScalingLevel(1.0 / window.devicePixelRatio); |
| 49 | + engine.displayLoadingUI(); |
| 50 | + |
| 51 | + const scene = new BABYLON.Scene(engine); |
| 52 | + const camera = new BABYLON.ArcRotateCamera( |
| 53 | + 'camera1', |
| 54 | + 1.0, |
| 55 | + 1.3, |
| 56 | + 2.0, |
| 57 | + new BABYLON.Vector3(0, 0.5, 0), |
| 58 | + scene, |
| 59 | + ); |
| 60 | + const light = new BABYLON.HemisphericLight( |
| 61 | + 'light', |
| 62 | + new BABYLON.Vector3(0, 1, 0), |
| 63 | + scene, |
| 64 | + ); |
| 65 | + |
| 66 | + camera.attachControl(canvas, true); |
| 67 | + camera.fov = (27 / 180) * Math.PI; |
| 68 | + camera.minZ = 1; |
| 69 | + camera.maxZ = 120; |
| 70 | + camera.lowerBetaLimit = 0.1; |
| 71 | + camera.upperBetaLimit = 1.5; |
| 72 | + light.intensity = 0.9; |
| 73 | + |
| 74 | + scene.clearColor.set(0.149, 0.149, 0.149, 1.0); |
| 75 | + scene.environmentTexture = new BABYLON.HDRCubeTexture( |
| 76 | + 'https://assets.babylonjs.com/ibl/parking.hdr', |
| 77 | + scene, |
| 78 | + 128, |
| 79 | + ); |
| 80 | + |
| 81 | + const sky = createUnderwaterSky(scene); |
| 82 | + const shark = await loadShark(scene); |
| 83 | + |
| 84 | + frameCamera(camera, shark.meshes); |
| 85 | + sky.scaling.setAll(shark.sceneSize * 4); |
| 86 | + sky.position.y = shark.sceneSize * 2 - 1; |
| 87 | + engine.hideLoadingUI(); |
| 88 | + |
| 89 | + engine.runRenderLoop(() => { |
| 90 | + if (this.shark.swimming) { |
| 91 | + shark.root.position.x += Math.sin(this.shark.heading) * this.shark.speed; |
| 92 | + shark.root.position.z += Math.cos(this.shark.heading) * this.shark.speed; |
| 93 | + shark.animationGroups.forEach((group) => { |
| 94 | + if (!group.isPlaying) group.play(true); |
| 95 | + group.speedRatio = Math.max(0.2, this.shark.speed * 30); |
| 96 | + }); |
| 97 | + } else { |
| 98 | + shark.animationGroups.forEach((group) => group.pause()); |
| 99 | + } |
| 100 | + |
| 101 | + shark.root.rotation.y = this.shark.heading; |
| 102 | + shark.root.position.y = this.shark.depth; |
| 103 | + scene.render(); |
| 104 | + }); |
| 105 | + |
| 106 | + window.addEventListener('resize', () => engine.resize()); |
| 107 | + } |
| 108 | + }, |
| 109 | + ); |
| 110 | + |
| 111 | +function createUnderwaterSky(scene) { |
| 112 | + const sky = BABYLON.MeshBuilder.CreateBox( |
| 113 | + 'sky', |
| 114 | + { size: 1, sideOrientation: BABYLON.Mesh.BACKSIDE }, |
| 115 | + scene, |
| 116 | + ); |
| 117 | + const material = new BABYLON.BackgroundMaterial('skyMaterial', scene); |
| 118 | + const source = 'https://raw.githubusercontent.com/CedricGuillemet/dump/master/underwater_env3/'; |
| 119 | + const files = ['px.png', 'py.png', 'pz.png', 'nx.png', 'ny.png', 'nz.png'].map( |
| 120 | + (file) => `${source}${file}`, |
| 121 | + ); |
| 122 | + |
| 123 | + material.reflectionTexture = BABYLON.CubeTexture.CreateFromImages(files, scene); |
| 124 | + material.reflectionTexture.coordinatesMode = BABYLON.Constants.TEXTURE_SKYBOX_MODE; |
| 125 | + material.enableGroundProjection = true; |
| 126 | + sky.material = material; |
| 127 | + |
| 128 | + return sky; |
| 129 | +} |
| 130 | + |
| 131 | +async function loadShark(scene) { |
| 132 | + const root = new BABYLON.TransformNode('sharkRoot', scene); |
| 133 | + const result = await BABYLON.SceneLoader.ImportMeshAsync( |
| 134 | + '', |
| 135 | + 'https://assets.babylonjs.com/meshes/', |
| 136 | + 'shark.glb', |
| 137 | + scene, |
| 138 | + ); |
| 139 | + const meshes = result.meshes.filter((mesh) => mesh instanceof BABYLON.Mesh); |
| 140 | + |
| 141 | + meshes.forEach((mesh) => { |
| 142 | + mesh.parent = root; |
| 143 | + }); |
| 144 | + |
| 145 | + const bounds = calculateBounds(meshes); |
| 146 | + const sceneSize = bounds.max.subtract(bounds.min).length(); |
| 147 | + const center = bounds.max.subtract(bounds.min).scale(0.5).add(bounds.min); |
| 148 | + |
| 149 | + root.position.subtractInPlace(center); |
| 150 | + |
| 151 | + return { |
| 152 | + animationGroups: result.animationGroups, |
| 153 | + meshes, |
| 154 | + root, |
| 155 | + sceneSize, |
| 156 | + }; |
| 157 | +} |
| 158 | + |
| 159 | +function frameCamera(camera, meshes) { |
| 160 | + const bounds = calculateBounds(meshes); |
| 161 | + const sceneSize = bounds.max.subtract(bounds.min).length(); |
| 162 | + const center = bounds.max.subtract(bounds.min).scale(0.5).add(bounds.min); |
| 163 | + |
| 164 | + if (!isFinite(center.x) || !isFinite(center.y) || !isFinite(center.z)) return; |
| 165 | + |
| 166 | + camera.minZ = 0.1 * sceneSize; |
| 167 | + camera.maxZ = 45 * sceneSize; |
| 168 | + camera.speed = sceneSize; |
| 169 | + camera.wheelPrecision = 100 / sceneSize; |
| 170 | + camera.panningSensibility = 5000 / sceneSize; |
| 171 | + camera.target = BABYLON.Vector3.Zero(); |
| 172 | + camera.radius = 2.0 * sceneSize; |
| 173 | + camera.beta = Math.PI * 0.4; |
| 174 | + camera.upperRadiusLimit = sceneSize * 2; |
| 175 | + camera.lowerRadiusLimit = sceneSize; |
| 176 | +} |
| 177 | + |
| 178 | +function calculateBounds(meshes) { |
| 179 | + const bounds = { |
| 180 | + min: new BABYLON.Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE), |
| 181 | + max: new BABYLON.Vector3(-Number.MAX_VALUE, -Number.MAX_VALUE, -Number.MAX_VALUE), |
| 182 | + }; |
| 183 | + |
| 184 | + meshes.forEach((mesh) => { |
| 185 | + const localBounds = mesh.getHierarchyBoundingVectors(true); |
| 186 | + |
| 187 | + bounds.min = BABYLON.Vector3.Minimize(bounds.min, localBounds.min); |
| 188 | + bounds.max = BABYLON.Vector3.Maximize(bounds.max, localBounds.max); |
| 189 | + }); |
| 190 | + |
| 191 | + return bounds; |
| 192 | +} |
0 commit comments