纳金网

标题: [脚本]unity3d coverflow 效果(追加倒影) [打印本页]

作者: 驰骋的风    时间: 2012-11-25 09:45
标题: [脚本]unity3d coverflow 效果(追加倒影)


弄几张图放到Assets/Resources目录下,取名叫photo0, photo1.........







    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
     
    public class CoverFlow : MonoBehaviour
    {
            private List<GameObject> photos = new List<GameObject> ();
            private int photosCount = 9;
     
            private int currentIndex=0;
            private float MARGIN_X=3f;  //plane 之间的间隔
            private float ITEM_W=10f;   //plane长宽为10M(10个单位长度)
     
            private float sliderValue=4f;
     
            void Start ()
            {
                    loadImages ();
            }
     
            void loadImages ()
            {
                    for (int i=0; i<photosCount; i++) {
                            GameObject photo = GameObject.CreatePrimitive (PrimitiveType.Plane);
                            photos.Add (photo);
                            //photo.renderer.material.shader=Shader.Find("Self-Illumin/Diffuse");
     
                            photo.transform.eulerAngles = new Vector3 (-90f,0f, 0f);
                            photo.transform.localScale=new Vector3(1.5f,1f,-1f);   //根据图片设定长宽比,z:-1,使图正向
                            photo.renderer.material.mainTexture = Resources.Load ("photo" + i.ToString(),typeof(Texture2D)) as Texture2D;
     
     
                            photo.transform.parent=gameObject.transform;
                    }
     
                    moveSlider(photos.Count/2);
            }
     
            // Update is called once per frame
            void Update ()
            {
     
            }
     
            void moveSlider(int id){
                    if(currentIndex==id)
                            return;
                    currentIndex=id;
     
                    for(int i=0;i<photosCount;i++){
                            float targetX=0f;
                            float targetZ=0f;
                            float targetRot=0f;
     
                            targetX=MARGIN_X*(i-id);
                            //left slides
                            if(i<id){
                                    targetX-=ITEM_W*0.6f;
                                    targetZ=ITEM_W*3f/4;
                                    targetRot=-60f;
     
                            }
                            //right slides
                            else if(i>id){
                                    targetX+=ITEM_W*0.6f;
                                    targetZ=ITEM_W*3f/4;
                                    targetRot=60f;
                            }else{
                                    targetX += 0f;
                                    targetZ = 0f;
                                    targetRot = 0f;
                            }
     
                           GameObject photo=photos;
                           //GameObject photo=photos【i】  论坛Bug  中括号无显示  各位务必加上
     
     
                            float ys=photo.transform.position.y;
                            Vector3 ea=photo.transform.eulerAngles;
     
                            //iTween.Stop(photo);
                            //本来想优化一下性能的,你可以去掉 iTween.Stop 前的注释,会发现Bug,
                            //希望iTween增加overwrite 这样的特性,
                            //及像 BetweenAS3 这样的具备高效并列、序列处理动画的算法
     
                            iTween.MoveTo(photo,new Vector3(targetX,ys,targetZ),1f);
                            iTween.RotateTo(photo,new Vector3(ea.x,targetRot,targetZ),1f);
                    }
            }
     
     
            void OnGUI ()
            {
     
                    sliderValue = GUI.HorizontalSlider(new Rect((Screen.width-175f)/2,Screen.height-30f,175f,30f),
                            sliderValue, 0f, photosCount-1);
     
     
                    moveSlider((int)sliderValue);
            }
     
    }






改天把倒影加上

PS:http://game.ceeger.com/f***m/read.php?tid=1411&ds=1#tpc,kuku小夭提供了NGui的方案,并追加了倒影



倒影另一种思路,每张Plane下方再建一个Plane(两个Plane为一个单位),赋与自定义Shader 和对应 贴图,参与动画



-----------------追加倒影版:











操作说明:







相关资源:

一,CoverFlow.cs:







    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
     
    public class CoverFlow : MonoBehaviour
    {
            public Texture2D refMask;
            private List<GameObject> photos = new List<GameObject> ();
            private int photosCount = 9;
            
            private int currentIndex=0;
            private float MARGIN_X=3f;
            private float ITEM_W=15f;
            
            private float sliderValue=4f;
            
            void Start ()
            {
                    loadImages ();
            }
            
            void loadImages ()
            {
                    for (int i=0; i<photosCount; i++) {
                            Texture2D tex=Resources.Load ("photo" + i.ToString(),typeof(Texture2D)) as Texture2D;
                            photos.Add (PhotoBuilder.GeneratePhotoWithReflect(refMask,tex,i,this.transform));
                    }
                    
                    moveSlider(photos.Count/2);
            }
            
            // Update is called once per frame
            void Update ()
            {
            
            }
            
            void moveSlider(int id){
                    if(currentIndex==id)
                            return;
                    currentIndex=id;
                    
                    for(int i=0;i<photosCount;i++){
                            float targetX=0f;
                            float targetZ=0f;
                            float targetRot=0f;
                           
                            targetX=MARGIN_X*(i-id);
                            //left slides
                            if(i<id){
                                    targetX-=ITEM_W*0.6f;
                                    targetZ=ITEM_W*3f/4;
                                    targetRot=-60f;
                                    
                            }
                            //right slides
                            else if(i>id){
                                    targetX+=ITEM_W*0.6f;
                                    targetZ=ITEM_W*3f/4;
                                    targetRot=60f;
                            }else{
                                    targetX += 0f;
                                    targetZ = 0f;
                                    targetRot = 0f;
                            }
                           
                            GameObject photo=photos;
                            float ys=photo.transform.position.y;
                            Vector3 ea=photo.transform.eulerAngles;
                           
                            //iTween.Stop(photo);
                           
                            iTween.MoveTo(photo,new Vector3(targetX,ys,targetZ),1f);
                            iTween.RotateTo(photo,new Vector3(ea.x,targetRot,targetZ),1f);
                    }
            }
            
            
            void OnGUI ()
            {
                    
                    sliderValue = GUI.HorizontalSlider(new Rect((Screen.width-175f)/2,Screen.height-30f,175f,30f),
                            sliderValue, 0f, photosCount-1);
                    
                    
                    moveSlider((int)sliderValue);
            }
            
    }
     
    class PhotoBuilder{
            public static GameObject GeneratePhotoWithReflect(Texture2D refMask,Texture2D tex,int id,Transform parent){
                    GameObject photoObj=new GameObject();
                    photoObj.name="photoObj"+id.ToString();
                           
                           
                    GameObject photo = GameObject.CreatePrimitive (PrimitiveType.Plane);
                    photo.renderer.material.shader=Shader.Find("Unlit/Texture");
                            //Self-Illumin/Diffuse
                    photo.name="photo";
                    photo.renderer.material.mainTexture = tex;
                           
                    photo.transform.localScale=new Vector3(1.5f,1f,-1f);
                    photo.transform.parent=photoObj.transform;
                           
                    GameObject reflect=GameObject.CreatePrimitive(PrimitiveType.Plane);
                    reflect.name="reflect";
                    reflect.renderer.material.shader=Shader.Find("MaskedTexture");
                    reflect.renderer.material.mainTexture=tex;
                    reflect.renderer.sharedMaterial.SetTexture("_Mask",refMask);
                    reflect.transform.localPosition=new Vector3(0f,0f,-9.70f);
                    reflect.transform.localScale=new Vector3(1.5f,1f,1f);
                    reflect.transform.parent=photoObj.transform;
                           
                    photoObj.transform.eulerAngles = new Vector3 (-90f,0f, 0f);
                    photoObj.transform.parent=parent;
                    
                    return photoObj;
            }
    }








二:TextureMasked.shader





    Shader "MaskedTexture"
    {
       Properties
       {
          _MainTex ("Base (RGB)", 2D) = "white" {}
          _Mask ("Culling Mask", 2D) = "white" {}
          _Cutoff ("Alpha cutoff", Range (0,1)) = 0.0
       }
       SubShader
       {
          Tags {"Queue"="Transparent"}
          Lighting Off
          ZWrite Off
          Blend SrcAlpha OneMinusSrcAlpha
          AlphaTest GEqual [_Cutoff]
          Pass
          {
             SetTexture [_Mask] {combine texture}
             SetTexture [_MainTex] {combine texture, previous}
          }
       }
    }








三:refmask.png

右击

下载





四:项目包,我在苹果下的Package在Windows下面没正常打开,故打了压缩包,大家打开项目来看吧

Layouts.zip

0 Bytes, 下载次数: 10






欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5