three.js 透明背景根本不起作用
Posted
技术标签:
【中文标题】three.js 透明背景根本不起作用【英文标题】:three.js transparent background isn't working at all 【发布时间】:2016-06-14 03:53:12 【问题描述】:我一直在这里阅读其他人已经提出的一些问题,尤其是这个:
Transparent background with three.js
我需要使背景透明而不是黑色,但这对我不起作用。
添加了这些行:(阅读我上面发布的问题)
var renderer = new THREE.WebGLRenderer( alpha: true );
renderer.setClearColor( 0x000000, 0 );
它根本不起作用。 有完整的代码:
var container, stats;
var camera, scene, renderer;
var mouseX = 0, mouseY = 0;
var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;
var renderer = new THREE.WebGLRenderer( alpha: true );
init();
animate();
function init()
container = document.createElement( 'div' );
document.body.appendChild( container );
camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
camera.position.z = 250;
// scene
scene = new THREE.Scene();
var ambient = new THREE.AmbientLight( 0x444444 );
scene.add( ambient );
var directionalLight = new THREE.DirectionalLight( 0xffeedd );
directionalLight.position.set( 0, 0, 1 ).normalize();
scene.add( directionalLight );
var geometry = new THREE.BoxGeometry( 100, 100, 100 );
var material = new THREE.MeshLambertMaterial( color: 0x00ff00 );
var cube = new THREE.Mesh( geometry, material );
cube.rotation.y = Math.PI * 0.35;
scene.add( cube );
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.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 ) / 2;
mouseY = ( event.clientY - windowHalfY ) / 2;
//
function animate()
requestAnimationFrame( animate );
render();
function render()
camera.position.x += ( mouseX - camera.position.x ) * .05;
camera.position.y += ( - mouseY - camera.position.y ) * .05;
camera.lookAt( scene.position );
renderer.render( scene, camera );
renderer.setClearColor( 0x000000, 0 );
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r74/three.min.js"></script>
【问题讨论】:
我可以将颜色从黑色更改为白色或任何我想要的颜色。只是不透明,它不起作用 【参考方案1】:错误是代码正在创建 2 THREE.WebGLRenderer
s
文件顶部附近的第一个具有 alpha。但实际使用的那个是在中间附近创建的,没有 alpha。
删除第二个,它将开始工作
【讨论】:
以上是关于three.js 透明背景根本不起作用的主要内容,如果未能解决你的问题,请参考以下文章