From 239627a297705358f73991e71a510c5d684350bd Mon Sep 17 00:00:00 2001 From: 06wj <06wj@163.com> Date: Sat, 25 Aug 2018 20:54:19 +0800 Subject: [PATCH 1/4] Sprite: The result of sprite.raycast add uv --- src/objects/Sprite.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/objects/Sprite.js b/src/objects/Sprite.js index f49892d0cc48f3..82d332047b5e41 100644 --- a/src/objects/Sprite.js +++ b/src/objects/Sprite.js @@ -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'; @@ -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 @@ -93,6 +98,22 @@ Sprite.prototype = Object.assign( Object.create( Object3D.prototype ), { } + var barycoord = 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(); + + } + return function raycast( raycaster, intersects ) { worldScale.setFromMatrixScale( this.matrixWorld ); @@ -114,6 +135,10 @@ 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 ); @@ -121,6 +146,8 @@ Sprite.prototype = Object.assign( Object.create( Object3D.prototype ), { // 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 ) { @@ -138,6 +165,7 @@ Sprite.prototype = Object.assign( Object.create( Object3D.prototype ), { distance: distance, point: intersectPoint.clone(), + uv: uvIntersection( intersectPoint, vA, vB, vC, uvA, uvB, uvC ), face: null, object: this From f6f4d8d3c970ce2a63d02409c83f6ed772ee0aac Mon Sep 17 00:00:00 2001 From: 06wj <06wj@163.com> Date: Wed, 29 Aug 2018 11:50:18 +0800 Subject: [PATCH 2/4] add Triangle.uvIntersection --- src/math/Triangle.js | 24 ++++++++++++++++++++++++ src/objects/Mesh.js | 20 ++------------------ src/objects/Sprite.js | 18 +----------------- 3 files changed, 27 insertions(+), 35 deletions(-) diff --git a/src/math/Triangle.js b/src/math/Triangle.js index d9c7281394118a..e305289c64d25e 100644 --- a/src/math/Triangle.js +++ b/src/math/Triangle.js @@ -1,3 +1,4 @@ +import { Vector2 } from './Vector2.js'; import { Vector3 } from './Vector3.js'; /** @@ -94,6 +95,29 @@ Object.assign( Triangle, { }(), + uvIntersection: function () { + + var barycoord = new Vector3(); + var v1 = new Vector2(); + var v2 = new Vector2(); + var v3 = new Vector2(); + + return function uvIntersection( point, p1, p2, p3, uv1, uv2, uv3, result ) { + + this.getBarycoord( point, p1, p2, p3, barycoord ); + + v1.copy( uv1 ).multiplyScalar( barycoord.x ); + v2.copy( uv2 ).multiplyScalar( barycoord.y ); + v3.copy( uv3 ).multiplyScalar( barycoord.z ); + + v1.add( v2 ).add( v3 ); + + return result.copy( v1 ); + + }; + + }(), + containsPoint: function () { var v1 = new Vector3(); diff --git a/src/objects/Mesh.js b/src/objects/Mesh.js index 4e84ac134285a0..255e96f6ddeb7c 100644 --- a/src/objects/Mesh.js +++ b/src/objects/Mesh.js @@ -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; @@ -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.uvIntersection( intersectionPoint, vA, vB, vC, uvA, uvB, uvC, new Vector2() ); } @@ -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.uvIntersection( intersectionPoint, fvA, fvB, fvC, uvA, uvB, uvC, new Vector2() ); } diff --git a/src/objects/Sprite.js b/src/objects/Sprite.js index 82d332047b5e41..156410593342a9 100644 --- a/src/objects/Sprite.js +++ b/src/objects/Sprite.js @@ -98,22 +98,6 @@ Sprite.prototype = Object.assign( Object.create( Object3D.prototype ), { } - var barycoord = 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(); - - } - return function raycast( raycaster, intersects ) { worldScale.setFromMatrixScale( this.matrixWorld ); @@ -165,7 +149,7 @@ Sprite.prototype = Object.assign( Object.create( Object3D.prototype ), { distance: distance, point: intersectPoint.clone(), - uv: uvIntersection( intersectPoint, vA, vB, vC, uvA, uvB, uvC ), + uv: Triangle.uvIntersection( intersectPoint, vA, vB, vC, uvA, uvB, uvC, new Vector2() ), face: null, object: this From 52b3ef42ec3ba1573616685c876dc8fb1b2a9b9a Mon Sep 17 00:00:00 2001 From: 06wj <06wj@163.com> Date: Wed, 29 Aug 2018 12:27:43 +0800 Subject: [PATCH 3/4] update Triangle.uvIntersection --- src/math/Triangle.js | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/src/math/Triangle.js b/src/math/Triangle.js index e305289c64d25e..7aa85c02f03a7a 100644 --- a/src/math/Triangle.js +++ b/src/math/Triangle.js @@ -95,6 +95,20 @@ Object.assign( Triangle, { }(), + containsPoint: function () { + + var v1 = new Vector3(); + + return function containsPoint( point, a, b, c ) { + + Triangle.getBarycoord( point, a, b, c, v1 ); + + return ( v1.x >= 0 ) && ( v1.y >= 0 ) && ( ( v1.x + v1.y ) <= 1 ); + + }; + + }(), + uvIntersection: function () { var barycoord = new Vector3(); @@ -116,20 +130,6 @@ Object.assign( Triangle, { }; - }(), - - containsPoint: function () { - - var v1 = new Vector3(); - - return function containsPoint( point, a, b, c ) { - - Triangle.getBarycoord( point, a, b, c, v1 ); - - return ( v1.x >= 0 ) && ( v1.y >= 0 ) && ( ( v1.x + v1.y ) <= 1 ); - - }; - }() } ); @@ -232,6 +232,12 @@ Object.assign( Triangle.prototype, { }, + uvIntersection: function ( point, uv1, uv2, uv3, result ) { + + return Triangle.uvIntersection( point, this.a, this.b, this.c, uv1, uv2, uv3, result ); + + }, + intersectsBox: function ( box ) { return box.intersectsTriangle( this ); From c48d55fc3a11004edb2a8837373f77661cc838b8 Mon Sep 17 00:00:00 2001 From: 06wj <06wj@163.com> Date: Thu, 30 Aug 2018 10:21:48 +0800 Subject: [PATCH 4/4] rename Triangle.uvIntersection to getUV --- src/math/Triangle.js | 23 +++++++++-------------- src/objects/Mesh.js | 4 ++-- src/objects/Sprite.js | 2 +- 3 files changed, 12 insertions(+), 17 deletions(-) diff --git a/src/math/Triangle.js b/src/math/Triangle.js index 7aa85c02f03a7a..328604d31b7810 100644 --- a/src/math/Triangle.js +++ b/src/math/Triangle.js @@ -1,4 +1,3 @@ -import { Vector2 } from './Vector2.js'; import { Vector3 } from './Vector3.js'; /** @@ -109,24 +108,20 @@ Object.assign( Triangle, { }(), - uvIntersection: function () { + getUV: function () { var barycoord = new Vector3(); - var v1 = new Vector2(); - var v2 = new Vector2(); - var v3 = new Vector2(); - return function uvIntersection( point, p1, p2, p3, uv1, uv2, uv3, result ) { + return function getUV( point, p1, p2, p3, uv1, uv2, uv3, target ) { this.getBarycoord( point, p1, p2, p3, barycoord ); - v1.copy( uv1 ).multiplyScalar( barycoord.x ); - v2.copy( uv2 ).multiplyScalar( barycoord.y ); - v3.copy( uv3 ).multiplyScalar( barycoord.z ); + target.set( 0, 0 ); + target.addScaledVector( uv1, barycoord.x ); + target.addScaledVector( uv2, barycoord.y ); + target.addScaledVector( uv3, barycoord.z ); - v1.add( v2 ).add( v3 ); - - return result.copy( v1 ); + return target; }; @@ -232,9 +227,9 @@ Object.assign( Triangle.prototype, { }, - uvIntersection: function ( point, uv1, uv2, uv3, result ) { + getUV: function ( point, uv1, uv2, uv3, result ) { - return Triangle.uvIntersection( point, this.a, this.b, this.c, uv1, uv2, uv3, result ); + return Triangle.getUV( point, this.a, this.b, this.c, uv1, uv2, uv3, result ); }, diff --git a/src/objects/Mesh.js b/src/objects/Mesh.js index 255e96f6ddeb7c..f796876dfdcdc1 100644 --- a/src/objects/Mesh.js +++ b/src/objects/Mesh.js @@ -190,7 +190,7 @@ Mesh.prototype = Object.assign( Object.create( Object3D.prototype ), { uvB.fromBufferAttribute( uv, b ); uvC.fromBufferAttribute( uv, c ); - intersection.uv = Triangle.uvIntersection( intersectionPoint, vA, vB, vC, uvA, uvB, uvC, new Vector2() ); + intersection.uv = Triangle.getUV( intersectionPoint, vA, vB, vC, uvA, uvB, uvC, new Vector2() ); } @@ -432,7 +432,7 @@ Mesh.prototype = Object.assign( Object.create( Object3D.prototype ), { uvB.copy( uvs_f[ 1 ] ); uvC.copy( uvs_f[ 2 ] ); - intersection.uv = Triangle.uvIntersection( intersectionPoint, fvA, fvB, fvC, uvA, uvB, uvC, new Vector2() ); + intersection.uv = Triangle.getUV( intersectionPoint, fvA, fvB, fvC, uvA, uvB, uvC, new Vector2() ); } diff --git a/src/objects/Sprite.js b/src/objects/Sprite.js index 156410593342a9..e653fc1c464f1a 100644 --- a/src/objects/Sprite.js +++ b/src/objects/Sprite.js @@ -149,7 +149,7 @@ Sprite.prototype = Object.assign( Object.create( Object3D.prototype ), { distance: distance, point: intersectPoint.clone(), - uv: Triangle.uvIntersection( intersectPoint, vA, vB, vC, uvA, uvB, uvC, new Vector2() ), + uv: Triangle.getUV( intersectPoint, vA, vB, vC, uvA, uvB, uvC, new Vector2() ), face: null, object: this