Home | History | Annotate | Download | only in shadow
      1 /*
      2  * Copyright (C) 2018 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.view.shadow;
     18 
     19 
     20 /**
     21  * Model for spot shadow rendering. Assumes single light, single object.
     22  */
     23 class SpotShadowConfig {
     24     private final int mWidth;
     25     private final int mHeight;
     26 
     27     // No need to be final but making it immutable for now.
     28     private final int mLightRadius;
     29     private final int mLightSourcePoints;
     30 
     31     // No need to be final but making it immutable for now.
     32     private final int mRays;
     33     private final int mLayers;
     34 
     35     // No need to be final but making it immutable for now.
     36     private final float[] mPoly;
     37     private final int mPolyLength;
     38 
     39     private float[] mLightCoord;
     40 
     41     private final float mShadowStrength;
     42 
     43     private SpotShadowConfig(SpotShadowConfig.Builder builder) {
     44         mWidth = builder.mWidth;
     45         mHeight = builder.mHeight;
     46         mLightRadius = builder.mLightRadius;
     47         mLightSourcePoints = builder.mLightSourcePoints;
     48         mRays = builder.mRays;
     49         mLayers = builder.mLayers;
     50         mPoly = builder.mPoly;
     51         mPolyLength = builder.mPolyLength;
     52 
     53         mLightCoord = new float[3];
     54         mLightCoord[0] = builder.mLightX;
     55         mLightCoord[1] = builder.mLightY;
     56         mLightCoord[2] = builder.mLightHeight;
     57         mShadowStrength = builder.mShadowStrength;
     58     }
     59 
     60     /**
     61      * World width / height
     62      */
     63     public int getWidth() {
     64         return mWidth;
     65     }
     66 
     67     public int getHeight() {
     68         return mHeight;
     69     }
     70 
     71     /**
     72      * @return number of light source points to ray trace
     73      */
     74     public int getLightSourcePoints() {
     75         return mLightSourcePoints;
     76     }
     77 
     78     /**
     79      * @return size of the light source radius (light source is always generated as a circular shape)
     80      */
     81     public int getLightRadius() {
     82         return mLightRadius;
     83     }
     84 
     85     /**
     86      * @return object that casts shadow. xyz coordinates.
     87      */
     88     public float[] getPoly() {
     89         return mPoly;
     90     }
     91 
     92     /**
     93      * @return # of vertices in the object {@link #getPoly()} that casts shadow.
     94      */
     95     public int getPolyLength() {
     96         return mPolyLength;
     97     }
     98 
     99     /**
    100      * @return number of rays to use in raytracing. It determines the accuracy of outline (bounds) of
    101      * the shadow.
    102      */
    103     public int getRays() {
    104         return mRays;
    105     }
    106 
    107     /**
    108      * @return number of layers. It determines the intensity of pen-umbra
    109      */
    110     public int getLayers() {
    111         return mLayers;
    112     }
    113 
    114     /**
    115      * Update the light source coord.
    116      * @param x - x in {@link #getWidth()} coordinate
    117      * @param y - y in {@link #getHeight()} coordinate
    118      */
    119     public void setLightCoord(float x, float y) {
    120         mLightCoord[0] = x;
    121         mLightCoord[1] = y;
    122     }
    123 
    124     /**
    125      * @return shadow intensity from 0 to 1
    126      */
    127     public float getShadowStrength() {
    128         return mShadowStrength;
    129     }
    130 
    131     public float[] getLightCoord() {
    132         return mLightCoord;
    133     }
    134 
    135     public static class Builder {
    136 
    137         private int mWidth;
    138         private int mHeight;
    139 
    140         // No need to be final but making it immutable for now.
    141         private int mLightRadius;
    142         private int mLightSourcePoints;
    143 
    144         // No need to be final but making it immutable for now.
    145         private int mRays;
    146         private int mLayers;
    147 
    148         // No need to be final but making it immutable for now.
    149         private float[] mPoly;
    150         private int mPolyLength;
    151 
    152         private float mLightX;
    153         private float mLightY;
    154         private float mLightHeight;
    155 
    156         private float mShadowStrength;
    157 
    158         /**
    159          * @param shadowStrength from 0 to 1
    160          */
    161         public Builder setShadowStrength(float shadowStrength) {
    162             this.mShadowStrength = shadowStrength;
    163             return this;
    164         }
    165 
    166         public Builder setSize(int width, int height) {
    167             mWidth = width;
    168             mHeight = height;
    169             return this;
    170         }
    171 
    172         public Builder setLightRadius(int mLightRadius) {
    173             this.mLightRadius = mLightRadius;
    174             return this;
    175         }
    176 
    177         public Builder setLightSourcePoints(int mLightSourcePoints) {
    178             this.mLightSourcePoints = mLightSourcePoints;
    179             return this;
    180         }
    181 
    182         public Builder setRays(int mRays) {
    183             this.mRays = mRays;
    184             return this;
    185         }
    186 
    187         public Builder setLayers(int mLayers) {
    188             this.mLayers = mLayers;
    189             return this;
    190         }
    191 
    192         public Builder setPolygon(float[] poly, int polyLength) {
    193             this.mPoly = poly;
    194             this.mPolyLength = polyLength;
    195             return this;
    196         }
    197 
    198         public Builder setLightCoord(float lightX, float lightY, float lightHeight) {
    199             this.mLightX = lightX;
    200             this.mLightY = lightY;
    201             this.mLightHeight = lightHeight;
    202             return this;
    203         }
    204 
    205         public SpotShadowConfig build() {
    206             return new SpotShadowConfig(this);
    207         }
    208     }
    209 
    210 }