Files
armorpaint/Sources/arm/node/MakeTexcoord.hx
T

90 lines
3.7 KiB
Haxe
Raw Normal View History

2019-12-06 11:55:43 +01:00
package arm.node;
2020-03-18 20:26:28 +01:00
import arm.ui.UISidebar;
2020-08-14 11:12:39 +02:00
import arm.shader.NodeShader;
2020-03-18 20:26:28 +01:00
import arm.Enums;
2019-12-06 11:55:43 +01:00
class MakeTexcoord {
2020-08-14 11:12:39 +02:00
public static function run(vert: NodeShader, frag: NodeShader) {
2019-12-06 11:55:43 +01:00
2020-09-02 13:38:20 +02:00
var fillLayer = Context.layer.fill_layer != null;
2020-06-09 18:28:46 +02:00
var uvType = fillLayer ? Context.layer.uvType : Context.brushPaint;
2019-12-06 11:55:43 +01:00
var decal = Context.tool == ToolDecal || Context.tool == ToolText;
// TexCoords - project
2019-12-08 16:59:08 +01:00
if (uvType == UVProject || decal) {
2019-12-06 11:55:43 +01:00
frag.add_uniform('float brushScale', '_brushScale');
frag.write_attrib('vec2 uvsp = sp.xy;');
2020-06-09 18:28:46 +02:00
if (fillLayer) { // Decal layer
2020-12-09 17:26:25 +01:00
frag.write_attrib('if (uvsp.x < 0.0 || uvsp.y < 0.0 || uvsp.x > 1.0 || uvsp.y > 1.0) discard;');
frag.n = true;
2020-12-14 16:20:09 +01:00
frag.add_uniform('vec3 decalLayerNor', '_decalLayerNor');
frag.write('if (abs(dot(n, decalLayerNor) - 1.0) > 0.2) discard;');
2020-12-10 17:22:33 +01:00
2020-12-14 16:20:09 +01:00
frag.wposition = true;
frag.add_uniform('vec3 decalLayerLoc', '_decalLayerLoc');
frag.add_uniform('float decalLayerDim', '_decalLayerDim');
frag.write_attrib('if (abs(dot(decalLayerNor, decalLayerLoc - wposition)) > decalLayerDim) discard;');
2020-06-09 18:28:46 +02:00
}
else if (decal) {
2020-10-24 13:30:23 +02:00
frag.add_uniform('vec4 decalMask', '_decalMask');
2020-10-24 23:36:14 +02:00
frag.write_attrib('vec4 decalMaskLocal = decalMask;'); // TODO: spirv workaround
frag.write_attrib('uvsp -= decalMaskLocal.xy;');
2019-12-06 11:55:43 +01:00
frag.write_attrib('uvsp.x *= aspectRatio;');
2020-10-24 23:36:14 +02:00
frag.write_attrib('uvsp *= 0.21 / (decalMaskLocal.w * 0.9);'); // Decal radius
2020-03-24 14:04:20 +01:00
2020-03-26 15:22:22 +01:00
if (Context.brushDirectional) {
2020-03-24 14:04:20 +01:00
frag.add_uniform('vec3 brushDirection', '_brushDirection');
frag.write_attrib('if (brushDirection.z == 0.0) discard;');
frag.write_attrib('uvsp = vec2(uvsp.x * brushDirection.x - uvsp.y * brushDirection.y, uvsp.x * brushDirection.y + uvsp.y * brushDirection.x);');
}
2020-03-26 15:22:22 +01:00
var angle = Context.brushAngle + Context.brushNodesAngle;
2020-09-02 13:38:20 +02:00
var uvAngle = Context.layer.fill_layer != null ? Context.layer.angle : angle;
2020-03-24 14:04:20 +01:00
if (uvAngle != 0.0) {
2020-03-25 17:04:38 +01:00
frag.add_uniform('vec2 brushAngle', '_brushAngle');
frag.write_attrib('uvsp = vec2(uvsp.x * brushAngle.x - uvsp.y * brushAngle.y, uvsp.x * brushAngle.y + uvsp.y * brushAngle.x);');
2020-03-24 14:04:20 +01:00
}
2019-12-06 11:55:43 +01:00
frag.add_uniform('float brushScaleX', '_brushScaleX');
frag.write_attrib('uvsp.x *= brushScaleX;');
2020-03-24 14:04:20 +01:00
2019-12-06 11:55:43 +01:00
frag.write_attrib('uvsp += vec2(0.5, 0.5);');
2020-03-24 14:04:20 +01:00
2020-03-10 17:21:03 +01:00
frag.write_attrib('if (uvsp.x < 0.0 || uvsp.y < 0.0 || uvsp.x > 1.0 || uvsp.y > 1.0) discard;');
2019-12-06 11:55:43 +01:00
}
else {
frag.write_attrib('uvsp.x *= aspectRatio;');
}
2020-03-10 17:21:03 +01:00
frag.write_attrib('vec2 texCoord = uvsp * brushScale;');
2019-12-06 11:55:43 +01:00
}
2019-12-09 00:28:10 +01:00
else if (uvType == UVMap) { // TexCoords - uvmap
2019-12-06 11:55:43 +01:00
vert.add_uniform('float brushScale', '_brushScale');
vert.add_out('vec2 texCoord');
2020-11-29 21:00:18 +01:00
vert.write('texCoord = tex * brushScale;');
2019-12-06 11:55:43 +01:00
2020-03-26 15:22:22 +01:00
var angle = Context.brushAngle + Context.brushNodesAngle;
2020-09-02 13:38:20 +02:00
var uvAngle = Context.layer.fill_layer != null ? Context.layer.angle : angle;
2020-03-16 22:07:24 +01:00
if (uvAngle > 0.0) {
2020-03-25 17:04:38 +01:00
vert.add_uniform('vec2 brushAngle', '_brushAngle');
vert.write('texCoord = vec2(texCoord.x * brushAngle.x - texCoord.y * brushAngle.y, texCoord.x * brushAngle.y + texCoord.y * brushAngle.x);');
2019-12-06 11:55:43 +01:00
}
}
else { // Triplanar
frag.wposition = true;
frag.n = true;
frag.add_uniform('float brushScale', '_brushScale');
frag.write_attrib('vec3 triWeight = wnormal * wnormal;'); // n * n
frag.write_attrib('float triMax = max(triWeight.x, max(triWeight.y, triWeight.z));');
frag.write_attrib('triWeight = max(triWeight - triMax * 0.75, 0.0);');
frag.write_attrib('vec3 texCoordBlend = triWeight * (1.0 / (triWeight.x + triWeight.y + triWeight.z));');
2020-03-10 17:21:03 +01:00
frag.write_attrib('vec2 texCoord = wposition.yz * brushScale * 0.5;');
frag.write_attrib('vec2 texCoord1 = wposition.xz * brushScale * 0.5;');
frag.write_attrib('vec2 texCoord2 = wposition.xy * brushScale * 0.5;');
2019-12-06 11:55:43 +01:00
}
}
}