Beispiel #1
0
        public void Init(DataVolume data, int filledValue, float3 cellSize, int cellResolution, UVMode uvMode = UVMode.NoScaling, float3 posOffset = default(float3))
        {
            this.data   = data;
            dataExtents = new Extents(data.XLength, data.YLength, data.ZLength);

            this.filledValue = filledValue;

            resolution = Mathf.Clamp(cellResolution, MinResolution, MaxResolution);
            if (resolution != cellResolution)
            {
                Debug.LogWarningFormat("cell resolution has been clamped ({0} -> {1})", cellResolution, resolution);
            }

            this.cellSize = cellSize;
            if (this.cellSize.x <= 0f || this.cellSize.y <= 0f || this.cellSize.z <= 0f)
            {
                this.cellSize = new float3(1, 1, 1);
                Debug.LogWarning("Cell size must be greater than zero!");
            }

            this.uvMode         = uvMode;
            this.positionOffset = posOffset;

            InitMeshArrayLengths();

            HeightMap = null;

            Inited();
        }
        private void SetupSkySphere()
        {
#if UNITY_EDITOR
            Camera = (Camera == null ? (Camera.main == null ? Camera.current : Camera.main) : Camera);

            if (Resolution != lastResolution)
            {
                lastResolution = Resolution;
                DestroyMesh();
            }
            if (UVMode != lastUVMode)
            {
                lastUVMode = UVMode;
                DestroyMesh();
            }
            Mesh mesh = meshFilter.sharedMesh;
            if (mesh == null)
            {
                meshFilter.sharedMesh = WeatherMakerSphereCreator.Create(Resolution, UVMode);
            }
#endif

            float farPlane = FarClipPlaneMultiplier * Camera.farClipPlane;
            float yOffset  = farPlane * YOffsetMultiplier;
            gameObject.transform.position = Camera.transform.position + new Vector3(0.0f, yOffset, 0.0f);
            float scale = farPlane * ((Camera.farClipPlane - Mathf.Abs(yOffset)) / Camera.farClipPlane);
            gameObject.transform.localScale = new Vector3(scale, scale, scale);
        }
        private static void CreateInternal(ref MeshData m, int resolution, UVMode uvMode)
        {
            int v       = 0;
            int vBottom = 0;
            int t       = 0;

            for (int i = 0; i < 4; i++)
            {
                m.Vertices[v++] = Vector3.down;
            }

            for (int i = 1; i <= resolution; i++)
            {
                float   progress = (float)i / resolution;
                Vector3 from, to;
                m.Vertices[v++] = to = Vector3.Lerp(Vector3.down, Vector3.forward, progress);
                for (int d = 0; d < 4; d++)
                {
                    from     = to;
                    to       = Vector3.Lerp(Vector3.down, directions[d], progress);
                    t        = CreateLowerStrip(i, v, vBottom, t, ref m);
                    v        = CreateVertexLine(from, to, i, v, ref m);
                    vBottom += i > 1 ? (i - 1) : 1;
                }
                vBottom = v - 1 - i * 4;
            }

            for (int i = resolution - 1; i >= 1; i--)
            {
                float   progress = (float)i / resolution;
                Vector3 from, to;
                m.Vertices[v++] = to = Vector3.Lerp(Vector3.up, Vector3.forward, progress);
                for (int d = 0; d < 4; d++)
                {
                    from     = to;
                    to       = Vector3.Lerp(Vector3.up, directions[d], progress);
                    t        = CreateUpperStrip(i, v, vBottom, t, ref m);
                    v        = CreateVertexLine(from, to, i, v, ref m);
                    vBottom += i + 1;
                }
                vBottom = v - 1 - i * 4;
            }

            for (int i = 0; i < 4; i++)
            {
                m.Triangles[t++] = vBottom;
                m.Triangles[t++] = v;
                m.Triangles[t++] = ++vBottom;
                m.Vertices[v++]  = Vector3.up;
            }

            CreateNormals(ref m);
            CreateTangents(ref m);
            FixVertices(ref m); // must be done before CreateUV is called
            CreateUV(ref m, uvMode);

            m.Apply();
        }
 public override void DrawFeatureGUI(Material mat)
 {
     perfMode           = (PerformanceMode)EditorGUILayout.EnumPopup(CShaderPerfMode, perfMode);
     maxTextureCount    = (MaxTextureCount)EditorGUILayout.EnumPopup(CMaxTexCount, maxTextureCount);
     lightingMode       = (LightingMode)EditorGUILayout.EnumPopup(CLightingMode, lightingMode);
     uvMode             = (UVMode)EditorGUILayout.EnumPopup(CUVMode, uvMode);
     shaderModel        = (ShaderModel)EditorGUILayout.EnumPopup(CForceShaderModel, shaderModel);
     samplerMode        = (SamplerMode)EditorGUILayout.EnumPopup(CSamplerMode, samplerMode);
     emissiveArray      = EditorGUILayout.Toggle(CEmissiveArray, emissiveArray);
     disableHeightBlend = EditorGUILayout.Toggle(CDisableHeightBlend, disableHeightBlend);
     //debugOutput = (DebugOutput)EditorGUILayout.EnumPopup("Debug", debugOutput);
 }
Beispiel #5
0
        public override void DrawFeatureGUI(Material mat)
        {
            bool isSurfaceShader = mat.IsKeywordEnabled("_MSRENDERLOOP_SURFACESHADER");

            perfMode        = (PerformanceMode)EditorGUILayout.EnumPopup(CShaderPerfMode, perfMode);
            maxTextureCount = (MaxTextureCount)EditorGUILayout.EnumPopup(CMaxTexCount, maxTextureCount);
            if (isSurfaceShader)
            {
                lightingMode = (LightingMode)EditorGUILayout.EnumPopup(CLightingMode, lightingMode);
            }
            uvMode             = (UVMode)EditorGUILayout.EnumPopup(CUVMode, uvMode);
            shaderModel        = (ShaderModel)EditorGUILayout.EnumPopup(CForceShaderModel, shaderModel);
            samplerMode        = (SamplerMode)EditorGUILayout.EnumPopup(CSamplerMode, samplerMode);
            emissiveArray      = EditorGUILayout.Toggle(CEmissiveArray, emissiveArray);
            disableHeightBlend = EditorGUILayout.Toggle(CDisableHeightBlend, disableHeightBlend);
            //debugOutput = (DebugOutput)EditorGUILayout.EnumPopup("Debug", debugOutput);
        }
Beispiel #6
0
        protected virtual void Update()
        {
#if UNITY_EDITOR
            if (Resolution != lastResolution)
            {
                lastResolution = Resolution;
                DestroyMesh();
            }
            if (UVMode != lastUVMode)
            {
                lastUVMode = UVMode;
                DestroyMesh();
            }
            Mesh mesh = MeshFilter.sharedMesh;
            if (mesh == null)
            {
                MeshFilter.sharedMesh = WeatherMakerSphereCreator.Create(gameObject.name, Resolution, UVMode);
            }
#endif
        }
      public override void DrawFeatureGUI(MicroSplatKeywords keywords)
      {
         bool isSurfaceShader = keywords.IsKeywordEnabled("_MSRENDERLOOP_SURFACESHADER");
         packMode = (TextureArrayConfig.PackingMode)EditorGUILayout.EnumPopup(CPackMode, packMode);
         perfMode = (PerformanceMode)EditorGUILayout.EnumPopup(CShaderPerfMode, perfMode);
         maxTextureCount = (MaxTextureCount)EditorGUILayout.EnumPopup(CMaxTexCount, maxTextureCount);
         if (isSurfaceShader)
         {
            lightingMode = (LightingMode)EditorGUILayout.EnumPopup(CLightingMode, lightingMode);
         }
         uvMode = (UVMode)EditorGUILayout.EnumPopup(CUVMode, uvMode);
         shaderModel = (ShaderModel)EditorGUILayout.EnumPopup(CForceShaderModel, shaderModel);
         samplerMode = (SamplerMode)EditorGUILayout.EnumPopup(CSamplerMode, samplerMode);
         emissiveArray = EditorGUILayout.Toggle(CEmissiveArray, emissiveArray);
         perPixelNormal = EditorGUILayout.Toggle(CPerPixelNormal, perPixelNormal);
         disableHeightBlend = EditorGUILayout.Toggle(CDisableHeightBlend, disableHeightBlend);
         disableNormals = EditorGUILayout.Toggle(CDisableNormals, disableNormals);
         useCustomSplatMaps = EditorGUILayout.Toggle(CUseCustomSplatMaps, useCustomSplatMaps);

         debugOutput = (DebugOutput)EditorGUILayout.EnumPopup("Debug", debugOutput);
      }
        /// <summary>
        /// Create a sphere mesh
        /// </summary>
        /// <param name="name">Mesh name</param>
        /// <param name="resolution">Resolution</param>
        /// <param name="uvMode">UV mode</param>
        /// <returns>Sphere mesh</returns>
        public static Mesh Create(string name, int resolution, UVMode uvMode)
        {
            if (resolution < 0)
            {
                resolution = 0;
                Debug.LogWarning("Sphere subdivisions increased to minimum, which is 0.");
            }
            else if (resolution > 6)
            {
                resolution = 6;
                Debug.LogWarning("Sphere subdivisions decreased to maximum, which is 6.");
            }

            int trianglesCapacity = (1 << (resolution * 2 + 3)) * 3;

            resolution = 1 << resolution;
            int      verticesCapacity = (resolution + 1) * (resolution + 1) * 4 - (resolution * 2 - 1) * 3;
            MeshData m = CreateMesh(name, verticesCapacity, trianglesCapacity, uvMode.ToString());

            CreateInternal(ref m, resolution, uvMode);

            return(m.Mesh);
        }
Beispiel #9
0
        public override void DrawFeatureGUI(MicroSplatKeywords keywords)
        {
            bool isSurfaceShader = keywords.IsKeywordEnabled("_MSRENDERLOOP_SURFACESHADER");

         #if __MICROSPLAT_MESHTERRAIN__ || __MICROSPLAT_MESH__
            workflow = (Workflow)EditorGUILayout.EnumPopup(CWorkflow, workflow);
         #endif
            packMode        = (TextureArrayConfig.PackingMode)EditorGUILayout.EnumPopup(CPackMode, packMode);
            perfMode        = (PerformanceMode)EditorGUILayout.EnumPopup(CShaderPerfMode, perfMode);
            maxTextureCount = (MaxTextureCount)EditorGUILayout.EnumPopup(CMaxTexCount, maxTextureCount);
            if (isSurfaceShader)
            {
                lightingMode = (LightingMode)EditorGUILayout.EnumPopup(CLightingMode, lightingMode);
                if (lightingMode != LightingMode.Automatic && lightingMode != LightingMode.StandardShaderNoSheen)
                {
                    EditorGUILayout.HelpBox("Shader is forced to run in forward rendering due to lighting mode", MessageType.Info);
                }
            }
            uvMode             = (UVMode)EditorGUILayout.EnumPopup(CUVMode, uvMode);
            shaderModel        = (ShaderModel)EditorGUILayout.EnumPopup(CForceShaderModel, shaderModel);
            samplerMode        = (SamplerMode)EditorGUILayout.EnumPopup(CSamplerMode, samplerMode);
            emissiveArray      = EditorGUILayout.Toggle(CEmissiveArray, emissiveArray);
            perPixelNormal     = EditorGUILayout.Toggle(CPerPixelNormal, perPixelNormal);
            disableHeightBlend = EditorGUILayout.Toggle(CDisableHeightBlend, disableHeightBlend);
            disableNormals     = EditorGUILayout.Toggle(CDisableNormals, disableNormals);
         #if __MICROSPLAT_MESHTERRAIN__ || __MICROSPLAT_MESH__
            if (workflow == Workflow.UnityTerrain)
            {
                useCustomSplatMaps = EditorGUILayout.Toggle(CUseCustomSplatMaps, useCustomSplatMaps);
            }
         #else
            useCustomSplatMaps = EditorGUILayout.Toggle(CUseCustomSplatMaps, useCustomSplatMaps);
         #endif

            debugOutput = (DebugOutput)EditorGUILayout.EnumPopup("Debug", debugOutput);
        }
        public override void Unpack(string[] keywords)
        {
            if (HasFeature(keywords, DefineFeature._MAX2LAYER))
            {
                perfMode = PerformanceMode.Fastest;
            }
            else if (HasFeature(keywords, DefineFeature._MAX3LAYER))
            {
                perfMode = PerformanceMode.Balanced;
            }
            else
            {
                perfMode = PerformanceMode.BestQuality;
            }

            useCustomSplatMaps = (HasFeature(keywords, DefineFeature._CUSTOMSPLATTEXTURES));

            packMode = HasFeature(keywords, DefineFeature._PACKINGHQ) ? TextureArrayConfig.PackingMode.Quality : TextureArrayConfig.PackingMode.Fastest;

            emissiveArray = HasFeature(keywords, DefineFeature._USEEMISSIVEMETAL);
            samplerMode   = SamplerMode.Default;
            if (HasFeature(keywords, DefineFeature._USELODMIP))
            {
                samplerMode = SamplerMode.LODSampler;
            }
            else if (HasFeature(keywords, DefineFeature._USEGRADMIP))
            {
                samplerMode = SamplerMode.GradientSampler;
            }
            // force gradient sampling for stochastic mode
            if (samplerMode == SamplerMode.Default && System.Array.Exists(keywords, e => e == "_STOCHASTIC"))
            {
                samplerMode = SamplerMode.GradientSampler;
            }


            uvMode = HasFeature(keywords, DefineFeature._WORLDUV) ? UVMode.WorldSpace : UVMode.UV;

            /*
             * if (HasFeature(keywords, DefineFeature._SPHEREUVS))
             * {
             * uvMode = UVMode.SphereUVs;
             * }
             */

            perTexHeightOffset   = HasFeature(keywords, DefineFeature._PERTEXHEIGHTOFFSET);
            perTexHeightContrast = HasFeature(keywords, DefineFeature._PERTEXHEIGHTCONTRAST);

            if (HasFeature(keywords, DefineFeature._MAX4TEXTURES))
            {
                maxTextureCount = MaxTextureCount.Four;
            }
            else if (HasFeature(keywords, DefineFeature._MAX8TEXTURES))
            {
                maxTextureCount = MaxTextureCount.Eight;
            }
            else if (HasFeature(keywords, DefineFeature._MAX12TEXTURES))
            {
                maxTextureCount = MaxTextureCount.Twelve;
            }
            else if (HasFeature(keywords, DefineFeature._MAX20TEXTURES))
            {
                maxTextureCount = MaxTextureCount.Twenty;
            }
            else if (HasFeature(keywords, DefineFeature._MAX24TEXTURES))
            {
                maxTextureCount = MaxTextureCount.TwentyFour;
            }
            else if (HasFeature(keywords, DefineFeature._MAX28TEXTURES))
            {
                maxTextureCount = MaxTextureCount.TwentyEight;
            }
            else if (HasFeature(keywords, DefineFeature._MAX32TEXTURES))
            {
                maxTextureCount = MaxTextureCount.ThirtyTwo;
            }
            else
            {
                maxTextureCount = MaxTextureCount.Sixteen;
            }

            disableHeightBlend = HasFeature(keywords, DefineFeature._DISABLEHEIGHTBLENDING);

            lightingMode = LightingMode.Automatic;
            if (HasFeature(keywords, DefineFeature._BDRF1))
            {
                lightingMode = LightingMode.StandardShader;
            }
            else if (HasFeature(keywords, DefineFeature._BDRF2))
            {
                lightingMode = LightingMode.Simplified;
            }
            else if (HasFeature(keywords, DefineFeature._BDRF3))
            {
                lightingMode = LightingMode.BlinnPhong;
            }
            else if (HasFeature(keywords, DefineFeature._BDRFLAMBERT))
            {
                lightingMode = LightingMode.Lambert;
            }

            perTexUVScale        = (HasFeature(keywords, DefineFeature._PERTEXUVSCALEOFFSET));
            perTexInterpContrast = HasFeature(keywords, DefineFeature._PERTEXINTERPCONTRAST);
            perTexBrightness     = HasFeature(keywords, DefineFeature._PERTEXBRIGHTNESS);
            perTexContrast       = HasFeature(keywords, DefineFeature._PERTEXCONTRAST);
            perTexAOStr          = (HasFeature(keywords, DefineFeature._PERTEXAOSTR));
            perTexMetallic       = (HasFeature(keywords, DefineFeature._PERTEXMETALLIC));
            perTexNormStr        = (HasFeature(keywords, DefineFeature._PERTEXNORMSTR));
            perTexSmoothStr      = (HasFeature(keywords, DefineFeature._PERTEXSMOOTHSTR));
            perTexTint           = (HasFeature(keywords, DefineFeature._PERTEXTINT));

            shaderModel = ShaderModel.Automatic;
            if (HasFeature(keywords, DefineFeature._FORCEMODEL46))
            {
                shaderModel = ShaderModel.Force46;
            }
            if (HasFeature(keywords, DefineFeature._FORCEMODEL50))
            {
                shaderModel = ShaderModel.Force50;
            }

            debugOutput = DebugOutput.None;
            if (HasFeature(keywords, DefineFeature._DEBUG_OUTPUT_ALBEDO))
            {
                debugOutput = DebugOutput.Albedo;
            }
            else if (HasFeature(keywords, DefineFeature._DEBUG_OUTPUT_HEIGHT))
            {
                debugOutput = DebugOutput.Height;
            }
            else if (HasFeature(keywords, DefineFeature._DEBUG_OUTPUT_NORMAL))
            {
                debugOutput = DebugOutput.Normal;
            }
            else if (HasFeature(keywords, DefineFeature._DEBUG_OUTPUT_SMOOTHNESS))
            {
                debugOutput = DebugOutput.Smoothness;
            }
            else if (HasFeature(keywords, DefineFeature._DEBUG_OUTPUT_METAL))
            {
                debugOutput = DebugOutput.Metallic;
            }
            else if (HasFeature(keywords, DefineFeature._DEBUG_OUTPUT_AO))
            {
                debugOutput = DebugOutput.AO;
            }
            else if (HasFeature(keywords, DefineFeature._DEBUG_OUTPUT_EMISSION))
            {
                debugOutput = DebugOutput.Emission;
            }
        }
Beispiel #11
0
        public override void Unpack(string[] keywords)
        {
            if (HasFeature(keywords, DefineFeature._MAX2LAYER))
            {
                perfMode = PerformanceMode.Fastest;
            }
            else if (HasFeature(keywords, DefineFeature._MAX3LAYER))
            {
                perfMode = PerformanceMode.Balanced;
            }
            else
            {
                perfMode = PerformanceMode.BestQuality;
            }

            emissiveArray = HasFeature(keywords, DefineFeature._USEEMISSIVEMETAL);
            samplerMode   = SamplerMode.Default;
            if (HasFeature(keywords, DefineFeature._USELODMIP))
            {
                samplerMode = SamplerMode.LODSampler;
            }
            else if (HasFeature(keywords, DefineFeature._USEGRADMIP))
            {
                samplerMode = SamplerMode.GradientSampler;
            }

            uvMode = HasFeature(keywords, DefineFeature._WORLDUV) ? UVMode.WorldSpace : UVMode.UV;



            if (HasFeature(keywords, DefineFeature._MAX4TEXTURES))
            {
                maxTextureCount = MaxTextureCount.Four;
            }
            else if (HasFeature(keywords, DefineFeature._MAX8TEXTURES))
            {
                maxTextureCount = MaxTextureCount.Eight;
            }
            else if (HasFeature(keywords, DefineFeature._MAX12TEXTURES))
            {
                maxTextureCount = MaxTextureCount.Twelve;
            }
            else if (HasFeature(keywords, DefineFeature._MAX20TEXTURES))
            {
                maxTextureCount = MaxTextureCount.Twenty;
            }
            else if (HasFeature(keywords, DefineFeature._MAX24TEXTURES))
            {
                maxTextureCount = MaxTextureCount.TwentyFour;
            }
            else if (HasFeature(keywords, DefineFeature._MAX28TEXTURES))
            {
                maxTextureCount = MaxTextureCount.TwentyEight;
            }
            else if (HasFeature(keywords, DefineFeature._MAX32TEXTURES))
            {
                maxTextureCount = MaxTextureCount.ThirtyTwo;
            }
            else
            {
                maxTextureCount = MaxTextureCount.Sixteen;
            }

            disableHeightBlend = HasFeature(keywords, DefineFeature._DISABLEHEIGHTBLENDING);

            lightingMode = LightingMode.Automatic;
            if (HasFeature(keywords, DefineFeature._BDRF1))
            {
                lightingMode = LightingMode.StandardShader;
            }
            else if (HasFeature(keywords, DefineFeature._BDRF2))
            {
                lightingMode = LightingMode.Simplified;
            }
            else if (HasFeature(keywords, DefineFeature._BDRF3))
            {
                lightingMode = LightingMode.BlinnPhong;
            }

            perTexUVScale        = (HasFeature(keywords, DefineFeature._PERTEXUVSCALEOFFSET));
            perTexInterpContrast = HasFeature(keywords, DefineFeature._PERTEXINTERPCONTRAST);
            perTexBrightness     = HasFeature(keywords, DefineFeature._PERTEXBRIGHTNESS);
            perTexContrast       = HasFeature(keywords, DefineFeature._PERTEXCONTRAST);
            perTexAOStr          = (HasFeature(keywords, DefineFeature._PERTEXAOSTR));
            perTexMetallic       = (HasFeature(keywords, DefineFeature._PERTEXMETALLIC));
            perTexNormStr        = (HasFeature(keywords, DefineFeature._PERTEXNORMSTR));
            perTexSmoothStr      = (HasFeature(keywords, DefineFeature._PERTEXSMOOTHSTR));
            perTexTint           = (HasFeature(keywords, DefineFeature._PERTEXTINT));

            shaderModel = ShaderModel.Automatic;
            if (HasFeature(keywords, DefineFeature._FORCEMODEL46))
            {
                shaderModel = ShaderModel.Force46;
            }
            if (HasFeature(keywords, DefineFeature._FORCEMODEL50))
            {
                shaderModel = ShaderModel.Force50;
            }

            debugOutput = DebugOutput.None;
            if (HasFeature(keywords, DefineFeature._DEBUG_OUTPUT_ALBEDO))
            {
                debugOutput = DebugOutput.Albedo;
            }
            else if (HasFeature(keywords, DefineFeature._DEBUG_OUTPUT_HEIGHT))
            {
                debugOutput = DebugOutput.Height;
            }
            else if (HasFeature(keywords, DefineFeature._DEBUG_OUTPUT_NORMAL))
            {
                debugOutput = DebugOutput.Normal;
            }
            else if (HasFeature(keywords, DefineFeature._DEBUG_OUTPUT_SMOOTHNESS))
            {
                debugOutput = DebugOutput.Smoothness;
            }
            else if (HasFeature(keywords, DefineFeature._DEBUG_OUTPUT_METAL))
            {
                debugOutput = DebugOutput.Metallic;
            }
            else if (HasFeature(keywords, DefineFeature._DEBUG_OUTPUT_AO))
            {
                debugOutput = DebugOutput.AO;
            }
            else if (HasFeature(keywords, DefineFeature._DEBUG_OUTPUT_EMISSION))
            {
                debugOutput = DebugOutput.Emission;
            }
        }
Beispiel #12
0
        public override void Unpack(string[] keywords)
        {
            if (HasFeature(keywords, DefineFeature._MAX2LAYER))
            {
                perfMode = PerformanceMode.Fastest;
            }
            else if (HasFeature(keywords, DefineFeature._MAX3LAYER))
            {
                perfMode = PerformanceMode.Balanced;
            }
            else
            {
                perfMode = PerformanceMode.BestQuality;
            }

            useCustomSplatMaps = (HasFeature(keywords, DefineFeature._CUSTOMSPLATTEXTURES));

         #if __MICROSPLAT_MESH__ || __MICROSPLAT_MESHTERRAIN__
            workflow = Workflow.UnityTerrain;
         #endif

         #if __MICROSPLAT_MESH__
            if (HasFeature(keywords, DefineFeature._MICROMESH))
            {
                workflow           = Workflow.Mesh;
                useCustomSplatMaps = false;
            }
         #endif
         #if __MICROSPLAT_MESHTERRAIN__
            if (HasFeature(keywords, DefineFeature._MICROMESHTERRAIN))
            {
                workflow           = Workflow.MeshTerrain;
                useCustomSplatMaps = false;
            }
         #endif

            disableNormals = (HasFeature(keywords, DefineFeature._NONOMALMAP));

            packMode = HasFeature(keywords, DefineFeature._PACKINGHQ) ? TextureArrayConfig.PackingMode.Quality : TextureArrayConfig.PackingMode.Fastest;

            emissiveArray = HasFeature(keywords, DefineFeature._USEEMISSIVEMETAL);
            samplerMode   = SamplerMode.Default;
            if (HasFeature(keywords, DefineFeature._USELODMIP))
            {
                samplerMode = SamplerMode.LODSampler;
            }
            else if (HasFeature(keywords, DefineFeature._USEGRADMIP))
            {
                samplerMode = SamplerMode.GradientSampler;
            }
            // force gradient sampling for stochastic mode
            if (samplerMode == SamplerMode.Default && System.Array.Exists(keywords, e => e == "_STOCHASTIC"))
            {
                samplerMode = SamplerMode.GradientSampler;
            }

            perPixelNormal = HasFeature(keywords, DefineFeature._PERPIXNORMAL);
            uvMode         = HasFeature(keywords, DefineFeature._WORLDUV) ? UVMode.WorldSpace : UVMode.UV;

            perTexHeightOffset   = HasFeature(keywords, DefineFeature._PERTEXHEIGHTOFFSET);
            perTexHeightContrast = HasFeature(keywords, DefineFeature._PERTEXHEIGHTCONTRAST);

            if (HasFeature(keywords, DefineFeature._MAX4TEXTURES))
            {
                maxTextureCount = MaxTextureCount.Four;
            }
            else if (HasFeature(keywords, DefineFeature._MAX8TEXTURES))
            {
                maxTextureCount = MaxTextureCount.Eight;
            }
            else if (HasFeature(keywords, DefineFeature._MAX12TEXTURES))
            {
                maxTextureCount = MaxTextureCount.Twelve;
            }
            else if (HasFeature(keywords, DefineFeature._MAX20TEXTURES))
            {
                maxTextureCount = MaxTextureCount.Twenty;
            }
            else if (HasFeature(keywords, DefineFeature._MAX24TEXTURES))
            {
                maxTextureCount = MaxTextureCount.TwentyFour;
            }
            else if (HasFeature(keywords, DefineFeature._MAX28TEXTURES))
            {
                maxTextureCount = MaxTextureCount.TwentyEight;
            }
            else if (HasFeature(keywords, DefineFeature._MAX32TEXTURES))
            {
                maxTextureCount = MaxTextureCount.ThirtyTwo;
            }
            else
            {
                maxTextureCount = MaxTextureCount.Sixteen;
            }

            disableHeightBlend = HasFeature(keywords, DefineFeature._DISABLEHEIGHTBLENDING);

            lightingMode = LightingMode.Automatic;
            if (HasFeature(keywords, DefineFeature._USESPECULARWORKFLOW))
            {
                lightingMode = LightingMode.StandardShaderNoSheen;
            }
            else if (HasFeature(keywords, DefineFeature._BDRF1))
            {
                lightingMode = LightingMode.StandardShader;
            }
            else if (HasFeature(keywords, DefineFeature._BDRF2))
            {
                lightingMode = LightingMode.Simplified;
            }
            else if (HasFeature(keywords, DefineFeature._BDRF3))
            {
                lightingMode = LightingMode.BlinnPhong;
            }
            else if (HasFeature(keywords, DefineFeature._BDRFLAMBERT))
            {
                lightingMode = LightingMode.Lambert;
            }

            perTexUVScale        = (HasFeature(keywords, DefineFeature._PERTEXUVSCALEOFFSET));
            perTexUVRotation     = (HasFeature(keywords, DefineFeature._PERTEXUVROTATION));
            perTexInterpContrast = HasFeature(keywords, DefineFeature._PERTEXINTERPCONTRAST);
            perTexBrightness     = HasFeature(keywords, DefineFeature._PERTEXBRIGHTNESS);
            perTexContrast       = HasFeature(keywords, DefineFeature._PERTEXCONTRAST);
            perTexSaturation     = HasFeature(keywords, DefineFeature._PERTEXSATURATION);
            perTexAOStr          = (HasFeature(keywords, DefineFeature._PERTEXAOSTR));
            perTexMetallic       = (HasFeature(keywords, DefineFeature._PERTEXMETALLIC));
            perTexNormStr        = (HasFeature(keywords, DefineFeature._PERTEXNORMSTR));
            perTexSmoothStr      = (HasFeature(keywords, DefineFeature._PERTEXSMOOTHSTR));
            perTexTint           = (HasFeature(keywords, DefineFeature._PERTEXTINT));

            shaderModel = ShaderModel.Automatic;
            if (HasFeature(keywords, DefineFeature._FORCEMODEL46))
            {
                shaderModel = ShaderModel.Force46;
            }
            if (HasFeature(keywords, DefineFeature._FORCEMODEL50))
            {
                shaderModel = ShaderModel.Force50;
            }

            debugOutput = DebugOutput.None;
            if (HasFeature(keywords, DefineFeature._DEBUG_OUTPUT_ALBEDO))
            {
                debugOutput = DebugOutput.Albedo;
            }
            else if (HasFeature(keywords, DefineFeature._DEBUG_OUTPUT_HEIGHT))
            {
                debugOutput = DebugOutput.Height;
            }
            else if (HasFeature(keywords, DefineFeature._DEBUG_OUTPUT_NORMAL))
            {
                debugOutput = DebugOutput.Normal;
            }
            else if (HasFeature(keywords, DefineFeature._DEBUG_OUTPUT_SMOOTHNESS))
            {
                debugOutput = DebugOutput.Smoothness;
            }
            else if (HasFeature(keywords, DefineFeature._DEBUG_OUTPUT_METAL))
            {
                debugOutput = DebugOutput.Metallic;
            }
            else if (HasFeature(keywords, DefineFeature._DEBUG_OUTPUT_AO))
            {
                debugOutput = DebugOutput.AO;
            }
            else if (HasFeature(keywords, DefineFeature._DEBUG_OUTPUT_EMISSION))
            {
                debugOutput = DebugOutput.Emission;
            }
#if __MICROSPLAT_PROCTEX__
            else if (HasFeature(keywords, DefineFeature._DEBUG_OUTPUT_SPLAT0))
            {
                debugOutput = DebugOutput.ProceduralSplatOutput0;
            }
            else if (HasFeature(keywords, DefineFeature._DEBUG_OUTPUT_SPLAT1))
            {
                debugOutput = DebugOutput.ProceduralSplatOutput1;
            }
            else if (HasFeature(keywords, DefineFeature._DEBUG_OUTPUT_SPLAT2))
            {
                debugOutput = DebugOutput.ProceduralSplatOutput2;
            }
            else if (HasFeature(keywords, DefineFeature._DEBUG_OUTPUT_SPLAT3))
            {
                debugOutput = DebugOutput.ProceduralSplatOutput3;
            }
            else if (HasFeature(keywords, DefineFeature._DEBUG_OUTPUT_SPLAT4))
            {
                debugOutput = DebugOutput.ProceduralSplatOutput4;
            }
            else if (HasFeature(keywords, DefineFeature._DEBUG_OUTPUT_SPLAT5))
            {
                debugOutput = DebugOutput.ProceduralSplatOutput5;
            }
            else if (HasFeature(keywords, DefineFeature._DEBUG_OUTPUT_SPLAT6))
            {
                debugOutput = DebugOutput.ProceduralSplatOutput6;
            }
            else if (HasFeature(keywords, DefineFeature._DEBUG_OUTPUT_SPLAT7))
            {
                debugOutput = DebugOutput.ProceduralSplatOutput7;
            }
            else if (HasFeature(keywords, DefineFeature._DEBUG_OUTPUT_SPLAT0A))
            {
                debugOutput = DebugOutput.ProceduralSplatOutput0A;
            }
            else if (HasFeature(keywords, DefineFeature._DEBUG_OUTPUT_SPLAT1A))
            {
                debugOutput = DebugOutput.ProceduralSplatOutput1A;
            }
            else if (HasFeature(keywords, DefineFeature._DEBUG_OUTPUT_SPLAT2A))
            {
                debugOutput = DebugOutput.ProceduralSplatOutput2A;
            }
            else if (HasFeature(keywords, DefineFeature._DEBUG_OUTPUT_SPLAT3A))
            {
                debugOutput = DebugOutput.ProceduralSplatOutput3A;
            }
            else if (HasFeature(keywords, DefineFeature._DEBUG_OUTPUT_SPLAT4A))
            {
                debugOutput = DebugOutput.ProceduralSplatOutput4A;
            }
            else if (HasFeature(keywords, DefineFeature._DEBUG_OUTPUT_SPLAT5A))
            {
                debugOutput = DebugOutput.ProceduralSplatOutput5A;
            }
            else if (HasFeature(keywords, DefineFeature._DEBUG_OUTPUT_SPLAT6A))
            {
                debugOutput = DebugOutput.ProceduralSplatOutput6A;
            }
            else if (HasFeature(keywords, DefineFeature._DEBUG_OUTPUT_SPLAT7A))
            {
                debugOutput = DebugOutput.ProceduralSplatOutput7A;
            }
#endif
        }
        private static void CreateUV(ref MeshData m, UVMode uvMode)
        {
            for (int i = 0; i < m.UV.Count; i++)
            {
                Vector3 n = m.Normals[i];
                Vector2 textureCoordinates = new Vector2();

                switch (uvMode)
                {
                case UVMode.Sphere:
                {
                    textureCoordinates.x = Mathf.Atan2(n.x, n.z) / (-2f * Mathf.PI);
                    textureCoordinates.y = Mathf.Asin(n.y) / Mathf.PI + 0.5f;
                }
                break;

                case UVMode.Panorama:
                {
                    textureCoordinates.x = Mathf.Atan2(n.x, n.z) / (-2f * Mathf.PI);
                    textureCoordinates.y = Mathf.Clamp(n.y, 0.0f, 1.0f);
                }
                break;

                case UVMode.PanoramaMirrorDown:
                {
                    textureCoordinates.x = Mathf.Atan2(n.x, n.z) / (-2f * Mathf.PI);
                    textureCoordinates.y = Mathf.Abs(n.y);
                }
                break;

                case UVMode.Dome:
                {
                    float y   = (n.y < 0.0f ? 0.0f : n.y);
                    float r   = Mathf.Atan2(Mathf.Sqrt(n.x * n.x + n.z * n.z), y) / Mathf.PI;
                    float phi = Mathf.Atan2(n.z, n.x);
                    textureCoordinates.x = (r * Mathf.Cos(phi)) + 0.5f;
                    textureCoordinates.y = (r * Mathf.Sin(phi)) + 0.5f;
                }
                break;

                case UVMode.DomeMirrorDown:
                {
                    float y   = Math.Abs(n.y);
                    float r   = Mathf.Atan2(Mathf.Sqrt(n.x * n.x + n.z * n.z), y) / Mathf.PI;
                    float phi = Mathf.Atan2(n.z, n.x);
                    textureCoordinates.x = (r * Mathf.Cos(phi)) + 0.5f;
                    textureCoordinates.y = (r * Mathf.Sin(phi)) + 0.5f;
                }
                break;

                case UVMode.DomeDouble:
                {
                    float y   = Math.Abs(n.y);
                    float r   = Mathf.Atan2(Mathf.Sqrt(n.x * n.x + n.z * n.z), y) / Mathf.PI;
                    float phi = Mathf.Atan2(n.z, n.x);
                    textureCoordinates.x = (r * Mathf.Cos(phi)) + 0.5f;
                    textureCoordinates.y = (r * Mathf.Sin(phi)) + 0.5f;
                    if (n.y >= 0.0f)
                    {
                        textureCoordinates.x = (textureCoordinates.x + 1.0f) * 0.5f;
                    }
                    else
                    {
                        textureCoordinates.x *= 0.5f;
                    }
                }
                break;

                case UVMode.FishEyeMirrored:
                {
                    float z   = Math.Abs(n.z);
                    float r   = Mathf.Atan2(Mathf.Sqrt(n.x * n.x + n.y * n.y), z) / Mathf.PI;
                    float phi = Mathf.Atan2(n.y, n.x);
                    textureCoordinates.x = (r * Mathf.Cos(phi)) + 0.5f;
                    textureCoordinates.y = (r * Mathf.Sin(phi)) + 0.5f;
                }
                break;

                case UVMode.FishEye360:
                {
                    float r = Mathf.Atan2(Mathf.Sqrt(n.x * n.x + n.y * n.y), n.z) / (Mathf.PI * 2.0f);

                    // TODO: The fish eye gets distorted at these coordinates, not sure if this is fixable or not...

                    /*
                     * const float t = 0.35f;
                     * if (n.x >= -t && n.x <= t && n.y >= -t && n.y <= t)
                     * {
                     *  //Debug.LogFormat("BOO: n={0}, t={1}, r={2}, phi={3}", n, textureCoordinates, r, phi);
                     *  //textureCoordinates.x = 0.0f;
                     *  //textureCoordinates.y = 0.0f;
                     * }
                     */

                    float phi = Mathf.Atan2(n.y, n.x);
                    textureCoordinates.x = (r * Mathf.Cos(phi)) + 0.5f;
                    textureCoordinates.y = (r * Mathf.Sin(phi)) + 0.5f;
                }
                break;

                default: throw new ArgumentException("Invalid uv mode: " + uvMode);
                }

                textureCoordinates.x = WrapFloat(textureCoordinates.x);
                textureCoordinates.y = WrapFloat(textureCoordinates.y);
                m.UV[i] = textureCoordinates;
            }
        }
Beispiel #14
0
	/**
	 * If dragRect is null, the selected UV array will be derived using the selected ProBuilder faces.
	 * If it ain't null, selected UVs will be set to the UV coordinates contained within the drag rect.
	 */
	void RefreshUVCoordinates(Rect? dragRect, bool isClick)
	{	
		if(editor == null || selection == null) return;

		#if PB_DEBUG
		profiler.BeginSample("RefreshUVCoordinates");
		#endif

		// Collect drawables
		uvs_canvas_space = new Vector2[selection.Length][];

		// Convert dragrect from Unity GUI space to uv_gui_space
		pb_Bounds2D dragBounds = dragRect != null ? 
			new pb_Bounds2D( GUIToCanvasPoint(((Rect)dragRect).center), new Vector2( ((Rect)dragRect).width, ((Rect)dragRect).height) / uvGraphScale ) :
			new pb_Bounds2D( Vector2.zero, Vector2.zero );

		selectedUVCount   = editor.selectedVertexCount;
		selectedFaceCount = editor.selectedFaceCount;
		// selectedEdgeCount = editor.selectedEdgeCount;

		for(int i = 0; i < selection.Length; i++)
		{
			pb_Object pb = selection[i];

			Vector2[] mshUV = GetUVs(pb, channel);
			int len = mshUV.Length;

			uvs_canvas_space[i] = new Vector2[len];

			for(int j = 0; j < len; j++)
				uvs_canvas_space[i][j] = pb_Handle_Utility.UVToGUIPoint(mshUV[j], uvGridSize);

			// this should probably be separate from RefreshUVCoordinates
			if(dragRect != null)
			{	
				switch(selectionMode)
				{
					case SelectMode.Vertex:
						List<int> selectedTris = new List<int>(pb.SelectedTriangles);

						for(int j = 0; j < len; j++)
						{
							if( dragBounds.ContainsPoint( uvs_canvas_space[i][j] ) )
							{
								int indx = selectedTris.IndexOf(j);

								if(indx > -1)
									selectedTris.RemoveAt(indx);
								else
									selectedTris.Add(j);

								// if this is a click, only do one thing per-click
								if(isClick)
									break;
							}
						}

						pb.SetSelectedTriangles(selectedTris.ToArray());
						break;

					case SelectMode.Edge:
						List<pb_Edge> selectedEdges = new List<pb_Edge>(pb.SelectedEdges);

						for(int n = 0; n < pb.faces.Length; n++)
						{
							for(int p = 0; p < pb.faces[n].edges.Length; p++)
							{
								pb_Edge edge = pb.faces[n].edges[p];

								if( dragBounds.IntersectsLineSegment( uvs_canvas_space[i][edge.x],  uvs_canvas_space[i][edge.y]) )	
								{
									if(!selectedEdges.Contains(edge))
										selectedEdges.Add( edge );
									else
										selectedEdges.Remove( edge );
								}
							}
						}

						pb.SetSelectedEdges(selectedEdges.ToArray());
						break;
				
					/**
					 * Check if any of the faces intersect with the mousedrag rect.
					 */
					case SelectMode.Face:

						HashSet<int> selectedFaces = new HashSet<int>(selection[i].SelectedFaceIndices);
						for(int n = 0; n < pb.faces.Length; n++)
						{
							// Vector2[] uvs = pbUtil.ValuesWithIndices(, pb.faces[n].distinctIndices);
							int[] distinctIndices = pb.faces[n].distinctIndices;

							bool allPointsContained = true;

							for(int t = 0; t < distinctIndices.Length; t++)
							{
								if( ! dragBounds.ContainsPoint(uvs_canvas_space[i][distinctIndices[t]]) )
								{
									allPointsContained = false;
									break;
								}
							}

							// // if(dragBounds.Intersects(faceBounds))
							// for(int t = 0; t < uvs.Length; t++)
							// {
							// 	if(!dragBounds.ContainsPoint(uvs[t]))
							// 	{
							// 		allPointsContained = false;
							// 		break;
							// 	}
							// }

							if(allPointsContained)
							{
								if( selectedFaces.Contains(n) )
										selectedFaces.Remove(n);
									else
										selectedFaces.Add(n);
							}
						}
						selection[i].SetSelectedFaces(selectedFaces.ToArray());

						break;
				}

				editor.UpdateSelection(false);
				SceneView.RepaintAll();
			}
		}

		// figure out what the mode of selected faces is
		if(editor.selectedFaceCount > 0)
		{
			// @todo write a more effecient method for this
			List<bool> manual = new List<bool>();
			for(int i = 0; i < selection.Length; i++)
				manual.AddRange( selection[i].SelectedFaces.Select(x => x.manualUV).ToList() );
			int c = manual.Distinct().Count();
			if(c > 1)
				mode = UVMode.Mixed;
			else if (c > 0)
				mode = manual[0] ? UVMode.Manual : UVMode.Auto;
		}
		else
		{
			mode = UVMode.Manual;
		}

		editor.GetFirstSelectedMaterial(ref preview_material);

		selected_canvas_bounds = CanvasSelectionBounds();
		handlePosition_canvas = selected_canvas_bounds.center - handlePosition_offset;

		#if PB_DEBUG
		profiler.EndSample();
		#endif
	}