-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAutofit.js
203 lines (172 loc) · 7.73 KB
/
Autofit.js
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
const Reactive = require('Reactive');
const Shaders = require('Shaders');
const Diagnostics = require('Diagnostics');
//––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
const uv = Shaders.fragmentStage(Shaders.vertexAttribute({ variableName: 'TEX_COORDS' }));
//––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
/**
* @param {ShaderSignal} uv
* @param {Point2DSignal} scale
* @return {Point2DSignal}
*/
function scaleUV(uv, scale) {
const scaleAxis = (axis, scale) => {
scale = Reactive.div(1, scale);
return axis.mul(scale).sub(scale.sub(1).mul(0.5));
}
const scaledU = scaleAxis(uv.x, scale.x);
const scaledV = scaleAxis(uv.y, scale.y);
return Reactive.pack2(scaledU, scaledV);
}
//––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
/** @enum {string} */
export const ObjectScaleMode = {
BASED_ON_HEIGHT: 'BASED_ON_HEIGHT',
BASED_ON_WIDTH: 'BASED_ON_WIDTH',
BASED_ON_1024: 'BASED_ON_1024',
}
/** @enum {string} */
export const TextureScaleMode = {
FIT: 'FIT',
ENVELOPE: 'ENVELOPE',
STRETCH: 'STRETCH',
Shrink: 'Shrink',
Expand: 'Expand',
Stretch: 'Stretch'
}
/**
* @param {SceneObjectBase} sceneObject
* @param {ObjectScaleMode} scaleMode `AutoFit.ObjectScaleMode`
* @returns {Promise<PointSignal>}
*/
export function getFitObjectScale(sceneObject, scaleMode) {
return sceneObject.getMaterial().then(mat =>
mat.getDiffuse().then(tex => {
switch (scaleMode) {
case ObjectScaleMode.BASED_ON_HEIGHT:
var scale = Reactive.scale(
tex.width.div(tex.height).mul(sceneObject.transform.scaleX.pinLastValue()),
sceneObject.transform.scaleY.pinLastValue(),
sceneObject.transform.scaleZ.pinLastValue()
);
break;
case ObjectScaleMode.BASED_ON_WIDTH:
var scale = Reactive.scale(
sceneObject.transform.scaleX.pinLastValue(),
tex.height.div(tex.width).mul(sceneObject.transform.scaleY.pinLastValue()),
sceneObject.transform.scaleZ.pinLastValue()
);
break;
case ObjectScaleMode.BASED_ON_1024:
var scale = Reactive.scale(
tex.width.div(1024).mul(sceneObject.transform.scaleX.pinLastValue()),
tex.height.div(1024).mul(sceneObject.transform.scaleY.pinLastValue()),
sceneObject.transform.scaleZ.pinLastValue()
);
break;
default:
Diagnostics.log(`Invaild scale mode.`);
break;
}
return scale;
})
)
}
/**
* @param {SceneObjectBase} sceneObject
* @param {ObjectScaleMode} scaleMode `AutoFit.ObjectScaleMode`
* @returns {Promise<void>}
*/
export function fitObject(sceneObject, scaleMode) {
return getFitObjectScale(sceneObject, scaleMode).then(scale => sceneObject.transform.scale = scale);
}
/**
* @param {SceneObjectBase} sceneObject
* @param {TextureScaleMode} scaleMode `AutoFit.TextureScaleMode`
* @returns {Promise<Point2DSignal>} scaledUV
*/
export function getFitTextureUV(sceneObject, scaleMode) {
return sceneObject.getMaterial().then(mat =>
mat.getDiffuse().then(tex => {
const scaleX = sceneObject.transform.scaleX;
const scaleY = sceneObject.transform.scaleY;
const textureRatio = tex.width.div(tex.height);
const scaleWidth = Reactive.div(textureRatio, tex.width);
const scaleHeight = Reactive.div(1, tex.height);
const isShort = scaleX.gt(scaleY.mul(textureRatio));
let width = 1;
let height = 1;
const expand = () => {
const textureScale = Reactive.max(scaleHeight, scaleWidth);
width = isShort.ifThenElse(
tex.width.mul(textureScale).div(textureRatio),
tex.width.mul(textureScale).mul(Reactive.div(scaleY, scaleX))
);
height = isShort.ifThenElse(
tex.height.mul(textureScale).mul(Reactive.div(scaleX, scaleY)).div(textureRatio),
tex.height.mul(textureScale)
);
}
const shrink = () => {
const textureScale = Reactive.min(scaleHeight, scaleWidth);
width = isShort.ifThenElse(
tex.width.mul(textureScale).mul(Reactive.div(scaleY, scaleX)),
tex.width.mul(textureScale).div(textureRatio)
);
height = isShort.ifThenElse(
tex.height.mul(textureScale),
tex.height.mul(textureScale).mul(Reactive.div(scaleX, scaleY)).div(textureRatio)
);
}
switch (scaleMode) {
case TextureScaleMode.FIT:
expand();
Diagnostics.log(`'TextureScaleMode.FIT' is obsolete. Use 'TextureScaleMode.Expand' instead`)
break;
case TextureScaleMode.ENVELOPE:
shrink();
Diagnostics.log(`'TextureScaleMode.Shrink' is obsolete. Use 'TextureScaleMode.Shrink' instead`)
break;
case TextureScaleMode.STRETCH:
width = 1;
height = 1;
Diagnostics.log(`'TextureScaleMode.STRETCH' is obsolete. Use 'TextureScaleMode.Stretch' instead`)
break;
case TextureScaleMode.Expand:
expand();
break;
case TextureScaleMode.Shrink:
shrink();
break;
case TextureScaleMode.Stretch:
width = 1;
height = 1;
break;
default:
Diagnostics.log(`Invaild scale mode.`);
break;
}
const scaledUV = scaleUV(uv, Reactive.pack2(width, height));
return scaledUV;
})
)
}
/**
* @param {SceneObjectBase} sceneObject
* @param {TextureScaleMode} scaleMode `AutoFit.TextureScaleMode`
* @param {Point4DSignal} backgroundColor The background color is filled in the empty space in `ENVELOPE` mode. Default is black.
* @returns {Promise<ShaderSignal>} The scaled texture signal.
*/
export function fitTexture(sceneObject, scaleMode, backgroundColor = Reactive.pack4(0, 0, 0, 1)) {
return getFitTextureUV(sceneObject, scaleMode).then(uv =>
sceneObject.getMaterial().then(mat =>
mat.getDiffuse().then(tex => {
const color = Shaders.composition(tex.signal, uv);
const region = Reactive.mul(Reactive.step(uv, 0), Reactive.step(Reactive.sub(1, uv), 0));
const cropped = Reactive.mix(backgroundColor, color, Reactive.min(region.x, region.y));
mat.setTextureSlot('DIFFUSE', cropped)
return cropped;
})
)
)
}