DecalGeometry.js 8.5 KB

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