gpt4 book ai didi

javascript - 如何在three.js中设置图片背景?

转载 作者:行者123 更新时间:2023-11-29 17:49:29 35 4
gpt4 key购买 nike

我正在尝试在 three.js 中为我的场景设置图像背景,但不幸的是,当我这样做时,我场景的主要对象也变成了黑色(与背景颜色相同。

我使用的方法:

renderer = new THREE.WebGLRenderer({ antialias: false, alpha:true });

这使得默认背景透明。然后我在 CSS 部分添加了图像背景。

我的整个场景脚本如下所示:

        var container, stats;
var camera, scene, renderer;
var group;
var mouseX = 0, mouseY = 0;

var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;

init();
animate();


function init() {

container = document.getElementById( 'container' );

camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 2000 );
//closer
camera.position.z = 500;

scene = new THREE.Scene();

group = new THREE.Group();
scene.add( group );

// earth

var loader = new THREE.TextureLoader();
loader.load( 'textures/mapnew1.jpg', function ( texture ) {

var geometry = new THREE.SphereGeometry( 180, 32, 32 );

var material = new THREE.MeshBasicMaterial( { map: texture, overdraw: 0.5 } );
var mesh = new THREE.Mesh( geometry, material );
group.add( mesh );

} );



// shadow

var canvas = document.createElement( 'canvas' );
canvas.width = 128;
canvas.height = 128;

var context = canvas.getContext( '2d' );
var gradient = context.createRadialGradient(
canvas.width / 2,
canvas.height / 2,
0,
canvas.width / 2,
canvas.height / 2,
canvas.width / 2
);
gradient.addColorStop( 0.1, '#000000' );
gradient.addColorStop( 1, '#000000' );

context.fillStyle = gradient;
context.fillRect( 0, 0, canvas.width, canvas.height );

var texture = new THREE.CanvasTexture( canvas );

var geometry = new THREE.PlaneBufferGeometry( 300, 300, 3, 3 );
var material = new THREE.MeshBasicMaterial( { map: texture, overdraw: 0.5 } );


var mesh = new THREE.Mesh( geometry, material );
mesh.position.y = - 200;
mesh.rotation.x = - Math.PI / 2;
group.add( mesh );


renderer = new THREE.CanvasRenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer = new THREE.WebGLRenderer({ antialias: false, alpha:true });
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setClearColor(0x000000, 0);
container.appendChild( renderer.domElement );

stats = new Stats();
container.appendChild( stats.dom );

document.addEventListener( 'mousemove', onDocumentMouseMove, false );

//

window.addEventListener( 'resize', onWindowResize, false );

}

function onWindowResize() {

windowHalfX = window.innerWidth / 2;
windowHalfY = window.innerHeight / 2;

camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();

renderer.setSize( window.innerWidth, window.innerHeight );

}

function onDocumentMouseMove( event ) {

mouseX = ( event.clientX - windowHalfX );
mouseY = ( event.clientY - windowHalfY );

}

//

function animate() {

requestAnimationFrame( animate );

render();
stats.update();

}


function render() {

camera.position.x += ( mouseX - camera.position.x ) * 0.08;
camera.position.y += ( - mouseY - camera.position.y ) * 0.08;
camera.lookAt( scene.position );

group.rotation.y -= 0.003;

renderer.render( scene, camera );


}


</script>

这是我的 CSS:

body {
color: #ffffff;
font-family:'Futura';
font-size:20px;
text-align: center;
background-image: url(textures/starfield.png);
background-color: black;
margin: 0px;
overflow: hidden;
}

您有什么想法可以修复它并使地球可见吗?

非常感谢!

最佳答案

关于背景图像,您正在为 WebGLRenderer 设置 alpha 值,这是正确的。您没有发布 CSS,但请确保您在 container 上设置背景图片,而不是在 Canvas 上。

另外,注释掉这一行:

renderer.setClearColor(0x000000, 0);

您不需要设置清晰的颜色,因为您清除的是透明度,而不是颜色。这应该可以解决背景图像问题。

关于全黑模型,您的场景中需要一盏灯。尝试将此添加到您的 init 方法中:

var light = new THREE.PointLight(0xffffff, 1, Infinity);
camera.add(light);

这会在您的相机位置添加一个光源(并且会随着相机移动)。

编辑以添加片段:

var container, stats;
var camera, scene, renderer;
var group;
var mouseX = 0,
mouseY = 0;

var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;

init();
animate();


function init() {

container = document.getElementById('container');

camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 2000);
//closer
camera.position.z = 500;

var light = new THREE.PointLight(0xffffff, 1, Infinity);
camera.add(light);

scene = new THREE.Scene();

group = new THREE.Group();
scene.add(group);

// earth

var loader = new THREE.TextureLoader();
loader.crossOrigin = '';
loader.load('https://upload.wikimedia.org/wikipedia/commons/thumb/8/83/Equirectangular_projection_SW.jpg/640px-Equirectangular_projection_SW.jpg', function(texture) {
var geometry = new THREE.SphereGeometry(180, 32, 32);

var material = new THREE.MeshBasicMaterial({
map: texture,
overdraw: 0.5
});
var mesh = new THREE.Mesh(geometry, material);
group.add(mesh);

});



// shadow

var canvas = document.createElement('canvas');
canvas.width = 128;
canvas.height = 128;

var context = canvas.getContext('2d');
var gradient = context.createRadialGradient(
canvas.width / 2,
canvas.height / 2,
0,
canvas.width / 2,
canvas.height / 2,
canvas.width / 2
);
gradient.addColorStop(0.1, '#000000');
gradient.addColorStop(1, '#000000');

context.fillStyle = gradient;
context.fillRect(0, 0, canvas.width, canvas.height);

var texture = new THREE.CanvasTexture(canvas);

var geometry = new THREE.PlaneBufferGeometry(300, 300, 3, 3);
var material = new THREE.MeshBasicMaterial({
map: texture,
overdraw: 0.5
});


var mesh = new THREE.Mesh(geometry, material);
mesh.position.y = -200;
mesh.rotation.x = -Math.PI / 2;
group.add(mesh);


renderer = new THREE.WebGLRenderer({
antialias: false,
alpha: true
});
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
//renderer.setClearColor(0x000000, 0);
container.appendChild(renderer.domElement);

stats = new Stats();
container.appendChild(stats.dom);

document.addEventListener('mousemove', onDocumentMouseMove, false);

//

window.addEventListener('resize', onWindowResize, false);

}

function onWindowResize() {

windowHalfX = window.innerWidth / 2;
windowHalfY = window.innerHeight / 2;

camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();

renderer.setSize(window.innerWidth, window.innerHeight);

}

function onDocumentMouseMove(event) {

mouseX = (event.clientX - windowHalfX);
mouseY = (event.clientY - windowHalfY);

}

//

function animate() {

requestAnimationFrame(animate);

render();
stats.update();

}


function render() {

camera.position.x += (mouseX - camera.position.x) * 0.08;
camera.position.y += (-mouseY - camera.position.y) * 0.08;
camera.lookAt(scene.position);

group.rotation.y -= 0.003;

renderer.render(scene, camera);


}
body {
color: #ffffff;
font-family: 'Futura';
font-size: 20px;
text-align: center;
background-image: url(https://upload.wikimedia.org/wikipedia/commons/6/62/Starsinthesky.jpg);
background-color: black;
margin: 0px;
overflow: hidden;
}
<script src="https://threejs.org/build/three.js"></script>
<script src="https://threejs.org/examples/js/renderers/Projector.js"></script>
<script src="https://threejs.org/examples/js/libs/stats.min.js"></script>
<div id="container"></div>

three.js r86

关于javascript - 如何在three.js中设置图片背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45443196/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com