【问题标题】:How to determine the orientation of a geometry surface in Scenekit如何在 Scenekit 中确定几何表面的方向
【发布时间】:2019-05-16 16:23:09
【问题描述】:

我正在尝试编写一个着色器来为 SceneKit 中的自定义几何图形着色。我想设置颜色,使朝上的水平表面为白色,朝下的水平表面为黑色,而介于两者之间的所有其他表面都是基于其方向的灰色阴影。我使用材质的表面入口点来调用以下着色器

vec4 normal = vec4(_surface.normal, 1.0);

// Assume value is [-1, 1], scale to [0, 1]
float color = normal.z * 0.5 + 0.5;

_surface.diffuse = vec4(color, color, color, 1.0);

似乎normal.z 与相机(或视图)相关。我假设我需要转换值,以便它在另一个空间中。我尝试了多种转换(和转换组合),例如u_inverseViewTransform,但结果似乎都在视图空间中。有谁知道如何根据其表面的方向为几何图形着色?

【问题讨论】:

  • vec3 normal = mat3(u_inverseViewTransform) * _surface.normal; 应该可以完成这项工作。
  • @Rabbid76 做到了。谢谢。如果你把它写成答案,我会接受。

标签: ios glsl shader scenekit coordinate-transformation


【解决方案1】:

由于_surface.normal 是视图空间中的向量,因此您必须将向量转换为世界空间。

点从世界空间到视图空间的转换是由u_viewTransform完成的,因此逆运算(从视图空间到世界空间)可以由u_inverseViewTransform完成。

_surface.normal 是方向向量,但不是点。向量必须由法线矩阵进行变换,法线矩阵是 4*4 矩阵的左上角 3*3 矩阵的转置逆矩阵:

transpose(inverse(mat3(u_viewTransform)))

Why is the transposed inverse of the model view matrix used to transform the normal vectors?Why transforming normals with the transpose of the inverse of the modelview matrix?

但是由于u_viewTransformu_inverseViewTransformOrthogonal matrices的转置矩阵,逆矩阵可以跳过,因为逆矩阵和转置矩阵是相等的。见In which cases is the inverse matrix equal to the transpose?

这跟随你必须通过mat3(u_inverseViewTransform)进行转换:

vec3 normal = mat3(u_inverseViewTransform) * _surface.normal;

float color = normal.z * 0.5 + 0.5;

_surface.diffuse = vec4(color, color, color, 1.0);

【讨论】:

    猜你喜欢
    • 2017-05-14
    • 2017-07-03
    • 2021-01-26
    • 2017-04-21
    • 2016-02-02
    • 2016-11-25
    • 2018-09-10
    • 2015-09-26
    • 1970-01-01
    相关资源
    最近更新 更多