THREE.JS STL形式を導入したモデル

26073 ワード

Three.jsフレーム
3 Dモデルレンダリング
three.jsなどjsを導入する
githubからダウンロード:https://github.com/mrdoob/three.js
教程
THREE.JS STL形式のモデルTHREE.JSのメッシュオブジェクトMESHの属性と方法を導入します.
シンプルDEMO
<html>
	<div style="width: 600px;height: 400px;" id="modelViewDiv">
                
    div>

<script th:src="@{/static/js/jquery/dist/jquery.min.js}">script>

<script th:src="@{/static/three/three.js}">script>
<script th:src="@{/static/three/OrbitControls.js}">script>
<script th:src="@{/static/three/STLLoader.js}">script>
<script th:src="@{/static/three/dat.gui.min.js}">script>
<script th:src="@{/static/three/stats.min.js}">script>
<script>
    //     
    var renderer;

    function initRender() {
        renderer = new THREE.WebGLRenderer({antialias:true});
        renderer.setSize(600, 400);
        //           
        renderer.setClearColor(0xEEEEEE);
        document.getElementById("modelViewDiv").appendChild(renderer.domElement);
    }

    //    
    var camera;

    function initCamera() {
        camera = new THREE.PerspectiveCamera(45, 600/400, 0.1, 1000);
        camera.position.set(0, 40, 50);
        camera.lookAt(new THREE.Vector3(0,0,0));
    }

    //    
    var scene;

    function initScene() {
        scene = new THREE.Scene();
    }

    //   dat.GUI      

    var gui;

    function initGui() {

        //                  

        gui = {

        };

        var datGui = new dat.GUI();

        //        gui  ,gui.add(  ,  ,   ,   )

    }


    //  
    var light;

    function initLight() {

        // scene.add(new THREE.AmbientLight(0x444444));

        light = new THREE.HemisphereLight(0x404040 ,0xffffff, 0.8);

        // light = new THREE.PointLight(0xffffff); //  

        // light.position.set(0,50,50);
        light.position.set(0,1,0);


        //             
        // light.castShadow = true;

        scene.add(light);

    }

    function initModel() {
        //    
        // var helper = new THREE.AxesHelper(50);
        // scene.add(helper);

        var loader = new THREE.STLLoader();
        loader.load("/cms/static/three/sowger.stl", function (geometry) {
            //    
            var mat = new THREE.MeshLambertMaterial({color: 0x00ffff});
            var mesh = new THREE.Mesh(geometry, mat);
            mesh.rotation.x = -0.5 * Math.PI; //     
            mesh.scale.set(4, 4, 4); //  
            geometry.center(); //    
            scene.add(mesh);
        });
    }


    //       
    var stats;
    function initStats() {
        stats = new Stats();
        document.body.appendChild(stats.dom);
    }

    //               ,      ,    
    var controls;
    function initControls() {
        controls = new THREE.OrbitControls( camera, renderer.domElement );
        //     animate   ,      
        //controls.addEventListener( 'change', render );

        //                      
        controls.enableDamping = true;

        //                  
        //controls.dampingFactor = 0.25;

        //      
        controls.enableZoom = true;

        //      
        controls.autoRotate = true;

        controls.autoRotateSpeed = 0.5;

        //             
        controls.minDistance  = 1;

        //             
        controls.maxDistance  = 200;

        //        
        controls.enablePan = true;

    }

    function render() {
        renderer.render( scene, camera );

    }

    //         
    function onWindowResize() {
        camera.aspect = 600 / 400;

        camera.updateProjectionMatrix();

        render();

        renderer.setSize( 600, 400 );
    }

    function animate() {
        //     
        render();

        //      
        // stats.update();

        controls.update();
        requestAnimationFrame(animate);
    }

    function draw() {
        // initGui();

        initRender();

        initScene();

        initCamera();

        initLight();

        initModel();

        initControls();

        // initStats();

        animate();

        window.onresize = onWindowResize;

    }

    //     
    draw();
script>
html>