123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363 |
- import {
- BufferGeometry,
- Float32BufferAttribute,
- Matrix4,
- Vector3
- } from '../../../build/three.module.js';
- class DecalGeometry extends BufferGeometry {
- constructor( mesh, position, orientation, size ) {
- super();
-
- const vertices = [];
- const normals = [];
- const uvs = [];
-
- const plane = new Vector3();
-
- const projectorMatrix = new Matrix4();
- projectorMatrix.makeRotationFromEuler( orientation );
- projectorMatrix.setPosition( position );
- const projectorMatrixInverse = new Matrix4();
- projectorMatrixInverse.copy( projectorMatrix ).invert();
-
- generate();
-
- this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
- this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
- this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
- function generate() {
- let decalVertices = [];
- const vertex = new Vector3();
- const normal = new Vector3();
-
- if ( mesh.geometry.isGeometry === true ) {
- console.error( 'THREE.DecalGeometry no longer supports THREE.Geometry. Use BufferGeometry instead.' );
- return;
- }
- const geometry = mesh.geometry;
- const positionAttribute = geometry.attributes.position;
- const normalAttribute = geometry.attributes.normal;
-
-
-
-
- if ( geometry.index !== null ) {
-
- const index = geometry.index;
- for ( let i = 0; i < index.count; i ++ ) {
- vertex.fromBufferAttribute( positionAttribute, index.getX( i ) );
- normal.fromBufferAttribute( normalAttribute, index.getX( i ) );
- pushDecalVertex( decalVertices, vertex, normal );
- }
- } else {
-
- for ( let i = 0; i < positionAttribute.count; i ++ ) {
- vertex.fromBufferAttribute( positionAttribute, i );
- normal.fromBufferAttribute( normalAttribute, i );
- pushDecalVertex( decalVertices, vertex, normal );
- }
- }
-
- decalVertices = clipGeometry( decalVertices, plane.set( 1, 0, 0 ) );
- decalVertices = clipGeometry( decalVertices, plane.set( - 1, 0, 0 ) );
- decalVertices = clipGeometry( decalVertices, plane.set( 0, 1, 0 ) );
- decalVertices = clipGeometry( decalVertices, plane.set( 0, - 1, 0 ) );
- decalVertices = clipGeometry( decalVertices, plane.set( 0, 0, 1 ) );
- decalVertices = clipGeometry( decalVertices, plane.set( 0, 0, - 1 ) );
-
- for ( let i = 0; i < decalVertices.length; i ++ ) {
- const decalVertex = decalVertices[ i ];
-
- uvs.push(
- 0.5 + ( decalVertex.position.x / size.x ),
- 0.5 + ( decalVertex.position.y / size.y )
- );
-
- decalVertex.position.applyMatrix4( projectorMatrix );
-
- vertices.push( decalVertex.position.x, decalVertex.position.y, decalVertex.position.z );
- normals.push( decalVertex.normal.x, decalVertex.normal.y, decalVertex.normal.z );
- }
- }
- function pushDecalVertex( decalVertices, vertex, normal ) {
-
- vertex.applyMatrix4( mesh.matrixWorld );
- vertex.applyMatrix4( projectorMatrixInverse );
- normal.transformDirection( mesh.matrixWorld );
- decalVertices.push( new DecalVertex( vertex.clone(), normal.clone() ) );
- }
- function clipGeometry( inVertices, plane ) {
- const outVertices = [];
- const s = 0.5 * Math.abs( size.dot( plane ) );
-
-
- for ( let i = 0; i < inVertices.length; i += 3 ) {
- let total = 0;
- let nV1;
- let nV2;
- let nV3;
- let nV4;
- const d1 = inVertices[ i + 0 ].position.dot( plane ) - s;
- const d2 = inVertices[ i + 1 ].position.dot( plane ) - s;
- const d3 = inVertices[ i + 2 ].position.dot( plane ) - s;
- const v1Out = d1 > 0;
- const v2Out = d2 > 0;
- const v3Out = d3 > 0;
-
- total = ( v1Out ? 1 : 0 ) + ( v2Out ? 1 : 0 ) + ( v3Out ? 1 : 0 );
- switch ( total ) {
- case 0: {
-
- outVertices.push( inVertices[ i ] );
- outVertices.push( inVertices[ i + 1 ] );
- outVertices.push( inVertices[ i + 2 ] );
- break;
- }
- case 1: {
-
- if ( v1Out ) {
- nV1 = inVertices[ i + 1 ];
- nV2 = inVertices[ i + 2 ];
- nV3 = clip( inVertices[ i ], nV1, plane, s );
- nV4 = clip( inVertices[ i ], nV2, plane, s );
- }
- if ( v2Out ) {
- nV1 = inVertices[ i ];
- nV2 = inVertices[ i + 2 ];
- nV3 = clip( inVertices[ i + 1 ], nV1, plane, s );
- nV4 = clip( inVertices[ i + 1 ], nV2, plane, s );
- outVertices.push( nV3 );
- outVertices.push( nV2.clone() );
- outVertices.push( nV1.clone() );
- outVertices.push( nV2.clone() );
- outVertices.push( nV3.clone() );
- outVertices.push( nV4 );
- break;
- }
- if ( v3Out ) {
- nV1 = inVertices[ i ];
- nV2 = inVertices[ i + 1 ];
- nV3 = clip( inVertices[ i + 2 ], nV1, plane, s );
- nV4 = clip( inVertices[ i + 2 ], nV2, plane, s );
- }
- outVertices.push( nV1.clone() );
- outVertices.push( nV2.clone() );
- outVertices.push( nV3 );
- outVertices.push( nV4 );
- outVertices.push( nV3.clone() );
- outVertices.push( nV2.clone() );
- break;
- }
- case 2: {
-
- if ( ! v1Out ) {
- nV1 = inVertices[ i ].clone();
- nV2 = clip( nV1, inVertices[ i + 1 ], plane, s );
- nV3 = clip( nV1, inVertices[ i + 2 ], plane, s );
- outVertices.push( nV1 );
- outVertices.push( nV2 );
- outVertices.push( nV3 );
- }
- if ( ! v2Out ) {
- nV1 = inVertices[ i + 1 ].clone();
- nV2 = clip( nV1, inVertices[ i + 2 ], plane, s );
- nV3 = clip( nV1, inVertices[ i ], plane, s );
- outVertices.push( nV1 );
- outVertices.push( nV2 );
- outVertices.push( nV3 );
- }
- if ( ! v3Out ) {
- nV1 = inVertices[ i + 2 ].clone();
- nV2 = clip( nV1, inVertices[ i ], plane, s );
- nV3 = clip( nV1, inVertices[ i + 1 ], plane, s );
- outVertices.push( nV1 );
- outVertices.push( nV2 );
- outVertices.push( nV3 );
- }
- break;
- }
- case 3: {
-
- break;
- }
- }
- }
- return outVertices;
- }
- function clip( v0, v1, p, s ) {
- const d0 = v0.position.dot( p ) - s;
- const d1 = v1.position.dot( p ) - s;
- const s0 = d0 / ( d0 - d1 );
- const v = new DecalVertex(
- new Vector3(
- v0.position.x + s0 * ( v1.position.x - v0.position.x ),
- v0.position.y + s0 * ( v1.position.y - v0.position.y ),
- v0.position.z + s0 * ( v1.position.z - v0.position.z )
- ),
- new Vector3(
- v0.normal.x + s0 * ( v1.normal.x - v0.normal.x ),
- v0.normal.y + s0 * ( v1.normal.y - v0.normal.y ),
- v0.normal.z + s0 * ( v1.normal.z - v0.normal.z )
- )
- );
-
-
- return v;
- }
- }
- }
- class DecalVertex {
- constructor( position, normal ) {
- this.position = position;
- this.normal = normal;
- }
- clone() {
- return new this.constructor( this.position.clone(), this.normal.clone() );
- }
- }
- export { DecalGeometry, DecalVertex };
|