console
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - geometry - shapes</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
font-family: Monospace;
background-color: #f0f0f0;
margin: 0px;
overflow: hidden;
}
</style>
</head>
<body>
<dir>
<input type="text" id = "qiangSize">
<input type="button" id = "qiangSizeBtn" value="设置" onclick="reSizeQiang()">
</dir>
<dir id="3DCon">
</dir>
<script src="https://cdn.jsdelivr.net/npm/three@0.110.0/build/three.min.js"></script>
<!-- <script src="js/controls/MapControls.js"></script> -->
<script src="https://cdn.jsdelivr.net/npm/three@0.110.0/examples/js/controls/OrbitControls.js"></script>
<script>
var container;
var camera, scene, renderer;
var controls;
var targetRotation = 0;
var targetRotationOnMouseDown = 0;
var mouseX = 0;
var mouseXOnMouseDown = 0;
var windowHalfX = window.innerWidth / 2;
var loader = new THREE.TextureLoader();
var texture = loader.load( "https://icweiliimg6.pstatp.com/weili/l/919894815608406120.webp" );
// it's necessary to apply these settings in order to correctly display the texture on a shape geometry
texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
texture.repeat.set( 0.008, 0.008 );
init();
animate();
function reSizeQiang() {
const newSize = Number(document.getElementById( 'qiangSize' ).value)
for (let idx = scene.children.length; idx--; idx >= 0 ) {
let objMesh = scene.children[idx]
if (objMesh.type2 === "qiang") {
var geometry = objMesh.geometry
for (let idx2 in geometry.attributes.position.array) {
if (geometry.attributes.position.array[idx2] === 320) {
geometry.attributes.position.array[idx2] = newSize;
}
}
for (let idx2 in geometry.attributes.uv.array) {
if (geometry.attributes.uv.array[idx2] === 320) {
geometry.attributes.uv.array[idx2] = newSize;
}
}
var mesh = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { side: THREE.DoubleSide, map: texture } ) );
mesh.position.set( objMesh.position.x, objMesh.position.y, objMesh.position.z);
mesh.rotation.set( objMesh.rotation.x, objMesh.rotation.y, objMesh.rotation.z );
mesh.scale.set( objMesh.scale.x, objMesh.scale.y, objMesh.scale.z );
mesh.type2 = "qiang"
scene.add( mesh );
objMesh.geometry.dispose(); // 删除几何体
objMesh.material.dispose(); // 删除材质
scene.remove(objMesh);
debugger
}
}
}
function addExtrudeShape( bGeometry, pi, rt, s ) {
// 原先是geometry, 但是bGeometry也可以
var mesh = new THREE.Mesh( bGeometry, new THREE.MeshBasicMaterial( { side: THREE.DoubleSide, map: texture } ) );
mesh.position.set( pi.x, pi.y, pi.z - 10 );
mesh.rotation.set( rt.x, rt.y, rt.z );
mesh.scale.set( s, s, s );
mesh.type2 = "qiang"
scene.add( mesh );
}
function addShape( shape, pi, rt, s ) {
var geometry = new THREE.ShapeBufferGeometry( shape );
var bGeometry = new THREE.BufferGeometry();
bGeometry.attributes.position = geometry.attributes.position;
bGeometry.attributes.uv = geometry.attributes.uv;
bGeometry.index = geometry.index
var mesh = new THREE.Mesh( bGeometry, new THREE.MeshBasicMaterial( { side: THREE.DoubleSide, map: texture } ) );
mesh.position.set( pi.x, pi.y, pi.z );
mesh.rotation.set( rt.x, rt.y, rt.z );
mesh.scale.set( s, s, s );
// group.add( mesh );
scene.add( mesh );
}
function QiangBufferGeometry(wdth, height, depth) {
var squareShape = new THREE.Shape();
squareShape.moveTo( 0, 0 );
squareShape.lineTo( 0, height);
squareShape.lineTo( wdth, height);
squareShape.lineTo( wdth, 0 );
squareShape.lineTo( 0, 0 );
var squareShapePath = new THREE.Path();
squareShapePath.moveTo( 10, 10 );
squareShapePath.lineTo( 10, height -10);
squareShapePath.lineTo( wdth - 10, height - 10);
squareShapePath.lineTo( wdth -10 , 10 );
squareShapePath.lineTo( 10, 10 );
squareShape.holes.push( squareShapePath );// 挖洞
var extrudeSettings = { depth: depth, bevelEnabled: true, bevelSegments: 0, steps: 1, bevelSize: 0, bevelThickness: 0 };
// extruded shape
var geometry = new THREE.ExtrudeBufferGeometry( squareShape, extrudeSettings );
// ExtrudeBufferGeometry转换BufferGeometry
var bGeometry = new THREE.BufferGeometry();
// bGeometry.attributes.normal = geometry.attributes.normal;
bGeometry.attributes.position = geometry.attributes.position;
bGeometry.attributes.uv = geometry.attributes.uv;
return bGeometry;
}
function init() {
container = document.getElementById( '3DCon' );
scene = new THREE.Scene();
scene.background = new THREE.Color( 0xf0f0f0 );
var axes = new THREE.AxesHelper(10);
scene.add(axes);
camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
camera.position.set( 0, 100, 500 );
camera.up.x = 0;
camera.up.y = 0;
camera.up.z = 1;//相机以Z方向为上方
// scene.add( camera );
controls = new THREE.MapControls( camera, container );
var light = new THREE.PointLight( 0xffffff, 0.8 );
camera.add( light );
// Square
const sqLength = 80;
const squareShape = QiangBufferGeometry(sqLength * 4, sqLength, 8);
addExtrudeShape( squareShape, {x: -sqLength * 2, y: -sqLength * 2, z: 0}, {x: Math.PI / 2, y: 0, z: 0}, 1 );
addExtrudeShape( squareShape, {x: -sqLength * 2, y: sqLength * 2, z: 0}, {x: Math.PI / 2, y: 0, z: 0}, 1 );
addExtrudeShape( squareShape, {x: -sqLength * 2, y: -sqLength * 2, z: 0}, {x: Math.PI / 2, y: Math.PI / 2, z: 0}, 1 );
addExtrudeShape( squareShape, {x: sqLength * 2-8, y: -sqLength * 2, z: 0}, {x: Math.PI / 2, y: Math.PI / 2, z: 0}, 1 );
// Square
var sqDibanLength = 80;
var squareDibanShape = new THREE.Shape();
squareDibanShape.moveTo( -sqDibanLength, -sqDibanLength );
squareDibanShape.lineTo( -sqDibanLength, sqDibanLength );
squareDibanShape.lineTo( sqDibanLength, sqDibanLength );
squareDibanShape.lineTo( sqDibanLength, -sqDibanLength );
squareDibanShape.lineTo( -sqDibanLength, -sqDibanLength );
addShape( squareDibanShape, {x: - sqDibanLength / 2, y: 0, z: 0}, {x: 0, y: 0, z: 0}, 1 );
//
onWindowResize1();
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
//document.addEventListener( 'mousedown', onDocumentMouseDown, false );
//document.addEventListener( 'touchstart', onDocumentTouchStart, false );
//document.addEventListener( 'touchmove', onDocumentTouchMove, false );
//
window.addEventListener( 'resize', onWindowResize, false );
}
function onWindowResize1() {
// let arrs = [{x: 832.0999999999999, y: -310.1},
// {x: 782.0999999999999, y: -310.1},
// {x: 782.0999999999999, y: -260.1},
// {x: 832.0999999999999, y: -260.1},
// {x: 832.0999999999999, y: -310.1}]
let arrs = [{x: 782, y: -260.1},
{x: 782, y: -310.1},
{x: 732, y: -310.1},
{x: 732, y: -260.1},{x: 782, y: -260.1}]
let shape = new THREE.Shape(arrs);
// let Geometry1 = new THREE.ShapeGeometry(shape);
// var mesh1 = new THREE.Mesh( Geometry1, new THREE.MeshBasicMaterial( { side: THREE.DoubleSide, map: texture } ) );
// scene.add(mesh1)
// addShape(new THREE.Shape(arrs),{x: - 1000 / 2, y: 0, z: 0}, {x: 0, y: 0, z: 0}, 1)
let arr1 = [{x: 751.5, y: -266.3},
{x: 781.5, y: -266.3},
{x: 781.5, y: -296.3},
{x: 751.5, y: -296.3},
{x: 751.5, y: -266.3}]
let hole = new THREE.Shape(arr1);
shape.holes.push(hole)
let Geometry = new THREE.ShapeGeometry(shape)
var mesh = new THREE.Mesh( Geometry, new THREE.MeshBasicMaterial( { side: THREE.DoubleSide, map: texture } ) );
scene.add(mesh)
// addShape(new THREE.Shape(shape),{x: - 1000 / 2, y: 0, z: 0}, {x: 0, y: 0, z: 0}, 1)
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
//
function onDocumentMouseDown( event ) {
event.preventDefault();
document.addEventListener( 'mousemove', onDocumentMouseMove, false );
document.addEventListener( 'mouseup', onDocumentMouseUp, false );
document.addEventListener( 'mouseout', onDocumentMouseOut, false );
mouseXOnMouseDown = event.clientX - windowHalfX;
targetRotationOnMouseDown = targetRotation;
}
function onDocumentMouseMove( event ) {
mouseX = event.clientX - windowHalfX;
targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
}
function onDocumentMouseUp() {
document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
}
function onDocumentMouseOut() {
document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
}
function onDocumentTouchStart( event ) {
if ( event.touches.length == 1 ) {
event.preventDefault();
mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
targetRotationOnMouseDown = targetRotation;
}
}
function onDocumentTouchMove( event ) {
if ( event.touches.length == 1 ) {
event.preventDefault();
mouseX = event.touches[ 0 ].pageX - windowHalfX;
targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
}
}
//
function animate() {
requestAnimationFrame( animate );
render();
}
function render() {
//group.rotation.y += ( targetRotation - group.rotation.y ) * 0.05;
renderer.render( scene, camera );
}
</script>
</body>
</html>