import { _decorator, Component, Node, Vec3, Material, MeshRenderer, Vec4 } from 'cc'; const { ccclass, property } = _decorator; @ccclass('CubeInfo') export class CubeInfo extends Component { // 方块在 map 中的坐标 private PosX: number = -1; private PosY: number = -1; private PosZ: number = -1; private id: number = -1; // 方块 id @property(Material) private mater: Material; // 方块材质 private lock: boolean; // 是否解锁 @property(Material) // 引用消融材质 dissolveMaterial: Material = null; private dissolveThreshold: number = 0.0; // 消融阈值 private dissolveSpeed: number = 1.0 / 3.0; // 每秒增加的消融值,确保3秒消融完成 private isDissolving: boolean = false; private _originalMaterial: Material = null; start() { } setOriginalMaterial(material: Material) { this._originalMaterial = material; } public triggerDissolve() { if (!this.isDissolving) { this.isDissolving = true; this.dissolveThreshold = 0.0; this.getComponentInChildren(MeshRenderer).setMaterialInstance(this.dissolveMaterial, 0); } } update(deltaTime: number) { if (this.isDissolving) { // 通过消融速度更新消融阈值,3秒内完成 this.dissolveThreshold += this.dissolveSpeed * deltaTime; console.log("Dissolve Threshold:", this.dissolveThreshold); // 控制消融速度,使其平滑 if (this.dissolveThreshold >= 1.0) { this.dissolveThreshold = 1.0; // 恢复原始材质 this.getComponentInChildren(MeshRenderer).setMaterialInstance(this._originalMaterial, 0); this.isDissolving = false; } // 获取方块的世界坐标 const worldPos = this.node.getWorldPosition(); console.log("World Position:", worldPos); // 输出世界坐标 // 将 Vec3 转换为 Vec4,最后一位设置为 1.0 const objectCenter = new Vec4(worldPos.x, worldPos.y, worldPos.z, 1.0); // 将方块位置传递给 Shader,作为 objectCenter this.dissolveMaterial.setProperty('objectCenter', objectCenter); // 更新消融阈值到材质中 this.dissolveMaterial.setProperty('dissolveThreshold', this.dissolveThreshold); } } public setPos(x: number, y: number, z: number) { this.PosX = x; this.PosY = y; this.PosZ = z; } public getPos(): Vec3 { return new Vec3(this.PosX, this.PosY, this.PosZ); } public setId(value: number) { this.id = value; } public getId() { return this.id; } public getLock() { return this.lock; } public setLock(value: boolean) { this.lock = value; } public getMaterial() { return this.mater; } public setMaterial(value: Material) { this.mater = value; } }