DecalGeometry.js 8.3 KB

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