在 ForgeViewer 中使切面与自定义网格一起使用
Make cutplanes work with custom Meshes in ForgeViewer
我正在使用 Autodesk 的 ForgeViewer 加载 IFC 文件和自定义 THREE.js 网格,类似于所做的 here
我遇到的问题是切面 (see e.g. here) 不影响自定义网格,只影响 Forge 模型。有什么方法可以使切面也适用于自定义网格吗?
如果我没记错的话,THREE.js 设置 clippingPlanes
的方式在 r71 版本中还没有引入(Forge 的自定义实现基于该版本),但也许有一个制定具体的方法来完成这项工作?
没错,Forge Viewer 使用自己的基于着色器的裁剪。查看我的其他 (especially this gist) 了解更多详情。
扩展 Petr 的回答,这里是支持纹理网格剖切面所需的代码:
const imgTexture = THREE_FORGE.ImageUtils.loadTexture(textureUrl);
const vertexShader = `
#if NUM_CUTPLANES > 0
varying vec3 vWorldPosition;
#endif
varying vec2 vUv;
void main() {
#if NUM_CUTPLANES > 0
vec4 _worldPosition = modelMatrix * vec4( position, 1.0 );
vWorldPosition = _worldPosition.xyz;
#endif
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.0);
}
`;
const fragmentShader = `
#include<cutplanes>
#if NUM_CUTPLANES > 0
varying highp vec3 vWorldPosition;
#endif
uniform sampler2D texture;
varying vec2 vUv;
void main() {
#if NUM_CUTPLANES > 0
checkCutPlanes(vWorldPosition);
#endif
gl_FragColor = texture2D(texture, vUv);
}
`;
const material = new THREE_FORGE.ShaderMaterial({
uniforms: {
cutplanes: {type: 'v4v', value: []},
hatchParams: {type: 'v2', value: new THREE_FORGE.Vector2(1.0, 10.0)},
hatchTintColor: {type: 'c', value: new THREE_FORGE.Color(0xFFFFFF)},
hatchTintIntensity: {type: 'f', value: 1.0},
texture: {type: 't', value: imgTexture},
},
vertexShader: vertexShader,
fragmentShader: fragmentShader,
});
const mesh = new THREE.Mesh(geometry, material);
我正在使用 Autodesk 的 ForgeViewer 加载 IFC 文件和自定义 THREE.js 网格,类似于所做的 here
我遇到的问题是切面 (see e.g. here) 不影响自定义网格,只影响 Forge 模型。有什么方法可以使切面也适用于自定义网格吗?
如果我没记错的话,THREE.js 设置 clippingPlanes
的方式在 r71 版本中还没有引入(Forge 的自定义实现基于该版本),但也许有一个制定具体的方法来完成这项工作?
没错,Forge Viewer 使用自己的基于着色器的裁剪。查看我的其他
扩展 Petr 的回答,这里是支持纹理网格剖切面所需的代码:
const imgTexture = THREE_FORGE.ImageUtils.loadTexture(textureUrl);
const vertexShader = `
#if NUM_CUTPLANES > 0
varying vec3 vWorldPosition;
#endif
varying vec2 vUv;
void main() {
#if NUM_CUTPLANES > 0
vec4 _worldPosition = modelMatrix * vec4( position, 1.0 );
vWorldPosition = _worldPosition.xyz;
#endif
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.0);
}
`;
const fragmentShader = `
#include<cutplanes>
#if NUM_CUTPLANES > 0
varying highp vec3 vWorldPosition;
#endif
uniform sampler2D texture;
varying vec2 vUv;
void main() {
#if NUM_CUTPLANES > 0
checkCutPlanes(vWorldPosition);
#endif
gl_FragColor = texture2D(texture, vUv);
}
`;
const material = new THREE_FORGE.ShaderMaterial({
uniforms: {
cutplanes: {type: 'v4v', value: []},
hatchParams: {type: 'v2', value: new THREE_FORGE.Vector2(1.0, 10.0)},
hatchTintColor: {type: 'c', value: new THREE_FORGE.Color(0xFFFFFF)},
hatchTintIntensity: {type: 'f', value: 1.0},
texture: {type: 't', value: imgTexture},
},
vertexShader: vertexShader,
fragmentShader: fragmentShader,
});
const mesh = new THREE.Mesh(geometry, material);