gpt4 book ai didi

javascript - Three.js 一个场景中的多个纹理和图像

转载 作者:行者123 更新时间:2023-11-28 03:28:10 24 4
gpt4 key购买 nike

我正在使用 THREE.js 在浏览器中显示 3d 旋转地球。我还想要一个图像出现在旋转的地球周围。我尝试使用内置方法,

var img = new THREE.ImageLoader();
img.load("texture/circle.png");

但图像不只出现。它只是旋转的球体。

我想要这样的东西

enter image description here

这是我的脚本标签,

<script>

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 );
camera.position.z = 650;

scene = new THREE.Scene();

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

// earth



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

var geometry = new THREE.SphereGeometry( 200, 20, 20 );

var material = new THREE.MeshBasicMaterial( { map: texture, overdraw: true } );
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, 'rgba(210,210,210,1)' );
//gradient.addColorStop( 1, 'rgba(255,255,255,1)' );

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

var texture = new THREE.Texture( canvas );
texture.needsUpdate = true;

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

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

renderer = new THREE.CanvasRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );

container.appendChild( renderer.domElement );

stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.top = '0px';
container.appendChild( stats.domElement );

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.05;
//camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
camera.lookAt( scene.position );

group.rotation.y -= 0.003;

renderer.render( scene, camera );

}


</script>

最佳答案

平面是水平的。删除这一行:

mesh.rotation.x = - Math.PI / 2;

确保渐变按照您想要的方式设置,例如,

gradient.addColorStop( 0.9, 'rgba( 210, 210, 210, 1 )' );
gradient.addColorStop( 1, 'rgba( 0, 0, 0, 0 )' );

此外,CanvasRenderer 将具有带有交叉对象的工件。

three.js r.62

关于javascript - Three.js 一个场景中的多个纹理和图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19743207/

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