Advertisement
Guest User

Ground

a guest
Jun 17th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Ground elements import
  2. import Normal from '../emotions/anger/template/Normal';
  3. import River from '../emotions/anger/template/River';
  4.  
  5. class Ground {
  6.     constructor(size = chunkSize, segments = 1, amp = 0.5) {
  7.         this.size = size;
  8.         this.segments = segments;
  9.         this.amp = amp;
  10.         this.mapNumber = 9;
  11.  
  12.         this.pieces = {
  13.             size: this.size,
  14.             piece: []
  15.         };
  16.  
  17.         this.uniforms = {
  18.             time: {
  19.                 value: 0
  20.             },
  21.             mouse: {
  22.                 value: {
  23.                     x: 0,
  24.                     y: 0,
  25.                     z: 0
  26.                 }
  27.             },
  28.             size: {
  29.                 value: 300
  30.             }
  31.         };
  32.     }
  33.  
  34.     init() {
  35.         return new Promise(async resolve => {
  36.             await this.createGround();
  37.             resolve();
  38.         })
  39.     }
  40.  
  41.     /**
  42.      *
  43.      * @param color
  44.      * @returns {THREE.Mesh}
  45.      */
  46.     createPiece(isNormal = true, color = '#b32b00') {
  47.  
  48.         let uniforms = this.uniforms;
  49.        
  50.         let geom = new THREE.PlaneBufferGeometry(this.size, this.size, this.segments, this.segments);
  51.  
  52.         /*
  53.         if(isNormal) {
  54.             let mat = new THREE.ShaderMaterial({
  55.                 uniforms,
  56.                 vertexShader: `
  57.                     varying vec3 vPos;
  58.                     uniform float time;
  59.                     uniform vec3 mouse;
  60.                     uniform float size;
  61.  
  62.                     //  https://github.com/BrianSharpe/Wombat/blob/master/  SimplexPerlin2D.glsl
  63.                     float SimplexPerlin2D( vec2 P ) {
  64.                         const float SKEWFACTOR = 0.36602540378443864676372317075294;
  65.                         const float UNSKEWFACTOR = 0.21132486540518711774542560974902;
  66.                         const float SIMPLEX_TRI_HEIGHT = 0.70710678118654752440084436210485;
  67.                         const vec3 SIMPLEX_POINTS = vec3( 1.0-UNSKEWFACTOR, -UNSKEWFACTOR, 1.0-2.0*UNSKEWFACTOR );
  68.                         P *= SIMPLEX_TRI_HEIGHT;
  69.                         vec2 Pi = floor( P + dot( P, vec2( SKEWFACTOR ) ) );
  70.                         vec4 Pt = vec4( Pi.xy, Pi.xy + 1.0 );
  71.                         Pt = Pt - floor(Pt * ( 1.0 / 71.0 )) * 71.0;
  72.                         Pt += vec2( 26.0, 161.0 ).xyxy;
  73.                         Pt *= Pt;
  74.                         Pt = Pt.xzxz * Pt.yyww;
  75.                         vec4 hash_x = fract( Pt * ( 1.0 / 951.135664 ) );
  76.                         vec4 hash_y = fract( Pt * ( 1.0 / 642.949883 ) );
  77.                         vec2 v0 = Pi - dot( Pi, vec2( UNSKEWFACTOR ) ) - P;
  78.                         vec4 v1pos_v1hash = (v0.x < v0.y) ? vec4(SIMPLEX_POINTS.xy, hash_x.y, hash_y.y) : vec4(SIMPLEX_POINTS.yx, hash_x.z, hash_y.z);
  79.                         vec4 v12 = vec4( v1pos_v1hash.xy, SIMPLEX_POINTS.zz ) + v0.xyxy;
  80.                         vec3 grad_x = vec3( hash_x.x, v1pos_v1hash.z, hash_x.w ) - 0.49999;
  81.                         vec3 grad_y = vec3( hash_y.x, v1pos_v1hash.w, hash_y.w ) - 0.49999;
  82.                         vec3 grad_results = inversesqrt( grad_x * grad_x + grad_y * grad_y ) * ( grad_x * vec3( v0.x, v12.xz ) + grad_y * vec3( v0.y, v12.yw ) );
  83.                         const float FINAL_NORMALIZATION = 99.204334582718712976990005025589;
  84.                         vec3 m = vec3( v0.x, v12.xz ) * vec3( v0.x, v12.xz ) + vec3( v0.y, v12.yw ) * vec3( v0.y, v12.yw );
  85.                         m = max(0.5 - m, 0.0);
  86.                         m = m*m;
  87.                         return dot(m*m, grad_results) * FINAL_NORMALIZATION;
  88.                     }
  89.  
  90.                     float map_range(float value, float low1, float high1, float low2, float high2) {
  91.                         return low2 + (high2 - low2) * (value - low1) / (high1 - low1);
  92.                     }
  93.  
  94.                     void main() {
  95.                         vPos = position;
  96.                         float distToMouse = length(position - mouse);
  97.                         float maxoffs = map_range(SimplexPerlin2D(uv * 2.5 + time * 0.6), -1.0, 1.0, 0.0, 15.);
  98.                         // float maxoffs = 15.;
  99.                         vPos.z += min(maxoffs * map_range(distToMouse, -20., 20., 0., 1.), maxoffs);
  100.                         float smoothEdge = .00001;
  101.                         float edges = (
  102.                               smoothstep(0.0, smoothEdge, uv.x)
  103.                               * smoothstep(0.0, smoothEdge, uv.y)
  104.                               * smoothstep(1.0, 1.0 - smoothEdge, uv.x)
  105.                               * smoothstep(1.0, 1.0 - smoothEdge, uv.y)
  106.                         );
  107.                         vPos.z *= edges;
  108.                         gl_Position = projectionMatrix * modelViewMatrix * vec4(vPos, 1.0);
  109.                     }
  110.                 `,
  111.                 fragmentShader: `
  112.                     varying vec3 vPos;
  113.  
  114.                     float when_gt(float x, float y) {
  115.                       return max(sign(x - y), 0.0);
  116.                     }
  117.  
  118.                     float when_le(float x, float y) {
  119.                       return 1.0 - when_gt(x, y);
  120.                     }
  121.  
  122.                     float grid(vec3 pos, vec3 axis, float size) {
  123.                         float width = 1.0;
  124.  
  125.                         // Grid size
  126.                         vec3 tile = pos / size;
  127.  
  128.                         // Grid centered gradient
  129.                         vec3 level = abs(fract(tile) - 0.5);
  130.  
  131.                         // Derivative (crisp line)
  132.                         vec3 deri = fwidth(tile);
  133.  
  134.                         vec3 grid3D = clamp(vPos, 0.0, 1.0);
  135.                         return grid3D.z;
  136.                     }
  137.  
  138.                     void main() {
  139.                         float l = grid(vPos, vec3(1.0, 1.0, 1.0), 5.0);
  140.                         gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
  141.                         if (vPos.z < 0.0) discard;
  142.                     }
  143.                 `,
  144.                 extensions: {
  145.                     derivatives: true
  146.                 }
  147.             });
  148.         }else {
  149.             let mat = new THREE.ShaderMaterial({
  150.                 uniforms: {
  151.                     "startColor": {
  152.                         type: "c",
  153.                         value: new THREE.Color(COLORS.orange),
  154.                     },
  155.                     "middleColor": {
  156.                         type: "c",
  157.                         value: new THREE.Color(COLORS.blue),
  158.                     },
  159.                     "endColor": {
  160.                         type: "c",
  161.                         value: new THREE.Color(COLORS.orange),
  162.                     },
  163.                 },
  164.                 vertexShader: `
  165.                 varying vec2 vUv;
  166.                
  167.                 void main() {
  168.                     vUv = uv;
  169.                     gl_Position = projectionMatrix *
  170.                                   modelViewMatrix *
  171.                                   vec4(position, 1.0);
  172.                 }
  173.                 `,
  174.                 fragmentShader: `
  175.                 uniform vec3 startColor;
  176.                 uniform vec3 middleColor;
  177.                 uniform vec3 endColor;
  178.                 vec3 color;
  179.                
  180.                 varying vec2 vUv;
  181.                
  182.                 void main() {
  183.                     color = mix(startColor,endColor,vUv.y);
  184.                
  185.                     gl_FragColor = vec4(color,1.0);
  186.                 }
  187.                 `,
  188.             })
  189.         }
  190.         */
  191.  
  192.         let mat = new THREE.MeshBasicMaterial({color: color});
  193.         let ground = new THREE.Mesh(geom, mat);
  194.  
  195.         ground.name = "chunk";
  196.  
  197.         // Utility, debug purpose only
  198.         let edges = new THREE.EdgesGeometry(geom);
  199.         let line = new THREE.LineSegments(edges, new THREE.LineBasicMaterial({color: 'red'}));
  200.         //ground.add(line);
  201.         // Utilities
  202.  
  203.         ground.rotation.x = Math.PI / 180 * -90;
  204.         ground.position.y = 0;
  205.  
  206.         return ground;
  207.     }
  208.  
  209.     /**
  210.      *
  211.      */
  212.     createGround() {
  213.         return new Promise(async resolve => {
  214.             let piece, template, templateID, templateName, posChunkX, posChunkZ;
  215.             let piecesNumber = 0;
  216.             let row = Math.sqrt(this.mapNumber);
  217.             for (let x = 0; x < row; x++) {
  218.                 for (let y = 0; y < row; y++) {
  219.                     posChunkX = x * this.size - ((this.size * 3) / 2) + (this.size / 2);
  220.                     posChunkZ = y * this.size - ((this.size * 3) / 2) + (this.size / 2);
  221.  
  222.                     window.grounds.push(
  223.                         {
  224.                             id: piecesNumber,
  225.                             elmt: undefined,
  226.                             posX: posChunkX,
  227.                             posZ: posChunkZ,
  228.                             objects: [],
  229.                             corals: [],
  230.                             unusedCorals: [],
  231.                         }
  232.                     );
  233.  
  234.                     if (!this.checkChunkTemplate(posChunkX)) {
  235.                         await this.loadNormalTemplate(piecesNumber, posChunkX, posChunkZ, true);
  236.                         window.physics.add('chunk', {
  237.                             groundID: piecesNumber,
  238.                             size: {
  239.                                 x: this.size,
  240.                                 y: this.size,
  241.                                 z: this.size
  242.                             },
  243.                             mass: 0,
  244.                             position: {
  245.                                 x: posChunkX,
  246.                                 y: -1,
  247.                                 z: posChunkZ
  248.                             },
  249.                             materials: {
  250.                                 state: 2,
  251.                                 texture: 'sand'
  252.                             }
  253.                         });
  254.                     } else {
  255.                         await this.loadRiverTemplate(piecesNumber, posChunkX, posChunkZ, true);
  256.                         window.physics.add('chunk', {
  257.                             groundID: piecesNumber,
  258.                             size: {
  259.                                 x: this.size,
  260.                                 y: this.size,
  261.                                 z: this.size
  262.                             },
  263.                             mass: 0,
  264.                             position: {
  265.                                 x: posChunkX,
  266.                                 y: -1,
  267.                                 z: posChunkZ
  268.                             },
  269.                             materials: {
  270.                                 state: 2,
  271.                                 texture: 'lava'
  272.                             }
  273.                         });
  274.                     }
  275.  
  276.                     piecesNumber++;
  277.                 }
  278.             }
  279.             console.log(grounds);
  280.             resolve();
  281.         });
  282.     }
  283.  
  284.     checkChunkTemplate(coord) {
  285.         return (coord < -chunkSize / 2 && coord > -chunkSize * 1.5)
  286.     }
  287.  
  288.     loadNormalTemplate(piecesNumber, posChunkX, posChunkZ, isInit) {
  289.         return new Promise(async resolve => {
  290.             await Normal.wait(piecesNumber, {x: posChunkX, y: posChunkZ}, isInit);
  291.             resolve();
  292.         })
  293.     }
  294.  
  295.     loadRiverTemplate(piecesNumber, posChunkX, posChunkZ, isInit) {
  296.         return new Promise(async resolve => {
  297.             await River.wait(piecesNumber, {x: posChunkX, y: posChunkZ}, isInit);
  298.             resolve();
  299.         })
  300.     }
  301. }
  302.  
  303.  
  304. const ground = {
  305.     wait() {
  306.         return new Promise(async resolve => {
  307.             const newGround = new Ground();
  308.             await newGround.init();
  309.             resolve();
  310.         });
  311.     }
  312. };
  313.  
  314. export default ground;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement