加载多个stl文件

时间:2015-09-02 10:21:11

标签: javascript stl

我需要对此代码进行一些更改,以便我可以加载多个对象,我不知道该怎么做。欢迎任何帮助! 这段代码目前还不行,我最大的问题是我应该把init函数放在哪里,以及如何异步加载多个对象

编辑:好的,现在它可以工作,并且可以将多个stl文件上传到场景中。

     var canvas;
   var viewer;
   var onModelLoaded;
      function init() {

    var filename = document.getElementById("hidden_file_path").innerHTML;
      canvas = document.getElementById('cv');
     viewer = new JSC3D.Viewer(canvas);
     viewer.setParameter('SceneUrl', 'object.stl');
     viewer.setParameter('InitRotationX', -76);
    viewer.setParameter('InitRotationY', -25);
     viewer.setParameter('InitRotationZ', 0);
    viewer.setParameter('ModelColor', '#b6a078'); //555555
     viewer.setParameter('BackgroundColor1', '#eaebeb');
    viewer.setParameter('BackgroundColor2', '#eaebeb');
     viewer.setParameter('RenderMode', 'smooth');  // point, wireframe, flat, smooth, texture, textureflat, texturesmooth
     viewer.setParameter('CreaseAngle', '30');
      viewer.setParameter('Definition', 'high');
    viewer.setParameter('Renderer', 'webgl');
    viewer.setParameter('MipMapping', 'off');
     viewer.init();
    viewer.update();
 var scene = viewer.getScene();
 var theScene = new JSC3D.Scene;
  onModelLoaded = function(scene) {

            var meshes = scene.getChildren();
            for (var i=0; i<meshes.length; i++) {
                    theScene.addChild(meshes[i]);
            }

                   viewer.replaceScene(theScene);
viewer.update();
    }


    // do somthing immediately after the model has been loaded sucessfully
    // viewer.onloadingcomplete = function() {
    //  var scene = viewer.getScene();
      // if (scene) {
//        // create and set materials with random colors for the meshes
//        var meshes = scene.getChildren();
//        for (var i=0; i<meshes.length; i++) {
//          meshes[i].setMaterial(new JSC3D.Material('', 0,      randomColor(), 0, true));
            //        }
    // generate a white ground plane that will be drawn as grids
    //makeGroundPlane(scene, 0xffffff/* white */, true);
    // generate a dark grey ground plane that will be drawn as grids
    makeGroundPlane(scene, 0x222222/* dark grey */, true);
    // ask the viewer to apply this change immediately
    viewer.update();
 function loadModel()
   {
     var counter;
     var filename = document.getElementById("hidden_file_path").innerHTML;
     var components = ['first.stl','second.stl'];
     //viewer.replaceSceneFromUrl('SceneUrl', components[counter]);
    for (counter=0;counter<components.length;counter++){
     var loader = new JSC3D.StlLoader;
  //   var onModelLoaded;
     loader.onload = onModelLoaded;
     loader.loadFromUrl(components[counter]);
     console.log("load model");
     counter++;
      }
     viewer.update();
   }

0 个答案:

没有答案