DecalGeometry.js 8.7 KB

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