【发布时间】:2021-01-18 11:57:46
【问题描述】:
我的问题与几年前回答的 Q 有关,我正在努力: three.js texture across InstanceGeometry
我正在将我的应用程序从 r95 更新到最新的 r124。
首先我创建了 InstancedBufferGeometry(Geometry.createBuffered 是我自己的位,它可以从一些 JSON 中弹出任何类型的几何):
let bg = Geometry.createBuffered(id, params.instance.geo),
ig : InstancedBufferGeometry = new InstancedBufferGeometry(),
mesh;
ig.copy( bg );
然后我开始从 JSON 参数生成 InstancedBufferAttributes 并将它们分配给 IBG:
for(var x = 0; x < xl; x++){
for ( var y = 0; y < yl; y++) {
for ( var z = 0; z < zl; z++) {
ox = ( x * params.instance.arrayPlacement.spacing[0] - disX ) + Math.random() * ( params.instance.arrayPlacement.displacement[1] - params.instance.arrayPlacement.displacement[0] ) + params.instance.arrayPlacement.displacement[0];
oy = ( y * params.instance.arrayPlacement.spacing[1] - disY ) + Math.random() * ( params.instance.arrayPlacement.displacement[1] - params.instance.arrayPlacement.displacement[0] ) + params.instance.arrayPlacement.displacement[0];
oz = ( z * params.instance.arrayPlacement.spacing[2] - disZ ) + Math.random() * ( params.instance.arrayPlacement.displacement[1] - params.instance.arrayPlacement.displacement[0] ) + params.instance.arrayPlacement.displacement[0];
offsets[ count * 3 ] = ox;
offsets[ ( count * 3 ) + 1 ] = oy;
offsets[ ( count * 3 ) + 2 ] = oz;
scales[count] = Math.random() * (params.instance.arrayPlacement.scale[1] - params.instance.arrayPlacement.scale[0]) + params.instance.arrayPlacement.scale[0];
orientations[ count * 4 ] = -0.5;
orientations[ ( count * 4 ) + 1 ] = -0.5;
orientations[ ( count * 4 ) + 2 ] = -0.5;
orientations[ ( count * 4 ) + 3 ] = 0.5;
uvOffsets[ count * 2 ] = y;
uvOffsets[ ( count * 2 ) + 1 ] = x;
iCount[ count * 2 ] = 1/yl;
iCount[ ( count * 2 ) + 1 ] = 1/xl;
count++;
}
}
}
let roti : InstancedBufferAttribute = new InstancedBufferAttribute( orientations, 4).setUsage( DynamicDrawUsage ),
offs : InstancedBufferAttribute = new InstancedBufferAttribute( offsets, 3 ).setUsage( DynamicDrawUsage ),
uvOffs : InstancedBufferAttribute = new InstancedBufferAttribute( uvOffsets, 2 ).setUsage( DynamicDrawUsage ),
iC : InstancedBufferAttribute = new InstancedBufferAttribute( iCount, 2 ).setUsage ( DynamicDrawUsage ),
scas : InstancedBufferAttribute = new InstancedBufferAttribute( scales, 1 ).setUsage( DynamicDrawUsage );
ig.setAttribute( 'offset', offs );
ig.setAttribute( 'uvOffset', uvOffs);
ig.setAttribute('iCount', iC);
ig.setAttribute( 'iScale', scas );
ig.setAttribute( 'orientation', roti );
然后,我使用带有 uvoffsets 和颜色的自定义着色器制作材质。但是,当我使用基本的 3js 材质创建网格时,什么也没有出现。如果我检查场景,网格就在那里,没有错误screenshot
更新: @Mugen87 是正确的,我仍在运行 r115。对于那个很抱歉。我现在在 r124 上,但奇怪的是,当我创建 IBG 时,我仍然看到 maxInstancedCount 以及 instanceCount(未定义)。此外,当我使用 IBG.copy(BoxBufferGeometry) 时,几何似乎没有被复制。我假设 BoxBufferGeometry 创建了一个 BufferGeometry?还是在 r125 中推出?
【问题讨论】:
-
maxInstancedCount在您的照片上未定义。您需要使用实例数量进行设置。只需ig.maxInstancedCount = _some_integer_;,在您设置属性之后。
标签: three.js