Use depth for 3d cursor normal
This commit is contained in:
@@ -2,15 +2,13 @@
|
||||
uniform mat4 VP;
|
||||
uniform mat4 invVP;
|
||||
uniform vec2 mouse;
|
||||
uniform vec2 step;
|
||||
uniform vec2 texStep;
|
||||
uniform float radius;
|
||||
uniform sampler2D gbufferD;
|
||||
uniform sampler2D gbuffer0;
|
||||
in vec4 pos;
|
||||
in vec2 nor;
|
||||
in vec2 tex;
|
||||
out vec2 texCoord;
|
||||
vec2 octahedronWrap(const vec2 v) { return (1.0 - abs(v.yx)) * (vec2(v.x >= 0.0 ? 1.0 : -1.0, v.y >= 0.0 ? 1.0 : -1.0)); }
|
||||
mat3 rotAxis(vec3 axis, float a) {
|
||||
float c = cos(a);
|
||||
vec3 as = axis * sin(a);
|
||||
@@ -18,32 +16,35 @@ mat3 rotAxis(vec3 axis, float a) {
|
||||
mat3 q = mat3(c, -as.z, as.y, as.z, c, -as.x, -as.y, as.x, c);
|
||||
return p * (1.0 - c) + q;
|
||||
}
|
||||
vec3 getNormal(vec2 uv) {
|
||||
vec2 g0 = textureLod(gbuffer0, mouse, 0.0).rg;
|
||||
vec3 n;
|
||||
n.z = 1.0 - abs(g0.x) - abs(g0.y);
|
||||
n.xy = n.z >= 0.0 ? g0.xy : octahedronWrap(g0.xy);
|
||||
return n;
|
||||
vec3 getPos(vec2 uv) {
|
||||
float depth = textureLod(gbufferD, uv, 0.0).r;
|
||||
vec4 wpos = vec4(uv * 2.0 - 1.0, depth * 2.0 - 1.0, 1.0);
|
||||
wpos = invVP * wpos;
|
||||
return wpos.xyz / wpos.w;
|
||||
}
|
||||
vec3 getNormal(vec3 p0, vec2 uv) {
|
||||
vec3 p1 = getPos(uv + vec2(texStep.x * 4, 0));
|
||||
vec3 p2 = getPos(uv + vec2(0, texStep.y * 4));
|
||||
return normalize(cross(p2 - p0, p1 - p0));
|
||||
}
|
||||
void main() {
|
||||
texCoord = tex;
|
||||
float depth = textureLod(gbufferD, mouse, 0.0).r;
|
||||
vec4 wpos = vec4(mouse * 2.0 - 1.0, depth * 2.0 - 1.0, 1.0);
|
||||
wpos = invVP * wpos;
|
||||
wpos.xyz /= wpos.w;
|
||||
vec3 wpos = getPos(mouse);
|
||||
vec2 uv1 = mouse + texStep * 4;
|
||||
vec2 uv2 = mouse - texStep * 4;
|
||||
vec3 wpos1 = getPos(uv1);
|
||||
vec3 wpos2 = getPos(uv2);
|
||||
vec3 n = normalize(
|
||||
getNormal(mouse + vec2(step.x, step.y)) +
|
||||
getNormal(mouse + vec2(-step.x, step.y)) +
|
||||
getNormal(mouse + vec2(-step.x, -step.y)) +
|
||||
getNormal(mouse + vec2(step.x, -step.y)) +
|
||||
getNormal(mouse)
|
||||
getNormal(wpos, mouse) +
|
||||
getNormal(wpos1, uv1) +
|
||||
getNormal(wpos2, uv2)
|
||||
);
|
||||
float ax = acos(dot(vec3(1,0,0), vec3(n.x,0,0)));
|
||||
float az = acos(dot(vec3(0,0,1), vec3(0,0,n.z)));
|
||||
float sy = -sign(n.y);
|
||||
wpos.xyz +=
|
||||
wpos +=
|
||||
rotAxis(vec3(1,0,0), -az * sy + 3.14/2) *
|
||||
rotAxis(vec3(0,0,1), ax + 3.14/2) *
|
||||
(pos.xyz * radius);
|
||||
gl_Position = VP * vec4(wpos.xyz, 1.0);
|
||||
gl_Position = VP * vec4(wpos, 1.0);
|
||||
}
|
||||
|
||||
Binary file not shown.
@@ -1,15 +1,12 @@
|
||||
uniform float4x4 VP;
|
||||
uniform float4x4 invVP;
|
||||
uniform float2 mouse;
|
||||
uniform float2 step;
|
||||
uniform float2 texStep;
|
||||
uniform float radius;
|
||||
Texture2D<float4> texa; // direct3d12 unit align
|
||||
SamplerState _texa_sampler; // direct3d12 unit align
|
||||
Texture2D<float4> gbufferD;
|
||||
SamplerState _gbufferD_sampler;
|
||||
Texture2D<float4> gbuffer0;
|
||||
SamplerState _gbuffer0_sampler;
|
||||
float2 octahedronWrap(float2 v) { return (1.0 - abs(v.yx)) * (float2(v.x >= 0.0 ? 1.0 : -1.0, v.y >= 0.0 ? 1.0 : -1.0)); }
|
||||
float3x3 rotAxis(float3 axis, float a) {
|
||||
float c = cos(a);
|
||||
float3 as = axis * sin(a).xxx;
|
||||
@@ -17,37 +14,40 @@ float3x3 rotAxis(float3 axis, float a) {
|
||||
float3x3 q = float3x3(c, -as.z, as.y, as.z, c, -as.x, -as.y, as.x, c);
|
||||
return p * (1.0 - c) + q;
|
||||
}
|
||||
float3 getNormal(float2 uv) {
|
||||
float2 g0 = gbuffer0.SampleLevel(_gbuffer0_sampler, uv, 0.0).rg;
|
||||
float3 n;
|
||||
n.z = 1.0 - abs(g0.x) - abs(g0.y);
|
||||
n.xy = n.z >= 0.0 ? g0.xy : octahedronWrap(g0.xy);
|
||||
return n;
|
||||
float3 getPos(float2 uv) {
|
||||
float2 uvinv = float2(uv.x, 1.0 - uv.y);
|
||||
float depth = gbufferD.SampleLevel(_gbufferD_sampler, uvinv, 0).r;
|
||||
float4 wpos = float4(uv * 2.0 - 1.0, depth * 2.0 - 1.0, 1.0);
|
||||
wpos = mul(wpos, invVP);
|
||||
return wpos.xyz / wpos.w;
|
||||
}
|
||||
float3 getNormal(float3 p0, float2 uv) {
|
||||
float3 p1 = getPos(uv + float2(texStep.x, 0));
|
||||
float3 p2 = getPos(uv + float2(0, texStep.y));
|
||||
return normalize(cross(p2 - p0, p1 - p0));
|
||||
}
|
||||
struct SPIRV_Cross_Output { float2 texCoord : TEXCOORD0; float4 gl_Position : SV_Position; };
|
||||
SPIRV_Cross_Output main(float2 nor : TEXCOORD0, float4 pos : TEXCOORD1, float2 tex : TEXCOORD2) {
|
||||
SPIRV_Cross_Output stage_output;
|
||||
stage_output.texCoord = tex;
|
||||
float2 mouseinv = float2(mouse.x, 1.0 - mouse.y);
|
||||
float depth = gbufferD.SampleLevel(_gbufferD_sampler, mouseinv, 0).r;
|
||||
float keep = texa.SampleLevel(_texa_sampler, mouseinv, 0).r; // direct3d12 unit align
|
||||
depth += keep * 0.0000001; // direct3d12 unit align
|
||||
float4 wpos = float4(mouse * 2.0 - 1.0, depth * 2.0 - 1.0, 1.0);
|
||||
wpos = mul(wpos, invVP);
|
||||
wpos.xyz /= wpos.w;
|
||||
float3 wpos = getPos(mouse);
|
||||
float2 uv1 = mouse + texStep * 4;
|
||||
float2 uv2 = mouse - texStep * 4;
|
||||
float3 wpos1 = getPos(uv1);
|
||||
float3 wpos2 = getPos(uv2);
|
||||
float keep = texa.SampleLevel(_texa_sampler, mouse, 0).r; // direct3d12 unit align
|
||||
wpos.x += keep * 0.0000001; // direct3d12 unit align;
|
||||
float3 n = normalize(
|
||||
getNormal(mouseinv + float2(step.x, step.y)) +
|
||||
getNormal(mouseinv + float2(-step.x, step.y)) +
|
||||
getNormal(mouseinv + float2(-step.x, -step.y)) +
|
||||
getNormal(mouseinv + float2(step.x, -step.y)) +
|
||||
getNormal(mouseinv)
|
||||
getNormal(wpos, mouse) +
|
||||
getNormal(wpos1, uv1) +
|
||||
getNormal(wpos2, uv2)
|
||||
);
|
||||
float ax = acos(dot(float3(1,0,0), float3(n.x,0,0)));
|
||||
float az = acos(dot(float3(0,0,1), float3(0,0,n.z)));
|
||||
float sy = -sign(n.y);
|
||||
wpos.xyz += mul(mul(pos.xyz * radius.xxx, rotAxis(float3(0,0,1), ax + 3.14/2)),
|
||||
rotAxis(float3(1,0,0), -az * sy + 3.14/2));
|
||||
stage_output.gl_Position = mul(float4(wpos.xyz, 1.0), VP);
|
||||
wpos += mul(mul(pos.xyz * radius.xxx, rotAxis(float3(0,0,1), ax + 3.14/2)),
|
||||
rotAxis(float3(1,0,0), -az * sy + 3.14/2));
|
||||
stage_output.gl_Position = mul(float4(wpos, 1.0), VP);
|
||||
stage_output.gl_Position.z = (stage_output.gl_Position.z + stage_output.gl_Position.w) * 0.5;
|
||||
return stage_output;
|
||||
}
|
||||
|
||||
@@ -39,11 +39,10 @@ class Layers {
|
||||
public static var cursorVP: ConstantLocation;
|
||||
public static var cursorInvVP: ConstantLocation;
|
||||
public static var cursorMouse: ConstantLocation;
|
||||
public static var cursorStep: ConstantLocation;
|
||||
public static var cursorTexStep: ConstantLocation;
|
||||
public static var cursorRadius: ConstantLocation;
|
||||
public static var cursorTex: TextureUnit;
|
||||
public static var cursorGbufferD: TextureUnit;
|
||||
public static var cursorGbuffer0: TextureUnit;
|
||||
|
||||
public static inline var defaultBase = 0.5;
|
||||
public static inline var defaultRough = 0.4;
|
||||
@@ -167,10 +166,9 @@ class Layers {
|
||||
cursorVP = pipeCursor.getConstantLocation("VP");
|
||||
cursorInvVP = pipeCursor.getConstantLocation("invVP");
|
||||
cursorMouse = pipeCursor.getConstantLocation("mouse");
|
||||
cursorStep = pipeCursor.getConstantLocation("step");
|
||||
cursorTexStep = pipeCursor.getConstantLocation("texStep");
|
||||
cursorRadius = pipeCursor.getConstantLocation("radius");
|
||||
cursorGbufferD = pipeCursor.getTextureUnit("gbufferD");
|
||||
cursorGbuffer0 = pipeCursor.getTextureUnit("gbuffer0");
|
||||
cursorTex = pipeCursor.getTextureUnit("tex");
|
||||
}
|
||||
|
||||
|
||||
@@ -268,7 +268,6 @@ class RenderPathPaint {
|
||||
g.setTexture(Layers.cursorTex, img);
|
||||
var gbuffer0 = path.renderTargets.get("gbuffer0").image;
|
||||
g.setTextureDepth(Layers.cursorGbufferD, gbuffer0);
|
||||
g.setTexture(Layers.cursorGbuffer0, gbuffer0);
|
||||
var mx = Input.getMouse().viewX / iron.App.w();
|
||||
var my = 1.0 - (Input.getMouse().viewY / iron.App.h());
|
||||
if (UITrait.inst.brushLocked) {
|
||||
@@ -276,7 +275,7 @@ class RenderPathPaint {
|
||||
my = 1.0 - (UITrait.inst.lockStartedY - iron.App.y()) / iron.App.h();
|
||||
}
|
||||
g.setFloat2(Layers.cursorMouse, mx, my);
|
||||
g.setFloat2(Layers.cursorStep, 2 / gbuffer0.width, 2 / gbuffer0.height);
|
||||
g.setFloat2(Layers.cursorTexStep, 1 / gbuffer0.width, 1 / gbuffer0.height);
|
||||
g.setFloat(Layers.cursorRadius, UITrait.inst.brushRadius / 3.4);
|
||||
g.setMatrix(Layers.cursorVP, Scene.active.camera.VP.self);
|
||||
var helpMat = iron.math.Mat4.identity();
|
||||
|
||||
Reference in New Issue
Block a user