DecalGeometry.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /**
  2. * @author Mugen87 / https://github.com/Mugen87
  3. * @author spite / https://github.com/spite
  4. *
  5. * You can use this geometry to create a decal mesh, that serves different kinds of purposes.
  6. * e.g. adding unique details to models, performing dynamic visual environmental changes or covering seams.
  7. *
  8. * Constructor parameter:
  9. *
  10. * mesh — Any mesh object
  11. * position — Position of the decal projector
  12. * orientation — Orientation of the decal projector
  13. * size — Size of the decal projector
  14. *
  15. * reference: http://blog.wolfire.com/2009/06/how-to-project-decals/
  16. *
  17. */
  18. import {
  19. BufferGeometry,
  20. Float32BufferAttribute,
  21. Matrix4,
  22. Vector3
  23. } from "../../../build/three.module.js";
  24. var DecalGeometry = function ( mesh, position, orientation, size ) {
  25. BufferGeometry.call( this );
  26. // buffers
  27. var vertices = [];
  28. var normals = [];
  29. var uvs = [];
  30. // helpers
  31. var plane = new Vector3();
  32. // this matrix represents the transformation of the decal projector
  33. var projectorMatrix = new Matrix4();
  34. projectorMatrix.makeRotationFromEuler( orientation );
  35. projectorMatrix.setPosition( position );
  36. var projectorMatrixInverse = new Matrix4().getInverse( projectorMatrix );
  37. // generate buffers
  38. generate();
  39. // build geometry
  40. this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  41. this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
  42. this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
  43. function generate() {
  44. var i;
  45. var geometry = new BufferGeometry();
  46. var decalVertices = [];
  47. var vertex = new Vector3();
  48. var normal = new Vector3();
  49. // handle different geometry types
  50. if ( mesh.geometry.isGeometry ) {
  51. geometry.fromGeometry( mesh.geometry );
  52. } else {
  53. geometry.copy( mesh.geometry );
  54. }
  55. var positionAttribute = geometry.attributes.position;
  56. var normalAttribute = geometry.attributes.normal;
  57. // first, create an array of 'DecalVertex' objects
  58. // three consecutive 'DecalVertex' objects represent a single face
  59. //
  60. // this data structure will be later used to perform the clipping
  61. if ( geometry.index !== null ) {
  62. // indexed BufferGeometry
  63. var index = geometry.index;
  64. for ( i = 0; i < index.count; i ++ ) {
  65. vertex.fromBufferAttribute( positionAttribute, index.getX( i ) );
  66. normal.fromBufferAttribute( normalAttribute, index.getX( i ) );
  67. pushDecalVertex( decalVertices, vertex, normal );
  68. }
  69. } else {
  70. // non-indexed BufferGeometry
  71. for ( i = 0; i < positionAttribute.count; i ++ ) {
  72. vertex.fromBufferAttribute( positionAttribute, i );
  73. normal.fromBufferAttribute( normalAttribute, i );
  74. pushDecalVertex( decalVertices, vertex, normal );
  75. }
  76. }
  77. // second, clip the geometry so that it doesn't extend out from the projector
  78. decalVertices = clipGeometry( decalVertices, plane.set( 1, 0, 0 ) );
  79. decalVertices = clipGeometry( decalVertices, plane.set( - 1, 0, 0 ) );
  80. decalVertices = clipGeometry( decalVertices, plane.set( 0, 1, 0 ) );
  81. decalVertices = clipGeometry( decalVertices, plane.set( 0, - 1, 0 ) );
  82. decalVertices = clipGeometry( decalVertices, plane.set( 0, 0, 1 ) );
  83. decalVertices = clipGeometry( decalVertices, plane.set( 0, 0, - 1 ) );
  84. // third, generate final vertices, normals and uvs
  85. for ( i = 0; i < decalVertices.length; i ++ ) {
  86. var decalVertex = decalVertices[ i ];
  87. // create texture coordinates (we are still in projector space)
  88. uvs.push(
  89. 0.5 + ( decalVertex.position.x / size.x ),
  90. 0.5 + ( decalVertex.position.y / size.y )
  91. );
  92. // transform the vertex back to world space
  93. decalVertex.position.applyMatrix4( projectorMatrix );
  94. // now create vertex and normal buffer data
  95. vertices.push( decalVertex.position.x, decalVertex.position.y, decalVertex.position.z );
  96. normals.push( decalVertex.normal.x, decalVertex.normal.y, decalVertex.normal.z );
  97. }
  98. }
  99. function pushDecalVertex( decalVertices, vertex, normal ) {
  100. // transform the vertex to world space, then to projector space
  101. vertex.applyMatrix4( mesh.matrixWorld );
  102. vertex.applyMatrix4( projectorMatrixInverse );
  103. normal.transformDirection( mesh.matrixWorld );
  104. decalVertices.push( new DecalVertex( vertex.clone(), normal.clone() ) );
  105. }
  106. function clipGeometry( inVertices, plane ) {
  107. var outVertices = [];
  108. var s = 0.5 * Math.abs( size.dot( plane ) );
  109. // a single iteration clips one face,
  110. // which consists of three consecutive 'DecalVertex' objects
  111. for ( var i = 0; i < inVertices.length; i += 3 ) {
  112. var v1Out, v2Out, v3Out, total = 0;
  113. var nV1, nV2, nV3, nV4;
  114. var d1 = inVertices[ i + 0 ].position.dot( plane ) - s;
  115. var d2 = inVertices[ i + 1 ].position.dot( plane ) - s;
  116. var d3 = inVertices[ i + 2 ].position.dot( plane ) - s;
  117. v1Out = d1 > 0;
  118. v2Out = d2 > 0;
  119. 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. var d0 = v0.position.dot( p ) - s;
  203. var d1 = v1.position.dot( p ) - s;
  204. var s0 = d0 / ( d0 - d1 );
  205. var 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. DecalGeometry.prototype = Object.create( BufferGeometry.prototype );
  223. DecalGeometry.prototype.constructor = DecalGeometry;
  224. // helper
  225. var DecalVertex = function ( position, normal ) {
  226. this.position = position;
  227. this.normal = normal;
  228. };
  229. DecalVertex.prototype.clone = function () {
  230. return new this.constructor( this.position.clone(), this.normal.clone() );
  231. };
  232. export { DecalGeometry, DecalVertex };