private static bool GetRectAndInvertMatrix(ref P3D_Matrix matrix, ref P3D_Rect rect, int canvasW, int canvasH) { // Grab transformed corners var a = matrix.MultiplyPoint(0.0f, 0.0f); var b = matrix.MultiplyPoint(1.0f, 0.0f); var c = matrix.MultiplyPoint(0.0f, 1.0f); var d = matrix.MultiplyPoint(1.0f, 1.0f); // Find min/max x/y var xMin = Mathf.Min(Mathf.Min(a.x, b.x), Mathf.Min(c.x, d.x)); var xMax = Mathf.Max(Mathf.Max(a.x, b.x), Mathf.Max(c.x, d.x)); var yMin = Mathf.Min(Mathf.Min(a.y, b.y), Mathf.Min(c.y, d.y)); var yMax = Mathf.Max(Mathf.Max(a.y, b.y), Mathf.Max(c.y, d.y)); // Has volume? if (xMin < xMax && yMin < yMax) { // Make sure rect doesn't go outside canvas rect.XMin = Mathf.Clamp(Mathf.FloorToInt(xMin), 0, canvasW); rect.XMax = Mathf.Clamp(Mathf.CeilToInt(xMax), 0, canvasW); rect.YMin = Mathf.Clamp(Mathf.FloorToInt(yMin), 0, canvasH); rect.YMax = Mathf.Clamp(Mathf.CeilToInt(yMax), 0, canvasH); matrix = matrix.Inverse; return(true); } return(false); }
public static P3D_Matrix CreateMatrix(Vector2 position, Vector2 size, float angle) { P3D_Matrix matrix3 = P3D_Matrix.Translation(size.x * -0.5f, size.y * -0.5f); P3D_Matrix matrix4 = P3D_Matrix.Scaling(size.x, size.y); return(((P3D_Matrix.Translation(position.x, position.y) * P3D_Matrix.Rotation(angle)) * matrix3) * matrix4); }
public void Paint(Texture2D newCanvas, P3D_Matrix newMatrix) { canvas = newCanvas; canvasW = newCanvas.width; canvasH = newCanvas.height; matrix = newMatrix; if (this.CalculateRect(ref rect)) { inverse = newMatrix.Inverse; opacity = this.Opacity; color = this.Color; direction = this.Direction; shape = this.Shape; detail = this.Detail; detailScale = this.DetailScale; if (OnPrePaint != null) { OnPrePaint(canvas, rect); } switch (this.Blend) { case P3D_BlendMode.AlphaBlend: AlphaBlend.Paint(); break; case P3D_BlendMode.AlphaBlendRgb: AlphaBlendRGB.Paint(); break; case P3D_BlendMode.AlphaErase: AlphaErase.Paint(); break; case P3D_BlendMode.AdditiveBlend: AdditiveBlend.Paint(); break; case P3D_BlendMode.SubtractiveBlend: SubtractiveBlend.Paint(); break; case P3D_BlendMode.NormalBlend: NormalBlend.Paint(); break; case P3D_BlendMode.Replace: Replace.Paint(); break; default: break; } if (OnPostPaint != null) { OnPostPaint(canvas, rect); } } }
// This causes the current paint operation to get applied to the specified matrix in pixel space public void Paint(P3D_Matrix matrix) { if (texture != null && paintOperation != null) { Dirty = true; paintOperation(texture, matrix, Opacity); } }
public static P3D_Matrix CreateMatrix(Vector2 position, Vector2 size, float angle) { var t = P3D_Matrix.Translation(position.x, position.y); var r = P3D_Matrix.Rotation(angle); var o = P3D_Matrix.Translation(size.x * -0.5f, size.y * -0.5f); var s = P3D_Matrix.Scaling(size.x, size.y); return(t * r * o * s); }
public bool Paint(P3D_Brush brush, P3D_Matrix matrix) { if ((this.Canvas == null) || (brush == null)) { return(false); } brush.Paint(this.Canvas, matrix); this.Dirty = true; return(true); }
public bool Paint(P3D_Brush brush, float x, float y) { if (brush == null) { return(false); } Vector2 vector = new Vector2(x, y); P3D_Matrix matrix = P3D_Helper.CreateMatrix(vector + brush.Offset, brush.Size, brush.Angle); return(this.Paint(brush, matrix)); }
// This causes the current paint operation to get applied to the specified matrix in pixel space public bool Paint(P3D_Brush brush, P3D_Matrix matrix) { if (Canvas != null && brush != null) { brush.Paint(Canvas, matrix); Dirty = true; return(true); } return(false); }
private static bool IsInsideShape(P3D_Matrix inverseMatrix, int x, int y, ref Vector2 shapeCoord) { shapeCoord = inverseMatrix.MultiplyPoint(x, y); if (shapeCoord.x >= 0.0f && shapeCoord.x < 1.0f) { if (shapeCoord.y >= 0.0f && shapeCoord.y < 1.0f) { return(true); } } return(false); }
private void UpdateShow(Mesh mesh, int submeshIndex, Transform target, float opacity, P3D_Matrix paintMatrix, Vector2 canvasResolution, Texture2D shape, Color color, Vector2 tiling, Vector2 offset) { if (target != null) { if (meshRenderer == null) { meshRenderer = gameObject.AddComponent <MeshRenderer>(); } if (meshFilter == null) { meshFilter = gameObject.AddComponent <MeshFilter>(); } if (material == null) { material = new Material(Shader.Find("Hidden/P3D_BrushPreview")); } transform.position = target.position; transform.rotation = target.rotation; transform.localScale = target.lossyScale; material.hideFlags = HideFlags.HideAndDontSave; material.SetMatrix("_WorldMatrix", target.localToWorldMatrix); material.SetMatrix("_PaintMatrix", paintMatrix.Matrix4x4); material.SetVector("_CanvasResolution", canvasResolution); material.SetVector("_Tiling", tiling); material.SetVector("_Offset", offset); material.SetColor("_Color", color); material.SetTexture("_Shape", shape); if (materials.Length != submeshIndex + 1) { materials = new Material[submeshIndex + 1]; } for (var i = 0; i < submeshIndex; i++) { materials[i] = P3D_Helper.ClearMaterial; } materials[submeshIndex] = material; meshRenderer.sharedMaterials = materials; meshFilter.sharedMesh = mesh; age = 0; } }
// Call this if you want to show the brush preview yourself public static void Show(Mesh mesh, int submeshIndex, Transform transform, float opacity, P3D_Matrix paintMatrix, Vector2 canvasResolution, Texture2D shape, Color color, Vector2 tiling, Vector2 offset) { for (var i = AllPreviews.Count - 1; i >= 0; i--) { var preview = AllPreviews[i]; if (preview != null && preview.age > 0) { preview.UpdateShow(mesh, submeshIndex, transform, opacity, paintMatrix, canvasResolution, shape, color, tiling, offset); return; } } var newGameObject = new GameObject("P3D_BrushPreview"); newGameObject.hideFlags = HideFlags.HideAndDontSave; var newPreview = newGameObject.AddComponent <P3D_BrushPreview>(); newPreview.hideFlags = HideFlags.HideAndDontSave; newPreview.UpdateShow(mesh, submeshIndex, transform, opacity, paintMatrix, canvasResolution, shape, color, tiling, offset); }
private void UpdateShow(Mesh mesh, int submeshIndex, Transform target, float opacity, P3D_Matrix paintMatrix, Vector2 canvasResolution, Texture2D shape, Vector2 tiling, Vector2 offset) { if (target != null) { if (this.meshRenderer == null) { this.meshRenderer = base.gameObject.AddComponent <MeshRenderer>(); } if (this.meshFilter == null) { this.meshFilter = base.gameObject.AddComponent <MeshFilter>(); } if (this.material == null) { this.material = new Material(Shader.Find("Hidden/P3D_BrushPreview")); } base.transform.position = target.position; base.transform.rotation = target.rotation; base.transform.localScale = target.lossyScale; this.material.hideFlags = HideFlags.HideAndDontSave; this.material.SetMatrix("_WorldMatrix", target.localToWorldMatrix); this.material.SetMatrix("_PaintMatrix", paintMatrix.Matrix4x4); this.material.SetVector("_CanvasResolution", canvasResolution); this.material.SetVector("_Tiling", tiling); this.material.SetVector("_Offset", offset); this.material.SetColor("_Color", new Color(1f, 1f, 1f, opacity)); this.material.SetTexture("_Shape", shape); if (this.materials.Length != (submeshIndex + 1)) { this.materials = new Material[submeshIndex + 1]; } int index = 0; while (true) { if (index >= submeshIndex) { this.materials[submeshIndex] = this.material; this.meshRenderer.sharedMaterials = this.materials; this.meshFilter.sharedMesh = mesh; this.age = 0; break; } this.materials[index] = P3D_Helper.ClearMaterial; index++; } } }
public static void Show(Mesh mesh, int submeshIndex, Transform transform, float opacity, P3D_Matrix paintMatrix, Vector2 canvasResolution, Texture2D shape, Vector2 tiling, Vector2 offset) { for (int i = AllPreviews.Count - 1; i >= 0; i--) { P3D_BrushPreview preview = AllPreviews[i]; if ((preview != null) && (preview.age > 0)) { preview.UpdateShow(mesh, submeshIndex, transform, opacity, paintMatrix, canvasResolution, shape, tiling, offset); return; } } P3D_BrushPreview preview2 = new GameObject("P3D_BrushPreview") { hideFlags = HideFlags.HideAndDontSave }.AddComponent <P3D_BrushPreview>(); preview2.hideFlags = HideFlags.HideAndDontSave; preview2.UpdateShow(mesh, submeshIndex, transform, opacity, paintMatrix, canvasResolution, shape, tiling, offset); }
private static bool IsInsideShape(P3D_Matrix inverseMatrix, int x, int y, ref Vector2 shapeCoord) { shapeCoord = inverseMatrix.MultiplyPoint((float)x, (float)y); return((shapeCoord.x >= 0f) && ((shapeCoord.x < 1f) && ((shapeCoord.y >= 0f) && (shapeCoord.y < 1f)))); }