DecalGeometry.js 9.8 KB

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