ThreeJs 导入外部三维模型,并实现鼠标滚动放大缩小旋转效果
let i = ;
function init() {
// create a scene, that will hold all our elements such as objects, cameras and lights.
var scene = new THREE.Scene();
// create a camera, which defines where we're looking at.
var camera = new THREE.PerspectiveCamera(, window.innerWidth / window.innerHeight, 0.1, );
var renderer = new THREE.WebGLRenderer();
var axes = new THREE.AxesHelper();
var controls = new THREE.TrackballControls(camera); //创建场景旋转缩放事件 camera.position.set(-, , );
camera.lookAt(scene.position); renderer.setClearColor(new THREE.Color(0xcccccc)); // 设置渲染面板颜色
renderer.setSize(window.innerWidth, window.innerHeight); // 设置渲染面板长宽 // // show axes in the screen
// 显示三维坐标轴
scene.add(axes); controls = new THREE.TrackballControls(camera); //创建场景旋转缩放事件
controls.rotateSpeed = 2.5;
controls.zoomSpeed = 1.2;
controls.panSpeed = 0.8;
controls.noZoom = false;
controls.noPan = false;
controls.staticMoving = true;
controls.dynamicDampingFactor = 0.3;
// create a render and set the size
// 设置渲染面板属性 // create the ground plane
// var planeGeometry = new THREE.PlaneGeometry(60, 20);
// var planeMaterial = new THREE.MeshBasicMaterial({
// color: 0xAAAAAA
// });
// var plane = new THREE.Mesh(planeGeometry, planeMaterial); // // rotate and position the plane
// plane.rotation.x = -0.5 * Math.PI;
// plane.position.set(15, 0, 0); // // add the plane to the scene
// scene.add(plane); // create a cube // position the cube
// cube.position.set(-4, 3, 0);
// add the cube to the scene // create a sphere
// var sphereGeometry = new THREE.SphereGeometry(4, 20, 20);
// var sphereMaterial = new THREE.MeshBasicMaterial({
// color: 0x7777FF,
// wireframe: true
// });
// var sphere = new THREE.Mesh(sphereGeometry, sphereMaterial); // // position the sphere
// sphere.position.set(20, 4, 2); // // add the sphere to the scene
// scene.add(sphere); // position and point the camera to the center of the scene // add the output of the renderer to the html element // render the scene
let addT = null;
let redt = null;
let timeAdd = null;
let timeDel = null;
let step = ;
let frequ = ;
// 数值增加到制定数字
function add (dis) {
clearInterval(timeDel);
timeAdd = setInterval(() => {
if (i < dis) {
i++;
intCub();
} else {
clearInterval(timeAdd);
// del(0);
}
console.log(i);
}, frequ);
};
// 数值减少到制定数字
function del (dis) {
clearInterval(timeAdd);
timeDel = setInterval(() => {
if (i > dis) {
i--;
intCub();
} else {
val = dis;
clearInterval(timeDel);
add()
}
console.log(i);
}, frequ);
}; function intCub () {
let random = parseInt( + ( - ) * (Math.random())); // 随机数用于正方体的长宽高
let randomC = parseInt( + ( - ) * (Math.random())); // 随机数用于球形的半径
let colorRandomNum = parseInt( + ( - ) * (Math.random())); // 随机数用于赋值后续的物体的材质颜色
let randomColor = [0xF7CE18, 0x2550EC, 0x57E10C, 0xEB6F0A, 0xEB0AE9, 0x820745, 0x8D11D8]; // 配置灯光
let light = new THREE.AmbientLight(0x820745);
light.position.set(, , ); // 生成正方体
var cubeGeometry = new THREE.BoxGeometry(random, random, random); // 长宽高
// 给正方体网格添加材质
var cubeMaterial = new THREE.MeshBasicMaterial({
color: randomColor[colorRandomNum],
wireframe: false // 是否显示网格状态
});
var cube = new THREE.Mesh(cubeGeometry, cubeMaterial); // 将材质贴到模型上 // 生成原型
var sphereGeometry = new THREE.SphereGeometry(randomC, , ); // 半径和网格数,网格数表示球体的粗糙程度
var sphereMaterial = new THREE.MeshBasicMaterial({
color: randomColor[colorRandomNum],
wireframe: false // 是否显示网格状态
});
var sphere = new THREE.Mesh(sphereGeometry, sphereMaterial); // 将材质贴到模型上 // position the sphere
// 设置球体的位置
sphere.position.set(parseInt(- + ( - ) * (Math.random())), parseInt(- + ( - ) * (Math.random())), parseInt(- + ( - ) * (Math.random()))); // 设置正方体的位置
cube.position.set(parseInt(- + ( - ) * (Math.random())), parseInt(- + ( - ) * (Math.random())), parseInt(- + ( - ) * (Math.random())));
// add the sphere to the scene
// 将贴好材质的模型和灯光添加到场景
scene.add(sphere);
scene.add(cube);
scene.add(light);
//声明raycaster和mouse变量
var raycaster = new THREE.Raycaster();
var mouse = new THREE.Vector2();
// 绑定点击事件
function onMouseClick( event ) {
//通过鼠标点击的位置计算出raycaster所需要的点的位置,以屏幕中心为原点,值的范围为-1到1.
mouse.x = ( event.clientX / window.innerWidth ) * - ;
mouse.y = - ( event.clientY / window.innerHeight ) * + ; // 通过鼠标点的位置和当前相机的矩阵计算出raycaster
raycaster.setFromCamera( mouse, camera );
// 获取raycaster直线和所有模型相交的数组集合
var intersects = raycaster.intersectObjects(scene.children);
console.log(intersects);
//将所有的相交的模型的颜色设置为红色,如果只需要将第一个触发事件,那就数组的第一个模型改变颜色即可
for ( var i = ; i < intersects.length; i++ ) {
intersects[i].object.material.color.set(0x000000);
let msg = JSON.stringify(intersects[i]);
document.querySelector('.msg .content').innerHTML = msg;
}
// alert('我点击了对象,对象信息已经在控制台打印出来');
renderer.render(scene, camera); // 渲染场景中的模型
}
// // window.removeEventListener('click', onMouseClick);
// document.onmousedown = function(event) {
// document.onmousemove = function () {
// controls.update();
// renderer.render(scene, camera); // 渲染场景中的模型
// }
// }
// document.onmouseup = function(event) {
// document.onmousemove = null
// }
document.getElementById("webgl-output").appendChild(renderer.domElement);
// 定时鼠标点击移动或者滚轮滚动的时候要触发渲染事件,画面是不会跟着放大缩小旋转
setInterval(() => {
controls.update();
renderer.render(scene, camera); // 渲染场景中的模型
}, );
// renderer.render(scene, camera); // 渲染场景中的模型 /**************加载模型************** */
var loader = new THREE.OBJLoader();//在init函数中,创建loader变量,用于导入模型
let i = 0.04;
let step = 0.02;
let v = ;
loader.load(
// 资源链接
'http://10.1.252.90:8080/src/chapter-01/models/man.obj',
// 资源加载完成后的回调函数
function (object) {
console.log(object);
object.position.set(, , );
object.rotation.z = 3.1415927; // 纠正导入
var ms = new THREE.MeshBasicMaterial({
color: 0xcccccc,
wireframe: true // 是否显示网格状态
});
scene.add(object); renderer.render(scene, camera); // 渲染场景中的模型
window.addEventListener('click', onMouseClick, false);
// // var sphere = new THREE.Mesh(object, ms); // 将材质贴到模型上
setInterval(() => {
i += step;
object.rotation.y = i;
scene.add(object);
// renderer.render(scene, camera); // 渲染场景中的模型
}, );
}
);
/**************加载模型************** */
};
// 运行渲染
add ();
intCub(); }
<!DOCTYPE html> <html> <head>
<title>Example 01.02 - First Scene</title>
<meta charset="UTF-8" />
<script type="text/javascript" charset="UTF-8" src="../../libs/three/three.js"></script>
<script type="text/javascript" charset="UTF-8" src="../../libs/three/loaders/OBJLoader.js"></script>
<script type="text/javascript" charset="UTF-8" src="../../libs/three/controls/TrackballControls.js"></script>
<script type="text/javascript" charset="UTF-8" src="../../libs/three/controls/DragControls.js"></script> <link rel="stylesheet" href="../../css/default.css">
<style>
#render{
position: fixed;
left: 20px;
top: 30px;
height: 40px;
width: 120px;
}
.msg{
position: absolute;
left: 10px;
top: 10px;
height: 600px;
min-height: 550px;
overflow:scroll;
width: 500px;
background: #fff;
border: 2px solid #eee;
}
h3{
padding: ;
margin: ;
text-align: center;
}
</style>
</head> <body> <!-- Div which will hold the Output -->
<div id="webgl-output"></div>
<div class="msg">
<h3>鼠标点击的对象信息</h3>
<div class="content"></div>
</div>
<script type="text/javascript" src="./js/01-02.js"></script>
<!-- Javascript code that runs our Three.js examples -->
<script type="text/javascript">
(function () {
// your page initialization code here
// the DOM will be available here
init();
})();
</script> </body> </html>
ThreeJs 导入外部三维模型,并实现鼠标滚动放大缩小旋转效果的更多相关文章
- allegro设置鼠标滚轮放大缩小
allegro设置鼠标滚轮放大缩小 allegro16版本以增加可以通过鼠标滚轮进行PCB的放大缩小.具体方法如下: 首先在HOME路径下找到PCBENV文件夹,进入该文件夹打开ENV文件. 在ENV ...
- Unity3D导入外部任务模型无法触发鼠标事件解决方案
前几日 在做U3D测试的时候 导入了网上的一个人物模型 但是后来发现无论如何该模型都无法响应诸如:OnMouseDown 这些鼠标事件 又用U3D自带的水管工做了测试 发现不是我系统的问题= = 水管 ...
- Js图片缩放代码 鼠标滚轮放大缩小 图片向右旋转
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- svg查看预览 , 鼠标控制放大缩小 , 托拉拽等
自己写是不可能了 , 所以要借用插件 svg-panzoom.js 地址 : https://github.com/ariutta/svg-pan-zoom#demos 及常见问题https://ww ...
- idea通过Ctrl+鼠标滚轮放大/缩小字体
- 鼠标滚轮图片放大缩小功能,使用layer弹框后不起作用
今天在项目中遇到的一个问题:点击按钮使用layer弹框弹出一张图片,需要加一个鼠标滚轮放大缩小,图片也跟着放大缩小的功能.于是在网上找了一个demo. DEMO: <!DOCTYPE html ...
- Eclipse中导入外部jar包步骤
昨天,学习了Jar包的打包过程,现在打算记录一下,如何在Eclipse中导入外部Jar包. 第一步:在项目中鼠标右键>>New>>点击Folder. 第二步:在弹出窗口将Fol ...
- 解决ScrollViewer嵌套的DataGrid、ListBox等控件的鼠标滚动事件无效
C# 中,两个ScrollViewer嵌套在一起或者ScrollViewer里面嵌套一个DataGrid.ListBox.Listview(控件本身有scrollviewer)的时候,我们本想要的效果 ...
- 利用WPF建立自己的3d gis软件(非axhost方式)(六)跳转,增加外部三维模型
原文:利用WPF建立自己的3d gis软件(非axhost方式)(六)跳转,增加外部三维模型 先下载SDK:https://pan.baidu.com/s/1M9kBS6ouUwLfrt0zV0bPe ...
随机推荐
- hdu1532 用BFS求拓扑排序
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1285 题目给出一些点对之间的先后顺序,要求给出一个字典序最小的拓扑排列.对于拓扑排序的问题,我们有DF ...
- redis 一百二十篇(简单介绍)之第一篇
前言 总结自己的redis,日常使用不是特别频繁,所以比较基础. 开篇 redis 是无关系型数据库,因为其实内存数据库,所以常常和他的竞争对手memcached对比,因为两者原理基础相似,存储方式也 ...
- Java井字棋游戏
试着写了一个井字棋游戏,希望各位能给予一些宝贵的建议. 一.棋盘类 package 井字棋游戏; public class ChessBoard { private int number; Perso ...
- npm git 常用命令行 记录
1. 推出node命令行: 两次ctrl+C或者一次ctrl+D 退出终端:exit; 2.npm 常用 npm install <name> 安装包 npm install &l ...
- 粒子群优化算法(PSO)之基于离散化的特征选择(FS)(一)
欢迎大家关注我们的网站和系列教程:http://www.tensorflownews.com/,学习更多的机器学习.深度学习的知识! 作者:Geppetto 在机器学习中,离散化(Discretiza ...
- 字典树模板 HDU - 1251
题意: 给一些单词,换行键后,查找以后输入的单词作为前缀的话们在之前出现过几次. 思路: 字典树模板----像查字典的顺序一样 #include<string> #include<s ...
- Jmeter4.0之语言修改(二)
下载最新Jmeter后,解压后,点击jmeter.bat,启动后,界面显示的是英文,那如何切换到中文了,步骤是点击Options中的Choose Language,选择中文 但是关闭Jmeter再次启 ...
- Oracle 11g中创建实例
1.打开“所有程序” -> “Oracle -OraDb11g_home1” -> “配置移植工具” -> “Database Configuration Assistant”. ...
- coding++:高并发解决方案限流技术-使用RateLimiter实现令牌桶限流-Demo
RateLimiter是guava提供的基于令牌桶算法的实现类,可以非常简单的完成限流特技,并且根据系统的实际情况来调整生成token的速率. 通常可应用于抢购限流防止冲垮系统:限制某接口.服务单位时 ...
- zookeeper 负载均衡
1,原理 将启动的服务注册到zookeeper 注册中心上面,采用临时节点,zookeeper 客户端从注册中心上读取服务的信息,之后再本地采用负载均衡算法(取模算法),将请求轮询到每个服务. 同时z ...