Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize color calculation #88

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions DragonBones/src/DragonBones/armature/Armature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -967,12 +967,7 @@ public object replacedTexture
}

this._replacedTexture = value;

foreach (var slot in this._slots)
{
slot.InvalidUpdate();
slot.Update(-1);
}
UpdateSlots();
}
}
/// <inheritDoc/>
Expand Down Expand Up @@ -1025,6 +1020,15 @@ public WorldClock clock
public Slot parent
{
get { return this._parent; }
}

internal void UpdateSlots()
{
foreach (var slot in this._slots)
{
slot.InvalidUpdate();
slot.Update(-1);
}
}
}
}
19 changes: 16 additions & 3 deletions Unity/src/DragonBones/Scripts/unity/UnityArmatureComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ public class UnityArmatureComponent : DragonBoneEventDispatcher, IArmatureProxy
private bool _disposeProxy = true;
/// <private/>
internal Armature _armature = null;
private Dictionary<Material, Material> _customMaterialOverride;
public Dictionary<Material, Material> CustomMaterialOverride => _customMaterialOverride;

[Tooltip("0 : Loop")]
[Range(0, 100)]
[SerializeField]
Expand Down Expand Up @@ -666,7 +669,8 @@ private bool _IsPrefab()

/// <private/>
void Awake()
{
{
_customMaterialOverride = new Dictionary<Material, Material>();
#if UNITY_EDITOR
if (_IsPrefab())
{
Expand Down Expand Up @@ -707,8 +711,8 @@ void Awake()
_armature.animation.Play(animationName, _playTimes);
}
}


else
Debug.LogError($"_armature is null, name: {name}");
}

void Start()
Expand Down Expand Up @@ -819,5 +823,14 @@ public void CloseCombineMeshs()
}
}
}

public void SetMaterialOverride(Material material, Material materialOverride)
{
if (materialOverride != null)
_customMaterialOverride[material] = materialOverride;
else
_customMaterialOverride.Remove(material);
_armature.UpdateSlots();
}
}
}
36 changes: 24 additions & 12 deletions Unity/src/DragonBones/Scripts/unity/UnitySlot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -397,12 +397,18 @@ internal override void _UpdateBlendMode()
if (this._childArmature == null)
{
if (this._uiDisplay != null)
{
this._uiDisplay.material = (this._textureData as UnityTextureData).GetMaterial(this._blendMode, true);
{
Material material = (this._textureData as UnityTextureData).GetMaterial(this._blendMode, true);
if (_proxy.CustomMaterialOverride.TryGetValue(material, out Material materialOverride))
material = materialOverride;
this._uiDisplay.material = material;
}
else
{
this._meshRenderer.sharedMaterial = (this._textureData as UnityTextureData).GetMaterial(this._blendMode);
Material material = (this._textureData as UnityTextureData).GetMaterial(this._blendMode);
if (_proxy.CustomMaterialOverride.TryGetValue(material, out Material materialOverride))
material = materialOverride;
this._meshRenderer.sharedMaterial = material;
}

this._meshBuffer.name = this._uiDisplay != null ? this._uiDisplay.material.name : this._meshRenderer.sharedMaterial.name;
Expand Down Expand Up @@ -430,13 +436,15 @@ protected override void _UpdateColor()
if (this._isCombineMesh)
{
var meshBuffer = this._combineMesh.meshBuffers[this._sumMeshIndex];
Color32 color32 = default;
color32.r = (byte)(_colorTransform.redMultiplier * proxyTrans.redMultiplier * 255);
color32.g = (byte)(_colorTransform.greenMultiplier * proxyTrans.greenMultiplier * 255);
color32.b = (byte)(_colorTransform.blueMultiplier * proxyTrans.blueMultiplier * 255);
color32.a = (byte)(_colorTransform.alphaMultiplier * proxyTrans.alphaMultiplier * 255);
for (var i = 0; i < this._meshBuffer.vertexBuffers.Length; i++)
{
var index = this._verticeOffset + i;
this._meshBuffer.color32Buffers[i].r = (byte)(_colorTransform.redMultiplier * proxyTrans.redMultiplier * 255);
this._meshBuffer.color32Buffers[i].g = (byte)(_colorTransform.greenMultiplier * proxyTrans.greenMultiplier * 255);
this._meshBuffer.color32Buffers[i].b = (byte)(_colorTransform.blueMultiplier * proxyTrans.blueMultiplier * 255);
this._meshBuffer.color32Buffers[i].a = (byte)(_colorTransform.alphaMultiplier * proxyTrans.alphaMultiplier * 255);
this._meshBuffer.color32Buffers[i] = color32;
//
meshBuffer.color32Buffers[index] = this._meshBuffer.color32Buffers[i];
}
Expand All @@ -445,12 +453,14 @@ protected override void _UpdateColor()
}
else if (this._meshBuffer.sharedMesh != null)
{
Color32 color32 = default;
color32.r = (byte)(_colorTransform.redMultiplier * proxyTrans.redMultiplier * 255);
color32.g = (byte)(_colorTransform.greenMultiplier * proxyTrans.greenMultiplier * 255);
color32.b = (byte)(_colorTransform.blueMultiplier * proxyTrans.blueMultiplier * 255);
color32.a = (byte)(_colorTransform.alphaMultiplier * proxyTrans.alphaMultiplier * 255);
for (int i = 0, l = this._meshBuffer.sharedMesh.vertexCount; i < l; ++i)
{
this._meshBuffer.color32Buffers[i].r = (byte)(_colorTransform.redMultiplier * proxyTrans.redMultiplier * 255);
this._meshBuffer.color32Buffers[i].g = (byte)(_colorTransform.greenMultiplier * proxyTrans.greenMultiplier * 255);
this._meshBuffer.color32Buffers[i].b = (byte)(_colorTransform.blueMultiplier * proxyTrans.blueMultiplier * 255);
this._meshBuffer.color32Buffers[i].a = (byte)(_colorTransform.alphaMultiplier * proxyTrans.alphaMultiplier * 255);
this._meshBuffer.color32Buffers[i] = color32;
}
//
this._meshBuffer.UpdateColors();
Expand All @@ -475,7 +485,9 @@ protected override void _UpdateFrame()
this._isActive = false;
if (this._displayIndex >= 0 && this._display != null && currentTextureData != null)
{
var currentTextureAtlas = _proxy.isUGUI ? currentTextureAtlasData.uiTexture : currentTextureAtlasData.texture;
Material currentTextureAtlas = _proxy.isUGUI ? currentTextureAtlasData.uiTexture : currentTextureAtlasData.texture;
if (_proxy.CustomMaterialOverride.TryGetValue(currentTextureAtlas, out Material materialOverride))
currentTextureAtlas = materialOverride;
if (currentTextureAtlas != null)
{
this._isActive = true;
Expand Down