3dsmax 模型创建+原生贴图+obj/mtl+three.js 实现WEB3D

3dsmax模型创建+原生贴图

  1. 模型创建请参考另一篇我的文章
    3dsmax 模型创建+原生贴图(多图)
  2. 为何要原生贴图?
    因为three.js只支持原生贴图的obj/mtl文件

obj/mtl导出

3dsmax 模型创建+原生贴图+obj/mtl+three.js 实现WEB3D_第1张图片
3dsmax 模型创建+原生贴图+obj/mtl+three.js 实现WEB3D_第2张图片
3dsmax 模型创建+原生贴图+obj/mtl+three.js 实现WEB3D_第3张图片

Three.js

  1. 完整代码
    链接:https://pan.baidu.com/s/1xmR-i7Q_k3Gv_WN2le3NyA 密码:luti
  2. 其中的html代码

<html>
<head>
    <title>Example 09.04 - Trackball controls title>
    <script type="text/javascript" src="libs/three.js">script>
    <script type="text/javascript" src="libs/OBJLoader.js">script>
    <script type="text/javascript" src="libs/MTLLoader.js">script>
    <script type="text/javascript" src="libs/OBJMTLLoader.js">script>
    <script type="text/javascript" src="libs/stats.js">script>
    <script type="text/javascript" src="libs/dat.gui.js">script>
    <script type="text/javascript" src="libs/chroma.js">script>
    <script type="text/javascript" src="libs/TrackballControls.js">script>
    <style>
        body {
            /* set margin to 0 and overflow to hidden, to go fullscreen */
            margin: 0;
            overflow: hidden;
        }
    style>
head>
<body>
<div id="Stats-output">
div>

<div id="WebGL-output">
div>


<script type="text/javascript">

    // once everything is loaded, we run our Three.js stuff.
    function init() {
        var clock = new THREE.Clock();
        var stats = initStats()
        // create a scene, that will hold all our elements such as objects, cameras and lights.
        var scene = new THREE.Scene();
        // create a camera, which defines where we're looking at.
        var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);

        // create a render and set the size
        var webGLRenderer = new THREE.WebGLRenderer();
        webGLRenderer.setClearColor(new THREE.Color(0xffffff, 1.0));
        webGLRenderer.setSize(window.innerWidth, window.innerHeight);
        webGLRenderer.shadowMapEnabled = true;

        // position and point the camera to the center of the scene
        camera.position.x = 100;
        camera.position.y = 100;
        camera.position.z = 300;
        camera.lookAt(new THREE.Vector3(0, 0, 0));

        var trackballControls = new THREE.TrackballControls(camera);
        trackballControls.rotateSpeed = 1.0;
        trackballControls.zoomSpeed = 1.0;
        trackballControls.panSpeed = 1.0;
//      trackballControls.noZoom=false;
//      trackballControls.noPan=false;
        trackballControls.staticMoving = true;
//      trackballControls.dynamicDampingFactor=0.3;

        var ambientLight = new THREE.AmbientLight(0x383838);
        scene.add(ambientLight);

        // add spotlight for the shadows
        var spotLight = new THREE.SpotLight(0xffffff);
        spotLight.position.set(300, 300, 300);
        spotLight.intensity = 1;
        scene.add(spotLight);

        // add the output of the renderer to the html element
        document.getElementById("WebGL-output").appendChild(webGLRenderer.domElement);

        // call the render function
        var step = 0;

        // setup the control gui
        var controls = new function () {
            // we need the first child, since it's a multimaterial
        };

        var gui = new dat.GUI();
        var mesh;

        var loader = new THREE.OBJMTLLoader();
        var load = function (object) {
            var scale = chroma.scale(['red', 'green', 'blue']);
            setRandomColors(object, scale);
            mesh = object;
            scene.add(mesh);
        };

        //var texture = THREE.ImageUtils.loadTexture('assets/textures/3d66Model-512897-files-2.jpg');
        //texture.wrapS = texture.wrapT = THREE.MirroredRepeatWrapping;
        loader.load('assets/models/15.obj', 'assets/models/15.mtl', load);
        //texture.repeat.set( 1 , 1);
        function setCamControls() {

        }

        render();

        function setRandomColors(object, scale) {
            var children = object.children;


            if (children && children.length > 0) {
                children.forEach(function (e) {
                    setRandomColors(e, scale)
                });
            } else {
                // no children assume contains a mesh
                if (object instanceof THREE.Mesh) {

                    object.material.color = new THREE.Color(0xCCCCCC);
                    if (object.material.name.indexOf("building") == 0) {
                        object.material.emissive = new THREE.Color(0x444444);
                        object.material.transparent = false;
                        object.material.opacity = 0.8;
                    }
                }
            }
        }

        function render() {
            stats.update();
            var delta = clock.getDelta();

            if (mesh) {
                //   mesh.rotation.y+=0.006;
            }
            trackballControls.update(delta);
            //webGLRenderer.clear();
            // render using requestAnimationFrame
            requestAnimationFrame(render);
            webGLRenderer.render(scene, camera)
        }

        function initStats() {
            var stats = new Stats();
            stats.setMode(0); // 0: fps, 1: ms

            // Align top-left
            stats.domElement.style.position = 'absolute';
            stats.domElement.style.left = '0px';
            stats.domElement.style.top = '0px';

            document.getElementById("Stats-output").appendChild(stats.domElement);
            return stats;
        }
    }
    window.onload = init;
script>
body>
html>

直接更改mtl文件

  • 两个文件只差最后两行代码
  • 直接修改文件名,进行贴图更改,也是可以的
    3dsmax 模型创建+原生贴图+obj/mtl+three.js 实现WEB3D_第4张图片
    3dsmax 模型创建+原生贴图+obj/mtl+three.js 实现WEB3D_第5张图片

你可能感兴趣的:(javascript,web3d)