DecalGeometry.js 8.3 KB

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