• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
zonozz

About water refraction

4 posts in this topic

Hi everyone !

 

I'm doing 2D water simulation, and there is a problem in my program.

 

Figure :

[attachment=15216:Problem.jpg]

 

Just as you see, I render the clip plane below to texture and over it to another texture, eventually to combine 2 texture to water plane.

 

Question :

 

1. I dont understand why my friends told me I should calculate reflection & refraction vector ? Why I need these vectors ? ( I already finish render the reflection & refraction effect ? ).

 

2. As you see the picture above, there is a obvious visual error in water plane, when I turn the camera, the goddess body teared ! How does it occurred ? How to fix it ?

 

Thanks.

0

Share this post


Link to post
Share on other sites

It looks like your refraction vector is wrong, the reflection one looks correct. How are you calculating it? The statue should appear elongated and bent at the bottom, not broken (same for the pool edge refractions in the background). Basically, post some code, because without code we cannot do much more than say "it looks wrong".

1

Share this post


Link to post
Share on other sites

It looks like your refraction vector is wrong, the reflection one looks correct. How are you calculating it? The statue should appear elongated and bent at the bottom, not broken (same for the pool edge refractions in the background). Basically, post some code, because without code we cannot do much more than say "it looks wrong".

 

OK?But I'll post HLSL's code, but I dont know Why I need reflection vector and refraction vector ? I already render the final effect (Combine reflection & refraction).

 

Here is my code in Vertex Shader & Pixel Shader :

/*
 * File name - Water.vs
 * Author - CuiXue
 * Updated date - 2013.4.21
 * Desc - 
 *
 */

 cbuffer MatrixBuffer
 {
	matrix world;
	matrix view;
	matrix proj;
 };

 cbuffer ReflectionBuffer
 {
	matrix reflectionMatrix;
 };

 cbuffer CameraBuffer
 {
	float3 cameraPosition;
	float padding;
 };

 struct VS_INPUT
 {
	float4 position : POSITION;
	float2 tex		: TEXCOORD0;
	float3 normal	: NORMAL;
	float3 tangent	: TANGENT;
	float3 binormal	: BINORMAL;
 };

 struct PS_INPUT
 {
	float4 position : SV_POSITION;
	float2 tex1		: TEXCOORD0;
	float2 tex2		: TEXCOORD4;
	float4 reflectionPos : TEXCOORD1;
	float4 refractionPos : TEXCOORD2;
	float3 viewDirection : TEXCOORD3;
	float3 normal	: NORMAL;
	float3 tangent		 : TANGENT;
	float3 binormal		 : BINORMAL;
 };

 PS_INPUT WaterVS(VS_INPUT input)
 {
	PS_INPUT output = (PS_INPUT)0;

	float4 worldPos;

	matrix reflectionProjWorld;
	matrix viewProjWorld;

	input.position.w = 1.0f;

	output.position = mul(input.position, world);
	output.position = mul(output.position, view);
	output.position = mul(output.position, proj);

	output.normal = mul(input.normal, (float3x3)world);
	output.normal = normalize(output.normal);

	output.tangent = mul(input.tangent, (float3x3)world);
	output.tangent = normalize(output.tangent);

	output.binormal = mul(input.binormal, (float3x3)world);
	output.binormal = normalize(output.binormal);

	reflectionProjWorld = mul(reflectionMatrix, proj);
	reflectionProjWorld = mul(world, reflectionProjWorld);
	output.reflectionPos = mul(input.position, reflectionProjWorld);

	viewProjWorld = mul(view, proj);
	viewProjWorld = mul(world, viewProjWorld);
	output.refractionPos = mul(input.position, viewProjWorld);

	worldPos = mul(input.position, world);
	output.viewDirection = cameraPosition.xyz - worldPos.xyz;
	output.viewDirection = normalize(output.viewDirection);

	output.tex1 = input.tex / 1.0f;
	output.tex2 = input.tex / 0.5f;

	return output;
 }

 

 

 

Pixel Shader :

/*
 * File name - Water.ps
 * Author - CuiXue
 * Updated date - 2013.4.21
 * Desc - 
 *
 */
 SamplerState samplerState;
 Texture2D reflectionTex;
 Texture2D refractionTex;
 Texture2D normalTex;

 cbuffer WaterBuffer
 {
	float reflection_refraction_scale;
	float water_translation;
	float2 padding;
 };

 cbuffer LightBuffer
 {
	float4 ambientColor;
	float4 diffuseColor;
	float3 direction;
	float specularPower;
	float4 specularColor;
 };

 struct PS_INPUT
 {
	float4 position : SV_POSITION;
	float2 tex1		: TEXCOORD0;
	float2 tex2		: TEXCOORD4;
	float4 reflectionPos : TEXCOORD1;
	float4 refractionPos : TEXCOORD2;
	float3 viewDirection : TEXCOORD3;
	float3 normal		 : NORMAL;
	float3 tangent		 : TANGENT;
	float3 binormal		 : BINORMAL;
 };

 float4 WaterPS(PS_INPUT input) : SV_TARGET
 {
	// Water params
	float fresnel;
	float specular;
	float r;
	float2 reflection_sample_coord;
	float2 refraction_sample_coord;
	float3 normal1;
	float3 normal2;
	float3 normal;
	float3 reflection;
	float3 heightView;
	float4 reflectionColor;
	float4 refractionColor;
	float4 refl_refr_lerpColor;
	float4 normalMap1;
	float4 normalMap2;
	float4 outputColor;

	float4 water_refl_color = float4( 0.7f, 0.9f, 0.7f, 1.0 );
	float4 water_refr_color = float4( 0.6f, 1.0f, 0.6f, 1.0 );

	input.tex1.y = input.tex1.y - water_translation;
	input.tex2.x = input.tex2.x + water_translation;

	heightView.x = input.viewDirection.y;
	heightView.y = input.viewDirection.y;
	heightView.z = input.viewDirection.y;

	reflection_sample_coord.x =  input.reflectionPos.x / input.reflectionPos.w / 2.0f + 0.5f;
	reflection_sample_coord.y = -input.reflectionPos.y / input.reflectionPos.w / 2.0f + 0.5f;

	refraction_sample_coord.x =  input.refractionPos.x / input.refractionPos.w / 2.0f + 0.5f;
	refraction_sample_coord.y = -input.refractionPos.y / input.refractionPos.w / 2.0f + 0.5f;
	
	// Sample normal map
	normalMap1 = normalTex.Sample(samplerState, input.tex1);
	normalMap2 = normalTex.Sample(samplerState, input.tex2);
	normal1 = (normalMap1.rgb * 2.0f) - 1.0f;
	normal2 = (normalMap2.rgb * 2.0f) - 1.0f;
	normal = normalize(normal1 + normal2);
	
	reflection_sample_coord = reflection_sample_coord + (normal.xy * reflection_refraction_scale);
	refraction_sample_coord = refraction_sample_coord + (normal.xy * reflection_refraction_scale);

	reflectionColor = reflectionTex.Sample(samplerState, reflection_sample_coord);
	refractionColor = refractionTex.Sample(samplerState, refraction_sample_coord);

	reflectionColor *= water_refl_color;
	refractionColor *= water_refr_color;

	// Calculate fresnel 
	r = pow((1.0 - 1.0 / 1.3), 2) / pow((1.0 + 1.0/1.3), 2);
	// r = pow((1.3f - 1.0f), 2) / pow((1.3f + 1.0f), 2);

	fresnel = max(0.0f, min(1.0f, r + (1.0f - r) * pow(1.0f - dot(normal, heightView), 5.0)));

	refl_refr_lerpColor = lerp(reflectionColor, refractionColor, fresnel);

	reflection = -reflect(normalize(direction), normal);

	specular = dot(normalize(reflection), normalize(input.viewDirection));

	if( specular > 0.0f )
	{
		specular = pow(specular, specularPower);

		refl_refr_lerpColor = saturate(refl_refr_lerpColor + specular * specularColor);
	}

	outputColor = refl_refr_lerpColor;

	return outputColor;
 }

 

 

I just sample the reflection texture & refraction texture. and combine these 2 pictures to water plane.

0

Share this post


Link to post
Share on other sites

Can anyone help me please ?

 

1. Why I need to calculate refraction & reflection vector ? Use reflection & refraction render texture for water plane will contribute some visual error ?

 

2. I debugged today, and another problem here, the whole water plane rocked when I turn my camera, I always feel that the surface of the water there is a delay. ( It means the water plane will dislocated with the bath. )

0

Share this post


Link to post
Share on other sites

Why are you using the y-component of the normal? Shouldn't it be the x and z component?

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0