-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathrefractionTexture.ts
87 lines (73 loc) · 3.41 KB
/
refractionTexture.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
import type { Scene } from "../../scene";
import { Plane } from "../../Maths/math.plane";
import { RenderTargetTexture } from "../../Materials/Textures/renderTargetTexture";
/**
* Creates a refraction texture used by refraction channel of the standard material.
* It is like a mirror but to see through a material.
* @see https://doc.babylonjs.com/features/featuresDeepDive/materials/using/reflectionTexture#refractiontexture
*/
export class RefractionTexture extends RenderTargetTexture {
/**
* Define the reflection plane we want to use. The refractionPlane is usually set to the constructed refractor.
* It is possible to directly set the refractionPlane by directly using a Plane(a, b, c, d) where a, b and c give the plane normal vector (a, b, c) and d is a scalar displacement from the refractionPlane to the origin. However in all but the very simplest of situations it is more straight forward to set it to the refractor as stated in the doc.
* @see https://doc.babylonjs.com/features/featuresDeepDive/materials/using/reflectionTexture#refraction
*/
public refractionPlane = new Plane(0, 1, 0, 1);
/**
* Define how deep under the surface we should see.
*/
public depth = 2.0;
/**
* Creates a refraction texture used by refraction channel of the standard material.
* It is like a mirror but to see through a material.
* @see https://doc.babylonjs.com/features/featuresDeepDive/materials/using/reflectionTexture#refraction
* @param name Define the texture name
* @param size Define the size of the underlying texture
* @param scene Define the scene the refraction belongs to
* @param generateMipMaps Define if we need to generate mips level for the refraction
*/
constructor(name: string, size: number, scene?: Scene, generateMipMaps?: boolean) {
super(name, size, scene, generateMipMaps, true);
this.onBeforeRenderObservable.add(() => {
this.getScene()!.clipPlane = this.refractionPlane;
});
this.onAfterRenderObservable.add(() => {
this.getScene()!.clipPlane = null;
});
}
/**
* Clone the refraction texture.
* @returns the cloned texture
*/
public override clone(): RefractionTexture {
const scene = this.getScene();
if (!scene) {
return this;
}
const textureSize = this.getSize();
const newTexture = new RefractionTexture(this.name, textureSize.width, scene, this._generateMipMaps);
// Base texture
newTexture.hasAlpha = this.hasAlpha;
newTexture.level = this.level;
// Refraction Texture
newTexture.refractionPlane = this.refractionPlane.clone();
if (this.renderList) {
newTexture.renderList = this.renderList.slice(0);
}
newTexture.depth = this.depth;
return newTexture;
}
/**
* Serialize the texture to a JSON representation you could use in Parse later on
* @returns the serialized JSON representation
*/
public override serialize(): any {
if (!this.name) {
return null;
}
const serializationObject = super.serialize();
serializationObject.mirrorPlane = this.refractionPlane.asArray();
serializationObject.depth = this.depth;
return serializationObject;
}
}