Jump to content

Help speed up /debug glass_refraction GLSL


flachdrache
 Share

Recommended Posts

Hi, i "wrote" some glass shader and it should be usable in the sandboxEd too.

It is based on the "Refraction in the Editor" addon from the refraction tutorialHere.

 

the lua script - effect_glass.lua

require("scripts/class")

local class=CreateClass(...)

function class:InitDialog(grid)
self.super:InitDialog(grid)

group1 = grid:AddGroup("Model")
group1:AddProperty("model", PROPERTY_FILE, "GMF Files (*.gmf):gmf", "Model File", "Model Files")
group1:Expand(1)
end

function class:CreateObject(model)
local object=self.super:CreateObject(model)
object.model = model

--Use the invisible material so the model is not visible, but can still be picked
object.model:Paint(LoadMaterial("abstract::invisible.mat"),1)

function object:Render()
	if fw~=nil then
		--Switch to the transparency world
		SetWorld(fw.transparency.world)

		--Create a sphere mesh parented to the model and scale it
		self.mesh=CreateSphere(16, object.model)	
		self.mesh:SetScalef(4,4,4)
		self.mesh:Hide()

		--Load the refraction material
		local mat=LoadMaterial("abstract::glass_refraction_glass.mat")

		--Get the material shader and set the refraction strength uniform
		local shader=GetMaterialShader(mat)
		SetShaderFloat(shader,"refractionstrength",0.05)

		if self.useModel ~= nil then
			object.useModel:Paint(mat)
			object.useModel:Show()
		else
			self.mesh:Paint(mat)
			self.mesh:Show()
		end

		--Switch back to the main world
		SetWorld(fw.main.world)
	end
end

-- performance ?!
function object:Update()
	if fw~=nil then
		if self.useModel ~= nil then
			self.useModel:SetPosition(object.model:GetPosition(1))
		end
	end
end

function object:SetKey(key,value)
	if key=="model" then
		-- load the model into this world ?!
		if fw~=nil then
			SetWorld(fw.transparency.world)

			self.useModel = LoadMesh("abstract::"..value )			
			self.useModel:Hide()

			--if self.useModel ~= nil then
			--	self.useModel:SetPosition(object.model:GetPosition(1))
			--end

			SetWorld(fw.main.world)
		end
		--
	else
		return self.super:SetKey(key,value)
	end
	return 1
end

function object:Free(model)
	if self.useModel ~= nil then
		self.useModel:Free()
		self.useModel=nil
	end

	self.super:Free()
	--Notify("Inside free")
end

end

 

the material file

color=1,1,1,0.85

texture0="tag::rendercolor"
texture1="abstract::glass2dot3.dds"
texture2="abstract::glass2.dds"
texture3="tag::renderdepth"
texture4="abstact::depthColor.dds"

//bumpmap_refraction
//texture0="abstract::glass2.dds"
//texture1="abstract::glass2dot3.dds"
//texture2="tag::rendercolor"
//texture3="tag::renderdepth"

shader="abstract::refraction.vert","abstract::refraction2.frag"
//cullface=0
zsort=1
depthmask=0

 

the "refraction2".frag

#define amplitude 0.1

uniform sampler2D texture0; //tag::rendercolor
uniform sampler2D texture1; //localmap
uniform sampler2D texture2; //diffusemap or aoc
uniform sampler2D texture3; //tag::renderdepth
uniform sampler2D texture4; //gradient

//lowdragon - next 3 added
uniform vec2 buffersize;
uniform vec2 camerarange;
uniform float thickness=0.5;
uniform float AppTime;

varying vec3 T,B,N;
varying vec4 projectSpace;
varying vec2 texcoord;
varying vec4 ModelVertex;

float DepthToZPosition(in float depth) {
return camerarange.x / (camerarange.y - depth * (camerarange.y - camerarange.x)) * camerarange.y;
}

void main(){
//vec3 normal;
//vec4 bumpcolor;
 vec4 bumpcolor = texture2D(texture1,vec2(texcoord.x*4.0,texcoord.y*4.0 + AppTime / 10000.0));
bumpcolor = (bumpcolor+texture2D(texture1,vec2(texcoord.x*4.0+0.5,texcoord.y*4.0 + AppTime / 20000.0)) )/2.0;	
 vec3 normal = normalize(bumpcolor.xyz - 0.5);
normal = T * normal.x + B * normal.y + N * normal.z;
normal = cross(normal,N);

 // dragon - reAdded color2 for AmbientOcclusionMap &
// Depth alpha ( adapted from "Mix in green color for shallow water" from ocean.frag)
float depthfade=1.0;
vec2 screencoord = vec2( gl_FragCoord.x/buffersize.x, gl_FragCoord.y/buffersize.y );

float depth = texture2D( texture3, screencoord ).x;
float linearbackgrounddepth = DepthToZPosition( depth );
float lineardepth = DepthToZPosition( gl_FragCoord.z );	
depthfade = -(lineardepth-linearbackgrounddepth+100.0)/20.0;
 depthfade = abs(depthfade);

 vec4 gradient = texture2D(texture4, vec2(depthfade) ); 
// --

vec4 color = texture2DProj(texture0, projectSpace + amplitude * vec4(normal.x,normal.y,normal.z,0.0) ) * gl_Color + gradient;
//oh the frames ...
//color *= texture2D(texture2,vec2(texcoord.x*4.0+0.5,texcoord.y*4.0 + AppTime / 20000.0) ); 

gl_FragColor=color*vec4(0.35,0.65,0.45,1);
gl_FragColor.a=gl_Color.a;
}

 

However, its horrible slow in the editor.

 

 

Ps :

I might should use a 1D texture but makeDDS does snatch its image (1x256px).

My lua script has a bug with getPosition on useModel (global?!).

 

 

http://www.devmex.com/twmp/texture1Depth.png

 

Suggestion & bugfixes welcome.

AMD 64 X2 Dual 5k - 4GB - XFX GForce9800GT - nv196.21 - WinXP Sp3

zBrush4R2 - Silo2Pro - Unwrap3DPro - Gile - MaPZone2.5

 

adv_banner-april2012_720x150_tex01.png

 

Xxploration FPS in progress ...

Link to comment
Share on other sites

I had today also an idea how to make a glass entity in LE, so that everything seen through it works, like lights, shadows, post-processing effects, etc...

 

My idea was simply to add a placeholder entity to Editor, which is then replaced in code by a OpenGL transparent 3D rectangle.

 

It would be what CryENGINE 3 does: a hybrid deferred/forward renderer.

Ryzen 9 RX 6800M ■ 16GB XF8 Windows 11 ■
Ultra ■ LE 2.53DWS 5.6  Reaper ■ C/C++ C# ■ Fortran 2008 ■ Story ■
■ Homepage: https://canardia.com ■

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...