DecalGeometry.js 9.2 KB

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