gpt4 book ai didi

svg - 如何在three.js中将SVG文件加载到SVGRenderer中

转载 作者:行者123 更新时间:2023-12-04 15:34:20 24 4
gpt4 key购买 nike

我正在尝试使用 SVGRenderer在 Three.js ( http://threejs.org/examples/#svg_sandbox ) 中。该示例向您展示了如何即时制作 SVG 元素(圆形)。我想导入我计算机中已有的 SVG 文件。我该怎么做?

createElementNS 命令似乎不支持导入 SVG 文件?

我基本上想要我的 image.svg显示在一个 Three.js 场景中。

最佳答案

您可以使用 THREE.SVGLoader()实现它的库:

var svgManager = new THREE.LegacySVGLoader();
var url = 'https://upload.wikimedia.org/wikipedia/commons/b/b0/NewTux.svg';

function svg_loading_done_callback(doc) {
init(new THREE.SVGObject(doc));
animate();
};

svgManager.load(url,
svg_loading_done_callback,
function() {
console.log("Loading SVG...");
},
function() {
console.log("Error loading SVG!");
});

var camera, scene, renderer;

function init(svgObject) {
camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 100000);
camera.position.z = 1500;

svgObject.position.x = Math.random() * innerWidth;
svgObject.position.y = 200;
svgObject.position.z = Math.random() * 10000 - 5000;
svgObject.scale.x = svgObject.scale.y = svgObject.scale.z = 0.01;

scene = new THREE.Scene();
scene.add(svgObject);

var ambient = new THREE.AmbientLight(0x80ffff);
scene.add(ambient);
var directional = new THREE.DirectionalLight(0xffff00);
directional.position.set(-1, 0.5, 0);
scene.add(directional);
renderer = new THREE.SVGRenderer();
renderer.setClearColor(0xf0f0f0);
renderer.setSize(window.innerWidth, window.innerHeight - 5);
document.body.appendChild(renderer.domElement);

window.addEventListener('resize', onWindowResize, false);
}

function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}

function animate() {
requestAnimationFrame(animate);
render();
}

function render() {
var time = Date.now() * 0.0002;
camera.position.x = Math.sin(time) * 200;
camera.position.z = Math.cos(time) * 200;
camera.lookAt(scene.position);
renderer.render(scene, camera);
}
* {
margin: 0
}
<script src="http://threejs.org/build/three.min.js"></script>
<script src="http://threejs.org/examples/js/renderers/SVGRenderer.js"></script>
<script src="http://threejs.org/examples/js/renderers/Projector.js"></script>
<script src="http://threejs.org/examples/js/loaders/SVGLoader.js"></script>
<script>
/**
* @name LegacySVGLoader
* @author mrdoob / http://mrdoob.com/
* @author zz85 / http://joshuakoo.com/
* @see https://github.com/mrdoob/three.js/issues/14387
*/

THREE.LegacySVGLoader = function(manager) {

this.manager = (manager !== undefined) ? manager : THREE.DefaultLoadingManager;

};

THREE.LegacySVGLoader.prototype = {

constructor: THREE.LegacySVGLoader,

load: function(url, onLoad, onProgress, onError) {

var scope = this;

var parser = new DOMParser();

var loader = new THREE.FileLoader(scope.manager);
loader.load(url, function(svgString) {

var doc = parser.parseFromString(svgString, 'image/svg+xml'); // application/xml

onLoad(doc.documentElement);

}, onProgress, onError);

}

};
</script>

关于svg - 如何在three.js中将SVG文件加载到SVGRenderer中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33140342/

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