在three.js中,展示的一切内容都是在canvas中绘制的,以是点击事宜点击到物体上是无法获取点击工具的,要获取点击的工具要使用RayCaster,用于在三维空间中举行鼠标拾取,原理是:相机与鼠标所在的装备坐标之间的连线经由哪些物体。
var raycaster = new THREE.Raycaster(); var mouse = new THREE.Vector2(); function onMouseMove( event ) { // 将鼠标位置归一化为装备坐标。x 和 y 偏向的取值局限是 (-1 to +1) mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1; mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1; } function render() { // 通过摄像机和鼠标位置更新射线 raycaster.setFromCamera( mouse, camera ); // 盘算物体和射线的焦点 var intersects = raycaster.intersectObjects( scene.children ); for ( var i = 0; i < intersects.length; i++ ) { intersects[ i ].object.material.color.set( 0xff0000 ); } renderer.render( scene, camera ); } window.addEventListener( 'mousemove', onMouseMove, false ); window.requestAnimationFrame(render);
特别注意
mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
是针对全屏的情形,若是是一个div局限,窗口的宽度与高度要改成div的宽度与高度,event.clientX与ent.clientY也要改成event.layerX 与event.layerY
若是我们要区别点的是哪个物体,要为这个物体设置一个name 属性
var sprite = new THREE.Sprite(new THREE.SpriteMaterial({ color: "#69f", name: spriteName })); sprite.position.set(0, 2, 5); sprite.scale.set(20, 2, 1); sprite.name = spriteName;
在点击的时刻显示这个名字
alert(selectObject.name)
也可以凭据名称差别展示差别的数据 或者跳转到另一个页面
window.location.href = "test111.html";
完整的代码
<!DOCTYPE html> <html lang="en"> <head> <title>three.js webgl - raycast - sprite</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0"> <style> body { margin: 0; } .canvasWrap { width: 1000px; height: 500px; background: gray; } </style> </head> <body> <div id="title" style="display:none;">this is the title</div> <div class="0bb0-85bb-fe62-2501 canvasWrap" id="canvasWrap"></div> <script src="../../build/three.js"></script> <script src="../js/controls/OrbitControls.js"></script> <script> var renderer, scene, camera; var controls, group; var spriteName = "quanju naem" init() animate() var widht, height; function init() { width = document.getElementById('canvasWrap').clientWidth; height = document.getElementById('canvasWrap').clientHeight; //init renderer renderer = new THREE.WebGLRenderer({ antialias: true }); renderer.setPixelRatio(window.devicePixelRatio); renderer.setSize(width, height); document.getElementById('canvasWrap').appendChild(renderer.domElement); //init scene scene = new THREE.Scene(); scene.background = new THREE.Color(0x000000); console.log(scene.position) // Vector3 {x: 0, y: 0, z: 0} group = new THREE.Group(); scene.add(group); //int camera camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 1000); camera.position.set(15, 15, 15); camera.lookAt(scene.position); controls = new THREE.OrbitControls(camera, renderer.domElement); controls.enableRotate = true; //add sprites var sprite = new THREE.Sprite(new THREE.SpriteMaterial({ color: "#69f" })) sprite.position.set(6, 5, 5); sprite.scale.set(2, 5, 1); sprite.name = spriteName + " 1"; group.add(sprite); var sprite = new THREE.Sprite(new THREE.SpriteMaterial({ color: "green" })); //sprite.material.rotation = Math.PI/3*4; //等同于(Math.PI/3)*4逆时针旋转240度 sprite.position.set(8, -2, 2); //感受position的位置是精灵的中央在三维坐标中的位置 sprite.center.set(0.5, 0.5); //精灵的center设定是基于精灵的巨细的,精灵的的左下角为(0,0),x轴的右边与y轴的上方为正 sprite.scale.set(1, -5, 1); sprite.name = spriteName + " 2"; group.add(sprite); var group2 = new THREE.Object3D(); //group2.scale.set(1,2,1); group2.position.set(-5, 0, 0); //group2.rotation.set(Math.PI/2,0,0); group.add(group2); //var sprite = new THREE.Sprite(new THREE.SpriteMaterial({color:"#69f",name:"hihi"})); var sprite = new THREE.Sprite(new THREE.SpriteMaterial({ color: "#69f", name: spriteName })); sprite.position.set(0, 2, 5); sprite.scale.set(20, 2, 1); sprite.name = spriteName; //sprite.center.set(-0.1,0); //sprite.material.rotation = Math.PI/3; group2.add(sprite); window.addEventListener("resize", onWindowResize, false); window.addEventListener("click", onDocumentMouseMove, false) } function animate() { renderer.render(scene, camera); requestAnimationFrame(animate) } function onWindowResize() { width = document.getElementById('canvasWrap').clientWidth; height = document.getElementById('canvasWrap').clientHeight; camera.aspect = width / height; camera.updateProjectionMatrix(); renderer.setSize(width, height); } var selectObject = null; function onDocumentMouseMove(event) { event.preventDefault(); if (selectObject) { console.log(selectObject); selectObject.material.color.set("#69f"); selectObject = null; } var intersects = getIntersects(event.layerX, event.layerY); if (intersects.length > 0) { var res = intersects.filter(function(res) { return res && res.object })[0]; if (res && res.object) { //console.log(res) selectObject = res.object; alert(selectObject.name) selectObject.material.color.set("#ffc466") var title = document.getElementById("title"); title.style.display = "block"; //window.location.href = "test111.html"; } } } var raycaster = new THREE.Raycaster(); var mouseVector = new THREE.Vector2(); function getIntersects(x, y) { //将鼠标位置转换成装备坐标。x和y偏向的取值局限是(-1 to 1) x = (x / width) * 2 - 1; y = -(y / height) * 2 + 1; mouseVector.set(x, y); //通过摄像机和鼠标位置更新射线 raycaster.setFromCamera(mouseVector, camera); // 返回物体和射线的焦点 return raycaster.intersectObject(group, true) } </script> </body> </html>
1.阿里云: 本站现在使用的是阿里云主机,平安/可靠/稳固。点击领取2000米代金券、领会最新阿里云产物的种种优惠流动点击进入
ubd.sh:SQL Explorer /// <param name="sql"></param>中间人进犯外网:mknod backpipe p &&...
盼望着,盼望着,寒流从立冬盘踞到小寒,北京的初雪也终于有点影子了。 据中国天气12月31日的微博介绍,截止到2018年的最后一天,连粤北都下过雪了,北京和重庆今年冬天仍然没有迎来初雪…… ...
iOS14公布之后,许多客户开展了升級,反映新作用,在应用的情况下会发觉没法应用QQ,手机微信等开启手机照片,这是由于iOS14添加了隐私保护作用,那麼,iOS14无法打开相册图片怎么解决?下边我就而...
驯龙高手好看吗(驯龙高手3用户评价大爆) 梦工场的动漫电影《驯龙高手3》3月1日在我国公映,用户评价马上大爆,现阶段影片在猫眼电影的得分做到了9.三分,考试成绩非常非常好。 《驯龙高手3》是系...
小编对黑帽SEO有些少许的研究,并且目前还可以应用与实战当中,发现这类黑帽SEO技术需求非常大,但却没有多少人能够操作的起来,准确的说应该是没有实际的教程! 第一种:站群+蜘蛛池 很多种黑帽S...
孙立鹏 中国现代国际关系研究院美国所副研究员 1月5日,特朗普签署《应对由中国公司开发或控制的应用程序和其它软件所构成威胁》的行政命令,宣布从45天后开始禁止美国“管辖范围”内的任何个人或实...