123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - materials - wireframe</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="main.css">
- </head>
- <body>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl wireframe demo
- </div>
- <script type="x-shader/x-vertex" id="vertexShader">
- attribute vec3 center;
- varying vec3 vCenter;
- void main() {
- vCenter = center;
- gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
- }
- </script>
- <script type="x-shader/x-fragment" id="fragmentShader">
- uniform float widthFactor;
- varying vec3 vCenter;
- float edgeFactorTri() {
- vec3 d = fwidth( vCenter.xyz );
- vec3 a3 = smoothstep( vec3( 0.0 ), d * widthFactor, vCenter.xyz );
- return min( min( a3.x, a3.y ), a3.z );
- }
- void main() {
- if ( edgeFactorTri() > 0.99 ) discard;
- gl_FragColor = gl_FrontFacing ? vec4( 0.9, 0.9, 1.0, 1.0 ) : vec4( 0.4, 0.4, 0.5, 1.0 );
- }
- </script>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import { GUI } from './jsm/libs/dat.gui.module.js';
- var API = {
- widthFactor: 1
- };
- var renderer, scene, camera, mesh1, mesh2;
- init();
- function init() {
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- document.body.appendChild( renderer.domElement );
- scene = new THREE.Scene();
- camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 500 );
- camera.position.z = 150;
- new THREE.BufferGeometryLoader().load( 'models/json/WaltHeadLo_buffergeometry.json', function ( geometry ) {
- // on the left
- var material = new THREE.MeshBasicMaterial( {
- color: 0xe0e0ff,
- wireframe: true
- } );
- mesh1 = new THREE.Mesh( geometry, material );
- mesh1.position.set( - 40, 0, 0 );
- scene.add( mesh1 );
- // on the right
- var uniforms = { 'widthFactor': { value: API.widthFactor } };
- var material = new THREE.ShaderMaterial( {
- uniforms: uniforms,
- vertexShader: document.getElementById( 'vertexShader' ).textContent,
- fragmentShader: document.getElementById( 'fragmentShader' ).textContent,
- side: THREE.DoubleSide
- } );
- material.extensions.derivatives = true;
- geometry.deleteAttribute( 'normal' );
- geometry.deleteAttribute( 'uv' );
- setupAttributes( geometry );
- mesh2 = new THREE.Mesh( geometry, material );
- mesh2.position.set( 40, 0, 0 );
- scene.add( mesh2 );
- //
- animate();
- } );
- //
- var gui = new GUI();
- gui.add( API, 'widthFactor', 0.4, 4 ).onChange( function () {
- mesh2.material.uniforms.widthFactor.value = API.widthFactor;
- } );
- gui.open();
- //
- window.addEventListener( 'resize', onWindowResize, false );
- }
- function setupAttributes( geometry ) {
- var vectors = [
- new THREE.Vector3( 1, 0, 0 ),
- new THREE.Vector3( 0, 1, 0 ),
- new THREE.Vector3( 0, 0, 1 )
- ];
- var position = geometry.attributes.position;
- var centers = new Float32Array( position.count * 3 );
- for ( var i = 0, l = position.count; i < l; i ++ ) {
- vectors[ i % 3 ].toArray( centers, i * 3 );
- }
- geometry.setAttribute( 'center', new THREE.BufferAttribute( centers, 3 ) );
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- function animate() {
- requestAnimationFrame( animate );
- mesh1.rotation.y += 0.01;
- mesh2.rotation.y += 0.01;
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|