作者:woainimamamamama | 来源:互联网 | 2023-02-09 17:01
我想在SCNSphere对象上应用脉冲效果,任何人都可以帮助我.
脉冲效应
提前致谢.
1> lock..:
我在我的一个应用程序中使用了类似的效果(见下文)并使用了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
)仅包含在上下文中.
脉冲节点由a组成,SCNPlane
片段着色器通过SceneKit的表面着色器修改器进行修改.如果您注释掉设置着色器修改器的代码,您将看到一个平滑的蓝色方块.
为着色器修改器的曲面入口点指定的代码将注入到SceneKit使用的片段着色器中.这意味着您在带有像素的屏幕空间中工作.着色器修改器的前几行使用逆模型和视图变换将屏幕空间坐标转换回模型空间坐标.
计算到平面中心(在模型空间中)的每个渲染像素距离xyLength
.然后将其标准化为"脉冲平面"的总大小xyLengthNormalised
.采用模运算时间来获得脉冲效应; 你可以把它切换到一个sin
函数来获得一个in-then-out类型的脉冲.我们使用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];