带有OrbitControls的SVGRenderer在相对的两侧显示了两个SVG元素

时间:2018-12-13 12:55:42

标签: javascript svg three.js

我创建了两个场景和两个渲染器,一个用于简单的3D网格球体,另一个用于svg圆。 SVG场景像叠加层一样位于Mesh场景的顶部。球和圆放置在3D空间中的同一位置。我使用OrbitControls环顾四周。加载程序时,我会在同一位置看到球体和圆,但是当我环顾四周时,我会在另一侧看到另一个svg球体。为什么在3D空间中看到两个svg图标,以及如何摆脱第二个图标?我肯定知道它是一个svg节点,而不是两个,但是以某种方式,轨道控制将其像镜子一样看到了它。

这里是演示链接:http://mystic-peanut.com/mystic_sites/svgdouble/svg_sandbox.html

这是代码段:

camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.z = -50;

scene = new THREE.Scene();
scene.background = new THREE.Color( 0xff0000 );

meshScene = new THREE.Scene();
meshScene.background = new THREE.Color( 0x000000 );

// 3d sphere mesh added to mesh scene

var sphereGeometry = new THREE.SphereGeometry( 51, 60, 40 )
var mesh = new THREE.Mesh( sphereGeometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) );
mesh.name = 'pointer sphere';
mesh.position.set( 0, 0, 1500 );
meshScene.add( mesh );

// svg circle added to svg scene

node = document.createElementNS( 'http://www.w3.org/2000/svg', 'circle' );
node.setAttribute( 'stroke', 'black' );
node.setAttribute( 'fill', 'blue' );
node.setAttribute( 'r', '10' );
node.style.cursor = 'pointer';
var object = new THREE.SVGObject( node );
object.position.x = 0;
object.position.y = 0;
object.position.z = 1500;
scene.add( object );

meshRenderer = new THREE.WebGLRenderer( { antialias: true } );
meshRenderer.setPixelRatio( window.devicePixelRatio );
meshRenderer.setSize(window.innerWidth, window.innerHeight);
meshRenderer.domElement.style.position = 'absolute';
meshRenderer.domElement.style.top = '0px';
threeJSCanvas.appendChild( meshRenderer.domElement );

renderer = new THREE.SVGRenderer( {alpha: true } );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setQuality( 'low' );
renderer.domElement.style.position = 'absolute';
renderer.domElement.style.top = '0px';
renderer.domElement.style.zIndex = 2;
renderer.setClearColor( 0x000000, 0);
threeJSCanvas.appendChild( renderer.domElement );

// controls

controls = new THREE.OrbitControls( camera, renderer.domElement );
controls.addEventListener( 'change', render ); // call this only in static scenes (i.e., if there is no animation loop)
controls.minDistance = 10;
controls.maxDistance = 100;
controls.maxPolarAngle = 2*Math.PI / 3;
controls.minPolarAngle = Math.PI / 3;

1 个答案:

答案 0 :(得分:0)

此问题已在此故障单中修复: github.com/mrdoob/three.js/issues/15476