public void RenderToTexture(GameObject target, RenderTexture texture) { // 第一步:把渲染的物体渲染进纹理先 additionalCamera.cullingMask = 1; additionalCamera.targetTexture = texture; additionalCamera.targetTexture.Release(); additionalCamera.Render(); // 第二步:把这个纹理备份到_SceneTex temRT1 = RenderTexture.GetTemporary(texture.width, texture.height); Graphics.Blit(additionalCamera.targetTexture, temRT1); TargetMaterial.SetTexture("_SceneTex", temRT1); // 第三步:渲染物体的面积 additionalCamera.cullingMask = 1 << LayerMask.NameToLayer("PostEffect"); meshFilters = target.GetComponentsInChildren <MeshFilter>(); for (int j = 0; j < meshFilters.Length; j++) { Graphics.DrawMesh(meshFilters[j].sharedMesh, meshFilters[j].transform.localToWorldMatrix, OccupiedMaterial, LayerMask.NameToLayer("PostEffect"), additionalCamera); // 描绘选中物体的所占面积 } additionalCamera.Render(); // 第四步:实现描边效果 TargetMaterial.SetColor("_Color", outlineColor); TargetMaterial.SetInt("_Width", outlineWidth); TargetMaterial.SetInt("_Iterations", iterations); temRT2 = RenderTexture.GetTemporary(texture.width, texture.height); Graphics.Blit(additionalCamera.targetTexture, temRT2, TargetMaterial); Graphics.Blit(temRT2, additionalCamera.targetTexture); }
void OnRenderImage(RenderTexture source, RenderTexture destination) { if (TargetMaterial != null && drawOccupied != null && additionalCamera != null) { tempRT = RenderTexture.GetTemporary(source.width, source.height, 0); additionalCamera.targetTexture = tempRT; // 额外相机中使用shader,绘制出物体所占面积 additionalCamera.RenderWithShader(drawOccupied, ""); TargetMaterial.SetTexture("_SceneTex", source); TargetMaterial.SetColor("_Color", outlineColor); TargetMaterial.SetInt("_Width", outlineWidth); TargetMaterial.SetInt("_Iterations", iterations); // 使用描边混合材质实现描边效果 Graphics.Blit(tempRT, destination, TargetMaterial); tempRT.Release(); } else { Graphics.Blit(source, destination); } }
void OnRenderImage(RenderTexture source, RenderTexture destination) { if (TargetMaterial != null && drawOccupied != null && additionalCamera != null && targetsDic.Count != 0) { SetupAddtionalCamera(); tempRT = RenderTexture.GetTemporary(source.width, source.height, 0); additionalCamera.targetTexture = tempRT; //---这种方法对于蒙皮动画无效,但是效率较高 //foreach (var item in targetsDic.Values) //{ // foreach (var meshInfo in item) // { // for (int i = 0; i < meshInfo.Value.Count; i++) // { // for (int k = 0; k < meshInfo.Value[i].subMeshCount; k++) // { // Graphics.DrawMesh(meshInfo.Value[i], meshInfo.Key.localToWorldMatrix, // OccupiedMaterial, GetLayer(layerMask), additionalCamera, k); // 描绘选中物体的所占面积 // } // } // } //} //设置层级 foreach (var item in targetsDic.Values) { foreach (var transInfo in item) { transInfo.Key.gameObject.layer = GetLayer(layerMask); } } additionalCamera.SetReplacementShader(drawOccupied, ""); additionalCamera.Render(); // 需要调用渲染函数,才能及时把描绘物体渲染到纹理中 foreach (var item in targetsDic.Values) { foreach (var transInfo in item) { transInfo.Key.gameObject.layer = transInfo.Value; } } TargetMaterial.SetTexture("_SceneTex", source); TargetMaterial.SetColor("_Color", outlineColor); TargetMaterial.SetInt("_Width", outlineWidth); TargetMaterial.SetInt("_Iterations", iterations); TargetMaterial.SetFloat("_Gradient", gradient); Graphics.Blit(tempRT, TestRender); // 使用描边混合材质实现描边效果 Graphics.Blit(tempRT, destination, TargetMaterial); additionalCamera.targetTexture = null; RenderTexture.ReleaseTemporary(tempRT); } else { Graphics.Blit(source, destination); } }
void OnRenderImage(RenderTexture src, RenderTexture dest) { if (TargetMaterial != null) { Matrix4x4 frustumCorners = Matrix4x4.identity; float fov = TargetCamera.fieldOfView; float near = TargetCamera.nearClipPlane; float aspect = TargetCamera.aspect; float halfHeight = near * Mathf.Tan(fov * 0.5f * Mathf.Deg2Rad); Vector3 toRight = CameraTransform.right * halfHeight * aspect; Vector3 toTop = CameraTransform.up * halfHeight; Vector3 topLeft = CameraTransform.forward * near + toTop - toRight; float scale = topLeft.magnitude / near; topLeft.Normalize(); topLeft *= scale; Vector3 topRight = CameraTransform.forward * near + toRight + toTop; topRight.Normalize(); topRight *= scale; Vector3 bottomLeft = CameraTransform.forward * near - toTop - toRight; bottomLeft.Normalize(); bottomLeft *= scale; Vector3 bottomRight = CameraTransform.forward * near + toRight - toTop; bottomRight.Normalize(); bottomRight *= scale; frustumCorners.SetRow(0, bottomLeft); frustumCorners.SetRow(1, bottomRight); frustumCorners.SetRow(2, topRight); frustumCorners.SetRow(3, topLeft); TargetMaterial.SetMatrix("_FrustumCornersRay", frustumCorners); TargetMaterial.SetFloat("_FogDensity", fogDensity); TargetMaterial.SetColor("_FogColor", fogColor); TargetMaterial.SetFloat("_FogStart", fogStart); TargetMaterial.SetFloat("_FogEnd", fogEnd); TargetMaterial.SetTexture("_NoiseTex", noiseTexture); TargetMaterial.SetFloat("_FogXSpeed", fogXSpeed); TargetMaterial.SetFloat("_FogYSpeed", fogYSpeed); TargetMaterial.SetFloat("_NoiseAmount", noiseAmount); Graphics.Blit(src, dest, TargetMaterial); } else { Graphics.Blit(src, dest); } }
public Color backgroundColor = Color.white; // 背景色 void OnRenderImage(RenderTexture src, RenderTexture dest) { if (TargetMaterial != null) { TargetMaterial.SetFloat("_EdgeOnly", edgesOnly); TargetMaterial.SetColor("_EdgeColor", edgeColor); TargetMaterial.SetColor("_BackgroundColor", backgroundColor); Graphics.Blit(src, dest, TargetMaterial); } else { Graphics.Blit(src, dest); } }
[ImageEffectOpaque] // 不透明物体渲染完后执行(不影响透明物体) void OnRenderImage(RenderTexture src, RenderTexture dest) { if (TargetMaterial != null) { TargetMaterial.SetFloat("_EdgeOnly", edgesOnly); TargetMaterial.SetColor("_EdgeColor", edgeColor); TargetMaterial.SetColor("_BackgroundColor", backgroundColor); TargetMaterial.SetFloat("_SampleDistance", sampleDistance); TargetMaterial.SetVector("_Sensitivity", new Vector4(sensitivityNormals, sensitivityDepth, 0.0f, 0.0f)); Graphics.Blit(src, dest, TargetMaterial); } else { Graphics.Blit(src, dest); } }
private void Update() { if (TargetMaterial != null && _endTime > Time.time) { _color.a = _alphaCurve.Evaluate((Time.time - _startTime) / (_durationTime)); TargetMaterial.SetColor("_Color", _color); TargetMaterial.SetFloat("_Width", _width); if (_meshFilters == null) { _meshFilters = gameObject.GetComponentsInChildren <MeshFilter>(); } for (int j = 0; j < _meshFilters.Length; j++) { Graphics.DrawMesh(_meshFilters[j].mesh, _meshFilters[j].transform.localToWorldMatrix, TargetMaterial, 0); // 对选中物体再次渲染。 } } }
private void Update() { if (isOpen && TargetMaterial != null && meshFilter != null) { if (Timer.Duration != blinkTime) { Timer.Reset(blinkTime); } // 颜色透明度变化:指定事件的周期,根据曲线变化透明度。 TargetMaterial.SetColor("_Color", new Color(color.r, color.g, color.b, maxColorAlpha * blinkCurve.Evaluate(Mathf.PingPong(Timer.GetPercent() * 2F, 1)))); for (int k = 0; k < meshFilter.sharedMesh.subMeshCount; k++) { Graphics.DrawMesh(meshFilter.sharedMesh, meshFilter.transform.localToWorldMatrix, TargetMaterial, 0, null, k); } } }
void OnRenderImage(RenderTexture source, RenderTexture destination) { if (TargetMaterial != null && drawOccupied != null && additionalCamera != null && targets != null) { SetupAddtionalCamera(); tempRT = RenderTexture.GetTemporary(source.width, source.height, 0); additionalCamera.targetTexture = tempRT; for (int i = 0; i < targets.Length; i++) { if (targets[i] == null) { continue; } meshFilters = targets[i].GetComponentsInChildren <MeshFilter>(); for (int j = 0; j < meshFilters.Length; j++) { if ((MainCamera.cullingMask & (1 << meshFilters[j].gameObject.layer)) != 0) // 把主相机没渲染的也不加入渲染队列 { for (int k = 0; k < meshFilters[j].sharedMesh.subMeshCount; k++) { Graphics.DrawMesh(meshFilters[j].sharedMesh, meshFilters[j].transform.localToWorldMatrix, OccupiedMaterial, LayerMask.NameToLayer("PostEffect"), additionalCamera, k); // 描绘选中物体的所占面积 } } } } additionalCamera.Render(); // 需要调用渲染函数,才能及时把描绘物体渲染到纹理中 TargetMaterial.SetTexture("_SceneTex", source); TargetMaterial.SetColor("_Color", outlineColor); TargetMaterial.SetInt("_Width", outlineWidth); TargetMaterial.SetInt("_Iterations", iterations); TargetMaterial.SetFloat("_Gradient", gradient); // 使用描边混合材质实现描边效果 Graphics.Blit(tempRT, destination, TargetMaterial); tempRT.Release(); } else { Graphics.Blit(source, destination); } }
// 也可以在OnPostRender()中更新 private void Update() { if (TargetMaterial != null && targets != null) { TargetMaterial.SetFloat("_Width", width); TargetMaterial.SetColor("_Color", color); for (int i = 0; i < targets.Length; i++) { if (targets[i] == null) { continue; } meshFilters = targets[i].GetComponentsInChildren <MeshFilter>(); for (int j = 0; j < meshFilters.Length; j++) { Graphics.DrawMesh(meshFilters[j].sharedMesh, meshFilters[j].transform.localToWorldMatrix, TargetMaterial, 0); // 对选中物体再次渲染。 } } } }
private void Update() { if (TargetMaterial != null && targets != null) { TargetMaterial.SetFloat("_Width", width); TargetMaterial.SetColor("_Color", color); for (int i = 0; i < targets.Length; i++) { if (targets[i] == null) { continue; } meshFilters = targets[i].GetComponentsInChildren <MeshFilter>(); for (int j = 0; j < meshFilters.Length; j++) { for (int k = 0; k < meshFilters[j].sharedMesh.subMeshCount; k++) // 每个网格还有子网格 { Graphics.DrawMesh(meshFilters[j].sharedMesh, meshFilters[j].transform.localToWorldMatrix, TargetMaterial, 0, MainCamera, k); } } } } }
void Update() { var pInfos = shaderValues.GetType().GetFields(); foreach (var p in pInfos) { if (Attribute.IsDefined(p, typeof(ShaderValue))) { if (p.FieldType == typeof(Color)) { foreach (Material TargetMaterial in TargetMaterials) { TargetMaterial.SetColor(p.Name, (Color)p.GetValue(shaderValues)); } } else if (p.FieldType == typeof(Color[])) { foreach (Material TargetMaterial in TargetMaterials) { TargetMaterial.SetColorArray(p.Name, (Color[])p.GetValue(shaderValues)); } } else if (p.FieldType == typeof(List <Color>)) { foreach (Material TargetMaterial in TargetMaterials) { TargetMaterial.SetColorArray(p.Name, (List <Color>)p.GetValue(shaderValues)); } } else if (p.FieldType == typeof(float)) { foreach (Material TargetMaterial in TargetMaterials) { TargetMaterial.SetFloat(p.Name, (float)p.GetValue(shaderValues)); } } else if (p.FieldType == typeof(float[])) { foreach (Material TargetMaterial in TargetMaterials) { TargetMaterial.SetFloatArray(p.Name, (float[])p.GetValue(shaderValues)); } } else if (p.FieldType == typeof(List <float>)) { foreach (Material TargetMaterial in TargetMaterials) { TargetMaterial.SetFloatArray(p.Name, (List <float>)p.GetValue(shaderValues)); } } else if (p.FieldType == typeof(int)) { foreach (Material TargetMaterial in TargetMaterials) { TargetMaterial.SetInt(p.Name, (int)p.GetValue(shaderValues)); } } else if (p.FieldType == typeof(bool)) { foreach (Material TargetMaterial in TargetMaterials) { TargetMaterial.SetInt(p.Name, (bool)p.GetValue(shaderValues) ? 1 : 0); } } else if (p.FieldType == typeof(Matrix4x4)) { foreach (Material TargetMaterial in TargetMaterials) { TargetMaterial.SetMatrix(p.Name, (Matrix4x4)p.GetValue(shaderValues)); } } else if (p.FieldType == typeof(Matrix4x4[])) { foreach (Material TargetMaterial in TargetMaterials) { TargetMaterial.SetMatrixArray(p.Name, (Matrix4x4[])p.GetValue(shaderValues)); } } else if (p.FieldType == typeof(List <Matrix4x4>)) { foreach (Material TargetMaterial in TargetMaterials) { TargetMaterial.SetMatrixArray(p.Name, (List <Matrix4x4>)p.GetValue(shaderValues)); } } else if (p.FieldType == typeof(string)) { foreach (Material TargetMaterial in TargetMaterials) { TargetMaterial.SetOverrideTag(p.Name, (string)p.GetValue(shaderValues)); } } else if (p.FieldType == typeof(Texture2D)) { foreach (Material TargetMaterial in TargetMaterials) { TargetMaterial.SetTexture(p.Name, (Texture2D)p.GetValue(shaderValues)); } //a vector2 can be a vector, or a material offset or scale //TODO: test this! } else if (p.FieldType == typeof(Vector2)) { if (Attribute.IsDefined(p, typeof(TextureOffset))) { foreach (Material TargetMaterial in TargetMaterials) { TargetMaterial.SetTextureOffset(((TextureOffset)Attribute.GetCustomAttribute(p, typeof(TextureOffset))).Name, (Vector2)p.GetValue(shaderValues)); } } else if (Attribute.IsDefined(p, typeof(TextureScale))) { foreach (Material TargetMaterial in TargetMaterials) { TargetMaterial.SetTextureOffset(((TextureScale)Attribute.GetCustomAttribute(p, typeof(TextureScale))).Name, (Vector2)p.GetValue(shaderValues)); } } else { foreach (Material TargetMaterial in TargetMaterials) { TargetMaterial.SetVector(p.Name, (Vector2)p.GetValue(shaderValues)); } } } else if (p.FieldType == typeof(Vector3)) { foreach (Material TargetMaterial in TargetMaterials) { TargetMaterial.SetVector(p.Name, (Vector3)p.GetValue(shaderValues)); } } else if (p.FieldType == typeof(Vector4)) { foreach (Material TargetMaterial in TargetMaterials) { TargetMaterial.SetVector(p.Name, (Vector4)p.GetValue(shaderValues)); } } else if (p.FieldType == typeof(Vector4[])) { foreach (Material TargetMaterial in TargetMaterials) { TargetMaterial.SetVectorArray(p.Name, (Vector4[])p.GetValue(shaderValues)); } } else if (p.FieldType == typeof(List <Vector4>)) { foreach (Material TargetMaterial in TargetMaterials) { TargetMaterial.SetVectorArray(p.Name, (List <Vector4>)p.GetValue(shaderValues)); } } } } }