Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

three js buffergeometry raycasting face site:stackoverflow.com

let vertexA;
let vertexB;
let vertexC;
let intersection;
const radius = 3;
const color = new THREE.Color('red');
const positionsAttr = mesh.geometry.attributes.position;
const colorAttr = mesh.geometry.attributes.color;

// on every mouseMove event, do below:

vertexA = new THREE.Vector3();
vertexB = new THREE.Vector3();
vertexC = new THREE.Vector3();
intersection = raycaster.intersectObject(mesh).point;

// function to detect tangent edge
function isEdgeTouched(v1, v2, point, radius) {
  const line = new THREE.Line3();
  const closestPoint = new THREE.Vector3();
  line.set(v1, v2);
  line.closestPointToPoint(point, true, closestPoint);
  return point.distanceTo(closestPoint) < radius;
}

// function to color a face
function colorFace(faceIndex) {
  colorAttr.setXYZ(faceIndex * 3 + 0, color.r, color.g, color.b);
  colorAttr.setXYZ(faceIndex * 3 + 0, color.r, color.g, color.b);
  colorAttr.setXYZ(faceIndex * 3 + 0, color.r, color.g, color.b);
  colorAttr.needsUpdate = true;
}


// iterate over each face, color it if tangent
for (let i=0; i < (positionsAttr.count) /3); i++) {
  vertexA.fromBufferAttribute(positionsAttr, i * 3 + 0);
  vertexB.fromBufferAttribute(positionsAttr, i * 3 + 1);
  vertexC.fromBufferAttribute(positionsAttr, i * 3 + 2);
  if (isEdgeTouched(vertexA, vertexB, point, radius)
    || isEdgeTouched(vertexA, vertexB, point, radius)
    || isEdgeTouched(vertexA, vertexB, point, radius)) {
    colorFace(i);
}
Comment

three js buffergeometry raycasting face site:stackoverflow.com

const sphere = new THREE.Sphere(intersection, radius);

// now checking if each vertex of a face is within sphere
// if all are, then color the face at index i
for (let i=0; i < (positionsAttr.count) /3); i++) {
  vertexA.fromBufferAttribute(positionsAttr, i * 3 + 0);
  vertexB.fromBufferAttribute(positionsAttr, i * 3 + 1);
  vertexC.fromBufferAttribute(positionsAttr, i * 3 + 2);
  if (sphere.containsPoint(vertexA)
    && sphere.containsPoint(vertexA)
    && sphere.containsPoint(vertexA)) {
    colorFace(i);
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to access values from a form event callback 
Javascript :: page slug vuejs 
Javascript :: react enzyme mount ReferenceError: is not defined 
Javascript :: iframe set value on input outside js 
Javascript :: functional not if then else 
Javascript :: bookshelfjs npm 
Javascript :: iron_to_nugget.json 
Javascript :: upload blob to server 
Javascript :: moment add days non destructive 
Javascript :: html working with JSON data 
Javascript :: postgresql create database mac 
Javascript :: angry professor javascript 
Javascript :: res : [ Circular ] nodejs 
Javascript :: Count number of nodes in each connected part of an undirected unweighted graph 
Javascript :: shaynlink 
Javascript :: When you run JavaScript in a Node.Js application, elements in a Node.JS Stack actually executes the JavaScript: 
Javascript :: how to make apk in android studio reac native 
Javascript :: javascript program german to english translation 
Javascript :: use state vs use ref 
Javascript :: fix your timestep javascript 
Javascript :: how to convert json to bootstrap treeview format 
Javascript :: $set 
Javascript :: keyup.enter will work in mac 
Javascript :: react open on different url instead of localhost 
Javascript :: using chalk and morgan together 
Javascript :: node express table view 
Javascript :: inferred type is Array<out GroupItem 
Javascript :: parallaxprovider 
Javascript :: appendchild element once if element presense in js 
Javascript :: difference between Redis and StrictRedis 
ADD CONTENT
Topic
Content
Source link
Name
6+4 =