public void SetMaterial(P3dBlendMode blendMode, Texture texture, Texture shape, P3dChannel shapeChannel, float hardness, float wrapping, float normalBack, float normalFront, float normalFade, Color color, float opacity, Texture tileTexture, Matrix4x4 tileMatrix, float tileBlend) { switch (Extrusions) { case 0: Material = cachedSpotMaterials[blendMode]; break; case 1: Material = cachedLineMaterials[blendMode]; break; case 2: Material = cachedQuadMaterials[blendMode]; break; } Blend = blendMode; Color = color; Opacity = opacity; Hardness = hardness; Wrapping = wrapping; Texture = texture; Shape = shape; ShapeChannel = P3dHelper.IndexToVector((int)shapeChannel); TileTexture = tileTexture; TileMatrix = tileMatrix; TileBlend = tileBlend; var pointA = normalFront - 1.0f - normalFade; var pointB = normalFront - 1.0f; var pointC = 1.0f - normalBack + normalFade; var pointD = 1.0f - normalBack; NormalFront = new Vector2(pointA, P3dHelper.Reciprocal(pointB - pointA)); NormalBack = new Vector2(pointC, P3dHelper.Reciprocal(pointD - pointC)); }
public void SetMask(Matrix4x4 matrix, Texture shape, P3dChannel channel, Vector3 stretch) { MaskMatrix = matrix; MaskShape = shape; MaskChannel = P3dHelper.IndexToVector((int)channel); MaskStretch = new Vector3(stretch.x * 2.0f, stretch.y * 2.0f, 2.0f); }
protected virtual void Update() { if (paintableTexture != null && paintableTexture.Activated == true) { if (speed > 0.0f) { counter += speed * Time.deltaTime; } if (counter >= threshold) { var step = Mathf.FloorToInt(counter * 255.0f); if (step > 0) { var change = step / 255.0f; counter -= change; P3dCommandFill.Instance.SetState(false, 0); P3dCommandFill.Instance.SetMaterial(blendMode, texture, color, Mathf.Min(change, 1.0f), Mathf.Min(change, 1.0f)); var command = P3dPaintableManager.Submit(P3dCommandFill.Instance, paintableTexture.Paintable, paintableTexture); if (maskPaintableTexture != null) { command.LocalMaskTexture = maskPaintableTexture.Current; command.LocalMaskChannel = P3dHelper.IndexToVector((int)maskChannel); } else if (maskTexture != null) { command.LocalMaskTexture = maskTexture; command.LocalMaskChannel = P3dHelper.IndexToVector((int)maskChannel); } } } } }
private void BakeOverlap() { var desc = new RenderTextureDescriptor(1024, 1024, RenderTextureFormat.ARGB32, 0); var renderTexture = P3dHelper.GetRenderTexture(desc); if (overlapMaterial == null) { overlapMaterial = P3dShader.BuildMaterial("Hidden/Paint in 3D/Overlap"); } overlapMaterial.SetVector(P3dShader._Coord, P3dHelper.IndexToVector(coord)); var oldActive = RenderTexture.active; RenderTexture.active = renderTexture; GL.Clear(true, true, Color.black); overlapMaterial.SetPass(0); Graphics.DrawMeshNow(mesh, Matrix4x4.identity, submesh); foreach (var obj in Selection.objects) { var otherMesh = obj as Mesh; if (otherMesh != null && otherMesh != mesh) { Graphics.DrawMeshNow(otherMesh, Matrix4x4.identity, submesh); } } RenderTexture.active = oldActive; overlapTex = P3dHelper.GetReadableCopy(renderTexture); P3dHelper.ReleaseRenderTexture(renderTexture); var utilizationCount = 0; var overlapCount = 0; for (var y = 0; y < overlapTex.height; y++) { for (var x = 0; x < overlapTex.width; x++) { var pixel = overlapTex.GetPixel(x, y); if (pixel.r > 0.0f) { if (pixel.r > 1.5 / 255.0f) { pixel = Color.red; overlapCount += 1; } else { pixel = Color.gray; } utilizationCount += 1; overlapTex.SetPixel(x, y, pixel); } } } var total = overlapTex.width * overlapTex.height * 0.01f; utilizationPercent = utilizationCount / total; overlapPercent = overlapCount / total; overlapTex.Apply(); }
private void UpdatePaintedMats() { // Mark foreach (var mat in scene.Mats) { foreach (var slot in mat.Slots) { slot.Dirty = true; } } /* * if (mat.Template != null) * { * foreach (var slot in mat.Template.Slots) * { * if (paintedGroups.Contains(slot.WriteR.SourceGroup) == true || * paintedGroups.Contains(slot.WriteG.SourceGroup) == true || * paintedGroups.Contains(slot.WriteB.SourceGroup) == true || * paintedGroups.Contains(slot.WriteA.SourceGroup) == true) * { * var matSlot = mat.Slots.Find(s => s.Name == slot.Name); * * if (matSlot != null) * { * matSlot.Dirty = true; * } * } * } * } * } */ scene.UpdateMergedLayers(currentLayer); foreach (var mat in scene.Mats) { if (mat.Template != null) { foreach (var templateSlot in mat.Template.Slots) { var sceneSlot = mat.GetSlot(templateSlot.Name); if (sceneSlot.Texture == null) { sceneSlot.Texture = P3dHelper.GetRenderTexture(mat.Desc); } var textureR = mat.GetFinalTexture(templateSlot.WriteR.SourceGroup); var textureG = mat.GetFinalTexture(templateSlot.WriteG.SourceGroup); var textureB = mat.GetFinalTexture(templateSlot.WriteB.SourceGroup); var textureA = mat.GetFinalTexture(templateSlot.WriteA.SourceGroup); var channelR = P3dHelper.IndexToVector((int)templateSlot.WriteR.SourceChannel); var channelG = P3dHelper.IndexToVector((int)templateSlot.WriteG.SourceChannel); var channelB = P3dHelper.IndexToVector((int)templateSlot.WriteB.SourceChannel); var channelA = P3dHelper.IndexToVector((int)templateSlot.WriteA.SourceChannel); P3dPaintReplaceChannels.Blit(sceneSlot.Texture, textureR, textureG, textureB, textureA, channelR, channelG, channelB, channelA); } } } // Sweep foreach (var mat in scene.Mats) { for (var i = 0; i < mat.Slots.Count; i++) { var slot = mat.Slots[i]; if (slot.Dirty == true) { P3dHelper.ReleaseRenderTexture(slot.Texture); mat.Slots.RemoveAt(i); } } } }
public virtual void Apply(P3dPaintableTexture paintableTexture) { LocalMaskTexture = paintableTexture.LocalMaskTexture; LocalMaskChannel = P3dHelper.IndexToVector((int)paintableTexture.LocalMaskChannel); }
private void Bake(RenderTexture temporary, int width, int height, RenderTextureFormat format, int scale) { if (bakedPixels == null) { bakedPixels = new List <bool>(); } else { bakedPixels.Clear(); } baked = true; bakedMesh = mesh; bakedSize = new Vector2Int(width, height); if (cachedMaterial == null) { cachedMaterial = P3dPaintableManager.BuildMaterial("Hidden/Paint in 3D/White"); } cachedMaterial.SetVector(P3dShader._Channel, P3dHelper.IndexToVector((int)PaintableTexture.Channel)); // Write to temp RenderTexture var oldActive = RenderTexture.active; var renderTexture = temporary; if (temporary == null) { var desc = new RenderTextureDescriptor(width, height, format, 0); renderTexture = P3dHelper.GetRenderTexture(desc); } RenderTexture.active = renderTexture; GL.Clear(true, true, Color.black); cachedMaterial.SetPass(0); Graphics.DrawMeshNow(mesh, Matrix4x4.identity, PaintableTexture.Slot.Index); RenderTexture.active = oldActive; // Get readable copy var readable = P3dHelper.GetReadableCopy(renderTexture); if (temporary == null) { P3dHelper.ReleaseRenderTexture(renderTexture); } // Run through pixels to count total and build binary mask bakedPixels.Capacity = width * height; total = 0; for (var y = 0; y < height; y++) { for (var x = 0; x < width; x++) { if (readable.GetPixel(x, y).r > 0.5f) { bakedPixels.Add(true); total += scale; } else { bakedPixels.Add(false); } } } // Clean up P3dHelper.Destroy(readable); }
private void DrawNextPaint() { if (P3dPaintMaterial.CachedInstances.Count > 0) { if (paintIndex >= P3dPaintMaterial.CachedInstances.Count) { paintIndex = 0; } var paint = P3dPaintMaterial.CachedInstances[paintIndex++]; DestroyImmediate(paint.Thumbnail); paint.Thumbnail = null; if (paint != null && paint.Thumbnail == null && paint.Material != null) { var tempTextures = new List <RenderTexture>(); if (paintProperties == null) { paintProperties = new MaterialPropertyBlock(); } else { paintProperties.Clear(); } if (paint.Template != null) { foreach (var slot in paint.Template.Slots) { var slotR = paint.GetSlot(slot.WriteR.SourceGroup); var slotG = paint.GetSlot(slot.WriteG.SourceGroup); var slotB = paint.GetSlot(slot.WriteB.SourceGroup); var slotA = paint.GetSlot(slot.WriteA.SourceGroup); if (slotR != null || slotG != null || slotB != null || slotA != null) { var tempTexture = P3dHelper.GetRenderTexture(new RenderTextureDescriptor(1024, 1024, RenderTextureFormat.ARGB32, 0)); tempTextures.Add(tempTexture); var textureR = P3dHelper.GetRenderTexture(tempTexture.descriptor); var textureG = P3dHelper.GetRenderTexture(tempTexture.descriptor); var textureB = P3dHelper.GetRenderTexture(tempTexture.descriptor); var textureA = P3dHelper.GetRenderTexture(tempTexture.descriptor); WriteThumbnailTex(textureR, slotR, slot.WriteR); WriteThumbnailTex(textureG, slotG, slot.WriteG); WriteThumbnailTex(textureB, slotB, slot.WriteB); WriteThumbnailTex(textureA, slotA, slot.WriteA); var channelR = P3dHelper.IndexToVector((int)slot.WriteR.SourceChannel); var channelG = P3dHelper.IndexToVector((int)slot.WriteG.SourceChannel); var channelB = P3dHelper.IndexToVector((int)slot.WriteB.SourceChannel); var channelA = P3dHelper.IndexToVector((int)slot.WriteA.SourceChannel); P3dPaintReplaceChannels.Blit(tempTexture, textureR, textureG, textureB, textureA, channelR, channelG, channelB, channelA); P3dHelper.ReleaseRenderTexture(textureR); P3dHelper.ReleaseRenderTexture(textureG); P3dHelper.ReleaseRenderTexture(textureB); P3dHelper.ReleaseRenderTexture(textureA); paintProperties.SetTexture(slot.Name, tempTexture); } } } BeginPreview(thumbnailUtil, new Rect(0, 0, settings.ThumbnailSize, settings.ThumbnailSize)); var probeAnchor = default(Transform); var probeExists = false; if (settings.Environment != null) { probeAnchor = settings.Environment.transform; probeExists = true; } switch (paint.Style) { case P3dPaintMaterial.StyleType.Seamless: { thumbnailUtil.DrawMesh(P3dHelper.GetSphereMesh(), Matrix4x4.identity, paint.Material, 0, paintProperties, probeAnchor, probeExists); } break; case P3dPaintMaterial.StyleType.Decal: { thumbnailUtil.DrawMesh(P3dHelper.GetQuadMesh(), Matrix4x4.identity, paint.Material, 0, paintProperties, probeAnchor, probeExists); } break; } paint.Thumbnail = P3dHelper.GetReadableCopy(EndPreview(thumbnailUtil)); foreach (var tempTexture in tempTextures) { P3dHelper.ReleaseRenderTexture(tempTexture); } } } }