DecalGeometry.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. THREE.DecalVertex = function( v, n ) {
  2. this.vertex = v;
  3. this.normal = n;
  4. };
  5. THREE.DecalVertex.prototype.clone = function() {
  6. return new THREE.DecalVertex( this.vertex.clone(), this.normal.clone() );
  7. };
  8. THREE.DecalGeometry = function( mesh, position, rotation, dimensions, check ) {
  9. THREE.Geometry.call( this );
  10. if ( check === undefined ) check = null;
  11. check = check || new THREE.Vector3( 1, 1, 1 );
  12. this.uvs = [];
  13. this.cube = new THREE.Mesh( new THREE.BoxGeometry( dimensions.x, dimensions.y, dimensions.z ), new THREE.MeshBasicMaterial() );
  14. this.cube.rotation.set( rotation.x, rotation.y, rotation.z );
  15. this.cube.position.copy( position );
  16. this.cube.scale.set( 1, 1, 1 );
  17. this.cube.updateMatrix();
  18. this.iCubeMatrix = ( new THREE.Matrix4() ).getInverse( this.cube.matrix );
  19. this.faceIndices = [ 'a', 'b', 'c', 'd' ];
  20. this.clipFace = function( inVertices, plane ) {
  21. var size = .5 * Math.abs( ( dimensions.clone() ).dot( plane ) );
  22. function clip( v0, v1, p ) {
  23. var d0 = v0.vertex.dot( p ) - size,
  24. d1 = v1.vertex.dot( p ) - size;
  25. var s = d0 / ( d0 - d1 );
  26. var v = new THREE.DecalVertex(
  27. new THREE.Vector3(
  28. v0.vertex.x + s * ( v1.vertex.x - v0.vertex.x ),
  29. v0.vertex.y + s * ( v1.vertex.y - v0.vertex.y ),
  30. v0.vertex.z + s * ( v1.vertex.z - v0.vertex.z )
  31. ),
  32. new THREE.Vector3(
  33. v0.normal.x + s * ( v1.normal.x - v0.normal.x ),
  34. v0.normal.y + s * ( v1.normal.y - v0.normal.y ),
  35. v0.normal.z + s * ( v1.normal.z - v0.normal.z )
  36. )
  37. );
  38. // need to clip more values (texture coordinates)? do it this way:
  39. //intersectpoint.value = a.value + s*(b.value-a.value);
  40. return v;
  41. }
  42. if ( inVertices.length === 0 ) return [];
  43. var outVertices = [];
  44. for ( var j = 0; j < inVertices.length; j += 3 ) {
  45. var v1Out, v2Out, v3Out, total = 0;
  46. var d1 = inVertices[ j + 0 ].vertex.dot( plane ) - size,
  47. d2 = inVertices[ j + 1 ].vertex.dot( plane ) - size,
  48. d3 = inVertices[ j + 2 ].vertex.dot( plane ) - size;
  49. v1Out = d1 > 0;
  50. v2Out = d2 > 0;
  51. v3Out = d3 > 0;
  52. total = ( v1Out ? 1 : 0 ) + ( v2Out ? 1 : 0 ) + ( v3Out ? 1 : 0 );
  53. switch ( total ) {
  54. case 0: {
  55. outVertices.push( inVertices[ j ] );
  56. outVertices.push( inVertices[ j + 1 ] );
  57. outVertices.push( inVertices[ j + 2 ] );
  58. break;
  59. }
  60. case 1: {
  61. var nV1, nV2, nV3, nV4;
  62. if ( v1Out ) {
  63. nV1 = inVertices[ j + 1 ];
  64. nV2 = inVertices[ j + 2 ];
  65. nV3 = clip( inVertices[ j ], nV1, plane );
  66. nV4 = clip( inVertices[ j ], nV2, plane );
  67. }
  68. if ( v2Out ) {
  69. nV1 = inVertices[ j ];
  70. nV2 = inVertices[ j + 2 ];
  71. nV3 = clip( inVertices[ j + 1 ], nV1, plane );
  72. nV4 = clip( inVertices[ j + 1 ], nV2, plane );
  73. outVertices.push( nV3 );
  74. outVertices.push( nV2.clone() );
  75. outVertices.push( nV1.clone() );
  76. outVertices.push( nV2.clone() );
  77. outVertices.push( nV3.clone() );
  78. outVertices.push( nV4 );
  79. break;
  80. }
  81. if ( v3Out ) {
  82. nV1 = inVertices[ j ];
  83. nV2 = inVertices[ j + 1 ];
  84. nV3 = clip( inVertices[ j + 2 ], nV1, plane );
  85. nV4 = clip( inVertices[ j + 2 ], nV2, plane );
  86. }
  87. outVertices.push( nV1.clone() );
  88. outVertices.push( nV2.clone() );
  89. outVertices.push( nV3 );
  90. outVertices.push( nV4 );
  91. outVertices.push( nV3.clone() );
  92. outVertices.push( nV2.clone() );
  93. break;
  94. }
  95. case 2: {
  96. var nV1, nV2, nV3;
  97. if ( ! v1Out ) {
  98. nV1 = inVertices[ j ].clone();
  99. nV2 = clip( nV1, inVertices[ j + 1 ], plane );
  100. nV3 = clip( nV1, inVertices[ j + 2 ], plane );
  101. outVertices.push( nV1 );
  102. outVertices.push( nV2 );
  103. outVertices.push( nV3 );
  104. }
  105. if ( ! v2Out ) {
  106. nV1 = inVertices[ j + 1 ].clone();
  107. nV2 = clip( nV1, inVertices[ j + 2 ], plane );
  108. nV3 = clip( nV1, inVertices[ j ], plane );
  109. outVertices.push( nV1 );
  110. outVertices.push( nV2 );
  111. outVertices.push( nV3 );
  112. }
  113. if ( ! v3Out ) {
  114. nV1 = inVertices[ j + 2 ].clone();
  115. nV2 = clip( nV1, inVertices[ j ], plane );
  116. nV3 = clip( nV1, inVertices[ j + 1 ], plane );
  117. outVertices.push( nV1 );
  118. outVertices.push( nV2 );
  119. outVertices.push( nV3 );
  120. }
  121. break;
  122. }
  123. case 3: {
  124. break;
  125. }
  126. }
  127. }
  128. return outVertices;
  129. };
  130. this.pushVertex = function( vertices, id, n ) {
  131. var v = mesh.geometry.vertices[ id ].clone();
  132. v.applyMatrix4( mesh.matrix );
  133. v.applyMatrix4( this.iCubeMatrix );
  134. vertices.push( new THREE.DecalVertex( v, n.clone() ) );
  135. };
  136. this.computeDecal = function() {
  137. var finalVertices = [];
  138. for ( var i = 0; i < mesh.geometry.faces.length; i ++ ) {
  139. var f = mesh.geometry.faces[ i ];
  140. var vertices = [];
  141. this.pushVertex( vertices, f[ this.faceIndices[ 0 ] ], f.vertexNormals[ 0 ] );
  142. this.pushVertex( vertices, f[ this.faceIndices[ 1 ] ], f.vertexNormals[ 1 ] );
  143. this.pushVertex( vertices, f[ this.faceIndices[ 2 ] ], f.vertexNormals[ 2 ] );
  144. if ( check.x ) {
  145. vertices = this.clipFace( vertices, new THREE.Vector3( 1, 0, 0 ) );
  146. vertices = this.clipFace( vertices, new THREE.Vector3( - 1, 0, 0 ) );
  147. }
  148. if ( check.y ) {
  149. vertices = this.clipFace( vertices, new THREE.Vector3( 0, 1, 0 ) );
  150. vertices = this.clipFace( vertices, new THREE.Vector3( 0, - 1, 0 ) );
  151. }
  152. if ( check.z ) {
  153. vertices = this.clipFace( vertices, new THREE.Vector3( 0, 0, 1 ) );
  154. vertices = this.clipFace( vertices, new THREE.Vector3( 0, 0, - 1 ) );
  155. }
  156. for ( var j = 0; j < vertices.length; j ++ ) {
  157. var v = vertices[ j ];
  158. this.uvs.push( new THREE.Vector2(
  159. .5 + ( v.vertex.x / dimensions.x ),
  160. .5 + ( v.vertex.y / dimensions.y )
  161. ) );
  162. vertices[ j ].vertex.applyMatrix4( this.cube.matrix );
  163. }
  164. if ( vertices.length === 0 ) continue;
  165. finalVertices = finalVertices.concat( vertices );
  166. }
  167. for ( var k = 0; k < finalVertices.length; k += 3 ) {
  168. this.vertices.push(
  169. finalVertices[ k ].vertex,
  170. finalVertices[ k + 1 ].vertex,
  171. finalVertices[ k + 2 ].vertex
  172. );
  173. var f = new THREE.Face3(
  174. k,
  175. k + 1,
  176. k + 2
  177. );
  178. f.vertexNormals.push( finalVertices[ k + 0 ].normal );
  179. f.vertexNormals.push( finalVertices[ k + 1 ].normal );
  180. f.vertexNormals.push( finalVertices[ k + 2 ].normal );
  181. this.faces.push( f );
  182. this.faceVertexUvs[ 0 ].push( [
  183. this.uvs[ k ],
  184. this.uvs[ k + 1 ],
  185. this.uvs[ k + 2 ]
  186. ] );
  187. }
  188. this.verticesNeedUpdate = true;
  189. this.elementsNeedUpdate = true;
  190. this.morphTargetsNeedUpdate = true;
  191. this.uvsNeedUpdate = true;
  192. this.normalsNeedUpdate = true;
  193. this.colorsNeedUpdate = true;
  194. this.computeFaceNormals();
  195. };
  196. this.computeDecal();
  197. };
  198. THREE.DecalGeometry.prototype = Object.create( THREE.Geometry.prototype );
  199. THREE.DecalGeometry.prototype.constructor = THREE.DecalGeometry;