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
| import { Component, Material, Vec3, _decorator } from 'cc';
const { ccclass, property, menu } = _decorator;
@ccclass('VertexAnimationController') @menu('Custom/VertexAnimationController') export class VertexAnimationController extends Component { @property({ range: [0.0, 3.0], displayName: '动画强度' }) public animationIntensity: number = 1.0; @property({ range: [0.0, 10.0], displayName: '动画速度' }) public animationSpeed: number = 1.0; @property({ type: Vec3, displayName: '动画方向' }) public animationDirection: Vec3 = new Vec3(1, 0, 0); @property({ displayName: '动画类型' }) public animationType: AnimationType = AnimationType.Wave; @property({ range: [0.0, 3.0], displayName: '波浪幅度' }) public waveAmplitude: number = 0.5; @property({ range: [0.1, 10.0], displayName: '波浪频率' }) public waveFrequency: number = 2.0; @property({ type: Vec3, displayName: '爆炸中心' }) public explosionCenter: Vec3 = new Vec3(0, 0, 0); @property({ range: [0.1, 10.0], displayName: '爆炸半径' }) public explosionRadius: number = 2.0; @property({ range: [0.0, 10.0], displayName: '爆炸力度' }) public explosionForce: number = 3.0; @property({ range: [0.0, 5.0], displayName: '爆炸时间' }) public explosionTime: number = 0.0; @property({ range: [0.0, 1.0], displayName: '生长进度' }) public growthProgress: number = 0.5; @property({ type: Vec3, displayName: '生长原点' }) public growthOrigin: Vec3 = new Vec3(0, -1, 0); @property({ range: [0.1, 5.0], displayName: '生长高度' }) public growthHeight: number = 2.0; @property({ range: [0.1, 5.0], displayName: '液体密度' }) public liquidDensity: number = 1.0; @property({ range: [0.0, 2.0], displayName: '粘度' }) public viscosity: number = 0.5; @property({ range: [0.0, 1.0], displayName: '湍流强度' }) public turbulence: number = 0.3; private _material: Material | null = null; private _isAnimating: boolean = false; private _animationTimer: number = 0; public enum AnimationType { Wave = 'wave-displacement', Explosion = 'explosion-displacement', Growth = 'growth-animation', Liquid = 'liquid-simulation' } onLoad() { const renderer = this.getComponent('cc.MeshRenderer'); if (renderer && renderer.material) { this._material = renderer.material; this.updateMaterial(); } } update(deltaTime: number) { if (this._isAnimating) { this._animationTimer += deltaTime; this.updateMaterial(); } } private updateMaterial() { if (!this._material) return; this._material.setProperty('animationIntensity', this.animationIntensity); this._material.setProperty('animationSpeed', this.animationSpeed); this._material.setProperty('animationDirection', this.animationDirection); this._material.setProperty('waveAmplitude', this.waveAmplitude); this._material.setProperty('waveFrequency', this.waveFrequency); this._material.setProperty('waveSpeed', this.animationSpeed); this._material.setProperty('waveDirection', this.animationDirection); this._material.setProperty('explosionCenter', this.explosionCenter); this._material.setProperty('explosionRadius', this.explosionRadius); this._material.setProperty('explosionForce', this.explosionForce); this._material.setProperty('explosionTime', this.explosionTime); this._material.setProperty('growthProgress', this.growthProgress); this._material.setProperty('growthOrigin', this.growthOrigin); this._material.setProperty('growthHeight', this.growthHeight); this._material.setProperty('liquidDensity', this.liquidDensity); this._material.setProperty('viscosity', this.viscosity); this._material.setProperty('turbulence', this.turbulence); } public setAnimationType(type: AnimationType) { this.animationType = type; console.log(`切换到顶点动画类�? ${type}`); this.updateMaterial(); } public startAnimation() { this._isAnimating = true; this._animationTimer = 0; } public stopAnimation() { this._isAnimating = false; } public resetAnimation() { this._animationTimer = 0; this.explosionTime = 0; this.growthProgress = 0; this.updateMaterial(); } public startWaveAnimation() { this.setAnimationType(AnimationType.Wave); this.startAnimation(); } public setWaveProperties(amplitude: number, frequency: number, speed: number) { this.waveAmplitude = amplitude; this.waveFrequency = frequency; this.animationSpeed = speed; this.updateMaterial(); } public triggerExplosion(center: Vec3, force: number = 3.0, radius: number = 2.0) { this.explosionCenter.set(center); this.explosionForce = force; this.explosionRadius = radius; this.explosionTime = 0; this.setAnimationType(AnimationType.Explosion); let elapsed = 0; const explode = (dt: number) => { elapsed += dt; this.explosionTime = elapsed; this.updateMaterial(); if (elapsed < 3.0) { this.scheduleOnce(explode, 0); } else { this.explosionTime = 0; this.updateMaterial(); } }; this.scheduleOnce(explode, 0); } public startGrowthAnimation(duration: number = 3.0) { this.growthProgress = 0; this.setAnimationType(AnimationType.Growth); let elapsed = 0; const grow = (dt: number) => { elapsed += dt; this.growthProgress = Math.min(elapsed / duration, 1.0); this.updateMaterial(); if (this.growthProgress < 1.0) { this.scheduleOnce(grow, 0); } }; this.scheduleOnce(grow, 0); } public setGrowthProperties(origin: Vec3, height: number) { this.growthOrigin.set(origin); this.growthHeight = height; this.updateMaterial(); } public startLiquidSimulation() { this.setAnimationType(AnimationType.Liquid); this.startAnimation(); } public setLiquidProperties(density: number, viscosity: number, turbulence: number) { this.liquidDensity = density; this.viscosity = viscosity; this.turbulence = turbulence; this.updateMaterial(); } public applyOceanWaves() { this.setWaveProperties(0.8, 1.5, 1.2); this.animationDirection.set(1, 0, 0.3); this.startWaveAnimation(); } public applyWindEffect() { this.setWaveProperties(0.3, 3.0, 2.5); this.animationDirection.set(0.8, 0.2, 0); this.startWaveAnimation(); } public applyPlantGrowth() { this.setGrowthProperties(new Vec3(0, -1, 0), 2.5); this.startGrowthAnimation(4.0); } public applyBuildingCollapse() { this.triggerExplosion(new Vec3(0, 0, 0), 5.0, 3.0); } public applyWaterFlow() { this.setLiquidProperties(1.2, 0.8, 0.4); this.startLiquidSimulation(); } public applyMagicEffect() { this.setWaveProperties(0.4, 4.0, 3.0); this.animationDirection.set(0, 1, 0); this.startWaveAnimation(); } public playAnimationSequence(animations: AnimationType[], durations: number[]) { if (animations.length !== durations.length) { console.error('动画类型和持续时间数组长度不匹配'); return; } let currentIndex = 0; const playNext = () => { if (currentIndex >= animations.length) { return; } const currentType = animations[currentIndex]; const currentDuration = durations[currentIndex]; this.setAnimationType(currentType); if (currentType === AnimationType.Growth) { this.startGrowthAnimation(currentDuration); } else if (currentType === AnimationType.Explosion) { this.triggerExplosion(this.explosionCenter, this.explosionForce, this.explosionRadius); } else { this.startAnimation(); } currentIndex++; if (currentIndex < animations.length) { this.scheduleOnce(playNext, currentDuration); } }; playNext(); } public resetToDefault() { this.animationIntensity = 1.0; this.animationSpeed = 1.0; this.animationDirection.set(1, 0, 0); this.waveAmplitude = 0.5; this.waveFrequency = 2.0; this.explosionTime = 0; this.growthProgress = 0.5; this.liquidDensity = 1.0; this.viscosity = 0.5; this.turbulence = 0.3; this.stopAnimation(); this.updateMaterial(); } }
export { AnimationType } from './VertexAnimationController';
|