我需要三个js开发人员的帮助

问题描述 投票:0回答: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);
  }

}
three.js three-tier 3d-rendering objloader
1个回答
0
投票

[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?

© www.soinside.com 2019 - 2024. All rights reserved.