jquery加载函数问题

时间:2016-02-13 16:34:58

标签: javascript jquery

我尝试使用jquery加载函数将按下按钮时转换为.DAE文件的3D模型加载到目标div中。但是,当按下按钮时,屏幕上会出现与文件相关的数字,而不是模型......

Here is a screen shot of the problem

如果不可能这样做,我还想知道是否可以使用jquery加载功能简单地替换单击按钮时页面中心显示的模型,如屏幕截图所示? / p>

任何帮助都是非常有吸引力的。我的代码可以在下面看到。

<!DOCTYPE html>

<html>

<head>

<script type="text/javascript" src="jquery.js"></script>

<script type="text/javascript">
$(document).ready(function() {

$('#LBC').click(function() {
    $('#target').load('bloodexternalcontent.DAE');
});



});
</script>

<title>Visualising Cells</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="style.css">
<script src="three.js"></script>
<script src="ColladaLoader.js"></script>

</head>

<body>
<script src="js/controls/OrbitControls.js"></script>

<script>
var controls = new THREE.OrbitControls( camera );
controls.addEventListener( 'change', render );
</script>

<script>

var width = window.innerWidth;
var height = window.innerHeight;


var scene = new THREE.Scene();

var camera = new THREE.PerspectiveCamera( 75, window.innerWidth/window.innerHeight, 0.01, 500 );
camera.position.z = 0.16;
camera.position.x = 0;
camera.position.y = 0;
scene.add(camera);


var renderer=new THREE.WebGLRenderer();
renderer.setSize(width,height); 
document.body.appendChild(renderer.domElement);
renderer.render(scene,camera);
renderer.setClearColor("rgb(181,181,181)");

light = new THREE.DirectionalLight(0xffffff);
        light.position.set(1, 1, 1);
        scene.add(light);

light = new THREE.DirectionalLight(0xffffff);
        light.position.set(0, 0, 0.14);
        scene.add(light);



var loader = new THREE.ColladaLoader();
        loader.load('egg2.DAE', function (collada) {

            object = collada.scene;
            object.position.x = 0;
            object.position.y = 0;
            object.position.z = 0;
            object.updateMatrix();
            scene.add(object);
            }
            );



document.addEventListener('keydown', function(event) {
console.log("Up Arrow Pressed");
console.log(camera.position.z);
if (event.keyCode == 38) {

    if (camera.position.z >= 0.1) {

        camera.position.z = camera.position.z - 0.01;

    }

}

else if (event.keyCode == 40) {

    console.log("Down Arrow Pressed")

    if (camera.position.z < 0.2) {

        camera.position.z = camera.position.z + 0.01;
    }

    }
}, true);

render = function () {
        requestAnimationFrame(render);

object.rotation.x += 0.0;
object.rotation.y += 0.0;


renderer.render(scene, camera);
controls.update();
        };
        render();

</script>


<div class="float-btn">
<button type="button" id="LBC">Load Red Blood Cell</button>
<button type="button" id="LEC">Load Egg Cell</button>
</div>

<div class="float-txt">
<div style="color:#000000">
<div style="font-family: Arial">
<div style="font-size: 18px">
<div style="text-decoration: underline">
<h1>Visualising Microscopic Cells</h1>
</div>

<div class="instructions">
<div style="color:#000000">
<div style="font-family: Arial">
<div style="font-size: 16px">
<div style="text-decoration: underline">
<h2>Instructions</h2>
</div>

<div class="instruction-txt">
<div style="color:#000000">
<div style="font-family: Arial">
<div style="font-size: 14px">
<p><u>Zoom In:</u> <strong>Up Arrow</strong> <br><u>Zoom Out:</u> <strong>Down Arrow</strong></br></p>
</div>

<div class="Model-Location" id="target">
</div>

</body>

</html>

1 个答案:

答案 0 :(得分:0)

您可以将.dae数据加载到可配置的THREE.ColladaLoader()对象中,与原始代码中使用loader.load的方式非常相似,从而替换页面中心显示的模型。

在下面的示例中,单击事件绑定到两个按钮。这些触发了一个删除当前模型的函数。然后加载相关的.dae文件,并将所需的模型添加到场景中。 (请注意,我在标记中保留了HTML问题,例如未公开的div。)

<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript" src="jquery.js"></script>
        <title>Visualising Cells</title>
        <meta charset="UTF-8">
        <link rel="stylesheet" href="style.css">
        <script src="three.js"></script>
        <script src="ColladaLoader.js"></script>
    </head>
    <body>
        <script src="js/controls/OrbitControls.js"></script>

        <script>
            var myModel; // used to reference the most recently-loaded model

            $(document).ready(function() {
                // when the page has loaded, add click functions to the two buttons
                $("#LBC").click(function() {
                    toggleModel("bloodexternalcontent");
                });

                $("#LEC").click(function() {
                    toggleModel("egg2");
                });
            });

            function toggleModel(modelName) {
                // remove the existing model from the scene
                scene.remove(myModel);
                // add the chosen model
                loadModel(modelName);
            }

            function loadModel(modelName) {
                // add the specified model
                loader.load(modelName+'.DAE', function (collada) {
                    myModel = collada.scene;
                    myModel.position.x = 0;
                    myModel.position.y = 0;
                    myModel.position.z = 0;
                    myModel.updateMatrix();
                    scene.add(myModel);                
                });
            }

            var width = window.innerWidth;
            var height = window.innerHeight;

            var scene = new THREE.Scene();

            var camera = new THREE.PerspectiveCamera( 75, window.innerWidth/window.innerHeight, 0.01, 500 );
            camera.position.z = 0.16;
            camera.position.x = 0;
            camera.position.y = 0;
            scene.add(camera);

            var controls = new THREE.OrbitControls( camera );

            var renderer=new THREE.WebGLRenderer();
            renderer.setSize(width,height); 
            document.body.appendChild(renderer.domElement);
            renderer.render(scene,camera);
            renderer.setClearColor("rgb(181,181,181)");

            light = new THREE.DirectionalLight(0xffffff);
                    light.position.set(1, 1, 1);
                    scene.add(light);

            light = new THREE.DirectionalLight(0xffffff);
                    light.position.set(0, 0, 0.14);
                    scene.add(light);

            var loader = new THREE.ColladaLoader();

            // load the default model
            loadModel("egg2");

            document.addEventListener('keydown', function(event) {        
                console.log(camera.position.z);
                if (event.keyCode == 38) {
                    // don't scroll the window        
                    console.log("Up Arrow Pressed");
                    event.preventDefault();
                    if (camera.position.z >= 0.1) {
                        camera.position.z = camera.position.z - 0.01;
                    }

                }
                else if (event.keyCode == 40) {
                    // don't scroll the window
                    event.preventDefault();
                    console.log("Down Arrow Pressed")

                    if (camera.position.z < 0.2) {
                        camera.position.z = camera.position.z + 0.01;
                    }

                    }
                }, true);

            render = function () {
                requestAnimationFrame(render);

                // object.rotation.x += 0.0;
                // object.rotation.y += 0.0;

                renderer.render(scene, camera);            
                // controls.update();
            };

            controls.addEventListener('change', render );

            render();
        </script>

        <div class="float-btn">
            <button type="button" id="LBC">Load Red Blood Cell</button>
            <button type="button" id="LEC">Load Egg Cell</button>
        </div>

        <div class="float-txt">
            <div style="color:#000000">
              <div style="font-family: Arial">
                 <div style="font-size: 18px">
                     <div style="text-decoration: underline">
                         <h1>Visualising Microscopic Cells</h1>
                    </div>

                 <div class="instructions">
                     <div style="color:#000000">
                         <div style="font-family: Arial">
                             <div style="font-size: 16px">
                                <div style="text-decoration: underline">
                                     <h2>Instructions</h2>
                                 </div>

                 <div class="instruction-txt">
                    <div style="color:#000000">
                        <div style="font-family: Arial">
                            <div style="font-size: 14px">
                                <p><u>Zoom In:</u> <strong>Up Arrow</strong> <br><u>Zoom Out:</u> <strong>Down Arrow</strong></br></p>
                            </div>

                 <div class="Model-Location" id="target">
                </div>
    </body>
</html>