Jump to content

Refraction Material


Michael Betke
 Share

Recommended Posts

Is it possible to have a refraction material without any coding? Just a texture and a .mat file?

I would like to do a simple and cheap water or glass surface.

 

Rivers in Crysis work this way and look very good. I hope it's possible with LE too.

Doing a waterplane is not a good option because reflections drop my frames to much and I cant control the bump values of the water.

Pure3d Visualizations Germany - digital essences

AAA 3D Model Shop specialized on nature and environments

Link to comment
Share on other sites

iirc the tutorial is working from a glass "shader" as sample to explain ... :) You could stress the material editor with the wobbleDot3 and alpha value textures, it maybe gives good results but it would not "bend" the view behind the surface. That is youll need to capture and obscure the currentscreen with a normal. Hmmmm, try a [edit] texture insteed the wobbleDot3 e.g. use a posterise filter on a normalmap.

 

[edit] removed a link to external DuDv map.

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

mixed something up for an icelake kind of thing - however, i didnt get the alpha right

 

"refraction2.frag"

#define amplitude 0.125

uniform sampler2D texture0;
uniform sampler2D texture1;
uniform sampler2D texture2;

uniform float AppTime;
//-->uniform float apptime;

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

void main(){
vec3 normal;
vec4 bumpcolor;
bumpcolor = texture2D(texture1,vec2(texcoord.x*4.0,texcoord.y*4.0 - 0.00015 * AppTime));
bumpcolor *= (bumpcolor+texture2D(texture1,vec2(texcoord.x*4.0,texcoord.y*4.0 - 0.00025 * AppTime)) )/2.0;	

normal = normalize(2.0*bumpcolor.xyz - 1.0);
normal = T * normal.x + B * normal.y + N * normal.z;
normal = cross(normal,N);

vec4 color = texture2DProj(texture0, projectSpace + amplitude * vec4(normal.x,normal.y,normal.z,0.0) ); // * gl_Color;
color *= texture2D(texture2,vec2(texcoord.x*4.0,texcoord.y*4.0 - 0.00015 * AppTime) );

gl_FragColor=color;
gl_FragColor.a=color.a;
}

 

material file :

texture0="abstract::glass_diffuse.dds"
texture1="abstract::wobbleDot3.dds"
texture2="abstract::glass_diffuse.dds"

blend=0
depthmask=1
depthtest=1
overlay=0
zsort=0
cullface=1
castshadows=0

shader="abstract::refraction.vert","abstract::refraction2.frag"
shadowshader="abstract::mesh_shadow.vert",""

 

hth and OK to post it here :)

 

[edit] PS:

Iam aware of the fact that i expose the refraction.frag in a incorrect way. :)

because of that i used to add 2x -> "refraction2.frag" :(

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 added a section at the end of this article that shows how to get refraction in the editor. It's very easy:

http://leadwerks.com/werkspace/index.php?/page/resources?record=10

 

For practical use, I would just load a copy of the mesh, parented to the model, instead of creating a sphere mesh. So instead of this:

 

CreateSphere(8,model)

You would just have this:

 

LoadMesh("whatever.gmf",model)

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Awesome. Works very good except one thing: Placing the refraction sphere aboove a waterplane makes the water invisible if you look through the sphere. You see the water caustics and the ground. Looks quite funny.

 

Is there a way with the script to animate (or use the waterplane textures) to get a flowing effect?

Pure3d Visualizations Germany - digital essences

AAA 3D Model Shop specialized on nature and environments

Link to comment
Share on other sites

Multiple layers of transparency aren't supported, because it would require a separate buffer and pass for each object. Now, water might be made in the future to work with refraction, because it is sort of a special case. We'll see.

 

Is there a way with the script to animate (or use the waterplane textures) to get a flowing effect?

Use a shader uniform to scroll the texture. Requires shader coding, but it is not a difficult problem. You could simply offset the texture coordinates by the apptime built-in uniform. Check the list of built-in uniforms in the wiki for the exact name.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

How ended this up ? I aint getting depthbuffers right ;)

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
		object.mesh=CreateSphere(16, object.model)	
		object.mesh:SetScalef(4,4,4)
		object.mesh:Hide()

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

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

		if object.useModel ~= nil then
		object.useModel:Paint(mat)
		else	
		--Apply the material to the mesh
		object.mesh:Paint(mat)
		object.mesh:Show()
		end


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

function object:SetKey(key,value)
	if key=="model" then
		self.useModel = LoadMesh("abstract::"..value, self.model)

		if self.useModel ~= nil then
			self.useModel:SetPosition(self.model:GetPosition())
		end
	else
		return self.super:SetKey(key,value)
	end
	return 1
end

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

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

 

Most likely a logic error ;), could one of you please take a look.

 

thx

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

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...