SceneKit如何实现类似Badger示例中的水效果?

5

有人知道如何创建类似于苹果示例中的水材质吗?

"scene.scn" 中有一个名为 "geotherm_01" 的对象,该对象具有材质 "_1_terrasses_orange_water" 和 "_1_terrasses_eau",可以创建出缓慢动画看起来逼真的水。

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

1个回答

11
使用在Xcode SceneKit场景编辑器中显示的着色器修改器来实现这种水效果。最重要的材质属性是法线贴图,它将用于影响光照以模拟完全平坦几何体上的波纹。

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);

1
谢谢你,mnuages。 - Manifestor

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接