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
| import { Component, Material, Color, Vec3, _decorator } from 'cc';
const { ccclass, property, menu } = _decorator;
@ccclass('HologramController') @menu('Custom/HologramController') export class HologramController extends Component { @property({ type: Color, displayName: '全息颜色' }) public hologramColor: Color = new Color(50, 200, 255, 255); @property({ range: [0.0, 1.0], displayName: '透明�? }) public transparency: number = 0.7; @property({ range: [10.0, 200.0], displayName: '扫描线数�? }) public scanlineCount: number = 50.0; @property({ range: [0.0, 10.0], displayName: '扫描线速度' }) public scanlineSpeed: number = 2.0; @property({ range: [0.0, 1.0], displayName: '扫描线强�? }) public scanlineIntensity: number = 0.3; @property({ range: [0.0, 20.0], displayName: '闪烁速度' }) public flickerSpeed: number = 5.0; @property({ range: [0.0, 1.0], displayName: '闪烁强度' }) public flickerIntensity: number = 0.2; @property({ range: [0.0, 0.5], displayName: '故障强度' }) public glitchIntensity: number = 0.1; @property({ range: [0.0, 0.1], displayName: 'RGB偏移' }) public rgbShift: number = 0.02; @property({ displayName: '全息类型' }) public hologramType: HologramType = HologramType.Basic; @property({ type: Vec3, displayName: '投影中心' }) public projectionCenter: Vec3 = new Vec3(0, 0, 0); @property({ range: [1.0, 20.0], displayName: '投影范围' }) public projectionRadius: number = 5.0; private _material: Material | null = null; private _isProjecting: boolean = false; private _glitchTimer: number = 0; // 全息类型枚举 public enum HologramType { Basic = 'basic-hologram', DataStream = 'data-stream-hologram', Glitch = 'glitch-hologram', ProjectionBounds = 'projection-bounds' } onLoad() { const renderer = this.getComponent('cc.MeshRenderer'); if (renderer && renderer.material) { this._material = renderer.material; this.updateMaterial(); } } update(deltaTime: number) { if (this._isProjecting) { this._glitchTimer += deltaTime; this.updateMaterial(); } } private updateMaterial() { if (!this._material) return; // 基础全息参数 this._material.setProperty('hologramColor', this.hologramColor); this._material.setProperty('transparency', this.transparency); this._material.setProperty('scanlineCount', this.scanlineCount); this._material.setProperty('scanlineSpeed', this.scanlineSpeed); this._material.setProperty('scanlineIntensity', this.scanlineIntensity); this._material.setProperty('flickerSpeed', this.flickerSpeed); this._material.setProperty('flickerIntensity', this.flickerIntensity); // 故障效果参数 this._material.setProperty('glitchIntensity', this.glitchIntensity); this._material.setProperty('rgbShift', this.rgbShift); // 投影边界参数 this._material.setProperty('projectionCenter', this.projectionCenter); this._material.setProperty('projectionRadius', this.projectionRadius); } // 公共接口 public setHologramType(type: HologramType) { this.hologramType = type; // 这里可以切换不同的材质或technique console.log(`切换到全息类�? ${type}`); this.updateMaterial(); } public startProjection() { this._isProjecting = true; this.transparency = 0; // 渐入动画 let elapsed = 0; const fadeIn = (dt: number) => { elapsed += dt; const t = Math.min(elapsed / 1.0, 1.0); this.transparency = t * 0.7; this.updateMaterial(); if (t < 1.0) { this.scheduleOnce(fadeIn, 0); } }; this.scheduleOnce(fadeIn, 0); } public stopProjection() { // 渐出动画 const originalTransparency = this.transparency; let elapsed = 0; const fadeOut = (dt: number) => { elapsed += dt; const t = Math.min(elapsed / 0.5, 1.0); this.transparency = originalTransparency * (1.0 - t); this.updateMaterial(); if (t >= 1.0) { this._isProjecting = false; } else { this.scheduleOnce(fadeOut, 0); } }; this.scheduleOnce(fadeOut, 0); } public setHologramColor(color: Color) { this.hologramColor = color; this.updateMaterial(); } public setTransparency(transparency: number) { this.transparency = transparency; this.updateMaterial(); } // 预设效果 public applyDataTerminalStyle() { this.hologramColor = new Color(0, 255, 200, 255); // 青绿�? this.scanlineCount = 80.0; this.scanlineSpeed = 1.5; this.flickerIntensity = 0.1; this.glitchIntensity = 0.05; this.setHologramType(HologramType.DataStream); } public applyErrorMessageStyle() { this.hologramColor = new Color(255, 100, 100, 255); // 红色 this.scanlineCount = 30.0; this.scanlineSpeed = 5.0; this.flickerIntensity = 0.8; this.glitchIntensity = 0.3; this.rgbShift = 0.05; this.setHologramType(HologramType.Glitch); } public applyCharacterStyle() { this.hologramColor = new Color(100, 150, 255, 255); // 蓝色 this.scanlineCount = 60.0; this.scanlineSpeed = 2.0; this.flickerIntensity = 0.15; this.glitchIntensity = 0.02; this.setHologramType(HologramType.Basic); } public applyMapProjectionStyle() { this.hologramColor = new Color(150, 255, 150, 255); // 绿色 this.projectionRadius = 10.0; this.transparency = 0.6; this.setHologramType(HologramType.ProjectionBounds); } // 特殊效果 public triggerGlitch(duration: number = 2.0) { const originalGlitch = this.glitchIntensity; const originalRgb = this.rgbShift; this.glitchIntensity = 0.5; this.rgbShift = 0.1; this.scheduleOnce(() => { this.glitchIntensity = originalGlitch; this.rgbShift = originalRgb; this.updateMaterial(); }, duration); this.updateMaterial(); } public flashHologram(duration: number = 0.5) { const originalTransparency = this.transparency; let elapsed = 0; const flash = (dt: number) => { elapsed += dt; const t = elapsed / duration; if (t < 0.5) { // 增强阶段 this.transparency = originalTransparency + (1.0 - originalTransparency) * (t * 2); } else { // 恢复阶段 this.transparency = 1.0 - (1.0 - originalTransparency) * ((t - 0.5) * 2); } this.updateMaterial(); if (t < 1.0) { this.scheduleOnce(flash, 0); } else { this.transparency = originalTransparency; this.updateMaterial(); } }; this.scheduleOnce(flash, 0); } public colorCycle(colors: Color[], cycleDuration: number = 3.0) { let colorIndex = 0; const startColor = this.hologramColor.clone(); const cycle = () => { const targetColor = colors[colorIndex % colors.length]; let elapsed = 0; const transition = (dt: number) => { elapsed += dt; const t = Math.min(elapsed / (cycleDuration / colors.length), 1.0); // 颜色插�? this.hologramColor.r = Math.lerp(startColor.r, targetColor.r, t); this.hologramColor.g = Math.lerp(startColor.g, targetColor.g, t); this.hologramColor.b = Math.lerp(startColor.b, targetColor.b, t); this.updateMaterial(); if (t >= 1.0) { startColor.set(targetColor); colorIndex++; if (colorIndex < colors.length) { this.scheduleOnce(cycle, 0); } } else { this.scheduleOnce(transition, 0); } }; this.scheduleOnce(transition, 0); }; cycle(); } public resetToDefault() { this.hologramColor = new Color(50, 200, 255, 255); this.transparency = 0.7; this.scanlineCount = 50.0; this.scanlineSpeed = 2.0; this.scanlineIntensity = 0.3; this.flickerSpeed = 5.0; this.flickerIntensity = 0.2; this.glitchIntensity = 0.1; this.rgbShift = 0.02; this.updateMaterial(); } }
// 导出枚举 export { HologramType } from './HologramController';
|