<%@ Page Language="C#" CodePage="65001" ContentType="text/html; charset=utf-8" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>3D Water Pump Model Viewer</title>
<link href="Content/bootstrap.min.css" rel="stylesheet" />
<style type="text/css">
body {
margin: 0;
padding: 20px;
background-color: #f5f5f5;
}
.container {
max-width: 1200px;
margin: 0 auto;
}
h1 {
text-align: center;
margin-bottom: 30px;
}
#canvas-container {
width: 100%;
height: 600px;
border: 2px solid #ddd;
border-radius: 5px;
margin-bottom: 20px;
background-color: #fff;
overflow: hidden;
}
.button-group {
text-align: center;
margin-bottom: 20px;
}
.btn {
margin: 0 10px;
padding: 10px 20px;
font-size: 16px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div class="container">
<h1>3D Water Pump Model Viewer</h1>
<div class="button-group">
<button type="button" id="startBtn" class="btn btn-success">Start Motor</button>
<button type="button" id="stopBtn" class="btn btn-danger">Stop Motor</button>
</div>
<div id="canvas-container">
<canvas id="myCanvas"></canvas>
</div>
</div>
</form>
<!-- Include Three.js library and components as ES modules -->
<script type="module">
// Import Three.js modules
import * as THREE from '/Scripts/threejs/three.module.js';
import { GLTFLoader } from '/Scripts/threejs/GLTFLoader.js';
import { OrbitControls } from '/Scripts/threejs/OrbitControls.js';
// Initialize Three.js scene
var scene, camera, renderer, controls, model;
var modelColor = 0xff0000; // Default red color
function init() {
console.log('Initializing Three.js scene');
// Create scene
scene = new THREE.Scene();
scene.background = new THREE.Color(0xffffff); // White background for better contrast
// Create camera
camera = new THREE.PerspectiveCamera(60, document.getElementById('canvas-container').clientWidth / document.getElementById('canvas-container').clientHeight, 0.1, 1000);
camera.position.set(5, 5, 5); // Move camera further away and at an angle
// Create renderer
renderer = new THREE.WebGLRenderer({ canvas: document.getElementById('myCanvas'), antialias: true });
renderer.setSize(document.getElementById('canvas-container').clientWidth, document.getElementById('canvas-container').clientHeight);
renderer.setPixelRatio(window.devicePixelRatio);
// Add orbit controls
controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.05;
// Add lights
var ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
scene.add(ambientLight);
var directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
directionalLight.position.set(1, 1, 1);
scene.add(directionalLight);
var directionalLight2 = new THREE.DirectionalLight(0xffffff, 0.5);
directionalLight2.position.set(-1, -1, -1);
scene.add(directionalLight2);
// Add a visible grid to help with orientation
var gridHelper = new THREE.GridHelper(10, 10);
scene.add(gridHelper);
// Add axes helper
var axesHelper = new THREE.AxesHelper(5);
scene.add(axesHelper);
// Add a simple sphere to verify Three.js is working
var sphereGeometry = new THREE.SphereGeometry(1, 32, 32);
var sphereMaterial = new THREE.MeshStandardMaterial({ color: 0xff0000 });
var sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
sphere.position.set(0, 0, 0);
scene.add(sphere);
console.log('Sphere added to scene');
// Load 3D model
console.log('Starting to load model...');
var loader = new GLTFLoader();
// First, check if the model file exists
fetch('/3D/shuibneg.glb')
.then(response => {
if (response.ok) {
console.log('Model file exists, starting to load...');
loader.load('/3D/shuibneg.glb',
function (gltf) {
console.log('Model loaded successfully!');
model = gltf.scene;
// Remove the sphere
scene.remove(sphere);
// Adjust model size and position
console.log('Original model bounding box:', new THREE.Box3().setFromObject(model).getSize(new THREE.Vector3()));
// Center the model
const box = new THREE.Box3().setFromObject(model);
const center = box.getCenter(new THREE.Vector3());
model.position.sub(center);
// Scale model to fit in view
const size = box.getSize(new THREE.Vector3());
const maxDim = Math.max(size.x, size.y, size.z);
const fov = camera.fov * (Math.PI / 180);
let cameraZ = Math.abs(maxDim / 2 / Math.tan(fov / 2));
cameraZ *= 1.5; // Add a little extra space
camera.position.set(0, 0, cameraZ);
// Look at model
const minZ = box.min.z;
const cameraToFarEdge = (minZ < 0) ? -minZ + cameraZ : cameraZ - minZ;
camera.far = cameraToFarEdge * 3;
camera.updateProjectionMatrix();
// Apply initial color
applyColor(model, modelColor);
scene.add(model);
console.log('Model added to scene');
},
function (xhr) {
console.log((xhr.loaded / xhr.total * 100) + '% loaded');
},
function (error) {
console.error('Error loading model:', error);
console.error('Error details:', error.message, error.stack);
}
);
} else {
console.error('Model file not found:', response.status);
}
})
.catch(error => {
console.error('Error checking model file:', error);
});
// Handle window resize
window.addEventListener('resize', onWindowResize, false);
// Start animation loop
animate();
}
function onWindowResize() {
camera.aspect = document.getElementById('canvas-container').clientWidth / document.getElementById('canvas-container').clientHeight;
camera.updateProjectionMatrix();
renderer.setSize(document.getElementById('canvas-container').clientWidth, document.getElementById('canvas-container').clientHeight);
}
function animate() {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
}
// Apply color to all materials of the model
function applyColor(object, color) {
object.traverse(function (child) {
if (child.isMesh) {
if (child.material) {
if (Array.isArray(child.material)) {
child.material.forEach(function (material) {
material.color.setHex(color);
});
} else {
child.material.color.setHex(color);
}
}
}
});
}
// Start button click event
document.getElementById('startBtn').addEventListener('click', function () {
modelColor = 0x00ff00; // Green color
if (model) {
applyColor(model, modelColor);
}
});
// Stop button click event
document.getElementById('stopBtn').addEventListener('click', function () {
modelColor = 0xff0000; // Red color
if (model) {
applyColor(model, modelColor);
}
});
// Initialize when all resources are loaded
window.addEventListener('load', init);
</script>
</body>
</html>为什么页面上3d模型无法加载
最新发布