webgl_materials_wireframe.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - wireframe</title>
  5. <meta charset="utf-8">
  6. <style>
  7. body {
  8. margin: 0px;
  9. background-color: #000000;
  10. overflow: hidden;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <script src="../build/three.min.js"></script>
  16. <script type="x-shader/x-vertex" id="vertexShader">
  17. attribute vec3 center;
  18. varying vec3 vCenter;
  19. void main() {
  20. vCenter = center;
  21. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  22. }
  23. </script>
  24. <script type="x-shader/x-fragment" id="fragmentShader">
  25. varying vec3 vCenter;
  26. float edgeFactorTri() {
  27. vec3 d = fwidth( vCenter.xyz );
  28. vec3 a3 = smoothstep( vec3( 0.0 ), d * 1.5, vCenter.xyz );
  29. return min( min( a3.x, a3.y ), a3.z );
  30. }
  31. void main() {
  32. gl_FragColor.rgb = mix( vec3( 1.0 ), vec3( 0.2 ), edgeFactorTri() );
  33. gl_FragColor.a = 1.0;
  34. }
  35. </script>
  36. <script>
  37. var camera, scene, renderer;
  38. init();
  39. animate();
  40. function init() {
  41. var geometry, material, mesh;
  42. var size = 150;
  43. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 2000 );
  44. camera.position.z = 800;
  45. scene = new THREE.Scene();
  46. //
  47. geometry = new THREE.BoxGeometry( size, size, size );
  48. material = new THREE.MeshBasicMaterial( { wireframe: true } );
  49. mesh = new THREE.Mesh( geometry, material );
  50. mesh.position.x = -150;
  51. scene.add( mesh );
  52. //
  53. geometry = new THREE.BufferGeometry().fromGeometry( new THREE.BoxGeometry( size, size, size ) );
  54. setupAttributes( geometry );
  55. material = new THREE.ShaderMaterial( {
  56. uniforms: {},
  57. vertexShader: document.getElementById( 'vertexShader' ).textContent,
  58. fragmentShader: document.getElementById( 'fragmentShader' ).textContent
  59. } );
  60. material.extensions.derivatives = true;
  61. mesh = new THREE.Mesh( geometry, material );
  62. mesh.position.x = 150;
  63. scene.add( mesh );
  64. //
  65. geometry = new THREE.BufferGeometry().fromGeometry( new THREE.SphereGeometry( size / 2, 32, 16 ) );
  66. setupAttributes( geometry );
  67. material = new THREE.ShaderMaterial( {
  68. uniforms: {},
  69. vertexShader: document.getElementById( 'vertexShader' ).textContent,
  70. fragmentShader: document.getElementById( 'fragmentShader' ).textContent
  71. } );
  72. material.extensions.derivatives = true;
  73. mesh = new THREE.Mesh( geometry, material );
  74. mesh.position.x = -150;
  75. scene.add( mesh );
  76. // renderer
  77. renderer = new THREE.WebGLRenderer( { antialias: true } );
  78. renderer.setPixelRatio( window.devicePixelRatio );
  79. renderer.setSize( window.innerWidth, window.innerHeight );
  80. document.body.appendChild( renderer.domElement );
  81. // events
  82. window.addEventListener( 'resize', onWindowResize, false );
  83. }
  84. function setupAttributes( geometry ) {
  85. // TODO: Bring back quads
  86. var vectors = [
  87. new THREE.Vector3( 1, 0, 0 ),
  88. new THREE.Vector3( 0, 1, 0 ),
  89. new THREE.Vector3( 0, 0, 1 )
  90. ];
  91. var position = geometry.attributes.position;
  92. var centers = new Float32Array( position.count * 3 );
  93. for ( var i = 0, l = position.count; i < l; i ++ ) {
  94. vectors[ i % 3 ].toArray( centers, i * 3 );
  95. }
  96. geometry.addAttribute( 'center', new THREE.BufferAttribute( centers, 3 ) );
  97. }
  98. function onWindowResize() {
  99. camera.aspect = window.innerWidth / window.innerHeight;
  100. camera.updateProjectionMatrix();
  101. renderer.setSize( window.innerWidth, window.innerHeight );
  102. }
  103. function animate() {
  104. requestAnimationFrame( animate );
  105. for ( var i = 0; i < scene.children.length; i ++ ) {
  106. var object = scene.children[ i ];
  107. object.rotation.x += 0.005;
  108. object.rotation.y += 0.01;
  109. }
  110. renderer.render( scene, camera );
  111. }
  112. </script>
  113. </body>
  114. </html>