Skip to content

Commit 18af8f5

Browse files
committed
add fresnel
1 parent bcdbdeb commit 18af8f5

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

src/scripts/shader.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import * as THREE from "three";
22

3+
34
import _frag from "./test.frag";
45
import _vert from "./test.vert";
56

7+
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
68

79
// const geometry = new THREE.IcosahedronGeometry(1, 5);
810
// const material = new THREE.MeshStandardMaterial();
@@ -35,18 +37,34 @@ scene.add(mesh);
3537

3638
const renderer = new THREE.WebGLRenderer({ antialias: true });
3739
renderer.setSize(width, height);
38-
renderer.setAnimationLoop(animate);
39-
// document.body.appendChild(renderer.domElement);
40+
document.body.appendChild(renderer.domElement);
4041
const elem = document.getElementById("my-canvas");
4142
if (elem !== null) {
4243
elem.appendChild(renderer.domElement);
4344
}
4445

4546
// animation
4647

47-
function animate(time: number) {
48-
mesh.rotation.x = time / 2000;
49-
mesh.rotation.y = time / 1000;
48+
// function animate(time: number) {
49+
// mesh.rotation.x = time / 2000;
50+
// mesh.rotation.y = time / 1000;
51+
52+
// renderer.render(scene, camera);
53+
// }
54+
5055

56+
renderer.setAnimationLoop(animate);
57+
// const controls = new OrbitControls(camera, renderer.domElement)
58+
// controls.enableDamping = true
59+
60+
const controls = new OrbitControls(camera, renderer.domElement);
61+
controls.update();
62+
// camera.position.set(0, 20, 100);
63+
64+
function animate() {
65+
requestAnimationFrame(animate); // required if controls.enableDamping or controls.autoRotate are set to true
66+
controls.update();
5167
renderer.render(scene, camera);
5268
}
69+
70+
// renderer.render(scene, camera);

src/scripts/test.frag

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
varying vec3 vPosition;
22
varying vec3 vNormal;
3+
varying vec2 vUv;
34

45
void main() {
5-
// gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
6-
// float c = sin(pos.x * 16.) / 2.0;
7-
vec3 c = normalize(vNormal * vNormal);
6+
vec3 c = normalize(cameraPosition - vPosition);
7+
float fresnel = 1. - dot(c, vNormal);
88

9-
gl_FragColor = vec4(c, 1.0);
10-
// gl_FragColor = vec4(vec3(1., 1., 1.), 1.0);
9+
// gl_FragColor = vec4(c, 1.0);
10+
gl_FragColor = vec4(vec3(fresnel), 1.0);
1111
}

src/scripts/test.vert

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
varying vec3 vPosition;
22
varying vec3 vNormal;
3+
varying vec2 vUv;
34

45
void main() {
56
vPosition = position;
67
vNormal = normal;
8+
vUv = uv;
79

8-
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
10+
vec4 res = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
11+
gl_Position = res;
912
}

0 commit comments

Comments
 (0)