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,然后就可以直接用了

相关推荐
知识分享小能手1 小时前
微信小程序入门学习教程,从入门到精通,微信小程序核心 API 详解与案例(13)
前端·javascript·学习·react.js·微信小程序·小程序·vue
2501_915106324 小时前
JavaScript编程工具有哪些?老前端的实用工具清单与经验分享
开发语言·前端·javascript·ios·小程序·uni-app·iphone
Terio_my5 小时前
微信小程序开发从零基础到项目发布的全流程实战教程(四)
微信小程序·小程序
2501_916013745 小时前
iOS 上架 App 全流程实战,应用打包、ipa 上传、App Store 审核与工具组合最佳实践
android·ios·小程序·https·uni-app·iphone·webview
2501_915106325 小时前
iOS 26 能耗监测全景,Adaptive Power、新电池视图
android·macos·ios·小程序·uni-app·cocoa·iphone
Stanford_11067 小时前
关于嵌入式硬件需要了解的基础知识
开发语言·c++·嵌入式硬件·微信小程序·微信公众平台·twitter·微信开放平台
明月(Alioo)7 小时前
用AI帮忙,开发刷题小程序:软考真经微信小程序API接口文档(更新版)
微信小程序·小程序
克里斯蒂亚诺更新13 小时前
微信小程序的页面生命周期 以及onShow的应用场景
微信小程序·小程序
江城开朗的豌豆15 小时前
小程序避坑指南:这些兼容性问题你遇到了几个?
前端·javascript·微信小程序
江城开朗的豌豆16 小时前
玩转小程序页面跳转:我的路由实战笔记
前端·javascript·微信小程序