Update API

This commit is contained in:
Ali
2023-05-15 18:43:14 +04:00
parent a4175c44ca
commit 8477cf454c
34 changed files with 797 additions and 520 deletions

View File

@@ -25,11 +25,11 @@ float doubleStep(float value, float lowerBound, float upperBound) {
return step(lowerBound, value) * (1.0 - step(upperBound, value));
}
float fieldFunction(float2 center, float2 position, float2 dimensions, float time) {
float fieldFunction(float2 center, float contentScale, float2 position, float2 dimensions, float time) {
float maxDimension = max(dimensions.x, dimensions.y);
float currentDistance = time * maxDimension;
float waveWidth = 100.0f * 3.0f;
float waveWidth = 100.0f * contentScale;
float d = distance(center, position);
@@ -50,7 +50,8 @@ vertex RasterizerData rippleVertex
device const uint2 &center [[buffer(0)]],
device const uint2 &gridResolution [[buffer(1)]],
device const uint2 &resolution [[buffer(2)]],
device const float &time [[buffer(3)]]
device const float &time [[buffer(3)]],
device const float &contentScale [[buffer(4)]]
) {
uint triangleIndex = vid / 6;
uint vertexIndex = vid % 6;
@@ -69,7 +70,7 @@ vertex RasterizerData rippleVertex
);
float2 texCoord = float2(position.x, 1.0 - position.y);
float zPosition = fieldFunction(float2(center), float2(position.x * dimensions.x, (1.0 - position.y) * dimensions.y), dimensions, time);
float zPosition = fieldFunction(float2(center), contentScale, float2(position.x * dimensions.x, (1.0 - position.y) * dimensions.y), dimensions, time);
zPosition *= 0.5f;
float leftEdgeDistance = abs(position.x);
@@ -84,9 +85,6 @@ vertex RasterizerData rippleVertex
zPosition *= edgeDistance;
zPosition *= max(0.0, min(1.0, linearDecay(time, 0.7)));
if (zPosition <= 0.1) {
//zPosition = 0.0;
}
float3 camPosition = float3(0.0, 0.0f, 1.0f);
float3 camTarget = float3(0.0, 0.0, 0.0);
@@ -147,6 +145,9 @@ fragment half4 rippleFragment(
float4 rgb = float4(texture.sample(textureSampler, texCoord));
float4 out = float4(rgb.xyz, 1.0);
/*out.r = 0.0;
out.g = 0.0;
out.b = 1.0;*/
out.a = 1.0 - step(in.visibilityFraction, 0.5);

View File

@@ -4,6 +4,9 @@ import MetalKit
import simd
public final class RippleEffectView: MTKView {
private let centerLocation: CGPoint
private let completion: () -> Void
private let textureLoader: MTKTextureLoader
private let commandQueue: MTLCommandQueue
private let drawPassthroughPipelineState: MTLRenderPipelineState
@@ -11,7 +14,7 @@ public final class RippleEffectView: MTKView {
private var viewportDimensions = CGSize(width: 1, height: 1)
private var time: Float = 0.0
private var startTime: Double?
private var lastUpdateTimestamp: Double?
@@ -21,7 +24,10 @@ public final class RippleEffectView: MTKView {
}
}
public init?(test: Bool) {
public init?(centerLocation: CGPoint, completion: @escaping () -> Void) {
self.centerLocation = centerLocation
self.completion = completion
let mainBundle = Bundle(for: RippleEffectView.self)
guard let path = mainBundle.path(forResource: "FullScreenEffectViewBundle", ofType: "bundle") else {
@@ -135,12 +141,21 @@ public final class RippleEffectView: MTKView {
}
private func redraw(drawable: MTLDrawable) {
if let lastUpdateTimestamp = self.lastUpdateTimestamp {
/*if let lastUpdateTimestamp = self.lastUpdateTimestamp {
if lastUpdateTimestamp + 1.0 < CACurrentMediaTime() {
self.updateImageFromSourceView()
}
} else {
self.updateImageFromSourceView()
}*/
let relativeTime: Double
let timestamp = CACurrentMediaTime()
if let startTime = self.startTime {
relativeTime = timestamp - startTime
} else {
self.startTime = timestamp
relativeTime = 0.0
}
guard let commandBuffer = self.commandQueue.makeCommandBuffer() else {
@@ -160,20 +175,20 @@ public final class RippleEffectView: MTKView {
renderEncoder.setRenderPipelineState(self.drawPassthroughPipelineState)
let gridSize = 1000
var time = self.time.truncatingRemainder(dividingBy: 0.7)
//time = 0.6
self.time += (1.0 / 60.0) * 0.1
var time: Float = Float(min(relativeTime, 0.7))
var gridResolution = simd_uint2(UInt32(gridSize), UInt32(gridSize))
var resolution = simd_uint2(UInt32(viewportDimensions.width), UInt32(viewportDimensions.height))
var center = simd_uint2(200, 200);
var center = simd_uint2(UInt32(self.centerLocation.x * self.contentScaleFactor), UInt32(self.centerLocation.y * self.contentScaleFactor));
if let texture = self.texture {
var contentScale: Float = Float(self.contentScaleFactor)
renderEncoder.setVertexBytes(&center, length: MemoryLayout<simd_uint2>.size, index: 0)
renderEncoder.setVertexBytes(&gridResolution, length: MemoryLayout<simd_uint2>.size, index: 1)
renderEncoder.setVertexBytes(&resolution, length: MemoryLayout<simd_uint2>.size, index: 2)
renderEncoder.setVertexBytes(&time, length: MemoryLayout<Float>.size, index: 3)
renderEncoder.setVertexBytes(&contentScale, length: MemoryLayout<Float>.size, index: 4)
renderEncoder.setFragmentTexture(texture, index: 0)
@@ -184,5 +199,11 @@ public final class RippleEffectView: MTKView {
commandBuffer.present(drawable)
commandBuffer.commit()
if relativeTime >= 0.7 {
//self.startTime = nil
self.isPaused = true
self.completion()
}
}
}