Repository: three.js
Issue: Rendering an object with multiple textures causes issues with later copyTextureToTexture calls
Labels: Needs Investigation

### Description

When rendering objects with multiple textures (thus using several texture slots) and the `renderer.copyTextureToTexture` function, the target texture in the `copyTextureToTexture` call actually references *the last texture bound in slot0* from a previous render call.

In other words: It ignores the input texture, and targets a previous, wrong one. 

This also causes some more errors later, as the wrong texture gets unbound after the copy.

### Reproduction steps

1. Render an object with two textures, texture1 and texture2
2. Then upload some more subparts into texture1 using copyTextureToTexture: `renderer.copyTextureToTexture(uv, someMoreData, texture1)`
3. The data from someMoreData actually goes to texture2 (!)
4. Subsequent calls to `renderer.copyTextureToTexture(uv, someMoreData, texture1)` complains that there is no texture bound.


### Code

```js
import * as THREE from 'three';

const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
camera.position.z = 2;
scene.add(camera);

scene.add(new THREE.AmbientLight(0xffffff));

const renderer = new THREE.WebGL1Renderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );

function makeColoredTexture(color, width, height) {
  const canvas = document.createElement('canvas');
  canvas.width = width;
  canvas.height = height;
  const context = canvas.getContext( '2d' );
  context.fillStyle = color;
  context.fillRect( 0, 0, width, height );
  return new THREE.CanvasTexture(canvas);
}

const redTexture = makeColoredTexture('rgba(255, 0, 0, 1.0)', 1024, 1024);
const greenTexture = makeColoredTexture('rgba(0, 255, 0, 1.0)', 1024, 1024);
const blueTexture = makeColoredTexture('rgba(0, 0, 255, 1.0)', 128, 128);

const mesh = new THREE.Mesh(new THREE.BoxGeometry(), new THREE.ShaderMaterial({
  uniforms: {
    map1: { value: redTexture },
    map2: { value: greenTexture }, // Bug only happens with more than one texture
  },
  vertexShader: document.getElementById( 'vertexShader' ).textContent,
  fragmentShader: document.getElementById( 'fragmentShader' ).textContent
}));
mesh.material.uniformsNeedUpdate = true;
scene.add(mesh);

// After this render call, the currently active slot is slot1 and bound texture is `greenTexture`
renderer.render(scene, camera);
scene.remove(mesh);

// The first setTexture2d in this copy call sees that slot0 already has
// the required texture, so it chooses not to bind.
// But the active texture is slot 1 with `greenTexture`, so this uses the wrong texture...
renderer.copyTextureToTexture(new THREE.Vector2(448, 448), blueTexture, redTexture);

//... so `blueTexture` got written to `greenTexture`! Let's confirm:
// If the bug is present, there will be a blue square on the green cube
// instead of the red cube
const redMesh = new THREE.Mesh(new THREE.BoxGeometry(), new THREE.MeshBasicMaterial({map: redTexture}));
redMesh.position.y += 0.5;
scene.add(redMesh);

const greenMesh = new THREE.Mesh(new THREE.BoxGeometry(), new THREE.MeshBasicMaterial({map: greenTexture}));
greenMesh.position.y -= 0.5;
scene.add(greenMesh);
renderer.render(scene, camera);
```

### Debug notes
From what I can tell, this seems to be the part where it gets a bit confused (this is in `setTexture2D`, in  `copyTextureToTexture`):
![Screenshot 2023-03-03 at 6 50 54 PM](https://user-images.githubusercontent.com/422174/222872188-f7c2f03c-5881-4e4d-bcaf-89445b361d43.png)

It seems like it needs to call this activeTexture/bindTexture, but skips it because it sees slot0 had this texture in currentBoundTextures? Unclear to me if it should be fixed here, or if it's the render() call that should have been cleaning up something more.

### Live example
The blue square should have been uploaded to the red texture, but was clearly uploaded to the green texture:
* [jsfiddle-latest-release](https://jsfiddle.net/15znhrcy/)
* [jsfiddle-dev](https://jsfiddle.net/kLsohrb0/)

### Screenshots

_No response_

### Version

r146-r150

### Device

Desktop, Phone

### Browser

Chrome, Edge, Firefox, Safari

### OS

OS X, Windows, iOS