private void UpdateMatrix(bool reset = false) { UnityEngine.Matrix4x4 thisWorldToLocalMatrix = this.transform.worldToLocalMatrix; if (reset || this.previousWorldToLocal != thisWorldToLocalMatrix || this.previousScale != this.scale) { MaterialPropertyBlockAdder materialPropertyBlockAdder = this.transform.GetComponentInParent <MaterialPropertyBlockAdder>(); if (materialPropertyBlockAdder != null) { int propertyCount = materialPropertyBlockAdder.PropertyMatrices != null ? materialPropertyBlockAdder.PropertyMatrices.Length : 0; bool found = false; for (int i = 0; i < propertyCount; ++i) { if (materialPropertyBlockAdder.PropertyMatrices[i].PropertyName == this.propertyName) { found = true; // Pour forcer la disparition on fait croire que le decal est infiniment petit et pas au bon endroit. UnityEngine.Matrix4x4 matrixToUSe = reset ? UnityEngine.Matrix4x4.Translate(UnityEngine.Vector3.one * -1) * UnityEngine.Matrix4x4.Scale(UnityEngine.Vector3.zero) : thisWorldToLocalMatrix; materialPropertyBlockAdder.PropertyMatrices[i].PropertyValue = UnityEngine.Matrix4x4.Scale(new UnityEngine.Vector3(1 / this.scale.x, 1 / scale.y, 1 / scale.z)) * matrixToUSe; } } if (!found) { System.Array.Resize(ref materialPropertyBlockAdder.PropertyMatrices, propertyCount + 1); materialPropertyBlockAdder.PropertyMatrices[propertyCount].PropertyName = this.propertyName; materialPropertyBlockAdder.PropertyMatrices[propertyCount].PropertyValue = thisWorldToLocalMatrix; } materialPropertyBlockAdder.Apply(); } this.previousWorldToLocal = thisWorldToLocalMatrix; this.previousScale = this.scale; } }
public void OnSpace() { MaterialPropertyBlockAdder materialPropertyBlockAdder = this.transform.GetComponent <MaterialPropertyBlockAdder>(); for (int i = 0; i < materialPropertyBlockAdder.PropertyVectors.Length; ++i) { if (materialPropertyBlockAdder.PropertyVectors[i].PropertyName == this.propertyName) { float unityTime = UnityEngine.Time.time; UnityEngine.Vector4 previousValue = materialPropertyBlockAdder.PropertyVectors[i].PropertyValue; float lerpFactor = System.Math.Max(0, System.Math.Min(1, (unityTime - previousValue.y) / (previousValue.w - previousValue.y))); float currentValue = previousValue.z * lerpFactor + previousValue.x * (1 - lerpFactor); float duration = (1 - previousValue.z) > 0.5 ? fadeIn : fadeOut; materialPropertyBlockAdder.PropertyVectors[i].PropertyValue = new UnityEngine.Vector4(currentValue, unityTime, 1 - previousValue.z, unityTime + duration * lerpFactor); } } materialPropertyBlockAdder.Apply(); }