THREE.DecalGeometry.js 7.7 KB

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