DecalGeometry.js 8.3 KB

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