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

相关推荐
2501_915918414 小时前
Fiddler中文版全面评测:功能亮点、使用场景与中文网资源整合指南
android·ios·小程序·https·uni-app·iphone·webview
说私域5 小时前
从品牌附庸到自我表达:定制开发开源AI智能名片S2B2C商城小程序赋能下的营销变革
人工智能·小程序
難釋懷5 小时前
第一个小程序
小程序
春哥的研究所5 小时前
可视化DIY小程序工具!开源拖拽式源码系统,自由搭建,完整的源代码包分享
小程序·开源·开源拖拽式源码系统·开源拖拽式源码·开源拖拽式系统
weixin_lynhgworld5 小时前
盲盒一番赏小程序技术实现方案:高并发与防作弊的平衡之道
小程序
今日热点7 小时前
小程序主体变更全攻略:流程、资料与异常处理方案
经验分享·微信·小程序·企业微信·微信公众平台·微信开放平台
鸭鸭梨吖11 小时前
微信小程序---下拉框
微信小程序·小程序
CRMEB定制开发12 小时前
CRMEB Pro版前端环境配置指南
前端·微信小程序·uni-app·商城源码·微信商城·crmeb
mon_star°14 小时前
搭建一款结合传统黄历功能的日历小程序
微信·微信小程序·小程序·微信公众平台
The_era_achievs_hero14 小时前
微信小程序91~100
微信小程序·小程序