微信小程序threejs三维开发

server/2025/3/16 4:21:41/

微信小程序threejs开发

import * as THREE from 'three';

const {

performance,

  document,

  window,

  HTMLCanvasElement,

  requestAnimationFrame,

  cancelAnimationFrame,

core,

  Event,

  Event0

} = THREE .DHTML

import Stats from 'three/examples/jsm/libs/stats.module.js';

import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';

import { OrbitControls0 } from 'three/examples/jsm/controls/OrbitControls0.js';

import { RoomEnvironment } from 'three/examples/jsm/environments/RoomEnvironment.js';

import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js';

import { DRACOLoader } from 'three/examples/jsm/loaders/DRACOLoader.js';

var requestId

Page({

  onUnload() {

    cancelAnimationFrame(requestId, this.canvas)

    this.worker && this.worker.terminate()

if(this.canvas) this.canvas = null

    setTimeout(() => {

      if (this.renderer instanceof THREE.WebGLRenderer) {

        this.renderer.dispose()

        this.renderer.forceContextLoss()

        this.renderer.context = null

        this.renderer.domElement = null

        this.renderer = null

      }

    }, 10)

  },

  webgl_touch(e){

    const web_e = (window.platform=="devtools"?Event:Event0).fix(e)

    this.canvas.dispatchEvent(web_e)

  },

  onLoad() {

    document.createElementAsync("canvas", "webgl2",this).then(canvas => {

      this.canvas = canvas

      this.body_load(canvas).then()

    })

  },

  async body_load(canvas3d) {

    let mixer;

    const clock = new THREE.Clock();

    const container = document.getElementById('container');

    const stats = new Stats();

    container.appendChild(stats.dom);

    const renderer = new THREE.WebGLRenderer( { antialias: true } );

    renderer.setPixelRatio( window.devicePixelRatio );

    renderer.setSize( window.innerWidth, window.innerHeight );

    container.appendChild( renderer.domElement );

    const pmremGenerator = new THREE.PMREMGenerator( renderer );

    const scene = new THREE.Scene();

        var AmbientLight = new THREE.AmbientLight(0xffffff,1)

    scene.add(AmbientLight)

    ///

    /*

          var spotLight = new THREE.SpotLight(0xaaaaaa, 1) //聚光灯

        spotLight.position.set(0, 0, 10)

        spotLight.lookAt(scene.position)

        scene.add(spotLight)*/

    //   

/*

    var directionalLight = new THREE.DirectionalLight(0xaaaaaa, 1) //方向光

    directionalLight.position.set(0, 0, 10)

    directionalLight.lookAt(scene.position)

    scene.add(directionalLight)*/

    

    scene.background = new THREE.Color( 0xbfe3dd );

    scene.environment = pmremGenerator.fromScene( new RoomEnvironment( renderer ), 0.04 ).texture;


 

    const camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 100);

    camera.position.set(5, 2, 8);

    const controls = new (window.platform=="devtools"?OrbitControls:OrbitControls0)(camera, renderer.domElement);

    controls.target.set(0, 0.5, 0);

    controls.update();

    controls.enablePan = true;

    controls.enableDamping = true;

    const dracoLoader = this.dracoLoader = new DRACOLoader();

    dracoLoader.setDecoderPath('jsm/libs/draco/gltf/');

    const loader = new GLTFLoader();

    loader.setDRACOLoader(dracoLoader);

    //loader.load('https://statics.jiuqianqu.com/btwc-file/20230222/1677055512471.glb', function (gltf) {

    loader.load('models/gltf/LittlestTokyo.glb', function (gltf) {

      //console.error("xxxxxxx",gltf)

      /*  gltf.scene.traverse(function (child) {

            if (child.isMesh) {

                child.frustumCulled = false;

                //模型阴影

                child.castShadow = true;

                //模型自发光

              child.material.emissive = child.material.color;

                child.material.emissiveMap = child.material.map;

            }

        })*/

      const model = gltf.scene;

      model.position.set(1, 1, 0);

      model.scale.set(0.01, 0.01, 0.01);

      scene.add(model);

      mixer = new THREE.AnimationMixer(model);

      mixer.clipAction(gltf.animations[0]).play();

      animate();

    },null, function (e) {

      console.error(e);

    });


 

    window.onresize = function () {

      camera.aspect = window.innerWidth / window.innerHeight;

      camera.updateProjectionMatrix();

      renderer.setSize(window.innerWidth, window.innerHeight);

    };


 

    function animate() {

      requestId = requestAnimationFrame(animate);

      const delta = clock.getDelta();

      mixer.update(delta);

      controls.update();

      // //stats.update();

      renderer.render(scene, camera);

    }

  }

})


http://www.ppmy.cn/server/175318.html

相关文章

影刀RPA安装32位与64位的差别

1. 影刀RPA概述 1.1 产品简介 影刀RPA是一款由杭州分叉智能科技有限公司研发的RPA自动化软件,致力于为各行业客户提供RPA自动化机器人产品与解决方案,能够实现PC、手机上的任何软件自动化操作。其功能特性丰富,包括桌面软件自动化、网页自动…

第十五届蓝桥杯C/C++ C 组全部题目详细题解

本文为第十五届蓝桥杯C/C C 组全部题目的详细题解,题目均来自于蓝桥杯官网,真题链接: 蓝桥杯真题卷 - 蓝桥云课 觉的有帮助或者写的不错可以点个赞,如果我题解写的有问题也欢迎评论指出,欢迎友好讨论 目录 题一:拼正方…

使用OpenCV和MediaPipe库——抽烟检测(姿态监控)

目录 抽烟检测的运用 1. 安全监控 (1) 公共场所禁烟监管 (2) 工业安全 2. 智能城市与执法 (1) 城市违章吸烟检测 (2) 无人值守管理 3. 健康管理与医疗 (1) 吸烟习惯分析 (2) 远程监护 4. AI 监控与商业分析 (1) 保险行业 (2) 商场营销 5. 技术实现 (1) 计算机视…

Golang倒腾一款简配的具有请求排队功能的并发受限服务器

golang官方指南[1]给了一些代码片段&#xff0c;层层递进演示了信道的能力: 1>. 信号量2>. 限流能力 var sem make(chan int, MaxOutstanding) func Serve(queue chan *Request) {for req : range queue {req: reqsem <- 1 go func() { // 只会开启MaxOutstandin…

VS Code 配置优化指南

目录 一、安装与基础设置1. 安装 VS Code2. 中文语言包 二、插件推荐三、常见配置项与优化1. 用户 / 工作区设置2. 全局配置 / Settings Sync3. 常用设置示例 四、性能优化五、调试与终端配置1. 调试配置2. 内置终端配置 六、快捷键配置七、美观与主题八、总结 VS Code&#xf…

【SpringMVC】深入解析使用 Postman 和浏览器模拟将单个与多个参数传递到后端的原理和后端接收参数的过程

SpringMVC—请求(Request) 访问不同的路径&#xff0c;就是发送不同的请求&#xff1b;在发送请求时&#xff0c;可能会带一些参数&#xff0c;所以学习Spring的请求&#xff0c;主要是学习如何传递参数到后端以及后端如何接收&#xff1b; 我们主要是使用 浏览器 和 Postman …

5-27 临摹大师-IP-Adapter

前言&#xff1a; 前一节我们主要介绍ControlNet中如何对黑白照片进行上色 主要介绍ControlNet中的IP-Adapter。这个也是一种类似的风格借鉴&#xff0c;类似Reference的能力。 当然IP-Adapter有两点或许可以吸引我们&#xff0c;一个是国人腾讯公司制作的。另一个在速度和效…

在 CentOS 上安装 Oracle 数据库

文章目录 **1. 系统准备****1.1 检查系统要求****1.2 更新系统****1.3 安装必要的依赖包****1.4 创建 Oracle 用户和组****1.5 配置内核参数****1.6 配置用户限制****1.7 配置 PAM 模块****1.8 创建 Oracle 安装目录** **2. 下载 Oracle 数据库安装包****2.1 访问 Oracle 官方网…