繁体   English   中英

更改背景颜色,在three.js中添加点光源

[英]Change background colour, add a pointlight in three.js

我一直在玩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 );
}

任何帮助是极大的赞赏。

renderer.setClearColor( 0xffffff, 1 ); 

应该吗 第一个参数是RGB的背景色,第二个参数是alpha。 例如:

renderer.setClearColor( 0xff0000, .5 ); 

这应该将渲染的背景设置为红色,透明度为50%。 我刚刚开始自己​​玩three.js,所以希望对您有用。

编辑::现在已经有超过一秒钟的时间,我已经在您的CodePen中进行了尝试。 初始化渲染器后,我立即在您的init()函数中添加了该行,并成功了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM