webgl_materials_wireframe.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - wireframe</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl wireframe demo
  12. </div>
  13. <script type="x-shader/x-vertex" id="vertexShader">
  14. in vec3 center;
  15. out vec3 vCenter;
  16. void main() {
  17. vCenter = center;
  18. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  19. }
  20. </script>
  21. <script type="x-shader/x-fragment" id="fragmentShader">
  22. uniform float thickness;
  23. in vec3 vCenter;
  24. out vec4 outColor;
  25. float edgeFactorTri() {
  26. vec3 d = fwidth( vCenter.xyz );
  27. vec3 a3 = smoothstep( vec3( 0.0 ), d * thickness, vCenter.xyz );
  28. return min( min( a3.x, a3.y ), a3.z );
  29. }
  30. void main() {
  31. if ( edgeFactorTri() > 0.99 ) discard;
  32. outColor = gl_FrontFacing ? vec4( 0.9, 0.9, 1.0, 1.0 ) : vec4( 0.4, 0.4, 0.5, 1.0 );
  33. }
  34. </script>
  35. <script type="x-shader/x-vertex" id="vertexShaderATC">
  36. in vec3 center;
  37. out vec3 vCenter;
  38. void main() {
  39. vCenter = center;
  40. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  41. }
  42. </script>
  43. <script type="x-shader/x-fragment" id="fragmentShaderATC">
  44. uniform float thickness;
  45. in vec3 vCenter;
  46. out vec4 outColor;
  47. void main() {
  48. vec3 afwidth = fwidth( vCenter.xyz );
  49. vec3 edge3 = smoothstep( thickness * afwidth, ( thickness + 1.0 ) * afwidth, vCenter.xyz );
  50. float edge = 1.0 - min( min( edge3.x, edge3.y ), edge3.z );
  51. outColor.rgb = gl_FrontFacing ? vec3( 0.9, 0.9, 1.0 ) : vec3( 0.4, 0.4, 0.5 );
  52. outColor.a = edge;
  53. }
  54. </script>
  55. <script type="module">
  56. import * as THREE from '../build/three.module.js';
  57. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  58. import { GUI } from './jsm/libs/dat.gui.module.js';
  59. const API = {
  60. thickness: 1
  61. };
  62. let renderer, scene, camera, mesh2, mesh3;
  63. init();
  64. function init() {
  65. renderer = new THREE.WebGLRenderer( { antialias: true } );
  66. renderer.setPixelRatio( window.devicePixelRatio );
  67. renderer.setSize( window.innerWidth, window.innerHeight );
  68. document.body.appendChild( renderer.domElement );
  69. scene = new THREE.Scene();
  70. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 500 );
  71. camera.position.z = 200;
  72. const controls = new OrbitControls( camera, renderer.domElement );
  73. controls.enablePan = false;
  74. controls.enableZoom = false;
  75. new THREE.BufferGeometryLoader().load( 'models/json/WaltHeadLo_buffergeometry.json', function ( geometry ) {
  76. geometry.deleteAttribute( 'normal' );
  77. geometry.deleteAttribute( 'uv' );
  78. setupAttributes( geometry );
  79. // left
  80. const material1 = new THREE.MeshBasicMaterial( {
  81. color: 0xe0e0ff,
  82. wireframe: true
  83. } );
  84. const mesh1 = new THREE.Mesh( geometry, material1 );
  85. mesh1.position.set( - 60, 0, 0 );
  86. scene.add( mesh1 );
  87. // middle
  88. const material2 = new THREE.ShaderMaterial( {
  89. uniforms: { 'thickness': { value: API.thickness } },
  90. vertexShader: document.getElementById( 'vertexShader' ).textContent,
  91. fragmentShader: document.getElementById( 'fragmentShader' ).textContent,
  92. side: THREE.DoubleSide,
  93. glslVersion: THREE.GLSL3
  94. } );
  95. mesh2 = new THREE.Mesh( geometry, material2 );
  96. mesh2.position.set( 0, 0, 0 );
  97. scene.add( mesh2 );
  98. // right
  99. const material3 = new THREE.ShaderMaterial( {
  100. uniforms: { 'thickness': { value: API.thickness } },
  101. vertexShader: document.getElementById( 'vertexShaderATC' ).textContent,
  102. fragmentShader: document.getElementById( 'fragmentShaderATC' ).textContent,
  103. side: THREE.DoubleSide,
  104. glslVersion: THREE.GLSL3,
  105. alphaToCoverage: true // only works when WebGLRenderer's "antialias" is set to "true"
  106. } );
  107. mesh3 = new THREE.Mesh( geometry, material3 );
  108. mesh3.position.set( 60, 0, 0 );
  109. scene.add( mesh3 );
  110. //
  111. animate();
  112. } );
  113. //
  114. const gui = new GUI();
  115. gui.add( API, 'thickness', 0.4, 4 ).onChange( function () {
  116. mesh2.material.uniforms.thickness.value = API.thickness;
  117. mesh3.material.uniforms.thickness.value = API.thickness;
  118. } );
  119. gui.open();
  120. //
  121. window.addEventListener( 'resize', onWindowResize );
  122. }
  123. function setupAttributes( geometry ) {
  124. const vectors = [
  125. new THREE.Vector3( 1, 0, 0 ),
  126. new THREE.Vector3( 0, 1, 0 ),
  127. new THREE.Vector3( 0, 0, 1 )
  128. ];
  129. const position = geometry.attributes.position;
  130. const centers = new Float32Array( position.count * 3 );
  131. for ( let i = 0, l = position.count; i < l; i ++ ) {
  132. vectors[ i % 3 ].toArray( centers, i * 3 );
  133. }
  134. geometry.setAttribute( 'center', new THREE.BufferAttribute( centers, 3 ) );
  135. }
  136. function onWindowResize() {
  137. camera.aspect = window.innerWidth / window.innerHeight;
  138. camera.updateProjectionMatrix();
  139. renderer.setSize( window.innerWidth, window.innerHeight );
  140. }
  141. function animate() {
  142. requestAnimationFrame( animate );
  143. renderer.render( scene, camera );
  144. }
  145. </script>
  146. </body>
  147. </html>