Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Three.js instancedBuffergeometry与spritesheet不匹配';我不能从三个JS v117中出现_Three.js - Fatal编程技术网

Three.js instancedBuffergeometry与spritesheet不匹配';我不能从三个JS v117中出现

Three.js instancedBuffergeometry与spritesheet不匹配';我不能从三个JS v117中出现,three.js,Three.js,我正在使用InstancedBufferGeometry绘制许多.gltf对象,并使用spritesheet应用多种纹理 为了使用spritesheet,我使用了基于MeshLambertMaterial的材质,使用了onbeforecomile函数,在ThreeJSV116之前,它工作得非常好 但是在将threejs和GLTFLoader升级到v117之后,什么也不显示 我在编译前实现onbeforecomile如下: 类InstancedLambertMaterial扩展了3.MeshLam

我正在使用InstancedBufferGeometry绘制许多
.gltf
对象,并使用spritesheet应用多种纹理

为了使用spritesheet,我使用了基于MeshLambertMaterial的材质,使用了
onbeforecomile
函数,在ThreeJSV116之前,它工作得非常好

但是在将threejs和
GLTFLoader
升级到v117之后,什么也不显示

我在编译前实现
onbeforecomile
如下:

类InstancedLambertMaterial扩展了3.MeshLambertMaterial{
构造函数(参数){
超级(params);
this.spriteGrids=params?.spriteGrids;
this.userData={
制服:{
vUvScale:{value:1/Math.sqrt(params?.spriteGrids)}
}
};
}
onbeforecomile(着色器){
分配(shader.uniforms,this.userData.uniforms);
shader.vertexShader=`#定义使用_实例化_自定义\n${shader.vertexShader}`;
常量instancedAttributes=`
属性向量3翻译;
属性向量4取向;
属性向量3量表;
属性向量2 vuvoffset;
可变的vec2 v_vuvoffset;
均匀浮动vUvScale;
`;
shader.vertexShader=shader.vertexShader.replace('#include','${instancedAttributes}\n#include`);
常量替换项目顶点=`
vec4 mvPosition=vec4(已转换,1.0);
#ifdef使用_实例
mvPosition=实例矩阵*mvPosition;
#恩迪夫
#ifdef使用_实例化_自定义
vUv=紫外线;
转换*=比例;
vec3 vcV=交叉(方向.xyz,变换);
转换=vcV*(2.0*方向.w)+(交叉(方向.xyz,vcV)*2.0+转换);
mvPosition=vec4(平移+变换,1.0);
#恩迪夫
mvPosition=modelViewMatrix*mvPosition;
gl_位置=投影矩阵*mvPosition;
#ifdef使用_实例化_自定义
v_vUvOffsets=vUvOffsets;
#恩迪夫
`;
shader.vertexShader=shader.vertexShader.replace('#include',replacedProjectVertex);
shader.fragmentShader=`define USE_SPRITESHEET\n${shader.fragmentShader}`;
常量精灵形状=`
#包括
#ifdef使用_SPRITESHEET
均匀浮动vUvScale;
可变的vec2 v_vuvoffset;
#恩迪夫
`;
shader.fragmentShader=shader.fragmentShader.replace('#include',spritesheetforms);
常量spriteSheetTexelColorBranch=`
#ifdef使用_SPRITESHEET
vec4 texelColor=texture2D(贴图,(vUv*vUvScale)+(v_vUvOffsets*vUvScale));
texelColor=mapTexelToLinear(texelColor);
漫反射颜色*=texelColor;
#恩迪夫
`;
shader.fragmentShader=shader.fragmentShader.replace('#include',spriteSheetTexelColorBranch);
this.userData=着色器;
}
}
准备并应用这样的每个转换属性

const scales=new THREE.InstancedBufferAttribute(new Float32Array(instances*3),3,false);
const translations=new THREE.InstancedBufferAttribute(new Float32Array(instances*3),3,false);
常量方向=新的三个.InstancedBufferAttribute(新的Float32Array(实例*4),4,false);
const tex_vec=new THREE.InstancedBufferAttribute(new Float32Array(instances*2),2,false);
我检查了着色器输出(故意引发着色器错误),看起来没有任何与绘制对象相关的更改。
我进行了调查,但似乎没有任何与我的项目相关的变化。
我希望能够为最新版本的threejs执行这些代码

我举了一个有效的例子。除了threejs和
GLTFLoader的版本之外,这两个代码都是相同的
这是v116的结果

和v117

问题在于这行:

常量igeo=new THREE.InstancedBufferGeometry().copy(几何体)

如果执行此操作,
InstancedBufferGeometry
的属性将变为
undefined
,因为它们在
BufferGeometry
中不存在。
r117
中的重构使此错误可见


我修复了您的第二个问题,恢复了
instanceCount
属性:

非常感谢,它已修复!我必须定义
igeo.instanceCount=void 0在旧版本中,因为如果我将较小的数字设置为instanceCount,则无法增加该数字。但现在看来不需要了。此行为是否已修复?问题在于复制操作导致属性
instanceCount
未定义。您应该手动复制缓冲区属性。好的,谢谢,我知道了!