SceneKit 水就像 Badger 示例中的那样

标签 scenekit

有人知道如何创建像 Apple 的獾示例中那样的水 Material 吗?

“scene.scn”中有一个“geotherm_01”对象,该对象获得 Material “_1_terrasses_orange_water”和“_1_terrasses_eau”,它们用看起来逼真的慢速动画创建水。

我尝试在测试项目中重复相同的 Material ,但无法得到相同的结果。

最佳答案

这种水效果是使用 shader modifiers 实现的如 Xcode 中的 SceneKit 场景编辑器所示。最重要的 Material 属性是 normal map将用于影响光照,以便在完美平坦的几何体上模拟小波。

Badger

更具体地说,.surface 的修饰符入口点是

float waterSpeed = u_time * -0.1;vec2 uvs = _surface.normalTexcoord;uvs.x *= 2;vec3 tn = texture2D(u_normalTexture, vec2(uvs.x, uvs.y + waterSpeed)).xyz;tn = tn * 2 - 1;vec3 tn2 = texture2D(u_normalTexture, vec2(uvs.x + 0.35 , uvs.y + 0.35 + (waterSpeed * 1.3))).xyz;tn2 = tn2 * 2 - 1;vec3 rn = (tn + tn2) * 0.5;mat3 ts = mat3(_surface.tangent, _surface.bitangent, _surface.geometryNormal);_surface.normal = normalize(ts * rn);

这是代码的注释版本:

// Elapsed time in seconds
float waterSpeed = u_time * -0.1;

// Texture coordinates that will be used to sample the normal map
vec2 uvs = _surface.normalTexcoord;
uvs.x *= 2;

// Sample the normal map
vec3 tn = texture2D(u_normalTexture, vec2(uvs.x, uvs.y + waterSpeed)).xyz;

// The texture stores values in the [0, 1] range
// Express the coordinates of the normal vector in the [-1, +1] range
tn = tn * 2 - 1;

// Sample the normal map again, using the `waterSpeed` offset this time
// in order to produce the animation effect
vec3 tn2 = texture2D(u_normalTexture, vec2(uvs.x + 0.35 , uvs.y + 0.35 + (waterSpeed * 1.3))).xyz;
tn2 = tn2 * 2 - 1;

// Combine the two normals (static and animated) to produce a richer (more complex and less uniform) effect
vec3 rn = (tn + tn2) * 0.5;

// Normals in the normal map are expressed in tangent space
// Convert them to object space and override the surface normal to simulate wavelets
mat3 ts = mat3(_surface.tangent, _surface.bitangent, _surface.geometryNormal);
_surface.normal = normalize(ts * rn);

关于SceneKit 水就像 Badger 示例中的那样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45781235/

相关文章:

swift - scnScene.write 未将场景写入 .scn 文件

ios - 是否有可能专注于选定的 SCNNode

ios - 扩展名为 .Shader、.vsh、.fsh 的着色器有什么区别?

ios - ARKit 中的 ChromaKey 视频

ios - 如何在 SKView 之上添加 SCNView

ios - Swift SceneKit 是围绕由两个 3D 点定义的轴的胶囊内的点

swift - 将 UIKit 转换为 SceneKit

swift - 如何正确使用Scenekit的类别 mask 、碰撞 mask 和物理体、contactTestBitMask并做出适当的碰撞?

ios - 使用 SCNAction 移动 SCNLight

ios - SceneKit - 如何使用 Xcode 编辑器将粒子发射器附加到场景中的节点?