aframe - A-Frame:如何使用光线投射器旋转远处的组件?
问题描述
在 Aframe 中,有没有办法使用控制器通过 raycaster 旋转远处的组件?
解决方案
是的。光标实体,带有 raycaster 和 class='clickable' 像这样
<a-entity id="mouseCursor" cursor="rayOrigin: mouse" raycaster="objects:
.clickable"></a-entity>
然后在要旋转的对象上添加一个自定义组件,该组件侦听鼠标事件,然后旋转对象,直到发生鼠标离开事件。像这样
AFRAME.registerComponent('over-listener', {
schema:{
mouseover:{type: 'boolean', default: false}
},
init: function () {
var el = this.el; // reference to the entity that contains this component
var data = this.data; // reference to the properties of this component.
// Listen for mouseenter event
this.el.addEventListener('mouseenter', function (evt) {
// You can't change the property directly. You must use setAttribute.
el.setAttribute('over-listener','mouseover', true);
// Change the color of the button to indicate rollover state is on.
el.setAttribute('material','color','#55ee00');;
});
// Listen for mouseleave event
this.el.addEventListener('mouseleave', function (evt) {
el.setAttribute('over-listener','mouseover', false);
el.setAttribute('material','color','orange');
});
},
tick: function(){ // called every frame
if(this.data.mouseover){ // Check the mouseover state
let elOcta = document.querySelector('#octahedron');
let rot = elOcta.getAttribute('rotation');
elOcta.setAttribute('rotation',{x: rot.x, y: rot.y , z: rot.z + 1});
}
}
});
推荐阅读
- javascript - 使用 JavaScript 切换 CSS 类无法生成动画
- rust - 在 Rust 中设置结构构造函数的类型
- c# - WPF 指定绑定时如何生成列
- excel - 复杂的excel问题,解析字符串然后从匹配单元格旁边的单元格中获取最大日期
- c - 什么是 htonl 的“to little endian”等价物?
- python - 将 folium 地图导出为 png
- php - 有可能使用 Jquery 将 PHP Post 传递给简单 MVC 结构中的构造函数类吗?
- node.js - 读取用 express 传递的参数
- bluetooth-lowenergy - iOS 蓝牙 CBPeripheralManager 字典键支持
- java - 从 csv 文件 Java 中读取数据