Examples : Shiny Refraction

Description

This examples demonstrates,

  • Creating a refraction effect with inner and outer shine.
  • Using the EffectComposer to add a Depth of Field (DOF) effect.
  • Dynamically changing the DOF depending on camera and ball distance.
  • Changing MeshPhysicalMaterial properties using the GUI.

Code

./src/server/server.ts

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import express from "express"
import path from "path"
import http from "http"

const port: number = 3000

class App {
    private server: http.Server
    private port: number

    constructor(port: number) {
        this.port = port
        const app = express()
        app.use(express.static(path.join(__dirname, '../client')))
        app.use('/build/three.module.js', express.static(path.join(__dirname, '../../node_modules/three/build/three.module.js')))
        app.use('/jsm/controls/OrbitControls', express.static(path.join(__dirname, '../../node_modules/three/examples/jsm/controls/OrbitControls.js')))
        app.use('/jsm/libs/stats.module', express.static(path.join(__dirname, '../../node_modules/three/examples/jsm/libs/stats.module.js')))
        app.use('/jsm/libs/dat.gui.module', express.static(path.join(__dirname, '../../node_modules/three/examples/jsm/libs/dat.gui.module.js')))
        app.use('/jsm/', express.static(path.join(__dirname, '../../node_modules/three/examples/jsm/')))
        app.use('/jsm/postprocessing/EffectComposer', express.static(path.join(__dirname, '../../node_modules/three/examples/jsm/postprocessing/EffectComposer.js')))
        app.use('/jsm/postprocessing/RenderPass', express.static(path.join(__dirname, '../../node_modules/three/examples/jsm/postprocessing/RenderPass.js')))
        app.use('/jsm/postprocessing/BokehPass', express.static(path.join(__dirname, '../../node_modules/three/examples/jsm/postprocessing/BokehPass.js')))

        this.server = new http.Server(app);
    }

    public Start() {
        this.server.listen(this.port, () => {
            console.log(`Server listening on port ${this.port}.`)
        })
    }
}

new App(port).Start()

./src/client/client.ts

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import * as THREE from '/build/three.module.js'
import { OrbitControls } from '/jsm/controls/OrbitControls'
import Stats from '/jsm/libs/stats.module'
import { GUI } from '/jsm/libs/dat.gui.module'
import { EffectComposer } from '/jsm/postprocessing/EffectComposer';
import { RenderPass } from '/jsm/postprocessing/RenderPass';
import { BokehPass } from '/jsm/postprocessing/BokehPass';

const scene: THREE.Scene = new THREE.Scene()

const envTexture = new THREE.CubeTextureLoader().load(["img/px_25.jpg", "img/nx_25.jpg", "img/py_25.jpg", "img/ny_25.jpg", "img/pz_25.jpg", "img/nz_25.jpg"])
scene.background = envTexture

const ambientLight = new THREE.AmbientLight(0x444444);
scene.add(ambientLight);

const light1 = new THREE.PointLight();
light1.position.set(-6, 10, -6)
light1.castShadow = true
light1.shadow.bias = -0.0002
light1.shadow.mapSize.height = 2048
light1.shadow.mapSize.width = 2048
scene.add(light1);

const camera: THREE.PerspectiveCamera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.01, 100)
camera.position.set(2, 0, 2)

const renderer: THREE.WebGLRenderer = new THREE.WebGLRenderer()
renderer.setSize(window.innerWidth, window.innerHeight)
renderer.shadowMap.enabled = true
document.body.appendChild(renderer.domElement)

const renderPass = new RenderPass(scene, camera);
const bokehPass = new BokehPass(scene, camera, {
    focus: 1.0,
    aperture: 0.0001,
    maxblur: 1.0,
    width: window.innerWidth,
    height: window.innerHeight
});
const composer = new EffectComposer(renderer);
composer.addPass(renderPass);
composer.addPass(bokehPass);

const orbitControls = new OrbitControls(camera, renderer.domElement)
orbitControls.enableDamping = true

const planeGeometry: THREE.PlaneGeometry = new THREE.PlaneGeometry(25, 25)
const texture = new THREE.TextureLoader().load("img/grid.png")
const plane: THREE.Mesh = new THREE.Mesh(planeGeometry, new THREE.MeshPhongMaterial({ map: texture }))
plane.rotateX(-Math.PI / 2)
plane.position.y = -1
plane.receiveShadow = true
scene.add(plane)

window.addEventListener('resize', onWindowResize, false)
function onWindowResize() {
    camera.aspect = window.innerWidth / window.innerHeight
    camera.updateProjectionMatrix()
    renderer.setSize(window.innerWidth, window.innerHeight)
    composer.setSize(window.innerWidth, window.innerHeight);
}

const pivot1 = new THREE.Object3D();
scene.add(pivot1);

const cubeRenderTarget = new THREE.WebGLCubeRenderTarget(2048);
const cubeCamera = new THREE.CubeCamera(0.1, 100, cubeRenderTarget);
scene.add(cubeCamera);

const material1 = new THREE.MeshPhysicalMaterial({
    metalness: 1.0,
    roughness: 0.05,
    refractionRatio: 0.1,
    envMap: cubeRenderTarget.texture,
    transparent: true,
    transmission: 1.0,
    side: THREE.BackSide,
    clearcoat: 0.63,
    clearcoatRoughness: 0.38
});
const material2 = new THREE.MeshPhysicalMaterial({
    metalness: 0,
    roughness: 0.2,
    envMap: envTexture,
    transparent: true,
    transmission: 1.0,
    side: THREE.FrontSide
});

//cubeRenderTarget.texture.mapping = THREE.CubeRefractionMapping;

const ball1 = new THREE.Mesh(new THREE.SphereGeometry(1, 32, 32), material1)
ball1.position.set(1.5, 0, 0)
ball1.castShadow = true
pivot1.add(ball1);

const ball2 = new THREE.Mesh(new THREE.SphereGeometry(1, 32, 32), material2)
ball2.position.set(1.5, 0, 0)
ball2.castShadow = true
pivot1.add(ball2)

const gui = new GUI()

const ball1Folder = gui.addFolder("Ball1")
ball1Folder.add(ball1, "visible").name("Visible")
ball1Folder.add(material1, "opacity", 0, 1.0, 0.01).name("Opacity")
ball1Folder.add(material1, "metalness", 0, 1.0, 0.01).name("Metalness")
ball1Folder.add(material1, "roughness", 0, 1.0, 0.01).name("Roughness")
ball1Folder.add(material1, "transmission", 0, 1.0, 0.01).name("Transmission")
ball1Folder.add(material1, "clearcoat", 0, 1.0, 0.01).name("Clearcoat")
ball1Folder.add(material1, "clearcoatRoughness", 0, 1.0, 0.01).name("ClearcoatRoughness")
ball1Folder.open()

const ball2Folder = gui.addFolder("Ball2")
ball2Folder.add(ball2, "visible").name("Visible")
ball2Folder.add(material2, "opacity", 0, 1.0, 0.01).name("Opacity")
ball2Folder.add(material2, "metalness", 0, 1.0, 0.01).name("Metalness")
ball2Folder.add(material2, "roughness", 0, 1.0, 0.01).name("Roughness")
ball2Folder.add(material2, "transmission", 0, 1.0, 0.01).name("Transmission")
ball2Folder.open()

const stats = Stats()
document.body.appendChild(stats.dom)

const clock: THREE.Clock = new THREE.Clock()

var animate = function () {
    requestAnimationFrame(animate)

    orbitControls.update()

    const ball1WorldPosition = new THREE.Vector3()
    ball1.getWorldPosition(ball1WorldPosition);
    (bokehPass.uniforms as any).focus.value = camera.position.distanceTo(ball1WorldPosition)

    const delta = clock.getDelta()
    ball1.rotateY(-0.2 * delta)
    ball2.rotateY(-0.2 * delta)
    pivot1.rotateY(0.2 * delta)

    render()

    stats.update()
};

function render() {
    cubeCamera.position.copy(camera.position)
    cubeCamera.update(renderer, scene);
    //renderer.render(scene, camera)
    composer.render(0.1);

}
animate();