CubeCamera Refractions

Description

The CubeCamera object can also be used to create realistic refraction effects.

The extra consideration when using the THREE.CubeCamera for refractions is,

  • Set the CubeRenderTargets texture.mapping to THREE.CubeRefractionMapping
  • Add the refractionRatio property to your Material

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
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')))

        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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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'

const scene: THREE.Scene = new THREE.Scene()
scene.background = new THREE.Color(0x87ceeb)

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

const light1 = new THREE.DirectionalLight();
light1.position.set(5, 10, 5)
light1.castShadow = true
light1.shadow.bias = -0.0002
light1.shadow.mapSize.height = 1024
light1.shadow.mapSize.width = 1024
light1.shadow.camera.left = -10
light1.shadow.camera.right = 10
light1.shadow.camera.top = 10
light1.shadow.camera.bottom = -10
scene.add(light1);

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

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

const orbitControls = new OrbitControls(camera, renderer.domElement)
orbitControls.dampingFactor = .1
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.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)
    render()
}

const cubeRenderTarget1: THREE.WebGLCubeRenderTarget = new THREE.WebGLCubeRenderTarget(128, { format: THREE.RGBFormat, generateMipmaps: true, minFilter: THREE.LinearMipmapLinearFilter });
const cubeRenderTarget2: THREE.WebGLCubeRenderTarget = new THREE.WebGLCubeRenderTarget(128, { format: THREE.RGBFormat, generateMipmaps: true, minFilter: THREE.LinearMipmapLinearFilter });
const cubeRenderTarget3: THREE.WebGLCubeRenderTarget = new THREE.WebGLCubeRenderTarget(128, { format: THREE.RGBFormat, generateMipmaps: true, minFilter: THREE.LinearMipmapLinearFilter });
const cubeCamera1: THREE.CubeCamera = new THREE.CubeCamera(.1, 1000, cubeRenderTarget1);
const cubeCamera2: THREE.CubeCamera = new THREE.CubeCamera(.1, 1000, cubeRenderTarget2);
const cubeCamera3: THREE.CubeCamera = new THREE.CubeCamera(.1, 1000, cubeRenderTarget3);

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

const material1 = new THREE.MeshPhongMaterial({
    shininess: 100,
    color: 0xffffff,
    specular: 0xffffff,
    envMap: cubeRenderTarget1.texture,
    refractionRatio: .5,
    transparent: true,
    side: THREE.BackSide,
    combine: THREE.MixOperation
});
const material2 = new THREE.MeshPhongMaterial({
    shininess: 100,
    color: 0xffffff,
    specular: 0xffffff,
    envMap: cubeRenderTarget2.texture,
    refractionRatio: .5,
    transparent: true,
    side: THREE.BackSide,
    combine: THREE.MixOperation
});
const material3 = new THREE.MeshPhongMaterial({
    shininess: 100,
    color: 0xffffff,
    specular: 0xffffff,
    envMap: cubeRenderTarget3.texture,
    refractionRatio: .5,
    transparent: true,
    side: THREE.BackSide,
    combine: THREE.MixOperation
});

cubeRenderTarget1.texture.mapping = THREE.CubeRefractionMapping
cubeRenderTarget2.texture.mapping = THREE.CubeRefractionMapping
cubeRenderTarget3.texture.mapping = THREE.CubeRefractionMapping

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

const ball2 = new THREE.Mesh(new THREE.SphereGeometry(1, 32, 32), material2);
ball2.position.set(3.1, 1.1, 0);
ball2.castShadow = true;
ball2.receiveShadow = true;
ball2.add(cubeCamera2);
pivot2.add(ball2);

const ball3 = new THREE.Mesh(new THREE.SphereGeometry(1, 32, 32), material3);
ball3.position.set(5.2, 1.1, 0);
ball3.castShadow = true;
ball3.receiveShadow = true;
ball3.add(cubeCamera3);
pivot3.add(ball3);

const data = { refractionRatio: 0 }

const gui = new GUI()
const refractionFolder = gui.addFolder('Refraction')
refractionFolder.add(data, 'refractionRatio', 0, 1, .01).onChange((v:number) => {
    material1.refractionRatio = v
    material2.refractionRatio = v
    material3.refractionRatio = v
})
refractionFolder.open()

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

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

var animate = function () {
    requestAnimationFrame(animate)

    const delta = clock.getDelta();
    ball1.rotateY(-0.2 * delta);
    pivot1.rotateY(0.2 * delta);
    ball2.rotateY(-0.3 * delta);
    pivot2.rotateY(0.3 * delta);
    ball3.rotateY(-0.4 * delta);
    pivot3.rotateY(0.4 * delta);

    orbitControls.update()

    render()

    stats.update()
};

function render() {
    ball1.visible = false
    cubeCamera1.update(renderer, scene);
    ball1.visible = true
    ball2.visible = false
    cubeCamera2.update(renderer, scene);
    ball2.visible = true
    ball3.visible = false
    cubeCamera3.update(renderer, scene);
    ball3.visible = true

    renderer.render(scene, camera)
}
animate();