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 导入外部三维模型,并实现鼠标滚动放大缩小旋转效果的更多相关文章

  1. allegro设置鼠标滚轮放大缩小

    allegro设置鼠标滚轮放大缩小 allegro16版本以增加可以通过鼠标滚轮进行PCB的放大缩小.具体方法如下: 首先在HOME路径下找到PCBENV文件夹,进入该文件夹打开ENV文件. 在ENV ...

  2. Unity3D导入外部任务模型无法触发鼠标事件解决方案

    前几日 在做U3D测试的时候 导入了网上的一个人物模型 但是后来发现无论如何该模型都无法响应诸如:OnMouseDown 这些鼠标事件 又用U3D自带的水管工做了测试 发现不是我系统的问题= = 水管 ...

  3. Js图片缩放代码 鼠标滚轮放大缩小 图片向右旋转

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. svg查看预览 , 鼠标控制放大缩小 , 托拉拽等

    自己写是不可能了 , 所以要借用插件 svg-panzoom.js 地址 : https://github.com/ariutta/svg-pan-zoom#demos 及常见问题https://ww ...

  5. idea通过Ctrl+鼠标滚轮放大/缩小字体

  6. 鼠标滚轮图片放大缩小功能,使用layer弹框后不起作用

    今天在项目中遇到的一个问题:点击按钮使用layer弹框弹出一张图片,需要加一个鼠标滚轮放大缩小,图片也跟着放大缩小的功能.于是在网上找了一个demo. DEMO: <!DOCTYPE html ...

  7. Eclipse中导入外部jar包步骤

    昨天,学习了Jar包的打包过程,现在打算记录一下,如何在Eclipse中导入外部Jar包. 第一步:在项目中鼠标右键>>New>>点击Folder. 第二步:在弹出窗口将Fol ...

  8. 解决ScrollViewer嵌套的DataGrid、ListBox等控件的鼠标滚动事件无效

    C# 中,两个ScrollViewer嵌套在一起或者ScrollViewer里面嵌套一个DataGrid.ListBox.Listview(控件本身有scrollviewer)的时候,我们本想要的效果 ...

  9. 利用WPF建立自己的3d gis软件(非axhost方式)(六)跳转,增加外部三维模型

    原文:利用WPF建立自己的3d gis软件(非axhost方式)(六)跳转,增加外部三维模型 先下载SDK:https://pan.baidu.com/s/1M9kBS6ouUwLfrt0zV0bPe ...

随机推荐

  1. cobalt strike 快速上手

    原文:https://klionsec.github.io/2017/09/23/cobalt-strike/#menu 0x01 关于 Cobalt Strike 1 2 3 一款非常优秀的后渗透平 ...

  2. hdu2066多源最短路

    题目链接:http://icpc.njust.edu.cn/Problem/Hdu/2066/ SPFA可以高效过,代码如下: #include<bits/stdc++.h> using ...

  3. 《JavaScript 模式》读书笔记(5)— 对象创建模式1

    这又是一个新的开始,对象的重要性不言而喻.在JavaScript中创建对象是十分容易的,之前聊过的对象字面量和构造函数都可以达到目的.但是本篇中,我们越过那些方法,以寻求一些额外的对象创建模式. 本篇 ...

  4. Springboot 事务注解--- @Transactional

    spring boot @Transactional事物处理    spring boot 添加事物使用 @Transactional注解 简单使用 在启动类上方添加 @EnableTransacti ...

  5. 使用FME平移shapefile文件

  6. 基于Andriod的简易计算器

    这学期有安卓这门课,这里做了一个简易的计算器,实现了两位数加减乘除的基本功能,比较简单适合用来入门学习. 运行效果 预备知识 实现这个计算器之前要先了解实现计算器需要的基本组件 1.TextView ...

  7. Spring Framework之IoC容器

    Spring IoC 概述 问题 1.什么是依赖倒置? 2.什么是控制反转? 3.什么是依赖注入? 4.它们之间的关系是怎样的? 5.优点有哪些? 依赖倒置原则 (Dependency Inversi ...

  8. Ubuntu环境下如何设置文件(文件夹)权限

    查看文件(文件夹)权限 cd到需要查看的文件(文件夹)所在目录并执行: (base) duanyongchun@hc1217:~$ ls -l #查看当前文件夹下的所有文件(包括文件夹)的权限 如图: ...

  9. MySQL出现的问题

    错误展示 今天还是老样子照常启动MySQL WorkBench的时候出了错误,无法连接服务器 CMD登陆也不行 发现mysql的服务都没启动,于是点击启动,却又报这个错 cmd查看MySQL的日志,想 ...

  10. SpringCloud入门(八): Zuul 过滤器详解

    Zuul 过滤器 zuul 有四种过滤器类型,分别是: 1.Pre:过滤器在请求被路由之前调用.我们可利用这种过滤器实现身份验证.在集群中选择请求的微服务.记录调试信息等: 2.Routing:过滤器 ...