我一直在玩three.js。我在线框立方体中制作了一个立方体。如何更改背景颜色?我知道我必须做一些关于创建天空盒和/或点光源的事情,但这似乎只是制作黑屏。
这是COdepen:http://codepen.io/FredHair/pen/Cagpf
我的代码:
var width = window.innerWidth;
var height = window.innerHeight;
var scene, camera, renderer;
var cube;
var fov = 30,
isUserInteracting = false,
cameraDistance = 100,
onMouseDownMouseX = 0, onMouseDownMouseY = 0,
lon = 0, onMouseDownLon = 0,
lat = 0, onMouseDownLat = 0,
phi = 0, theta = 0;
$(function() {
init();
});
function init() {
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera( fov, window.innerWidth / window.innerHeight, 1, 1000 );
camera.position.set( 0, 0, 100 );
renderer = new THREE.WebGLRenderer({antialias:true});
renderer.setSize( window.innerWidth, window.innerHeight );
$('#container').append( renderer.domElement );
cube = new THREE.Mesh(
new THREE.BoxGeometry( 23, 33, 18 ),
new THREE.MeshBasicMaterial({color:0xFF6600, wireframe:true})
);
scene.add( cube );
cube2 = new THREE.Mesh(
new THREE.BoxGeometry( 10, 10, 10 ),
new THREE.MeshBasicMaterial({color:0xFF6600, })
);
scene.add( cube2 );
cube = new THREE.Mesh(
new THREE.BoxGeometry( 23, 33, 18 ),
new THREE.MeshBasicMaterial({color:0xFF6600, wireframe:true})
);
scene.add( cube );
$(document).on( 'mousedown', onMouseDown );
$(document).on( 'mousewheel', onMouseWheel );
$(window).on( 'resize', onWindowResize );
animate();
}
function animate() {
requestAnimationFrame( animate );
render();
}
function render() {
lon += .15;
lat = Math.max( - 85, Math.min( 85, lat ) );
phi = THREE.Math.degToRad( 90 - lat );
theta = THREE.Math.degToRad( lon );
camera.position.x = cameraDistance * Math.sin( phi ) * Math.cos( theta );
camera.position.y = cameraDistance * Math.cos( phi );
camera.position.z = cameraDistance * Math.sin( phi ) * Math.sin( theta );
camera.lookAt( scene.position );
renderer.render( scene, camera );
}
function onMouseWheel(event) {
cameraDistance += event.originalEvent.deltaY * 0.1;
cameraDistance = Math.min( cameraDistance, camera.far );
cameraDistance = Math.max( cameraDistance, camera.near );
}
function onMouseDown(event) {
event.preventDefault();
onPointerDownPointerX = event.clientX;
onPointerDownPointerY = event.clientY;
onPointerDownLon = lon;
onPointerDownLat = lat;
$(document).on( 'mousemove', onMouseMove );
$(document).on( 'mouseup', onMouseUp );
}
function onMouseMove(event) {
lon = ( event.clientX - onPointerDownPointerX ) * 0.1 + onPointerDownLon;
lat = ( event.clientY - onPointerDownPointerY ) * 0.1 + onPointerDownLat;
}
function onMouseUp(event) {
$(document).off( 'mousemove' );
$(document).off( 'mouseup' );
}
function onWindowResize() {
renderer.setSize( window.innerWidth, window.innerHeight );
camera.projectionMatrix.makePerspective( fov, window.innerWidth / window.innerHeight, 1, 1000 );
}
非常感谢任何帮助。
答案 0 :(得分:1)
renderer.setClearColor( 0xffffff, 1 );
你应该做的。第一个参数是RGB中的背景颜色,第二个参数是alpha。例如:
renderer.setClearColor( 0xff0000, .5 );
这应该将渲染的背景设置为红色,透明度为50%。我自己刚开始玩three.js,所以我希望这适合你。
编辑::现在我已经有一秒多了,我已经在你的CodePen中试过了。我在初始化渲染器后立即将上面的行放在init()函数中,并且它有效。