private static void TryBlendInto(ref RenderTexture renderTexture, Layer layer, int matId, P3dGroup group) { var image = layer.GetImage(matId, group); var texture = image.Preview ?? image.Current; if (texture != null) { renderTexture = P3dCommandFill.Blit(renderTexture, P3dBlendMode.AlphaBlend, texture, Color.white, layer.Opacity, 0.0f); } }
public void UpdateMergedLayers(Layer currentLayer) { foreach (var mat in Mats) { foreach (var mergedLayer in mat.MergedLayers.Values) { mergedLayer.Dirty = true; if (mergedLayer.Layer != currentLayer) { mergedLayer.Clear(); mergedLayer.Layer = currentLayer; } } if (mat.Template != null) { foreach (var slot in mat.Template.Slots) { mat.UpdateMergedLayers(slot.WriteR.SourceGroup); mat.UpdateMergedLayers(slot.WriteG.SourceGroup); mat.UpdateMergedLayers(slot.WriteB.SourceGroup); mat.UpdateMergedLayers(slot.WriteA.SourceGroup); } } foreach (var pair in mat.MergedLayers) { var group = pair.Key; var mergedLayer = pair.Value; if (mergedLayer.Dirty == true) { mergedLayer.Clear(); } else { var currentLayerIndex = Layers.IndexOf(currentLayer); if (mergedLayer.Under == null) { mergedLayer.Under = P3dHelper.GetRenderTexture(mat.Desc); var groupData = P3dGroupData.GetGroupData(group); if (groupData != null) { P3dCommandReplace.Blit(mergedLayer.Under, groupData.DefaultTexture, groupData.DefaultColor); } else { P3dCommandReplace.Blit(mergedLayer.Under, default(Texture), default(Color)); } for (var i = 0; i < currentLayerIndex; i++) { TryBlendInto(ref mergedLayer.Under, Layers[i], mat.Id, group); } } // Last layer? if (currentLayerIndex == Layers.Count - 1) { if (mergedLayer.Above != null) { mergedLayer.Above = P3dHelper.ReleaseRenderTexture(mergedLayer.Above); } } else { if (mergedLayer.Above == null) { mergedLayer.Above = P3dHelper.GetRenderTexture(mat.Desc); P3dCommandReplace.Blit(mergedLayer.Above, default(Texture), default(Color)); for (var i = currentLayerIndex + 1; i < Layers.Count; i++) { TryBlendInto(ref mergedLayer.Above, Layers[i], mat.Id, group); } } } if (mergedLayer.Final == null) { mergedLayer.Final = P3dHelper.GetRenderTexture(mat.Desc); } P3dCommandReplace.Blit(mergedLayer.Final, mergedLayer.Under, Color.white); TryBlendInto(ref mergedLayer.Final, currentLayer, mat.Id, group); if (mergedLayer.Above != null) { mergedLayer.Final = P3dCommandFill.Blit(mergedLayer.Final, P3dBlendMode.AlphaBlend, mergedLayer.Above, Color.white, 1.0f, 0.0f); } } } } }