Update Texture coordinations after deleting some vertices

Hello :smile:,
I use the LodGenerator to generate a new index buffer and then use this index buffer to update the remaining buffers of the mesh. However, I did not get the desired result . Here first is my code :


        public void reduceVertexCount(Spatial node) {

	if (node instanceof Node) {
		for (Spatial s : ((Node) node).getChildren()) {
			reduceVertexCount(s);
		}
	} else if (node instanceof Geometry) {
		Geometry geometry = (Geometry) node;
		Mesh mesh = geometry.getMesh();
		
		LodGenerator lodGenerator = new LodGenerator(geometry);
		VertexBuffer[] reducedMesh = lodGenerator.computeLods(
				LodGenerator.TriangleReductionMethod.PROPORTIONAL, 0.5f);
		System.out.println("reducedMesh.length=" + reducedMesh.length);
		VertexBuffer newIndexBuffer = reducedMesh[1];
		Buffer b = newIndexBuffer.getData();
		
		mesh.clearBuffer(newIndexBuffer.getBufferType());
		mesh.setBuffer(newIndexBuffer);
		
		int[] test = new int[mesh.getIndexBuffer().size()];
		for(int i=0;i<mesh.getIndexBuffer().size();i++){
			test[i] = mesh.getIndexBuffer().get(i);
			}
		
	//delete the entries which are dublicated in the index buffer to get all indices
        int[] newArray = new int[test.length];
        System.arraycopy(test, 0, newArray, 0, test.length);
        int pos = 0;
        Set<Integer> set = new HashSet<Integer>();
        for(int i=0; i< test.length; i++) {
            if(! set.contains(test[i])) {
                set.add(test[i]);
                newArray[pos] = test[i];
                pos++;
            }
        }
        int[] returnArray = new int[pos];
        
        Arrays.sort(returnArray);
     

        //create new position array
        VertexBuffer buffer = mesh.getBuffer(VertexBuffer.Type.Position);
        Vector3f[] newPosArray = new Vector3f[buffer.getNumElements()];
       
        	for(int index : returnArray){
        		newPosArray[index] = new Vector3f();
	            newPosArray[index].setX((Float) buffer.getElementComponent(index, 0));
	            newPosArray[index].setY((Float) buffer.getElementComponent(index, 1));
	            newPosArray[index].setZ((Float) buffer.getElementComponent(index, 2));
        }
        
        //create new normal array
        VertexBuffer normalBuffer = mesh.getBuffer(VertexBuffer.Type.Normal);
        Vector3f[] newNormalArray = new Vector3f[normalBuffer.getNumElements()];
 	       
        	for(int index : returnArray){
        		newNormalArray[index] = new Vector3f();
        		newNormalArray[index].setX((Float) normalBuffer.getElementComponent(index, 0));
        		newNormalArray[index].setY((Float) normalBuffer.getElementComponent(index, 1));
        		newNormalArray[index].setZ((Float) normalBuffer.getElementComponent(index, 2));
        }
        	
        	//create new textureC array
	        VertexBuffer TextureCBuffer = mesh.getBuffer(VertexBuffer.Type.TexCoord);
	        Vector3f[] newTextureCArray = new Vector3f[TextureCBuffer.getNumElements()];
	 	       
	        	for(int index : returnArray){
	        		newTextureCArray[index] = new Vector3f();
	        		newTextureCArray[index].setX((Float) TextureCBuffer.getElementComponent(index, 0));
	        		newTextureCArray[index].setY((Float) TextureCBuffer.getElementComponent(index, 1));
	        		newTextureCArray[index].setZ((Float) TextureCBuffer.getElementComponent(index, 2));
	        	
	        }
      
        
        mesh.clearBuffer(VertexBuffer.Type.TexCoord);
        mesh.clearBuffer(VertexBuffer.Type.Normal);
        mesh.clearBuffer(VertexBuffer.Type.Position);
        mesh.setBuffer(VertexBuffer.Type.TexCoord , 2 , BufferUtils.createFloatBuffer(newTextureCArray)); 
        mesh.setBuffer(VertexBuffer.Type.Normal , 1 , BufferUtils.createFloatBuffer(newNormalArray));
        mesh.setBuffer(VertexBuffer.Type.Position , 3 , BufferUtils.createFloatBuffer(newPosArray));

        mesh.updateCounts();
        mesh.updateBound();
        
	}

Without changing the buffers ( original model ) :

and when changing the buffers :

I think it should not look like this :sweat_smile:
Do i need to update something else?

thanks :slight_smile: