Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. import {
  3.     // Math as Math2,
  4.     Mesh,
  5.     Geometry,
  6.     CylinderGeometry,
  7.     MeshPhongMaterial,
  8.     FaceColors
  9. } from "../../node_modules/three/build/three.module.js";
  10. import { linear as random } from "../../util/random.mjs";
  11. import { WOOD } from "../colors.mjs";
  12.  
  13. const createPost = ( { height, width, color } ) => {
  14.  
  15.     const post = new CylinderGeometry(
  16.         width + Math.random() / 24,
  17.         width + Math.random() / 24,
  18.         height + Math.random() / 16 );
  19.  
  20.     for ( let i = 0; i < post.faces.length; i ++ )
  21.         post.faces[ i ].color = color.clone().offsetHSL( random( 1 / 72 ), 0, 0 );
  22.  
  23.     post.rotateX( Math.PI / 2 + ( Math.random() - 0.5 ) / 6 );
  24.     post.rotateZ( Math.PI * Math.random() );
  25.     post.rotateY( ( Math.random() - 0.5 ) / 16 );
  26.     post.translate( 0, 0, height / 2 );
  27.  
  28.     return post;
  29.  
  30. };
  31.  
  32. const createPosts = ( { length, width, height, angle, color } ) => {
  33.  
  34.     const geometry = new Geometry();
  35.  
  36.     const postDisplacement = length / 2 - Math.random() / 16;
  37.  
  38.     const leftPost = createPost( { height, width, color } );
  39.     leftPost.translate( Math.cos( angle + Math.PI / 2 ) * - postDisplacement, Math.sin( angle + Math.PI / 2 ) * - postDisplacement, 0 );
  40.     geometry.merge( leftPost );
  41.  
  42.     const rightPost = createPost( { height, width, color } );
  43.     rightPost.translate( Math.cos( angle + Math.PI / 2 ) * postDisplacement, Math.sin( angle + Math.PI / 2 ) * postDisplacement, 0 );
  44.     geometry.merge( rightPost );
  45.  
  46.     return geometry;
  47.  
  48. };
  49.  
  50. const createRail = ( { width, length, color } ) => {
  51.  
  52.     const rail = new CylinderGeometry(
  53.         width + Math.random() / 24,
  54.         width + Math.random() / 24,
  55.         length + width + Math.random() / 4 );
  56.  
  57.     for ( let i = 0; i < rail.faces.length; i ++ )
  58.         rail.faces[ i ].color = color.clone().offsetHSL( random( 1 / 72 ), 0, 0 );
  59.  
  60.     rail.rotateY( Math.PI * Math.random() );
  61.     rail.rotateX( ( Math.random() - 0.5 ) / 4 / length );
  62.  
  63.     return rail;
  64.  
  65. };
  66.  
  67. const createRails = ( { length, height, width, angle, color } ) => {
  68.  
  69.     const geometry = new Geometry();
  70.  
  71.     const topRail = createRail( { width, length, color } );
  72.     topRail.translate( 0, 0, height / 3 );
  73.     topRail.rotateZ( angle );
  74.     geometry.merge( topRail );
  75.  
  76.     const bottomRail = createRail( { width, length, color } );
  77.     bottomRail.translate( 0, 0, height / 3 * 2 );
  78.     bottomRail.rotateZ( angle );
  79.     geometry.merge( bottomRail );
  80.  
  81.     return geometry;
  82.  
  83. };
  84.  
  85. export default class Fence extends Mesh {
  86.  
  87.     constructor( {
  88.         length = 2 - 1 / 4,
  89.         width = 1 / 24,
  90.         height = 1 / 2,
  91.         angle = 0,
  92.         color = WOOD.clone().offsetHSL( random( 1 / 64 ), 0, 0 )
  93.     } = {} ) {
  94.  
  95.         const geometry = new Geometry();
  96.         const material = new MeshPhongMaterial( {
  97.             vertexColors: FaceColors,
  98.             flatShading: true
  99.         } );
  100.  
  101.         geometry.merge( createPosts( { length, width, height, angle, color } ) );
  102.         geometry.merge( createRails( { length, width, height, angle, color } ) );
  103.  
  104.         geometry.computeFaceNormals();
  105.         geometry.computeVertexNormals();
  106.  
  107.         super( geometry, material );
  108.  
  109.         this.castShadow = true;
  110.         this.receiveShadow = true;
  111.  
  112.     }
  113.  
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement