gpt4 book ai didi

javascript - three.js 透明背景

转载 作者:IT王子 更新时间:2023-10-29 02:42:07 25 4
gpt4 key购买 nike

代码有效,但我在使用 three.js 为 Canvas 设置透明背景时遇到问题。我使用:

Background.renderer.setClearColor(0xffffff, 0);

但是背景变黑了。如何将其更改为透明?


代码:

   var camera, scene, renderer;
var mouseX = 0, mouseY = 0;
var p;

var windowHalfX = site.Width / 2;
var windowHalfY = site.Height / 2;

Background.camera = new THREE.PerspectiveCamera( 35, site.Width / site.Height, 1, 2000 );
Background.camera.position.z = 300;
//camera.position.y = 200;

// scene
Background.scene = new THREE.Scene();

// texture
var manager = new THREE.LoadingManager();
manager.onProgress = function ( item, loaded, total ) {
console.log('webgl, twice??');
//console.log( item, loaded, total );
};


// particles
var p_geom = new THREE.Geometry();
var p_material = new THREE.ParticleBasicMaterial({
color: 0xFFFFFF,
size: 1
});

// model
var loader = new THREE.OBJLoader( manager );
loader.load( site.base_url + '/assets/models/head.obj', function ( object ) {

object.traverse( function ( child ) {

if ( child instanceof THREE.Mesh ) {

// child.material.map = texture;

var scale = 6;

$(child.geometry.vertices).each(function() {
p_geom.vertices.push(new THREE.Vector3(this.x * scale, this.y * scale, this.z * scale));
})
}
});

Background.scene.add(p)
});

p = new THREE.ParticleSystem(
p_geom,
p_material
);

Background.renderer = new THREE.WebGLRenderer();
Background.renderer.setSize( site.Width, site.Height );
Background.renderer.setClearColor(0xffffff, 0);

$('.particlehead').append(Background.renderer.domElement);
$('#content').on('mousemove', onDocumentMouseMove);
site.window.on('resize', onWindowResize);

function onWindowResize() {
windowHalfX = site.Width / 2;
windowHalfY = site.Height / 2;
//console.log(windowHalfX);

Background.camera.aspect = site.Width / site.Height;
Background.camera.updateProjectionMatrix();

Background.renderer.setSize( site.Width, site.Height );
}

function onDocumentMouseMove( event ) {
mouseX = ( event.clientX - windowHalfX ) / 2;
mouseY = ( event.clientY - windowHalfY ) / 2;
//console.log(mouseX)
}

Background.animate = function() {

//console.log('animate2');
Background.ticker = TweenMax.ticker;
Background.ticker.addEventListener("tick", Background.animate);

render();
}

function render() {
Background.camera.position.x += ( (mouseX * .5) - Background.camera.position.x ) * .05;
Background.camera.position.y += ( -(mouseY * .5) - Background.camera.position.y ) * .05;

Background.camera.lookAt( Background.scene.position );

Background.renderer.render( Background.scene, Background.camera );
}

render();

最佳答案

three.js中如果想要透明背景,需要在WebGLRenderer构造函数中传入alpha参数。

var renderer = new THREE.WebGLRenderer( { alpha: true } );

您可以将清除颜色保留为默认值。

renderer.setClearColor( 0x000000, 0 ); // the default

three.js r.71

关于javascript - three.js 透明背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20495302/

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