大佬教程收集整理的这篇文章主要介绍了ios – Swift SceneKit照明和影响发射纹理,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
override func viewDidLoad() { super.viewDidLoad() let scene = SCNScene() let earth = SCNSphere(radius: 1) let earthNode = SCNNode() let earthMaterial = SCNMaterial() earthMaterial.diffuse.contents = UIImage(named: "earth.jpg") earthMaterial.emission.contents = UIImage(named: "earthEmission.jpg") earth.materials = [earthMaterial] earthNode.geometry = earth scene.rootNode.addChildNode(earthNodE) let lightNode = SCNNode() lightNode.light = SCNLight() lightNode.light?.type = .omni lightNode.position = SCNVector3(x: 0,y: 10,z: 5) scene.rootNode.addChildNode(lightNodE) sceneView.scene = scene }
您可以看到最终结果here的镜头.
片段着色器修改器
我们可以使用_lighTingContribution.diffuse(表示应用于漫反射的光的RGB(vec3)颜色)来确定被照亮的对象(在本例中为地球)的区域,然后使用它来掩盖fragment shader modifier中的发射纹理.
你使用它的方式取决于你.这是我提出的最简单的解决方案(使用GLSL语法,但如果你使用它将在运行时自动转换为Metal)
uniform sampler2D emissiontexture; vec3 light = _lighTingContribution.diffuse; float lum = max(0.0,1 - (0.2126*light.r + 0.7152*light.g + 0.0722*light.b)); // 1 vec4 emission = texture2D(emissiontexture,_surface.diffuseTexcoord) * lum; // 2,3 _output.color += emission; // 4
>计算_lighTingContribution.diffuse颜色的亮度(使用公式from here)(如果灯光不是纯白色)
>从@L_944_0@中减去它以获得“黑暗面”的亮度
>使用漫反射UV坐标从自定义纹理获取发射(授予发射和漫反射纹理具有相同的纹理)并通过乘法将亮度应用于它
>将其添加到最终输出颜色(与应用常规发射的方式相同)
这就是着色器部分,现在让我们通过Swift方面的东西.
Swift设置
首先,我们不会使用材质的emission.contents属性,而是需要创建自定义SCNMaterialProperty
let emissiontexture = UIImage(named: "earthEmission.jpg")! let emission = SCNMaterialProperty(contents: emissiontexturE)
并使用SETVALue(_:forKey :)将其设置为材质
earthMaterial.SETVALue(emission,forKey: "emissiontexture")
密切关注键 – 它应该与着色器修改器中的制服相同.此外,您不需要自己保留材质属性,SETVALue会创建@L_944_0@强引用.
let shaderModifier = """ uniform sampler2D emissiontexture; vec3 light = _lighTingContribution.diffuse; float lum = max(0.0,1 - (0.2126*light.r + 0.7152*light.g + 0.0722*light.b)); vec4 emission = texture2D(emissiontexture,_surface.diffuseTexcoord) * lum; _output.color += emission; """ earthMaterial.shaderModifiers = [.fragment: shaderModifier]
请注意,光源必须非常明亮,否则会在“地球”周围看到昏暗的灯光.我必须在你的设置中将lightNode.light?.intensity设置为至少2000才能按预期工作.您可能希望尝试计算发光度并应用于发射以获得更好结果的方式.
如果您可能需要它,_lighTingContribution是片段着色器修饰符中可用的结构,它还具有环境和镜面成员(下面是Metal语法):
struct SCNShaderLighTingContribution { float3 ambient; float3 diffuse; float3 specular; } _lighTingContribution;
以上是大佬教程为你收集整理的ios – Swift SceneKit照明和影响发射纹理全部内容,希望文章能够帮你解决ios – Swift SceneKit照明和影响发射纹理所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。