|
| 1 | +// Unity C# reference source |
| 2 | +// Copyright (c) Unity Technologies. For terms of use, see |
| 3 | +// https://unity3d.com/legal/licenses/Unity_Reference_Only_License |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using UnityEditor; |
| 8 | +using UnityEngine; |
| 9 | +using Object = UnityEngine.Object; |
| 10 | + |
| 11 | +namespace UnityEditorInternal.PlayEm |
| 12 | +{ |
| 13 | + static internal class MaterialAnimationUtility |
| 14 | + { |
| 15 | + const string kMaterialPrefix = "material."; |
| 16 | + |
| 17 | + static PropertyModification[] CreatePropertyModifications(int count, Object target) |
| 18 | + { |
| 19 | + PropertyModification[] modifications = new PropertyModification[count]; |
| 20 | + for (int i = 0; i < modifications.Length; i++) |
| 21 | + { |
| 22 | + modifications[i] = new PropertyModification(); |
| 23 | + modifications[i].target = target; |
| 24 | + } |
| 25 | + return modifications; |
| 26 | + } |
| 27 | + |
| 28 | + static void SetupPropertyModification(string name, float value, PropertyModification prop) |
| 29 | + { |
| 30 | + prop.propertyPath = kMaterialPrefix + name; |
| 31 | + prop.value = value.ToString(); |
| 32 | + } |
| 33 | + |
| 34 | + static PropertyModification[] MaterialPropertyToPropertyModifications(MaterialProperty materialProp, Object target, float value) |
| 35 | + { |
| 36 | + PropertyModification[] modifications = CreatePropertyModifications(1, target); |
| 37 | + SetupPropertyModification(materialProp.name, value, modifications[0]); |
| 38 | + return modifications; |
| 39 | + } |
| 40 | + |
| 41 | + static PropertyModification[] MaterialPropertyToPropertyModifications(MaterialProperty materialProp, Object target, Color color) |
| 42 | + { |
| 43 | + PropertyModification[] modifications = CreatePropertyModifications(4, target); |
| 44 | + SetupPropertyModification(materialProp.name + ".r", color.r, modifications[0]); |
| 45 | + SetupPropertyModification(materialProp.name + ".g", color.g, modifications[1]); |
| 46 | + SetupPropertyModification(materialProp.name + ".b", color.b, modifications[2]); |
| 47 | + SetupPropertyModification(materialProp.name + ".a", color.a, modifications[3]); |
| 48 | + return modifications; |
| 49 | + } |
| 50 | + |
| 51 | + static PropertyModification[] MaterialPropertyToPropertyModifications(MaterialProperty materialProp, Object target, Vector4 vec) |
| 52 | + { |
| 53 | + return MaterialPropertyToPropertyModifications(materialProp.name, target, vec); |
| 54 | + } |
| 55 | + |
| 56 | + static PropertyModification[] MaterialPropertyToPropertyModifications(string name, Object target, Vector4 vec) |
| 57 | + { |
| 58 | + PropertyModification[] modifications = CreatePropertyModifications(4, target); |
| 59 | + SetupPropertyModification(name + ".x", vec.x, modifications[0]); |
| 60 | + SetupPropertyModification(name + ".y", vec.y, modifications[1]); |
| 61 | + SetupPropertyModification(name + ".z", vec.z, modifications[2]); |
| 62 | + SetupPropertyModification(name + ".w", vec.w, modifications[3]); |
| 63 | + return modifications; |
| 64 | + } |
| 65 | + |
| 66 | + static bool ApplyMaterialModificationToAnimationRecording(PropertyModification[] modifications) |
| 67 | + { |
| 68 | + UndoPropertyModification[] undoModifications = new UndoPropertyModification[modifications.Length]; |
| 69 | + for (int i = 0; i < undoModifications.Length; ++i) |
| 70 | + { |
| 71 | + undoModifications[i].previousValue = modifications[i]; |
| 72 | + } |
| 73 | + |
| 74 | + UndoPropertyModification[] ret = Undo.InvokePostprocessModifications(undoModifications); |
| 75 | + return ret.Length != modifications.Length; |
| 76 | + } |
| 77 | + |
| 78 | + static public bool OverridePropertyColor(MaterialProperty materialProp, Renderer target, out Color color) |
| 79 | + { |
| 80 | + var propertyPaths = new List<string>(); |
| 81 | + string basePropertyPath = kMaterialPrefix + materialProp.name; |
| 82 | + |
| 83 | + if (materialProp.type == MaterialProperty.PropType.Texture) |
| 84 | + { |
| 85 | + propertyPaths.Add(basePropertyPath + "_ST.x"); |
| 86 | + propertyPaths.Add(basePropertyPath + "_ST.y"); |
| 87 | + propertyPaths.Add(basePropertyPath + "_ST.z"); |
| 88 | + propertyPaths.Add(basePropertyPath + "_ST.w"); |
| 89 | + } |
| 90 | + else if (materialProp.type == MaterialProperty.PropType.Color) |
| 91 | + { |
| 92 | + propertyPaths.Add(basePropertyPath + ".r"); |
| 93 | + propertyPaths.Add(basePropertyPath + ".g"); |
| 94 | + propertyPaths.Add(basePropertyPath + ".b"); |
| 95 | + propertyPaths.Add(basePropertyPath + ".a"); |
| 96 | + } |
| 97 | + else |
| 98 | + { |
| 99 | + propertyPaths.Add(basePropertyPath); |
| 100 | + } |
| 101 | + |
| 102 | + if (propertyPaths.Exists(path => AnimationMode.IsPropertyAnimated(target, path))) |
| 103 | + { |
| 104 | + color = AnimationMode.animatedPropertyColor; |
| 105 | + if (AnimationMode.InAnimationRecording()) |
| 106 | + color = AnimationMode.recordedPropertyColor; |
| 107 | + else if (propertyPaths.Exists(path => AnimationMode.IsPropertyCandidate(target, path))) |
| 108 | + color = AnimationMode.candidatePropertyColor; |
| 109 | + |
| 110 | + return true; |
| 111 | + } |
| 112 | + |
| 113 | + color = Color.white; |
| 114 | + return false; |
| 115 | + } |
| 116 | + |
| 117 | + static public void SetupMaterialPropertyBlock(MaterialProperty materialProp, int changedMask, Renderer target) |
| 118 | + { |
| 119 | + MaterialPropertyBlock block = new MaterialPropertyBlock(); |
| 120 | + target.GetPropertyBlock(block); |
| 121 | + materialProp.WriteToMaterialPropertyBlock(block, changedMask); |
| 122 | + target.SetPropertyBlock(block); |
| 123 | + } |
| 124 | + |
| 125 | + static public void TearDownMaterialPropertyBlock(Renderer target) |
| 126 | + { |
| 127 | + target.SetPropertyBlock(null); |
| 128 | + } |
| 129 | + |
| 130 | + static public bool ApplyMaterialModificationToAnimationRecording(MaterialProperty materialProp, int changedMask, Renderer target, object oldValue) |
| 131 | + { |
| 132 | + bool applied = false; |
| 133 | + switch (materialProp.type) |
| 134 | + { |
| 135 | + case MaterialProperty.PropType.Color: |
| 136 | + SetupMaterialPropertyBlock(materialProp, changedMask, target); |
| 137 | + applied = ApplyMaterialModificationToAnimationRecording(MaterialPropertyToPropertyModifications(materialProp, target, (Color)oldValue)); |
| 138 | + if (!applied) |
| 139 | + TearDownMaterialPropertyBlock(target); |
| 140 | + return applied; |
| 141 | + |
| 142 | + case MaterialProperty.PropType.Vector: |
| 143 | + SetupMaterialPropertyBlock(materialProp, changedMask, target); |
| 144 | + applied = ApplyMaterialModificationToAnimationRecording(MaterialPropertyToPropertyModifications(materialProp, target, (Vector4)oldValue)); |
| 145 | + if (!applied) |
| 146 | + TearDownMaterialPropertyBlock(target); |
| 147 | + return applied; |
| 148 | + |
| 149 | + case MaterialProperty.PropType.Float: |
| 150 | + case MaterialProperty.PropType.Range: |
| 151 | + SetupMaterialPropertyBlock(materialProp, changedMask, target); |
| 152 | + applied = ApplyMaterialModificationToAnimationRecording(MaterialPropertyToPropertyModifications(materialProp, target, (float)oldValue)); |
| 153 | + if (!applied) |
| 154 | + TearDownMaterialPropertyBlock(target); |
| 155 | + return applied; |
| 156 | + |
| 157 | + case MaterialProperty.PropType.Texture: |
| 158 | + { |
| 159 | + if (MaterialProperty.IsTextureOffsetAndScaleChangedMask(changedMask)) |
| 160 | + { |
| 161 | + string name = materialProp.name + "_ST"; |
| 162 | + SetupMaterialPropertyBlock(materialProp, changedMask, target); |
| 163 | + applied = ApplyMaterialModificationToAnimationRecording(MaterialPropertyToPropertyModifications(name, target, (Vector4)oldValue)); |
| 164 | + if (!applied) |
| 165 | + TearDownMaterialPropertyBlock(target); |
| 166 | + return applied; |
| 167 | + } |
| 168 | + else |
| 169 | + return false; |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + return false; |
| 174 | + } |
| 175 | + |
| 176 | + static public PropertyModification[] MaterialPropertyToPropertyModifications(MaterialProperty materialProp, Renderer target) |
| 177 | + { |
| 178 | + switch (materialProp.type) |
| 179 | + { |
| 180 | + case MaterialProperty.PropType.Color: |
| 181 | + return MaterialPropertyToPropertyModifications(materialProp, target, materialProp.colorValue); |
| 182 | + case MaterialProperty.PropType.Vector: |
| 183 | + return MaterialPropertyToPropertyModifications(materialProp, target, materialProp.vectorValue); |
| 184 | + case MaterialProperty.PropType.Float: |
| 185 | + case MaterialProperty.PropType.Range: |
| 186 | + return MaterialPropertyToPropertyModifications(materialProp, target, materialProp.floatValue); |
| 187 | + |
| 188 | + case MaterialProperty.PropType.Texture: |
| 189 | + { |
| 190 | + string name = materialProp.name + "_ST"; |
| 191 | + return MaterialPropertyToPropertyModifications(name, target, materialProp.vectorValue); |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + return null; |
| 196 | + } |
| 197 | + } |
| 198 | +} |
0 commit comments