소스 검색

shader demo

hsiao 5 년 전
부모
커밋
b65409c40b
2개의 변경된 파일98개의 추가작업 그리고 50개의 파일을 삭제
  1. 60
    49
      public/scenes/foo.gltf
  2. 38
    1
      src/index.ts

+ 60
- 49
public/scenes/foo.gltf
파일 크기가 너무 크기때문에 변경 상태를 표시하지 않습니다.
파일 보기


+ 38
- 1
src/index.ts 파일 보기

@@ -1,6 +1,6 @@
1 1
 import * as THREE from "three"
2 2
 import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'
3
-import { Object3D } from "three";
3
+import { Object3D, Mesh, ShaderMaterial } from "three";
4 4
 
5 5
 import { GameController } from "./controller"
6 6
 
@@ -114,16 +114,53 @@ export class FooGame {
114 114
 
115 115
 let game = new FooGame();
116 116
 
117
+let vertexShader =  `
118
+uniform float time;
119
+varying vec3 vPos;
120
+void main() {
121
+	vPos=position;
122
+	vPos.x += sin(time * vPos.z) * 2.0;
123
+	vPos.y += cos(time * vPos.z) * 2.0;
124
+	//vPos.z += tan(time * vPos.z) * 4.0;
125
+	gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
126
+}
127
+`;
128
+
129
+let fragmentShader =  `
130
+varying vec3 vPos;
131
+void main() {
132
+	gl_FragColor = vec4( vPos, 1.0 );
133
+}
134
+`;
135
+
117 136
 game.loadScene(() => {
118 137
     let box = game.scene.getObjectByName("Box");
119 138
     if (box) {
120 139
         game.bindKeyAction(box);
140
+        let mat:ShaderMaterial;
141
+        if (box instanceof Mesh) {
142
+            let mesh: Mesh = box;
143
+            
144
+            mat = new ShaderMaterial({
145
+                uniforms: {
146
+                    time: {
147
+                        value: 0
148
+                    }
149
+                },
150
+                vertexShader: vertexShader,
151
+                fragmentShader: fragmentShader
152
+            });
153
+            mesh.material = mat;
154
+        }
155
+
121 156
         game.setUpdate(box, (obj: Object3D) => {
122 157
             //game.camera.rotateOnAxis(obj.position, 0.01)
123 158
             //obj.position.x += performance.now() * 0.000001;
124 159
             //obj.position.z += performance.now() * 0.000001;
125 160
             obj.rotateY(0.01);
161
+
126 162
             //game.camera.lookAt(obj.position)
163
+            mat.uniforms.time.value = performance.now()/500;
127 164
         });
128 165
     }
129 166
 })

Loading…
취소
저장