当前位置: 首页 > news >正文

从Unity到Three.js(计时器、Transform)

计时器、模型对象平移函数、枚举定义的使用
对应unity中的一些常用功能

import * as THREE from 'three';const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 1000);const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);const geometry = new THREE.BoxGeometry(1, 1, 1);
const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
const cube = new THREE.Mesh(geometry, material);
scene.add(cube);
camera.position.z = 50;//枚举定义
const direction = {UP: '上',DOWN: '下',LEFT: '左',RIGHT: '右'
};let state;
function changeState(dir) {console.log("dir=" + dir);if (state != dir) {state = dir;}
}//计时器
const clock = new THREE.Clock();
let timer = 0;
//枚举索引,不会js的int转枚举语法
let stateIndex = 1;
function updateChangeState() {timer += clock.getDelta();if (timer >= 3) {timer = 0;stateIndex++;if (stateIndex == 5) {stateIndex = 1;}changeState(convertToEnum(stateIndex));}
}//自定义int转枚举方法
function convertToEnum(value) {switch (value) {case 1:return direction.UP;case 2:return direction.LEFT;case 3:return direction.DOWN;case 4:return direction.RIGHT;}
}//更新模型位置
function updateSetObjPosition() {switch (state) {case direction.UP:cube.translateY(0.1);break;case direction.DOWN:cube.translateY(-0.1);break;case direction.LEFT:cube.translateX(-0.1);break;case direction.RIGHT:cube.translateX(0.1);break;}
}function animate() {requestAnimationFrame(animate);updateChangeState();updateSetObjPosition();renderer.render(scene, camera);
}animate();
http://www.lryc.cn/news/299573.html

相关文章:

  • 红日靶场(初学)
  • 【PyTorch】改变张量(Tensor)形状操作
  • 《金融人工智能:用python实现ai量化交易》
  • 位运算+leetcode ( 2 )
  • 17 ABCD数码管显示与动态扫描原理
  • 【Zigbee课程设计系列文章】Zigbee开发环境搭建
  • [Linux开发工具]项目自动化构建工具-make/Makefile
  • PLC_博图系列☞参数实例
  • LLaMA 2 和 QianWen-14B
  • 浅谈Java常见设计模式及实例
  • 【RISC-V DSP设计】基于CEVA DSP架构的指令集分析(一)-总体介绍
  • Rust标量类型详解
  • 【双指针】【C++算法】1537. 最大得分
  • golang常用库之-操作数据库ORM:GORM 包介绍 | 一些 GORM 提示和注意事项
  • Stream流学习笔记
  • 单片机——FLASH(2)
  • 个体诊所门诊电子处方开单管理系统软件,配方模板病历模板设置一键导入操作教程
  • ELAdmin 配置定时任务
  • 【服务器部署】Docker环境的安装
  • leetcode刷题--贪心算法
  • 《Java 简易速速上手小册》第5章:Java 开发工具和框架(2024 最新版)
  • Python json解析
  • [FFmpeg学习]从视频中获取图片
  • Redis集中管理Session和系统初始化参数详解
  • [网鼎杯 2020 朱雀组]phpweb
  • 情人节html代码
  • 键盘重映射禁用 CtrlAltDel 键的利弊
  • 【网工】华为设备命令学习(综合实验一)
  • JavaScript中的常见算法
  • 桥接模式:连接抽象与实现的设计艺术