|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | + <head> |
| 4 | + <title>three.js webgl - clipIntersection</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 | + <style> |
| 8 | + body { |
| 9 | + margin: 0px; |
| 10 | + background-color: #000000; |
| 11 | + overflow: hidden; |
| 12 | + } |
| 13 | + </style> |
| 14 | + </head> |
| 15 | + <body> |
| 16 | + |
| 17 | + <script src="../build/three.js"></script> |
| 18 | + <script src="js/controls/OrbitControls.js"></script> |
| 19 | + <script src="js/libs/stats.min.js"></script> |
| 20 | + <script src="js/libs/dat.gui.min.js"></script> |
| 21 | + |
| 22 | + <script> |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | + function init() { |
| 27 | + |
| 28 | + camera = new THREE.PerspectiveCamera( |
| 29 | + 40, window.innerWidth / window.innerHeight, 1, 800 ); |
| 30 | + |
| 31 | + camera.position.set( -20, 10, 50 ); |
| 32 | + camera.lookAt( new THREE.Vector3( 0, 0, 0) ); |
| 33 | + |
| 34 | + scene = new THREE.Scene(); |
| 35 | + |
| 36 | + // Lights |
| 37 | + |
| 38 | + var light = new THREE.HemisphereLight( 0xffffbb, 0x080820, 1 ); |
| 39 | + scene.add( light ); |
| 40 | + |
| 41 | + var clipPlanes = [ new THREE.Plane( new THREE.Vector3( 1, 0, 0 ), 0 ), |
| 42 | + new THREE.Plane( new THREE.Vector3( 0, -1, 0 ), 0 ), |
| 43 | + new THREE.Plane( new THREE.Vector3( 0, 0, -1 ), 0 ) ] |
| 44 | + |
| 45 | + scene.add( new THREE.AmbientLight( 0x505050 ) ); |
| 46 | + |
| 47 | + balls = new THREE.Object3D(); |
| 48 | + |
| 49 | + for ( var i = 1; i < 25; i++ ) { |
| 50 | + balls.add( new THREE.Mesh( |
| 51 | + new THREE.SphereBufferGeometry( i / 2, 48, 48), |
| 52 | + new THREE.MeshStandardMaterial( { color: new THREE.Color( Math.sin( i * 0.5 ) * 0.5 + 0.5, |
| 53 | + Math.cos( i * 1.5 ) * 0.5 + 0.5, |
| 54 | + Math.sin( i * 4.5 + 0 ) * 0.5 + 0.5 ), |
| 55 | + roughness: 0.95, metalness: 0.0, side: THREE.DoubleSide, clippingPlanes: clipPlanes, clipIntersection: true } ) |
| 56 | + ) ); |
| 57 | + } |
| 58 | + |
| 59 | + scene.add( balls ); |
| 60 | + |
| 61 | + // Renderer |
| 62 | + |
| 63 | + var container = document.body; |
| 64 | + |
| 65 | + renderer = new THREE.WebGLRenderer(); |
| 66 | + renderer.antialias = true; |
| 67 | + renderer.setPixelRatio( window.devicePixelRatio ); |
| 68 | + renderer.setSize( window.innerWidth, window.innerHeight ); |
| 69 | + renderer.setClearColor( 0x222222 ); |
| 70 | + renderer.localClippingEnabled = true; |
| 71 | + |
| 72 | + window.addEventListener( 'resize', onWindowResize, false ); |
| 73 | + container.appendChild( renderer.domElement ); |
| 74 | + |
| 75 | + |
| 76 | + // Stats |
| 77 | + |
| 78 | + stats = new Stats(); |
| 79 | + container.appendChild( stats.dom ); |
| 80 | + |
| 81 | + // GUI |
| 82 | + |
| 83 | + mode = {}; |
| 84 | + mode.clipIntersection = true; |
| 85 | + mode.clipPosition = 0; |
| 86 | + var gui = new dat.GUI(); |
| 87 | + gui.width = 800; |
| 88 | + gui.add( mode, 'clipIntersection' ).onChange( function() { |
| 89 | + for (var i = 0; i < balls.children.length; i++) { |
| 90 | + balls.children[i].material.clipIntersection = !balls.children[i].material.clipIntersection; |
| 91 | + } |
| 92 | + } ); |
| 93 | + |
| 94 | + gui.add( mode, 'clipPosition', -16, 16 ).onChange( function( value ) { |
| 95 | + |
| 96 | + } ); |
| 97 | + |
| 98 | + // Controls |
| 99 | + |
| 100 | + var controls = new THREE.OrbitControls( camera, renderer.domElement ); |
| 101 | + controls.target.set( 0, 1, 0 ); |
| 102 | + controls.update(); |
| 103 | + |
| 104 | + // Start |
| 105 | + |
| 106 | + startTime = Date.now(); |
| 107 | + time = 0; |
| 108 | + |
| 109 | + } |
| 110 | + |
| 111 | + function onWindowResize() { |
| 112 | + |
| 113 | + camera.aspect = window.innerWidth / window.innerHeight; |
| 114 | + camera.updateProjectionMatrix(); |
| 115 | + |
| 116 | + renderer.setSize( window.innerWidth, window.innerHeight ); |
| 117 | + |
| 118 | + } |
| 119 | + |
| 120 | + function animate() { |
| 121 | + |
| 122 | + |
| 123 | + requestAnimationFrame( animate ); |
| 124 | + |
| 125 | + |
| 126 | + for ( var obj = 0; obj < balls.children.length; obj++ ) { |
| 127 | + var current = balls.children[ obj ].material; |
| 128 | + for ( var i = 0; i < current.clippingPlanes.length; i++ ) { |
| 129 | + var plane = current.clippingPlanes[ i ]; |
| 130 | + plane.constant = ( 49 * plane.constant + mode.clipPosition ) / 50; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + |
| 135 | + stats.begin(); |
| 136 | + renderer.render( scene, camera ); |
| 137 | + stats.end(); |
| 138 | + |
| 139 | + } |
| 140 | + |
| 141 | + init(); |
| 142 | + animate(); |
| 143 | + |
| 144 | + </script> |
| 145 | + |
| 146 | + </body> |
| 147 | +</html> |
0 commit comments