[英]Scaling a 2D SVG group object in three.js
I'm attempting to create a map of 2d SVG tiles in three.js.我正在尝试在 three.js 中创建二维 SVG 瓷砖的 map。 I have used
SVGLoader()
Like so (Keep in mind some brackets are for parent scopes that aren't shown. That is not the issue):我已经使用
SVGLoader()
像这样(请记住,一些括号用于未显示的父范围。这不是问题):
loader = new SVGLoader();
loader.load(
// resource URL
filePath,
// called when the resource is loaded
function ( data ) {
console.log("SVG file successfully loaded");
const paths = data.paths;
for ( let i = 0; i < paths.length; i ++ ) {
const path = paths[ i ];
const material = new THREE.MeshBasicMaterial( {
color: path.color,
side: THREE.DoubleSide,
depthWrite: false
} );
const shapes = SVGLoader.createShapes( path );
console.log(`Shapes length = ${shapes.length}`);
try{
for ( let j = 0; j < shapes.length; j ++ ) {
const shape = shapes[ j ];
const geometry = new THREE.ShapeGeometry( shape );
const testGeometry = new THREE.PlaneGeometry(2,2);
try{
const mesh = new THREE.Mesh(geometry, material );
group.add( mesh );
}catch(e){console.log(e)}
}
}catch(e){console.log(e)}
}
},
// called when loading is in progress
function ( xhr ) {
console.log( ( xhr.loaded / xhr.total * 100 ) + '% loaded' );
},
// called when loading has errors
function ( error ) {
console.log( 'An error happened' );
}
);
return group;
}
Dismiss the fact that I surrounded alot of it in try{}catch(){}
忽略我在
try{}catch(){}
中包含了很多它的事实
I have also created grid lines and added it to my axis helper in the application that allows me to see where each cooordinate is, in relation to the X and Y axis.我还创建了网格线并将其添加到应用程序中的轴助手中,这样我就可以查看每个坐标相对于 X 和 Y 轴的位置。
This is how the svg appears on screen: Application Output这是 svg 出现在屏幕上的方式: Application Output
I can't seem to figure out how to correlate the scale of the svg, with the individual grid lines.我似乎无法弄清楚如何将 svg 的比例与各个网格线相关联。 I have a feeling that Im going to have to dive deeper into the SVG loading script that I have above then scale each shape mesh specifically.
我有一种感觉,我必须深入研究上面的 SVG 加载脚本,然后专门缩放每个形状网格。 I call the SVG group itself in the following code.
我在以下代码中调用 SVG 组本身。
try{
//SVG returns a group, TGA returns a texture to be added to a material
var object1 = LOADER.textureLoader("TGA", './Art/tile1.tga', pGeometry);
var object2 = LOADER.textureLoader("SVG", '/Art/bitmap.svg');
const testMaterial = new THREE.MeshBasicMaterial({
color: 0xffffff,
map: object1,
side: THREE.DoubleSide
});
//const useMesh = new THREE.Mesh(pGeometry, testMaterial);
//testing scaling the tile
try{
const worldScale = new THREE.Vector3();
object2.getWorldScale(worldScale);
console.log(`World ScaleX: ${worldScale.x} World ScaleY: ${worldScale.y} World ScaleZ: ${worldScale.z}`);
//object2.scale.set(2,2,0);
}catch(error){console.log(error)}
scene.add(object2);
}
Keep in mind that the SVG is object2 in this case.请记住,在这种情况下,SVG 是 object2。 Some of the ideas to tackle this problem I have had is looking into what a world scale is, matrix4 transformations, and the scale methods of either the object3d parent properties or the bufferGeometry parent properties of this particular svg group object.
解决这个问题的一些想法是研究什么是世界比例,matrix4 转换,以及这个特定 svg 组 object 的 object3d 父属性或 bufferGeometry 父属性的缩放方法。 I am also fully aware that three.js is designed for 3d graphics, however I would like to master 2d graphics programming in this library before I get into the 3d aspect of things.
我也完全知道 three.js 是为 3d 图形设计的,但是在我进入 3d 方面之前,我想在这个库中掌握 2d 图形编程。 I also have a thought that the scale of the SVG group is distinctly different from the scale of the scene and its XY and Z axis.
我还认为 SVG 组的比例与场景的比例及其 XY 和 Z 轴明显不同。
If this question has already been answered a link to the corresponding answer would be of great help to me.如果已经回答了这个问题,那么指向相应答案的链接将对我有很大帮助。
Thank you for the time you take to answer this question.感谢您花时间回答这个问题。
I messed with the dimensions of the svg file itself in the editor I used to paint it and I got it to scale.我在我用来绘制它的编辑器中弄乱了 svg 文件本身的尺寸,然后我让它按比例缩放。 Not exactly a solution in the code, however I guess the code is just closely tied to the data that the svg file provides and cant be altered too much.
不完全是代码中的解决方案,但我猜代码只是与 svg 文件提供的数据密切相关,不能进行太多更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.