threeLineExt.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419
  1. function Float32BufferAttribute( array, itemSize, normalized ) {
  2. THREE.BufferAttribute.call( this, new Float32Array( array ), itemSize, normalized );
  3. }
  4. Float32BufferAttribute.prototype = Object.create( THREE.BufferAttribute.prototype );
  5. Float32BufferAttribute.prototype.constructor = Float32BufferAttribute;
  6. THREE.Float32BufferAttribute = Float32BufferAttribute;
  7. /**
  8. * @author WestLangley / http://github.com/WestLangley
  9. *
  10. * parameters = {
  11. * color: <hex>,
  12. * linewidth: <float>,
  13. * dashed: <boolean>,
  14. * dashScale: <float>,
  15. * dashSize: <float>,
  16. * gapSize: <float>,
  17. * resolution: <Vector2>, // to be set by renderer
  18. * }
  19. */
  20. THREE.UniformsLib.line = {
  21. linewidth: { value: 1 , type:'f'},
  22. resolution: { value: new THREE.Vector2( 1, 1 ), type:'v2' },
  23. dashScale: { value: 1 , type:'f'},
  24. dashSize: { value: 1 , type:'f'},
  25. gapSize: { value: 1 , type:'f'} // todo FIX - maybe change to totalSize
  26. };
  27. THREE.ShaderLib[ 'line' ] = {
  28. uniforms: THREE.UniformsUtils.merge( [
  29. THREE.UniformsLib.common,
  30. THREE.UniformsLib.fog,
  31. THREE.UniformsLib.line
  32. ] ),
  33. /* vertexShader:
  34. `
  35. #include <common>
  36. #include <color_pars_vertex>
  37. #include <logdepthbuf_pars_vertex>
  38. uniform float linewidth;
  39. uniform vec2 resolution;
  40. attribute vec3 instanceStart;
  41. attribute vec3 instanceEnd;
  42. attribute vec3 instanceColorStart;
  43. attribute vec3 instanceColorEnd;
  44. varying vec2 vUv;
  45. #ifdef USE_DASH
  46. uniform float dashScale;
  47. attribute float instanceDistanceStart;
  48. attribute float instanceDistanceEnd;
  49. varying float vLineDistance;
  50. #endif
  51. void trimSegment( const in vec4 start, inout vec4 end ) {
  52. // trim end segment so it terminates between the camera plane and the near plane
  53. // conservative estimate of the near plane
  54. float a = projectionMatrix[ 2 ][ 2 ]; // 3nd entry in 3th column
  55. float b = projectionMatrix[ 3 ][ 2 ]; // 3nd entry in 4th column
  56. float nearEstimate = - 0.5 * b / a;
  57. float alpha = ( nearEstimate - start.z ) / ( end.z - start.z );
  58. end.xyz = mix( start.xyz, end.xyz, alpha );
  59. }
  60. void main() {
  61. #ifdef USE_COLOR
  62. vColor.xyz = ( position.y < 0.5 ) ? instanceColorStart : instanceColorEnd;
  63. #endif
  64. #ifdef USE_DASH
  65. vLineDistance = ( position.y < 0.5 ) ? dashScale * instanceDistanceStart : dashScale * instanceDistanceEnd;
  66. #endif
  67. float aspect = resolution.x / resolution.y;
  68. vUv = uv;
  69. // camera space
  70. vec4 start = modelViewMatrix * vec4( instanceStart, 1.0 );
  71. vec4 end = modelViewMatrix * vec4( instanceEnd, 1.0 );
  72. // special case for perspective projection, and segments that terminate either in, or behind, the camera plane
  73. // clearly the gpu firmware has a way of addressing this issue when projecting into ndc space
  74. // but we need to perform ndc-space calculations in the shader, so we must address this issue directly
  75. // perhaps there is a more elegant solution -- WestLangley
  76. bool perspective = ( projectionMatrix[ 2 ][ 3 ] == - 1.0 ); // 4th entry in the 3rd column
  77. if ( perspective ) {
  78. if ( start.z < 0.0 && end.z >= 0.0 ) {
  79. trimSegment( start, end );
  80. } else if ( end.z < 0.0 && start.z >= 0.0 ) {
  81. trimSegment( end, start );
  82. }
  83. }
  84. // clip space
  85. vec4 clipStart = projectionMatrix * start;
  86. vec4 clipEnd = projectionMatrix * end;
  87. // ndc space
  88. vec2 ndcStart = clipStart.xy / clipStart.w;
  89. vec2 ndcEnd = clipEnd.xy / clipEnd.w;
  90. // direction
  91. vec2 dir = ndcEnd - ndcStart;
  92. // account for clip-space aspect ratio
  93. dir.x *= aspect;
  94. dir = normalize( dir );
  95. // perpendicular to dir
  96. vec2 offset = vec2( dir.y, - dir.x );
  97. // undo aspect ratio adjustment
  98. dir.x /= aspect;
  99. offset.x /= aspect;
  100. // sign flip
  101. if ( position.x < 0.0 ) offset *= - 1.0;
  102. // endcaps
  103. if ( position.y < 0.0 ) {
  104. offset += - dir;
  105. } else if ( position.y > 1.0 ) {
  106. offset += dir;
  107. }
  108. // adjust for linewidth
  109. offset *= linewidth;
  110. // adjust for clip-space to screen-space conversion // maybe resolution should be based on viewport ...
  111. offset /= resolution.y;
  112. // select end
  113. vec4 clip = ( position.y < 0.5 ) ? clipStart : clipEnd;
  114. // back to clip space
  115. offset *= clip.w;
  116. clip.xy += offset;
  117. gl_Position = clip;
  118. vec4 mvPosition = ( position.y < 0.5 ) ? start : end; // this is an approximation
  119. #include <logdepthbuf_vertex>
  120. }
  121. `,
  122. fragmentShader:
  123. `
  124. uniform vec3 diffuse;
  125. uniform float opacity;
  126. #ifdef USE_DASH
  127. uniform float dashSize;
  128. uniform float gapSize;
  129. #endif
  130. varying float vLineDistance;
  131. #include <common>
  132. #include <color_pars_fragment>
  133. #include <fog_pars_fragment>
  134. #include <logdepthbuf_pars_fragment>
  135. varying vec2 vUv;
  136. void main() {
  137. #ifdef USE_DASH
  138. if ( vUv.y < - 1.0 || vUv.y > 1.0 ) discard; // discard endcaps
  139. if ( mod( vLineDistance, dashSize + gapSize ) > dashSize ) discard; // todo - FIX
  140. #endif
  141. if ( abs( vUv.y ) > 1.0 ) {
  142. float a = vUv.x;
  143. float b = ( vUv.y > 0.0 ) ? vUv.y - 1.0 : vUv.y + 1.0;
  144. float len2 = a * a + b * b;
  145. if ( len2 > 1.0 ) discard;
  146. }
  147. vec4 diffuseColor = vec4( diffuse, opacity );
  148. #include <logdepthbuf_fragment>
  149. #include <color_fragment>
  150. gl_FragColor = vec4( diffuseColor.rgb, diffuseColor.a );
  151. #include <premultiplied_alpha_fragment>
  152. #include <tonemapping_fragment>
  153. #include <encodings_fragment>
  154. #include <fog_fragment>
  155. }
  156. ` */
  157. //---------------去掉注释的版本-:----
  158. /*
  159. vertexShader:
  160. `
  161. #include <common>
  162. #include <color_pars_vertex>
  163. #include <logdepthbuf_pars_vertex>
  164. uniform float linewidth;
  165. uniform vec2 resolution;
  166. attribute vec3 instanceStart;
  167. attribute vec3 instanceEnd;
  168. attribute vec3 instanceColorStart;
  169. attribute vec3 instanceColorEnd;
  170. varying vec2 vUv;
  171. #ifdef USE_DASH
  172. uniform float dashScale;
  173. attribute float instanceDistanceStart;
  174. attribute float instanceDistanceEnd;
  175. varying float vLineDistance;
  176. #endif
  177. void trimSegment( const in vec4 start, inout vec4 end ) {
  178. float a = projectionMatrix[ 2 ][ 2 ];
  179. float b = projectionMatrix[ 3 ][ 2 ];
  180. float nearEstimate = - 0.5 * b / a;
  181. float alpha = ( nearEstimate - start.z ) / ( end.z - start.z );
  182. end.xyz = mix( start.xyz, end.xyz, alpha );
  183. }
  184. void main() {
  185. #ifdef USE_COLOR
  186. vColor.xyz = ( position.y < 0.5 ) ? instanceColorStart : instanceColorEnd;
  187. #endif
  188. #ifdef USE_DASH
  189. vLineDistance = ( position.y < 0.5 ) ? dashScale * instanceDistanceStart : dashScale * instanceDistanceEnd;
  190. #endif
  191. float aspect = resolution.x / resolution.y;
  192. vUv = uv;
  193. vec4 start = modelViewMatrix * vec4( instanceStart, 1.0 );
  194. vec4 end = modelViewMatrix * vec4( instanceEnd, 1.0 );
  195. bool perspective = ( projectionMatrix[ 2 ][ 3 ] == - 1.0 );
  196. if ( perspective ) {
  197. if ( start.z < 0.0 && end.z >= 0.0 ) {
  198. trimSegment( start, end );
  199. } else if ( end.z < 0.0 && start.z >= 0.0 ) {
  200. trimSegment( end, start );
  201. }
  202. }
  203. vec4 clipStart = projectionMatrix * start;
  204. vec4 clipEnd = projectionMatrix * end;
  205. vec2 ndcStart = clipStart.xy / clipStart.w;
  206. vec2 ndcEnd = clipEnd.xy / clipEnd.w;
  207. vec2 dir = ndcEnd - ndcStart;
  208. dir.x *= aspect;
  209. dir = normalize( dir );
  210. vec2 offset = vec2( dir.y, - dir.x );
  211. dir.x /= aspect;
  212. offset.x /= aspect;
  213. if ( position.x < 0.0 ) offset *= - 1.0;
  214. if ( position.y < 0.0 ) {
  215. offset += - dir;
  216. } else if ( position.y > 1.0 ) {
  217. offset += dir;
  218. }
  219. offset *= linewidth;
  220. offset /= resolution.y;
  221. vec4 clip = ( position.y < 0.5 ) ? clipStart : clipEnd;
  222. offset *= clip.w;
  223. clip.xy += offset;
  224. gl_Position = clip;
  225. vec4 mvPosition = ( position.y < 0.5 ) ? start : end;
  226. #include <logdepthbuf_vertex>
  227. }
  228. `,
  229. fragmentShader:
  230. `
  231. uniform vec3 diffuse;
  232. uniform float opacity;
  233. #ifdef USE_DASH
  234. uniform float dashSize;
  235. uniform float gapSize;
  236. #endif
  237. varying float vLineDistance;
  238. #include <common>
  239. #include <color_pars_fragment>
  240. #include <fog_pars_fragment>
  241. #include <logdepthbuf_pars_fragment>
  242. varying vec2 vUv;
  243. void main() {
  244. #ifdef USE_DASH
  245. if ( vUv.y < - 1.0 || vUv.y > 1.0 ) discard;
  246. if ( mod( vLineDistance, dashSize + gapSize ) > dashSize ) discard;
  247. #endif
  248. if ( abs( vUv.y ) > 1.0 ) {
  249. float a = vUv.x;
  250. float b = ( vUv.y > 0.0 ) ? vUv.y - 1.0 : vUv.y + 1.0;
  251. float len2 = a * a + b * b;
  252. if ( len2 > 1.0 ) discard;
  253. }
  254. vec4 diffuseColor = vec4( diffuse, opacity );
  255. #include <logdepthbuf_fragment>
  256. #include <color_fragment>
  257. gl_FragColor = vec4( diffuseColor.rgb, diffuseColor.a );
  258. #include <premultiplied_alpha_fragment>
  259. #include <tonemapping_fragment>
  260. #include <encodings_fragment>
  261. #include <fog_fragment>
  262. }
  263. `
  264. */
  265. //ie不支持模板字符串所以转成:
  266. vertexShader : "#include <common>\n#include <color_pars_vertex>\n \n#include <logdepthbuf_pars_vertex>\n \nuniform float linewidth;\nuniform vec2 resolution;\nattribute vec3 instanceStart;\nattribute vec3 instanceEnd;\nattribute vec3 instanceColorStart;\nattribute vec3 instanceColorEnd;\nvarying vec2 vUv;\n#ifdef USE_DASH\n uniform float dashScale;\n attribute float instanceDistanceStart;\n attribute float instanceDistanceEnd;\n varying float vLineDistance;\n#endif\nvoid trimSegment( const in vec4 start, inout vec4 end ) {\n float a = projectionMatrix[ 2 ][ 2 ]; \n float b = projectionMatrix[ 3 ][ 2 ]; \n float nearEstimate = - 0.5 * b / a;\n float alpha = ( nearEstimate - start.z ) / ( end.z - start.z );\n end.xyz = mix( start.xyz, end.xyz, alpha );\n}\nvoid main() {\n #ifdef USE_COLOR\n vColor.xyz = ( position.y < 0.5 ) ? instanceColorStart : instanceColorEnd;\n #endif\n #ifdef USE_DASH\n vLineDistance = ( position.y < 0.5 ) ? dashScale * instanceDistanceStart : dashScale * instanceDistanceEnd;\n #endif\n float aspect = resolution.x / resolution.y;\n vUv = uv;\n vec4 start = modelViewMatrix * vec4( instanceStart, 1.0 );\n vec4 end = modelViewMatrix * vec4( instanceEnd, 1.0 );\n bool perspective = ( projectionMatrix[ 2 ][ 3 ] == - 1.0 ); \n if ( perspective ) {\n if ( start.z < 0.0 && end.z >= 0.0 ) {\n trimSegment( start, end );\n } else if ( end.z < 0.0 && start.z >= 0.0 ) {\n trimSegment( end, start );\n }\n }\n vec4 clipStart = projectionMatrix * start;\n vec4 clipEnd = projectionMatrix * end;\n vec2 ndcStart = clipStart.xy / clipStart.w;\n vec2 ndcEnd = clipEnd.xy / clipEnd.w;\n vec2 dir = ndcEnd - ndcStart;\n dir.x *= aspect;\n dir = normalize( dir );\n vec2 offset = vec2( dir.y, - dir.x );\n dir.x /= aspect;\n offset.x /= aspect;\n if ( position.x < 0.0 ) offset *= - 1.0;\n if ( position.y < 0.0 ) {\n offset += - dir;\n } else if ( position.y > 1.0 ) {\n offset += dir;\n }\n offset *= linewidth;\n offset /= resolution.y;\n vec4 clip = ( position.y < 0.5 ) ? clipStart : clipEnd;\n offset *= clip.w;\n clip.xy += offset;\n gl_Position = clip;\n vec4 mvPosition = ( position.y < 0.5 ) ? start : end; \n #include <logdepthbuf_vertex>\n \n \n} ",
  267. fragmentShader: "uniform vec3 diffuse;\n uniform float opacity;\n #ifdef USE_DASH\n uniform float dashSize;\n uniform float gapSize;\n #endif\n varying float vLineDistance;\n #include <common>\n #include <color_pars_fragment>\n #include <fog_pars_fragment>\n #include <logdepthbuf_pars_fragment>\n \n varying vec2 vUv;\n void main() {\n \n #ifdef USE_DASH\n\nif ( vUv.y < - 1.0 || vUv.y > 1.0 ) discard; \n\nif ( mod( vLineDistance, dashSize + gapSize ) > dashSize ) discard; \n #endif\n if ( abs( vUv.y ) > 1.0 ) {\n\nfloat a = vUv.x;\n\nfloat b = ( vUv.y > 0.0 ) ? vUv.y - 1.0 : vUv.y + 1.0;\n\nfloat len2 = a * a + b * b;\n\nif ( len2 > 1.0 ) discard;\n }\n vec4 diffuseColor = vec4( diffuse, opacity );\n #include <logdepthbuf_fragment>\n #include <color_fragment>\n gl_FragColor = vec4( diffuseColor.rgb, diffuseColor.a );\n #include <premultiplied_alpha_fragment>\n #include <tonemapping_fragment>\n #include <encodings_fragment>\n #include <fog_fragment>\n }"
  268. };
  269. THREE.LineMaterial = function ( parameters ) {
  270. THREE.ShaderMaterial.call( this, {
  271. type: 'LineMaterial',
  272. uniforms: THREE.UniformsUtils.clone( THREE.ShaderLib[ 'line' ].uniforms ),
  273. vertexShader: THREE.ShaderLib[ 'line' ].vertexShader,
  274. fragmentShader: THREE.ShaderLib[ 'line' ].fragmentShader
  275. } );
  276. this.dashed = false;
  277. Object.defineProperties( this, {
  278. color: {
  279. enumerable: true,
  280. get: function () {
  281. return this.uniforms.diffuse.value;
  282. },
  283. set: function ( value ) {
  284. this.uniforms.diffuse.value = value;
  285. }
  286. },
  287. linewidth: {
  288. enumerable: true,
  289. get: function () {
  290. return this.uniforms.linewidth.value;
  291. },
  292. set: function ( value ) {
  293. this.uniforms.linewidth.value = value;
  294. }
  295. },
  296. dashScale: {
  297. enumerable: true,
  298. get: function () {
  299. return this.uniforms.dashScale.value;
  300. },
  301. set: function ( value ) {
  302. this.uniforms.dashScale.value = value;
  303. }
  304. },
  305. dashSize: {
  306. enumerable: true,
  307. get: function () {
  308. return this.uniforms.dashSize.value;
  309. },
  310. set: function ( value ) {
  311. this.uniforms.dashSize.value = value;
  312. }
  313. },
  314. gapSize: {
  315. enumerable: true,
  316. get: function () {
  317. return this.uniforms.gapSize.value;
  318. },
  319. set: function ( value ) {
  320. this.uniforms.gapSize.value = value;
  321. }
  322. },
  323. resolution: {
  324. enumerable: true,
  325. get: function () {
  326. return this.uniforms.resolution.value;
  327. },
  328. set: function ( value ) {
  329. this.uniforms.resolution.value.copy( value );
  330. }
  331. }
  332. } );
  333. this.setValues( parameters );
  334. };
  335. THREE.LineMaterial.prototype = Object.create( THREE.ShaderMaterial.prototype );
  336. THREE.LineMaterial.prototype.constructor = THREE.LineMaterial;
  337. THREE.LineMaterial.prototype.isLineMaterial = true;
  338. THREE.LineMaterial.prototype.copy = function ( source ) {
  339. THREE.ShaderMaterial.prototype.copy.call( this, source );
  340. this.color.copy( source.color );
  341. this.linewidth = source.linewidth;
  342. this.resolution = source.resolution;
  343. // todo
  344. return this;
  345. };
  346. /**
  347. * @author WestLangley / http://github.com/WestLangley
  348. *
  349. */
  350. THREE.LineSegmentsGeometry = function () {
  351. THREE.InstancedBufferGeometry.call( this );
  352. this.type = 'LineSegmentsGeometry';
  353. var plane = new THREE.BufferGeometry();
  354. var positions = [ - 1, 2, 0, 1, 2, 0, - 1, 1, 0, 1, 1, 0, - 1, 0, 0, 1, 0, 0, - 1, - 1, 0, 1, - 1, 0 ];
  355. var uvs = [ - 1, 2, 1, 2, - 1, 1, 1, 1, - 1, - 1, 1, - 1, - 1, - 2, 1, - 2 ];
  356. var index = new THREE.BufferAttribute(new Uint16Array([ 0, 2, 1, 2, 3, 1, 2, 4, 3, 4, 5, 3, 4, 6, 5, 6, 7, 5 ]), 1)
  357. this.setIndex( index );
  358. this.addAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
  359. this.addAttribute( 'uv', new THREE.Float32BufferAttribute( uvs, 2 ) );
  360. };
  361. THREE.LineSegmentsGeometry.prototype = Object.assign( Object.create( THREE.InstancedBufferGeometry.prototype ), {
  362. constructor: THREE.LineSegmentsGeometry,
  363. isLineSegmentsGeometry: true,
  364. applyMatrix: function ( matrix ) {
  365. var start = this.attributes.instanceStart;
  366. var end = this.attributes.instanceEnd;
  367. if ( start !== undefined ) {
  368. matrix.applyToBufferAttribute( start );
  369. matrix.applyToBufferAttribute( end );
  370. start.data.needsUpdate = true;
  371. }
  372. if ( this.boundingBox !== null ) {
  373. this.computeBoundingBox();
  374. }
  375. if ( this.boundingSphere !== null ) {
  376. this.computeBoundingSphere();
  377. }
  378. return this;
  379. },
  380. setPositions: function ( array ) {
  381. var lineSegments;
  382. if ( array instanceof Float32Array ) {
  383. lineSegments = array;
  384. } else if ( Array.isArray( array ) ) {
  385. lineSegments = new Float32Array( array );
  386. }
  387. var instanceBuffer = new THREE.InstancedInterleavedBuffer( lineSegments, 6, 1 ); // xyz, xyz
  388. this.addAttribute( 'instanceStart', new THREE.InterleavedBufferAttribute( instanceBuffer, 3, 0 ) ); // xyz
  389. this.addAttribute( 'instanceEnd', new THREE.InterleavedBufferAttribute( instanceBuffer, 3, 3 ) ); // xyz
  390. //
  391. this.computeBoundingBox();
  392. this.computeBoundingSphere();
  393. return this;
  394. },
  395. setColors: function ( array ) {
  396. var colors;
  397. if ( array instanceof Float32Array ) {
  398. colors = array;
  399. } else if ( Array.isArray( array ) ) {
  400. colors = new Float32Array( array );
  401. }
  402. var instanceColorBuffer = new THREE.InstancedInterleavedBuffer( colors, 6, 1 ); // rgb, rgb
  403. this.addAttribute( 'instanceColorStart', new THREE.InterleavedBufferAttribute( instanceColorBuffer, 3, 0 ) ); // rgb
  404. this.addAttribute( 'instanceColorEnd', new THREE.InterleavedBufferAttribute( instanceColorBuffer, 3, 3 ) ); // rgb
  405. return this;
  406. },
  407. fromWireframeGeometry: function ( geometry ) {
  408. this.setPositions( geometry.attributes.position.array );
  409. return this;
  410. },
  411. fromEdgesGeometry: function ( geometry ) {
  412. this.setPositions( geometry.attributes.position.array );
  413. return this;
  414. },
  415. fromMesh: function ( mesh ) {
  416. this.fromWireframeGeometry( new THREE.WireframeGeometry( mesh.geometry ) );
  417. // set colors, maybe
  418. return this;
  419. },
  420. fromLineSegements: function ( lineSegments ) {
  421. var geometry = lineSegments.geometry;
  422. if ( geometry.isGeometry ) {
  423. this.setPositions( geometry.vertices );
  424. } else if ( geometry.isBufferGeometry ) {
  425. this.setPositions( geometry.position.array ); // assumes non-indexed
  426. }
  427. // set colors, maybe
  428. return this;
  429. },
  430. computeBoundingBox: function () {
  431. var box = new THREE.Box3();
  432. return function computeBoundingBox() {
  433. if ( this.boundingBox === null ) {
  434. this.boundingBox = new THREE.Box3();
  435. }
  436. var start = this.attributes.instanceStart;
  437. var end = this.attributes.instanceEnd;
  438. if ( start !== undefined && end !== undefined ) {
  439. this.boundingBox.setFromBufferAttribute( start );
  440. box.setFromBufferAttribute( end );
  441. this.boundingBox.union( box );
  442. }
  443. };
  444. }(),
  445. computeBoundingSphere: function () {
  446. var vector = new THREE.Vector3();
  447. return function computeBoundingSphere() {
  448. if ( this.boundingSphere === null ) {
  449. this.boundingSphere = new THREE.Sphere();
  450. }
  451. if ( this.boundingBox === null ) {
  452. this.computeBoundingBox();
  453. }
  454. var start = this.attributes.instanceStart;
  455. var end = this.attributes.instanceEnd;
  456. if ( start !== undefined && end !== undefined ) {
  457. var center = this.boundingSphere.center;
  458. this.boundingBox.getCenter( center );
  459. var maxRadiusSq = 0;
  460. for ( var i = 0, il = start.count; i < il; i ++ ) {
  461. vector.fromBufferAttribute( start, i );
  462. maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( vector ) );
  463. vector.fromBufferAttribute( end, i );
  464. maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( vector ) );
  465. }
  466. this.boundingSphere.radius = Math.sqrt( maxRadiusSq );
  467. if ( isNaN( this.boundingSphere.radius ) ) {
  468. console.error( 'THREE.LineSegmentsGeometry.computeBoundingSphere(): Computed radius is NaN. The instanced position data is likely to have NaN values.', this );
  469. }
  470. }
  471. };
  472. }(),
  473. toJSON: function () {
  474. // todo
  475. },
  476. clone: function () {
  477. // todo
  478. },
  479. copy: function ( source ) {
  480. // todo
  481. return this;
  482. }
  483. } );
  484. /**
  485. * @author WestLangley / http://github.com/WestLangley
  486. *
  487. */
  488. THREE.LineGeometry = function () {
  489. THREE.LineSegmentsGeometry.call( this );
  490. this.type = 'LineGeometry';
  491. };
  492. THREE.LineGeometry.prototype = Object.assign( Object.create( THREE.LineSegmentsGeometry.prototype ), {
  493. constructor: THREE.LineGeometry,
  494. isLineGeometry: true,
  495. setPositions: function ( array ) {
  496. // converts [ x1, y1, z1, x2, y2, z2, ... ] to pairs format
  497. var length = array.length - 3;
  498. var points = new Float32Array( 2 * length );
  499. for ( var i = 0; i < length; i += 3 ) {
  500. points[ 2 * i ] = array[ i ];
  501. points[ 2 * i + 1 ] = array[ i + 1 ];
  502. points[ 2 * i + 2 ] = array[ i + 2 ];
  503. points[ 2 * i + 3 ] = array[ i + 3 ];
  504. points[ 2 * i + 4 ] = array[ i + 4 ];
  505. points[ 2 * i + 5 ] = array[ i + 5 ];
  506. }
  507. THREE.LineSegmentsGeometry.prototype.setPositions.call( this, points );
  508. return this;
  509. },
  510. setColors: function ( array ) {
  511. // converts [ r1, g1, b1, r2, g2, b2, ... ] to pairs format
  512. var length = array.length - 3;
  513. var colors = new Float32Array( 2 * length );
  514. for ( var i = 0; i < length; i += 3 ) {
  515. colors[ 2 * i ] = array[ i ];
  516. colors[ 2 * i + 1 ] = array[ i + 1 ];
  517. colors[ 2 * i + 2 ] = array[ i + 2 ];
  518. colors[ 2 * i + 3 ] = array[ i + 3 ];
  519. colors[ 2 * i + 4 ] = array[ i + 4 ];
  520. colors[ 2 * i + 5 ] = array[ i + 5 ];
  521. }
  522. THREE.LineSegmentsGeometry.prototype.setColors.call( this, colors );
  523. return this;
  524. },
  525. fromLine: function ( line ) {
  526. var geometry = line.geometry;
  527. if ( geometry.isGeometry ) {
  528. this.setPositions( geometry.vertices );
  529. } else if ( geometry.isBufferGeometry ) {
  530. this.setPositions( geometry.position.array ); // assumes non-indexed
  531. }
  532. // set colors, maybe
  533. return this;
  534. },
  535. copy: function ( source ) {
  536. // todo
  537. return this;
  538. }
  539. } );
  540. /**
  541. * @author WestLangley / http://github.com/WestLangley
  542. *
  543. */
  544. THREE.LineSegments2 = function ( geometry, material ) {
  545. THREE.Mesh.call( this );
  546. this.type = 'LineSegments2';
  547. this.geometry = geometry !== undefined ? geometry : new THREE.LineSegmentsGeometry();
  548. this.material = material !== undefined ? material : new THREE.LineMaterial( { color: Math.random() * 0xffffff } );
  549. };
  550. THREE.LineSegments2.prototype = Object.assign( Object.create( THREE.Mesh.prototype ), {
  551. constructor: THREE.LineSegments2,
  552. isLineSegments2: true,
  553. computeLineDistances: ( function () { // for backwards-compatability, but could be a method of LineSegmentsGeometry...
  554. var start = new THREE.Vector3();
  555. var end = new THREE.Vector3();
  556. return function computeLineDistances() {
  557. var geometry = this.geometry;
  558. var instanceStart = geometry.attributes.instanceStart;
  559. var instanceEnd = geometry.attributes.instanceEnd;
  560. var lineDistances = new Float32Array( 2 * instanceStart.data.count );
  561. for ( var i = 0, j = 0, l = instanceStart.data.count; i < l; i ++, j += 2 ) {
  562. start.fromBufferAttribute( instanceStart, i );
  563. end.fromBufferAttribute( instanceEnd, i );
  564. lineDistances[ j ] = ( j === 0 ) ? 0 : lineDistances[ j - 1 ];
  565. lineDistances[ j + 1 ] = lineDistances[ j ] + start.distanceTo( end );
  566. }
  567. var instanceDistanceBuffer = new THREE.InstancedInterleavedBuffer( lineDistances, 2, 1 ); // d0, d1
  568. geometry.addAttribute( 'instanceDistanceStart', new THREE.InterleavedBufferAttribute( instanceDistanceBuffer, 1, 0 ) ); // d0
  569. geometry.addAttribute( 'instanceDistanceEnd', new THREE.InterleavedBufferAttribute( instanceDistanceBuffer, 1, 1 ) ); // d1
  570. return this;
  571. };
  572. }() ),
  573. copy: function ( source ) {
  574. // todo
  575. return this;
  576. }
  577. } );
  578. //fat line
  579. THREE.Fatline = function ( geometry, material ) {
  580. THREE.LineSegments2.call( this );
  581. this.type = 'Fatline';
  582. this.geometry = geometry !== undefined ? geometry : new THREE.LineGeometry();
  583. this.material = material !== undefined ? material : new THREE.LineMaterial( { color: Math.random() * 0xffffff } );
  584. };
  585. THREE.Fatline.prototype = Object.assign( Object.create( THREE.LineSegments2.prototype ), {
  586. constructor: THREE.Fatline,
  587. isFatline: true,
  588. copy: function ( source ) {
  589. // todo
  590. return this;
  591. }
  592. } );
  593. //////////CSS3DObject////////////
  594. /**
  595. * Based on http://www.emagix.net/academic/mscs-project/item/camera-sync-with-css3-and-webgl-threejs
  596. * @author mrdoob / http://mrdoob.com/
  597. * @author yomotsu / https://yomotsu.net/
  598. */
  599. (function(){
  600. /* import {
  601. Matrix4,
  602. Object3D,
  603. Vector3
  604. } from "../../../build/three.module.js";
  605. */
  606. var Matrix4 = THREE.Matrix4,
  607. Object3D = THREE.Object3D,
  608. Vector3 = THREE.Vector3
  609. var CSS3DObject = THREE.CSS3DObject = function ( element ) {
  610. Object3D.call( this );
  611. this.element = element;
  612. this.element.style.position = 'absolute';
  613. this.element.style.pointerEvents = 'auto';
  614. this.addEventListener( 'removed', function () {
  615. this.traverse( function ( object ) {
  616. if ( object.element instanceof Element && object.element.parentNode !== null ) {
  617. object.element.parentNode.removeChild( object.element );
  618. }
  619. } );
  620. } );
  621. };
  622. CSS3DObject.prototype = Object.create( Object3D.prototype );
  623. CSS3DObject.prototype.constructor = CSS3DObject;
  624. var CSS3DSprite = THREE.CSS3DSprite = function ( element ) {
  625. CSS3DObject.call( this, element );
  626. };
  627. CSS3DSprite.prototype = Object.create( CSS3DObject.prototype );
  628. CSS3DSprite.prototype.constructor = CSS3DSprite;
  629. //
  630. var CSS3DRenderer = THREE.CSS3DRenderer = function () {
  631. var _width, _height;
  632. var _widthHalf, _heightHalf;
  633. var matrix = new Matrix4();
  634. var cache = {
  635. camera: { fov: 0, style: '' },
  636. objects: new WeakMap()
  637. };
  638. var domElement = document.createElement( 'div' );
  639. domElement.style.overflow = 'hidden';
  640. this.domElement = domElement;
  641. var cameraElement = document.createElement( 'div' );
  642. cameraElement.style.WebkitTransformStyle = 'preserve-3d';
  643. cameraElement.style.transformStyle = 'preserve-3d';
  644. cameraElement.style.pointerEvents = 'none';
  645. domElement.appendChild( cameraElement );
  646. var isIE = /Trident/i.test( navigator.userAgent );
  647. this.getSize = function () {
  648. return {
  649. width: _width,
  650. height: _height
  651. };
  652. };
  653. this.setSize = function ( width, height ) {
  654. _width = width;
  655. _height = height;
  656. _widthHalf = _width / 2;
  657. _heightHalf = _height / 2;
  658. domElement.style.width = width + 'px';
  659. domElement.style.height = height + 'px';
  660. cameraElement.style.width = width + 'px';
  661. cameraElement.style.height = height + 'px';
  662. };
  663. function epsilon( value ) {
  664. return Math.abs( value ) < 1e-10 ? 0 : value;
  665. }
  666. function getCameraCSSMatrix( matrix ) {
  667. var elements = matrix.elements;
  668. return 'matrix3d(' +
  669. epsilon( elements[ 0 ] ) + ',' +
  670. epsilon( - elements[ 1 ] ) + ',' +
  671. epsilon( elements[ 2 ] ) + ',' +
  672. epsilon( elements[ 3 ] ) + ',' +
  673. epsilon( elements[ 4 ] ) + ',' +
  674. epsilon( - elements[ 5 ] ) + ',' +
  675. epsilon( elements[ 6 ] ) + ',' +
  676. epsilon( elements[ 7 ] ) + ',' +
  677. epsilon( elements[ 8 ] ) + ',' +
  678. epsilon( - elements[ 9 ] ) + ',' +
  679. epsilon( elements[ 10 ] ) + ',' +
  680. epsilon( elements[ 11 ] ) + ',' +
  681. epsilon( elements[ 12 ] ) + ',' +
  682. epsilon( - elements[ 13 ] ) + ',' +
  683. epsilon( elements[ 14 ] ) + ',' +
  684. epsilon( elements[ 15 ] ) +
  685. ')';
  686. }
  687. function getObjectCSSMatrix( matrix, cameraCSSMatrix ) {
  688. var elements = matrix.elements;
  689. var matrix3d = 'matrix3d(' +
  690. epsilon( elements[ 0 ] ) + ',' +
  691. epsilon( elements[ 1 ] ) + ',' +
  692. epsilon( elements[ 2 ] ) + ',' +
  693. epsilon( elements[ 3 ] ) + ',' +
  694. epsilon( - elements[ 4 ] ) + ',' +
  695. epsilon( - elements[ 5 ] ) + ',' +
  696. epsilon( - elements[ 6 ] ) + ',' +
  697. epsilon( - elements[ 7 ] ) + ',' +
  698. epsilon( elements[ 8 ] ) + ',' +
  699. epsilon( elements[ 9 ] ) + ',' +
  700. epsilon( elements[ 10 ] ) + ',' +
  701. epsilon( elements[ 11 ] ) + ',' +
  702. epsilon( elements[ 12 ] ) + ',' +
  703. epsilon( elements[ 13 ] ) + ',' +
  704. epsilon( elements[ 14 ] ) + ',' +
  705. epsilon( elements[ 15 ] ) +
  706. ')';
  707. if ( isIE ) {
  708. return 'translate(-50%,-50%)' +
  709. 'translate(' + _widthHalf + 'px,' + _heightHalf + 'px)' +
  710. cameraCSSMatrix +
  711. matrix3d;
  712. }
  713. return 'translate(-50%,-50%)' + matrix3d;
  714. }
  715. function renderObject( object, camera, cameraCSSMatrix ) {
  716. if ( object instanceof CSS3DObject ) {
  717. var style;
  718. if ( object instanceof CSS3DSprite ) {
  719. // http://swiftcoder.wordpress.com/2008/11/25/constructing-a-billboard-matrix/
  720. matrix.copy( camera.matrixWorldInverse );
  721. matrix.transpose();
  722. matrix.copyPosition( object.matrixWorld );
  723. matrix.scale( object.scale );
  724. matrix.elements[ 3 ] = 0;
  725. matrix.elements[ 7 ] = 0;
  726. matrix.elements[ 11 ] = 0;
  727. matrix.elements[ 15 ] = 1;
  728. style = getObjectCSSMatrix( matrix, cameraCSSMatrix );
  729. } else {
  730. //style = getObjectCSSMatrix( object.matrixWorld, cameraCSSMatrix );
  731. style = getObjectCSSMatrix( object.matrixWorld, cameraCSSMatrix ) + (isIE ? "" : object.baseScale); //许钟文 加 object.baseScale针对小数,先在elem放大,然后这里缩小
  732. }
  733. var element = object.element;
  734. var cachedObject = cache.objects.get( object );
  735. if ( cachedObject === undefined || cachedObject.style !== style ) {
  736. element.style.WebkitTransform = style;
  737. element.style.transform = style;
  738. var objectData = { style: style };
  739. if ( isIE ) {
  740. objectData.distanceToCameraSquared = getDistanceToSquared( camera, object );
  741. }
  742. cache.objects.set( object, objectData );
  743. }
  744. if ( element.parentNode !== cameraElement ) {
  745. cameraElement.appendChild( element );
  746. }
  747. }
  748. for ( var i = 0, l = object.children.length; i < l; i ++ ) {
  749. renderObject( object.children[ i ], camera, cameraCSSMatrix );
  750. }
  751. }
  752. var getDistanceToSquared = function () {
  753. var a = new Vector3();
  754. var b = new Vector3();
  755. return function ( object1, object2 ) {
  756. a.setFromMatrixPosition( object1.matrixWorld );
  757. b.setFromMatrixPosition( object2.matrixWorld );
  758. return a.distanceToSquared( b );
  759. };
  760. }();
  761. function filterAndFlatten( scene ) {
  762. var result = [];
  763. scene.traverse( function ( object ) {
  764. if ( object instanceof CSS3DObject ) result.push( object );
  765. } );
  766. return result;
  767. }
  768. function zOrder( scene ) {
  769. var sorted = filterAndFlatten( scene ).sort( function ( a, b ) {
  770. var distanceA = cache.objects.get( a ).distanceToCameraSquared;
  771. var distanceB = cache.objects.get( b ).distanceToCameraSquared;
  772. return distanceA - distanceB;
  773. } );
  774. var zMax = sorted.length;
  775. for ( var i = 0, l = sorted.length; i < l; i ++ ) {
  776. sorted[ i ].element.style.zIndex = zMax - i;
  777. }
  778. }
  779. this.render = function ( scene, camera ) {
  780. var fov = camera.projectionMatrix.elements[ 5 ] * _heightHalf;
  781. if ( cache.camera.fov !== fov ) {
  782. if ( camera.isPerspectiveCamera ) {
  783. domElement.style.WebkitPerspective = fov + 'px';
  784. domElement.style.perspective = fov + 'px';
  785. } else {
  786. domElement.style.WebkitPerspective = '';
  787. domElement.style.perspective = '';
  788. }
  789. cache.camera.fov = fov;
  790. }
  791. if ( scene.autoUpdate === true ) scene.updateMatrixWorld();
  792. if ( camera.parent === null ) camera.updateMatrixWorld();
  793. if ( camera.isOrthographicCamera ) {
  794. var tx = - ( camera.right + camera.left ) / 2;
  795. var ty = ( camera.top + camera.bottom ) / 2;
  796. }
  797. var cameraCSSMatrix = camera.isOrthographicCamera ?
  798. 'scale(' + fov + ')' + 'translate(' + epsilon( tx ) + 'px,' + epsilon( ty ) + 'px)' + getCameraCSSMatrix( camera.matrixWorldInverse ) :
  799. 'translateZ(' + fov + 'px)' + getCameraCSSMatrix( camera.matrixWorldInverse );
  800. var style = cameraCSSMatrix +
  801. 'translate(' + _widthHalf + 'px,' + _heightHalf + 'px)';
  802. if ( cache.camera.style !== style && ! isIE ) {
  803. cameraElement.style.WebkitTransform = style;
  804. cameraElement.style.transform = style;
  805. cache.camera.style = style;
  806. }
  807. renderObject( scene, camera, cameraCSSMatrix );
  808. if ( isIE ) {
  809. // IE10 and 11 does not support 'preserve-3d'.
  810. // Thus, z-order in 3D will not work.
  811. // We have to calc z-order manually and set CSS z-index for IE.
  812. // FYI: z-index can't handle object intersection
  813. zOrder( scene );
  814. }
  815. };
  816. };
  817. //export { CSS3DObject, CSS3DSprite, CSS3DRenderer };
  818. })()