DecalGeometry.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. ( function () {
  2. /**
  3. * You can use this geometry to create a decal mesh, that serves different kinds of purposes.
  4. * e.g. adding unique details to models, performing dynamic visual environmental changes or covering seams.
  5. *
  6. * Constructor parameter:
  7. *
  8. * mesh — Any mesh object
  9. * position — Position of the decal projector
  10. * orientation — Orientation of the decal projector
  11. * size — Size of the decal projector
  12. *
  13. * reference: http://blog.wolfire.com/2009/06/how-to-project-decals/
  14. *
  15. */
  16. class DecalGeometry extends THREE.BufferGeometry {
  17. constructor( mesh, position, orientation, size ) {
  18. super(); // buffers
  19. const vertices = [];
  20. const normals = [];
  21. const uvs = []; // helpers
  22. const plane = new THREE.Vector3(); // this matrix represents the transformation of the decal projector
  23. const projectorMatrix = new THREE.Matrix4();
  24. projectorMatrix.makeRotationFromEuler( orientation );
  25. projectorMatrix.setPosition( position );
  26. const projectorMatrixInverse = new THREE.Matrix4();
  27. projectorMatrixInverse.copy( projectorMatrix ).invert(); // generate buffers
  28. generate(); // build geometry
  29. this.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  30. this.setAttribute( 'normal', new THREE.Float32BufferAttribute( normals, 3 ) );
  31. this.setAttribute( 'uv', new THREE.Float32BufferAttribute( uvs, 2 ) );
  32. function generate() {
  33. let decalVertices = [];
  34. const vertex = new THREE.Vector3();
  35. const normal = new THREE.Vector3(); // handle different geometry types
  36. if ( mesh.geometry.isGeometry === true ) {
  37. console.error( 'THREE.DecalGeometry no longer supports THREE.Geometry. Use THREE.BufferGeometry instead.' );
  38. return;
  39. }
  40. const geometry = mesh.geometry;
  41. const positionAttribute = geometry.attributes.position;
  42. const normalAttribute = geometry.attributes.normal; // first, create an array of 'DecalVertex' objects
  43. // three consecutive 'DecalVertex' objects represent a single face
  44. //
  45. // this data structure will be later used to perform the clipping
  46. if ( geometry.index !== null ) {
  47. // indexed THREE.BufferGeometry
  48. const index = geometry.index;
  49. for ( let i = 0; i < index.count; i ++ ) {
  50. vertex.fromBufferAttribute( positionAttribute, index.getX( i ) );
  51. normal.fromBufferAttribute( normalAttribute, index.getX( i ) );
  52. pushDecalVertex( decalVertices, vertex, normal );
  53. }
  54. } else {
  55. // non-indexed THREE.BufferGeometry
  56. for ( let i = 0; i < positionAttribute.count; i ++ ) {
  57. vertex.fromBufferAttribute( positionAttribute, i );
  58. normal.fromBufferAttribute( normalAttribute, i );
  59. pushDecalVertex( decalVertices, vertex, normal );
  60. }
  61. } // second, clip the geometry so that it doesn't extend out from the projector
  62. decalVertices = clipGeometry( decalVertices, plane.set( 1, 0, 0 ) );
  63. decalVertices = clipGeometry( decalVertices, plane.set( - 1, 0, 0 ) );
  64. decalVertices = clipGeometry( decalVertices, plane.set( 0, 1, 0 ) );
  65. decalVertices = clipGeometry( decalVertices, plane.set( 0, - 1, 0 ) );
  66. decalVertices = clipGeometry( decalVertices, plane.set( 0, 0, 1 ) );
  67. decalVertices = clipGeometry( decalVertices, plane.set( 0, 0, - 1 ) ); // third, generate final vertices, normals and uvs
  68. for ( let i = 0; i < decalVertices.length; i ++ ) {
  69. const decalVertex = decalVertices[ i ]; // create texture coordinates (we are still in projector space)
  70. uvs.push( 0.5 + decalVertex.position.x / size.x, 0.5 + decalVertex.position.y / size.y ); // transform the vertex back to world space
  71. decalVertex.position.applyMatrix4( projectorMatrix ); // now create vertex and normal buffer data
  72. vertices.push( decalVertex.position.x, decalVertex.position.y, decalVertex.position.z );
  73. normals.push( decalVertex.normal.x, decalVertex.normal.y, decalVertex.normal.z );
  74. }
  75. }
  76. function pushDecalVertex( decalVertices, vertex, normal ) {
  77. // transform the vertex to world space, then to projector space
  78. vertex.applyMatrix4( mesh.matrixWorld );
  79. vertex.applyMatrix4( projectorMatrixInverse );
  80. normal.transformDirection( mesh.matrixWorld );
  81. decalVertices.push( new DecalVertex( vertex.clone(), normal.clone() ) );
  82. }
  83. function clipGeometry( inVertices, plane ) {
  84. const outVertices = [];
  85. const s = 0.5 * Math.abs( size.dot( plane ) ); // a single iteration clips one face,
  86. // which consists of three consecutive 'DecalVertex' objects
  87. for ( let i = 0; i < inVertices.length; i += 3 ) {
  88. let total = 0;
  89. let nV1;
  90. let nV2;
  91. let nV3;
  92. let nV4;
  93. const d1 = inVertices[ i + 0 ].position.dot( plane ) - s;
  94. const d2 = inVertices[ i + 1 ].position.dot( plane ) - s;
  95. const d3 = inVertices[ i + 2 ].position.dot( plane ) - s;
  96. const v1Out = d1 > 0;
  97. const v2Out = d2 > 0;
  98. const v3Out = d3 > 0; // calculate, how many vertices of the face lie outside of the clipping plane
  99. total = ( v1Out ? 1 : 0 ) + ( v2Out ? 1 : 0 ) + ( v3Out ? 1 : 0 );
  100. switch ( total ) {
  101. case 0:
  102. {
  103. // the entire face lies inside of the plane, no clipping needed
  104. outVertices.push( inVertices[ i ] );
  105. outVertices.push( inVertices[ i + 1 ] );
  106. outVertices.push( inVertices[ i + 2 ] );
  107. break;
  108. }
  109. case 1:
  110. {
  111. // one vertex lies outside of the plane, perform clipping
  112. if ( v1Out ) {
  113. nV1 = inVertices[ i + 1 ];
  114. nV2 = inVertices[ i + 2 ];
  115. nV3 = clip( inVertices[ i ], nV1, plane, s );
  116. nV4 = clip( inVertices[ i ], nV2, plane, s );
  117. }
  118. if ( v2Out ) {
  119. nV1 = inVertices[ i ];
  120. nV2 = inVertices[ i + 2 ];
  121. nV3 = clip( inVertices[ i + 1 ], nV1, plane, s );
  122. nV4 = clip( inVertices[ i + 1 ], nV2, plane, s );
  123. outVertices.push( nV3 );
  124. outVertices.push( nV2.clone() );
  125. outVertices.push( nV1.clone() );
  126. outVertices.push( nV2.clone() );
  127. outVertices.push( nV3.clone() );
  128. outVertices.push( nV4 );
  129. break;
  130. }
  131. if ( v3Out ) {
  132. nV1 = inVertices[ i ];
  133. nV2 = inVertices[ i + 1 ];
  134. nV3 = clip( inVertices[ i + 2 ], nV1, plane, s );
  135. nV4 = clip( inVertices[ i + 2 ], nV2, plane, s );
  136. }
  137. outVertices.push( nV1.clone() );
  138. outVertices.push( nV2.clone() );
  139. outVertices.push( nV3 );
  140. outVertices.push( nV4 );
  141. outVertices.push( nV3.clone() );
  142. outVertices.push( nV2.clone() );
  143. break;
  144. }
  145. case 2:
  146. {
  147. // two vertices lies outside of the plane, perform clipping
  148. if ( ! v1Out ) {
  149. nV1 = inVertices[ i ].clone();
  150. nV2 = clip( nV1, inVertices[ i + 1 ], plane, s );
  151. nV3 = clip( nV1, inVertices[ i + 2 ], plane, s );
  152. outVertices.push( nV1 );
  153. outVertices.push( nV2 );
  154. outVertices.push( nV3 );
  155. }
  156. if ( ! v2Out ) {
  157. nV1 = inVertices[ i + 1 ].clone();
  158. nV2 = clip( nV1, inVertices[ i + 2 ], plane, s );
  159. nV3 = clip( nV1, inVertices[ i ], plane, s );
  160. outVertices.push( nV1 );
  161. outVertices.push( nV2 );
  162. outVertices.push( nV3 );
  163. }
  164. if ( ! v3Out ) {
  165. nV1 = inVertices[ i + 2 ].clone();
  166. nV2 = clip( nV1, inVertices[ i ], plane, s );
  167. nV3 = clip( nV1, inVertices[ i + 1 ], plane, s );
  168. outVertices.push( nV1 );
  169. outVertices.push( nV2 );
  170. outVertices.push( nV3 );
  171. }
  172. break;
  173. }
  174. case 3:
  175. {
  176. // the entire face lies outside of the plane, so let's discard the corresponding vertices
  177. break;
  178. }
  179. }
  180. }
  181. return outVertices;
  182. }
  183. function clip( v0, v1, p, s ) {
  184. const d0 = v0.position.dot( p ) - s;
  185. const d1 = v1.position.dot( p ) - s;
  186. const s0 = d0 / ( d0 - d1 );
  187. const v = new DecalVertex( new THREE.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 THREE.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 ) ) ); // need to clip more values (texture coordinates)? do it this way:
  188. // intersectpoint.value = a.value + s * ( b.value - a.value );
  189. return v;
  190. }
  191. }
  192. } // helper
  193. class DecalVertex {
  194. constructor( position, normal ) {
  195. this.position = position;
  196. this.normal = normal;
  197. }
  198. clone() {
  199. return new this.constructor( this.position.clone(), this.normal.clone() );
  200. }
  201. }
  202. THREE.DecalGeometry = DecalGeometry;
  203. THREE.DecalVertex = DecalVertex;
  204. } )();