webgl_materials_wireframe.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. attribute vec3 center;
  15. varying 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 widthFactor;
  23. varying vec3 vCenter;
  24. float edgeFactorTri() {
  25. vec3 d = fwidth( vCenter.xyz );
  26. vec3 a3 = smoothstep( vec3( 0.0 ), d * widthFactor, vCenter.xyz );
  27. return min( min( a3.x, a3.y ), a3.z );
  28. }
  29. void main() {
  30. if ( edgeFactorTri() > 0.99 ) discard;
  31. gl_FragColor = gl_FrontFacing ? vec4( 0.9, 0.9, 1.0, 1.0 ) : vec4( 0.4, 0.4, 0.5, 1.0 );
  32. }
  33. </script>
  34. <script type="module">
  35. import * as THREE from '../build/three.module.js';
  36. import { GUI } from './jsm/libs/dat.gui.module.js';
  37. var API = {
  38. widthFactor: 1
  39. };
  40. var renderer, scene, camera, mesh1, mesh2;
  41. init();
  42. function init() {
  43. renderer = new THREE.WebGLRenderer( { antialias: true } );
  44. renderer.setPixelRatio( window.devicePixelRatio );
  45. renderer.setSize( window.innerWidth, window.innerHeight );
  46. document.body.appendChild( renderer.domElement );
  47. scene = new THREE.Scene();
  48. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 500 );
  49. camera.position.z = 150;
  50. new THREE.BufferGeometryLoader().load( 'models/json/WaltHeadLo_buffergeometry.json', function ( geometry ) {
  51. // on the left
  52. var material = new THREE.MeshBasicMaterial( {
  53. color: 0xe0e0ff,
  54. wireframe: true
  55. } );
  56. mesh1 = new THREE.Mesh( geometry, material );
  57. mesh1.position.set( - 40, 0, 0 );
  58. scene.add( mesh1 );
  59. // on the right
  60. var uniforms = { 'widthFactor': { value: API.widthFactor } };
  61. var material = new THREE.ShaderMaterial( {
  62. uniforms: uniforms,
  63. vertexShader: document.getElementById( 'vertexShader' ).textContent,
  64. fragmentShader: document.getElementById( 'fragmentShader' ).textContent,
  65. side: THREE.DoubleSide
  66. } );
  67. material.extensions.derivatives = true;
  68. geometry.deleteAttribute( 'normal' );
  69. geometry.deleteAttribute( 'uv' );
  70. setupAttributes( geometry );
  71. mesh2 = new THREE.Mesh( geometry, material );
  72. mesh2.position.set( 40, 0, 0 );
  73. scene.add( mesh2 );
  74. //
  75. animate();
  76. } );
  77. //
  78. var gui = new GUI();
  79. gui.add( API, 'widthFactor', 0.4, 4 ).onChange( function () {
  80. mesh2.material.uniforms.widthFactor.value = API.widthFactor;
  81. } );
  82. gui.open();
  83. //
  84. window.addEventListener( 'resize', onWindowResize, false );
  85. }
  86. function setupAttributes( geometry ) {
  87. var vectors = [
  88. new THREE.Vector3( 1, 0, 0 ),
  89. new THREE.Vector3( 0, 1, 0 ),
  90. new THREE.Vector3( 0, 0, 1 )
  91. ];
  92. var position = geometry.attributes.position;
  93. var centers = new Float32Array( position.count * 3 );
  94. for ( var i = 0, l = position.count; i < l; i ++ ) {
  95. vectors[ i % 3 ].toArray( centers, i * 3 );
  96. }
  97. geometry.setAttribute( 'center', new THREE.BufferAttribute( centers, 3 ) );
  98. }
  99. function onWindowResize() {
  100. camera.aspect = window.innerWidth / window.innerHeight;
  101. camera.updateProjectionMatrix();
  102. renderer.setSize( window.innerWidth, window.innerHeight );
  103. }
  104. function animate() {
  105. requestAnimationFrame( animate );
  106. mesh1.rotation.y += 0.01;
  107. mesh2.rotation.y += 0.01;
  108. renderer.render( scene, camera );
  109. }
  110. </script>
  111. </body>
  112. </html>