如果我正确理解了这个问题:
您要求“本地”职位是正确的。可以说单股草是一条窄条,有一些高度段。
如果您希望它是模块化的、易于扩展的等,这很可能会在 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;
}