How can I downsample a texture?

I'm using the garment transfer with copyFrame, but it's behaving really poorly on my device. Is there some way to downsample the copied texture?

Best Answer

  • Joshua Beckwith
    Joshua Beckwith Posts: 18 🔥
    #2 Answer ✓

    I'm not sure if this will increase performance or not, but at least I managed to downsample the texture with a few ProceduralTextureProviders

    //@input float downScale = 3 {"widget": "slider", "min": 1, "max":20, "step": 1}
    /** @type {number} */
    var downScale = script.downScale
    
    //@input Component.Image image
    /** @type {Image} */
    var image = script.image
    
    //@input Asset.Texture cameraTexture
    /** @type {Texture} */
    var cameraTexture = script.cameraTexture
    
    function doSnapshot() {
      var channels = 4 // RGBA
      var frame = cameraTexture.copyFrame()
      var camTex = ProceduralTextureProvider.createFromTexture(frame)
      var camWidth = camTex.getWidth()
      var camHeight = camTex.getHeight()
      var camPixels = new Uint8Array(camWidth * camHeight * channels)
      camTex.control.getPixels(0, 0, camWidth, camHeight, camPixels)
    
      var scale = 1/downScale
      print(scale)
      var width = Math.floor(camWidth * scale)
      var height = Math.floor(camHeight * scale)
    
      var newTex = ProceduralTextureProvider.create(width, height, Colorspace.RGBA)
      var newData = new Uint8Array(width * height * channels)
    
      for (var y = 0; y < height; y++) {
        for (var x = 0; x < width; x++) {
          var newIndex = (y * width + x) * channels
          var oldIndex = (y * camWidth / scale + x / scale) * channels
    
          // Set R, G, B, A
          newData[newIndex] = camPixels[oldIndex]
          newData[newIndex + 1] = camPixels[oldIndex + 1]
          newData[newIndex + 2] = camPixels[oldIndex + 2]
          newData[newIndex + 3] = 255
        }
      }
    
      newTex.control.setPixels(0, 0, width, height, newData)
      image.mainMaterial.mainPass.baseTex = newTex
      snapDelay.reset(2)
    }
    
    var snapDelay = script.createEvent('DelayedCallbackEvent')
    snapDelay.bind(doSnapshot)
    snapDelay.reset(1)
    
    

Answers

  • If I face such issue with size of texture go to explore in resource panel copy the image on desktop. I edit them in Photoshop reducing the size + quality and save it on desktop as a new file.
    And dragging it back to lens studio and assigning it with respective material. And it just works other then doing the whole long process in 3d program.

  • Thank you for your insights, but unfortunately I can't do that for device camera texture snapshots since they are generated at runtime