DecalGeometry.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 decalVertices = [];
  44. var vertex = new Vector3();
  45. var 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. var geometry = mesh.geometry;
  52. var positionAttribute = geometry.attributes.position;
  53. var 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. var index = geometry.index;
  61. for ( 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 ( 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 ( i = 0; i < decalVertices.length; i ++ ) {
  83. var 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. var outVertices = [];
  105. var 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 ( var i = 0; i < inVertices.length; i += 3 ) {
  109. var v1Out, v2Out, v3Out, total = 0;
  110. var nV1, nV2, nV3, nV4;
  111. var d1 = inVertices[ i + 0 ].position.dot( plane ) - s;
  112. var d2 = inVertices[ i + 1 ].position.dot( plane ) - s;
  113. var d3 = inVertices[ i + 2 ].position.dot( plane ) - s;
  114. v1Out = d1 > 0;
  115. v2Out = d2 > 0;
  116. v3Out = d3 > 0;
  117. // calculate, how many vertices of the face lie outside of the clipping plane
  118. total = ( v1Out ? 1 : 0 ) + ( v2Out ? 1 : 0 ) + ( v3Out ? 1 : 0 );
  119. switch ( total ) {
  120. case 0: {
  121. // the entire face lies inside of the plane, no clipping needed
  122. outVertices.push( inVertices[ i ] );
  123. outVertices.push( inVertices[ i + 1 ] );
  124. outVertices.push( inVertices[ i + 2 ] );
  125. break;
  126. }
  127. case 1: {
  128. // one vertex lies outside of the plane, perform clipping
  129. if ( v1Out ) {
  130. nV1 = inVertices[ i + 1 ];
  131. nV2 = inVertices[ i + 2 ];
  132. nV3 = clip( inVertices[ i ], nV1, plane, s );
  133. nV4 = clip( inVertices[ i ], nV2, plane, s );
  134. }
  135. if ( v2Out ) {
  136. nV1 = inVertices[ i ];
  137. nV2 = inVertices[ i + 2 ];
  138. nV3 = clip( inVertices[ i + 1 ], nV1, plane, s );
  139. nV4 = clip( inVertices[ i + 1 ], nV2, plane, s );
  140. outVertices.push( nV3 );
  141. outVertices.push( nV2.clone() );
  142. outVertices.push( nV1.clone() );
  143. outVertices.push( nV2.clone() );
  144. outVertices.push( nV3.clone() );
  145. outVertices.push( nV4 );
  146. break;
  147. }
  148. if ( v3Out ) {
  149. nV1 = inVertices[ i ];
  150. nV2 = inVertices[ i + 1 ];
  151. nV3 = clip( inVertices[ i + 2 ], nV1, plane, s );
  152. nV4 = clip( inVertices[ i + 2 ], nV2, plane, s );
  153. }
  154. outVertices.push( nV1.clone() );
  155. outVertices.push( nV2.clone() );
  156. outVertices.push( nV3 );
  157. outVertices.push( nV4 );
  158. outVertices.push( nV3.clone() );
  159. outVertices.push( nV2.clone() );
  160. break;
  161. }
  162. case 2: {
  163. // two vertices lies outside of the plane, perform clipping
  164. if ( ! v1Out ) {
  165. nV1 = inVertices[ i ].clone();
  166. nV2 = clip( nV1, inVertices[ i + 1 ], plane, s );
  167. nV3 = clip( nV1, inVertices[ i + 2 ], plane, s );
  168. outVertices.push( nV1 );
  169. outVertices.push( nV2 );
  170. outVertices.push( nV3 );
  171. }
  172. if ( ! v2Out ) {
  173. nV1 = inVertices[ i + 1 ].clone();
  174. nV2 = clip( nV1, inVertices[ i + 2 ], plane, s );
  175. nV3 = clip( nV1, inVertices[ i ], plane, s );
  176. outVertices.push( nV1 );
  177. outVertices.push( nV2 );
  178. outVertices.push( nV3 );
  179. }
  180. if ( ! v3Out ) {
  181. nV1 = inVertices[ i + 2 ].clone();
  182. nV2 = clip( nV1, inVertices[ i ], plane, s );
  183. nV3 = clip( nV1, inVertices[ i + 1 ], plane, s );
  184. outVertices.push( nV1 );
  185. outVertices.push( nV2 );
  186. outVertices.push( nV3 );
  187. }
  188. break;
  189. }
  190. case 3: {
  191. // the entire face lies outside of the plane, so let's discard the corresponding vertices
  192. break;
  193. }
  194. }
  195. }
  196. return outVertices;
  197. }
  198. function clip( v0, v1, p, s ) {
  199. var d0 = v0.position.dot( p ) - s;
  200. var d1 = v1.position.dot( p ) - s;
  201. var s0 = d0 / ( d0 - d1 );
  202. var v = new DecalVertex(
  203. new Vector3(
  204. v0.position.x + s0 * ( v1.position.x - v0.position.x ),
  205. v0.position.y + s0 * ( v1.position.y - v0.position.y ),
  206. v0.position.z + s0 * ( v1.position.z - v0.position.z )
  207. ),
  208. new Vector3(
  209. v0.normal.x + s0 * ( v1.normal.x - v0.normal.x ),
  210. v0.normal.y + s0 * ( v1.normal.y - v0.normal.y ),
  211. v0.normal.z + s0 * ( v1.normal.z - v0.normal.z )
  212. )
  213. );
  214. // need to clip more values (texture coordinates)? do it this way:
  215. // intersectpoint.value = a.value + s * ( b.value - a.value );
  216. return v;
  217. }
  218. };
  219. DecalGeometry.prototype = Object.create( BufferGeometry.prototype );
  220. DecalGeometry.prototype.constructor = DecalGeometry;
  221. // helper
  222. var DecalVertex = function ( position, normal ) {
  223. this.position = position;
  224. this.normal = normal;
  225. };
  226. DecalVertex.prototype.clone = function () {
  227. return new this.constructor( this.position.clone(), this.normal.clone() );
  228. };
  229. export { DecalGeometry, DecalVertex };