The main example of this article introduces the loading of obj model by three.js. Three.js is a webGL framework and is widely used due to its ease of use. If you want to learn webGL, it is a good choice to abandon those complicated native interfaces and start with this framework. Okay, let me introduce you to three.js loading obj model through a piece of code. The specific code is as follows:
nbsp;html> <script></script> <script></script> <script> var scene = null; var camera = null; var renderer = null; var mesh = null; var id = null; function init() { renderer = new THREE.WebGLRenderer({//渲染器 canvas: document.getElementById('mainCanvas')//画布 }); renderer.setClearColor(0x000000);//画布颜色 scene = new THREE.Scene();//创建场景 camera = new THREE.OrthographicCamera(-5, 5, 3.75, -3.75, 0.1, 100);//正交投影照相机 camera.position.set(15, 25, 25);//相机位置 camera.lookAt(new THREE.Vector3(0, 2, 0));//lookAt()设置相机所看的位置 scene.add(camera);//把相机添加到场景中 var loader = new THREE.OBJLoader();//在init函数中,创建loader变量,用于导入模型 loader.load('libs/port.obj', function(obj) {//第一个表示模型路径,第二个表示完成导入后的回调函数,一般我们需要在这个回调函数中将导入的模型添加到场景中 obj.traverse(function(child) { if (child instanceof THREE.Mesh) { child.material.side = THREE.DoubleSide; } }); mesh = obj;//储存到全局变量中 scene.add(obj);//将导入的模型添加到场景中 }); var light = new THREE.DirectionalLight(0xffffff);//光源颜色 light.position.set(20, 10, 5);//光源位置 scene.add(light);//光源添加到场景中 id = setInterval(draw, 20);//每隔20s重绘一次 } function draw() {//们在重绘函数中让茶壶旋转: renderer.render(scene, camera);//调用WebGLRenderer的render函数刷新场景 mesh.rotation.y += 0.01;//添加动画 if (mesh.rotation.y > Math.PI * 2) { mesh.rotation.y -= Math.PI * 2; } } </script> <canvas></canvas>
Let’s take a look at how to use Three.js to load obj and mtl files
OBJ and MTL are the geometric model files and material files of the 3D model.
In the latest three.js version (r78), the previous OBJMTLLoader class has been deprecated.
Now to load OBJ and MTL files, you need to combine the two classes OBJLoader and MTLLoader to achieve this, which also provides operational flexibility.
In the following code, MTLLoader is first used to load the egg.mtl material file, and then the material is set to an OBJLoader object so that it can be applied when loading the obj model.
onProgress is the loading process callback function, and onError is the error handling function.
// model var onProgress = function(xhr) { if (xhr.lengthComputable) { var percentComplete = xhr.loaded / xhr.total * 100; console.log(Math.round(percentComplete, 2) + '% downloaded'); } }; var onError = function(xhr) {}; THREE.Loader.Handlers.add(/\.dds$/i, new THREE.DDSLoader()); var mtlLoader = new THREE.MTLLoader(); mtlLoader.setPath('/uploads/160601/obj/'); mtlLoader.load('egg.mtl', function(materials) { materials.preload(); var objLoader = new THREE.OBJLoader(); objLoader.setMaterials(materials); objLoader.setPath('/uploads/160601/obj/'); objLoader.load('egg.obj', function(object) { object.position.y = -0.5; scene.add(object); }, onProgress, onError); });
Related recommendations:
Example explanation of Three.js loading external model
Detailed explanation of three.js local running method
JS Library Three.js Basics Introduction
The above is the detailed content of Examples to explain three.js loading obj model. For more information, please follow other related articles on the PHP Chinese website!

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version
Recommended: Win version, supports code prompts!