12 第1页 | 共2 页下一页
返回列表 发新帖
查看: 4925|回复: 12
打印 上一主题 下一主题

[经验分享] unity3d中创建动态环境光

[复制链接]

3795

主题

2

听众

5万

积分

版主

Rank: 7Rank: 7Rank: 7

纳金币
53202
精华
32

活跃会员 优秀版主 推广达人 突出贡献 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2011-11-24 09:37:00 |只看该作者 |倒序浏览


           不错的教程,全英文,主要讲解了如何在unity3d中实现动态环境光的效果,似乎可以用此方法来做出一天中不同时间的光线效果。下面是原文:
         

           I’ve been working on getting dynamic ambient lighting working within Unity. Based off Valve’s 6-colour pre-baked ambient lighting (detailed here, pg 5, ch 8.4.1), but it grabs the 6 colours dynamically.
         

           There’s probably lots more you could do to optimise it further (e.g. use replacement shaders when rendering the cubemap that do simpler lighting calcs). But you could do that yourself as required.
         

           I’d also advise against using it on anything other than your main character, as it’s likely too expensive to run on multiple objects.
         

           cubemap camera script
         

           Create a new camera and turn off the GUI, Flare and Audio components.
         

           Set up it’s Culling Layers to not render non-essential things like particles or incidental detail. Also move your character to it’s own layer and set the camera not to render it (we don’t want bits of the character rendered into the cubemap).
         

           Attach this javascript to it and set the target to be your character and set up the offset from your character’s position so that it’s in the centre of your character (i.e. a 2m tall character wants to be offset 0, 1, 0 so that the camera renders from the characters centre.
           

           下面为摄像机脚本:
         

           @script ExecuteInEditMode
         

           public var target : Transform;
         

           public var cubemapsize : int = 128;
         

           public var oneFacePerFrame : boolean = true;
         

           public var offset : Vector3 = Vector3.zero;
         

           private var cam : Camera;
         

           private var rtex : RenderTexture;
         

           function Start () {
         

           cam = camera;
         

           cam.enabled = false;
         

           // render all six faces at startup
         

           UpdateCubemap( 63 );
         

           transform.rotation = Quaternion.identity;
         

           }
         

           function LateUpdate () {
         

               if ( oneFacePerFrame ) {
         

                   var faceToRender = Time.frameCount % 6;
         

                   var faceMask = 1 << faceToRender;
         

                   UpdateCubemap ( faceMask );
         

               } else {
         

                   UpdateCubemap ( 63 ); // all six faces
         

               }
         

           }
         

           function UpdateCubemap ( faceMask : int ) {
         

           if ( !rtex ) {
         

           rtex = new RenderTexture ( cubemapSize, cubemapSize, 16 );
         

           rtex.isPowerOfTwo = true;
         

           rtex.isCubemap = true;
         

           rtex.useMipMap = true;
         

           rtex.hideFlags = HideFlags.HideAndDontSave;
         

           rtex.SetGlobalShaderProperty ( "
           


            _
           
           WorldCube" );
         

           }
         

           transform.position = target.position + offset;
         

           cam.RenderToCubemap ( rtex, faceMask );
         

           }
         

           function OnDisable () {
         

           DestroyImmediate ( rtex );
         

           }
         

           dynamic ambient shader
           

           环境光shader
         

           This is the shader that generates and applies the ambient lighting from the cubemap rendered by the camera above.
         

           Create a new shader, paste this code into it and save it. We’ll integrate it into our shaders next.
           

           下面是shader代码:
         

            
         

           Shader "DynamicAmbient" {
         

           Properties {
         


            _
           
           MainTex ("Diffuse (RGB) Alpha (A)", 2D) = "white" {}
         


            _
           
           BumpMap ("Normal (Normal)", 2D) = "bump" {}
         

           }
         

           SubShader{
         

           Pass {
         

           Name "DynamicAmbient"
         

           Tags {"LightMode" = "Always"}
         

            
         

           CGPROGRAM
         

           #pragma vertex vert
         

           #pragma fragment frag
         

           #pragma fragmentoption ARB
           
            _
           
           precision
           
            _
           
           hint
           
            _
           
           fastest
         

           #include "UnityCG.cginc"
         

           struct v2f
         

           {
         

           float4 pos : SV
           
            _
           
           POSITION;
         

           float2 uv : TEXCOORD0;
         

           float3 normal : TEXCOORD2;
         

           float3 tangent : TEXCOORD3;
         

           float3 binormal : TEXCOORD4;
         

           };
         

           v2f vert (appdata
           
            _
           
           tan v)
         

           {
         

           v2f o;
         

           o.pos = mul(UNITY
           
            _
           
           MATRIX
           
            _
           
           MVP, v.vertex);
         

           o.uv = v.texcoord.xy;
         

           o.normal = mul(
           
            _
           
           Object2World, float4(v.normal, 0)).xyz;
         

           o.tangent = v.tangent.xyz;
         

           o.binormal = cross(o.normal, o.tangent) * v.tangent.w;
         

           return o;
         

           }
         

           sampler2D
           
            _
           
           MainTex;
         

           sampler2D
           
            _
           
           BumpMap;
         

           samplerCUBE
           
            _
           
           WorldCube;
         

           float4 frag(v2f i) : COLOR
         

           {
         

           fixed4 albedo = tex2D(
           
            _
           
           MainTex, i.uv);
         

           float3 normal = UnpackNormal(tex2D(
           
            _
           
           BumpMap, i.uv));
         

           float3 worldNormal = normalize((i.tangent * normal.x) + (i.binormal * normal.y) + (i.normal * normal.z));
         

           float3 nSquared = worldNormal * worldNormal;
         

           fixed3 linearColor;
         

           linearColor = nSquared.x * texCUBEbias(
           
            _
           
           WorldCube, float4(worldNormal.x, 0.00001, 0.00001, 999)).rgb; // For unknown reasons, giving an absolute vector ignores the mips....
         

           linearColor += nSquared.y * texCUBEbias(
           
            _
           
           WorldCube, float4(0.00001, worldNormal.y, 0.00001, 999)).rgb; // ...so unused components must have a tiny, non-zero value in.
         

           linearColor += nSquared.z * texCUBEbias(
           
            _
           
           WorldCube, float4(0.00001, 0.00001, worldNormal.z, 999)).rgb;
         

            
         

           float4 c;
         

           c.rgb = linearColor * albedo.rgb;
         

           c.a = albedo.a;
         

           return c;
         

           }
         

           ENDCG
         

           }
         

           }
         

           FallBack Off
         

           }
         

           integrating the ambient shader into surface shaders
         

           将环境光shader与物体表面的shader发生互相影响
         

           Now, we can use the above shader wherever we want it via the UsePass command, and blending everything else on top.
         

           The key here is to ensure your surface shader’s blend mode is set to additive (One One) otherwise it’ll just write clean over the lovely ambient light that’s been applied.So, before your surface shader’s CGPROGRAM block, add the lines;
         

           添加shader代码:
         

           UsePass "DynamicAmbient/DYNAMICAMBIENT"
         

           Blend One One
         

           We’ve also got to ensure that our surface shader doesn’t use the ambient light value that’s set in the editor, otherwise it’ll add the two together and defeat the purpose. So when you define the surface shader to use, ensure you add the noambient argument. e.g;
         

           #pragma surf BlinnPhong noambient
         

           Your new surface shader with dynamic ambient lighting should look something like this;
         

           Shader "Bumped Specular" {
         

           Properties {
         


            _
           
           Color ("Main Color", Color) = (1,1,1,1)
         


            _
           
           SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
         


            _
           
           Shininess ("Shininess", Range (0.03, 1)) = 0.078125
         


            _
           
           MainTex ("Base (RGB) Gloss (A)", 2D) = "white" {}
         


            _
           
           BumpMap ("Normalmap", 2D) = "bump" {}
         

           }
         

           SubShader {
         

           Tags { "RenderType"="Opaque" }
         

           LOD 400
         

           UsePass "DynamicAmbient/DYNAMICAMBIENT"
         

           Blend One One
         

           CGPROGRAM
         

           #pragma surface surf BlinnPhong noambient
         

           sampler2D
           
            _
           
           MainTex;
         

           sampler2D
           
            _
           
           BumpMap;
         

           fixed4
           
            _
           
           Color;
         

           half
           
            _
           
           Shininess;
         

           struct Input {
         

           float2 uv
           
            _
           
           MainTex;
         

           float2 uv
           
            _
           
           BumpMap;
         

           };
         

           void surf (Input IN, inout SurfaceOutput o) {
         

           fixed4 tex = tex2D(
           
            _
           
           MainTex, IN.uv
           
            _
           
           MainTex);
         

           o.Albedo = tex.rgb *
           
            _
           
           Color.rgb;
         

           o.Gloss = tex.a;
         

           o.Alpha = tex.a *
           
            _
           
           Color.a;
         

           o.Specular =
           
            _
           
           Shininess;
         

           o.Normal = UnpackNormal(tex2D(
           
            _
           
           BumpMap, IN.uv
           
            _
           
           BumpMap));
         

           }
         

           ENDCG
         

           }
         

           FallBack "Specular"
         

           }
         

           Now apply your new shader to your character’s material and we’re done
         

           转自:http://www.farfarer.com/blog/2011/07/25/dynamic-ambient-lighting-in-unity/
           


            http://www.unity3d8.com/content/unity3d%E4%B8%AD%E5%88%9B%E5%BB%BA%E5%8A%A8%E6%80%81%E7%8E%AF%E5%A2%83%E5%85%89
           

分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

tc    

5089

主题

1

听众

33万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2012-4-10 23:31:39 |只看该作者
呵呵,真得不错哦!!
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

板凳
发表于 2012-4-20 23:20:38 |只看该作者
呵呵,很好,方便罗。
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

地板
发表于 2012-4-24 08:09:22 |只看该作者
赞一个,哈哈
回复

使用道具 举报

   

671

主题

1

听众

3247

积分

中级设计师

Rank: 5Rank: 5

纳金币
324742
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

5#
发表于 2012-5-2 23:22:25 |只看该作者
加精、加亮滴铁子,尤其要多丁页丁页
回复

使用道具 举报

markq    

511

主题

1

听众

1万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
15839
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

6#
发表于 2012-5-4 23:23:25 |只看该作者
  谢谢分享



爱生活 爱3D 爱纳金网



www.narkii.com
回复

使用道具 举报

462

主题

1

听众

31万

积分

首席设计师

Rank: 8Rank: 8

纳金币
2
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

7#
发表于 2012-6-2 23:26:43 |只看该作者
你们都躲开,我来顶
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

8#
发表于 2012-8-18 23:47:12 |只看该作者
再看一看,再顶楼主
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

9#
发表于 2012-12-2 23:26:45 |只看该作者
佩服,好多阿 ,哈哈
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

10#
发表于 2013-2-1 23:34:47 |只看该作者
加精、加亮滴铁子,尤其要多丁页丁页
回复

使用道具 举报

12 第1页 | 共2 页下一页
返回列表 发新帖
您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-5-15 14:12 , Processed in 0.146668 second(s), 29 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部