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

相关推荐
说私域6 小时前
基于AI智能名片链动2+1模式服务预约商城系统的社群运营与顾客二次消费吸引策略研究
大数据·人工智能·小程序·开源·流量运营
StarChainTech12 小时前
无人机租赁平台:开启智能租赁新时代
大数据·人工智能·微信小程序·小程序·无人机·软件需求
计算机毕设指导612 小时前
基于微信小程序的运动场馆服务系统【源码文末联系】
java·spring boot·微信小程序·小程序·tomcat·maven·intellij-idea
码农客栈12 小时前
小程序学习(十一)之uni-app和原生小程序开发区别
学习·小程序·uni-app
说私域13 小时前
基于AI客服链动2+1模式商城小程序的社群运营策略研究——以千人社群活跃度提升为例
人工智能·微信·小程序·私域运营
mqjFKmYf13 小时前
VSC整流系统定功率控制系统的MATLAB仿真模型(支持MATLAB 2014a及以上版本)
小程序
Json____14 小时前
使用uni-app开发抖音小程序遇到previewImage方法图片加载不出来解决方案
小程序·uni-app
枕咸鱼的猫14 小时前
【龙雏晴雨通】实时查看天气小程序
微信小程序
毕设源码-钟学长15 小时前
【开题答辩全过程】以 基于微信小程序教学评价平台的设计与实现为例,包含答辩的问题和答案
微信小程序·小程序
说私域15 小时前
基于AI智能名片链动2+1模式S2B2C商城小程序的流量运营策略研究
人工智能·微信·小程序·产品运营·流量运营