ios wkwebview 上实现threejs双击选中模型
2017-05-07 本文已影响353人
bbmm
问题概况:IOS 项目中遇到一个问题,wkweview 上加载一个threejs 的页面.threejs 本身的旋转,缩放,平移事件都自动转换到移动端的各种手势上。只有双击选中模型事件一直无法实现,pc端双击选中模型没有问题。
threejs 中的双击事件:
1、添加双击事件
document.addEventListener('dblclick', clickHandler, false);
2、双击事件实现
function clickHandler(event) {
event.preventDefault();
reviewMaterial();
selectedModel = null;
var vector = new THREE.Vector3((event.clientX / window.innerWidth) * 2 - 1, -(event.clientY / window.innerHeight) * 2 + 1, 0.5);
projector.unprojectVector(vector, JKBIM.camera);
var raycaster = new THREE.Raycaster(JKBIM.camera.position, vector.sub(JKBIM.camera.position).normalize());
intersects = raycaster.intersectObjects(targetList);
if (intersects.length > 0) {
var j = 0;
while (j < intersects.length) {
if (intersects[j].object.type == "Sprite") {
break;
}
//FOR MESHES:
if (!$.isEmptyObject(intersects[j].object.userData)) {
if (lastMeshMaterial != -1) {
//reset last material for last lastMeshID
for (var i = 0; i < JKBIM.scene.children.length; i++) {
if (JKBIM.scene.children[i].id == lastMeshID) {
JKBIM.scene.children[i].material = lastMeshMaterial;
}
}
}
//set lastMaterial
if (objuuid == bimId) {
lastMeshMaterial = lastsMeshMaterial;
} else {
lastMeshMaterial = intersects[j].object.material;
}
//set lastMeshID
lastMeshID = intersects[j].object.id;
//apply SelMaterial
intersects[j].object.material = selMaterial;
break;
}
//FOR OBJECT3D
if (!$.isEmptyObject(intersects[j].object.parent.uuid)) {
if (intersects[j].object.parent.visible) {
selectedModel = intersects[j];
objuuid = intersects[j].object.parent.uuid;
objId = intersects[j].object.parent.name.split('<')[1].split(' ')[0];
if (objuuid == bimId) {
lastObjectMaterial = lastsMeshMaterial;
} else {
//set lastMaterial
lastObjectMaterial = intersects[j].object.material;
}
//set lastObjectID
lastObjectID = intersects[j].object.parent.id;
//apply SelMaterial
intersects[j].object.material = selMaterial;
Mname = intersects[j].object.parent.name;
//获取objId
objId = intersects[j].object.parent.name.split('<')[1].split(' ')[0];
//获取objName
objName = intersects[j].object.parent.name.split(' ')[1].split(' ')[0];
break;
}
}
j++;
}
}
}
解决: 通过在wkwebview 中增加双击手势,获取到触摸点,并将触摸点通过js交互发送到threejs 中的方法中实现双击。手势实现时需要注意:
增加回调:- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
否则无法实现,这是因为webview内部实现了手势。
接受坐标的双击方法由上面的方法改造就可以了。