最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Add background to THREE scene - Stack Overflow

programmeradmin1浏览0评论

I am trying to add a background to a THREE scene, but I cannot get it to work. I followed the advice given here and here without success.

Here are the lines I added to my (plex) code:

            // Load the background texture
            var loader = new THREE.TextureLoader();
            var texture = loader.load( 'textures/stars_texture2956.jpg' );
            var backgroundMesh = new THREE.Mesh(
                new THREE.PlaneGeometry(2, 2, 0),
                new THREE.MeshBasicMaterial({
                    map: texture
                }));

            backgroundMesh.material.depthTest = false;
            backgroundMesh.material.depthWrite = false;

            // Create your background scene
            backgroundScene = new THREE.Scene();
            backgroundCamera = new THREE.Camera();
            backgroundScene.add(backgroundCamera );
            backgroundScene.add(backgroundMesh );

and the render function looks like this:

function render() {
    renderer.render(backgroundScene, backgroundCamera);
    renderer.render( scene, camera );
}

Still I do not see the background (it is still white), but everything else works as expected. Is there a way to fix this?

I am trying to add a background to a THREE scene, but I cannot get it to work. I followed the advice given here and here without success.

Here are the lines I added to my (plex) code:

            // Load the background texture
            var loader = new THREE.TextureLoader();
            var texture = loader.load( 'textures/stars_texture2956.jpg' );
            var backgroundMesh = new THREE.Mesh(
                new THREE.PlaneGeometry(2, 2, 0),
                new THREE.MeshBasicMaterial({
                    map: texture
                }));

            backgroundMesh.material.depthTest = false;
            backgroundMesh.material.depthWrite = false;

            // Create your background scene
            backgroundScene = new THREE.Scene();
            backgroundCamera = new THREE.Camera();
            backgroundScene.add(backgroundCamera );
            backgroundScene.add(backgroundMesh );

and the render function looks like this:

function render() {
    renderer.render(backgroundScene, backgroundCamera);
    renderer.render( scene, camera );
}

Still I do not see the background (it is still white), but everything else works as expected. Is there a way to fix this?

Share Improve this question edited May 23, 2017 at 12:02 CommunityBot 11 silver badge asked Nov 19, 2015 at 9:45 AlexAlex 44.5k104 gold badges300 silver badges513 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

If you wish to add a static background to your scene then the easiest way is to make the background of your scene transparent and place an image under canvas:

var renderer = new THREE.WebGLRenderer({ alpha: true });
renderer.setClearColor( 0xffffff, 0);

If you wish to create panoramic background which changes when you rotate camera then you need to create a skybox - a big mesh around your scene textured with a set of textures which cover 360 degrees of view. Have a look at this example: http://threejs/examples/#webgl_materials_envmaps

The map is undefined because the TextureLoader Constructor expects a manager, not an url.

// instantiate a loader
var loader = new THREE.TextureLoader();

// load a resource
var texture = loader.load( 'textures/land_ocean_ice_cloud_2048.jpg' );

http://threejs/docs/#Reference/Loaders/TextureLoader

To solve your problem with the two scenes, you need to disable autoClear. The second renderer call clears the first one. Set after initializing: renderer.autoClear = false;. Now manually clear in your render-function:

function render() {
    renderer.clear(); // <-
    renderer.render(backgroundScene, backgroundCamera);
    renderer.render( scene, camera );
}

try to coding like this

    THREE.ImageUtils.crossOrigin = '';
  var img = 'http://bpic.588ku./back_pic/03/92/40/4957e29f80d8a4a.jpg!ww650';
  var grass = THREE.ImageUtils.loadTexture(img);
new THREE.MeshLambertMaterial({  map: grass });

it works for me

I have found a solution: Instead of creating a different scene and not knowing how to add cameras/whatever so it get rendered correctly, just add the background mesh to the actual scene.

The code then is as follows:

// Load the background texture
var loader = new THREE.TextureLoader();
var texture = loader.load( 'textures/messier-41.jpg' );               
var backgroundMesh = new THREE.Mesh( 
    new THREE.PlaneGeometry(2048, 2048,8,8),
    new THREE.MeshBasicMaterial({
         map: texture
    }));


backgroundMesh.material.depthTest = false;
backgroundMesh.material.depthWrite = false;

which must be put before the code that adds the other parts of the scene.

发布评论

评论列表(0)

  1. 暂无评论