unity3d中用2D背景当作图片
来源:互联网 发布:如何删掉淘宝评价 编辑:程序博客网 时间:2024/06/05 07:30
using UnityEngine;
using System.Collections;
public class Backdrop : MonoBehaviour
{
const string SHADER_CODE =
"Shader \"UnlitAlpha\"{" +
"Properties {" +
"_Color (\"Color Tint (A = Opacity)\", Color) = (1,1,1,1) " +
"_MainTex (\"Texture (A = Transparency)\", 2D) = \"\"" +
"} " +
"SubShader { " +
"Tags {" +
"Queue = Transparent" +
"}" +
"ZWrite Off Blend SrcAlpha OneMinusSrcAlpha Pass {" +
"SetTexture[_MainTex] {" +
"Combine texture * constant ConstantColor[_Color]" +
"}" +
"}" +
"}" +
"}";
public Texture2D image;
public float distance = 500;
GameObject backdrop;
Mesh mesh;
float prevDistance;
Vector3 prevRotation, prevPosition;
Texture2D prevImage;
void Start ()
{
//check that we are on a camera!
if(camera == null){
Debug.LogError("Backdrop must be used on a camera!");
Destroy(this);
return;
}
//create backdrop GameObject and components:
backdrop = new GameObject("Backdrop");
backdrop.AddComponent<MeshFilter>();
backdrop.AddComponent<MeshRenderer>();
backdrop.transform.parent = transform;
backdrop.renderer.material = new Material(SHADER_CODE);
mesh = backdrop.GetComponent<MeshFilter>().mesh;
mesh.vertices = CalcVerts();
mesh.uv = new Vector2[] {new Vector2(0,0), new Vector2(1,0), new Vector2(0,1), new Vector2(1,1)};
mesh.triangles = new int[] {1,0,3,3,0,2};
}
void Update(){
//reset texture if it has changed:
if( backdrop.renderer.material.mainTexture != image ){
backdrop.renderer.material.mainTexture = image;
}
//nothing needs to be recalculated if distance hasn't changed or a move/rotate was attempted on billboard moved without our permission:
if(distance == prevDistance &&
backdrop.transform.position == prevPosition &&
backdrop.transform.localEulerAngles == prevRotation
) return;
//error for attempting a backdrop placement beyond camera's far clip plane:
if(distance > camera.farClipPlane){
Debug.LogError("Backdrop's distance is further than the camera's far clip plane. Extend the camera's far clip plane or reduce the billboard's distance.");
return;
}
//error for attempting a backdrop placement before camera's near clip plane:
if(distance < camera.nearClipPlane){
Debug.LogError("Backdrop's distance is closer than the camera's near clip plane. Extend the distance or reduce the camera's near clip plane.");
return;
}
//set backdrop's placement:
backdrop.transform.position = transform.forward * distance;
//calculate mesh:
mesh.vertices = CalcVerts();
mesh.RecalculateNormals();
//readjust comparison values:
prevDistance = distance;
prevPosition = backdrop.transform.position;
prevRotation = backdrop.transform.localEulerAngles;
}
Vector3[] CalcVerts()
{
return new Vector3[] {
backdrop.transform.InverseTransformPoint(camera.ScreenToWorldPoint(new Vector3(0,0,distance))),
backdrop.transform.InverseTransformPoint(camera.ScreenToWorldPoint(new Vector3(Screen.width,0,distance))),
backdrop.transform.InverseTransformPoint(camera.ScreenToWorldPoint(new Vector3(0,Screen.height,distance))),
backdrop.transform.InverseTransformPoint(camera.ScreenToWorldPoint(new Vector3(Screen.width,Screen.height,distance)))
};
}
}
using System.Collections;
public class Backdrop : MonoBehaviour
{
const string SHADER_CODE =
"Shader \"UnlitAlpha\"{" +
"Properties {" +
"_Color (\"Color Tint (A = Opacity)\", Color) = (1,1,1,1) " +
"_MainTex (\"Texture (A = Transparency)\", 2D) = \"\"" +
"} " +
"SubShader { " +
"Tags {" +
"Queue = Transparent" +
"}" +
"ZWrite Off Blend SrcAlpha OneMinusSrcAlpha Pass {" +
"SetTexture[_MainTex] {" +
"Combine texture * constant ConstantColor[_Color]" +
"}" +
"}" +
"}" +
"}";
public Texture2D image;
public float distance = 500;
GameObject backdrop;
Mesh mesh;
float prevDistance;
Vector3 prevRotation, prevPosition;
Texture2D prevImage;
void Start ()
{
//check that we are on a camera!
if(camera == null){
Debug.LogError("Backdrop must be used on a camera!");
Destroy(this);
return;
}
//create backdrop GameObject and components:
backdrop = new GameObject("Backdrop");
backdrop.AddComponent<MeshFilter>();
backdrop.AddComponent<MeshRenderer>();
backdrop.transform.parent = transform;
backdrop.renderer.material = new Material(SHADER_CODE);
mesh = backdrop.GetComponent<MeshFilter>().mesh;
mesh.vertices = CalcVerts();
mesh.uv = new Vector2[] {new Vector2(0,0), new Vector2(1,0), new Vector2(0,1), new Vector2(1,1)};
mesh.triangles = new int[] {1,0,3,3,0,2};
}
void Update(){
//reset texture if it has changed:
if( backdrop.renderer.material.mainTexture != image ){
backdrop.renderer.material.mainTexture = image;
}
//nothing needs to be recalculated if distance hasn't changed or a move/rotate was attempted on billboard moved without our permission:
if(distance == prevDistance &&
backdrop.transform.position == prevPosition &&
backdrop.transform.localEulerAngles == prevRotation
) return;
//error for attempting a backdrop placement beyond camera's far clip plane:
if(distance > camera.farClipPlane){
Debug.LogError("Backdrop's distance is further than the camera's far clip plane. Extend the camera's far clip plane or reduce the billboard's distance.");
return;
}
//error for attempting a backdrop placement before camera's near clip plane:
if(distance < camera.nearClipPlane){
Debug.LogError("Backdrop's distance is closer than the camera's near clip plane. Extend the distance or reduce the camera's near clip plane.");
return;
}
//set backdrop's placement:
backdrop.transform.position = transform.forward * distance;
//calculate mesh:
mesh.vertices = CalcVerts();
mesh.RecalculateNormals();
//readjust comparison values:
prevDistance = distance;
prevPosition = backdrop.transform.position;
prevRotation = backdrop.transform.localEulerAngles;
}
Vector3[] CalcVerts()
{
return new Vector3[] {
backdrop.transform.InverseTransformPoint(camera.ScreenToWorldPoint(new Vector3(0,0,distance))),
backdrop.transform.InverseTransformPoint(camera.ScreenToWorldPoint(new Vector3(Screen.width,0,distance))),
backdrop.transform.InverseTransformPoint(camera.ScreenToWorldPoint(new Vector3(0,Screen.height,distance))),
backdrop.transform.InverseTransformPoint(camera.ScreenToWorldPoint(new Vector3(Screen.width,Screen.height,distance)))
};
}
}
0 0
- unity3d中用2D背景当作图片
- Unity3D -- 使用可移动图片作为3D背景
- unity3d技术---在2d游戏中用shader模拟阴影
- cocos2d-x3.2中用shader使图片背景透明
- Unity3D~2D游戏背景的循环滚动
- 把地球当作背景
- [unity3d]设置3D模型显示在2D背景之前(多个相机分层显示)
- 【Unity】为什么导入2D图片背景不是透明的
- 【Unity3D】学习笔记(第2记) 2D游戏开发基本技巧之背景制作
- 【unity3d学习笔记】Shader-通过Shader实现2D游戏中无限滚动的背景Shader
- Unity3d 2d设置
- Unity 2D背景滚动
- Unity3d 中如何旋转一张2D图片到指定角度
- [原创]unity3D学习【功能实现】之二:图片的旋转/2d物体的旋转
- Unity3D 2D Toolkit (之一)
- unity3d插件 2D Toolkit
- Unity3d 2d动画组件
- Unity3D 2D ToolKit --tk2dCamera
- 在win7下安装IIS 7后打开自己的项目之后会出现500.19错误
- 使用CSS3 Media Queries实现网页自适应
- Spring的事务及五种配置方式
- Java 回顾笔记_基本数据类型对象包装类
- 做一个合格的程序猿之浅析Spring AOP源码(十二) AOP概念理解
- unity3d中用2D背景当作图片
- 与LSGO一起学“第1章 初识C++(1.1 C++简介)”
- 关于分类树节点编辑成功后刷新树并展开操作节点
- balabala半年了
- Linux服务器安装Redis
- SharedPreferences方便存取工具类
- wget 命令用法详解
- 生产环境中centOS7最简版安装
- 腾讯面试体验