forked from pmndrs/lamina
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvanilla.ts
165 lines (138 loc) · 4.19 KB
/
vanilla.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
import * as THREE from 'three'
import Abstract from './core/Abstract'
import Depth from './core/Depth'
import Color from './core/Color'
import Noise from './core/Noise'
import Fresnel from './core/Fresnel'
import Gradient from './core/Gradient'
import Matcap from './core/Matcap'
import Texture from './core/Texture'
import Displace from './core/Displace'
import Normal from './core/Normal'
import BlendModesChunk from './chunks/BlendModes'
import NoiseChunk from './chunks/Noise'
import HelpersChunk from './chunks/Helpers'
import { LayerMaterialParameters, SerializedLayer, ShadingType, ShadingTypes } from './types'
import {
ColorRepresentation,
MeshBasicMaterialParameters,
MeshLambertMaterialParameters,
MeshPhongMaterialParameters,
MeshPhysicalMaterialParameters,
MeshStandardMaterialParameters,
MeshToonMaterialParameters,
} from 'three'
import CustomShaderMaterial from 'three-custom-shader-material/vanilla'
type AllMaterialParams =
| MeshPhongMaterialParameters
| MeshPhysicalMaterialParameters
| MeshToonMaterialParameters
| MeshBasicMaterialParameters
| MeshLambertMaterialParameters
| MeshStandardMaterialParameters
class LayerMaterial extends CustomShaderMaterial {
name: string = 'LayerMaterial'
layers: Abstract[] = []
lighting: ShadingType = 'basic'
constructor({ color, alpha, lighting, layers, name, ...props }: LayerMaterialParameters & AllMaterialParams = {}) {
super({
baseMaterial: ShadingTypes[lighting || 'basic'],
...props,
})
const _baseColor = color || 'white'
const _alpha = alpha ?? 1
this.uniforms = {
u_lamina_color: {
value: typeof _baseColor === 'string' ? new THREE.Color(_baseColor).convertSRGBToLinear() : _baseColor,
},
u_lamina_alpha: {
value: _alpha,
},
}
this.layers = layers || this.layers
this.lighting = lighting || this.lighting
this.name = name || this.name
this.refresh()
}
genShaders() {
let vertexVariables = ''
let fragmentVariables = ''
let vertexShader = ''
let fragmentShader = ''
let uniforms: any = {}
this.layers
.filter((l) => l.visible)
.forEach((l) => {
// l.buildShaders(l.constructor)
vertexVariables += l.vertexVariables + '\n'
fragmentVariables += l.fragmentVariables + '\n'
vertexShader += l.vertexShader + '\n'
fragmentShader += l.fragmentShader + '\n'
uniforms = {
...uniforms,
...l.uniforms,
}
})
uniforms = {
...uniforms,
...this.uniforms,
}
return {
uniforms,
vertexShader: `
${HelpersChunk}
${NoiseChunk}
${vertexVariables}
void main() {
vec3 lamina_finalPosition = position;
vec3 lamina_finalNormal = normal;
${vertexShader}
csm_Position = lamina_finalPosition;
csm_Normal = lamina_finalNormal;
}
`,
fragmentShader: `
${HelpersChunk}
${NoiseChunk}
${BlendModesChunk}
${fragmentVariables}
uniform vec3 u_lamina_color;
uniform float u_lamina_alpha;
void main() {
vec4 lamina_finalColor = vec4(u_lamina_color, u_lamina_alpha);
${fragmentShader}
csm_DiffuseColor = lamina_finalColor;
}
`,
}
}
refresh() {
const { uniforms, fragmentShader, vertexShader } = this.genShaders()
super.update({ fragmentShader, vertexShader, uniforms })
}
serialize(): SerializedLayer {
return {
constructor: 'LayerMaterial',
properties: {
color: this.color,
alpha: this.alpha,
name: this.name,
lighting: this.lighting,
},
}
}
set color(v: ColorRepresentation) {
if (this.uniforms?.u_lamina_color?.value)
this.uniforms.u_lamina_color.value = typeof v === 'string' ? new THREE.Color(v).convertSRGBToLinear() : v
}
get color() {
return this.uniforms?.u_lamina_color?.value
}
set alpha(v: number) {
this.uniforms.u_lamina_alpha.value = v
}
get alpha() {
return this.uniforms.u_lamina_alpha.value
}
}
export { LayerMaterial, Abstract, Depth, Color, Noise, Fresnel, Gradient, Matcap, Texture, Displace, Normal }