微信小程序> 微信小游戏开发之四:使用three.js引擎

微信小游戏开发之四:使用three.js引擎

浏览量:1751 时间: 来源:Emperor_Dandy

一、前言

微信小游戏中最魔性的‘跳一跳’就是基于three.js 引擎开发的

看这里!!!!不要再让我发邮箱了!

源码放到github上了:GitHub地址   请自行下载。

二、下载

three.min.js 打开页面,复制代码到本地

三、引用

使用如下方式在小游戏中引用three

let THREE = require('three.min.js的路径')

四、开始

创建3dgame.js文件

需要注意的是,在微信小游戏中并没有‘ImageBitmap’这个全局对象,所以在加载纹理贴图时会报错,此时需要修改源码

let THREE = require('./three/three')export default class Game3d {    constructor() {        // 场景        this.scene = new THREE.Scene();        // 透视摄像头        this.camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);        // webGL渲染器        // 同时指定canvas为小游戏暴露出来的canvas        this.renderer = new THREE.WebGLRenderer({            canvas: canvas        });        this.start()    }    start() {        this.renderer.setSize(window.innerWidth, window.innerHeight);        var geometry = new THREE.CubeGeometry(1, 1, 1);        // 加载纹理贴图        var texture = new THREE.TextureLoader().load("images/metal.jpg");        var material = new THREE.MeshBasicMaterial({ map: texture });        this.cube = new THREE.Mesh(geometry, material);        this.scene.add(this.cube);        // 设置camera的高度,若是低于当前场景的高度则屁也看不到        this.camera.position.z = 2.5;        this.cube.castShadow = true        console.log(this.cube)        window.requestAnimationFrame(this.loop.bind(this), canvas);    }    update() {        this.cube.rotation.x += 0.02;        this.cube.rotation.y += 0.04;        this.cube.rotation.z += 0.06;    }    loop() {        this.update()        this.renderer.render(this.scene, this.camera);        window.requestAnimationFrame(this.loop.bind(this), canvas);    }}
在game.js中调用

import './js/libs/weapp-adapter'import './js/libs/symbol'import Game3d from './3dgame'new Game3d()

五、效果

小程序

版权声明

即速应用倡导尊重与保护知识产权。如发现本站文章存在版权问题,烦请提供版权疑问、身份证明、版权证明、联系方式等发邮件至197452366@qq.com ,我们将及时处理。本站文章仅作分享交流用途,作者观点不等同于即速应用观点。用户与作者的任何交易与本站无关,请知悉。

产品经理

手机 : 13312967497

擅长 : 小程序流量变现

扫码领取礼包

最新资讯

热门模板

  • 头条
  • 搜狐
  • 微博
  • 百家
  • 一点资讯
  • 知乎