【问题标题】:Three.js Get local position of vertex in shader, is that even what I need?Three.js 在着色器中获取顶点的本地位置,这甚至是我需要的吗?
【发布时间】:2018-04-21 00:16:05
【问题描述】:

我正在尝试将这种将草渲染到我的 three.js 应用程序中的技术。

http://davideprati.com/demo/grass/

在 y 位置 0 的水平地形上,一切看起来都非常棒!

问题是,我的应用(游戏)的地形由高度图修改,因此该地形上的很少(如果有)位置位于 y 位置 0。

似乎这个顶点着色器动画代码假定草对象位于 y 位置 0 以使以下顶点着色器代码按预期工作:

    if (pos.y > 1.0) {
        float noised = noise(pos.xy);
        pos.y += sin(globalTime * magnitude * noised);
        pos.z += sin(globalTime * magnitude * noised);
        if (pos.y > 1.7){
            pos.x += sin(globalTime * noised);
        }
    }

此条件在地形平坦且位置为 0 的假设下起作用,因此只有地面上方的顶点会产生动画。嗯.. 嗯.. 由于所有顶点都高于 1 且带有高度图(大多数情况下),因此会出现一些奇怪的效果,例如草在整个地方滑动哈哈。

有没有办法做到这一点,我可以根据精灵而不是世界位置来指定 y 位置阈值?或者有没有更好的方法来处理这个“滑动”问题?

在着色器代码方面我是一个极端的菜鸟 =]

任何帮助将不胜感激。

我不知道自己在做什么。

编辑*好的,我认为问题在于我正在根据其所在地形的 y 位置更改合并到主草容器几何图形中的每个网格的 y 位置。我猜着色器正在查看本地位置,但由于几何体本身垂直移位,着色器不知道如何补偿。嗯……

好的,我做了一个小提琴来演示这个问题: https://jsfiddle.net/titansoftime/a3xr8yp7/

将第 128 行的值更改为 1 而不是 2,一切看起来都很好。不知道如何解决这个问题。

另外,我不知道颜色为什么会这样,它们在我的应用中看起来不错。

【问题讨论】:

  • 我不完全确定我是否理解了这个问题,但是换行 128 并没有真正产生任何明显的差异。
  • 我想我在重新阅读并再次查看小提琴后了解更多。此逻辑在应用于草叶的着色器中。你不希望这个几何图形被动画化,但条件是只有顶部移动而底部是固定的?
  • 是的,很抱歉我很难描述这一点,因为我不确定问题到底是什么,不过我现在开始明白了 =]

标签: three.js glsl


【解决方案1】:

如果我正确理解了这个问题:

您要求“本地”职位是正确的。可以说单股草是一条窄条,有一些高度段。

如果您希望它是模块化的、易于扩展的等,这很可能会在 0-1 范围内的某个方向上扩展。假设它沿该方向有四个线段,这将产生坐标为 [0.0, 0.333, 0.666, 1.0] 的顶点。它比任意范围更有意义,因为很容易推断 0 是地面,1 是刀片的尖端。

这是“本地”或模型空间。当您将此与modelMatrix 相乘时,您会将其转换为世界空间(称为localToWorld)。

在着色器中它可能看起来像这样

void main(){
   vec4 localPosition = vec4( position, 1.);
   vec4 worldPosition = modelMatrix * localPosition;
   vec4 viewPosition = viewMatrix * worldPosition;
   vec4 projectedPosition = projectionMatrix * viewPosition; //either orthographic or perspective

   gl_Position = projectedPosition;
}

这是您转换的经典“您有一个场景图节点”。根据您为网格设置的position、rotation 和scale vec4 worldPosition 会有所不同,但本地位置始终相同。您无法仅从该值判断某物是底部还是顶部,任何值都是可行的,因为您的地形可以是任何东西。

使用这种方法,您可以编写一个着色器和逻辑,说明如果顶点的高度为 0(或小于某个 epsilon),则不要进行动画处理。

所以这给我们带来了一些逻辑,它适用于一些假设的空间(你有一个适用于 1.0 和 1.7 的规则)。

因为您正在平移和合并几何图形,所以您不再拥有这个用户友好的空间,即模型空间。现在这些刀片很可能会跳过local2world 转换(它很可能最终只是一个单位矩阵)。

这显然会打乱您选择顶点的逻辑。

如果你必须采取这样分发它们的方法,那么你需要另一个渠道来承载那个本地空间的意义,即使你只将它用于那个动画。

已经存在两个合适的通道 - UV 和顶点颜色。您可以将 Uv 想象为在另一个空间中具有另一个平面网格,该网格映射到您正在渲染的网格。但在这种特殊情况下,您似乎可以使用自定义属性 aBladeHeight,例如可以是浮点数。

void main(){
     vec4 worldPosition = vec4(position, 1.); //you "burnt/baked" this transformation in, so no need to go from local to world in the shader

     vec2 localPosition = uv; //grass in 2d, not transformed to your terrain
     //this check knows whats on the bottom of the grass
     //rather than whats on the ground (has no idea where the ground is)
     if(localPosition.y){
        //since local does not exist, the only space we work in is world
        //we apply the transformation in that space, but the filter 
        //is the check above, in uv space, where we know whats the bottom, whats the top
        worldPosition.xy += myLogic();
     } 
     gl_Position = projectionMatrix * viewMatrix * worldPosition;
}

模仿“局部空间”

void main(){
   vec4 localSpace = vec4(uv,0.,1.);
   gl_Position = projectionMatrix * modelViewMatrix * localSpace;
}

所有的刀片都会相互重叠。

编辑

通过实例化着色器看起来像这样:

attribute vec4 aInstanceMatrix0; //16 floats to encode a matrix4
attribute vec4 aInstanceMatrix1;
attribute vec4 aInstanceMatrix2;
//attribute vec4 aInstanceMatrix3; //but one you know will be 0,0,0,1 so you can pack in the first 3


void main(){
  vec4 localPos = vec4(position, 1.); //the local position is intact, its the normalized 0-1 blade

  //do your thing in local space
  if(localPos.y > foo){
     localPos.xz += myLogic();
  }

  //notice the difference, instead of using the modelMatrix, you use the instance attributes in it's place
  mat4 localToWorld = mat4(
    aInstanceMatrix0,
    aInstanceMatrix1,
    aInstanceMatrix2,
    //aInstanceMatrix3
    0. , 0. , 0. , 1. //this is actually wrong i think, it should be the last column not row, but for illustrative purposes, 
  ); 

  //to pack it more effeciently the rows would look like this
  // xyz w
  // xyz w
  // xyz w  
  // 000 1
  // off the top of my head i dont know what the correct code is
  mat4 foo = mat4( 
    aInstanceMatrix0.xyz, 0.,
    aInstanceMatrix1.xyz, 0.,
    aInstanceMatrix2.xyz, 0.,
    aInstanceMatrix0.w, aInstanceMatrix1.w, aInstanceMatrix2.w, 1.
)

  //you can still use the modelMatrix with this if you want to move the ENTIRE hill with all the grass with .position.set()
  vec4 worldPos = localToWorld * localPos; 
  gl_Position = projectionMatrix * viewMatrix * worldPos;
}

【讨论】:

  • 将偏移高度作为单独的属性传递的想法似乎是要走的路。谢谢先生的详细解释。我想我至少了解如何开始解决这个问题。我真的可以合并缓冲几何,就像我可以使用网格平移规则几何一样 =[
  • 您可以使用实例化。这或多或少像一个粒子系统,每个刀片都可以有自己的位置(你当前烧入几何体的东西)。相同的想法,除了您必须使用属性position 来获取根/尖端(0-1)并对其进行转换。我会编辑答案。
  • 感谢论坛上的帮助,我确实弄清楚了如何仅使用 buffegeometry,一切运行良好。实例化似乎很棒,但不完全了解如何设置它。
  • 如果您已经在使用着色器,那真的没那么难。如果您尝试将此表述为一个问题,询问如何针对您的情况准确地做到这一点,我想我可以写一个简单的答案来解释这个概念。这里有点跑题了。
  • 确实如此。实际上,我现在正在研究您的 THREE.InstancedMesh 代码。会玩那个。我确定我会有问题嘿嘿。
猜你喜欢
  • 2013-07-13
  • 2016-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多