当前位置:网站首页>Threejs Internet of things, 3D visualization of farms (I)
Threejs Internet of things, 3D visualization of farms (I)
2022-07-05 04:02:00 【Zuo Ben】
1, Introduce
This example uses r95 edition Three.js library .
Main functions : Quote the farm model for display . The renderings are as follows :
2, Main description
farm 3D Show main use OBJLoader and MTLLoader Load the model and directly assign materials to display .
The imported model code is as follows :
3, Source code
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Threejs The Internet of things , farm 3D visualization </title>
<meta charset="utf-8">
<script type="text/javascript" src="libs/three.js"></script>
<script type="text/javascript" src="libs/OrbitControls.js"></script>
<script type="text/javascript" src="libs/OBJLoader.js"></script>
<script type="text/javascript" src="libs/MTLLoader.js"></script>
<style>
body {
margin: 0;
overflow: hidden;
}
</style>
</head>
<body>
<div id="dom"></div>
<script type="text/javascript">
var camera;
var renderer;
var mesh;
function init() {
// Create a scene , It will contain all our elements , Like an object , Cameras and lights .
var scene = new THREE.Scene();
var urls = [
'assets/textures/posx.jpg',
'assets/textures/negx.jpg',
'assets/textures/posy.jpg',
'assets/textures/negy.jpg',
'assets/textures/posz.jpg',
'assets/textures/negz.jpg'
];
var cubeLoader = new THREE.CubeTextureLoader();
scene.background = cubeLoader.load(urls);
// Create a camera , It defines where we are looking
camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 30000);
// Aim the camera at the center of the scene
camera.position.x = 5500;
camera.position.y = 3000;
camera.position.z = 2000;
camera.lookAt(scene.position);
var orbit = new THREE.OrbitControls(camera);
// Create a renderer and set the size ,WebGLRenderer The computer graphics card will be used to render the scene
// initialize basic renderer
renderer = new THREE.WebGLRenderer({
antialias: true,
logarithmicDepthBuffer: true,
});
renderer.setSize(window.innerWidth, window.innerHeight);
// Add ambient light
scene.add(new THREE.AmbientLight("#ffffff", 1.5));
// Add the output of the renderer to HTML Elements
document.getElementById("dom").appendChild(renderer.domElement);
// Display the coordinate axis on the screen
var axes = new THREE.AxesHelper(10000);
// scene.add(axes);
initModel();
// Start animation
renderScene();
// Add model
function initModel() {
var mtlLoader = new THREE.MTLLoader();
mtlLoader.setPath("assets/models/obj_mtl/")
mtlLoader.load('yangzhichang.mtl', function(materials) {
materials.preload();
var objLoader = new THREE.OBJLoader();
objLoader.setMaterials(materials);
objLoader.load('assets/models/obj_mtl/yangzhichang.obj', function(object) {
mesh = object;
object.traverse(function(node) {
if (node.material) {
node.material.side = THREE.DoubleSide;
}
});
scene.add(mesh);
});
});
}
document.addEventListener('click', onDocumentMouseDown, false);
function onDocumentMouseDown(event) {
// Click on the screen to create a vector
var vector = new THREE.Vector3((event.clientX / window.innerWidth) * 2 - 1, -(event.clientY / window
.innerHeight) * 2 + 1, 0.5);
vector = vector.unproject(camera); // Convert the coordinates of the screen into coordinates in the 3D scene
var raycaster = new THREE.Raycaster(camera.position, vector.sub(camera.position).normalize());
var intersects = raycaster.intersectObjects(mesh, true);
if (intersects.length > 0) {
var name = intersects[0].object.name;
var obj = scene.getObjectByName(name);
console.log(obj)
scene.remove(obj);
}
}
function renderScene() {
orbit.update();
requestAnimationFrame(renderScene);
renderer.render(scene, camera);
}
// Rendered scenes
renderer.render(scene, camera);
}
window.onload = init;
function onResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
// Listen for resizing events
window.addEventListener('resize', onResize, false);
</script>
</body>
</html>
4, download
Use threejs Rendering farm model source code , Farm model obj+mtl Format ,threejs Model Threejs Rendering obj+mtl Model ,Threejs load 3D Factory model Threejs Realize the introduction of more download resources for factory models 、 For learning materials, please visit CSDN Download channel .https://download.csdn.net/download/baidu_29701003/54823032 If you need other source code, please leave a message or contact me on wechat :1171053128
边栏推荐
- Get to know MySQL connection query for the first time
- Threejs realizes sky box, panoramic scene, ground grass
- 阿里云ECS使用cloudfs4oss挂载OSS
- Redis source code analysis: redis cluster
- Use threejs to create geometry, dynamically add geometry, delete geometry, and add coordinate axes
- laravel8 导出Excle文件
- error Couldn‘t find a package. JSON file in "your path“
- open graph协议
- On the day 25K joined Tencent, I cried
- 根据入栈顺序判断出栈顺序是否合理
猜你喜欢
已解决(sqlalchemy+pandas.read_sql)AttributeError: ‘Engine‘ object has no attribute ‘execution_options‘
Enterprise level: spire Office for . NET:Platinum|7.7. x
[brush questions] BFS topic selection
postman和postman interceptor的安装
Threejs implements labels and displays labels with custom styles
[an Xun cup 2019] not file upload
Threejs realizes sky box, panoramic scene, ground grass
An elegant program for Euclid‘s algorithm
“金九银十”是找工作的最佳时期吗?那倒未必
【刷题】BFS题目精选
随机推荐
How to realize real-time audio and video chat function
Installation of postman and postman interceptor
[数组]566. 重塑矩阵-简单
NEW:Devart dotConnect ADO.NET
函数基础学习02
@Transactional 注解导致跨库查询失效的问题
Containerd series - detailed explanation of plugins
Basic function learning 02
An elegant program for Euclid‘s algorithm
Rust blockchain development - signature encryption and private key public key
如何实现实时音视频聊天功能
Online sql to excel (xls/xlsx) tool
在线文本行固定长度填充工具
Why do big companies such as Baidu and Alibaba prefer to spend 25K to recruit fresh students rather than raise wages by 5K to retain old employees?
Rome链分析
Get to know MySQL connection query for the first time
C语言课设:影院售票管理系统
Behavior perception system
Threejs loads the city obj model, loads the character gltf model, and tweetjs realizes the movement of characters according to the planned route
Threejs clicks the scene object to obtain object information, and threejs uses raycaster to pick up object information