Gradient Stop Locations at Variable Values SwiftUI - xcode

I'm trying to make a gradient composed of 4 stops in SwiftUI. I'd like the middle two to be anchored to the bottom of a specific stack. Something like this:
Imgur Link of what I'm trying to do
Here's the gradient code I currently have:
LinearGradient(gradient: Gradient(stops: [
//top Gradient.Stop(color: Color(hue: 0.0, saturation: 0.0, brightness: 0.0, opacity: 0.0), location: 0.0),
//variable1 Gradient.Stop(color: Color(hue: 0.0, saturation: 0.0, brightness: 0.0, opacity: 0.3), location: 0.30 //<-Variable),
//variable2 Gradient.Stop(color: Color(hue: 0.0, saturation: 0.0, brightness: 0.0, opacity: 1.0), location: 0.35 //<-Variable),
//bottom Gradient.Stop(color: Color(hue: 0.1, saturation: 1.0, brightness: 0.0, opacity: 1.0), location: 1.0)),
startPoint: UnitPoint.top, endPoint: UnitPoint.bottom)

You can simply use the frame size of the "Content Box" to determine at what position to place the gradient stop. I did this by just having a variable called frameHeight.
import SwiftUI
struct ContentView: View {
let screenWidth = UIScreen.main.bounds.width
let screenHeight = UIScreen.main.bounds.height
var frameHeight = 200
var body: some View {
let gradient = Gradient(stops: [Gradient.Stop(color: .blue, location: 0.0),
Gradient.Stop(color: .white, location: CGFloat(frameHeight)/screenHeight),
Gradient.Stop(color: .black, location: CGFloat(frameHeight)/screenHeight + 0.05),
Gradient.Stop(color: .black, location: 1.0)])
VStack{
//Content Box view of frameHeight
VStack {
Text("Content Box")
}.frame(width: screenWidth, height: CGFloat(frameHeight))
Spacer()
}.frame(width: screenWidth, height: screenHeight)
.background(LinearGradient(gradient: gradient, startPoint: UnitPoint.top, endPoint: UnitPoint.bottom))
.edgesIgnoringSafeArea(.all)
}
}

Related

'vertexColors' parameter is undefined?

const material = new THREE.LineBasicMaterial({
vertexColors: THREE.VertexColors,
transparent: true,
side: THREE.DoubleSide,
})
why?
.vertexColors can either be true or false, nothing else. THREE.VertexColors does not exist, so it comes out as undefined.
https://threejs.org/docs/index.html#api/en/materials/Material.vertexColors
Parameters in Material vertexColors are boolean, true or false.
so if you have specified point color in float array then it can define boolean value (true or false), I will give a little example like this, and this is what I did it worked.
const myGeo = new THREE.BufferGeometry();
let vertices = new Float32Array([
-1.0, -1.0, 1.0, // 0
1.0, 1.0, 1.0, // 1
-1.0, 1.0, 1.0, // 2
1.0, -1.0, 1.0, // 3
-1.0, -1.0, -1.0, // 4
1.0, 1.0, -1.0, // 5
-1.0, 1.0, -1.0, // 6
1.0, -1.0, -1.0, // 7
]);
let colors = new Float32Array([
1.0, 0.0, 0.0, // Vertex 0 (Red)
1.0, 0.0, 0.0, // Vertex 1 (Red)
1.0, 1.0, 0.0, // Vertex 2 (Yellow)
1.0, 1.0, 0.0, // Vertex 3 (Yellow)
0.0, 1.0, 0.0, // Vertex 4 (Green)
0.0, 1.0, 0.0, // Vertex 5 (Green)
0.0, 0.0, 1.0, // Vertex 6 (Blue)
0.0, 0.0, 1.0, // Vertex 7 (Blue)
]);
myGeo.setAttribute('position', new THREE.BufferAttribute( vertices, 3 ));
myGeo.setAttribute('color', new THREE.BufferAttribute( colors, 3 ));
myGeo.setIndex([.....]);
const material= new THREE.MeshBasicMaterial({ vertexColors:true, side: THREE.DoubleSide, });
let mesh = new THREE.Mesh(myGeo, material)
scene.add(mesh);

Swift UI Update a view by doing something in another view

In my code, I have a value which counts calls from Navigation Links. This works fine but I want that if this count is over 1, the ContentView updates and add a cell. At the moment, I have to restart the app and then I have the new cell, but I want, that it updates directly. Is this possible? This is my code:
struct Website : Identifiable, Hashable, Comparable{
static func < (lhs: Website, rhs: Website) -> Bool {
lhs.name < rhs.name
}
var id = UUID()
var url : String
var name : String
var image : String
var calls : Int
}
struct websiteCallsCell : View{
var website : Website
#State var press = false
#State var tap = false
#State var isPresented = false
var body: some View{
NavigationLink(destination: WebView(website: website, url: URL(string: website.url)), label: {
Text(website.name)
.font(.system(size: 20, weight: .semibold, design: .rounded))
.frame(width: UIScreen.main.bounds.width*0.85, height: 60)
.background(
ZStack {
Color(press ? colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0) : colorLiteral(red: 0.7608050108, green: 0.8164883852, blue: 0.9259157777, alpha: 1))
RoundedRectangle(cornerRadius: 16, style: .continuous)
.foregroundColor(Color(press ? colorLiteral(red: 0.7608050108, green: 0.8164883852, blue: 0.9259157777, alpha: 1) : colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)))
.blur(radius: 4)
.offset(x: -8, y: -8)
RoundedRectangle(cornerRadius: 16, style: .continuous)
.fill(
LinearGradient(gradient: Gradient(colors: [Color( colorLiteral(red: 0.9019607843, green: 0.9294117647, blue: 0.9882352941, alpha: 1)), Color.white]), startPoint: .topLeading, endPoint: .bottomTrailing)
)
.padding(2)
.blur(radius: 2)
}
)
.clipShape(RoundedRectangle(cornerRadius: 16, style: .continuous))
.overlay(
HStack {
AnimatedImage(url: URL(string: website.image))
.font(.system(size: 24, weight: .light))
.foregroundColor(Color.white.opacity(press ? 0 : 1))
.frame(width: press ? 220 : 54, height: press ? 4 : 50)
.background(Color( colorLiteral(red: 0.3647058904, green: 0.06666667014, blue: 0.9686274529, alpha: 1)))
.clipShape(RoundedRectangle(cornerRadius: 16, style: .continuous))
.shadow(color: Color( colorLiteral(red: 0.3647058904, green: 0.06666667014, blue: 0.9686274529, alpha: 1)).opacity(0.3), radius: 10, x: 10, y: 10)
.offset(x: press ? 70 : -10, y: press ? 16 : 0)
Spacer()
}
)
.shadow(color: Color(press ? colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0) : colorLiteral(red: 0.7608050108, green: 0.8164883852, blue: 0.9259157777, alpha: 1)), radius: 20, x: 20, y: 20)
.shadow(color: Color(press ? colorLiteral(red: 0.7608050108, green: 0.8164883852, blue: 0.9259157777, alpha: 1) : colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)), radius: 20, x: -20, y: -20)
.scaleEffect(tap ? 1.2 : 1)
})
}
}
List{
ForEach(websites){ website in
if UserDefaults.standard.integer(forKey: "calls\(website.name)") > 0 && (UserDefaults.standard.integer(forKey: "lastCallsCount") < 8) {
VStack{
websiteCallsCell(website: website)
lineSpacing(20)
Spacer(minLength: 5)
}
}
}
}
You are checking on UserDefaults , Which is not a publisher
I have two solutions fir that :
1- use #Environmentobject with a #Published var for clicks count , then check on it , in your ContentView
2- pass the State variable to the navigation link , and in the navigation link destination declare #Binding variable , and change it in each press , then check on it in your ContentView
struct websiteCallsCell : View {
var website : Website
#Binding var pressCount : Int
// other code
}
//
struct ContentView : View {
#State var pressCount = 0
}
//
List{
ForEach(websites){ website in
if pressCount > 0 && pressCount< 8 {
VStack{
websiteCallsCell(website: website, pressCount: self.$pressCount)
lineSpacing(20)
Spacer(minLength: 5)
}
}
}
}

An image into three.js scene is blurred on some screen resolutions

I try to create some object that a user could able by mouse rotate use Three.js and webgl. It's displaying fine when a screen has a full HD resolution (1960x1080) but when a user screen had resolution roughly 1366x768 object is ugly (blurred). Most notably text on the image.
Originally image has the resolution 1024x748 and the scene has a size roughly 530x270.
Clear - https://ibb.co/0s7W2hk
Blurred - https://ibb.co/tzCkq6W (in some cases blur corruption have more influence)
I connect the model to the scene by next code
// ...
scene.add({
genotype: VOLUMETRIC_OBJECT,
phenotype: MODELLED_MESH,
data: {
model: 'assets/FBX 2013/model.FBX',
position: {
x: 0,
y: isMobile ? 0.05 : 0.375,
z: 0,
},
scale: isMobile ? {
x: 70e-3,
y: 70e-3,
z: 70e-3,
} : {
x: 75e-3,
y: 75e-3,
z: 75e-3,
},
name: 'model',
material: {
hb_03_label1Model: {
map: 'assets/mm1.jpg',
alphaMap: 'assets/mam1.jpg',
color: 0xffffff,
specular: new Color(0xffffff),
shininess: 5,
transparent: true,
opacity: 1,
needsUpdate: true,
},
body_hb_03Model: {
normalMap: 'assets/mnm2.jpg',
color: 0xaaaaaa,
specular: new Color(0xFFFFFF),
opacity: 0.35,
shininess: 100,
transparent: true,
needsUpdate: true,
normalScale: {
x: 1,
y: 1,
},
},
hb_03_leqidoModel: {
type: MATERIAL__SHADER,
map: 'assets/mm3.jpg',
thicknessMap: 'assets/mtm3.jpg',
specularMap: 'assets/msm3.jpg',
normalMap: 'assets/mnm3.jpg',
repeat: [10, 10],
color: 0xffffff,
refractionRatio: 0.985,
reflectivity: 0.15,
diffuse: new Vector3(1, 0.3, 0.3),
thicknessColor: new Vector3(0.11, 0.11, 0.11),
needsUpdate: true,
uniformsNeedUpdate: true,
},
hb_03_cappaModel: {
color: 0x00AD16,
needsUpdate: true,
},
},
},
},
(model) => {
// ...
},
)
Update 1:
In my case when I change mag and min filters to Linear, I have strong sharpness and text not readable, again.
I think, for the best result, I need to use mag filter as Linear and min filter as LinearMipMapLinearFilter with the generated by myself mipmap.
But, currently, generate custom mipmap so strong for me. I did increase anisotropy for texture, and text is displayed acceptable

UIButton animation title position changed unexpected

UIButton animation title position changed suddenly (jumped to the left), I want to keep the title in the button center nearby always.
You can see the button title ("Login In") jumped to the left unexpected, which should be optimized.
Here is the code:
UIView.animate(withDuration: 1.5, delay: 0.0, usingSpringWithDamping: 0.2, initialSpringVelocity: 0.0, options: [], animations: {
self.loginButton.bounds.size.width -= 80.0
}){ (yes) in }
UIView.animate(withDuration: 0.33, delay: 0.0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.0, options: [], animations: {
self.loginButton.center.y -= 60.0
self.loginButton.backgroundColor = UIColor(red: 0.63, green: 0.84, blue: 0.35, alpha: 1.0)
self.spinner.center = CGPoint(x: -20, y: -16)
self.spinner.alpha = 0.0
}){ (yes) in }
Here is the code I tried , which does not work too.
UIView.animate(withDuration: 0.3, delay: 0.2, options: [], animations: {
self.loginButton.backgroundColor = UIColor(red: 0.63, green: 0.84, blue: 0.35, alpha: 1.0)
self.loginButton.bounds.size.width -= 80.0
self.loginButton.center.y -= 60.0
self.spinner.frame.origin = CGPoint(x: -20, y: 16)
self.spinner.alpha = 0.0
}) { (isOK) in }
PS:
I add the login button through storyboard , as the following image
And the code relevant is here
Add line in animation function
UIView.animate(withDuration: 1, delay: 0.2, options: [], animations: {
//Your code for animation
.
.
.
//Add this line
self.view.layoutIfNeeded()
}) { (isOK) in }
Note
Use Auto Layout for all device support and easy to animation

WebGL/GLSL vertex shader collapses 9 points into single point when I set x coord of gl_Position to zero

I do not understand why the following two shaders produce different results when I render a vertex buffer with x coordinates equal to zero:
First:
attribute vec3 position;
void main() {
gl_Position = vec4(position.x, position.y, 0.0, 1.0);
}
Second:
attribute vec3 position;
void main() {
gl_Position = vec4(0.0, position.y, 0.0, 1.0);
}
The result of the first is a line of nine dots. The result of the second is a single dot.
I'm drawing the following vertex array as GL_POINTS:
0.0, -1.00, 0.0,
0.0, -0.75, 0.0,
0.0, -0.50, 0.0,
0.0, -0.25, 0.0,
0.0, 0.00, 0.0,
0.0, 0.25, 0.0,
0.0, 0.50, 0.0,
0.0, 0.75, 0.0,
0.0, 1.00, 0.0
Here's the VBO preparation calls:
var a = new Float32Array([
0.0, -1.00, 0.0,
0.0, -0.75, 0.0,
0.0, -0.50, 0.0,
0.0, -0.25, 0.0,
0.0, 0.00, 0.0,
0.0, 0.25, 0.0,
0.0, 0.50, 0.0,
0.0, 0.75, 0.0,
0.0, 1.00, 0.0
]);
gl.bindBuffer(gl.ARRAY_BUFFER, b);
gl.bufferData(gl.ARRAY_BUFFER, a.byteLength, gl.STATIC_DRAW);
gl.bufferSubData(gl.ARRAY_BUFFER, 0, a);
Here's the draw calls:
gl.bindBuffer(gl.ARRAY_BUFFER, b);
gl.vertexAttribPointer(p.position, 3, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(p.position);
gl.drawArrays(gl.POINTS, 0, 9);
That is a known problem (sorry, no link for now).
Basically, GLSL compiler thinks that attribute 'position' is not used, because the first component of it is not used. To check this try the following test:
gl_Position = vec4(0.0, position.y, 0.0, 1.0) + position.x*0.001;
Once you confirm the bug, you can either update you video driver or use an obvious workaround.

Resources