Threejs TextureLoader 加载进度处理

11 篇文章 0 订阅

TextureLoader 加载进度处理

参考:Revert FileLoader-based ImageLoader · Issue #10439 · mrdoob/three.js · GitHub

处理这类型图片

three_demo: https://threejs.org/examples/#webgl_panorama_equirectangular

demo

- 添加缓存后显示进度

调用

this.dealTextureLoader(url)

调用后处理

dealTextureLoader(path) {
   this.initTextureLoader().load(
       path,
       texture => {
       /**
       * RepeatWrapping:重复平铺
       * MirroredRepeatWrapping:重复平铺。每一次重复前先镜像,使用镜像后的生成的纹理重复平铺
       *
       * wrapS: 纹理在水平方向上纹理包裹方式
       * wrapT: 纹理贴图在垂直方向上将如何包裹
       */
       texture.wrapS = texture.wrapT = RepeatWrapping
       texture.repeat.set(-1, 1)

       const geometry = new SphereGeometry(500, 64, 64)
       geometry.scale(-1, 1, 1)
       const material = new MeshBasicMaterial({
          map: texture,
          side: DoubleSide
       })

       const mesh = new Mesh(geometry, material)
       scene.add(mesh)

       if (this.progress >= 100) {
           this.$emit('loading', false)
       }
    },
      num => {
         this.$emit('progress', Math.floor((num.loaded / num.total) * 100))
      }
    )
}

进度条处理

initTextureLoader() {
  cache = Cache
  // Turn on shared caching for FileLoader, ImageLoader and TextureLoader
  cache.enabled = true

  const textureLoader = new TextureLoader()
  const fileLoader = new FileLoader()
  fileLoader.setResponseType('blob')

  const _self = this
  function load(url, onLoad, onProgress, onError) {
    const cached = cache.get(url)
    if (cached) {
      // return cached
      hanveImage()
    } else {
      fileLoader.load(url, cacheImage, onProgress, onError)
    }

    function hanveImage() {
      // document.body.appendChild(cached)
      let _num = 10
      let _timer = window.setInterval(() => {
        if (_num >= 100) {
          window.clearInterval(_timer)
          loadImageAsTexture()
        } else {
          _num += 10
          _self.$emit('progress', _num)
        }
      }, 50)
    }

    /**
     * The cache is currently storing a Blob, but we need to cast it to an Image
     * or else it won't work as a texture. TextureLoader won't do this automatically.
     */
    function cacheImage(blob) {
      // ObjectURLs should be released as soon as is safe, to free memory
      objUrl = URL.createObjectURL(blob)
      image = document.createElementNS('http://www.w3.org/1999/xhtml', 'img')

      image.onload = () => {
        cache.add(url, image)
        URL.revokeObjectURL(objUrl)
        document.body.removeChild(image)
        loadImageAsTexture()
      }

      image.src = objUrl
      image.style.visibility = 'hidden'
      document.body.appendChild(image)
    }

    function loadImageAsTexture() {
      textureLoader.load(url, onLoad, () => {}, onError)
    }
  }

  return Object.assign({}, textureLoader, { load })
},

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值