简体   繁体   English

“无法设置未定义的属性 'getStrokeStyle'”对于 Three.js 程序意味着什么?

[英]What does "Cannot set property 'getStrokeStyle' of undefined" mean for a Three.js program?

I'm using SVGloader to load an SVG so I can map it on my OBJ file.我正在使用 SVGloader 加载一个 SVG,这样我就可以将它映射到我的 OBJ 文件中。 But when gave it url to the svg file it generates an error但是当给它 url 到 svg 文件时,它会产生一个错误

TypeError:Cannot set property 'getStrokeStyle' of undefined类型错误:无法设置未定义的属性“getStrokeStyle”

I'm using Angular 8 and rendering a Obj file using THREE.js.我正在使用 Angular 8 并使用 THREE.js 渲染一个 Obj 文件。 I want to load an svg and map it on the obj file to add texture to that file, but as I told above it is generating an error and I don't know how to solve it.我想加载一个 svg 并将其映射到 obj 文件上以将纹理添加到该文件,但正如我上面所说,它产生了一个错误,我不知道如何解决它。

Here is code file.这是代码文件。

         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);
  }

}

The SVGLoader is not for loading svg-files to be used as textures but for loading them as Geometry: https://threejs.org/docs/#examples/en/loaders/SVGLoader SVGLoader不是用于加载 svg 文件以用作纹理,而是用于将它们加载为几何图形: https : SVGLoader

If you want to use an svg-file as a texture, you should be able to use the TextureLoader like this:如果你想使用 svg 文件作为纹理,你应该能够像这样使用TextureLoader

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

I'm not sure if you actually need to rasterize it to a canvas first, if the above doesn't work, try what is described here: How do you load and display SVG graphics in three.js?我不确定您是否真的需要先将其栅格化到画布上,如果上述方法不起作用,请尝试此处描述的内容: 如何在 Three.js 中加载和显示 SVG 图形?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM