threejs 微信小程序原生版本的使用 obj模型的加载

直接上代码,

复制代码
<canvas class="webgl" type="webgl" id="gl" bindtouchstart="onTX" bindtouchend="onTX" bindtouchmove="onTX" style="width:100vw;height:90vh"></canvas>

const common_vendor = require("../../common/vendor.js");

Page({

  onLoad() {
    const query = common_vendor.index.createSelectorQuery().in(this);
    query.select("#gl").boundingClientRect((x) => {
      this.win = x;
    }).exec();
    common_vendor.index.createSelectorQuery().select("#gl").node().exec((res) => {
      const canvas = this.canvas = res[0].node;
      this.platform = new common_vendor.WechatPlatform(canvas);
      common_vendor.PLATFORM.set(this.platform);
      const scene = this.scene = new common_vendor.Scene();
      scene.background = new common_vendor.Color(16777200);
      scene.add(new common_vendor.AmbientLight(16777215, 1));
      scene.add(new common_vendor.DirectionalLight(16777215, 1));
      const camera = this.camera = new common_vendor.PerspectiveCamera(75, canvas.width / canvas.height, 0.1, 1e3);
      camera.position.set(0, 7, 15);
      const renderer = new common_vendor.WebGL1Renderer({ canvas, antialias: true, alpha: true });
      renderer.outputEncoding = common_vendor.sRGBEncoding;
      renderer.setSize(canvas.width, canvas.height);
      renderer.setPixelRatio(common_vendor.$window.devicePixelRatio);
      const controls = new common_vendor.OrbitControls(camera, canvas);
      controls.enableDamping = true;
      controls.dampingFactor = 0.25;
      controls.enableZoom = true;
      var textureLoader = new common_vendor.TextureLoader();
      var texture = textureLoader.load("https://files.ifok.cn/m2photo");
      texture.minFilter = common_vendor.LinearFilter;
      var bump = textureLoader.load("https://files.ifok.cn/security_sign/design_fabric_skin/fabric004.png");
      const loader = new common_vendor.OBJLoader();
      loader.load("https://files.ifok.cn/m2_obj/x.obj", (object) => {
        object.traverse((child) => {
          child.material = new common_vendor.MeshStandardMaterial({
            color: 11119017,
            side: common_vendor.DoubleSide,
            map: texture,
            bumpMap: bump,
            bumpScale: 2
          });
        });
        object.scale.set(0.1, 0.1, 0.1);
        object.position.set(0, -10, 0);
        scene.add(object);
        this.clo_object = object;
      }, () => {
      }, () => {
      });
      const axesHelper = new common_vendor.AxesHelper(10);
      scene.add(axesHelper);
      this.raycaster = new common_vendor.Raycaster();
      this.mouse = new common_vendor.Vector2();
      const render = () => {
        if (!this.disposing)
          this.frameId = common_vendor.$requestAnimationFrame(render);
        controls.update();
        renderer.render(scene, camera);
      };
      render();
    });
  },

  onTX(e) {
    this.platform.dispatchTouchEvent(e);
  }
})

我使用的是three-platformize,可以通过npm install的方式直接安装的,小程序原生的npm我还没有试过,加载进去后用THREE替换掉common_vendor即可,加载器那里需要导入OBJLoader,然后就可以直接用了

相关推荐
Dignity_呱33 分钟前
聊聊小程序的双线程架构
前端·微信小程序·架构
说私域1 天前
基于开源AI智能名片链动2+1模式S2B2C商城小程序的运营策略创新研究
人工智能·小程序·开源
蓝屏的钙1 天前
企业级微信小程序服务号授权关联机制
微信小程序
fakaifa1 天前
【最新版】沃德云商协系统全开源+uniapp小程序
小程序·uni-app·源码下载·沃德云商协会·商协会管理系统
夏小花花2 天前
Java 日常开发笔记(小程序页面交互传参-id)
java·微信小程序·vue
说私域2 天前
基于梅特卡夫定律的开源链动2+1模式AI智能名片S2B2C商城小程序价值重构研究
人工智能·小程序·开源
2501_916013742 天前
移动端网页调试实战,跨设备兼容与触控交互问题排查全流程
android·ios·小程序·https·uni-app·iphone·webview
不如摸鱼去2 天前
uni-app 网络请求终极选型:uni.request、axios、uni-network、alova 谁才是你的真命请求库?
前端·小程序·uni-app
说私域2 天前
生产工具革命:定制开发开源AI智能名片S2B2C商城小程序重构商业生态的范式研究
人工智能·小程序·开源
hunzi_12 天前
自建纯竞拍系统小程序需准备的事项
小程序