Hello,
over the past days I have been porting the projected grid ocean demo that Johanson (link to original paper and demo [here][1]) developed for his theseis to Unity. I have it almost working, however I am running into a couple of problems, so I thought I would ask here.
Regarding getting the frustum and checking intersections between lower and upper ocean bounds, I have the following:
int[] cube = { 0,1, 0,2, 2,3, 1,3,
0,4, 2,6, 3,7, 1,5,
4,6, 4,5, 5,7, 6,7};
frustum[0] = renderingCamera.ViewportToWorldPoint(new Vector3(0,0,renderingCamera.nearClipPlane));
frustum[1] = renderingCamera.ViewportToWorldPoint(new Vector3(0,1,renderingCamera.nearClipPlane));
frustum[2] = renderingCamera.ViewportToWorldPoint(new Vector3(1,0,renderingCamera.nearClipPlane));
frustum[3] = renderingCamera.ViewportToWorldPoint(new Vector3(1,1,renderingCamera.nearClipPlane));
frustum[4] = renderingCamera.ViewportToWorldPoint(new Vector3(0,0,renderingCamera.farClipPlane));
frustum[5] = renderingCamera.ViewportToWorldPoint(new Vector3(0,1,renderingCamera.farClipPlane));
frustum[6] = renderingCamera.ViewportToWorldPoint(new Vector3(1,0,renderingCamera.farClipPlane));
frustum[7] = renderingCamera.ViewportToWorldPoint(new Vector3(1,1,renderingCamera.farClipPlane));
Vector3 testLine;
// check intersection with the lower and upper bounds
for (i=0; i<12; i++) {
src = cube[i*2];
dst = cube[i*2+1];
testLine = frustum[dst] - frustum[src];
dist = testLine.magnitude;
testLine.Normalize();
ray = new Ray(frustum[src], testLine);
float distanceToUpper;
float distanceToLower;
if (upperBoundPlane.Raycast(ray, out distanceToUpper)) {
if (distanceToUpper < dist + 0.00001f)
projPoints[nPoints++] = frustum[src] + distanceToUpper * testLine;
}
if (lowerBoundPlane.Raycast(ray, out distanceToLower)) {
if (distanceToLower < dist + 0.00001f)
projPoints[nPoints++] = frustum[src] + distanceToLower * testLine;
}
}
// check if the frustum vertices lie between the lower and upper bounds
for (i=0; i<8; i++) {
if ( (upperBoundPlane.GetDistanceToPoint(frustum_) / lowerBoundPlane.GetDistanceToPoint(frustum*)) < 0) {*_
_ projPoints[nPoints++] = frustum*;
}
}
Here, I am not sure about the frustum vertices defined in the cube[] array. These are the ones that Johanson used, obviously it worked for him. However I think that this is the culprit for the wrong visual results that I am getting. Basically when I move the Camera around, the water is clipped wrong and only at some angles is projected correctly.
[5620-screen+shot+2012-12-08+at+12.20.31.png*|5620]*
The 2nd thing that I am unsure of is calculating the world position based on the range matrix (the range matrix is basically the matrix used to project the grid vertices in the original demo). My ported code is the following:
* private Vector4 calcWorldPosition (Vector2 uv, Matrix4x4 range) {*
* Vector4 origin = new Vector4(uv.x, uv.y, -1, 1);
Vector4 direction = new Vector4(uv.x, uv.y, 1, 1);*_
_ origin = range * origin;
direction = range * direction;_
* direction -= origin;*
* float l = -origin.y / direction.y;*
_ return origin + direction * l;
* }
And I calculated the corners as defined in the original demo as such:
corners[0] = _calcWorldPosition(new Vector2(0,0), rangeMatrix);
corners[1] = _calcWorldPosition(new Vector2(0,1), rangeMatrix);
corners[2] = _calcWorldPosition(new Vector2(1,0), rangeMatrix);
corners[3] = _calcWorldPosition(new Vector2(1,1), rangeMatrix);
That was quite a bit to read, sorry for that. I did not include the matrix generation and grid projection functions as these are probably correct. I am sure it has something to do with the aforementioned calculations. May I also say that the original demo was written in Direct3D, maybe some conventions are different.
I also include a video of the issues [here][3].
Can someone shed some light here?
Cheers
[1]: http://fileadmin.cs.lth.se/graphics/theses/projects/projgrid/*_
*
*[3]: https://dl.dropbox.com/u/4674849/projected%20grid.mov*_