Skip to content

Commit

Permalink
The drops now look like Evan's.
Browse files Browse the repository at this point in the history
  • Loading branch information
shanecelis committed Dec 26, 2018
1 parent af0a648 commit 40c4ac4
Show file tree
Hide file tree
Showing 8 changed files with 214 additions and 76 deletions.
11 changes: 9 additions & 2 deletions Materials/CustomRenderTexture_WaterIntegrate.mat
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,16 @@ Material:
disabledShaderPasses: []
m_SavedProperties:
serializedVersion: 3
m_TexEnvs: []
m_TexEnvs:
- _Tex:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Floats:
- _dampingConstant: 0.1
- _gridSpacing: 1
- _waveSpeed: 1
m_Colors: []
- radius: 1
- strength: 0.03
m_Colors:
- center: {r: 0, g: 0, b: 0, a: 0}
2 changes: 1 addition & 1 deletion Materials/Unlit_Water Above.mat
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ Material:
- poolHeight: 1
- sphereRadius: 0.25
m_Colors:
- eye: {r: 2.1289804, g: 1.1330482, b: -2.3896728, a: 0}
- eye: {r: 2.53948, g: 1.8776947, b: -3.4477127, a: 0}
- light: {r: 0.3213938, g: 0.76604444, b: -0.5566705, a: 0}
- sphereCenter: {r: 0, g: -0.766, b: 0.423, a: 0}
2 changes: 1 addition & 1 deletion Materials/Unlit_Water Below.mat
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ Material:
- poolHeight: 1
- sphereRadius: 0.25
m_Colors:
- eye: {r: 2.1289804, g: 1.1330482, b: -2.3896728, a: 0}
- eye: {r: 2.53948, g: 1.8776947, b: -3.4477127, a: 0}
- light: {r: 0.3213938, g: 0.76604444, b: -0.5566705, a: 0}
- sphereCenter: {r: 0, g: -0.766, b: 0.423, a: 0}
121 changes: 105 additions & 16 deletions Scenes/cube-of-water.unity
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,6 @@ GameObject:
- component: {fileID: 824587889}
- component: {fileID: 824587891}
- component: {fileID: 824587890}
- component: {fileID: 824587892}
m_Layer: 5
m_Name: RawImage
m_TagString: Untagged
Expand Down Expand Up @@ -413,7 +412,7 @@ MonoBehaviour:
m_Script: {fileID: -98529514, guid: f70555f144d8491a825f0804e09c671c, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Material: {fileID: 10302, guid: 0000000000000000f000000000000000, type: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_OnCullStateChanged:
Expand All @@ -435,18 +434,6 @@ CanvasRenderer:
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 824587888}
m_CullTransparentMesh: 0
--- !u!114 &824587892
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 824587888}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 98362f4f60da34bf5bb06ff669a7349f, type: 3}
m_Name:
m_EditorClassIdentifier:
texture: {fileID: 8600000, guid: 9ac4f57ecbd684ba582087e52f76ccd5, type: 2}
--- !u!43 &983403497
Mesh:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -1192,6 +1179,108 @@ Transform:
m_Father: {fileID: 0}
m_RootOrder: 8
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1413870187
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1413870192}
- component: {fileID: 1413870191}
- component: {fileID: 1413870190}
- component: {fileID: 1413870189}
- component: {fileID: 1413870188}
m_Layer: 0
m_Name: Click Plane
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &1413870188
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1413870187}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 98362f4f60da34bf5bb06ff669a7349f, type: 3}
m_Name:
m_EditorClassIdentifier:
texture: {fileID: 8600000, guid: 9ac4f57ecbd684ba582087e52f76ccd5, type: 2}
dropRadius: 0.1
pause: 0
--- !u!64 &1413870189
MeshCollider:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1413870187}
m_Material: {fileID: 0}
m_IsTrigger: 0
m_Enabled: 1
serializedVersion: 3
m_Convex: 0
m_CookingOptions: 14
m_SkinWidth: 0.01
m_Mesh: {fileID: 10209, guid: 0000000000000000e000000000000000, type: 0}
--- !u!23 &1413870190
MeshRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1413870187}
m_Enabled: 0
m_CastShadows: 1
m_ReceiveShadows: 1
m_DynamicOccludee: 1
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_RenderingLayerMask: 4294967295
m_Materials:
- {fileID: 10303, guid: 0000000000000000f000000000000000, type: 0}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0
m_StaticBatchRoot: {fileID: 0}
m_ProbeAnchor: {fileID: 0}
m_LightProbeVolumeOverride: {fileID: 0}
m_ScaleInLightmap: 1
m_PreserveUVs: 0
m_IgnoreNormalsForChartDetection: 0
m_ImportantGI: 0
m_StitchLightmapSeams: 0
m_SelectedEditorRenderState: 3
m_MinimumChartSize: 4
m_AutoUVMaxDistance: 0.5
m_AutoUVMaxAngle: 89
m_LightmapParameters: {fileID: 0}
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
--- !u!33 &1413870191
MeshFilter:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1413870187}
m_Mesh: {fileID: 10209, guid: 0000000000000000e000000000000000, type: 0}
--- !u!4 &1413870192
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1413870187}
m_LocalRotation: {x: 0, y: 1, z: 0, w: 0}
m_LocalPosition: {x: -0.0068359375, y: 0.024, z: -0.036621094}
m_LocalScale: {x: 0.2, y: 0.2, z: 0.2}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 13
m_LocalEulerAnglesHint: {x: 0, y: 180, z: 0}
--- !u!1 &1633307278
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -1516,8 +1605,8 @@ Transform:
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1835825394}
m_LocalRotation: {x: 0.15035664, y: -0.35120445, z: 0.05671942, w: 0.9224051}
m_LocalPosition: {x: 2.1289804, y: 1.1330482, z: -2.3896728}
m_LocalRotation: {x: 0.23217313, y: -0.30542606, z: 0.07633219, w: 0.92031735}
m_LocalPosition: {x: 2.53948, y: 1.8776947, z: -3.4477127}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
Expand Down
97 changes: 60 additions & 37 deletions Scripts/WaterSimulation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,40 @@
public class WaterSimulation : MonoBehaviour, IPointerClickHandler, IDragHandler {
public CustomRenderTexture texture;
private CustomRenderTextureUpdateZone[] zones = null;
public float dropRadius = 1f; // uv units [0, 1]
public bool pause = false;

private Collider collider;
private CustomRenderTextureUpdateZone defaultZone, normalZone, clickZone;
void Start() {
texture.Initialize();
collider = GetComponent<Collider>();

defaultZone = new CustomRenderTextureUpdateZone();
defaultZone.needSwap = true;
defaultZone.passIndex = 0;
defaultZone.rotation = 0f;
defaultZone.updateZoneCenter = new Vector2(0.5f, 0.5f);
defaultZone.updateZoneSize = new Vector2(1f, 1f);

normalZone = new CustomRenderTextureUpdateZone();
normalZone.needSwap = true;
normalZone.passIndex = 2;
normalZone.rotation = 0f;
normalZone.updateZoneCenter = new Vector2(0.5f, 0.5f);
normalZone.updateZoneSize = new Vector2(1f, 1f);

clickZone = new CustomRenderTextureUpdateZone();
clickZone.needSwap = true;
clickZone.passIndex = 1; // drop
clickZone.rotation = 0f;
// clickZone.updateZoneCenter = uv;
// clickZone.updateZoneCenter = new Vector2(0.5f, 0.5f);
clickZone.updateZoneSize = new Vector2(dropRadius, dropRadius);
// clickZone.updateZoneSize = new Vector2(1f, 1f);

texture.ClearUpdateZones();
texture.SetUpdateZones(new CustomRenderTextureUpdateZone[] { defaultZone, defaultZone, normalZone });
}

public void OnDrag(PointerEventData ped) {
Expand Down Expand Up @@ -39,54 +68,48 @@ void AddWave(PointerEventData ped) {
}

void AddWave(Vector2 uv, int passIndex) {

var defaultZone = new CustomRenderTextureUpdateZone();
defaultZone.needSwap = true;
defaultZone.passIndex = 0; // 波動方程式のシミュレーションのパス
defaultZone.rotation = 0f;
defaultZone.updateZoneCenter = new Vector2(0.5f, 0.5f);
defaultZone.updateZoneSize = new Vector2(1f, 1f);

var clickZone = new CustomRenderTextureUpdateZone();
clickZone.needSwap = true;
clickZone.passIndex = 1;
clickZone.rotation = 0f;
// clickZone.updateZoneCenter = uv;
clickZone.updateZoneCenter = new Vector2(uv.x, 1 - uv.y);
clickZone.updateZoneSize = new Vector2(0.1f, 0.1f);
// clickZone.updateZoneCenter = new Vector2(0.5f, 0.5f);
// clickZone.updateZoneSize = new Vector2(1f, 1f);

zones = new CustomRenderTextureUpdateZone[] { defaultZone, clickZone };

if (pause) {
zones = new CustomRenderTextureUpdateZone[] { clickZone, normalZone };
} else {
zones = new CustomRenderTextureUpdateZone[] { defaultZone, defaultZone, clickZone, normalZone };
}
// texture.Update(1);
}

void Update() {
// UpdateZones();
if (Input.GetKeyDown(KeyCode.Space))
pause = !pause;
UpdateZones();
if (zones != null) {
texture.SetUpdateZones(zones);
zones = null;
} else {
texture.ClearUpdateZones();
// texture.ClearUpdateZones();
texture.SetUpdateZones(new CustomRenderTextureUpdateZone[] { defaultZone, defaultZone, normalZone });
}
texture.Update(1);
// if (! pause
// || Input.GetKeyDown(KeyCode.N))
texture.Update(1);
}

// void UpdateZones() {
// if (collider == null) return;
// bool leftClick = Input.GetMouseButton(0);
// bool rightClick = Input.GetMouseButton(1);
// if (!leftClick && !rightClick) return;

// RaycastHit hit;
// var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// // if (Physics.Raycast(ray, out hit)
// // && hit.transform == transform) {

// if (collider.Raycast(ray, out hit, 100f)) {
// Debug.Log("Clicked uv " + hit.textureCoord2);
// AddWave(hit.textureCoord2, leftClick ? 2 : 3);
// }
// }
void UpdateZones() {
if (collider == null) return;
bool leftClick = Input.GetMouseButtonDown(0);
bool rightClick = Input.GetMouseButtonDown(1);
// bool leftClick = Input.GetMouseButton(0);
// bool rightClick = Input.GetMouseButton(1);
if (!leftClick && !rightClick) return;

RaycastHit hit;
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// if (Physics.Raycast(ray, out hit)
// && hit.transform == transform) {

if (collider.Raycast(ray, out hit, 100f)) {
Debug.Log("Clicked uv " + hit.textureCoord2);
AddWave(hit.textureCoord2, leftClick ? 2 : 3);
}
}
}
2 changes: 2 additions & 0 deletions Shaders/Water.shader
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ Shader "Unlit/Water"
float3 refractedColor = getSurfaceRayColor(i.position, refractedRay, float3(1, 1, 1)) * float3(0.8, 1.0, 1.1);

fixed4 col = float4(lerp(reflectedColor, refractedColor, (1.0 - fresnel) * length(refractedRay)), 1.0);
// fixed4 col = float4(reflectedColor, 1);
// fixed4 col = float4(refractedColor, 1);
#else
/* above water */
float3 reflectedRay = reflect(incomingRay, normal);
Expand Down
30 changes: 16 additions & 14 deletions Shaders/WaterIntegrate.shader
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ Shader "CustomRenderTexture/WaterIntegrate"
fixed4 frag_integrate(v2f_customrendertexture IN) : COLOR
{
float2 delta = float2(1 / _CustomRenderTextureWidth, 1 / _CustomRenderTextureHeight);
float2 coord = float2(IN.localTexcoord.xy);
float2 coord = IN.globalTexcoord.xy;

/* get vertex info */
float4 info = tex2D(_SelfTexture2D, coord);

/* calculate average neighbor height */
float2 dx = float2(delta.x, 0.0);
float2 dy = float2(0.0, delta.y);
// float2 dx = float2(delta.x, 0.0);
// float2 dy = float2(0.0, delta.y);
float2 dx = ddx(coord);
float2 dy = ddy(coord);
float average = (
tex2D(_SelfTexture2D, coord - dx).r +
tex2D(_SelfTexture2D, coord - dy).r +
Expand All @@ -52,12 +54,11 @@ Shader "CustomRenderTexture/WaterIntegrate"
/* move the vertex along the velocity */
info.r += info.g;

/* Let's just update the normal while we're at it. */

/* update the normal */
float3 dx2 = float3(delta.x, tex2D(_SelfTexture2D, float2(coord.x + delta.x, coord.y)).r - info.r, 0.0);
float3 dy2 = float3(0.0, tex2D(_SelfTexture2D, float2(coord.x, coord.y + delta.y)).r - info.r, delta.y);
info.ba = normalize(cross(dy2, dx2)).xz;
// /* Let's just update the normal while we're at it. */
// /* update the normal */
// float3 dx2 = float3(delta.x, tex2D(_SelfTexture2D, float2(coord.x + delta.x, coord.y)).r - info.r, 0.0);
// float3 dy2 = float3(0.0, tex2D(_SelfTexture2D, float2(coord.x, coord.y + delta.y)).r - info.r, delta.y);
// info.ba = normalize(cross(dy2, dx2)).xz;
fixed4 col = info;
// col.a = 1;
return col;
Expand All @@ -80,14 +81,15 @@ Shader "CustomRenderTexture/WaterIntegrate"

float4 frag_drop(v2f_customrendertexture IN) : COLOR
{
// float4 coord = float4(IN.localTexcoord.xy, 0, 0);
float2 coord = float2(IN.globalTexcoord.xy);
float2 coord = IN.localTexcoord.xy;
// float2 coord = float2(IN.globalTexcoord.xy);
/* get vertex info */
float4 info = tex2D(_SelfTexture2D, coord);
float2 delta = float2(1 / _CustomRenderTextureWidth, 1 / _CustomRenderTextureHeight);
// float4 info = tex2D(_SelfTexture2D, coord);
float4 info = tex2D(_SelfTexture2D, IN.globalTexcoord.xy);

/* add the drop to the height */
float drop = max(0.0, 1.0 - length(center * 0.5 + 0.5 - coord) / radius);
// float drop = max(0.0, 1.0 - length(center * 0.5 + 0.5 - coord) / radius);
float drop = max(0, 1.0 - length(float2(0.5, 0.5) - coord) / 0.5);
drop = 0.5 - cos(drop * PI) * 0.5;
info.r += drop * strength;

Expand Down
Loading

0 comments on commit 40c4ac4

Please sign in to comment.