Skip to content

CubeCamera Refractions

Description

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

<>

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

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

Code

./src/main.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
169
170
171
172
173
174
175
176
177
178
179
import './style.css'
import * as THREE from 'three'
import { OrbitControls } from 'three/addons/controls/OrbitControls.js'
import Stats from 'three/addons/libs/stats.module.js'
import { GUI } from 'three/addons/libs/lil-gui.module.min.js'

const scene = new THREE.Scene()
scene.background = new THREE.CubeTextureLoader().load(['img/px_eso0932a.jpg', 'img/nx_eso0932a.jpg', 'img/py_eso0932a.jpg', 'img/ny_eso0932a.jpg', 'img/pz_eso0932a.jpg', 'img/nz_eso0932a.jpg'])

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

const light1 = new THREE.DirectionalLight(0xffffff, 5)
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 = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.01, 100)
camera.position.set(0, 8, 0)

const renderer = 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.enableDamping = true

const 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', () => {
  camera.aspect = window.innerWidth / window.innerHeight
  camera.updateProjectionMatrix()
  renderer.setSize(window.innerWidth, window.innerHeight)
})

const cubeRenderTarget1: THREE.WebGLCubeRenderTarget = new THREE.WebGLCubeRenderTarget(128, {
  format: THREE.RGBAFormat,
  generateMipmaps: true,
  minFilter: THREE.LinearMipmapLinearFilter,
})
const cubeRenderTarget2: THREE.WebGLCubeRenderTarget = new THREE.WebGLCubeRenderTarget(128, {
  format: THREE.RGBAFormat,
  generateMipmaps: true,
  minFilter: THREE.LinearMipmapLinearFilter,
})
const cubeRenderTarget3: THREE.WebGLCubeRenderTarget = new THREE.WebGLCubeRenderTarget(128, {
  format: THREE.RGBAFormat,
  generateMipmaps: true,
  minFilter: THREE.LinearMipmapLinearFilter,
})
const cubeCamera1: THREE.CubeCamera = new THREE.CubeCamera(0.1, 1000, cubeRenderTarget1)
const cubeCamera2: THREE.CubeCamera = new THREE.CubeCamera(0.1, 1000, cubeRenderTarget2)
const cubeCamera3: THREE.CubeCamera = new THREE.CubeCamera(0.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: 0.5,
  transparent: true,
  side: THREE.BackSide,
  combine: THREE.MixOperation,
})
const material2 = new THREE.MeshPhongMaterial({
  shininess: 100,
  color: 0xffffff,
  specular: 0xffffff,
  envMap: cubeRenderTarget2.texture,
  refractionRatio: 0.5,
  transparent: true,
  side: THREE.BackSide,
  combine: THREE.MixOperation,
})
const material3 = new THREE.MeshPhongMaterial({
  shininess: 100,
  color: 0xffffff,
  specular: 0xffffff,
  envMap: cubeRenderTarget3.texture,
  refractionRatio: 0.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, 0.01).onChange((v: number) => {
  material1.refractionRatio = v
  material2.refractionRatio = v
  material3.refractionRatio = v
})
refractionFolder.open()

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

const clock = new THREE.Clock()

function animate() {
  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()

Comments