/* VertexDirt plug-in for Unity Copyright 2014-2015, Zoltan Farago, All rights reserved. */ #pragma strict #pragma downcast /* class: VertexDirt Main Vertex dirt class. VertexDirt is an Ambient Occlusion baking plug-in. */ static class VertexDirt { // private variables for mesh merging and vertex sampling private var v : Vector3[]; private var n : Vector3[]; private var c : Color32[]; /* variable: vertexSample public variable, but this is used by the baking. */ var vertexSample : VertexSample = new VertexSample(); /* integer: sampleWidth Vertical resolution of the sample. The default value of 64 should be fine in all circumstances. Lower values could cause visual artefacts. */ var sampleWidth : int = 64; /* integer: sampleHeight Horizontal resolution of the sample. The default value of 64 should be fine in all circumstances. Lower values could cause visual artefacts. */ var sampleHeight : int = 64; /* float: samplingBias The near clip plane of the sampling camera. */ var samplingBias : float = 0.001; /* float: samplingDistance The far clip plane of the sampling camera. */ var samplingDistance : float = 100; /* float: samplingAngle The FOV of the sampling camera. Please note that this value normally should be between 100-160. */ var samplingAngle : float = 100; /* boolean: edgeSmooth Enable to smoothing out hard edges. Basically just averages the normals of the vertices in the same position. */ var edgeSmooth : boolean = false; /* boolean: invertNormals Set true if you want to render the inside of the objects. Use this parameter enabled to render thickness. */ var invertNormals : boolean = false; /* float: edgeSmoothBias The range of edge smoothing. The normals of vertices closer than this value will be averaged. */ var edgeSmoothBias : float = 0.001; /* enumeration: skyMode Parameter for sampling camera backdrop. */ var skyMode : CameraClearFlags = CameraClearFlags.SolidColor; /* boolean: disableOccluders Set true if you only want to bake the background colour/cubeMap to the vertex colours. */ var disableOccluders : boolean = false; /* variable: skyColor The colour of the Sky. */ var skyColor : Color = Color.white; /* variable: globalOccluderColor Colour tint for the occluders. This property is designed for the VDOccluder shader. */ var globalOccluderColor : Color = Color.black; /* string: occluderShader The shader used on occluders during the bake. The default VD shader is "Hidden/VDOccluder AO" If this string is empty or the shader is not exist, then the occluder objects will use their original shaders. */ var occluderShader : String = VDSHADER.AMBIENTOCCLUSION; /* variable: skyCube The cubeMap of the sampling camera's sky. */ var skyCube : Material; /* variable: colorHandlerClass The component to store baked vertex color data. The component must derived from VDColorHandlerBase class. The default class is VDColorHandler. */ var colorHandlerClass : String = "VDColorHandler"; /* function: Dirt Main function for vertex baking. The Object[] array will be used. */ function Dirt(sels : Object[], passes : VDPass[]) { } function Dirt(sels : Object[]) { if (sels.Length > 0) { //vertex camera var camGO : GameObject = new GameObject("VDSamplerCamera"); var cam : Camera = camGO.AddComponent(Camera); camGO.AddComponent.(); RenderTexture.active = null; cam.renderingPath = RenderingPath.Forward; cam.pixelRect = Rect(0,0,sampleWidth, sampleHeight); cam.aspect = 1.0; cam.nearClipPlane = samplingBias; cam.farClipPlane = samplingDistance; cam.fieldOfView = Mathf.Clamp ( samplingAngle, 5, 160 ); cam.clearFlags = skyMode; cam.backgroundColor = skyColor; var tempSkybox : Material = RenderSettings.skybox; if (skyMode == CameraClearFlags.Skybox) { RenderSettings.skybox = skyCube; } UpdateShaderVariables(); cam.SetReplacementShader(Shader.Find(occluderShader), disableOccluders ? "ibl-only" : ""); for (var go : Transform in sels) { if (go.gameObject.GetComponent(MeshFilter)) { PrepareVertices(go); ResetColors(); SmoothVertices(); CalcColors(camGO, cam); ApplyColors(go); } } RenderSettings.skybox = tempSkybox; GameObject.DestroyImmediate(camGO); var handlers : VDColorHandlerBase[] = UnityEngine.Object.FindObjectsOfType(VDColorHandlerBase); for (var handler : VDColorHandlerBase in handlers) { if (handler.originalMesh && !handler.coloredMesh) { handler.coloredMesh = UnityEngine.Object.Instantiate(handler.originalMesh); handler.gameObject.GetComponent(MeshFilter).mesh = handler.coloredMesh; } } } } // function to update shader properties function UpdateShaderVariables() { Shader.SetGlobalColor("_VDOccluderColor", globalOccluderColor); } /* function: SetPreset Set preset for VertexDirt. Presets are for batch change common VertexDirt parameters. */ function SetPreset (v : VDPRESET) { switch (v) { case VDPRESET.AMBIENTOCCLUSION : invertNormals = false; skyMode = CameraClearFlags.SolidColor; occluderShader = VDSHADER.AMBIENTOCCLUSION; break; } } /* function: ResetSettings Reset every VertexDirt parameters to defaults. */ function ResetSettings () { sampleWidth = 64; sampleHeight = 64; samplingBias = 0.001; samplingDistance = 100; samplingAngle = 100; edgeSmooth = false; invertNormals = false; edgeSmoothBias = 0.001; skyMode = CameraClearFlags.SolidColor; disableOccluders = false; skyColor = Color.white; globalOccluderColor = Color.black; occluderShader = "Hidden/VDOccluder AO"; skyCube = null; } //private function CombineVertices(sel : Object[]) { private function PrepareVertices(go : Transform) { var vertexCount : int; v = new Vector3[0]; n = new Vector3[0]; c = new Color32[0]; if (!go.gameObject.GetComponent(VDColorHandlerBase)) { go.gameObject.AddComponent(VDColorHandler); } var v0 = go.gameObject.GetComponent(MeshFilter).sharedMesh.vertices; var n0 = go.gameObject.GetComponent(MeshFilter).sharedMesh.normals; for (var t : int = 0; t < v0.Length; t++) { v0[t] = go.TransformPoint(v0[t]); n0[t] = Vector3.Normalize(go.TransformDirection(n0[t])); } vertexCount += v0.Length; v = MergeVector3 (v, v0); n = MergeVector3 (n, n0); } function ResetColors() { c = new Color32[v.length]; } function ResetColors(Color32) { c = new Color32[v.length]; } private function SmoothVertices() { if (edgeSmooth) { for (var a = 0; a < v.length; a++) { for (var d = a; d < v.length; d++) { if (Vector3.Distance(v[a],v[d]) < edgeSmoothBias) { n[a] = Vector3.Normalize(n[a] + n[d]); n[d] = n[a]; } } } for (var k : int = 0; k