Skip to content

Commit

Permalink
Eraser enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
Harley-xk committed Apr 7, 2019
1 parent 806462a commit d513703
Show file tree
Hide file tree
Showing 9 changed files with 276 additions and 178 deletions.
4 changes: 4 additions & 0 deletions Example/MaLiang.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
C388DE5A22550F1200CE19C6 /* pen.png in Resources */ = {isa = PBXBuildFile; fileRef = C388DE5822550F1200CE19C6 /* pen.png */; };
C388DE5B22550F1200CE19C6 /* pencil.png in Resources */ = {isa = PBXBuildFile; fileRef = C388DE5922550F1200CE19C6 /* pencil.png */; };
C3E122562255CC6800114EBD /* MLTexture.swift in Sources */ = {isa = PBXBuildFile; fileRef = B69E669C208C6117005ED0E1 /* MLTexture.swift */; };
C3E7B55122599CB8001D45EC /* Eraser.swift in Sources */ = {isa = PBXBuildFile; fileRef = C3E7B55022599CB8001D45EC /* Eraser.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand Down Expand Up @@ -67,6 +68,7 @@
C388DE562254FB6900CE19C6 /* Maths.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Maths.swift; sourceTree = "<group>"; };
C388DE5822550F1200CE19C6 /* pen.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = pen.png; sourceTree = "<group>"; };
C388DE5922550F1200CE19C6 /* pencil.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = pencil.png; sourceTree = "<group>"; };
C3E7B55022599CB8001D45EC /* Eraser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Eraser.swift; sourceTree = "<group>"; };
E4F56C2627A50ADFF8265C8F /* Pods-MaLiang_Example.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-MaLiang_Example.debug.xcconfig"; path = "Pods/Target Support Files/Pods-MaLiang_Example/Pods-MaLiang_Example.debug.xcconfig"; sourceTree = "<group>"; };
F3CBFDC0B0167759FA9BD760 /* MaLiang.podspec */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text; name = MaLiang.podspec; path = ../MaLiang.podspec; sourceTree = "<group>"; };
F4B98B2F9C80C87DAC438AFA /* LICENSE */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text; name = LICENSE; path = ../LICENSE; sourceTree = "<group>"; };
Expand Down Expand Up @@ -177,6 +179,7 @@
children = (
B69E669D208C6117005ED0E1 /* Canvas.swift */,
B69E66A2208C6117005ED0E1 /* Brush.swift */,
C3E7B55022599CB8001D45EC /* Eraser.swift */,
B6F023B1208F2A4700D5CFA7 /* Document.swift */,
B6F023B0208F1E9200D5CFA7 /* Utils */,
C388DE512254F57800CE19C6 /* MetalBase */,
Expand Down Expand Up @@ -341,6 +344,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
C3E7B55122599CB8001D45EC /* Eraser.swift in Sources */,
C3E122562255CC6800114EBD /* MLTexture.swift in Sources */,
C388DE552254F7CC00CE19C6 /* Shaders.metal in Sources */,
B604919D209A71C000372B21 /* PaintingGestureRecognizer.swift in Sources */,
Expand Down
359 changes: 208 additions & 151 deletions Example/MaLiang/Base.lproj/Main.storyboard

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions Example/MaLiang/Images.xcassets/image.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "image@2x.jpg",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 9 additions & 2 deletions Example/MaLiang/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class ViewController: UIViewController {
canvas.backgroundColor = .clear

let pen = canvas.defaultBrush!
pen.opacity = 1
pen.pointSize = 5
pen.pointStep = 1
pen.color = color
Expand All @@ -54,9 +55,15 @@ class ViewController: UIViewController {
brush.forceSensitive = 0.6
brush.color = color

// let eraser = Eraser.global
// make eraser with a texture for pencil
// let path = Bundle.main.path(forResource: "pencil", ofType: "png")!
// let texture = try? canvas.makeTexture(with: URL(fileURLWithPath: path))
// let eraser = Eraser(texture: texture, target: canvas)

brushes = [pen, pencil, brush]
/// make eraser with default round point
let eraser = Eraser(target: canvas)

brushes = [pen, pencil, brush, eraser]

brushSegement.removeAllSegments()
for i in 0 ..< brushes.count {
Expand Down
25 changes: 1 addition & 24 deletions MaLiang/Classes/Brush.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ open class Brush {
/// target to draw
weak var target: Canvas?

init(texture: MTLTexture?, target: Canvas) {
init(texture: MTLTexture? = nil, target: Canvas) {
self.texture = texture
self.target = target
self.updatePointPipeline()
Expand Down Expand Up @@ -149,26 +149,3 @@ open class Brush {
}

}

public final class Eraser: Brush {

/// only a global eraser needed
// public static let global = Eraser()

// private init() {
// super.init(texture: nil, target: nil)
// pointSize = 10
// opacity = 1
// forceSensitive = 0
// }

// color of eraser can't be changed
override public var color: UIColor {
get {
return .clear
}
set {
// set color of eraser will do nothing
}
}
}
24 changes: 24 additions & 0 deletions MaLiang/Classes/Eraser.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// Eraser.swift
// MaLiang_Example
//
// Created by Harley-xk on 2019/4/7.
// Copyright © 2019 Harley-xk. All rights reserved.
//

import Foundation
import UIKit
import Metal

open class Eraser: Brush {

open override func setupBlendOptions(for attachment: MTLRenderPipelineColorAttachmentDescriptor) {
attachment.isBlendingEnabled = true
attachment.alphaBlendOperation = .reverseSubtract
attachment.rgbBlendOperation = .reverseSubtract
attachment.sourceRGBBlendFactor = .zero
attachment.sourceAlphaBlendFactor = .one
attachment.destinationRGBBlendFactor = .one
attachment.destinationAlphaBlendFactor = .one
}
}
5 changes: 5 additions & 0 deletions MaLiang/Classes/MetalBase/MetalView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ open class MetalView: MTKView {
return try textureLoader.newTexture(data: data, options: [.SRGB : false])
}

func makeTexture(with file: URL) throws -> MTLTexture {
let data = try Data(contentsOf: file)
return try makeTexture(with: data)
}

// MARK: - Render Target

/// final render target, contents of this texture will be rendered into drawables
Expand Down
5 changes: 4 additions & 1 deletion MaLiang/Classes/MetalBase/Shaders.metal
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ fragment float4 fragment_point_func_without_texture(Point point_data [[stage_in]
{
float dist = length(pointCoord - float2(0.5));
float4 out_color = point_data.color;
out_color.a = 1.0 - smoothstep(0.3, 0.5, dist);
if (dist >= 0.5) {
out_color.a = 0;
}
// out_color.a = out_color.a * (1.0 - smoothstep(0.4, 0.5, dist));
return float4(out_color);
}

0 comments on commit d513703

Please sign in to comment.