From d045c255c6f46c8d6c41e4d07d2d47e807e44eb6 Mon Sep 17 00:00:00 2001 From: Florian <45694132+flo-bit@users.noreply.github.com> Date: Sun, 28 Jan 2024 07:45:52 +0100 Subject: [PATCH] switch planet glow --- .../FakeGlowMaterial/FakeGlowMaterial.svelte | 56 +++++++++++ .../FakeGlowMaterial.svelte.d.ts | 19 ++++ src/lib/3D/FakeGlowMaterial/fragment.ts | 29 ++++++ src/lib/3D/FakeGlowMaterial/vertex.ts | 10 ++ src/lib/3D/PlanetModel.svelte | 40 ++++++-- src/lib/3D/Scene.svelte | 6 +- src/lib/components/Contact.svelte | 14 ++- src/lib/components/Tools.svelte | 98 +++++++++++++++++++ src/routes/+page.svelte | 3 + 9 files changed, 257 insertions(+), 18 deletions(-) create mode 100644 src/lib/3D/FakeGlowMaterial/FakeGlowMaterial.svelte create mode 100644 src/lib/3D/FakeGlowMaterial/FakeGlowMaterial.svelte.d.ts create mode 100644 src/lib/3D/FakeGlowMaterial/fragment.ts create mode 100644 src/lib/3D/FakeGlowMaterial/vertex.ts create mode 100644 src/lib/components/Tools.svelte diff --git a/src/lib/3D/FakeGlowMaterial/FakeGlowMaterial.svelte b/src/lib/3D/FakeGlowMaterial/FakeGlowMaterial.svelte new file mode 100644 index 0000000..b9c5ee5 --- /dev/null +++ b/src/lib/3D/FakeGlowMaterial/FakeGlowMaterial.svelte @@ -0,0 +1,56 @@ + + + + + diff --git a/src/lib/3D/FakeGlowMaterial/FakeGlowMaterial.svelte.d.ts b/src/lib/3D/FakeGlowMaterial/FakeGlowMaterial.svelte.d.ts new file mode 100644 index 0000000..23b12d2 --- /dev/null +++ b/src/lib/3D/FakeGlowMaterial/FakeGlowMaterial.svelte.d.ts @@ -0,0 +1,19 @@ +import type { Events, Props, Slots } from '@threlte/core' +import { SvelteComponent } from 'svelte' +import type { ShaderMaterial, ColorRepresentation } from 'three' + +export type FakeGlowMaterialProps = Props & { + falloff?: number + glowInternalRadius?: number + glowColor?: ColorRepresentation + glowSharpness?: number +} + +export type FakeGlowMaterialEvents = Events +export type FakeGlowMaterialSlots = Slots + +export default class FakeGlowMaterial extends SvelteComponent< + FakeGlowMaterialProps, + FakeGlowMaterialEvents, + FakeGlowMaterialSlots +> {} diff --git a/src/lib/3D/FakeGlowMaterial/fragment.ts b/src/lib/3D/FakeGlowMaterial/fragment.ts new file mode 100644 index 0000000..8fd9992 --- /dev/null +++ b/src/lib/3D/FakeGlowMaterial/fragment.ts @@ -0,0 +1,29 @@ +import { ShaderChunk } from 'three'; + +export const fragmentShader = ` +uniform vec3 glowColor; +uniform float falloffAmount; +uniform float glowSharpness; +uniform float glowInternalRadius; + +varying vec3 vPosition; +varying vec3 vNormal; + +void main() +{ + // Normal + vec3 normal = normalize(vNormal); + if(!gl_FrontFacing) + normal *= - 1.0; + vec3 viewDirection = normalize(cameraPosition - vPosition); + float fresnel = dot(viewDirection, normal); + fresnel = pow(fresnel, glowInternalRadius + 0.1); + float falloff = smoothstep(0., falloffAmount, fresnel); + float fakeGlow = fresnel; + fakeGlow += fresnel * glowSharpness; + fakeGlow *= falloff; + gl_FragColor = vec4(clamp(glowColor * fresnel, 0., 1.0), clamp(fakeGlow, 0., 1.0) * 0.5); + + ${ShaderChunk.tonemapping_fragment} + ${ShaderChunk.colorspace_fragment} +}`; diff --git a/src/lib/3D/FakeGlowMaterial/vertex.ts b/src/lib/3D/FakeGlowMaterial/vertex.ts new file mode 100644 index 0000000..15db0d2 --- /dev/null +++ b/src/lib/3D/FakeGlowMaterial/vertex.ts @@ -0,0 +1,10 @@ +export const vertexShader = `varying vec3 vPosition; +varying vec3 vNormal; + +void main() { + vec4 modelPosition = modelMatrix * vec4(position, 1.0); + gl_Position = projectionMatrix * viewMatrix * modelPosition; + vec4 modelNormal = modelMatrix * vec4(normal, 0.0); + vPosition = modelPosition.xyz; + vNormal = modelNormal.xyz; +}` diff --git a/src/lib/3D/PlanetModel.svelte b/src/lib/3D/PlanetModel.svelte index 173e4d3..74c5a64 100644 --- a/src/lib/3D/PlanetModel.svelte +++ b/src/lib/3D/PlanetModel.svelte @@ -7,7 +7,9 @@ Command: npx @threlte/gltf@2.0.1 static/planet.gltf -t -T import * as THREE from 'three'; import { Group } from 'three'; import { T, type Props, type Events, type Slots, forwardEventHandlers } from '@threlte/core'; - import { useGltf } from '@threlte/extras'; + import { useGltf, useTexture } from '@threlte/extras'; + + import FakeGlowMaterial from './FakeGlowMaterial/FakeGlowMaterial.svelte'; type $$Props = Props; type $$Events = Events; @@ -94,8 +96,14 @@ Command: npx @threlte/gltf@2.0.1 static/planet.gltf -t -T float angle = pow(0.8 - abs(dot(viewDirection, vNormal)), 3.0); vec4 diffuseColor = vec4(diffuse, opacity * angle);` ); - }; + + // let map = useTexture('/night.jpg', { + // transform: (texture) => { + // //texture.encoding = THREE.LinearEncoding; + // return texture; + // } + // }); @@ -105,22 +113,20 @@ Command: npx @threlte/gltf@2.0.1 static/planet.gltf -t -T - - - - - + + + {:catch error} @@ -128,3 +134,17 @@ Command: npx @threlte/gltf@2.0.1 static/planet.gltf -t -T + + diff --git a/src/lib/3D/Scene.svelte b/src/lib/3D/Scene.svelte index 64af091..bee9dda 100644 --- a/src/lib/3D/Scene.svelte +++ b/src/lib/3D/Scene.svelte @@ -31,7 +31,7 @@ let rotation = 0; let distance = 1; - let rotationSpeed = 0.1; + let rotationSpeed = 0.5; useTask((delta) => { // rotation += delta * rotationSpeed; @@ -71,11 +71,11 @@ - + { - rotate.set($rotate + 1); + rotate.set($rotate + 5); }} on:pointerleave={() => { size.set(2) diff --git a/src/lib/components/Contact.svelte b/src/lib/components/Contact.svelte index 616fad2..10d39d1 100644 --- a/src/lib/components/Contact.svelte +++ b/src/lib/components/Contact.svelte @@ -17,7 +17,8 @@
    -
    +
    +
    +
    +

    + My Tools +

    +

    + Here are some of the tools I use to create things for the web: +

    +
    + +
    + +
    + {#each all as {title, items}} +
    + +
    +
    {title}
    + + {#each items as item} +
    {item}
    + {/each} +
    + {/each} + +
    +
    +
    +
    +
    diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index fbed894..0a86a98 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -14,6 +14,7 @@ import About from '$lib/components/About.svelte'; import Learning from '$lib/components/Learning.svelte'; import Posts from '$lib/components/Posts.svelte'; + import Tools from '$lib/components/Tools.svelte'; let active: 'home' | 'about' | 'projects' | 'learning' | 'contact' = 'home'; @@ -64,6 +65,8 @@ + + -- knotmirror 0.0.1