Skip to content

Commit

Permalink
Caustics was backwards on Z-axis. Working on cube now.
Browse files Browse the repository at this point in the history
  • Loading branch information
shanecelis committed Dec 28, 2018
1 parent 40c4ac4 commit 562aa8f
Show file tree
Hide file tree
Showing 15 changed files with 514 additions and 229 deletions.
4 changes: 2 additions & 2 deletions Materials/Cube.mat
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,5 @@ Material:
m_Colors:
- _Color: {r: 1, g: 1, b: 1, a: 1}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
- light: {r: 0.3213938, g: 0.76604444, b: -0.5566705, a: 0}
- sphereCenter: {r: 0, g: -0.766, b: 0.423, a: 0}
- light: {r: 0.6562178, g: 0.69221956, b: 0.3003507, a: 0}
- sphereCenter: {r: -0.4, g: -0.75, b: 0.2, a: 0}
4 changes: 2 additions & 2 deletions Materials/Unlit_Caustics.mat
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ Material:
- poolHeight: 1
- sphereRadius: 0.25
m_Colors:
- light: {r: 0.3213938, g: 0.76604444, b: -0.5566705, a: 0}
- sphereCenter: {r: 0, g: -0.766, b: 0.423, a: 0}
- light: {r: 0.6562178, g: 0.69221956, b: 0.3003507, a: 0}
- sphereCenter: {r: -0.4, g: -0.75, b: 0.2, a: 0}
4 changes: 2 additions & 2 deletions Materials/Unlit_Sphere.mat
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ Material:
- poolHeight: 1
- sphereRadius: 0.25
m_Colors:
- light: {r: 0.3213938, g: 0.76604444, b: -0.5566705, a: 0}
- sphereCenter: {r: 0, g: -0.766, b: 0.423, a: 0}
- light: {r: 0.6562178, g: 0.69221956, b: 0.3003507, a: 0}
- sphereCenter: {r: -0.4, g: -0.75, b: 0.2, a: 0}
6 changes: 3 additions & 3 deletions 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.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}
- eye: {r: 1.3820654, g: 1.0016792, b: 3.6965032, a: 0}
- light: {r: 0.6562178, g: 0.69221956, b: 0.3003507, a: 0}
- sphereCenter: {r: -0.4, g: -0.75, b: 0.2, a: 0}
6 changes: 3 additions & 3 deletions 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.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}
- eye: {r: 1.3820654, g: 1.0016792, b: 3.6965032, a: 0}
- light: {r: 0.6562178, g: 0.69221956, b: 0.3003507, a: 0}
- sphereCenter: {r: -0.4, g: -0.75, b: 0.2, a: 0}
619 changes: 422 additions & 197 deletions Scenes/cube-of-water.unity

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions Scripts/CameraOrbit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraOrbit : MonoBehaviour {
public float rotationRate = 100f;

// Use this for initialization
void Start () {
}

// Update is called once per frame
void Update () {
if (Input.GetMouseButton(0)) {
transform.Rotate(-Input.GetAxis("Mouse Y") * rotationRate * Time.deltaTime, 0f, 0f, Space.Self);
transform.Rotate(0f, Input.GetAxis("Mouse X") * rotationRate * Time.deltaTime, 0f, Space.World);
}
}
}
11 changes: 11 additions & 0 deletions Scripts/CameraOrbit.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 19 additions & 12 deletions Scripts/WaterSimulation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,27 @@
// http://tips.hecomi.com/entry/2017/05/17/020037
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 CustomRenderTextureUpdateZone[] zones = null;
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.passIndex = 0; // integrate
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.passIndex = 2; // update normals
normalZone.rotation = 0f;
normalZone.updateZoneCenter = new Vector2(0.5f, 0.5f);
normalZone.updateZoneSize = new Vector2(1f, 1f);
Expand All @@ -39,8 +40,13 @@ void Start() {
clickZone.updateZoneSize = new Vector2(dropRadius, dropRadius);
// clickZone.updateZoneSize = new Vector2(1f, 1f);

texture.ClearUpdateZones();
texture.SetUpdateZones(new CustomRenderTextureUpdateZone[] { defaultZone, defaultZone, normalZone });
texture.SetUpdateZones(new CustomRenderTextureUpdateZone[] { clickZone, normalZone });
for (int i = 0; i < 20; i++) {
clickZone.updateZoneCenter = new Vector2(Random.Range(0f, 1f),
1f - Random.Range(0f, 1f));
texture.Update(1);
}
// texture.ClearUpdateZones();
}

public void OnDrag(PointerEventData ped) {
Expand All @@ -64,11 +70,12 @@ void AddWave(PointerEventData ped) {
var leftClick = ped.button == PointerEventData.InputButton.Left;

Debug.Log("We got a click " + localCursor + " uv " + uv);
AddWave(uv, leftClick ? 2 : 3); // 1 または -1 にバッファを塗るパス);
// AddWave(uv, leftClick ? 2 : 3); // 1 または -1 にバッファを塗るパス);
AddWave(uv);
}

void AddWave(Vector2 uv, int passIndex) {
clickZone.updateZoneCenter = new Vector2(uv.x, 1 - uv.y);
void AddWave(Vector2 uv) {
clickZone.updateZoneCenter = new Vector2(uv.x, 1f - uv.y);

if (pause) {
zones = new CustomRenderTextureUpdateZone[] { clickZone, normalZone };
Expand All @@ -86,11 +93,10 @@ void Update() {
texture.SetUpdateZones(zones);
zones = null;
} else {
// texture.ClearUpdateZones();
texture.SetUpdateZones(new CustomRenderTextureUpdateZone[] { defaultZone, defaultZone, normalZone });
}
// if (! pause
// || Input.GetKeyDown(KeyCode.N))
if (! pause
|| Input.GetKeyDown(KeyCode.N))
texture.Update(1);
}

Expand All @@ -109,7 +115,8 @@ void UpdateZones() {

if (collider.Raycast(ray, out hit, 100f)) {
Debug.Log("Clicked uv " + hit.textureCoord2);
AddWave(hit.textureCoord2, leftClick ? 2 : 3);
// AddWave(hit.textureCoord2, leftClick ? 2 : 3);
AddWave(hit.textureCoord2);
}
}
}
8 changes: 6 additions & 2 deletions Shaders/Caustics.shader
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ Shader "Unlit/Caustics"
o.ray = refract(-light, normal, IOR_AIR / IOR_WATER);
o.oldPos = project(v.vertex.xzy, refractedLight, refractedLight);
o.newPos = project(v.vertex.xzy + float3(0.0, info.r, 0.0), o.ray, refractedLight);

// o.vertex = float4(0.75 * (o.newPos.xz + refractedLight.xz / refractedLight.y), 0.0, 1.0);
o.vertex = float4(0.75 * (o.newPos.xz + refractedLight.xz / refractedLight.y), 0.0, 1.0);
// o.vertex.y *= 1;

// o.position = v.vertex.xyz;
// // o.worldPos = mul (unity_ObjectToWorld, v.vertex);
Expand Down Expand Up @@ -99,7 +100,10 @@ Shader "Unlit/Caustics"
float3 refractedLight = refract(-light, float3(0.0, 1.0, 0.0), IOR_AIR / IOR_WATER);

/* compute a blob shadow and make sure we only draw a shadow if the player is blocking the light */
float3 dir = (sphereCenter - i.newPos) / sphereRadius;
// float3 dir = (float3(sphereCenter.xy, -sphereCenter.z) - i.newPos) / sphereRadius;
// float3 dir = (float3(sphereCenter.xy, sphereCenter.z) - i.newPos) / sphereRadius;
// float3 dir = (float3(sphereCenter.xy, -sphereCenter.z) - i.oldPos) / sphereRadius;
float3 dir = (sphereCenter - i.oldPos) / sphereRadius;
float3 area = cross(dir, refractedLight);
float shadow = dot(area, area);
float dist = dot(dir, -refractedLight);
Expand Down
8 changes: 7 additions & 1 deletion Shaders/HelperFunctions.cginc
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@
return color;
}

float2 flipY(float2 v) {
v.y = 1 - v.y;
return v;
}

float3 getWallColor(float3 _point) {
float scale = 0.5;

Expand All @@ -82,7 +87,8 @@
float diffuse = max(0.0, dot(refractedLight, normal));
float4 info = tex2D(water, _point.xz * 0.5 + 0.5);
if (_point.y < info.r) {
float4 caustic = tex2D(causticTex, 0.75 * (_point.xz - _point.y * refractedLight.xz / refractedLight.y) * 0.5 + 0.5);
// float4 caustic = tex2D(causticTex, 0.75 * (_point.xz - _point.y * refractedLight.xz / refractedLight.y) * 0.5 + 0.5);
float4 caustic = tex2D(causticTex, flipY(0.75 * (_point.xz - _point.y * refractedLight.xz / refractedLight.y) * 0.5 + 0.5));
scale += diffuse * caustic.r * 2.0 * caustic.g;
} else {
/* shadow for the rim of the pool */
Expand Down
6 changes: 3 additions & 3 deletions Shaders/Sphere.shader
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@
v2f vert (appdata v)
{
v2f o;
// o.position = sphereCenter + v.vertex.xyz * sphereRadius;
o.position = sphereCenter + v.vertex.xyz * sphereRadius;
o.vertex = UnityObjectToClipPos(v.vertex * sphereRadius);
// o.vertex = UnityObjectToClipPos(o.position);
o.position = sphereCenter + v.vertex.xyz * sphereRadius;
// o.vertex = UnityObjectToClipPos(v.vertex * sphereRadius);
o.vertex = UnityObjectToClipPos(o.position);
// o.vertex = mul(UNITY_MATRIX_MVP, float4(o.position, 1));
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
// UNITY_TRANSFER_FOG(o,o.vertex);
Expand Down
4 changes: 2 additions & 2 deletions Textures/sky cubemap.cubemap

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions todo.org
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
* TODO
- [ ] Sphere shadow is not working.
- [ ] Make camera pivot-able.
- [ ] Add ripples
- [ ] Made sphere draggable.
- [ ] Add sphere physics.
7 changes: 7 additions & 0 deletions todo.org.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 562aa8f

Please sign in to comment.