DecalGeometry.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. import {
  2. BufferGeometry,
  3. Euler,
  4. Float32BufferAttribute,
  5. Matrix3,
  6. Matrix4,
  7. Mesh,
  8. Vector3
  9. } from 'three';
  10. /**
  11. * This class can be used to create a decal mesh that serves different kinds of purposes e.g.
  12. * adding unique details to models, performing dynamic visual environmental changes or covering seams.
  13. *
  14. * Please not that decal projections can be distorted when used around corners. More information at
  15. * this GitHub issue: [Decal projections without distortions]{@link https://github.com/mrdoob/three.js/issues/21187}.
  16. *
  17. * Reference: [How to project decals]{@link http://blog.wolfire.com/2009/06/how-to-project-decals/}
  18. *
  19. * ```js
  20. * const geometry = new DecalGeometry( mesh, position, orientation, size );
  21. * const material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
  22. * const mesh = new THREE.Mesh( geometry, material );
  23. * scene.add( mesh );
  24. * ```
  25. *
  26. * @augments BufferGeometry
  27. * @three_import import { DecalGeometry } from 'three/addons/geometries/DecalGeometry.js';
  28. */
  29. class DecalGeometry extends BufferGeometry {
  30. /**
  31. * Constructs a new decal geometry.
  32. *
  33. * @param {Mesh} [mesh] - The base mesh the decal should be projected on.
  34. * @param {Vector3} [position] - The position of the decal projector.
  35. * @param {Euler} [orientation] - The orientation of the decal projector.
  36. * @param {Vector3} [size] - Tje scale of the decal projector.
  37. */
  38. constructor( mesh = new Mesh(), position = new Vector3(), orientation = new Euler(), size = new Vector3( 1, 1, 1 ) ) {
  39. super();
  40. // buffers
  41. const vertices = [];
  42. const normals = [];
  43. const uvs = [];
  44. // helpers
  45. const plane = new Vector3();
  46. const normalMatrix = new Matrix3().getNormalMatrix( mesh.matrixWorld );
  47. // this matrix represents the transformation of the decal projector
  48. const projectorMatrix = new Matrix4();
  49. projectorMatrix.makeRotationFromEuler( orientation );
  50. projectorMatrix.setPosition( position );
  51. const projectorMatrixInverse = new Matrix4();
  52. projectorMatrixInverse.copy( projectorMatrix ).invert();
  53. // generate buffers
  54. generate();
  55. // build geometry
  56. this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  57. this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
  58. if ( normals.length > 0 ) {
  59. this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
  60. }
  61. //
  62. function generate() {
  63. let decalVertices = [];
  64. const vertex = new Vector3();
  65. const normal = new Vector3();
  66. // handle different geometry types
  67. const geometry = mesh.geometry;
  68. const positionAttribute = geometry.attributes.position;
  69. const normalAttribute = geometry.attributes.normal;
  70. // first, create an array of 'DecalVertex' objects
  71. // three consecutive 'DecalVertex' objects represent a single face
  72. //
  73. // this data structure will be later used to perform the clipping
  74. if ( geometry.index !== null ) {
  75. // indexed BufferGeometry
  76. const index = geometry.index;
  77. for ( let i = 0; i < index.count; i ++ ) {
  78. vertex.fromBufferAttribute( positionAttribute, index.getX( i ) );
  79. if ( normalAttribute ) {
  80. normal.fromBufferAttribute( normalAttribute, index.getX( i ) );
  81. pushDecalVertex( decalVertices, vertex, normal );
  82. } else {
  83. pushDecalVertex( decalVertices, vertex );
  84. }
  85. }
  86. } else {
  87. if ( positionAttribute === undefined ) return; // empty geometry
  88. // non-indexed BufferGeometry
  89. for ( let i = 0; i < positionAttribute.count; i ++ ) {
  90. vertex.fromBufferAttribute( positionAttribute, i );
  91. if ( normalAttribute ) {
  92. normal.fromBufferAttribute( normalAttribute, i );
  93. pushDecalVertex( decalVertices, vertex, normal );
  94. } else {
  95. pushDecalVertex( decalVertices, vertex );
  96. }
  97. }
  98. }
  99. // second, clip the geometry so that it doesn't extend out from the projector
  100. decalVertices = clipGeometry( decalVertices, plane.set( 1, 0, 0 ) );
  101. decalVertices = clipGeometry( decalVertices, plane.set( - 1, 0, 0 ) );
  102. decalVertices = clipGeometry( decalVertices, plane.set( 0, 1, 0 ) );
  103. decalVertices = clipGeometry( decalVertices, plane.set( 0, - 1, 0 ) );
  104. decalVertices = clipGeometry( decalVertices, plane.set( 0, 0, 1 ) );
  105. decalVertices = clipGeometry( decalVertices, plane.set( 0, 0, - 1 ) );
  106. // third, generate final vertices, normals and uvs
  107. for ( let i = 0; i < decalVertices.length; i ++ ) {
  108. const decalVertex = decalVertices[ i ];
  109. // create texture coordinates (we are still in projector space)
  110. uvs.push(
  111. 0.5 + ( decalVertex.position.x / size.x ),
  112. 0.5 + ( decalVertex.position.y / size.y )
  113. );
  114. // transform the vertex back to world space
  115. decalVertex.position.applyMatrix4( projectorMatrix );
  116. // now create vertex and normal buffer data
  117. vertices.push( decalVertex.position.x, decalVertex.position.y, decalVertex.position.z );
  118. if ( decalVertex.normal !== null ) {
  119. normals.push( decalVertex.normal.x, decalVertex.normal.y, decalVertex.normal.z );
  120. }
  121. }
  122. }
  123. function pushDecalVertex( decalVertices, vertex, normal = null ) {
  124. // transform the vertex to world space, then to projector space
  125. vertex.applyMatrix4( mesh.matrixWorld );
  126. vertex.applyMatrix4( projectorMatrixInverse );
  127. if ( normal ) {
  128. normal.applyNormalMatrix( normalMatrix );
  129. decalVertices.push( new DecalVertex( vertex.clone(), normal.clone() ) );
  130. } else {
  131. decalVertices.push( new DecalVertex( vertex.clone() ) );
  132. }
  133. }
  134. function clipGeometry( inVertices, plane ) {
  135. const outVertices = [];
  136. const s = 0.5 * Math.abs( size.dot( plane ) );
  137. // a single iteration clips one face,
  138. // which consists of three consecutive 'DecalVertex' objects
  139. for ( let i = 0; i < inVertices.length; i += 3 ) {
  140. let total = 0;
  141. let nV1;
  142. let nV2;
  143. let nV3;
  144. let nV4;
  145. const d1 = inVertices[ i + 0 ].position.dot( plane ) - s;
  146. const d2 = inVertices[ i + 1 ].position.dot( plane ) - s;
  147. const d3 = inVertices[ i + 2 ].position.dot( plane ) - s;
  148. const v1Out = d1 > 0;
  149. const v2Out = d2 > 0;
  150. const v3Out = d3 > 0;
  151. // calculate, how many vertices of the face lie outside of the clipping plane
  152. total = ( v1Out ? 1 : 0 ) + ( v2Out ? 1 : 0 ) + ( v3Out ? 1 : 0 );
  153. switch ( total ) {
  154. case 0: {
  155. // the entire face lies inside of the plane, no clipping needed
  156. outVertices.push( inVertices[ i ] );
  157. outVertices.push( inVertices[ i + 1 ] );
  158. outVertices.push( inVertices[ i + 2 ] );
  159. break;
  160. }
  161. case 1: {
  162. // one vertex lies outside of the plane, perform clipping
  163. if ( v1Out ) {
  164. nV1 = inVertices[ i + 1 ];
  165. nV2 = inVertices[ i + 2 ];
  166. nV3 = clip( inVertices[ i ], nV1, plane, s );
  167. nV4 = clip( inVertices[ i ], nV2, plane, s );
  168. }
  169. if ( v2Out ) {
  170. nV1 = inVertices[ i ];
  171. nV2 = inVertices[ i + 2 ];
  172. nV3 = clip( inVertices[ i + 1 ], nV1, plane, s );
  173. nV4 = clip( inVertices[ i + 1 ], nV2, plane, s );
  174. outVertices.push( nV3 );
  175. outVertices.push( nV2.clone() );
  176. outVertices.push( nV1.clone() );
  177. outVertices.push( nV2.clone() );
  178. outVertices.push( nV3.clone() );
  179. outVertices.push( nV4 );
  180. break;
  181. }
  182. if ( v3Out ) {
  183. nV1 = inVertices[ i ];
  184. nV2 = inVertices[ i + 1 ];
  185. nV3 = clip( inVertices[ i + 2 ], nV1, plane, s );
  186. nV4 = clip( inVertices[ i + 2 ], nV2, plane, s );
  187. }
  188. outVertices.push( nV1.clone() );
  189. outVertices.push( nV2.clone() );
  190. outVertices.push( nV3 );
  191. outVertices.push( nV4 );
  192. outVertices.push( nV3.clone() );
  193. outVertices.push( nV2.clone() );
  194. break;
  195. }
  196. case 2: {
  197. // two vertices lies outside of the plane, perform clipping
  198. if ( ! v1Out ) {
  199. nV1 = inVertices[ i ].clone();
  200. nV2 = clip( nV1, inVertices[ i + 1 ], plane, s );
  201. nV3 = clip( nV1, inVertices[ i + 2 ], plane, s );
  202. outVertices.push( nV1 );
  203. outVertices.push( nV2 );
  204. outVertices.push( nV3 );
  205. }
  206. if ( ! v2Out ) {
  207. nV1 = inVertices[ i + 1 ].clone();
  208. nV2 = clip( nV1, inVertices[ i + 2 ], plane, s );
  209. nV3 = clip( nV1, inVertices[ i ], plane, s );
  210. outVertices.push( nV1 );
  211. outVertices.push( nV2 );
  212. outVertices.push( nV3 );
  213. }
  214. if ( ! v3Out ) {
  215. nV1 = inVertices[ i + 2 ].clone();
  216. nV2 = clip( nV1, inVertices[ i ], plane, s );
  217. nV3 = clip( nV1, inVertices[ i + 1 ], plane, s );
  218. outVertices.push( nV1 );
  219. outVertices.push( nV2 );
  220. outVertices.push( nV3 );
  221. }
  222. break;
  223. }
  224. case 3: {
  225. // the entire face lies outside of the plane, so let's discard the corresponding vertices
  226. break;
  227. }
  228. }
  229. }
  230. return outVertices;
  231. }
  232. function clip( v0, v1, p, s ) {
  233. const d0 = v0.position.dot( p ) - s;
  234. const d1 = v1.position.dot( p ) - s;
  235. const s0 = d0 / ( d0 - d1 );
  236. const position = new Vector3(
  237. v0.position.x + s0 * ( v1.position.x - v0.position.x ),
  238. v0.position.y + s0 * ( v1.position.y - v0.position.y ),
  239. v0.position.z + s0 * ( v1.position.z - v0.position.z )
  240. );
  241. let normal = null;
  242. if ( v0.normal !== null && v1.normal !== null ) {
  243. normal = new Vector3(
  244. v0.normal.x + s0 * ( v1.normal.x - v0.normal.x ),
  245. v0.normal.y + s0 * ( v1.normal.y - v0.normal.y ),
  246. v0.normal.z + s0 * ( v1.normal.z - v0.normal.z )
  247. );
  248. }
  249. const v = new DecalVertex( position, normal );
  250. // need to clip more values (texture coordinates)? do it this way:
  251. // intersectpoint.value = a.value + s * ( b.value - a.value );
  252. return v;
  253. }
  254. }
  255. }
  256. // helper
  257. class DecalVertex {
  258. constructor( position, normal = null ) {
  259. this.position = position;
  260. this.normal = normal;
  261. }
  262. clone() {
  263. const position = this.position.clone();
  264. const normal = ( this.normal !== null ) ? this.normal.clone() : null;
  265. return new this.constructor( position, normal );
  266. }
  267. }
  268. export { DecalGeometry, DecalVertex };