答案 0 :(得分:5)
我在我的一个应用程序中使用了类似的效果(见下文)并使用了SceneKit着色器修改器。
这里有一些可能有帮助的示例代码。
let boxNode = SCNNode(geometry: SCNBox(width: 1.0, height: 1.0, length: 1.0, chamferRadius: 0))
boxNode.geometry?.firstMaterial?.diffuse.contents = UIColor.red
scene.rootNode.addChildNode(boxNode)
let pulseSize:CGFloat = 5.0
let pulsePlane = SCNPlane(width: pulseSize, height: pulseSize)
pulsePlane.firstMaterial?.isDoubleSided = true
pulsePlane.firstMaterial?.diffuse.contents = UIColor.blue
let pulseNode = SCNNode(geometry: pulsePlane)
let pulseShaderModifier =
"#pragma transparent; \n" +
"vec4 originalColour = _surface.diffuse; \n" +
"vec4 transformed_position = u_inverseModelTransform * u_inverseViewTransform * vec4(_surface.position, 1.0); \n" +
"vec2 xy = vec2(transformed_position.x, transformed_position.y); \n" +
"float xyLength = length(xy); \n" +
"float xyLengthNormalised = xyLength/" + String(describing: pulseSize / 2) + "; \n" +
"float speedFactor = 1.5; \n" +
"float maxDist = fmod(u_time, speedFactor) / speedFactor; \n" +
"float distbasedalpha = step(maxDist, xyLengthNormalised); \n" +
"distbasedalpha = max(distbasedalpha, maxDist); \n" +
"_surface.diffuse = mix(originalColour, vec4(0.0), distbasedalpha);"
pulsePlane.firstMaterial?.shaderModifiers = [SCNShaderModifierEntryPoint.surface:pulseShaderModifier]
boxNode.addChildNode(pulseNode)
红色框(boxNode
)仅包含在上下文中。
脉冲节点由SCNPlane
组成,片段着色器通过SceneKit的表面着色器修改器进行修改。如果您注释掉设置着色器修改器的代码,则会按预期看到平坦的蓝色方块。
为着色器修改器的曲面入口点指定的代码将注入到SceneKit使用的片段着色器中。这意味着您在带有像素的屏幕空间中工作。着色器修改器的前几行使用逆模型和视图变换将屏幕空间坐标转换回模型空间坐标。
计算xyLength
到平面中心(在模型空间中)的每个渲染像素距离。然后将其标准化为“脉冲平面”的总大小。 xyLengthNormalised
。采用模运算时间来获得脉冲效应;您可以将其切换为sin
函数以获得进入中断类型的脉冲。我们使用mod操作的结果来确定哪些像素应该是透明的。
飞机旋转红色框,因为它是盒子的子节点。您可以通过添加约束来覆盖此行为,如下所示。我已经习惯了约束,因为SCNBillboardConstraint
我从来没有取得多大成功。
let pulseNodeConstraint = SCNLookAtConstraint(target: cameraNode)
pulseNode.constraints = [pulseNodeConstraint]
最终结果
对于喜欢Objective-C的人......
SCNNode *boxNode = [SCNNode nodeWithGeometry:[SCNBox boxWithWidth:1.0 height:1.0 length:1.0 chamferRadius:0.0]];
boxNode.geometry.firstMaterial.diffuse.contents = [UIColor redColor];
[scene.rootNode addChildNode:boxNode];
CGFloat pulseSize = 5.0;
SCNPlane *pulsePlane = [SCNPlane planeWithWidth:pulseSize height:pulseSize];
[pulsePlane.firstMaterial setDoubleSided:true];
pulsePlane.firstMaterial.diffuse.contents = [UIColor blueColor];
SCNNode *pulseNode = [SCNNode nodeWithGeometry:pulsePlane];
NSString *pulseShaderModifier = [NSString stringWithFormat:
@"#pragma transparent; \n"
"vec4 originalColour = _surface.diffuse; \n"
"vec4 transformed_position = u_inverseModelTransform * u_inverseViewTransform * vec4(_surface.position, 1.0); \n"
"vec2 xy = vec2(transformed_position.x, transformed_position.y); \n"
"float xyLength = length(xy); \n"
"float xyLengthNormalised = xyLength/%f; \n"
"float speedFactor = 1.5; \n"
"float maxDist = fmod(u_time, speedFactor) / speedFactor; \n"
"float distbasedalpha = step(maxDist, xyLengthNormalised); \n"
"distbasedalpha = max(distbasedalpha, maxDist); \n"
"_surface.diffuse = mix(originalColour, vec4(0.0), distbasedalpha);", pulseSize/2.0];
NSDictionary *smdict = [NSDictionary dictionaryWithObject:pulseShaderModifier forKey:SCNShaderModifierEntryPointSurface];
[pulsePlane.firstMaterial setShaderModifiers:smdict];
[boxNode addChildNode:pulseNode];
SCNLookAtConstraint *pulseNodeConstraint = [SCNLookAtConstraint lookAtConstraintWithTarget:cameraNode];
NSArray *constraints = [NSArray arrayWithObject:pulseNodeConstraint];
[pulseNode setConstraints:constraints];
答案 1 :(得分:1)
您可以使用https://github.com/shu223/Pulsator
中的广告连播<强>配置强>
let pulsator = Pulsator()
view.layer.addSublayer(pulsator)
pulsator.start()
脉冲数
pulsator.numPulse = 3
<强>半径强>
pulsator.radius = 240.0
<强>颜色强>
pulsator.backgroundColor = UIColor(red: 1, green: 1, blue: 0, alpha: 1).CGColor
动画片段
使用以下属性
animationDuration : duration for each pulse
pulseInterval : interval between pulses
<强>缓解强>
您可以设置timingFunction
属性。
<强>重复强>
使用repeatCount
属性。
正确配置它。祝你好运。