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
| import { Component, Material, Color, Vec3, _decorator } from 'cc';
const { ccclass, property, menu } = _decorator;
@ccclass('AdvancedRimLightController') @menu('Custom/AdvancedRimLightController') export class AdvancedRimLightController extends Component { @property({ type: Color, displayName: '边缘光颜�? }) public rimColor: Color = new Color(128, 200, 255, 255); @property({ range: [0.1, 10.0], displayName: '边缘光强�? }) public rimPower: number = 2.0; @property({ range: [0.0, 5.0], displayName: '边缘光亮�? }) public rimIntensity: number = 1.0; @property({ range: [-1.0, 1.0], displayName: '边缘光偏�? }) public rimOffset: number = 0.0; @property({ displayName: '启用动态效�? }) public enableDynamicEffect: boolean = false; @property({ range: [0.0, 10.0], displayName: '脉冲速度' }) public pulseSpeed: number = 2.0; @property({ range: [0.0, 1.0], displayName: '脉冲幅度' }) public pulseAmplitude: number = 0.7; @property({ displayName: '启用多层效果' }) public enableMultiLayer: boolean = false; @property({ type: Color, displayName: '第二层颜�? }) public rimColor2: Color = new Color(255, 128, 128, 255); @property({ type: Color, displayName: '第三层颜�? }) public rimColor3: Color = new Color(128, 255, 128, 255); @property({ displayName: '特效类型' }) public effectType: EffectType = EffectType.Basic; private _material: Material | null = null; private _isAnimating: boolean = false; private _originalRimColor: Color = new Color(); // 特效类型枚举 public enum EffectType { Basic = 'basic-rim-light', MultiLayer = 'multi-rim-light', Pulse = 'pulse-rim-light', EnergyShield = 'energy-shield-rim', Rainbow = 'rainbow-rim-light', Electric = 'electric-rim-light' } onLoad() { const renderer = this.getComponent('cc.MeshRenderer'); if (renderer && renderer.material) { this._material = renderer.material; this._originalRimColor.set(this.rimColor); this.updateMaterial(); } } update(deltaTime: number) { if (this._isAnimating || this.enableDynamicEffect) { this.updateMaterial(); } } private updateMaterial() { if (!this._material) return; // 基础边缘光参�? this._material.setProperty('rimColor', this.rimColor); this._material.setProperty('rimPower', this.rimPower); this._material.setProperty('rimIntensity', this.rimIntensity); this._material.setProperty('rimOffset', this.rimOffset); // 动态效果参�? if (this.enableDynamicEffect) { this._material.setProperty('pulseSpeed', this.pulseSpeed); this._material.setProperty('pulseAmplitude', this.pulseAmplitude); } // 多层效果参数 if (this.enableMultiLayer) { this._material.setProperty('rimColor2', this.rimColor2); this._material.setProperty('rimColor3', this.rimColor3); } } // 公共接口 public setEffectType(type: EffectType) { this.effectType = type; // 这里可以切换不同的材质或technique console.log(`切换到边缘光类型: ${type}`); this.updateMaterial(); } public startAnimation() { this._isAnimating = true; this.enableDynamicEffect = true; } public stopAnimation() { this._isAnimating = false; this.enableDynamicEffect = false; } public setRimColor(color: Color) { this.rimColor = color; this.updateMaterial(); } public setRimIntensity(intensity: number) { this.rimIntensity = intensity; this.updateMaterial(); } // 预设效果 public applyMagicItemEffect() { this.rimColor = new Color(100, 150, 255, 255); this.rimPower = 1.5; this.rimIntensity = 2.0; this.enableDynamicEffect = true; this.pulseSpeed = 1.5; this.pulseAmplitude = 0.5; this.updateMaterial(); } public applyLegendaryItemEffect() { this.rimColor = new Color(255, 215, 0, 255); // 金色 this.rimPower = 2.5; this.rimIntensity = 2.5; this.enableMultiLayer = true; this.rimColor2 = new Color(255, 165, 0, 255); // 橙色 this.rimColor3 = new Color(255, 69, 0, 255); // 红橙�? this.enableDynamicEffect = true; this.updateMaterial(); } public applyEnemyEffect() { this.rimColor = new Color(255, 50, 50, 255); // 红色 this.rimPower = 3.0; this.rimIntensity = 1.8; this.enableDynamicEffect = true; this.pulseSpeed = 3.0; this.pulseAmplitude = 0.8; this.updateMaterial(); } public applyFriendlyEffect() { this.rimColor = new Color(50, 255, 50, 255); // 绿色 this.rimPower = 2.0; this.rimIntensity = 1.2; this.enableDynamicEffect = false; this.updateMaterial(); } public applyShieldEffect() { this.setEffectType(EffectType.EnergyShield); this.rimColor = new Color(50, 200, 255, 255); this.rimPower = 1.5; this.rimIntensity = 2.0; this.updateMaterial(); } public applyElectricEffect() { this.setEffectType(EffectType.Electric); this.rimColor = new Color(100, 200, 255, 255); this.rimPower = 1.8; this.rimIntensity = 2.5; this.updateMaterial(); } // 动画效果 public flashRim(duration: number = 1.0) { const originalIntensity = this.rimIntensity; const flashIntensity = originalIntensity * 3.0; let elapsed = 0; const flash = (dt: number) => { elapsed += dt; const t = elapsed / duration; if (t < 0.3) { // 快速增强阶�? this.rimIntensity = originalIntensity + (flashIntensity - originalIntensity) * (t / 0.3); } else { // 缓慢衰减阶段 this.rimIntensity = flashIntensity - (flashIntensity - originalIntensity) * ((t - 0.3) / 0.7); } this.updateMaterial(); if (t < 1.0) { this.scheduleOnce(flash, 0); } else { this.rimIntensity = originalIntensity; this.updateMaterial(); } }; this.scheduleOnce(flash, 0); } public colorTransition(targetColor: Color, duration: number = 1.0) { const startColor = this.rimColor.clone(); let elapsed = 0; const transition = (dt: number) => { elapsed += dt; const t = Math.min(elapsed / duration, 1.0); // 颜色插�? this.rimColor.r = Math.lerp(startColor.r, targetColor.r, t); this.rimColor.g = Math.lerp(startColor.g, targetColor.g, t); this.rimColor.b = Math.lerp(startColor.b, targetColor.b, t); this.rimColor.a = Math.lerp(startColor.a, targetColor.a, t); this.updateMaterial(); if (t < 1.0) { this.scheduleOnce(transition, 0); } }; this.scheduleOnce(transition, 0); } public resetToDefault() { this.rimColor.set(this._originalRimColor); this.rimPower = 2.0; this.rimIntensity = 1.0; this.rimOffset = 0.0; this.enableDynamicEffect = false; this.enableMultiLayer = false; this.updateMaterial(); } }
// 导出枚举以便其他脚本使用 export { EffectType } from './AdvancedRimLightController';
|