Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/math/Triangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,25 @@ Object.assign( Triangle, {

};

}(),

getUV: function () {

var barycoord = new Vector3();

return function getUV( point, p1, p2, p3, uv1, uv2, uv3, target ) {

this.getBarycoord( point, p1, p2, p3, barycoord );

target.set( 0, 0 );
target.addScaledVector( uv1, barycoord.x );
target.addScaledVector( uv2, barycoord.y );
target.addScaledVector( uv3, barycoord.z );

return target;

};

}()

} );
Expand Down Expand Up @@ -208,6 +227,12 @@ Object.assign( Triangle.prototype, {

},

getUV: function ( point, uv1, uv2, uv3, result ) {

return Triangle.getUV( point, this.a, this.b, this.c, uv1, uv2, uv3, result );

},

intersectsBox: function ( box ) {

return box.intersectsTriangle( this );
Expand Down
20 changes: 2 additions & 18 deletions src/objects/Mesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,25 +140,9 @@ Mesh.prototype = Object.assign( Object.create( Object3D.prototype ), {
var uvB = new Vector2();
var uvC = new Vector2();

var barycoord = new Vector3();

var intersectionPoint = new Vector3();
var intersectionPointWorld = new Vector3();

function uvIntersection( point, p1, p2, p3, uv1, uv2, uv3 ) {

Triangle.getBarycoord( point, p1, p2, p3, barycoord );

uv1.multiplyScalar( barycoord.x );
uv2.multiplyScalar( barycoord.y );
uv3.multiplyScalar( barycoord.z );

uv1.add( uv2 ).add( uv3 );

return uv1.clone();

}

function checkIntersection( object, material, raycaster, ray, pA, pB, pC, point ) {

var intersect;
Expand Down Expand Up @@ -206,7 +190,7 @@ Mesh.prototype = Object.assign( Object.create( Object3D.prototype ), {
uvB.fromBufferAttribute( uv, b );
uvC.fromBufferAttribute( uv, c );

intersection.uv = uvIntersection( intersectionPoint, vA, vB, vC, uvA, uvB, uvC );
intersection.uv = Triangle.getUV( intersectionPoint, vA, vB, vC, uvA, uvB, uvC, new Vector2() );

}

Expand Down Expand Up @@ -448,7 +432,7 @@ Mesh.prototype = Object.assign( Object.create( Object3D.prototype ), {
uvB.copy( uvs_f[ 1 ] );
uvC.copy( uvs_f[ 2 ] );

intersection.uv = uvIntersection( intersectionPoint, fvA, fvB, fvC, uvA, uvB, uvC );
intersection.uv = Triangle.getUV( intersectionPoint, fvA, fvB, fvC, uvA, uvB, uvC, new Vector2() );

}

Expand Down
12 changes: 12 additions & 0 deletions src/objects/Sprite.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { Vector2 } from '../math/Vector2.js';
import { Vector3 } from '../math/Vector3.js';
import { Matrix4 } from '../math/Matrix4.js';
import { Triangle } from '../math/Triangle.js';
import { Object3D } from '../core/Object3D.js';
import { BufferGeometry } from '../core/BufferGeometry.js';
import { InterleavedBuffer } from '../core/InterleavedBuffer.js';
Expand Down Expand Up @@ -66,6 +67,10 @@ Sprite.prototype = Object.assign( Object.create( Object3D.prototype ), {
var vB = new Vector3();
var vC = new Vector3();

var uvA = new Vector2();
var uvB = new Vector2();
var uvC = new Vector2();

function transformVertex( vertexPosition, mvPosition, center, scale, sin, cos ) {

// compute position in camera space
Expand Down Expand Up @@ -114,13 +119,19 @@ Sprite.prototype = Object.assign( Object.create( Object3D.prototype ), {
transformVertex( vB.set( 0.5, - 0.5, 0 ), mvPosition, center, worldScale, sin, cos );
transformVertex( vC.set( 0.5, 0.5, 0 ), mvPosition, center, worldScale, sin, cos );

uvA.set( 0, 0 );
uvB.set( 1, 0 );
uvC.set( 1, 1 );

// check first triangle
var intersect = raycaster.ray.intersectTriangle( vA, vB, vC, false, intersectPoint );

if ( intersect === null ) {

// check second triangle
transformVertex( vB.set( - 0.5, 0.5, 0 ), mvPosition, center, worldScale, sin, cos );
uvB.set( 0, 1 );

intersect = raycaster.ray.intersectTriangle( vA, vC, vB, false, intersectPoint );
if ( intersect === null ) {

Expand All @@ -138,6 +149,7 @@ Sprite.prototype = Object.assign( Object.create( Object3D.prototype ), {

distance: distance,
point: intersectPoint.clone(),
uv: Triangle.getUV( intersectPoint, vA, vB, vC, uvA, uvB, uvC, new Vector2() ),
face: null,
object: this

Expand Down