Advertisement
Guest User

wew

a guest
Oct 14th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.25 KB | None | 0 0
  1. import { Component, OnInit } from '@angular/core';
  2. import {
  3. EdgesGeometry, LineSegments, LineBasicMaterial, Color,
  4. Material, GridHelper, WebGLRenderer, DoubleSide, Scene, PerspectiveCamera, BoxGeometry,
  5. MeshBasicMaterial, Mesh, Vector3, Group, TextureLoader, LinearFilter, Clock, AmbientLight, SpotLight, HemisphereLight} from 'three';
  6. import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
  7. import { PointerLockControls } from 'three/examples/jsm/controls/PointerLockControls.js';
  8. import { Interaction } from 'three.interaction';
  9.  
  10. @Component({
  11. selector: 'app-root',
  12. templateUrl: './app.component.html',
  13. styleUrls: ['./app.component.scss']
  14. })
  15. export class AppComponent implements OnInit {
  16. title = 'homeshapers';
  17.  
  18. protected scene: Scene;
  19. protected renderer: WebGLRenderer;
  20. protected textureLoader: TextureLoader;
  21.  
  22. ngOnInit() {
  23. this.scene = new Scene();
  24. this.scene.background = new Color('black'); // 0xf0f0f0
  25. this.textureLoader = new TextureLoader();
  26.  
  27. var camera = new PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 10000);
  28. camera.position.set(0, 250, 250);
  29.  
  30. this.renderer = new WebGLRenderer({ antialias: true, powerPreference: 'high-performance' });
  31. this.renderer.setPixelRatio( window.devicePixelRatio );
  32. this.renderer.setSize(window.innerWidth, window.innerHeight);
  33. document.body.appendChild(this.renderer.domElement);
  34.  
  35. const controls = new OrbitControls(camera, this.renderer.domElement);
  36. // const controls = new PointerLockControls(camera, this.renderer.domElement);
  37.  
  38. new Interaction(this.renderer, this.scene, camera);
  39.  
  40. const helper = new GridHelper(2000, 100);
  41. helper.position.y = - 200;
  42. (helper.material as Material).opacity = 0.25;
  43. (helper.material as Material).transparent = true;
  44. this.scene.add(helper);
  45.  
  46. var light = new HemisphereLight( 0xeeeeff, 0x777788, 0.75 );
  47. light.position.set( 0.5, 1, 0.75 );
  48. this.scene.add( light );
  49.  
  50. const wallsAndFloors = [
  51. // wall
  52. this.createShape(new BoxGeometry(100, 100, 5), new Vector3(0, 52.5, 47.5), 'wall', [8, 9, 10, 11]),
  53. this.createShape(new BoxGeometry(100, 100, 5), new Vector3(0, 52.5, -47.5), 'wallcoat', [8, 9, 10, 11]),
  54.  
  55. // floor
  56. this.createShape(new BoxGeometry(100, 5, 100), new Vector3(0, 0, 0), 'carpet', [4, 5]),
  57. this.createShape(new BoxGeometry(100, 5, 100), new Vector3(100, 0, 0), 'floorwood', [4, 5]),
  58. ];
  59.  
  60. this.scene.add(...wallsAndFloors);
  61.  
  62. controls.enableDamping = true;
  63. controls.dampingFactor = 0.05;
  64. // controls.update(clock.getDelta());
  65.  
  66. var animate = () => {
  67. requestAnimationFrame(() => animate());
  68.  
  69. controls.update();
  70.  
  71. this.renderer.render(this.scene, camera);
  72. };
  73.  
  74. animate();
  75. }
  76.  
  77. protected createShape(geo: BoxGeometry, pos: Vector3, textureUrl: string, textureFaces: number[]) {
  78. geo.computeFaceNormals();
  79.  
  80. const material = new MeshBasicMaterial({ map: this.getTexture(textureUrl) });
  81. const materialColor = new MeshBasicMaterial({ map: this.getTexture('concrete') });
  82.  
  83. geo.faces.forEach((f, i) => f.materialIndex = textureFaces.indexOf(i) >= 0 ? 0 : 1);
  84.  
  85. geo.sortFacesByMaterialIndex();
  86.  
  87. const cube = new Mesh(geo, [material, materialColor]);
  88. const edges = new EdgesGeometry(geo);
  89. const line = new LineSegments(edges, new LineBasicMaterial({ color: 'grey' }));
  90. const interactable = cube as any as Interactable;
  91. interactable.cursor = 'pointer';
  92. interactable.on('click', (event) => {
  93. (line.material as LineBasicMaterial).linewidth = 5;
  94. (line.material as LineBasicMaterial).color = new Color('red');
  95. });
  96.  
  97. cube.position.set(pos.x, pos.y, pos.z);
  98. line.position.set(pos.x, pos.y, pos.z);
  99.  
  100. return new Group().add(line).add(cube);
  101. }
  102.  
  103. protected getTexture(id: string) {
  104. const textureLoader = new TextureLoader();
  105. const texture = textureLoader.load(`/assets/textures/${id}.jpg`);
  106. texture.minFilter = LinearFilter;
  107. texture.magFilter = LinearFilter;
  108. return texture;
  109. }
  110.  
  111. protected makeControls(type: string) {
  112.  
  113. }
  114. }
  115.  
  116. export interface Interactable {
  117. cursor: string;
  118.  
  119. on(eventName: string, handler: (event: Event) => void): void;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement