-1

我正在使用 SVGloader 加载 SVG,以便将其映射到我的 OBJ 文件上。但是当将它的 url 提供给 svg 文件时,它会产生一个错误

TypeError:无法设置未定义的属性“getStrokeStyle”

我正在使用 Angular 8 并使用 THREE.js 渲染一个 Obj 文件。我想加载一个 svg 并将其映射到 obj 文件以向该文件添加纹理,但正如我在上面所说的那样,它正在生成一个错误,我不知道如何解决它。

这是代码文件。

         import { Component, AfterViewInit, ViewChild, Input, ElementRef } from '@angular/core';
         import * as THREE from 'three';
         import { OrbitControls } from '@avatsaev/three-orbitcontrols-ts';
         import {OBJLoader} from 'three-obj-mtl-loader';
         import {SVGLoader} from 'three-svg-loader';
         @Component({
          selector: 'app-scene',
          templateUrl: './scene.component.html',
          styleUrls: ['./scene.component.css']
           })
          export class SceneComponent implements AfterViewInit {
            @Input() name: string;
            @ViewChild('canvas', {static:true}) canvasRef: ElementRef;


            renderer = new THREE.WebGLRenderer;
            scene = null;
            camera = null;
            controls = null;
            mesh = null;
            light = null;
            loader;
            svgLoader;
            private calculateAspectRatio(): number {
            const height = this.canvas.clientHeight;
             if (height === 0) {
                return 0;
              }
             return this.canvas.clientWidth / this.canvas.clientHeight;
             }

              private get canvas(): HTMLCanvasElement {
              return this.canvasRef.nativeElement;
             }

  constructor() {
    // this.loader = new OBJLoader();
    this.scene = new THREE.Scene();
    this.loader = new OBJLoader();
    this.svgLoader = new SVGLoader();

    this.camera = new THREE.PerspectiveCamera(15, window.innerWidth / window.innerHeight, 0.1, 1000)
  }

  ngAfterViewInit() {
    this.configScene();
    this.configCamera();
    this.configRenderer();
    this.configControls();
    this.createLight();
    this.createMesh();
    this.animate();
  }

  configScene() {
    // this.scene.background = new THREE.Color( 0xdddddd );
  }

  configCamera() {
    this.camera.aspect = this.calculateAspectRatio();
    this.camera.updateProjectionMatrix();
      this.camera.position.set( 0, 0, 3 );
      this.camera.lookAt( this.scene.position );
  }

  configRenderer() {
    this.renderer = new THREE.WebGLRenderer({
      canvas: this.canvas,
      antialias: true,
      alpha: true
    });
    this.renderer.setPixelRatio(devicePixelRatio);

    // setClearColor for transparent background
    // i.e. scene or canvas background shows through
    this.renderer.setClearColor( 0x000000, 0 );
    this.renderer.setSize((window.innerWidth/2), (window.innerHeight/2));
    window.addEventListener('resize', ()=>{
      this.renderer.setSize((window.innerWidth/2), (window.innerHeight)/2);
      this.camera.aspect = window.innerWidth / window.innerHeight;
      this.camera.updateProjectionMatrix();
    })
    console.log('clientWidth', this.canvas.clientWidth);
    console.log('clientHeight', this.canvas.clientHeight);
  }

  configControls() {
    this.controls = new OrbitControls(this.camera);
    this.controls.autoRotate = false;
    this.controls.enableZoom = false;
    // this.controls.maxDistance = 5;
    // this.controls.minDistance = 10;
    this.controls.enablePan = false;
    this.controls.update();
  }


  createLight() {
    this.light = new THREE.PointLight( 0xffffff );
      this.light.position.set( -10, 10, 10 );
      this.scene.add( this.light );
  }
  createMesh() {
        this.svgLoader.load('../../../../assets/abc.svg')
        console.log("SVG Loader", this.svgLoader)

        this.loader.load('../../../../assets/nonunified.obj', (object)=>{
            object.traverse( function ( child ) {

              if ( child instanceof THREE.Mesh ) {
                  child.geometry.center();
              }
            } );

            this.scene.add(object)
          },
            // called when loading is in progresses
            function (xhr) {

            console.log( ( xhr.loaded / xhr.total * 100 ) + '% loaded' );

          },
        // called when loading has errors
            function ( error ) {
            console.log( 'An error happened' );

          }
        )}

  animate() {
    window.requestAnimationFrame(() => this.animate());
    this.controls.update();
    this.renderer.render(this.scene, this.camera);
  }

}
4

1 回答 1

1

SVGLoader不是用于加载 svg 文件以用作纹理,而是用于将它们加载为几何:https ://threejs.org/docs/#examples/en/loaders/SVGLoader

如果你想使用 svg 文件作为纹理,你应该能够使用TextureLoader这样的:

obj.material.map = new TextureLoader().load('../../../../assets/abc.svg');

我不确定您是否真的需要先将其光栅化到画布上,如果上述方法不起作用,请尝试此处描述的内容:How do you load and display SVG graphics in three.js?

于 2019-12-04T12:33:26.800 回答