2

テクスチャの読み込みとメッシュ キューブへの適用でThree.jsに問題があります。ローカルでは、wamp(localhost)を使用してApacheサーバーでhtmlファイルを実行するなど、いくつかの問題があることを知っています。

しかし、Node.jsと socket.io を使用する場合、どのように修正すればよいでしょうか? まず、three.js をロードするには、ローカルの "three.js" の代わりにスクリプト src の Web アドレスを入力する必要があります。

http://threejs.org/build/three.min.js

それは機能しますが、テクスチャはどうですか?

テクスチャのローカル アドレスもインターネット アドレスも機能していません...

 //var mapUrl = "mercury.jpg"; 
    var mapUrl = "http://threejs.org/examples/textures/cube/skybox/px.jpg"; 
    var map = THREE.ImageUtils.loadTexture(mapUrl); // its not working with both

また、 Cloud9 などの Node.js で実行されている Web サーバーにコードを配置した場合、それを修正するにはどうすればよいですか? (Node.js を使用したローカルと同じ問題)。

ご清聴ありがとうございました。


これが私の完全なコードです。

サーバ

var http    =   require('http');
var fs      =   require('fs');

// Creation du serveur
var app = http.createServer(function (req, res) {
    // On lit notre fichier app.html
    fs.readFile('gl.html', 'utf-8', function(error, content) {
        res.writeHead(200, {'Content-Type' : 'text/html'});
        res.end(content);
    });

  });

var io = require("socket.io");
io = io.listen(app);

io.sockets.on('connection', function (socket) {

  socket.on('send', function () {
    socket.broadcast.emit('rec');

  }); // send

});  // connection


app.listen(8080);

クライアント (gl.html)

<!DOCTYPE html>
<html>
<head>
<title>Welcome to WebGL</title>
</head>
<body onLoad="onLoad();" style="">
<h1>Webgl</h1>
<div id="container" style="width:95%; height:80%; position:absolute;"></div>


<script type="text/javascript" src="http://threejs.org/build/three.min.js"></script>
<!--<script type="text/javascript" src="Three.js"></script> not working-->
<script type="text/javascript" src="/socket.io/socket.io.js"></script>

<script>
    var socket = io.connect();

    var renderer = null, 
        scene = null, 
        camera = null,
        cube = null,
        animating = false;

    function onLoad()
    {
        // Grab our container div
        var container = document.getElementById("container");

        // Create the Three.js renderer, add it to our div
        renderer = new THREE.WebGLRenderer( { antialias: true } );
        renderer.setSize(container.offsetWidth, container.offsetHeight);
        container.appendChild( renderer.domElement );

        // Create a new Three.js scene
        scene = new THREE.Scene();

        // Put in a camera
        camera = new THREE.PerspectiveCamera( 45, container.offsetWidth / container.offsetHeight, 1, 4000 );
        camera.position.set( 0, 0, 3 );

        // Create a directional light to show off the object
        var light = new THREE.DirectionalLight( 0xffffff, 1.5);
        light.position.set(0, 0, 1);
        scene.add( light );






        // Create a shaded, texture-mapped cube and add it to the scene
        // First, create the texture map

   //   var mapUrl = "mercury.jpg";
  var mapUrl = "http://threejs.org/examples/textures/cube/skybox/px.jpg";
        var map = THREE.ImageUtils.loadTexture(mapUrl); // not working with both !!!






        // Now, create a Phong material to show shading; pass in the map
        var material = new THREE.MeshPhongMaterial({ map: map });

        // Create the cube geometry
        var geometry = new THREE.CubeGeometry(1, 1, 1);

        // And put the geometry and material together into a mesh
        cube = new THREE.Mesh(geometry, material);

        // Turn it toward the scene, or we won't see the cube shape!

       // cube.rotation.x = Math.PI / 5;
        cube.rotation.x = 0.5;
        //cube.rotation.y = Math.PI / 5;

        // Add the cube to our scene
        scene.add( cube );

        // Add a mouse up handler to toggle the animation
        addMouseHandler();

        // Run our render loop
        run();


    }

    function run()
    {
        // Render the scene
        renderer.render( scene, camera );

        // Spin the cube for next frame
        if (animating)
        {
            cube.rotation.y -= 0.01;
            //cube.rotation.x += 0.05;
        }

        // Ask for another frame
        //requestAnimationFrame(run);
    setTimeout(run, 1000/60);

    }

    function addMouseHandler()
    {
        var dom = renderer.domElement;

        dom.addEventListener( 'mouseup', onMouseUp, false);
    }

    function onMouseUp  (event)
    {
        event.preventDefault();

        animating = !animating;

        socket.emit('send'); 

    }

    socket.on('rec', function () {

      animating = !animating;

  });

    </script>
    </body>
</html>
4

2 に答える 2

0

CORS で問題が発生しています。CORS は、テクスチャが同じベース URL から来る必要があるか、サーバー側で特別な処理が必要であると述べています。これは、プロキシを使用して簡単に修正できます。すでにサーバーを使用している場合、プロキシ リクエストを処理するようにサーバーを構成するのは難しくありません。

于 2013-08-12T09:46:10.287 に答える