Trying to use an Expo Three Raycaster but intersects are empty or wrong
See original GitHub issueI’m trying to intersect meshes with Three Raycaster but for some reason the default array of intersects is defaulting to 10 meshes and when I “touch” the intersects array becomes empty. Does Raycaster not work with your version of THREE? I investigated the NPM package, specifically it is loading THREE but when I look closer I don’t see an instance of THREE inside npm_modules
. Here’s the code
Snack: https://snack.expo.io/@adriaanbalt/raycaster-using-expo-three
import Expo from "expo";
import React from "react";
import { Dimensions, View, Animated, PanResponder } from "react-native";
import ExpoTHREE, { THREE } from "expo-three"; // 3.0.0-alpha.4
import MeshContainer from "./MeshContainer";
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
pan: new Animated.ValueXY(),
mouse: new THREE.Vector2(),
};
THREE.suppressExpoWarnings(true);
}
componentWillMount() {
this._val = { x: 0, y: 0 };
this.state.pan.addListener(value => (this._val = value));
this.panResponder = PanResponder.create({
onStartShouldSetPanResponder: (e, gesture) => true,
onPanResponderGrant: (e, gesture) => {
this.state.pan.setOffset({
x: this._val.x,
y: this._val.y
});
this.state.pan.setValue({ x: -1, y: -1 });
},
onPanResponderMove: ({ nativeEvent }, gestureState) => {
this.state.mouse.x = nativeEvent.locationX;
this.state.mouse.y = nativeEvent.locationY;
},
});
}
render() {
return (
<View
{...this.panResponder.panHandlers}
style={[
{
width: Dimensions.get("window").width,
height: Dimensions.get("window").height
}
]}
>
<Expo.GLView
style={{ flex: 1 }}
onContextCreate={this._onGLContextCreate}
/>
</View>
);
}
_onGLContextCreate = async gl => {
const scene = new THREE.Scene();
const raycaster = new THREE.Raycaster(); // is this correct?
const camera = new THREE.PerspectiveCamera(
100,
gl.drawingBufferWidth / gl.drawingBufferHeight,
1,
10000
);
camera.position.z = 2;
const renderer = new ExpoTHREE.Renderer({ gl });
renderer.setSize(gl.drawingBufferWidth, gl.drawingBufferHeight);
const Container = new MeshContainer();
const containerMesh = Container.getMesh();
scene.add(containerMesh);
let objects = []
objects.push(containerMesh);
let INTERSECTED;
const animate = p => {
requestAnimationFrame(animate);
raycaster.setFromCamera( this.state.mouse, camera );
let intersects = raycaster.intersectObjects( objects );
// let intersects = raycaster.intersectObjects( scene.children ); // this doesn't seem to make any difference
console.log( 'intersects length: ', intersects.length ); // defaults to 10 before touch and after touch becomes 0
if ( intersects.length > 0 ) {
if ( INTERSECTED != intersects[ 0 ].object ) {
INTERSECTED = intersects[ 0 ].object;
}
} else {
INTERSECTED = null;
}
renderer.render(scene, camera);
gl.endFrameEXP();
};
animate();
};
}
MeshContainer.js
import React from "react";
import ExpoTHREE, { THREE } from "expo-three"; // 3.0.0-alpha.4
export default class MeshContainer extends React.Component {
constructor() {
super();
let geometry = new THREE.CircleGeometry(.25, 10);
let material = new THREE.MeshBasicMaterial({
color: 0xFF00FF,
opacity: 1
});
this.mesh = new THREE.Mesh(geometry, material);
}
getMesh() {
// this.mesh.superName = 'MovingLetter'
return this.mesh
}
}
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (1 by maintainers)
Top Results From Across the Web
Three.js raycast produces empty intersects array
I have a 3D Array of cubes which populates and displays fine but when my mouse down function is called, the intersect array...
Read more >Three.js Picking
Probably the most common way of picking is by doing raycasting which means to cast a ray from the mouse through the frustum...
Read more >react-three-fiber-cambrian - npm package | Snyk
This hooks gives you access to all the basic objects that are kept internally, like the default renderer, scene, camera. It also gives...
Read more >Typeerror is not a function typescript - Seba Online
Sep 08, 2021 · But running the script throws a runtime error: TypeError: callback is not a function. register is not a function...
Read more >Why Raycast with GLTF not working - Questions - three.js forum
This is my code i don't know why array of intersects is empty after intersectObjects and raycasting is not working import * as...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This is working perfectly, save lots of time Thanks👍👍👍
?? You shouldn’t directly mutate a components state:
If you want to save the variable without updating the component, then use some other variable: