在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米代金券、领会最新阿里云产物的种种优惠流动点击进入
测验对其进行反编译,能够运用PyInstaller的archive_viewer脚本提取对应的pyc脚本,但因为脚本运转是需求用户交互指定反编译文件名,所以能够定制代码将所需求反编译的pyc悉数提取,...
跟着快时代糊口的成长,各人在早上的时候都是在外面吃,包罗中餐、晚餐也是一样的,许多工钱了利便、自制、快捷许多人城市选择去面馆用饭,刀削面,许多人都吃过,刀削面加盟谁人品牌较好?大概投资商从来都没有...
伴随着本周第六期节目的播出,六位导师至此已经全部亮相,六位性格各异的导师指导少年的风格为节目带来了许多的讨论话题。而在预估的热度下,第六期节目播出后,全网播放量将突破...
本文导读目录: 1、推荐几个音效比较好的电影 2、有谁给我推荐几部声音效果做得好的电影。 3、谁看懂黑客帝国了? 4、哪些DTS电影做的好 5、求《黑客帝国》电影里的各种音效 6、...
本文目录一览: 1、花3个月时间设计一个镜头?《黑客帝国》的幕后故事你知道多少? 2、黑客帝国这部电影一直没看懂,谁有这方面的资料,介绍一下 3、跪求电影《黑客帝国》特技鉴赏论文! 花3个月...
本文导读目录: 1、你们谁看过黑客帝国动画版,里面很多故事其中有一个跳楼少年最后了死了吗?还是说他去了真实世界? 2、成神之日黑客少年最后结局。 3、之前有看过一部漫画,讲的是日本少年黑客协助...