浏览代码

Examples: Add external three-bvh-csg demo (#25299)

* Add csg example

* Add screenshot, small updates

* Update tags.json

* Update webgl_geometry_csg.html

* Update webgl_geometry_csg.html

Co-authored-by: Michael Herzog <[email protected]>
Garrett Johnson 2 年之前
父节点
当前提交
a628b7f511
共有 4 个文件被更改,包括 237 次插入0 次删除
  1. 1 0
      examples/files.json
  2. 二进制
      examples/screenshots/webgl_geometry_csg.jpg
  3. 1 0
      examples/tags.json
  4. 235 0
      examples/webgl_geometry_csg.html

+ 1 - 0
examples/files.json

@@ -27,6 +27,7 @@
 		"webgl_geometry_colors",
 		"webgl_geometry_colors",
 		"webgl_geometry_colors_lookuptable",
 		"webgl_geometry_colors_lookuptable",
 		"webgl_geometry_convex",
 		"webgl_geometry_convex",
+		"webgl_geometry_csg",
 		"webgl_geometry_cube",
 		"webgl_geometry_cube",
 		"webgl_geometry_dynamic",
 		"webgl_geometry_dynamic",
 		"webgl_geometry_extrude_shapes",
 		"webgl_geometry_extrude_shapes",

二进制
examples/screenshots/webgl_geometry_csg.jpg


+ 1 - 0
examples/tags.json

@@ -9,6 +9,7 @@
 	"webgl_geometries": [ "geometry" ],
 	"webgl_geometries": [ "geometry" ],
 	"webgl_geometries_parametric": [ "geometry" ],
 	"webgl_geometries_parametric": [ "geometry" ],
 	"webgl_geometry_colors_lookuptable": [ "vertex" ],
 	"webgl_geometry_colors_lookuptable": [ "vertex" ],
+	"webgl_geometry_csg": [ "external", "csg", "bvh", "constructive", "solid", "geometry", "games", "level" ],
 	"webgl_geometry_nurbs": [ "curve", "surface" ],
 	"webgl_geometry_nurbs": [ "curve", "surface" ],
 	"webgl_geometry_spline_editor": [ "curve" ],
 	"webgl_geometry_spline_editor": [ "curve" ],
 	"webgl_geometry_terrain": [ "fog" ],
 	"webgl_geometry_terrain": [ "fog" ],

+ 235 - 0
examples/webgl_geometry_csg.html

@@ -0,0 +1,235 @@
+<!DOCTYPE html>
+<html lang="en">
+	<head>
+		<title>three.js geometry - csg</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">
+		<style>
+			body {
+				background-color: #eeeeee;
+				color: #333;
+			}
+
+			a {
+				color: #009688;
+				text-decoration: underline;
+			}
+		</style>
+	</head>
+
+	<body>
+		<div id="info">
+			<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> bvh csg - <a href="https://github.com/gkjohnson/three-bvh-csg" target="_blank" rel="noopener">three-bvh-csg</a><br/>
+			See <a href="https://github.com/gkjohnson/three-bvh-csg" target="_blank" rel="noopener">main project repository</a> for more information and examples on constructive solid geometry.
+		</div>
+
+		<!-- Import maps polyfill -->
+		<!-- Remove this when import maps will be widely supported -->
+		<script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
+
+		<script type="importmap">
+			{
+				"imports": {
+					"three": "../build/three.module.js",
+					"three/addons/": "./jsm/",
+					"three-mesh-bvh": "https://unpkg.com/three-mesh-bvh@^0.5.22/build/index.module.js",
+					"three-bvh-csg": "https://unpkg.com/three-bvh-csg@^0.0.3/build/index.module.js"
+				}
+			}
+		</script>
+
+		<script type="module">
+
+			import * as THREE from 'three';
+			import Stats from 'three/addons/libs/stats.module.js';
+			import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
+			import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
+			import { SUBTRACTION, INTERSECTION, ADDITION, Brush, Evaluator } from 'three-bvh-csg';
+
+			let stats;
+			let camera, scene, renderer;
+			let baseBrush, brush;
+			let core;
+			let result, evaluator, wireframe;
+
+			const params = {
+
+				operation: SUBTRACTION,
+				useGroups: true,
+				wireframe: false,
+
+			};
+
+			init();
+			animate();
+
+			function init() {
+
+				// environment
+				camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 100 );
+				camera.position.set( - 1, 1, 1 ).normalize().multiplyScalar( 10 );
+
+				scene = new THREE.Scene();
+				scene.background = new THREE.Color( 0xfce4ec );
+
+				// lights
+				const ambient = new THREE.HemisphereLight( 0xffffff, 0xbfd4d2, 0.9 );
+				scene.add( ambient );
+
+				const directionalLight = new THREE.DirectionalLight( 0xffffff, 0.1 );
+				directionalLight.position.set( 1, 4, 3 ).multiplyScalar( 3 );
+				directionalLight.castShadow = true;
+				directionalLight.shadow.mapSize.setScalar( 2048 );
+				directionalLight.shadow.bias = - 1e-4;
+				directionalLight.shadow.normalBias = 1e-4;
+				scene.add( directionalLight );
+
+				// renderer
+				renderer = new THREE.WebGLRenderer( { antialias: true } );
+				renderer.setPixelRatio( window.devicePixelRatio );
+				renderer.setSize( window.innerWidth, window.innerHeight );
+				renderer.shadowMap.enabled = true;
+				renderer.shadowMap.type = THREE.PCFSoftShadow;
+				document.body.appendChild( renderer.domElement );
+
+				stats = new Stats();
+				document.body.appendChild( stats.dom );
+
+				// add shadow plane
+				const plane = new THREE.Mesh(
+					new THREE.PlaneGeometry(),
+					new THREE.ShadowMaterial( {
+						color: 0xd81b60,
+						transparent: true,
+						opacity: 0.075,
+						side: THREE.DoubleSide,
+					} ),
+				);
+				plane.position.y = - 3;
+				plane.rotation.x = - Math.PI / 2;
+				plane.scale.setScalar( 10 );
+				plane.receiveShadow = true;
+				scene.add( plane );
+
+				// create brushes
+				evaluator = new Evaluator();
+
+				baseBrush = new Brush(
+					new THREE.IcosahedronGeometry( 2, 3 ),
+					new THREE.MeshStandardMaterial( {
+						flatShading: true,
+
+						polygonOffset: true,
+						polygonOffsetUnits: 1,
+						polygonOffsetFactor: 1,
+					} ),
+				);
+
+				brush = new Brush(
+					new THREE.CylinderGeometry( 1, 1, 5, 45 ),
+					new THREE.MeshStandardMaterial( {
+						color: 0x80cbc4,
+
+						polygonOffset: true,
+						polygonOffsetUnits: 1,
+						polygonOffsetFactor: 1,
+
+					} ),
+				);
+
+				core = new Brush(
+					new THREE.IcosahedronGeometry( 0.1, 1 ),
+					new THREE.MeshStandardMaterial( {
+						flatShading: true,
+						color: 0xff9800,
+						emissive: 0xff9800,
+						emissiveIntensity: 0.35,
+
+						polygonOffset: true,
+						polygonOffsetUnits: 1,
+						polygonOffsetFactor: 1,
+
+					} ),
+				);
+				scene.add( core );
+
+				// create wireframe
+				wireframe = new THREE.Mesh(
+					undefined,
+					new THREE.MeshBasicMaterial( { color: 0x009688, wireframe: true } ),
+				);
+				scene.add( wireframe );
+
+				// controls
+				const controls = new OrbitControls( camera, renderer.domElement );
+				controls.minDistance = 5;
+				controls.maxDistance = 75;
+
+				// set up gui
+				const gui = new GUI();
+				gui.add( params, 'operation', { SUBTRACTION, INTERSECTION, ADDITION } );
+				
+				// disabling wireframe until next CSG release. See mrdoob/three.js#25299
+				// gui.add( params, 'wireframe' );
+				gui.add( params, 'useGroups' );
+
+				window.addEventListener( 'resize', onWindowResize );
+				onWindowResize();
+
+			}
+
+			function updateCSG() {
+
+				evaluator.useGroups = params.useGroups;
+				result = evaluator.evaluate( baseBrush, brush, params.operation, result );
+
+				result.castShadow = true;
+				result.receiveShadow = true;
+				scene.add( result );
+
+			}
+
+			function onWindowResize() {
+
+				camera.aspect = window.innerWidth / window.innerHeight;
+				camera.updateProjectionMatrix();
+
+				renderer.setSize( window.innerWidth, window.innerHeight );
+
+			}
+
+			function animate() {
+
+				requestAnimationFrame( animate );
+
+				// update the transforms
+				const t = window.performance.now() + 9000;
+				baseBrush.rotation.x = t * 0.0001;
+				baseBrush.rotation.y = t * 0.00025;
+				baseBrush.rotation.z = t * 0.0005;
+				baseBrush.updateMatrixWorld();
+
+				brush.rotation.x = t * - 0.0002;
+				brush.rotation.y = t * - 0.0005;
+				brush.rotation.z = t * - 0.001;
+
+				const s = 0.5 + 0.5 * ( 1 + Math.sin( t * 0.001 ) );
+				brush.scale.set( s, 1, s );
+				brush.updateMatrixWorld();
+
+				// update the csg
+				updateCSG();
+
+				wireframe.geometry = result.geometry;
+				wireframe.visible = params.wireframe;
+
+				renderer.render( scene, camera );
+				stats.update();
+
+			}
+
+		</script>
+
+	</body>
+</html>