Skip to content

Instantly share code, notes, and snippets.

@ChoWonmin
Last active October 9, 2019 04:25
Show Gist options
  • Save ChoWonmin/be3ff4bfdf6465a5410abee7fd9c0f53 to your computer and use it in GitHub Desktop.
Save ChoWonmin/be3ff4bfdf6465a5410abee7fd9c0f53 to your computer and use it in GitHub Desktop.

Revisions

  1. ChoWonmin revised this gist Oct 9, 2019. 2 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
  2. ChoWonmin revised this gist Oct 9, 2019. 1 changed file with 93 additions and 0 deletions.
    93 changes: 93 additions & 0 deletions main.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,93 @@
    import { Component, Vue } from 'vue-property-decorator';
    import * as THREE from 'three';
    import { OrbitControls } from 'three-orbitcontrols-ts';

    // @ts-ignore
    import vs from '!!raw-loader!./Wolf.vert';
    // @ts-ignore
    import fs from '!!raw-loader!./Wolf.frag';
    import { Vector3 } from 'three';

    @Component({
    components: {
    //
    },
    })
    export default class Wolf extends Vue {
    private camera: any = null;
    private scene: any = null;
    private renderer: any = null;
    private controls: any = null;
    private material: any = null;
    private requestAnimationID: number = 0;

    private width: number = -1;
    private height: number = -1;
    private start: number = -1;

    private init() {
    const container = document.getElementById('container') as HTMLElement;

    this.start = Date.now();
    this.width = container.clientWidth;
    this.height = container.clientHeight;

    this.scene = new THREE.Scene();
    this.camera = new THREE.PerspectiveCamera(
    70,
    this.width / this.height,
    1,
    15000,
    );
    this.camera.position.z = 1000;

    this.material = new THREE.RawShaderMaterial({
    uniforms: {
    time: { type: 'f', value: 0.0 },
    },
    vertexShader: vs,
    fragmentShader: fs,
    side: THREE.DoubleSide,
    });

    const geometry = new THREE.PlaneBufferGeometry(200, 200);

    const mesh = new THREE.Mesh(geometry, this.material);
    this.scene.add(mesh);

    this.renderer = new THREE.WebGLRenderer({
    antialias: true,
    });

    this.renderer.setSize(this.width, this.height);

    if (container) {
    container.appendChild(this.renderer.domElement);
    this.controls = new OrbitControls(this.camera, this.renderer.domElement);
    }
    }

    private animate() {
    this.requestAnimationID = requestAnimationFrame(this.animate);
    this.material.uniforms.time.value = 0.00004 * (Date.now() - this.start);

    this.controls.update();
    this.renderer.render(this.scene, this.camera);
    }

    private mounted() {
    this.init();
    this.animate();
    }

    private beforeDestroy() {
    this.scene.dispose();
    this.renderer.dispose();
    this.renderer.forceContextLoss();
    window.cancelAnimationFrame(this.requestAnimationID);
    // @ts-ignore release force
    this.renderer.domElement = null;
    // @ts-ignore release force
    this.renderer = null;
    }
    }
  3. ChoWonmin revised this gist Oct 9, 2019. 3 changed files with 35 additions and 1 deletion.
    12 changes: 12 additions & 0 deletions frag
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    precision mediump float;

    uniform float time;

    varying vec3 vPosition;
    varying vec2 vUv;

    void main() {

    gl_FragColor = vec4( 1.0, 0.0, 0.0, 1.0);

    }
    1 change: 0 additions & 1 deletion test
    Original file line number Diff line number Diff line change
    @@ -1 +0,0 @@
    hello world
    23 changes: 23 additions & 0 deletions vert
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    precision mediump float;

    uniform mat4 modelMatrix;
    uniform mat4 modelViewMatrix;
    uniform mat4 projectionMatrix;
    uniform vec3 cameraPosition;

    attribute vec2 uv;
    attribute vec3 position;
    attribute vec3 normal;

    varying vec3 vPosition;
    varying vec2 vUv;

    void main() {

    vUv = uv;
    vPosition = position;

    gl_Position = projectionMatrix * modelViewMatrix * vec4( vPosition, 1.0 );

    }

  4. ChoWonmin created this gist Oct 9, 2019.
    1 change: 1 addition & 0 deletions test
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    hello world