6
2012
360 Panorama Reflection Shader
Tried to convert this webgl shader to unity.. but its not working yet.
Webplayer:
http://unitycoder.com/upload/demos/360ReflectionShader_unity/ (v2.0)
Download Source: (webplayer scene)
360panoramaFakeReflection.unityPackage
Source v1.0 (broken):
– Can anyone spot what could be the problem..or are the textures just wrong? I have no idea about those matrix stuffs either, just picked something that sounded similar in unity 🙂
– Update: was missing “IN.” at pitch line.. also updated some calculations to unity built-in variables.. now it almost works..
– Added webplayer with torus and new screenshot below.
// original source: http://www.clicktorelease.com/code/streetViewReflectionMapping Shader "Custom/360reflection2" { Properties { _texture ("texture (RGB)", 2D) = "white" {} _scaledTexture ("scaledTexture (RGB)", 2D) = "white" {} } SubShader { Tags { "RenderType"="Opaque" } LOD 200 CGPROGRAM #pragma target 3.0 #pragma surface surf Lambert vertex:vert float3 vReflect; float3 vRefract; struct Input { float2 uv_MainTex; float4 pos : SV_POSITION; float3 vReflect; float3 vRefract; }; void vert (inout appdata_full v, out Input o) { float4 mPosition = mul(_Object2World,float4(v.vertex.xyz, 1.0 )); float3 nWorld = normalize( mul( float3x3(_Object2World[0].xyz, _Object2World[1].xyz, _Object2World[2].xyz) ,v.normal )); o.vReflect = normalize( reflect( normalize( mPosition.xyz - _WorldSpaceCameraPos ), nWorld ) ); o.vRefract = normalize( refract( normalize( mPosition.xyz - _WorldSpaceCameraPos ), nWorld, 0.9 ) ); o.pos = mul(UNITY_MATRIX_P * UNITY_MATRIX_MV,float4(v.vertex.xyz, 1.0)); } uniform float rAmount; uniform sampler2D _texture; uniform sampler2D _scaledTexture; void surf (Input IN, inout SurfaceOutput o) { float PI = 3.14159265358979323846264; float yaw = .5 - atan2(IN.vReflect.z, - IN.vReflect.x ) / ( 2.0 * PI ); float pitch = .5 - asin( vReflect.y ) / PI; float3 color = tex2D(_scaledTexture, float2( yaw, pitch ) ).rgb * ( 1.0 - rAmount ); yaw = .5 - atan2(IN.vRefract.z, - vRefract.x ) / ( 2.0 * PI ); pitch = .5 - asin(IN.vRefract.y ) / PI; color += tex2D(_texture, float2( yaw, pitch ) ).rgb * rAmount; half4 c = float4( color, 1.0 ); o.Albedo = c.rgb; o.Alpha = c.a; } ENDCG } FallBack "Diffuse" }
—
Image #2: Almost.. reflection image is upside down. Added webplayer for this version.
Related Posts
5 Comments + Add Comment
Leave a comment
Recent posts
- [GreaseMonkey] Unity Forum Fixer
- UnityHub: Make Hub application background Translucent
- Customize SpriteShapeRenderer quality (but has issues)
- Editor tool: Copy selected gameobject’s names into clipboard as rows (for Excel)
- Editor tool: Replace string in selected gameobject’s names
- UnityHub: Enable built-in Login Dialog (no more browser login/logout issues!)
- Use TikTok-TTS in Unity (with WebRequest)
- Create Scene Thumbnail Image using OnSceneSaved & OnPreviewGUI
- webgl+javascript TTS
- Using Moonsharp (LUA) + Unity Webgl
- Using 3D gameobject prefabs with Unity Tilemap + NavMesh Surface
- Custom Unity Hub Project Template Preview Image/Video (using HTML+CSS in package description)
Recent Comments
- Vector3 maths for dummies! on
- UnityHub: Make Hub application background Translucent on
- UnityHub: Make Hub application background Translucent on
- Install Android SDK+JDK+NDK for Unity (without AndroidStudio or Unity Hub) on
- Install Android SDK+JDK+NDK for Unity (without AndroidStudio or Unity Hub) on
- [Asset Store] Point Cloud Viewer & Tools on
- [Asset Store] Point Cloud Viewer & Tools on
- ffmpeg stream raw video into Unity Texture2D on
it looks pretty good already, like awesome, sell it or give it in the asset store MAN!
and the unify wiki is very cool also! good way to get clicks back to your website!
Hello! Thank you very much for shader! I found small mistake.. =)
In line 42 must be “IN.vReflect.y” instead of “vReflect.y”:
float pitch = .5 – asin( IN.vReflect.y ) / PI;
otherwise pitch will be always .5
ouch!..Thanks 🙂
fixed:
void surf (Input IN, inout SurfaceOutput o)
{
float PI = 3.14159265358979323846264;
float yaw = .5 – atan2(IN.vReflect.z, – IN.vReflect.x ) / ( 2.0 * PI );
float pitch = .5 – asin( -IN.vReflect.y ) / PI;
float3 color = tex2D(_scaledTexture, float2( yaw, pitch ) ).rgb * ( 1.0 – rAmount );
yaw = .5 – atan2(IN.vRefract.z, – IN.vRefract.x ) / ( 2.0 * PI );
pitch = .5 – asin(IN.vRefract.y ) / PI;
color += tex2D(_texture, float2( yaw, pitch ) ).rgb * rAmount;
half4 c = float4( color, 1.0 );
o.Albedo = c.rgb;
o.Alpha = c.a;
}