智能合约的运行机制
作为自动决策程序,智能合约既具有计算机程序代码,也具有传统合同元素,二者相对独立又相互联系。智能合约的运行机制一般包括三方内容:
一是每一个智能合约都必须有特定的条件框架(Conditional Framework),这一条件框架既符合合同法规则,也是计算机编码必需的。条件框架由多方参与者共同制定,完成智能合约的构建。
智能合约是运行在区块链系统可复制、共享账本上的计算机程序,可以处理信息,接收、储存和发送价值。基于区块链技术的智能合约,不仅可以发挥智能合约在成本效率方面的优势,而且可以避免恶意行为对合约正常执行的干扰。
将智能合约以数字化的形式写入区块链中,由区块链技术的特性保障存储、读取、执行整个过程透明可跟踪、不可篡改。同时,由区块链自带的共识算法构建出一套状态机系统,使智能合约能够高效地运行。
Shader"Custom/FakeRoom"
{
Properties
{
[NoScaleOffset]_WindowTex("Window Texture",2D)="black"{}
_RoomTex("Room Texture",CUBE)=""{}
_RoomDepth("Room Depth",Range(0.01,1))=1.0
}
SubShader
{
Tags{"RenderType"="Opaque"}
Cull Back
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include"UnityCG.cginc"
#define FLT_EPS 5.960464478e-8//2^-24,machine epsilon:1+EPS=1(half of the ULP for 1.0f)
#define Max3(a,b,c)max(max(a,b),c)
#define Min3(a,b,c)min(min(a,b),c)
struct appdata
{
float4 positionOS:POSITION;
float2 uv:TEXCOORD0;
float3 normal:NORMAL;
};
struct v2f
{
float2 uv:TEXCOORD0;
float4 positionCS:SV_POSITION;
float3 positionOS:TEXCOORD1;
float3 viewDirOS:TEXCOORD2;
float3 normalOS:TEXCOORD3;
};
sampler2D _WindowTex;
samplerCUBE _RoomTex;
float4 _RoomTex_ST;
fixed _RoomDepth;
bool IntersectRayAABB(float3 rayOrigin,float3 rayDirection,
float3 boxMin,float3 boxMax,
float tMin,float tMax,
out float tEntr,out float tExit)
{
//Could be precomputed.Clamp to avoid INF.clamp()is a single ALU on GCN.
//rcp(FLT_EPS)=16,777,216,which is large enough for our purposes,
//yet doesn't cause a lot of numerical issues associated with FLT_MAX.
float3 rayDirInv=clamp(rcp(rayDirection),-rcp(FLT_EPS),rcp(FLT_EPS));
//Perform ray-slab intersection(component-wise).
float3 t0=boxMinrayDirInv-(rayOriginrayDirInv);
float3 t1=boxMaxrayDirInv-(rayOriginrayDirInv);
//Find the closest/farthest distance(component-wise).
float3 tSlabEntr=min(t0,t1);
float3 tSlabExit=max(t0,t1);
//Find the farthest entry and the nearest exit.
tEntr=Max3(tSlabEntr.x,tSlabEntr.y,tSlabEntr.z);
tExit=Min3(tSlabExit.x,tSlabExit.y,tSlabExit.z);
//Clamp to the range.
tEntr=max(tEntr,tMin);
tExit=min(tExit,tMax);
return tEntr<tExit;
}
v2f vert(appdata v)
{
v2f o;
o.positionCS=UnityObjectToClipPos(v.positionOS);
o.uv=v.uv;
o.positionOS=v.positionOS;
o.viewDirOS=ObjSpaceViewDir(v.positionOS);
o.normalOS=v.normal;
return o;
}
fixed4 frag(v2f i):SV_Target
{
fixed4 windowColor=tex2D(_WindowTex,i.uv);
float3 viewDirOS=normalize(i.viewDirOS);
float3 normalOS=i.normalOS;
float radius=0.5,posEntr,posExit;
float bias=2radius(1-_RoomDepth);
float3 boxMin=(float3)(-radius)+lerp((float3)0,bias*normalOS,Max3(normalOS.x,normalOS.y,normalOS.z));
float3 boxMax=(float3)(radius)+lerp(bias*normalOS,(float3)0,Max3(normalOS.x,normalOS.y,normalOS.z));
IntersectRayAABB(i.positionOS,-viewDirOS,boxMin,boxMax,1,2,posEntr,posExit);
float3 sampleDir=i.positionOS-posExit*viewDirOS;
sampleDir-=bias*normalOS;
fixed4 col=texCUBElod(_RoomTex,float4(sampleDir,0));
col.rgb+=windowColor.rgb*windowColor.a;
return col;
}
ENDCG
}
}
}