/// <summary>This allows you to clear all pixels in this paintable texture with the specified color and texture.</summary> public void Clear(Texture texture, Color color) { if (activated == true) { P3dPaintReplace.Blit(current, texture, color); } }
/// <summary>This method will resize the current texture state with the specified width and height. /// NOTE: This component must be activated.</summary> public bool Resize(int width, int height, bool copyContents = true) { if (activated == true) { if (current.width != width || current.height != height) { var descriptor = current.descriptor; descriptor.width = width; descriptor.height = height; var newCurrent = P3dHelper.GetRenderTexture(descriptor); if (copyContents == true) { P3dPaintReplace.Blit(newCurrent, current, Color.white); if (newCurrent.useMipMap == true) { newCurrent.GenerateMips(); } } P3dHelper.ReleaseRenderTexture(current); current = newCurrent; return(true); } } return(false); }
private void RebuildFromCommands() { P3dPaintReplace.Blit(current, texture, color); var localToWorldMatrix = transform.localToWorldMatrix; for (var i = 0; i <= stateIndex; i++) { var paintableState = paintableStates[i]; var commandCount = paintableState.Commands.Count; for (var j = 0; j < commandCount; j++) { var worldCommand = paintableState.Commands[j].SpawnCopy(); worldCommand.SetLocation(localToWorldMatrix * worldCommand.Matrix); AddCommand(worldCommand); } } ExecuteCommands(false); NotifyOnModified(false); }
public static bool Downsample(RenderTexture renderTexture, int steps, ref RenderTexture temporary) { if (steps > 0 && renderTexture != null) { // Perform initial downsample to get buffer var oldActive = RenderTexture.active; var desc = new RenderTextureDescriptor(renderTexture.width / 2, renderTexture.height / 2, renderTexture.format, 0); var halfRenderTexture = GetRenderTexture(desc); P3dPaintReplace.BlitFast(halfRenderTexture, renderTexture, Color.white); // Ping pong downsamples for (var i = 1; i < steps; i++) { desc.width /= 2; desc.height /= 2; renderTexture = halfRenderTexture; halfRenderTexture = GetRenderTexture(desc); Graphics.Blit(renderTexture, halfRenderTexture); ReleaseRenderTexture(renderTexture); } temporary = halfRenderTexture; RenderTexture.active = oldActive; return(true); } return(false); }
public void Activate() { if (activated == false) { UpdateMaterial(); if (material != null) { var finalWidth = width; var finalHeight = height; var finalTexture = material.GetTexture(slot.Name); CachedPaintable.ScaleSize(ref finalWidth, ref finalHeight); if (texture != null) { finalTexture = texture; } if (string.IsNullOrEmpty(shaderKeyword) == false) { material.EnableKeyword(shaderKeyword); } var desc = new RenderTextureDescriptor(width, height, format, 0); desc.autoGenerateMips = false; if (mipMaps == MipType.ForceOn) { desc.useMipMap = true; } else if (mipMaps == MipType.Auto && P3dHelper.HasMipMaps(finalTexture) == true) { desc.useMipMap = true; } current = P3dHelper.GetRenderTexture(desc); P3dPaintReplace.Blit(current, finalTexture, color); if (current.useMipMap == true) { current.GenerateMips(); } material.SetTexture(slot.Name, current); activated = true; if (string.IsNullOrEmpty(saveName) == false) { Load(); } NotifyOnModified(false); } } }
public void Resize() { foreach (var slot in Slots) { if (slot.Texture != null) { if (slot.Texture.width != Width || slot.Texture.height != Height) { var desc = slot.Texture.descriptor; desc.width = Width; desc.height = Height; var resizedTexture = P3dHelper.GetRenderTexture(desc); P3dPaintReplace.Blit(resizedTexture, slot.Texture, Color.white); P3dHelper.ReleaseRenderTexture(slot.Texture); slot.Texture = resizedTexture; } } } }
private void WriteThumbnailTex(RenderTexture texture, P3dPaintMaterial.Slot paintSlot, P3dShaderTemplate.Write write) { if (paintSlot != null) { P3dPaintReplace.Blit(texture, paintSlot.Texture, paintSlot.Color); } else { var group = P3dGroupData.GetGroupData(write.SourceGroup); if (group != null) { P3dPaintReplace.Blit(texture, group.DefaultTexture, group.DefaultColor); } else { P3dPaintReplace.Blit(texture, default(Texture), default(Color)); } } }
public void Activate() { if (activated == false) { UpdateMaterial(); if (material != null) { var finalWidth = width; var finalHeight = height; var finalTexture = material.GetTexture(slot.Name); CachedPaintable.ScaleSize(ref finalWidth, ref finalHeight); if (texture != null) { finalTexture = texture; } if (string.IsNullOrEmpty(shaderKeyword) == false) { material.EnableKeyword(shaderKeyword); } current = P3dHelper.GetRenderTexture(finalWidth, finalHeight, 0, format); P3dPaintReplace.Blit(current, finalTexture, color); material.SetTexture(slot.Name, current); activated = true; if (string.IsNullOrEmpty(saveName) == false) { Load(); } NotifyOnModified(false); } } }
private void Render(P3dCommand command, P3dScene.Mat mat, P3dScene.Image image, P3dScene.Obj obj, int subMesh) { var oldActive = RenderTexture.active; if (image.Current == null) { if (image.Width > 0 && image.Height > 0 && image.Pixels != null && image.Pixels.Length > 0) { var texture = new Texture2D(1, 1); if (texture.LoadImage(image.Pixels) == true) { var desc = mat.Desc; desc.width = image.Width; desc.height = image.Height; image.Current = P3dHelper.GetRenderTexture(mat.Desc); P3dPaintReplace.BlitFast(image.Current, texture, Color.white); } else { image.Current = P3dHelper.GetRenderTexture(mat.Desc); P3dPaintReplace.BlitFast(image.Current, default(Texture), default(Color)); } DestroyImmediate(texture); } else { image.Current = P3dHelper.GetRenderTexture(mat.Desc); P3dPaintReplace.BlitFast(image.Current, default(Texture), default(Color)); } } var swap = P3dHelper.GetRenderTexture(image.Current.descriptor); if (command.Preview == true) { if (image.Preview == null) { image.Preview = P3dHelper.GetRenderTexture(image.Current.descriptor); P3dPaintReplace.BlitFast(image.Preview, image.Current, Color.white); } P3dPaintReplace.BlitFast(swap, image.Preview, Color.white); command.Apply(image.Preview); } else { P3dPaintReplace.BlitFast(swap, image.Current, Color.white); command.Apply(image.Current); } RenderTexture.active = swap; if (command.RequireMesh == true) { P3dHelper.Draw(command.Material, obj.Mesh, obj.Matrix, subMesh, obj.Coord); } else { P3dHelper.Draw(command.Material); } RenderTexture.active = oldActive; if (command.Preview == true) { P3dHelper.ReleaseRenderTexture(image.Preview); image.Preview = swap; previewDrawn = true; } else { P3dHelper.ReleaseRenderTexture(image.Current); image.Current = swap; } paintedGroups.Add(command.Priority); // Group is stored in priority }
/// <summary>This allows you to manually execute all commands in the paint stack. /// This is useful if you need to modify the state of your object before the end of the frame.</summary> public void ExecuteCommands(bool sendNotifications) { if (activated == true) { var hidePreview = true; if (CommandsPending == true) { var oldActive = RenderTexture.active; var swap = P3dHelper.GetRenderTexture(current.descriptor); var preparedMesh = default(Mesh); var preparedMatrix = default(Matrix4x4); // Blit so we don't lose non-UVd areas P3dPaintReplace.BlitFast(swap, current, Color.white); // Paint if (paintCommands.Count > 0) { ExecuteCommands(paintCommands, sendNotifications, ref current, ref swap, ref preparedMesh, ref preparedMatrix); } // Preview if (previewCommands.Count > 0) { if (previewSet == false) { preview = P3dHelper.GetRenderTexture(current.descriptor); previewSet = true; } hidePreview = false; preview.DiscardContents(); Graphics.Blit(current, preview); previewCommands.Sort(P3dCommand.Compare); ExecuteCommands(previewCommands, sendNotifications, ref preview, ref swap, ref preparedMesh, ref preparedMatrix); } P3dHelper.ReleaseRenderTexture(swap); RenderTexture.active = oldActive; } if (hidePreview == true && previewSet == true) { P3dHelper.ReleaseRenderTexture(preview); preview = null; previewSet = false; } if (materialSet == false) { UpdateMaterial(); } material.SetTexture(slot.Name, previewSet == true ? preview : current); } }
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) { P3dPaintReplace.Blit(mergedLayer.Under, groupData.DefaultTexture, groupData.DefaultColor); } else { P3dPaintReplace.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); P3dPaintReplace.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); } P3dPaintReplace.Blit(mergedLayer.Final, mergedLayer.Under, Color.white); TryBlendInto(ref mergedLayer.Final, currentLayer, mat.Id, group); if (mergedLayer.Above != null) { mergedLayer.Final = P3dPaintFill.Blit(mergedLayer.Final, P3dBlendMode.AlphaBlend, mergedLayer.Above, Color.white, 1.0f, 0.0f); } } } } }