DecalGeometry.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. import {
  2. BufferGeometry,
  3. Float32BufferAttribute,
  4. Matrix4,
  5. Vector3
  6. } from '../../../build/three.module.js';
  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. if ( mesh.geometry.isGeometry === true ) {
  48. console.error( 'THREE.DecalGeometry no longer supports THREE.Geometry. Use BufferGeometry instead.' );
  49. return;
  50. }
  51. const geometry = mesh.geometry;
  52. const positionAttribute = geometry.attributes.position;
  53. const normalAttribute = geometry.attributes.normal;
  54. // first, create an array of 'DecalVertex' objects
  55. // three consecutive 'DecalVertex' objects represent a single face
  56. //
  57. // this data structure will be later used to perform the clipping
  58. if ( geometry.index !== null ) {
  59. // indexed BufferGeometry
  60. const index = geometry.index;
  61. for ( let i = 0; i < index.count; i ++ ) {
  62. vertex.fromBufferAttribute( positionAttribute, index.getX( i ) );
  63. normal.fromBufferAttribute( normalAttribute, index.getX( i ) );
  64. pushDecalVertex( decalVertices, vertex, normal );
  65. }
  66. } else {
  67. // non-indexed BufferGeometry
  68. for ( let i = 0; i < positionAttribute.count; i ++ ) {
  69. vertex.fromBufferAttribute( positionAttribute, i );
  70. normal.fromBufferAttribute( normalAttribute, i );
  71. pushDecalVertex( decalVertices, vertex, normal );
  72. }
  73. }
  74. // second, clip the geometry so that it doesn't extend out from the projector
  75. decalVertices = clipGeometry( decalVertices, plane.set( 1, 0, 0 ) );
  76. decalVertices = clipGeometry( decalVertices, plane.set( - 1, 0, 0 ) );
  77. decalVertices = clipGeometry( decalVertices, plane.set( 0, 1, 0 ) );
  78. decalVertices = clipGeometry( decalVertices, plane.set( 0, - 1, 0 ) );
  79. decalVertices = clipGeometry( decalVertices, plane.set( 0, 0, 1 ) );
  80. decalVertices = clipGeometry( decalVertices, plane.set( 0, 0, - 1 ) );
  81. // third, generate final vertices, normals and uvs
  82. for ( let i = 0; i < decalVertices.length; i ++ ) {
  83. const decalVertex = decalVertices[ i ];
  84. // create texture coordinates (we are still in projector space)
  85. uvs.push(
  86. 0.5 + ( decalVertex.position.x / size.x ),
  87. 0.5 + ( decalVertex.position.y / size.y )
  88. );
  89. // transform the vertex back to world space
  90. decalVertex.position.applyMatrix4( projectorMatrix );
  91. // now create vertex and normal buffer data
  92. vertices.push( decalVertex.position.x, decalVertex.position.y, decalVertex.position.z );
  93. normals.push( decalVertex.normal.x, decalVertex.normal.y, decalVertex.normal.z );
  94. }
  95. }
  96. function pushDecalVertex( decalVertices, vertex, normal ) {
  97. // transform the vertex to world space, then to projector space
  98. vertex.applyMatrix4( mesh.matrixWorld );
  99. vertex.applyMatrix4( projectorMatrixInverse );
  100. normal.transformDirection( mesh.matrixWorld );
  101. decalVertices.push( new DecalVertex( vertex.clone(), normal.clone() ) );
  102. }
  103. function clipGeometry( inVertices, plane ) {
  104. const outVertices = [];
  105. const s = 0.5 * Math.abs( size.dot( plane ) );
  106. // a single iteration clips one face,
  107. // which consists of three consecutive 'DecalVertex' objects
  108. for ( let i = 0; i < inVertices.length; i += 3 ) {
  109. let total = 0;
  110. let nV1;
  111. let nV2;
  112. let nV3;
  113. let nV4;
  114. const d1 = inVertices[ i + 0 ].position.dot( plane ) - s;
  115. const d2 = inVertices[ i + 1 ].position.dot( plane ) - s;
  116. const d3 = inVertices[ i + 2 ].position.dot( plane ) - s;
  117. const v1Out = d1 > 0;
  118. const v2Out = d2 > 0;
  119. const v3Out = d3 > 0;
  120. // calculate, how many vertices of the face lie outside of the clipping plane
  121. total = ( v1Out ? 1 : 0 ) + ( v2Out ? 1 : 0 ) + ( v3Out ? 1 : 0 );
  122. switch ( total ) {
  123. case 0: {
  124. // the entire face lies inside of the plane, no clipping needed
  125. outVertices.push( inVertices[ i ] );
  126. outVertices.push( inVertices[ i + 1 ] );
  127. outVertices.push( inVertices[ i + 2 ] );
  128. break;
  129. }
  130. case 1: {
  131. // one vertex lies outside of the plane, perform clipping
  132. if ( v1Out ) {
  133. nV1 = inVertices[ i + 1 ];
  134. nV2 = inVertices[ i + 2 ];
  135. nV3 = clip( inVertices[ i ], nV1, plane, s );
  136. nV4 = clip( inVertices[ i ], nV2, plane, s );
  137. }
  138. if ( v2Out ) {
  139. nV1 = inVertices[ i ];
  140. nV2 = inVertices[ i + 2 ];
  141. nV3 = clip( inVertices[ i + 1 ], nV1, plane, s );
  142. nV4 = clip( inVertices[ i + 1 ], nV2, plane, s );
  143. outVertices.push( nV3 );
  144. outVertices.push( nV2.clone() );
  145. outVertices.push( nV1.clone() );
  146. outVertices.push( nV2.clone() );
  147. outVertices.push( nV3.clone() );
  148. outVertices.push( nV4 );
  149. break;
  150. }
  151. if ( v3Out ) {
  152. nV1 = inVertices[ i ];
  153. nV2 = inVertices[ i + 1 ];
  154. nV3 = clip( inVertices[ i + 2 ], nV1, plane, s );
  155. nV4 = clip( inVertices[ i + 2 ], nV2, plane, s );
  156. }
  157. outVertices.push( nV1.clone() );
  158. outVertices.push( nV2.clone() );
  159. outVertices.push( nV3 );
  160. outVertices.push( nV4 );
  161. outVertices.push( nV3.clone() );
  162. outVertices.push( nV2.clone() );
  163. break;
  164. }
  165. case 2: {
  166. // two vertices lies outside of the plane, perform clipping
  167. if ( ! v1Out ) {
  168. nV1 = inVertices[ i ].clone();
  169. nV2 = clip( nV1, inVertices[ i + 1 ], plane, s );
  170. nV3 = clip( nV1, inVertices[ i + 2 ], plane, s );
  171. outVertices.push( nV1 );
  172. outVertices.push( nV2 );
  173. outVertices.push( nV3 );
  174. }
  175. if ( ! v2Out ) {
  176. nV1 = inVertices[ i + 1 ].clone();
  177. nV2 = clip( nV1, inVertices[ i + 2 ], plane, s );
  178. nV3 = clip( nV1, inVertices[ i ], plane, s );
  179. outVertices.push( nV1 );
  180. outVertices.push( nV2 );
  181. outVertices.push( nV3 );
  182. }
  183. if ( ! v3Out ) {
  184. nV1 = inVertices[ i + 2 ].clone();
  185. nV2 = clip( nV1, inVertices[ i ], plane, s );
  186. nV3 = clip( nV1, inVertices[ i + 1 ], plane, s );
  187. outVertices.push( nV1 );
  188. outVertices.push( nV2 );
  189. outVertices.push( nV3 );
  190. }
  191. break;
  192. }
  193. case 3: {
  194. // the entire face lies outside of the plane, so let's discard the corresponding vertices
  195. break;
  196. }
  197. }
  198. }
  199. return outVertices;
  200. }
  201. function clip( v0, v1, p, s ) {
  202. const d0 = v0.position.dot( p ) - s;
  203. const d1 = v1.position.dot( p ) - s;
  204. const s0 = d0 / ( d0 - d1 );
  205. const v = new DecalVertex(
  206. new Vector3(
  207. v0.position.x + s0 * ( v1.position.x - v0.position.x ),
  208. v0.position.y + s0 * ( v1.position.y - v0.position.y ),
  209. v0.position.z + s0 * ( v1.position.z - v0.position.z )
  210. ),
  211. new Vector3(
  212. v0.normal.x + s0 * ( v1.normal.x - v0.normal.x ),
  213. v0.normal.y + s0 * ( v1.normal.y - v0.normal.y ),
  214. v0.normal.z + s0 * ( v1.normal.z - v0.normal.z )
  215. )
  216. );
  217. // need to clip more values (texture coordinates)? do it this way:
  218. // intersectpoint.value = a.value + s * ( b.value - a.value );
  219. return v;
  220. }
  221. }
  222. }
  223. // helper
  224. class DecalVertex {
  225. constructor( position, normal ) {
  226. this.position = position;
  227. this.normal = normal;
  228. }
  229. clone() {
  230. return new this.constructor( this.position.clone(), this.normal.clone() );
  231. }
  232. }
  233. export { DecalGeometry, DecalVertex };